TextureResource.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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-2023 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 "../../Include/RmlUi/Core/Core.h"
  30. #include "../../Include/RmlUi/Core/Log.h"
  31. #include "../../Include/RmlUi/Core/Profiling.h"
  32. #include "../../Include/RmlUi/Core/RenderInterface.h"
  33. #include "TextureDatabase.h"
  34. namespace Rml {
  35. TextureResource::TextureResource() {}
  36. TextureResource::~TextureResource()
  37. {
  38. Reset();
  39. }
  40. void TextureResource::Set(const String& _source)
  41. {
  42. Reset();
  43. source = _source;
  44. }
  45. void TextureResource::Set(const String& name, const TextureCallback& callback)
  46. {
  47. Reset();
  48. source = name;
  49. texture_callback = MakeUnique<TextureCallback>(callback);
  50. TextureDatabase::AddCallbackTexture(this);
  51. }
  52. void TextureResource::Reset()
  53. {
  54. Release();
  55. if (texture_callback)
  56. {
  57. TextureDatabase::RemoveCallbackTexture(this);
  58. texture_callback.reset();
  59. }
  60. source.clear();
  61. }
  62. TextureHandle TextureResource::GetHandle()
  63. {
  64. if (!loaded)
  65. Load();
  66. return handle;
  67. }
  68. Vector2i TextureResource::GetDimensions()
  69. {
  70. if (!loaded)
  71. Load();
  72. return dimensions;
  73. }
  74. const String& TextureResource::GetSource() const
  75. {
  76. return source;
  77. }
  78. void TextureResource::Release()
  79. {
  80. if (loaded)
  81. {
  82. RenderInterface* render_interface = ::Rml::GetRenderInterface();
  83. RMLUI_ASSERT(render_interface);
  84. render_interface->ReleaseTexture(handle);
  85. handle = {};
  86. dimensions = {};
  87. loaded = false;
  88. }
  89. }
  90. bool TextureResource::IsLoaded() const
  91. {
  92. return loaded;
  93. }
  94. bool TextureResource::Load()
  95. {
  96. RMLUI_ZoneScoped;
  97. RenderInterface* render_interface = ::Rml::GetRenderInterface();
  98. loaded = true;
  99. // Generate the texture from the callback function if we have one.
  100. if (texture_callback)
  101. {
  102. TextureCallback& callback_fnc = *texture_callback;
  103. if (!callback_fnc(render_interface, source, handle, dimensions) || !handle)
  104. {
  105. Log::Message(Log::LT_WARNING, "Failed to generate texture from callback function %s.", source.c_str());
  106. handle = {};
  107. dimensions = {};
  108. return false;
  109. }
  110. return true;
  111. }
  112. // No callback function, load the texture through the render interface.
  113. if (!render_interface->LoadTexture(handle, dimensions, source))
  114. {
  115. Log::Message(Log::LT_WARNING, "Failed to load texture from %s.", source.c_str());
  116. handle = {};
  117. dimensions = {};
  118. return false;
  119. }
  120. return true;
  121. }
  122. } // namespace Rml