ElementBackgroundBorder.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "ElementBackgroundBorder.h"
  29. #include "../../Include/RmlUi/Core/Box.h"
  30. #include "../../Include/RmlUi/Core/ComputedValues.h"
  31. #include "../../Include/RmlUi/Core/Context.h"
  32. #include "../../Include/RmlUi/Core/DecorationTypes.h"
  33. #include "../../Include/RmlUi/Core/Element.h"
  34. #include "../../Include/RmlUi/Core/MeshUtilities.h"
  35. #include "../../Include/RmlUi/Core/Profiling.h"
  36. #include "../../Include/RmlUi/Core/RenderManager.h"
  37. #include "BoxShadowCache.h"
  38. #include "GeometryBoxShadow.h"
  39. namespace Rml {
  40. ElementBackgroundBorder::ElementBackgroundBorder() {}
  41. void ElementBackgroundBorder::Render(Element* element)
  42. {
  43. if (background_dirty || border_dirty)
  44. {
  45. for (auto& background : backgrounds)
  46. {
  47. if (background.first != BackgroundType::BackgroundBorder)
  48. background.second.geometry.Release();
  49. }
  50. GenerateGeometry(element);
  51. background_dirty = false;
  52. border_dirty = false;
  53. }
  54. if (Background* shadow = GetBackground(BackgroundType::BoxShadowAndBackgroundBorder))
  55. {
  56. const Vector2f offset = element->GetAbsoluteOffset(BoxArea::Border);
  57. shadow->box_shadow_and_background_border->geometry.Render(offset, shadow->box_shadow_and_background_border->texture);
  58. }
  59. else if (Background* background = GetBackground(BackgroundType::BackgroundBorder))
  60. {
  61. const Vector2f offset = element->GetAbsoluteOffset(BoxArea::Border);
  62. background->geometry.Render(offset);
  63. }
  64. }
  65. void ElementBackgroundBorder::DirtyBackground()
  66. {
  67. background_dirty = true;
  68. }
  69. void ElementBackgroundBorder::DirtyBorder()
  70. {
  71. border_dirty = true;
  72. }
  73. Geometry* ElementBackgroundBorder::GetClipGeometry(Element* element, BoxArea clip_area)
  74. {
  75. BackgroundType type = {};
  76. switch (clip_area)
  77. {
  78. case Rml::BoxArea::Border: type = BackgroundType::ClipBorder; break;
  79. case Rml::BoxArea::Padding: type = BackgroundType::ClipPadding; break;
  80. case Rml::BoxArea::Content: type = BackgroundType::ClipContent; break;
  81. default: RMLUI_ERROR; return nullptr;
  82. }
  83. RenderManager* render_manager = element->GetRenderManager();
  84. Geometry& geometry = GetOrCreateBackground(type).geometry;
  85. if (render_manager && !geometry)
  86. {
  87. Mesh mesh = geometry.Release(Geometry::ReleaseMode::ClearMesh);
  88. MeshUtilities::GenerateBackground(mesh, element->GetRenderBox(clip_area), ColourbPremultiplied(255));
  89. geometry = render_manager->MakeGeometry(std::move(mesh));
  90. }
  91. return &geometry;
  92. }
  93. ElementBackgroundBorder::Background* ElementBackgroundBorder::GetBackground(BackgroundType type)
  94. {
  95. auto it = backgrounds.find(type);
  96. if (it != backgrounds.end())
  97. return &it->second;
  98. return nullptr;
  99. }
  100. ElementBackgroundBorder::Background& ElementBackgroundBorder::GetOrCreateBackground(BackgroundType type)
  101. {
  102. auto it = backgrounds.find(type);
  103. if (it != backgrounds.end())
  104. return it->second;
  105. Background& background = backgrounds[type];
  106. return background;
  107. }
  108. void ElementBackgroundBorder::EraseBackground(BackgroundType type)
  109. {
  110. backgrounds.erase(type);
  111. }
  112. void ElementBackgroundBorder::GenerateGeometry(Element* element)
  113. {
  114. RMLUI_ZoneScoped;
  115. RenderManager* render_manager = element->GetRenderManager();
  116. if (!render_manager)
  117. return;
  118. const ComputedValues& computed = element->GetComputedValues();
  119. const bool has_box_shadow = computed.has_box_shadow();
  120. if (has_box_shadow)
  121. {
  122. // The box shadow geometry also includes the element's background and border, thus we can skip the normal background generation.
  123. EraseBackground(BackgroundType::BackgroundBorder);
  124. Background& shadow_background = GetOrCreateBackground(BackgroundType::BoxShadowAndBackgroundBorder);
  125. shadow_background.box_shadow_and_background_border = BoxShadowCache::GetHandle(element, computed);
  126. return;
  127. }
  128. EraseBackground(BackgroundType::BoxShadowAndBackgroundBorder);
  129. const float opacity = computed.opacity();
  130. ColourbPremultiplied background_color = computed.background_color().ToPremultiplied(opacity);
  131. Array<ColourbPremultiplied, 4> border_colors = {
  132. computed.border_top_color().ToPremultiplied(opacity),
  133. computed.border_right_color().ToPremultiplied(opacity),
  134. computed.border_bottom_color().ToPremultiplied(opacity),
  135. computed.border_left_color().ToPremultiplied(opacity),
  136. };
  137. Geometry& geometry = GetOrCreateBackground(BackgroundType::BackgroundBorder).geometry;
  138. Mesh mesh = geometry.Release(Geometry::ReleaseMode::ClearMesh);
  139. for (int i = 0; i < element->GetNumBoxes(); i++)
  140. MeshUtilities::GenerateBackgroundBorder(mesh, element->GetRenderBox(BoxArea::Padding, i), background_color, border_colors.data());
  141. geometry = render_manager->MakeGeometry(std::move(mesh));
  142. }
  143. } // namespace Rml