TextureResource.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 "precompiled.h"
  29. #include "TextureResource.h"
  30. #include "TextureDatabase.h"
  31. #include "FontEngineDefault/FontFaceHandleDefault.h"
  32. #include "../../Include/RmlUi/Core.h"
  33. namespace Rml {
  34. namespace Core {
  35. TextureResource::TextureResource()
  36. {
  37. }
  38. TextureResource::~TextureResource()
  39. {
  40. Release();
  41. }
  42. // Attempts to load a texture from the application into the resource.
  43. bool TextureResource::Load(const String& _source)
  44. {
  45. Release();
  46. source = _source;
  47. return true;
  48. }
  49. // Returns the resource's underlying texture.
  50. TextureHandle TextureResource::GetHandle(RenderInterface* render_interface)
  51. {
  52. auto texture_iterator = texture_data.find(render_interface);
  53. if (texture_iterator == texture_data.end())
  54. {
  55. Load(render_interface);
  56. texture_iterator = texture_data.find(render_interface);
  57. }
  58. return texture_iterator->second.first;
  59. }
  60. // Returns the dimensions of the resource's texture.
  61. const Vector2i& TextureResource::GetDimensions(RenderInterface* render_interface)
  62. {
  63. auto texture_iterator = texture_data.find(render_interface);
  64. if (texture_iterator == texture_data.end())
  65. {
  66. Load(render_interface);
  67. texture_iterator = texture_data.find(render_interface);
  68. }
  69. return texture_iterator->second.second;
  70. }
  71. // Returns the resource's source.
  72. const String& TextureResource::GetSource() const
  73. {
  74. return source;
  75. }
  76. // Releases the texture's handle.
  77. void TextureResource::Release(RenderInterface* render_interface)
  78. {
  79. if (!render_interface)
  80. {
  81. for (auto& interface_data_pair : texture_data)
  82. {
  83. TextureHandle handle = interface_data_pair.second.first;
  84. if (handle)
  85. interface_data_pair.first->ReleaseTexture(handle);
  86. }
  87. texture_data.clear();
  88. }
  89. else
  90. {
  91. TextureDataMap::iterator texture_iterator = texture_data.find(render_interface);
  92. if (texture_iterator == texture_data.end())
  93. return;
  94. TextureHandle handle = texture_iterator->second.first;
  95. if (handle)
  96. texture_iterator->first->ReleaseTexture(handle);
  97. texture_data.erase(render_interface);
  98. }
  99. }
  100. // Attempts to load the texture from the source.
  101. bool TextureResource::Load(RenderInterface* render_interface)
  102. {
  103. RMLUI_ZoneScoped;
  104. // Check for special loader tokens.
  105. if (!source.empty() && source[0] == '?')
  106. {
  107. Vector2i dimensions;
  108. UniquePtr<const byte[]> data = nullptr;
  109. // Find the generation protocol and generate the data accordingly.
  110. String protocol = source.substr(1, source.find("::") - 1);
  111. #ifndef RMLUI_NO_FONT_INTERFACE_DEFAULT
  112. if (protocol == "font")
  113. {
  114. // The requested texture is a font layer.
  115. FontFaceHandleDefault* handle;
  116. FontEffect* layer_id;
  117. int texture_id;
  118. int handle_version;
  119. if (sscanf(source.c_str(), "?font::%p/%p/%d/%d", &handle, &layer_id, &texture_id, &handle_version) == 4)
  120. {
  121. if (!handle->GenerateLayerTexture(data, dimensions, layer_id, texture_id, handle_version))
  122. {
  123. Log::Message(Log::LT_WARNING, "Failed to generate font layer texture %s.", source.c_str());
  124. texture_data[render_interface] = TextureData(0, Vector2i(0, 0));
  125. return false;
  126. }
  127. }
  128. }
  129. #endif
  130. // If texture data was generated, great! Otherwise, fallback to the LoadTexture() code and
  131. // hope the client knows what the hell to do with the question mark in their file name.
  132. if (data)
  133. {
  134. TextureHandle handle;
  135. bool success = render_interface->GenerateTexture(handle, data.get(), dimensions);
  136. if (success)
  137. {
  138. texture_data[render_interface] = TextureData(handle, dimensions);
  139. return true;
  140. }
  141. else
  142. {
  143. Log::Message(Log::LT_WARNING, "Failed to generate internal texture %s.", source.c_str());
  144. texture_data[render_interface] = TextureData(0, Vector2i(0, 0));
  145. return false;
  146. }
  147. }
  148. }
  149. TextureHandle handle;
  150. Vector2i dimensions;
  151. if (!render_interface->LoadTexture(handle, dimensions, source))
  152. {
  153. Log::Message(Log::LT_WARNING, "Failed to load texture from %s.", source.c_str());
  154. texture_data[render_interface] = TextureData(0, Vector2i(0, 0));
  155. return false;
  156. }
  157. texture_data[render_interface] = TextureData(handle, dimensions);
  158. return true;
  159. }
  160. }
  161. }