easy-gl › RecoverableResource

RecoverableResource

Abstract base class for OpenGL resources that can survive an OpenGL context loss and restoration cycle. Implement this interface to integrate your resource with ResourceRegistry.

The context-loss problem

On mobile platforms and some desktop scenarios, the operating system can destroy the OpenGL context at any time (e.g. app backgrounded on Android). All GPU handles immediately become invalid — any attempt to use them causes undefined behaviour.

RecoverableResource solves this by separating CPU-side data (pixels, shader source, vertex data) from the GPU handle. On context loss, the GPU handle is discarded. On context restoration, the resource recreates the GPU object from its stored CPU data.

Implementing the interface

Store all CPU-side data needed to recreate the GPU resource, then implement the two pure virtual methods:

class MyTexture : public easygl::RecoverableResource
{
public:
    void load(int w, int h, const void* pixels)
    {
        width_ = w;  height_ = h;
        pixels_.assign(
            static_cast<const uint8_t*>(pixels),
            static_cast<const uint8_t*>(pixels) + w * h * 4);
        recreate_gl_resource();
    }

    void release_gl_handle_only() override
    {
        tex_.reset_handle_no_gl(); // discard stale handle — no GL call
    }

    void recreate_gl_resource() override
    {
        tex_ = easygl::Texture::create_2d_rgba8(width_, height_, pixels_.data());
    }

private:
    easygl::Texture tex_;
    int width_ = 0, height_ = 0;
    std::vector<uint8_t> pixels_;
};
Register instances with ResourceRegistry so they are notified automatically when the context is lost and restored.

Method reference

MethodDescription
release_gl_handle_only() [pure virtual]Drop the GPU handle without calling any gl* functions. Called after context loss. Must keep CPU-side data intact.
recreate_gl_resource() [pure virtual]Recreate the GPU resource from stored CPU-side data. Called after context is restored.