RenderInterfaceCompatibility.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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/RenderInterfaceCompatibility.h"
  29. #include "../../Include/RmlUi/Core/Math.h"
  30. namespace Rml {
  31. static void UnPremultiplyAlpha(const byte* source, byte* destination)
  32. {
  33. const byte alpha = source[3];
  34. destination[0] = (alpha > 0 ? (source[0] * 255) / alpha : 255);
  35. destination[1] = (alpha > 0 ? (source[1] * 255) / alpha : 255);
  36. destination[2] = (alpha > 0 ? (source[2] * 255) / alpha : 255);
  37. destination[3] = alpha;
  38. }
  39. RenderInterfaceCompatibility::RenderInterfaceCompatibility() : adapter(new RenderInterfaceAdapter(*this)) {}
  40. RenderInterfaceCompatibility::~RenderInterfaceCompatibility() {}
  41. CompiledGeometryHandle RenderInterfaceCompatibility::CompileGeometry(Vertex* /*vertices*/, int /*num_vertices*/, int* /*indices*/,
  42. int /*num_indices*/, TextureHandle /*texture*/)
  43. {
  44. return 0;
  45. }
  46. void RenderInterfaceCompatibility::RenderCompiledGeometry(CompiledGeometryHandle /*geometry*/, const Vector2f& /*translation*/) {}
  47. void RenderInterfaceCompatibility::ReleaseCompiledGeometry(CompiledGeometryHandle /*geometry*/) {}
  48. bool RenderInterfaceCompatibility::LoadTexture(TextureHandle& /*texture_handle*/, Vector2i& /*texture_dimensions*/, const String& /*source*/)
  49. {
  50. return false;
  51. }
  52. bool RenderInterfaceCompatibility::GenerateTexture(TextureHandle& /*texture_handle*/, const byte* /*source*/, const Vector2i& /*source_dimensions*/)
  53. {
  54. return false;
  55. }
  56. void RenderInterfaceCompatibility::ReleaseTexture(TextureHandle /*texture*/) {}
  57. void RenderInterfaceCompatibility::SetTransform(const Matrix4f* /*transform*/) {}
  58. RenderInterface* RenderInterfaceCompatibility::GetAdaptedInterface()
  59. {
  60. return static_cast<RenderInterface*>(adapter.get());
  61. }
  62. RenderInterfaceAdapter::RenderInterfaceAdapter(RenderInterfaceCompatibility& legacy) : legacy(legacy) {}
  63. CompiledGeometryHandle RenderInterfaceAdapter::CompileGeometry(Span<const Vertex> vertices, Span<const int> indices)
  64. {
  65. // Previously, vertex colors were given in unpremultipled alpha, while now they are given in premultiplied alpha. If
  66. // not corrected for, transparent colors may look darker than they should with the legacy renderer. Thus, here we
  67. // make such a conversion.
  68. //
  69. // When upgrading your renderer, it is strongly recommended to convert your pipeline to use premultiplied alpha,
  70. // both to avoid copying vertex data like here and to achieve correct blending results.
  71. //
  72. // Note that, the vertices and indices are now guaranteed to be valid and immutable until the call to
  73. // ReleaseGeometry. Thus, it is possible to avoid copying the data even if you need access to it during the render
  74. // call. However, (1) due to the need to modify the vertices, we need to make a copy of them here. And (2), due to a
  75. // limitation in the legacy render interface, vertices and indices were previously submitted as pointers to mutable
  76. // vertices and indices. They were never intended to be mutable, but to avoid a const_cast we need to copy both of
  77. // them for that reason too.
  78. Vector<Vertex> vertices_unpremultiplied(vertices.begin(), vertices.end());
  79. for (size_t i = 0; i < vertices.size(); i++)
  80. {
  81. UnPremultiplyAlpha(vertices[i].colour, vertices_unpremultiplied[i].colour);
  82. }
  83. Vector<int> indices_copy(indices.begin(), indices.end());
  84. AdaptedGeometry* data = new AdaptedGeometry{std::move(vertices_unpremultiplied), std::move(indices_copy), {}};
  85. return reinterpret_cast<Rml::CompiledGeometryHandle>(data);
  86. }
  87. void RenderInterfaceAdapter::RenderGeometry(CompiledGeometryHandle handle, Vector2f translation, TextureHandle texture)
  88. {
  89. AdaptedGeometry* geometry = reinterpret_cast<AdaptedGeometry*>(handle);
  90. // Textures were previously stored with the compiled geometry, but is now instead submitted during rendering.
  91. LegacyCompiledGeometryHandle& legacy_geometry = geometry->textures[texture];
  92. if (!legacy_geometry)
  93. {
  94. legacy_geometry = legacy.CompileGeometry(geometry->vertices.data(), (int)geometry->vertices.size(), geometry->indices.data(),
  95. (int)geometry->indices.size(), texture);
  96. }
  97. // If the legacy renderer supports compiling, use that, otherwise render the geometry in immediate mode.
  98. if (legacy_geometry)
  99. {
  100. legacy.RenderCompiledGeometry(legacy_geometry, translation);
  101. }
  102. else
  103. {
  104. legacy.RenderGeometry(geometry->vertices.data(), (int)geometry->vertices.size(), geometry->indices.data(), (int)geometry->indices.size(),
  105. texture, translation);
  106. }
  107. }
  108. void RenderInterfaceAdapter::ReleaseGeometry(CompiledGeometryHandle handle)
  109. {
  110. AdaptedGeometry* geometry = reinterpret_cast<AdaptedGeometry*>(handle);
  111. for (auto& pair : geometry->textures)
  112. legacy.ReleaseCompiledGeometry(pair.second);
  113. delete reinterpret_cast<AdaptedGeometry*>(geometry);
  114. }
  115. void RenderInterfaceAdapter::EnableScissorRegion(bool enable)
  116. {
  117. legacy.EnableScissorRegion(enable);
  118. }
  119. void RenderInterfaceAdapter::SetScissorRegion(Rectanglei region)
  120. {
  121. legacy.SetScissorRegion(region.Left(), region.Top(), region.Width(), region.Height());
  122. }
  123. void RenderInterfaceAdapter::EnableClipMask(bool enable)
  124. {
  125. legacy.EnableScissorRegion(enable);
  126. }
  127. void RenderInterfaceAdapter::RenderToClipMask(ClipMaskOperation operation, CompiledGeometryHandle handle, Vector2f translation)
  128. {
  129. switch (operation)
  130. {
  131. case ClipMaskOperation::Set:
  132. case ClipMaskOperation::Intersect:
  133. // Intersect is considered like Set. This typically occurs in nested clipping situations, which never worked
  134. // correctly in legacy.
  135. break;
  136. case ClipMaskOperation::SetInverse:
  137. // Using features not supported in legacy, bail out.
  138. return;
  139. }
  140. // New features can render more complex clip masks, while legacy only supported rectangle scissoring. Find the
  141. // geometry's rectangular coverage.
  142. const AdaptedGeometry* geometry = reinterpret_cast<AdaptedGeometry*>(handle);
  143. Rectanglef rectangle = Rectanglef::FromPosition(geometry->vertices[0].position);
  144. for (const Vertex& vertex : geometry->vertices)
  145. rectangle = rectangle.Join(vertex.position);
  146. const Rectanglei scissor = Rectanglei(rectangle.Translate(translation));
  147. legacy.SetScissorRegion(scissor.Left(), scissor.Top(), scissor.Width(), scissor.Height());
  148. }
  149. TextureHandle RenderInterfaceAdapter::LoadTexture(Vector2i& texture_dimensions, const String& source)
  150. {
  151. TextureHandle texture_handle = {};
  152. if (!legacy.LoadTexture(texture_handle, texture_dimensions, source))
  153. texture_handle = {};
  154. return texture_handle;
  155. }
  156. TextureHandle RenderInterfaceAdapter::GenerateTexture(Span<const byte> source_data, Vector2i source_dimensions)
  157. {
  158. // Previously, textures were given in unpremultiplied alpha format. Since RmlUi 6, they are given in premultiplied
  159. // alpha. For compatibility, convert the texture to unpremultiplied alpha which is expected by legacy render
  160. // interfaces.
  161. const int num_bytes = source_dimensions.x * source_dimensions.y * 4;
  162. std::unique_ptr<byte[]> unpremultiplied_copy(new byte[num_bytes]);
  163. for (int i = 0; i < num_bytes; i += 4)
  164. {
  165. UnPremultiplyAlpha(&source_data[i], unpremultiplied_copy.get() + i);
  166. }
  167. TextureHandle texture_handle = {};
  168. if (!legacy.GenerateTexture(texture_handle, unpremultiplied_copy.get(), source_dimensions))
  169. texture_handle = {};
  170. return texture_handle;
  171. }
  172. void RenderInterfaceAdapter::ReleaseTexture(TextureHandle texture_handle)
  173. {
  174. legacy.ReleaseTexture(texture_handle);
  175. }
  176. void RenderInterfaceAdapter::SetTransform(const Matrix4f* transform)
  177. {
  178. legacy.SetTransform(transform);
  179. }
  180. } // namespace Rml