2
0

BoxShadowCache.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "BoxShadowCache.h"
  2. #include "../../Include/RmlUi/Core/ComputedValues.h"
  3. #include "../../Include/RmlUi/Core/MeshUtilities.h"
  4. #include "../../Include/RmlUi/Core/Profiling.h"
  5. #include "../../Include/RmlUi/Core/RenderManager.h"
  6. #include "../Core/ControlledLifetimeResource.h"
  7. #include "BoxShadowHash.h"
  8. #include "GeometryBoxShadow.h"
  9. namespace Rml {
  10. struct BoxShadowCacheData {
  11. StableUnorderedMap<BoxShadowGeometryInfo, WeakPtr<BoxShadowRenderable>> handles;
  12. };
  13. static void ReleaseHandle(BoxShadowRenderable* handle);
  14. BoxShadowRenderable::BoxShadowRenderable(const BoxShadowGeometryInfo& geometry_info) : cache_key(geometry_info) {}
  15. BoxShadowRenderable::~BoxShadowRenderable()
  16. {
  17. ReleaseHandle(this);
  18. }
  19. static ControlledLifetimeResource<BoxShadowCacheData> shadow_cache_data;
  20. void BoxShadowCache::Initialize()
  21. {
  22. shadow_cache_data.Initialize();
  23. }
  24. void BoxShadowCache::Shutdown()
  25. {
  26. shadow_cache_data.Shutdown();
  27. }
  28. static SharedPtr<BoxShadowRenderable> GetOrCreateBoxShadow(RenderManager& render_manager, const BoxShadowGeometryInfo& info)
  29. {
  30. RMLUI_ZoneScoped;
  31. auto it_handle = shadow_cache_data->handles.find(info);
  32. if (it_handle != shadow_cache_data->handles.end())
  33. {
  34. SharedPtr<BoxShadowRenderable> result = it_handle->second.lock();
  35. RMLUI_ASSERTMSG(result, "Failed to lock handle in Box Shadow cache");
  36. return result;
  37. }
  38. const auto iterator_inserted = shadow_cache_data->handles.emplace(info, WeakPtr<BoxShadowRenderable>());
  39. RMLUI_ASSERTMSG(iterator_inserted.second, "Could not insert entry into the Box Shadow cache handle map, duplicate key.");
  40. const BoxShadowGeometryInfo& inserted_key = iterator_inserted.first->first;
  41. WeakPtr<BoxShadowRenderable>& inserted_weak_data_pointer = iterator_inserted.first->second;
  42. auto shadow_handle = MakeShared<BoxShadowRenderable>(inserted_key);
  43. GeometryBoxShadow::GenerateTexture(shadow_handle->texture, shadow_handle->background_border_geometry, render_manager, inserted_key);
  44. Mesh mesh;
  45. const byte alpha = byte(info.opacity * 255.f);
  46. MeshUtilities::GenerateQuad(mesh, -info.element_offset_in_texture, Vector2f(info.texture_dimensions), ColourbPremultiplied(alpha, alpha));
  47. shadow_handle->geometry = render_manager.MakeGeometry(std::move(mesh));
  48. inserted_weak_data_pointer = shadow_handle;
  49. return shadow_handle;
  50. }
  51. static void ReleaseHandle(BoxShadowRenderable* handle)
  52. {
  53. // There are no longer any users of the cache entry uniquely identified by the handle address. Start from the
  54. // tip (i.e. per-color data) and remove that entry from its parent. Move up the cache ancestry and erase any
  55. // entries that no longer have any children.
  56. auto& handles = shadow_cache_data->handles;
  57. const BoxShadowGeometryInfo& key = handle->cache_key;
  58. auto it_handle = handles.find(key);
  59. RMLUI_ASSERT(it_handle != handles.cend());
  60. handles.erase(it_handle);
  61. }
  62. SharedPtr<BoxShadowRenderable> BoxShadowCache::GetHandle(Element* element, const ComputedValues& computed)
  63. {
  64. RenderManager* render_manager = element->GetRenderManager();
  65. if (!render_manager)
  66. return {};
  67. ColourbPremultiplied background_color = computed.background_color().ToPremultiplied();
  68. Array<ColourbPremultiplied, 4> border_colors = {
  69. computed.border_top_color().ToPremultiplied(),
  70. computed.border_right_color().ToPremultiplied(),
  71. computed.border_bottom_color().ToPremultiplied(),
  72. computed.border_left_color().ToPremultiplied(),
  73. };
  74. const CornerSizes border_radius = computed.border_radius();
  75. BoxShadowGeometryInfo geom_info = GeometryBoxShadow::Resolve(element, border_radius, background_color, border_colors, computed.opacity());
  76. return GetOrCreateBoxShadow(*render_manager, geom_info);
  77. }
  78. } // namespace Rml