TextureDatabase.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "TextureDatabase.h"
  29. #include "../../Include/RmlUi/Core/Log.h"
  30. #include "../../Include/RmlUi/Core/RenderInterface.h"
  31. namespace Rml {
  32. CallbackTextureDatabase::CallbackTextureDatabase()
  33. {
  34. constexpr size_t reserve_callback_textures = 30;
  35. texture_list.reserve(reserve_callback_textures);
  36. }
  37. CallbackTextureDatabase::~CallbackTextureDatabase()
  38. {
  39. if (!texture_list.empty())
  40. {
  41. Log::Message(Log::LT_ERROR, "TextureDatabase destroyed with outstanding callback textures. Will likely result in memory corruption.");
  42. RMLUI_ERROR;
  43. }
  44. }
  45. StableVectorIndex CallbackTextureDatabase::CreateTexture(CallbackTextureFunction&& callback)
  46. {
  47. RMLUI_ASSERT(callback);
  48. return texture_list.insert(CallbackTextureEntry{std::move(callback), TextureHandle(), Vector2i()});
  49. }
  50. void CallbackTextureDatabase::ReleaseTexture(RenderInterface* render_interface, StableVectorIndex callback_index)
  51. {
  52. CallbackTextureEntry& data = texture_list[callback_index];
  53. if (data.texture_handle)
  54. render_interface->ReleaseTexture(data.texture_handle);
  55. texture_list.erase(callback_index);
  56. }
  57. Vector2i CallbackTextureDatabase::GetDimensions(RenderManager* render_manager, RenderInterface* render_interface, StableVectorIndex callback_index)
  58. {
  59. return EnsureLoaded(render_manager, render_interface, callback_index).dimensions;
  60. }
  61. TextureHandle CallbackTextureDatabase::GetHandle(RenderManager* render_manager, RenderInterface* render_interface, StableVectorIndex callback_index)
  62. {
  63. return EnsureLoaded(render_manager, render_interface, callback_index).texture_handle;
  64. }
  65. auto CallbackTextureDatabase::EnsureLoaded(RenderManager* render_manager, RenderInterface* render_interface, StableVectorIndex callback_index)
  66. -> CallbackTextureEntry&
  67. {
  68. CallbackTextureEntry& data = texture_list[callback_index];
  69. if (!data.texture_handle && !data.load_failed)
  70. {
  71. if (!data.callback(CallbackTextureInterface(*render_manager, *render_interface, data.texture_handle, data.dimensions)))
  72. {
  73. data.load_failed = true;
  74. data.texture_handle = {};
  75. data.dimensions = {};
  76. }
  77. }
  78. return data;
  79. }
  80. size_t CallbackTextureDatabase::size() const
  81. {
  82. return texture_list.size();
  83. }
  84. void CallbackTextureDatabase::ReleaseAllTextures(RenderInterface* render_interface)
  85. {
  86. texture_list.for_each([render_interface](CallbackTextureEntry& texture) {
  87. if (texture.texture_handle)
  88. {
  89. render_interface->ReleaseTexture(texture.texture_handle);
  90. texture.texture_handle = {};
  91. texture.dimensions = {};
  92. }
  93. });
  94. }
  95. FileTextureDatabase::FileTextureDatabase() {}
  96. FileTextureDatabase::~FileTextureDatabase()
  97. {
  98. #ifdef RMLUI_DEBUG
  99. for (const FileTextureEntry& texture : texture_list)
  100. {
  101. RMLUI_ASSERTMSG(!texture.texture_handle,
  102. "TextureDatabase destroyed without releasing all file textures first. Ensure that 'ReleaseAllTextures' is called before destruction.");
  103. }
  104. #endif
  105. }
  106. TextureFileIndex FileTextureDatabase::InsertTexture(const String& source)
  107. {
  108. auto it = texture_map.find(source);
  109. if (it != texture_map.end())
  110. return it->second;
  111. // The texture is not yet loaded from the render interface. That is deferred until the texture is needed, such as when it becomes visible.
  112. const auto index = TextureFileIndex(texture_list.size());
  113. texture_map[source] = index;
  114. texture_list.push_back({});
  115. return index;
  116. }
  117. FileTextureDatabase::FileTextureEntry FileTextureDatabase::LoadTextureEntry(RenderInterface* render_interface, const String& source)
  118. {
  119. FileTextureEntry result = {};
  120. result.texture_handle = render_interface->LoadTexture(result.dimensions, source);
  121. if (!result.texture_handle)
  122. {
  123. result.load_texture_failed = true;
  124. Rml::Log::Message(Rml::Log::LT_WARNING, "Could not load texture: %s", source.c_str());
  125. }
  126. return result;
  127. }
  128. FileTextureDatabase::FileTextureEntry& FileTextureDatabase::EnsureLoaded(RenderInterface* render_interface, TextureFileIndex index)
  129. {
  130. FileTextureEntry& entry = texture_list[size_t(index)];
  131. if (!entry.texture_handle)
  132. {
  133. auto it = std::find_if(texture_map.begin(), texture_map.end(), [index](const auto& pair) { return pair.second == index; });
  134. RMLUI_ASSERT(it != texture_map.end());
  135. const String& source = it->first;
  136. if (!entry.load_texture_failed)
  137. entry = LoadTextureEntry(render_interface, source);
  138. }
  139. return entry;
  140. }
  141. TextureHandle FileTextureDatabase::GetHandle(RenderInterface* render_interface, TextureFileIndex index)
  142. {
  143. RMLUI_ASSERT(size_t(index) < texture_list.size());
  144. return EnsureLoaded(render_interface, index).texture_handle;
  145. }
  146. Vector2i FileTextureDatabase::GetDimensions(RenderInterface* render_interface, TextureFileIndex index)
  147. {
  148. RMLUI_ASSERT(size_t(index) < texture_list.size());
  149. return EnsureLoaded(render_interface, index).dimensions;
  150. }
  151. void FileTextureDatabase::GetSourceList(StringList& source_list) const
  152. {
  153. source_list.reserve(source_list.size() + texture_list.size());
  154. for (const auto& texture : texture_map)
  155. source_list.push_back(texture.first);
  156. }
  157. bool FileTextureDatabase::ReleaseTexture(RenderInterface* render_interface, const String& source)
  158. {
  159. auto it = texture_map.find(source);
  160. if (it == texture_map.end())
  161. return false;
  162. FileTextureEntry& texture = texture_list[size_t(it->second)];
  163. if (texture.texture_handle)
  164. {
  165. render_interface->ReleaseTexture(texture.texture_handle);
  166. texture = {};
  167. return true;
  168. }
  169. return false;
  170. }
  171. void FileTextureDatabase::ReleaseAllTextures(RenderInterface* render_interface)
  172. {
  173. for (FileTextureEntry& texture : texture_list)
  174. {
  175. if (texture.texture_handle)
  176. {
  177. render_interface->ReleaseTexture(texture.texture_handle);
  178. texture = {};
  179. }
  180. }
  181. }
  182. } // namespace Rml