Geometry.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 "../../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()
  38. {
  39. database_handle = GeometryDatabase::Insert(this);
  40. }
  41. Geometry::Geometry(Geometry&& other) noexcept
  42. {
  43. MoveFrom(other);
  44. database_handle = GeometryDatabase::Insert(this);
  45. }
  46. Geometry& Geometry::operator=(Geometry&& other) noexcept
  47. {
  48. MoveFrom(other);
  49. // Keep the database handles from construction unchanged, they are tied to the *this* pointer and should not change.
  50. return *this;
  51. }
  52. void Geometry::MoveFrom(Geometry& other) noexcept
  53. {
  54. vertices = std::move(other.vertices);
  55. indices = std::move(other.indices);
  56. texture = std::exchange(other.texture, nullptr);
  57. compiled_geometry = std::exchange(other.compiled_geometry, 0);
  58. compile_attempted = std::exchange(other.compile_attempted, false);
  59. }
  60. Geometry::~Geometry()
  61. {
  62. GeometryDatabase::Erase(database_handle);
  63. Release();
  64. }
  65. void Geometry::Render(Vector2f translation)
  66. {
  67. RenderInterface* render_interface = GetRenderInterface();
  68. RMLUI_ASSERT(render_interface);
  69. translation = translation.Round();
  70. // Render our compiled geometry if possible.
  71. if (compiled_geometry)
  72. {
  73. RMLUI_ZoneScopedN("RenderCompiled");
  74. render_interface->RenderCompiledGeometry(compiled_geometry, translation, texture ? texture->GetHandle() : 0);
  75. }
  76. // Otherwise, if we actually have geometry, try to compile it if we haven't already done so, otherwise render it in
  77. // immediate mode.
  78. else
  79. {
  80. if (vertices.empty() || indices.empty())
  81. return;
  82. RMLUI_ZoneScopedN("RenderGeometry");
  83. if (!compile_attempted)
  84. {
  85. compile_attempted = true;
  86. compiled_geometry = render_interface->CompileGeometry(&vertices[0], (int)vertices.size(), &indices[0], (int)indices.size());
  87. // If we managed to compile the geometry, we can clear the local copy of vertices and indices and
  88. // immediately render the compiled version.
  89. if (compiled_geometry)
  90. {
  91. render_interface->RenderCompiledGeometry(compiled_geometry, translation, texture ? texture->GetHandle() : 0);
  92. return;
  93. }
  94. }
  95. // Either we've attempted to compile before (and failed), or the compile we just attempted failed; either way,
  96. // render the uncompiled version.
  97. render_interface->RenderGeometry(&vertices[0], (int)vertices.size(), &indices[0], (int)indices.size(), texture ? texture->GetHandle() : 0,
  98. translation);
  99. }
  100. }
  101. void Geometry::RenderToClipMask(ClipMaskOperation clip_mask, Vector2f translation)
  102. {
  103. RenderInterface* render_interface = GetRenderInterface();
  104. RMLUI_ASSERT(render_interface);
  105. if (!compile_attempted)
  106. {
  107. if (vertices.empty() || indices.empty())
  108. return;
  109. RMLUI_ZoneScoped;
  110. compile_attempted = true;
  111. compiled_geometry = render_interface->CompileGeometry(&vertices[0], (int)vertices.size(), &indices[0], (int)indices.size());
  112. }
  113. if (compiled_geometry)
  114. {
  115. translation = translation.Round();
  116. render_interface->RenderToClipMask(clip_mask, compiled_geometry, translation);
  117. }
  118. }
  119. Vector<Vertex>& Geometry::GetVertices()
  120. {
  121. return vertices;
  122. }
  123. Vector<int>& Geometry::GetIndices()
  124. {
  125. return indices;
  126. }
  127. const Texture* Geometry::GetTexture() const
  128. {
  129. return texture;
  130. }
  131. void Geometry::SetTexture(const Texture* _texture)
  132. {
  133. texture = _texture;
  134. Release();
  135. }
  136. void Geometry::Release(bool clear_buffers)
  137. {
  138. if (compiled_geometry)
  139. {
  140. ::Rml::GetRenderInterface()->ReleaseCompiledGeometry(compiled_geometry);
  141. compiled_geometry = 0;
  142. }
  143. compile_attempted = false;
  144. if (clear_buffers)
  145. {
  146. vertices.clear();
  147. indices.clear();
  148. }
  149. }
  150. Geometry::operator bool() const
  151. {
  152. return !indices.empty();
  153. }
  154. } // namespace Rml