TextureResource.cpp 4.8 KB

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