ElementEffects.cpp 12 KB

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