TextureDatabase.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/Core.h"
  30. #include "../../Include/RmlUi/Core/StringUtilities.h"
  31. #include "../../Include/RmlUi/Core/SystemInterface.h"
  32. #include "TextureResource.h"
  33. namespace Rml {
  34. static TextureDatabase* texture_database = nullptr;
  35. TextureDatabase::TextureDatabase()
  36. {
  37. RMLUI_ASSERT(texture_database == nullptr);
  38. texture_database = this;
  39. }
  40. TextureDatabase::~TextureDatabase()
  41. {
  42. RMLUI_ASSERT(texture_database == this);
  43. #ifdef RMLUI_DEBUG
  44. // All textures not owned by the database should have been released at this point.
  45. int num_leaks_file = 0;
  46. for (auto& texture : textures)
  47. num_leaks_file += (texture.second.use_count() > 1);
  48. const int num_leaks_callback = (int)callback_textures.size();
  49. const int total_num_leaks = num_leaks_file + num_leaks_callback;
  50. if (total_num_leaks > 0)
  51. {
  52. Log::Message(Log::LT_ERROR, "Textures leaked during shutdown. Total: %d From file: %d Generated: %d.", total_num_leaks, num_leaks_file,
  53. num_leaks_callback);
  54. }
  55. #endif
  56. texture_database = nullptr;
  57. }
  58. void TextureDatabase::Initialise()
  59. {
  60. new TextureDatabase();
  61. }
  62. void TextureDatabase::Shutdown()
  63. {
  64. delete texture_database;
  65. }
  66. SharedPtr<TextureResource> TextureDatabase::Fetch(const String& source, const String& source_directory)
  67. {
  68. String path;
  69. if (source.size() > 0 && source[0] == '?')
  70. path = source;
  71. else
  72. GetSystemInterface()->JoinPath(path, StringUtilities::Replace(source_directory, '|', ':'), source);
  73. auto iterator = texture_database->textures.find(path);
  74. if (iterator != texture_database->textures.end())
  75. return iterator->second;
  76. auto resource = MakeShared<TextureResource>();
  77. resource->Set(path);
  78. texture_database->textures[path] = resource;
  79. return resource;
  80. }
  81. void TextureDatabase::AddCallbackTexture(TextureResource* texture)
  82. {
  83. if (texture_database)
  84. texture_database->callback_textures.insert(texture);
  85. }
  86. void TextureDatabase::RemoveCallbackTexture(TextureResource* texture)
  87. {
  88. if (texture_database)
  89. texture_database->callback_textures.erase(texture);
  90. }
  91. StringList TextureDatabase::GetSourceList()
  92. {
  93. StringList result;
  94. if (texture_database)
  95. {
  96. result.reserve(texture_database->textures.size());
  97. for (const auto& pair : texture_database->textures)
  98. result.push_back(pair.first);
  99. }
  100. return result;
  101. }
  102. void TextureDatabase::ReleaseTextures()
  103. {
  104. if (texture_database)
  105. {
  106. for (const auto& texture : texture_database->textures)
  107. texture.second->Release();
  108. for (const auto& texture : texture_database->callback_textures)
  109. texture->Release();
  110. }
  111. }
  112. bool TextureDatabase::AllTexturesReleased()
  113. {
  114. if (texture_database)
  115. {
  116. for (const auto& texture : texture_database->textures)
  117. if (!texture.second->IsLoaded())
  118. return false;
  119. for (const auto& texture : texture_database->callback_textures)
  120. if (!texture->IsLoaded())
  121. return false;
  122. }
  123. return true;
  124. }
  125. } // namespace Rml