Skip to content

Commit

Permalink
Fix wrong dimensions stored for callback textures
Browse files Browse the repository at this point in the history
  • Loading branch information
mikke89 committed Sep 22, 2023
1 parent 2b475f6 commit 3fe0a4c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
14 changes: 9 additions & 5 deletions Source/Core/CallbackTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,31 @@ CallbackTextureInterface::CallbackTextureInterface(RenderManager& render_manager
render_interface(render_interface), texture_handle(texture_handle), dimensions(dimensions)
{}

bool CallbackTextureInterface::GenerateTexture(const byte* source, Vector2i dimensions) const
bool CallbackTextureInterface::GenerateTexture(const byte* source, Vector2i new_dimensions) const
{
if (texture_handle)
{
RMLUI_ERRORMSG("Texture already set");
return false;
}
bool result = render_interface.GenerateTexture(texture_handle, source, dimensions);
if (!result)
const bool result = render_interface.GenerateTexture(texture_handle, source, new_dimensions);
if (result)
dimensions = new_dimensions;
else
texture_handle = {};
return result;
}

void CallbackTextureInterface::SaveLayerAsTexture(Vector2i dimensions) const
void CallbackTextureInterface::SaveLayerAsTexture(Vector2i new_dimensions) const
{
if (texture_handle)
{
RMLUI_ERRORMSG("Texture already set");
return;
}
texture_handle = render_interface.SaveLayerAsTexture(dimensions);
texture_handle = render_interface.SaveLayerAsTexture(new_dimensions);
if (texture_handle)
dimensions = new_dimensions;
}

RenderManager& CallbackTextureInterface::GetRenderManager() const
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/RenderManagerAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Vector2i RenderManagerAccess::GetDimensions(RenderManager* render_manager, Textu

Vector2i RenderManagerAccess::GetDimensions(RenderManager* render_manager, StableVectorIndex callback_texture)
{
return render_manager->texture_database->callback_database.GetDimensions(callback_texture);
return render_manager->texture_database->callback_database.GetDimensions(render_manager, render_manager->render_interface, callback_texture);
}

void RenderManagerAccess::Render(RenderManager* render_manager, const Geometry& geometry, Vector2f translation, Texture texture,
Expand Down
32 changes: 21 additions & 11 deletions Source/Core/TextureDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ namespace Rml {

CallbackTextureDatabase::CallbackTextureDatabase()
{
texture_list.reserve(30);
constexpr size_t reserve_callback_textures = 30;
texture_list.reserve(reserve_callback_textures);
}

CallbackTextureDatabase::~CallbackTextureDatabase()
Expand All @@ -49,32 +50,40 @@ CallbackTextureDatabase::~CallbackTextureDatabase()
StableVectorIndex CallbackTextureDatabase::CreateTexture(CallbackTextureFunction&& callback)
{
RMLUI_ASSERT(callback);
return texture_list.insert(CallbackTextureEntry{std::move(callback), TextureHandle{}, Vector2i()});
return texture_list.insert(CallbackTextureEntry{std::move(callback), TextureHandle(), Vector2i()});
}

void CallbackTextureDatabase::ReleaseTexture(RenderInterface* render_interface, StableVectorIndex callback_entry)
void CallbackTextureDatabase::ReleaseTexture(RenderInterface* render_interface, StableVectorIndex callback_index)
{
CallbackTextureEntry& data = texture_list[callback_entry];
CallbackTextureEntry& data = texture_list[callback_index];
if (data.texture_handle)
render_interface->ReleaseTexture(data.texture_handle);
texture_list.erase(callback_entry);
texture_list.erase(callback_index);
}

Vector2i CallbackTextureDatabase::GetDimensions(StableVectorIndex callback_texture) const
Vector2i CallbackTextureDatabase::GetDimensions(RenderManager* render_manager, RenderInterface* render_interface, StableVectorIndex callback_index)
{
return texture_list[callback_texture].dimensions;
return EnsureLoaded(render_manager, render_interface, callback_index).dimensions;
}

TextureHandle CallbackTextureDatabase::GetHandle(RenderManager* render_manager, RenderInterface* render_interface, StableVectorIndex callback_entry)
TextureHandle CallbackTextureDatabase::GetHandle(RenderManager* render_manager, RenderInterface* render_interface, StableVectorIndex callback_index)
{
CallbackTextureEntry& data = texture_list[callback_entry];
return EnsureLoaded(render_manager, render_interface, callback_index).texture_handle;
}

auto CallbackTextureDatabase::EnsureLoaded(RenderManager* render_manager, RenderInterface* render_interface, StableVectorIndex callback_index)
-> CallbackTextureEntry&
{
CallbackTextureEntry& data = texture_list[callback_index];
if (!data.texture_handle)
{
if (!data.callback(CallbackTextureInterface(*render_manager, *render_interface, data.texture_handle, data.dimensions)))
{
data.texture_handle = {};
data.dimensions = {};
}
}

return data.texture_handle;
return data;
}

size_t CallbackTextureDatabase::size() const
Expand All @@ -89,6 +98,7 @@ void CallbackTextureDatabase::ReleaseAllTextures(RenderInterface* render_interfa
{
render_interface->ReleaseTexture(texture.texture_handle);
texture.texture_handle = {};
texture.dimensions = {};
}
});
}
Expand Down
9 changes: 6 additions & 3 deletions Source/Core/TextureDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ class CallbackTextureDatabase : NonCopyMoveable {
~CallbackTextureDatabase();

StableVectorIndex CreateTexture(CallbackTextureFunction&& callback);
void ReleaseTexture(RenderInterface* render_interface, StableVectorIndex callback_entry);
void ReleaseTexture(RenderInterface* render_interface, StableVectorIndex callback_index);

Vector2i GetDimensions(StableVectorIndex callback_texture) const;
TextureHandle GetHandle(RenderManager* render_manager, RenderInterface* render_interface, StableVectorIndex callback_entry);
Vector2i GetDimensions(RenderManager* render_manager, RenderInterface* render_interface, StableVectorIndex callback_index);
TextureHandle GetHandle(RenderManager* render_manager, RenderInterface* render_interface, StableVectorIndex callback_index);

size_t size() const;

Expand All @@ -58,6 +58,9 @@ class CallbackTextureDatabase : NonCopyMoveable {
TextureHandle texture_handle = {};
Vector2i dimensions;
};

CallbackTextureEntry& EnsureLoaded(RenderManager* render_manager, RenderInterface* render_interface, StableVectorIndex callback_index);

StableVector<CallbackTextureEntry> texture_list;
};

Expand Down

0 comments on commit 3fe0a4c

Please sign in to comment.