GeometryBoxShadow.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 "GeometryBoxShadow.h"
  29. #include "../../Include/RmlUi/Core/Box.h"
  30. #include "../../Include/RmlUi/Core/Context.h"
  31. #include "../../Include/RmlUi/Core/DecorationTypes.h"
  32. #include "../../Include/RmlUi/Core/Element.h"
  33. #include "../../Include/RmlUi/Core/Geometry.h"
  34. #include "../../Include/RmlUi/Core/GeometryUtilities.h"
  35. #include "../../Include/RmlUi/Core/Math.h"
  36. #include "../../Include/RmlUi/Core/RenderInterface.h"
  37. #include "../../Include/RmlUi/Core/RenderManager.h"
  38. #include "../../Include/RmlUi/Core/Texture.h"
  39. namespace Rml {
  40. void GeometryBoxShadow::Generate(Geometry& out_shadow_geometry, Texture& out_shadow_texture, Element* element, Geometry& background_border_geometry,
  41. BoxShadowList shadow_list, const Vector4f border_radius, const float opacity)
  42. {
  43. // Find the box-shadow texture dimension and offset required to cover all box-shadows and element boxes combined.
  44. Vector2f element_offset_in_texture;
  45. Vector2i texture_dimensions;
  46. // Resolve all lengths to px units.
  47. for (BoxShadow& shadow : shadow_list)
  48. {
  49. shadow.blur_radius = NumericValue(element->ResolveLength(shadow.blur_radius), Unit::PX);
  50. shadow.spread_distance = NumericValue(element->ResolveLength(shadow.spread_distance), Unit::PX);
  51. shadow.offset_x = NumericValue(element->ResolveLength(shadow.offset_x), Unit::PX);
  52. shadow.offset_y = NumericValue(element->ResolveLength(shadow.offset_y), Unit::PX);
  53. }
  54. {
  55. Vector2f extend_min;
  56. Vector2f extend_max;
  57. // Extend the render-texture to encompass box-shadow blur and spread.
  58. for (const BoxShadow& shadow : shadow_list)
  59. {
  60. if (!shadow.inset)
  61. {
  62. const float extend = 1.5f * shadow.blur_radius.number + shadow.spread_distance.number;
  63. const Vector2f offset = {shadow.offset_x.number, shadow.offset_y.number};
  64. extend_min = Math::Min(extend_min, offset - Vector2f(extend));
  65. extend_max = Math::Max(extend_max, offset + Vector2f(extend));
  66. }
  67. }
  68. Rectanglef texture_region;
  69. // Extend the render-texture further to cover all the element's boxes.
  70. for (int i = 0; i < element->GetNumBoxes(); i++)
  71. {
  72. Vector2f offset;
  73. const Box& box = element->GetBox(i, offset);
  74. texture_region.Join(Rectanglef::FromPositionSize(offset, box.GetSize(BoxArea::Border)));
  75. }
  76. texture_region.ExtendTopLeft(-extend_min);
  77. texture_region.ExtendBottomRight(extend_max);
  78. Math::ExpandToPixelGrid(texture_region);
  79. element_offset_in_texture = -texture_region.TopLeft();
  80. texture_dimensions = Vector2i(texture_region.Size());
  81. }
  82. // Callback for generating the box-shadow texture. Using a callback ensures that the texture can be regenerated at any time, for example if the
  83. // device loses its GPU context and the client calls Rml::ReleaseTextures().
  84. auto texture_callback = [&background_border_geometry, element, border_radius, texture_dimensions, element_offset_in_texture,
  85. shadow_list = std::move(shadow_list)](RenderInterface* render_interface, const String& /*name*/,
  86. TextureHandle& out_handle, Vector2i& out_dimensions) -> bool {
  87. Context* context = element->GetContext();
  88. if (!context)
  89. {
  90. RMLUI_ERROR;
  91. return false;
  92. }
  93. Geometry geometry_padding; // Render geometry for inner box-shadow.
  94. Geometry geometry_padding_border; // Clipping mask for outer box-shadow.
  95. bool has_inner_shadow = false;
  96. bool has_outer_shadow = false;
  97. for (const BoxShadow& shadow : shadow_list)
  98. {
  99. if (shadow.inset)
  100. has_inner_shadow = true;
  101. else
  102. has_outer_shadow = true;
  103. }
  104. // Generate the geometry for all the element's boxes and extend the render-texture further to cover all of them.
  105. for (int i = 0; i < element->GetNumBoxes(); i++)
  106. {
  107. Vector2f offset;
  108. const Box& box = element->GetBox(i, offset);
  109. ColourbPremultiplied white(255);
  110. if (has_inner_shadow)
  111. GeometryUtilities::GenerateBackground(&geometry_padding, box, offset, border_radius, white, BoxArea::Padding);
  112. if (has_outer_shadow)
  113. GeometryUtilities::GenerateBackground(&geometry_padding_border, box, offset, border_radius, white, BoxArea::Border);
  114. }
  115. RenderManager& render_manager = context->GetRenderManager();
  116. const RenderState initial_render_state = render_manager.GetState();
  117. render_manager.ResetState();
  118. render_manager.SetScissorRegion(Rectanglei::FromSize(texture_dimensions));
  119. render_interface->PushLayer(LayerFill::Clear);
  120. background_border_geometry.Render(element_offset_in_texture);
  121. for (int shadow_index = (int)shadow_list.size() - 1; shadow_index >= 0; shadow_index--)
  122. {
  123. const BoxShadow& shadow = shadow_list[shadow_index];
  124. const Vector2f shadow_offset = {shadow.offset_x.number, shadow.offset_y.number};
  125. const bool inset = shadow.inset;
  126. const float spread_distance = shadow.spread_distance.number;
  127. const float blur_radius = shadow.blur_radius.number;
  128. Vector4f spread_radii = border_radius;
  129. for (int i = 0; i < 4; i++)
  130. {
  131. float& radius = spread_radii[i];
  132. float spread_factor = (inset ? -1.f : 1.f);
  133. if (radius < spread_distance)
  134. {
  135. const float ratio_minus_one = (radius / spread_distance) - 1.f;
  136. spread_factor *= 1.f + ratio_minus_one * ratio_minus_one * ratio_minus_one;
  137. }
  138. radius = Math::Max(radius + spread_factor * spread_distance, 0.f);
  139. }
  140. Geometry shadow_geometry;
  141. // Generate the shadow geometry. For outer box-shadows it is rendered normally, while for inner box-shadows it is used as a clipping mask.
  142. for (int i = 0; i < element->GetNumBoxes(); i++)
  143. {
  144. Vector2f offset;
  145. Box box = element->GetBox(i, offset);
  146. const float signed_spread_distance = (inset ? -spread_distance : spread_distance);
  147. offset -= Vector2f(signed_spread_distance);
  148. for (int j = 0; j < Box::num_edges; j++)
  149. {
  150. BoxEdge edge = (BoxEdge)j;
  151. const float new_size = box.GetEdge(BoxArea::Padding, edge) + signed_spread_distance;
  152. box.SetEdge(BoxArea::Padding, edge, new_size);
  153. }
  154. GeometryUtilities::GenerateBackground(&shadow_geometry, box, offset, spread_radii, shadow.color,
  155. inset ? BoxArea::Padding : BoxArea::Border);
  156. }
  157. CompiledFilterHandle blur = {};
  158. if (blur_radius > 0.5f)
  159. {
  160. blur = render_interface->CompileFilter("blur", Dictionary{{"radius", Variant(blur_radius)}});
  161. if (blur)
  162. render_interface->PushLayer(LayerFill::Clear);
  163. }
  164. if (inset)
  165. {
  166. render_manager.SetClipMask(ClipMaskOperation::SetInverse, &shadow_geometry, shadow_offset + element_offset_in_texture);
  167. for (Rml::Vertex& vertex : geometry_padding.GetVertices())
  168. vertex.colour = shadow.color;
  169. geometry_padding.Release();
  170. geometry_padding.Render(element_offset_in_texture);
  171. render_manager.SetClipMask(ClipMaskOperation::Set, &geometry_padding, element_offset_in_texture);
  172. }
  173. else
  174. {
  175. render_manager.SetClipMask(ClipMaskOperation::SetInverse, &geometry_padding_border, element_offset_in_texture);
  176. shadow_geometry.Render(shadow_offset + element_offset_in_texture);
  177. }
  178. if (blur)
  179. {
  180. render_interface->PopLayer(BlendMode::Blend, {blur});
  181. render_interface->ReleaseCompiledFilter(blur);
  182. }
  183. }
  184. TextureHandle shadow_texture = render_interface->SaveLayerAsTexture(texture_dimensions);
  185. if (!shadow_texture)
  186. return false;
  187. render_interface->PopLayer(BlendMode::Discard, {});
  188. render_manager.SetState(initial_render_state);
  189. out_dimensions = texture_dimensions;
  190. out_handle = shadow_texture;
  191. return true;
  192. };
  193. RMLUI_ASSERT(!out_shadow_geometry);
  194. Vector<Vertex>& vertices = out_shadow_geometry.GetVertices();
  195. Vector<int>& indices = out_shadow_geometry.GetIndices();
  196. vertices.resize(4);
  197. indices.resize(6);
  198. const byte alpha = byte(opacity * 255.f);
  199. GeometryUtilities::GenerateQuad(vertices.data(), indices.data(), -element_offset_in_texture, Vector2f(texture_dimensions),
  200. ColourbPremultiplied(alpha, alpha));
  201. out_shadow_texture.Set("box-shadow", texture_callback);
  202. out_shadow_geometry.SetTexture(&out_shadow_texture);
  203. }
  204. } // namespace Rml