TextureDatabase.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 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 "TextureResource.h"
  30. #include "../../Include/RmlUi/Core/Core.h"
  31. #include "../../Include/RmlUi/Core/StringUtilities.h"
  32. #include "../../Include/RmlUi/Core/SystemInterface.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.",
  53. total_num_leaks, num_leaks_file, 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. // If the requested texture is already in the database, it will be returned with an extra reference count. If not, it
  67. // will be loaded through the application's render interface.
  68. SharedPtr<TextureResource> TextureDatabase::Fetch(const String& source, const String& source_directory)
  69. {
  70. String path;
  71. if (source.size() > 0 && source[0] == '?')
  72. path = source;
  73. else
  74. GetSystemInterface()->JoinPath(path, StringUtilities::Replace(source_directory, '|', ':'), source);
  75. TextureMap::iterator iterator = texture_database->textures.find(path);
  76. if (iterator != texture_database->textures.end())
  77. {
  78. return iterator->second;
  79. }
  80. auto resource = MakeShared<TextureResource>();
  81. resource->Set(path);
  82. texture_database->textures[resource->GetSource()] = resource;
  83. return resource;
  84. }
  85. void TextureDatabase::AddCallbackTexture(TextureResource* texture)
  86. {
  87. if (texture_database)
  88. texture_database->callback_textures.insert(texture);
  89. }
  90. void TextureDatabase::RemoveCallbackTexture(TextureResource* texture)
  91. {
  92. if (texture_database)
  93. texture_database->callback_textures.erase(texture);
  94. }
  95. StringList TextureDatabase::GetSourceList()
  96. {
  97. StringList result;
  98. if (texture_database)
  99. {
  100. result.reserve(texture_database->textures.size());
  101. for (const auto& pair : texture_database->textures)
  102. result.push_back(pair.first);
  103. }
  104. return result;
  105. }
  106. void TextureDatabase::ReleaseTextures(RenderInterface* render_interface)
  107. {
  108. if (texture_database)
  109. {
  110. for (const auto& texture : texture_database->textures)
  111. texture.second->Release(render_interface);
  112. for (const auto& texture : texture_database->callback_textures)
  113. texture->Release(render_interface);
  114. }
  115. }
  116. } // namespace Rml