Geometry.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 "../../Include/RmlUi/Core/Geometry.h"
  29. #include "../../Include/RmlUi/Core/Context.h"
  30. #include "../../Include/RmlUi/Core/Core.h"
  31. #include "../../Include/RmlUi/Core/Element.h"
  32. #include "../../Include/RmlUi/Core/Profiling.h"
  33. #include "../../Include/RmlUi/Core/RenderInterface.h"
  34. #include "GeometryDatabase.h"
  35. #include <utility>
  36. namespace Rml {
  37. Geometry::Geometry(Element* host_element) : host_element(host_element)
  38. {
  39. database_handle = GeometryDatabase::Insert(this);
  40. }
  41. Geometry::Geometry(Context* host_context) : host_context(host_context)
  42. {
  43. database_handle = GeometryDatabase::Insert(this);
  44. }
  45. Geometry::Geometry(Geometry&& other)
  46. {
  47. MoveFrom(other);
  48. database_handle = GeometryDatabase::Insert(this);
  49. }
  50. Geometry& Geometry::operator=(Geometry&& other)
  51. {
  52. MoveFrom(other);
  53. // Keep the database handles from construction unchanged, they are tied to the *this* pointer and should not change.
  54. return *this;
  55. }
  56. void Geometry::MoveFrom(Geometry& other)
  57. {
  58. host_context = std::exchange(other.host_context, nullptr);
  59. host_element = std::exchange(other.host_element, nullptr);
  60. vertices = std::move(other.vertices);
  61. indices = std::move(other.indices);
  62. texture = std::exchange(other.texture, nullptr);
  63. compiled_geometry = std::exchange(other.compiled_geometry, 0);
  64. compile_attempted = std::exchange(other.compile_attempted, false);
  65. }
  66. Geometry::~Geometry()
  67. {
  68. GeometryDatabase::Erase(database_handle);
  69. Release();
  70. }
  71. // Set the host element for this geometry; this should be passed in the constructor if possible.
  72. void Geometry::SetHostElement(Element* _host_element)
  73. {
  74. if (host_element == _host_element)
  75. return;
  76. if (host_element != nullptr)
  77. {
  78. Release();
  79. host_context = nullptr;
  80. }
  81. host_element = _host_element;
  82. }
  83. void Geometry::Render(Vector2f translation)
  84. {
  85. RenderInterface* const render_interface = GetRenderInterface();
  86. if (!render_interface)
  87. return;
  88. translation = translation.Round();
  89. // Render our compiled geometry if possible.
  90. if (compiled_geometry)
  91. {
  92. RMLUI_ZoneScopedN("RenderCompiled");
  93. render_interface->RenderCompiledGeometry(compiled_geometry, translation);
  94. }
  95. // Otherwise, if we actually have geometry, try to compile it if we haven't already done so, otherwise render it in
  96. // immediate mode.
  97. else
  98. {
  99. if (vertices.empty() ||
  100. indices.empty())
  101. return;
  102. RMLUI_ZoneScopedN("RenderGeometry");
  103. if (!compile_attempted)
  104. {
  105. compile_attempted = true;
  106. compiled_geometry = render_interface->CompileGeometry(&vertices[0], (int)vertices.size(), &indices[0], (int)indices.size(), texture ? texture->GetHandle(render_interface) : 0);
  107. // If we managed to compile the geometry, we can clear the local copy of vertices and indices and
  108. // immediately render the compiled version.
  109. if (compiled_geometry)
  110. {
  111. render_interface->RenderCompiledGeometry(compiled_geometry, translation);
  112. return;
  113. }
  114. }
  115. // Either we've attempted to compile before (and failed), or the compile we just attempted failed; either way,
  116. // render the uncompiled version.
  117. render_interface->RenderGeometry(&vertices[0], (int)vertices.size(), &indices[0], (int)indices.size(), texture ? texture->GetHandle(GetRenderInterface()) : 0, translation);
  118. }
  119. }
  120. // Returns the geometry's vertices. If these are written to, Release() should be called to force a recompile.
  121. Vector< Vertex >& Geometry::GetVertices()
  122. {
  123. return vertices;
  124. }
  125. // Returns the geometry's indices. If these are written to, Release() should be called to force a recompile.
  126. Vector< int >& Geometry::GetIndices()
  127. {
  128. return indices;
  129. }
  130. // Gets the geometry's texture.
  131. const Texture* Geometry::GetTexture() const
  132. {
  133. return texture;
  134. }
  135. // Sets the geometry's texture.
  136. void Geometry::SetTexture(const Texture* _texture)
  137. {
  138. texture = _texture;
  139. Release();
  140. }
  141. void Geometry::Release(bool clear_buffers)
  142. {
  143. if (compiled_geometry)
  144. {
  145. GetRenderInterface()->ReleaseCompiledGeometry(compiled_geometry);
  146. compiled_geometry = 0;
  147. }
  148. compile_attempted = false;
  149. if (clear_buffers)
  150. {
  151. vertices.clear();
  152. indices.clear();
  153. }
  154. }
  155. Geometry::operator bool() const
  156. {
  157. return !indices.empty();
  158. }
  159. // Returns the host context's render interface.
  160. RenderInterface* Geometry::GetRenderInterface()
  161. {
  162. if (!host_context)
  163. {
  164. if (host_element)
  165. host_context = host_element->GetContext();
  166. }
  167. if (host_context)
  168. return host_context->GetRenderInterface();
  169. else
  170. return ::Rml::GetRenderInterface();
  171. }
  172. } // namespace Rml