TextureResource.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 "FontFaceHandle.h"
  31. #include "TextureDatabase.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. // Check for special loader tokens.
  104. if (!source.empty() && source[0] == '?')
  105. {
  106. Vector2i dimensions;
  107. bool delete_data = false;
  108. const byte* data = nullptr;
  109. // Find the generation protocol and generate the data accordingly.
  110. String protocol = source.substr(1, source.find("::") - 1);
  111. if (protocol == "font")
  112. {
  113. // The requested texture is a font layer.
  114. delete_data = true;
  115. FontFaceHandle* handle;
  116. FontEffect* layer_id;
  117. int texture_id;
  118. if (sscanf(source.c_str(), "?font::%p/%p/%d", &handle, &layer_id, &texture_id) == 3)
  119. {
  120. handle->GenerateLayerTexture(data,
  121. dimensions,
  122. layer_id,
  123. texture_id);
  124. }
  125. }
  126. // If texture data was generated, great! Otherwise, fallback to the LoadTexture() code and
  127. // hope the client knows what the hell to do with the question mark in their file name.
  128. if (data)
  129. {
  130. TextureHandle handle;
  131. bool success = render_interface->GenerateTexture(handle, data, dimensions);
  132. if (delete_data)
  133. delete[] data;
  134. if (success)
  135. {
  136. texture_data[render_interface] = TextureData(handle, dimensions);
  137. return true;
  138. }
  139. else
  140. {
  141. Log::Message(Log::LT_WARNING, "Failed to generate internal texture %s.", source.c_str());
  142. texture_data[render_interface] = TextureData(0, Vector2i(0, 0));
  143. return false;
  144. }
  145. }
  146. }
  147. TextureHandle handle;
  148. Vector2i dimensions;
  149. if (!render_interface->LoadTexture(handle, dimensions, source))
  150. {
  151. Log::Message(Log::LT_WARNING, "Failed to load texture from %s.", source.c_str());
  152. texture_data[render_interface] = TextureData(0, Vector2i(0, 0));
  153. return false;
  154. }
  155. texture_data[render_interface] = TextureData(handle, dimensions);
  156. return true;
  157. }
  158. }
  159. }