TextureResource.cpp 4.7 KB

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