ElementDecoration.cpp 9.7 KB

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