class TextureResourceHandler
Overview
This class is a workaround to crashes and memory leaks caused by not reserving and freeing opengl textures from the same thread.
Textures uploaded from the same image data within the same share scope are shared (see acquireSharedTexture()), so each texture name is reference counted and only deleted in OpenGL once its last user releases it.
class TextureResourceHandler { public: // methods static TextureResourceHandler& Instance(); texture_name_t generateTextureID(); std::optional<texture_name_t> acquireSharedTexture(const SharedTextureKey& key); void registerSharedTexture(texture_name_t name, const SharedTextureKey& key, const mrpt::img::CImage& rgb, const mrpt::img::CImage* alpha); void releaseTextureID(unsigned int texName); };
Methods
texture_name_t generateTextureID()
Return textureName.
std::optional<texture_name_t> acquireSharedTexture(const SharedTextureKey& key)
If a texture was already uploaded with this key, adds one user to it and returns its name.
void registerSharedTexture( texture_name_t name, const SharedTextureKey& key, const mrpt::img::CImage& rgb, const mrpt::img::CImage* alpha )
Makes a texture just uploaded from these images available to later users of the same key.
The images (shallow copies) are kept alive, so their pixel buffers cannot be reused by other images meanwhile.