BoxShadowCache.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "BoxShadowCache.h"
  29. #include "../../Include/RmlUi/Core/ComputedValues.h"
  30. #include "../../Include/RmlUi/Core/MeshUtilities.h"
  31. #include "../../Include/RmlUi/Core/Profiling.h"
  32. #include "../../Include/RmlUi/Core/RenderManager.h"
  33. #include "../Core/ControlledLifetimeResource.h"
  34. #include "BoxShadowHash.h"
  35. #include "GeometryBoxShadow.h"
  36. namespace Rml {
  37. struct BoxShadowCacheData {
  38. StableUnorderedMap<BoxShadowGeometryInfo, WeakPtr<BoxShadowRenderable>> handles;
  39. };
  40. static void ReleaseHandle(BoxShadowRenderable* handle);
  41. BoxShadowRenderable::BoxShadowRenderable(const BoxShadowGeometryInfo& geometry_info) : cache_key(geometry_info) {}
  42. BoxShadowRenderable::~BoxShadowRenderable()
  43. {
  44. ReleaseHandle(this);
  45. }
  46. static ControlledLifetimeResource<BoxShadowCacheData> shadow_cache_data;
  47. void BoxShadowCache::Initialize()
  48. {
  49. shadow_cache_data.Initialize();
  50. }
  51. void BoxShadowCache::Shutdown()
  52. {
  53. shadow_cache_data.Shutdown();
  54. }
  55. static SharedPtr<BoxShadowRenderable> GetOrCreateBoxShadow(RenderManager& render_manager, const BoxShadowGeometryInfo& info)
  56. {
  57. RMLUI_ZoneScoped;
  58. auto it_handle = shadow_cache_data->handles.find(info);
  59. if (it_handle != shadow_cache_data->handles.end())
  60. {
  61. SharedPtr<BoxShadowRenderable> result = it_handle->second.lock();
  62. RMLUI_ASSERTMSG(result, "Failed to lock handle in Box Shadow cache");
  63. return result;
  64. }
  65. const auto iterator_inserted = shadow_cache_data->handles.emplace(info, WeakPtr<BoxShadowRenderable>());
  66. RMLUI_ASSERTMSG(iterator_inserted.second, "Could not insert entry into the Box Shadow cache handle map, duplicate key.");
  67. const BoxShadowGeometryInfo& inserted_key = iterator_inserted.first->first;
  68. WeakPtr<BoxShadowRenderable>& inserted_weak_data_pointer = iterator_inserted.first->second;
  69. auto shadow_handle = MakeShared<BoxShadowRenderable>(inserted_key);
  70. GeometryBoxShadow::GenerateTexture(shadow_handle->texture, shadow_handle->background_border_geometry, render_manager, inserted_key);
  71. Mesh mesh;
  72. const byte alpha = byte(info.opacity * 255.f);
  73. MeshUtilities::GenerateQuad(mesh, -info.element_offset_in_texture, Vector2f(info.texture_dimensions), ColourbPremultiplied(alpha, alpha));
  74. shadow_handle->geometry = render_manager.MakeGeometry(std::move(mesh));
  75. inserted_weak_data_pointer = shadow_handle;
  76. return shadow_handle;
  77. }
  78. static void ReleaseHandle(BoxShadowRenderable* handle)
  79. {
  80. // There are no longer any users of the cache entry uniquely identified by the handle address. Start from the
  81. // tip (i.e. per-color data) and remove that entry from its parent. Move up the cache ancestry and erase any
  82. // entries that no longer have any children.
  83. auto& handles = shadow_cache_data->handles;
  84. const BoxShadowGeometryInfo& key = handle->cache_key;
  85. auto it_handle = handles.find(key);
  86. RMLUI_ASSERT(it_handle != handles.cend());
  87. handles.erase(it_handle);
  88. }
  89. SharedPtr<BoxShadowRenderable> BoxShadowCache::GetHandle(Element* element, const ComputedValues& computed)
  90. {
  91. RenderManager* render_manager = element->GetRenderManager();
  92. if (!render_manager)
  93. return {};
  94. ColourbPremultiplied background_color = computed.background_color().ToPremultiplied();
  95. Array<ColourbPremultiplied, 4> border_colors = {
  96. computed.border_top_color().ToPremultiplied(),
  97. computed.border_right_color().ToPremultiplied(),
  98. computed.border_bottom_color().ToPremultiplied(),
  99. computed.border_left_color().ToPremultiplied(),
  100. };
  101. const CornerSizes border_radius = computed.border_radius();
  102. BoxShadowGeometryInfo geom_info = GeometryBoxShadow::Resolve(element, border_radius, background_color, border_colors, computed.opacity());
  103. return GetOrCreateBoxShadow(*render_manager, geom_info);
  104. }
  105. } // namespace Rml