TextureDatabase.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. Vector2i dimensions;
  111. if (TextureHandle handle = render_interface->LoadTexture(dimensions, source))
  112. {
  113. TextureFileIndex result = TextureFileIndex(texture_list.size());
  114. texture_map[source] = result;
  115. texture_list.push_back(FileTextureEntry{handle, dimensions});
  116. return result;
  117. }
  118. return TextureFileIndex::Invalid;
  119. }
  120. TextureHandle FileTextureDatabase::GetHandle(TextureFileIndex index) const
  121. {
  122. RMLUI_ASSERT(size_t(index) < texture_list.size());
  123. return texture_list[size_t(index)].texture_handle;
  124. }
  125. Vector2i FileTextureDatabase::GetDimensions(TextureFileIndex index) const
  126. {
  127. RMLUI_ASSERT(size_t(index) < texture_list.size());
  128. return texture_list[size_t(index)].dimensions;
  129. }
  130. void FileTextureDatabase::GetSourceList(StringList& source_list) const
  131. {
  132. source_list.reserve(source_list.size() + texture_list.size());
  133. for (const auto& texture : texture_map)
  134. source_list.push_back(texture.first);
  135. }
  136. void FileTextureDatabase::ReleaseAllTextures(RenderInterface* render_interface)
  137. {
  138. for (FileTextureEntry& texture : texture_list)
  139. {
  140. if (texture.texture_handle)
  141. {
  142. render_interface->ReleaseTexture(texture.texture_handle);
  143. texture.texture_handle = {};
  144. }
  145. }
  146. }
  147. } // namespace Rml