TextureDatabase.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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)
  70. {
  71. if (!data.callback(CallbackTextureInterface(*render_manager, *render_interface, data.texture_handle, data.dimensions)))
  72. {
  73. data.texture_handle = {};
  74. data.dimensions = {};
  75. }
  76. }
  77. return data;
  78. }
  79. size_t CallbackTextureDatabase::size() const
  80. {
  81. return texture_list.size();
  82. }
  83. void CallbackTextureDatabase::ReleaseAllTextures(RenderInterface* render_interface)
  84. {
  85. texture_list.for_each([render_interface](CallbackTextureEntry& texture) {
  86. if (texture.texture_handle)
  87. {
  88. render_interface->ReleaseTexture(texture.texture_handle);
  89. texture.texture_handle = {};
  90. texture.dimensions = {};
  91. }
  92. });
  93. }
  94. FileTextureDatabase::FileTextureDatabase() {}
  95. FileTextureDatabase::~FileTextureDatabase()
  96. {
  97. #ifdef RMLUI_DEBUG
  98. for (const FileTextureEntry& texture : texture_list)
  99. {
  100. RMLUI_ASSERTMSG(!texture.texture_handle,
  101. "TextureDatabase destroyed without releasing all file textures first. Ensure that 'ReleaseAllTextures' is called before destruction.");
  102. }
  103. #endif
  104. }
  105. TextureFileIndex FileTextureDatabase::LoadTexture(RenderInterface* render_interface, const String& source)
  106. {
  107. auto it = texture_map.find(source);
  108. if (it != texture_map.end())
  109. return it->second;
  110. FileTextureEntry entry = LoadTextureEntry(render_interface, source);
  111. if (!entry.texture_handle)
  112. {
  113. Rml::Log::Message(Rml::Log::LT_WARNING, "Could not load texture: %s", source.c_str());
  114. return TextureFileIndex::Invalid;
  115. }
  116. const auto index = TextureFileIndex(texture_list.size());
  117. texture_map[source] = index;
  118. texture_list.push_back(std::move(entry));
  119. return index;
  120. }
  121. FileTextureDatabase::FileTextureEntry FileTextureDatabase::LoadTextureEntry(RenderInterface* render_interface, const String& source)
  122. {
  123. FileTextureEntry result = {};
  124. result.texture_handle = render_interface->LoadTexture(result.dimensions, source);
  125. return result;
  126. }
  127. FileTextureDatabase::FileTextureEntry& FileTextureDatabase::EnsureLoaded(RenderInterface* render_interface, TextureFileIndex index)
  128. {
  129. FileTextureEntry& entry = texture_list[size_t(index)];
  130. if (!entry.texture_handle)
  131. {
  132. auto it = std::find_if(texture_map.begin(), texture_map.end(), [index](const auto& pair) { return pair.second == index; });
  133. RMLUI_ASSERT(it != texture_map.end());
  134. const String& source = it->first;
  135. entry = LoadTextureEntry(render_interface, source);
  136. }
  137. return entry;
  138. }
  139. TextureHandle FileTextureDatabase::GetHandle(RenderInterface* render_interface, TextureFileIndex index)
  140. {
  141. RMLUI_ASSERT(size_t(index) < texture_list.size());
  142. return EnsureLoaded(render_interface, index).texture_handle;
  143. }
  144. Vector2i FileTextureDatabase::GetDimensions(RenderInterface* render_interface, TextureFileIndex index)
  145. {
  146. RMLUI_ASSERT(size_t(index) < texture_list.size());
  147. return EnsureLoaded(render_interface, index).dimensions;
  148. }
  149. void FileTextureDatabase::GetSourceList(StringList& source_list) const
  150. {
  151. source_list.reserve(source_list.size() + texture_list.size());
  152. for (const auto& texture : texture_map)
  153. source_list.push_back(texture.first);
  154. }
  155. bool FileTextureDatabase::ReleaseTexture(RenderInterface* render_interface, const String& source)
  156. {
  157. auto it = texture_map.find(source);
  158. if (it == texture_map.end())
  159. return false;
  160. FileTextureEntry& texture = texture_list[size_t(it->second)];
  161. if (texture.texture_handle)
  162. {
  163. render_interface->ReleaseTexture(texture.texture_handle);
  164. texture.texture_handle = {};
  165. return true;
  166. }
  167. return false;
  168. }
  169. void FileTextureDatabase::ReleaseAllTextures(RenderInterface* render_interface)
  170. {
  171. for (FileTextureEntry& texture : texture_list)
  172. {
  173. if (texture.texture_handle)
  174. {
  175. render_interface->ReleaseTexture(texture.texture_handle);
  176. texture.texture_handle = {};
  177. }
  178. }
  179. }
  180. } // namespace Rml