TextureResource.cpp 4.9 KB

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