ElementDecoration.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 "ElementDecoration.h"
  29. #include "../../Include/RmlUi/Core/ComputedValues.h"
  30. #include "../../Include/RmlUi/Core/Context.h"
  31. #include "../../Include/RmlUi/Core/Decorator.h"
  32. #include "../../Include/RmlUi/Core/Element.h"
  33. #include "../../Include/RmlUi/Core/ElementDocument.h"
  34. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  35. #include "../../Include/RmlUi/Core/Filter.h"
  36. #include "../../Include/RmlUi/Core/Profiling.h"
  37. #include "../../Include/RmlUi/Core/RenderInterface.h"
  38. #include "../../Include/RmlUi/Core/StyleSheet.h"
  39. namespace Rml {
  40. ElementDecoration::ElementDecoration(Element* _element) : element(_element) {}
  41. ElementDecoration::~ElementDecoration()
  42. {
  43. ReleaseDecorators();
  44. }
  45. void ElementDecoration::InstanceDecorators()
  46. {
  47. if (!decorators_dirty)
  48. return;
  49. decorators_dirty = false;
  50. decorators_data_dirty = true;
  51. RMLUI_ZoneScopedC(0xB22222);
  52. ReleaseDecorators();
  53. const ComputedValues& computed = element->GetComputedValues();
  54. if (computed.has_decorator())
  55. {
  56. const Property* property = element->GetLocalProperty(PropertyId::Decorator);
  57. if (!property || property->unit != Unit::DECORATOR)
  58. return;
  59. DecoratorsPtr decorators_ptr = property->Get<DecoratorsPtr>();
  60. if (!decorators_ptr)
  61. return;
  62. const StyleSheet* style_sheet = element->GetStyleSheet();
  63. if (!style_sheet)
  64. return;
  65. PropertySource document_source("", 0, "");
  66. const PropertySource* source = property->source.get();
  67. if (!source)
  68. {
  69. if (ElementDocument* document = element->GetOwnerDocument())
  70. {
  71. document_source.path = document->GetSourceURL();
  72. source = &document_source;
  73. }
  74. }
  75. const DecoratorPtrList& decorator_list = style_sheet->InstanceDecorators(*decorators_ptr, source);
  76. for (const SharedPtr<const Decorator>& decorator : decorator_list)
  77. {
  78. if (decorator)
  79. {
  80. DecoratorEntry decorator_handle;
  81. decorator_handle.decorator_data = 0;
  82. decorator_handle.decorator = decorator;
  83. decorators.push_back(std::move(decorator_handle));
  84. }
  85. }
  86. }
  87. if (computed.has_filter() || computed.has_backdrop_filter())
  88. {
  89. for (const auto id : {PropertyId::Filter, PropertyId::BackdropFilter})
  90. {
  91. const Property* property = element->GetLocalProperty(id);
  92. if (!property || property->unit != Unit::FILTER)
  93. return;
  94. FiltersPtr filters_ptr = property->Get<FiltersPtr>();
  95. if (!filters_ptr)
  96. return;
  97. FilterEntryList& list = (id == PropertyId::Filter ? filters : backdrop_filters);
  98. list.reserve(filters_ptr->list.size());
  99. for (const FilterDeclaration& declaration : filters_ptr->list)
  100. {
  101. SharedPtr<const Filter> filter = declaration.instancer->InstanceFilter(declaration.type, declaration.properties);
  102. if (filter)
  103. {
  104. list.push_back({std::move(filter), CompiledFilterHandle{}});
  105. }
  106. else
  107. {
  108. const auto& source = property->source;
  109. Log::Message(Log::LT_WARNING, "Filter '%s' in '%s' could not be instanced, declared at %s:%d", declaration.type.c_str(),
  110. filters_ptr->value.c_str(), source ? source->path.c_str() : "", source ? source->line_number : -1);
  111. }
  112. }
  113. }
  114. }
  115. }
  116. void ElementDecoration::ReloadDecoratorsData()
  117. {
  118. if (decorators_data_dirty)
  119. {
  120. decorators_data_dirty = false;
  121. for (DecoratorEntry& decorator : decorators)
  122. {
  123. if (decorator.decorator_data)
  124. decorator.decorator->ReleaseElementData(decorator.decorator_data);
  125. decorator.decorator_data = decorator.decorator->GenerateElementData(element);
  126. }
  127. for (FilterEntryList* list : {&filters, &backdrop_filters})
  128. {
  129. for (FilterEntry& filter : *list)
  130. {
  131. if (filter.handle)
  132. filter.filter->ReleaseCompiledFilter(element, filter.handle);
  133. filter.handle = filter.filter->CompileFilter(element);
  134. }
  135. }
  136. }
  137. }
  138. void ElementDecoration::ReleaseDecorators()
  139. {
  140. for (DecoratorEntry& decorator : decorators)
  141. {
  142. if (decorator.decorator_data)
  143. decorator.decorator->ReleaseElementData(decorator.decorator_data);
  144. }
  145. decorators.clear();
  146. for (FilterEntryList* list : {&filters, &backdrop_filters})
  147. {
  148. for (FilterEntry& filter : *list)
  149. {
  150. if (filter.handle)
  151. filter.filter->ReleaseCompiledFilter(element, filter.handle);
  152. }
  153. list->clear();
  154. }
  155. }
  156. void ElementDecoration::RenderDecorators(RenderStage render_stage)
  157. {
  158. InstanceDecorators();
  159. ReloadDecoratorsData();
  160. if (!decorators.empty())
  161. {
  162. if (render_stage == RenderStage::Decoration)
  163. {
  164. // Render the decorators attached to this element in its current state.
  165. // Render from back to front for correct render order.
  166. for (int i = (int)decorators.size() - 1; i >= 0; i--)
  167. {
  168. DecoratorEntry& decorator = decorators[i];
  169. decorator.decorator->RenderElement(element, decorator.decorator_data);
  170. }
  171. }
  172. }
  173. if (filters.empty() && backdrop_filters.empty())
  174. return;
  175. RenderInterface* render_interface = ::Rml::GetRenderInterface();
  176. Context* context = element->GetContext();
  177. if (!render_interface || !context)
  178. return;
  179. auto ApplyClippingRegion = [this, render_interface, context](bool extend_ink_overflow) {
  180. ElementUtilities::SetClippingRegion(element); // TODO: For backdrop-filter only: Force clipping to our border-box.
  181. // Find the region being affected by the active filters and apply it as a scissor.
  182. Rectanglef filter_region = Rectanglef::MakeInvalid();
  183. ElementUtilities::GetBoundingBox(filter_region, element, BoxArea::Auto);
  184. if (extend_ink_overflow)
  185. {
  186. for (const auto& filter : filters)
  187. filter.filter->ExtendInkOverflow(element, filter_region);
  188. }
  189. Math::ExpandToPixelGrid(filter_region);
  190. Rectanglei scissor_region = Rectanglei::FromSize(context->GetDimensions());
  191. Vector2i clip_position, clip_size;
  192. if (context->GetActiveClipRegion(clip_position, clip_size))
  193. scissor_region.Intersect(Rectanglei::FromPositionSize(clip_position, clip_size));
  194. scissor_region.IntersectIfValid(Rectanglei(filter_region));
  195. render_interface->EnableScissorRegion(true);
  196. render_interface->SetScissorRegion(scissor_region.Left(), scissor_region.Top(), scissor_region.Width(), scissor_region.Height());
  197. };
  198. if (!backdrop_filters.empty())
  199. {
  200. if (render_stage == RenderStage::Enter)
  201. {
  202. ApplyClippingRegion(false);
  203. render_interface->PushLayer(LayerFill::Clone);
  204. FilterHandleList filter_handles;
  205. for (auto& filter : backdrop_filters)
  206. {
  207. if (filter.handle)
  208. filter_handles.push_back(filter.handle);
  209. }
  210. render_interface->PopLayer(BlendMode::Replace, filter_handles);
  211. ElementUtilities::ApplyActiveClipRegion(context);
  212. }
  213. }
  214. if (!filters.empty())
  215. {
  216. if (render_stage == RenderStage::Enter)
  217. {
  218. render_interface->PushLayer(LayerFill::Clear);
  219. }
  220. else if (render_stage == RenderStage::Exit)
  221. {
  222. ApplyClippingRegion(true);
  223. FilterHandleList filter_handles;
  224. for (auto& filter : filters)
  225. {
  226. if (filter.handle)
  227. filter_handles.push_back(filter.handle);
  228. }
  229. render_interface->PopLayer(BlendMode::Blend, filter_handles);
  230. ElementUtilities::ApplyActiveClipRegion(context);
  231. }
  232. }
  233. }
  234. void ElementDecoration::DirtyDecorators()
  235. {
  236. decorators_dirty = true;
  237. }
  238. void ElementDecoration::DirtyDecoratorsData()
  239. {
  240. decorators_data_dirty = true;
  241. }
  242. } // namespace Rml