TextureResource.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "TextureResource.h"
  29. #include "TextureDatabase.h"
  30. #include "../../Include/RmlUi/Core/Log.h"
  31. #include "../../Include/RmlUi/Core/RenderInterface.h"
  32. #include "../../Include/RmlUi/Core/Profiling.h"
  33. namespace Rml {
  34. namespace Core {
  35. TextureResource::TextureResource()
  36. {
  37. }
  38. TextureResource::~TextureResource()
  39. {
  40. Reset();
  41. }
  42. void TextureResource::Set(const String& _source)
  43. {
  44. Reset();
  45. source = _source;
  46. }
  47. void TextureResource::Set(const String& name, const TextureCallback& callback)
  48. {
  49. Reset();
  50. source = name;
  51. texture_callback = std::make_unique<TextureCallback>(callback);
  52. TextureDatabase::AddCallbackTexture(this);
  53. }
  54. void TextureResource::Reset()
  55. {
  56. Release();
  57. if (texture_callback)
  58. {
  59. TextureDatabase::RemoveCallbackTexture(this);
  60. texture_callback.reset();
  61. }
  62. source.clear();
  63. }
  64. // Returns the resource's underlying texture.
  65. TextureHandle TextureResource::GetHandle(RenderInterface* render_interface)
  66. {
  67. auto texture_iterator = texture_data.find(render_interface);
  68. if (texture_iterator == texture_data.end())
  69. {
  70. Load(render_interface);
  71. texture_iterator = texture_data.find(render_interface);
  72. }
  73. return texture_iterator->second.first;
  74. }
  75. // Returns the dimensions of the resource's texture.
  76. const Vector2i& TextureResource::GetDimensions(RenderInterface* render_interface)
  77. {
  78. auto texture_iterator = texture_data.find(render_interface);
  79. if (texture_iterator == texture_data.end())
  80. {
  81. Load(render_interface);
  82. texture_iterator = texture_data.find(render_interface);
  83. }
  84. return texture_iterator->second.second;
  85. }
  86. // Returns the resource's source.
  87. const String& TextureResource::GetSource() const
  88. {
  89. return source;
  90. }
  91. // Releases the texture's handle.
  92. void TextureResource::Release(RenderInterface* render_interface)
  93. {
  94. if (!render_interface)
  95. {
  96. for (auto& interface_data_pair : texture_data)
  97. {
  98. TextureHandle handle = interface_data_pair.second.first;
  99. if (handle)
  100. interface_data_pair.first->ReleaseTexture(handle);
  101. }
  102. texture_data.clear();
  103. }
  104. else
  105. {
  106. TextureDataMap::iterator texture_iterator = texture_data.find(render_interface);
  107. if (texture_iterator == texture_data.end())
  108. return;
  109. TextureHandle handle = texture_iterator->second.first;
  110. if (handle)
  111. texture_iterator->first->ReleaseTexture(handle);
  112. texture_data.erase(render_interface);
  113. }
  114. }
  115. bool TextureResource::Load(RenderInterface* render_interface)
  116. {
  117. RMLUI_ZoneScoped;
  118. // Generate the texture from the callback function if we have one.
  119. if (texture_callback)
  120. {
  121. Vector2i dimensions;
  122. UniquePtr<const byte[]> data = nullptr;
  123. TextureCallback& callback_fnc = *texture_callback;
  124. if (!callback_fnc(source, data, dimensions) || !data)
  125. {
  126. Log::Message(Log::LT_WARNING, "Failed to generate texture from callback function %s.", source.c_str());
  127. texture_data[render_interface] = TextureData(0, Vector2i(0, 0));
  128. return false;
  129. }
  130. TextureHandle handle;
  131. bool success = render_interface->GenerateTexture(handle, data.get(), dimensions);
  132. if (success)
  133. {
  134. texture_data[render_interface] = TextureData(handle, dimensions);
  135. }
  136. else
  137. {
  138. Log::Message(Log::LT_WARNING, "Failed to generate internal texture %s.", source.c_str());
  139. texture_data[render_interface] = TextureData(0, Vector2i(0, 0));
  140. }
  141. return success;
  142. }
  143. // No callback function, load the texture through the render interface.
  144. TextureHandle handle;
  145. Vector2i dimensions;
  146. if (!render_interface->LoadTexture(handle, dimensions, source))
  147. {
  148. Log::Message(Log::LT_WARNING, "Failed to load texture from %s.", source.c_str());
  149. texture_data[render_interface] = TextureData(0, Vector2i(0, 0));
  150. return false;
  151. }
  152. texture_data[render_interface] = TextureData(handle, dimensions);
  153. return true;
  154. }
  155. }
  156. }