ElementDecoration.cpp 9.9 KB

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