ElementEffects.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 "ElementEffects.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. ElementEffects::ElementEffects(Element* _element) : element(_element) {}
  39. ElementEffects::~ElementEffects()
  40. {
  41. ReleaseEffects();
  42. }
  43. void ElementEffects::InstanceEffects()
  44. {
  45. if (!effects_dirty)
  46. return;
  47. effects_dirty = false;
  48. effects_data_dirty = true;
  49. RMLUI_ZoneScopedC(0xB22222);
  50. ReleaseEffects();
  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 ElementEffects::ReloadEffectsData()
  132. {
  133. if (effects_data_dirty)
  134. {
  135. effects_data_dirty = false;
  136. bool decorator_data_failed = false;
  137. for (DecoratorEntryList* list : {&decorators, &mask_images})
  138. {
  139. for (DecoratorEntry& decorator : *list)
  140. {
  141. if (decorator.decorator_data)
  142. decorator.decorator->ReleaseElementData(decorator.decorator_data);
  143. decorator.decorator_data = decorator.decorator->GenerateElementData(element, decorator.paint_area);
  144. if (!decorator.decorator_data)
  145. decorator_data_failed = true;
  146. }
  147. }
  148. if (decorator_data_failed)
  149. Log::Message(Log::LT_WARNING, "Could not generate decorator element data: %s", element->GetAddress().c_str());
  150. bool filter_compile_failed = false;
  151. for (FilterEntryList* list : {&filters, &backdrop_filters})
  152. {
  153. for (FilterEntry& filter : *list)
  154. {
  155. filter.compiled = filter.filter->CompileFilter(element);
  156. if (!filter.compiled)
  157. filter_compile_failed = true;
  158. }
  159. }
  160. if (filter_compile_failed)
  161. Log::Message(Log::LT_WARNING, "Could not compile filter on element: %s", element->GetAddress().c_str());
  162. }
  163. }
  164. void ElementEffects::ReleaseEffects()
  165. {
  166. for (DecoratorEntryList* list : {&decorators, &mask_images})
  167. {
  168. for (DecoratorEntry& decorator : *list)
  169. {
  170. if (decorator.decorator_data)
  171. decorator.decorator->ReleaseElementData(decorator.decorator_data);
  172. }
  173. list->clear();
  174. }
  175. filters.clear();
  176. backdrop_filters.clear();
  177. }
  178. void ElementEffects::RenderEffects(RenderStage render_stage)
  179. {
  180. InstanceEffects();
  181. ReloadEffectsData();
  182. if (!decorators.empty())
  183. {
  184. if (render_stage == RenderStage::Decoration)
  185. {
  186. // Render the decorators attached to this element in its current state.
  187. // Render from back to front for correct render order.
  188. for (int i = (int)decorators.size() - 1; i >= 0; i--)
  189. {
  190. DecoratorEntry& decorator = decorators[i];
  191. if (decorator.decorator_data)
  192. decorator.decorator->RenderElement(element, decorator.decorator_data);
  193. }
  194. }
  195. }
  196. if (filters.empty() && backdrop_filters.empty() && mask_images.empty())
  197. return;
  198. RenderManager* render_manager = element->GetRenderManager();
  199. if (!render_manager)
  200. return;
  201. Rectanglei initial_scissor_region = render_manager->GetScissorRegion();
  202. auto ApplyClippingRegion = [this, &render_manager](PropertyId filter_id) {
  203. RMLUI_ASSERT(filter_id == PropertyId::Filter || filter_id == PropertyId::BackdropFilter);
  204. const bool force_clip_to_self_border_box = (filter_id == PropertyId::BackdropFilter);
  205. ElementUtilities::SetClippingRegion(element, force_clip_to_self_border_box);
  206. // Find the region being affected by the active filters and apply it as a scissor.
  207. Rectanglef filter_region = Rectanglef::MakeInvalid();
  208. ElementUtilities::GetBoundingBox(filter_region, element, force_clip_to_self_border_box ? BoxArea::Border : BoxArea::Auto);
  209. // The filter property may draw outside our normal clipping region due to ink overflow.
  210. if (filter_id == PropertyId::Filter)
  211. {
  212. for (const auto& filter : filters)
  213. filter.filter->ExtendInkOverflow(element, filter_region);
  214. }
  215. Math::ExpandToPixelGrid(filter_region);
  216. Rectanglei scissor_region = Rectanglei(filter_region).IntersectIfValid(render_manager->GetScissorRegion());
  217. render_manager->SetScissorRegion(scissor_region);
  218. };
  219. auto ApplyScissorRegionForBackdrop = [this, &render_manager]() {
  220. // Set the scissor region for backdrop drawing, which covers the element's border box plus any area we may need
  221. // to read from, such as any blur radius.
  222. Rectanglef filter_region = Rectanglef::MakeInvalid();
  223. ElementUtilities::GetBoundingBox(filter_region, element, BoxArea::Border);
  224. for (const auto& filter : backdrop_filters)
  225. filter.filter->ExtendInkOverflow(element, filter_region);
  226. Math::ExpandToPixelGrid(filter_region);
  227. render_manager->SetScissorRegion(Rectanglei(filter_region));
  228. };
  229. if (render_stage == RenderStage::Enter)
  230. {
  231. const LayerHandle backdrop_source_layer = render_manager->GetTopLayer();
  232. if (!filters.empty() || !mask_images.empty())
  233. {
  234. render_manager->PushLayer();
  235. }
  236. if (!backdrop_filters.empty())
  237. {
  238. const LayerHandle backdrop_destination_layer = render_manager->GetTopLayer();
  239. // @performance We strictly only need this temporary buffer when having to read from outside the element
  240. // boundaries, which currently only applies to blur and drop-shadow. Alternatively, we could avoid this
  241. // completely if we introduced a render interface API concept of different input and output clipping. That
  242. // is, we set a large input scissor to cover all input data, which can be used e.g. during blurring, and use
  243. // our small border-area-only clipping region for the composite layers output.
  244. ApplyScissorRegionForBackdrop();
  245. render_manager->PushLayer();
  246. const LayerHandle backdrop_temp_layer = render_manager->GetTopLayer();
  247. FilterHandleList filter_handles;
  248. for (auto& filter : backdrop_filters)
  249. filter.compiled.AddHandleTo(filter_handles);
  250. // Render the backdrop filters in the extended scissor region including any ink overflow.
  251. render_manager->CompositeLayers(backdrop_source_layer, backdrop_temp_layer, BlendMode::Blend, filter_handles);
  252. // Then composite the filter output to our destination while applying our clipping region, including any border-radius.
  253. ApplyClippingRegion(PropertyId::BackdropFilter);
  254. render_manager->CompositeLayers(backdrop_temp_layer, backdrop_destination_layer, BlendMode::Blend, {});
  255. render_manager->PopLayer();
  256. render_manager->SetScissorRegion(initial_scissor_region);
  257. }
  258. }
  259. else if (render_stage == RenderStage::Exit)
  260. {
  261. if (!filters.empty() || !mask_images.empty())
  262. {
  263. ApplyClippingRegion(PropertyId::Filter);
  264. CompiledFilter mask_image_filter;
  265. FilterHandleList filter_handles;
  266. filter_handles.reserve(filters.size() + (mask_images.empty() ? 0 : 1));
  267. for (auto& filter : filters)
  268. filter.compiled.AddHandleTo(filter_handles);
  269. if (!mask_images.empty())
  270. {
  271. render_manager->PushLayer();
  272. for (int i = (int)mask_images.size() - 1; i >= 0; i--)
  273. {
  274. DecoratorEntry& mask_image = mask_images[i];
  275. if (mask_image.decorator_data)
  276. mask_image.decorator->RenderElement(element, mask_image.decorator_data);
  277. }
  278. mask_image_filter = render_manager->SaveLayerAsMaskImage();
  279. mask_image_filter.AddHandleTo(filter_handles);
  280. render_manager->PopLayer();
  281. }
  282. render_manager->CompositeLayers(render_manager->GetTopLayer(), render_manager->GetNextLayer(), BlendMode::Blend, filter_handles);
  283. render_manager->PopLayer();
  284. render_manager->SetScissorRegion(initial_scissor_region);
  285. }
  286. }
  287. }
  288. void ElementEffects::DirtyEffects()
  289. {
  290. effects_dirty = true;
  291. }
  292. void ElementEffects::DirtyEffectsData()
  293. {
  294. effects_data_dirty = true;
  295. }
  296. } // namespace Rml