StackingContext.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/QuickSort.h>
  7. #include <AK/StringBuilder.h>
  8. #include <LibGfx/Painter.h>
  9. #include <LibWeb/Layout/Box.h>
  10. #include <LibWeb/Layout/InitialContainingBlock.h>
  11. #include <LibWeb/Layout/ReplacedBox.h>
  12. #include <LibWeb/Painting/PaintableBox.h>
  13. #include <LibWeb/Painting/StackingContext.h>
  14. namespace Web::Painting {
  15. static void paint_node(Layout::Node const& layout_node, PaintContext& context, PaintPhase phase)
  16. {
  17. if (auto const* paintable = layout_node.paintable())
  18. paintable->paint(context, phase);
  19. }
  20. StackingContext::StackingContext(Layout::Box& box, StackingContext* parent)
  21. : m_box(box)
  22. , m_parent(parent)
  23. {
  24. VERIFY(m_parent != this);
  25. if (m_parent)
  26. m_parent->m_children.append(this);
  27. }
  28. void StackingContext::sort()
  29. {
  30. quick_sort(m_children, [](auto& a, auto& b) {
  31. auto a_z_index = a->m_box.computed_values().z_index().value_or(0);
  32. auto b_z_index = b->m_box.computed_values().z_index().value_or(0);
  33. if (a_z_index == b_z_index)
  34. return a->m_box.is_before(b->m_box);
  35. return a_z_index < b_z_index;
  36. });
  37. for (auto* child : m_children)
  38. child->sort();
  39. }
  40. void StackingContext::paint_descendants(PaintContext& context, Layout::Node& box, StackingContextPaintPhase phase) const
  41. {
  42. if (phase == StackingContextPaintPhase::Foreground) {
  43. if (auto* paintable = box.paintable())
  44. paintable->before_children_paint(context, PaintPhase::Foreground);
  45. }
  46. box.for_each_child([&](auto& child) {
  47. if (child.establishes_stacking_context())
  48. return;
  49. bool child_is_inline_or_replaced = child.is_inline() || is<Layout::ReplacedBox>(child);
  50. switch (phase) {
  51. case StackingContextPaintPhase::BackgroundAndBorders:
  52. if (!child_is_inline_or_replaced && !child.is_floating() && !child.is_positioned()) {
  53. paint_node(child, context, PaintPhase::Background);
  54. paint_node(child, context, PaintPhase::Border);
  55. paint_descendants(context, child, phase);
  56. }
  57. break;
  58. case StackingContextPaintPhase::Floats:
  59. if (!child.is_positioned()) {
  60. if (child.is_floating()) {
  61. paint_node(child, context, PaintPhase::Background);
  62. paint_node(child, context, PaintPhase::Border);
  63. paint_descendants(context, child, StackingContextPaintPhase::BackgroundAndBorders);
  64. }
  65. paint_descendants(context, child, phase);
  66. }
  67. break;
  68. case StackingContextPaintPhase::BackgroundAndBordersForInlineLevelAndReplaced:
  69. if (!child.is_positioned()) {
  70. if (child_is_inline_or_replaced) {
  71. paint_node(child, context, PaintPhase::Background);
  72. paint_node(child, context, PaintPhase::Border);
  73. paint_descendants(context, child, StackingContextPaintPhase::BackgroundAndBorders);
  74. }
  75. paint_descendants(context, child, phase);
  76. }
  77. break;
  78. case StackingContextPaintPhase::Foreground:
  79. if (!child.is_positioned()) {
  80. paint_node(child, context, PaintPhase::Foreground);
  81. paint_descendants(context, child, phase);
  82. }
  83. break;
  84. case StackingContextPaintPhase::FocusAndOverlay:
  85. if (context.has_focus()) {
  86. paint_node(child, context, PaintPhase::FocusOutline);
  87. }
  88. paint_node(child, context, PaintPhase::Overlay);
  89. paint_descendants(context, child, phase);
  90. break;
  91. }
  92. });
  93. if (phase == StackingContextPaintPhase::Foreground) {
  94. if (auto* paintable = box.paintable())
  95. paintable->after_children_paint(context, PaintPhase::Foreground);
  96. }
  97. }
  98. void StackingContext::paint_internal(PaintContext& context) const
  99. {
  100. // For a more elaborate description of the algorithm, see CSS 2.1 Appendix E
  101. // Draw the background and borders for the context root (steps 1, 2)
  102. paint_node(m_box, context, PaintPhase::Background);
  103. paint_node(m_box, context, PaintPhase::Border);
  104. // Draw positioned descendants with negative z-indices (step 3)
  105. for (auto* child : m_children) {
  106. if (child->m_box.computed_values().z_index().has_value() && child->m_box.computed_values().z_index().value() < 0)
  107. child->paint(context);
  108. }
  109. // Draw the background and borders for block-level children (step 4)
  110. paint_descendants(context, m_box, StackingContextPaintPhase::BackgroundAndBorders);
  111. // Draw the non-positioned floats (step 5)
  112. paint_descendants(context, m_box, StackingContextPaintPhase::Floats);
  113. // Draw inline content, replaced content, etc. (steps 6, 7)
  114. paint_descendants(context, m_box, StackingContextPaintPhase::BackgroundAndBordersForInlineLevelAndReplaced);
  115. paint_node(m_box, context, PaintPhase::Foreground);
  116. paint_descendants(context, m_box, StackingContextPaintPhase::Foreground);
  117. // Draw other positioned descendants (steps 8, 9)
  118. for (auto* child : m_children) {
  119. if (child->m_box.computed_values().z_index().has_value() && child->m_box.computed_values().z_index().value() < 0)
  120. continue;
  121. child->paint(context);
  122. }
  123. paint_node(m_box, context, PaintPhase::FocusOutline);
  124. paint_node(m_box, context, PaintPhase::Overlay);
  125. paint_descendants(context, m_box, StackingContextPaintPhase::FocusAndOverlay);
  126. }
  127. void StackingContext::paint(PaintContext& context) const
  128. {
  129. Gfx::PainterStateSaver saver(context.painter());
  130. if (m_box.is_fixed_position()) {
  131. context.painter().translate(context.scroll_offset());
  132. }
  133. auto opacity = m_box.computed_values().opacity();
  134. if (opacity == 0.0f)
  135. return;
  136. if (opacity < 1.0f) {
  137. auto bitmap_or_error = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, context.painter().target()->size());
  138. if (bitmap_or_error.is_error())
  139. return;
  140. auto bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  141. Gfx::Painter painter(bitmap);
  142. PaintContext paint_context(painter, context.palette(), context.scroll_offset());
  143. paint_internal(paint_context);
  144. context.painter().blit(Gfx::IntPoint(m_box.paint_box()->absolute_position()), bitmap, Gfx::IntRect(m_box.paint_box()->absolute_rect()), opacity);
  145. } else {
  146. paint_internal(context);
  147. }
  148. }
  149. HitTestResult StackingContext::hit_test(Gfx::IntPoint const& position, HitTestType type) const
  150. {
  151. // NOTE: Hit testing basically happens in reverse painting order.
  152. // https://www.w3.org/TR/CSS22/visuren.html#z-index
  153. // 7. the child stacking contexts with positive stack levels (least positive first).
  154. for (ssize_t i = m_children.size() - 1; i >= 0; --i) {
  155. auto const& child = *m_children[i];
  156. if (child.m_box.computed_values().z_index().value_or(0) < 0)
  157. break;
  158. auto result = child.hit_test(position, type);
  159. if (result.paintable)
  160. return result;
  161. }
  162. HitTestResult result;
  163. // 6. the child stacking contexts with stack level 0 and the positioned descendants with stack level 0.
  164. m_box.for_each_in_subtree_of_type<Layout::Box>([&](Layout::Box const& box) {
  165. if (box.is_positioned() && !box.paint_box()->stacking_context()) {
  166. result = box.paint_box()->hit_test(position, type);
  167. if (result.paintable)
  168. return IterationDecision::Break;
  169. }
  170. return IterationDecision::Continue;
  171. });
  172. if (result.paintable)
  173. return result;
  174. // 5. the in-flow, inline-level, non-positioned descendants, including inline tables and inline blocks.
  175. if (m_box.children_are_inline() && is<Layout::BlockContainer>(m_box)) {
  176. auto result = m_box.paint_box()->hit_test(position, type);
  177. if (result.paintable)
  178. return result;
  179. }
  180. // 4. the non-positioned floats.
  181. m_box.for_each_in_subtree_of_type<Layout::Box>([&](Layout::Box const& box) {
  182. if (box.is_floating()) {
  183. result = box.paint_box()->hit_test(position, type);
  184. if (result.paintable)
  185. return IterationDecision::Break;
  186. }
  187. return IterationDecision::Continue;
  188. });
  189. // 3. the in-flow, non-inline-level, non-positioned descendants.
  190. if (!m_box.children_are_inline()) {
  191. m_box.for_each_in_subtree_of_type<Layout::Box>([&](Layout::Box const& box) {
  192. if (!box.is_absolutely_positioned() && !box.is_floating()) {
  193. result = box.paint_box()->hit_test(position, type);
  194. if (result.paintable)
  195. return IterationDecision::Break;
  196. }
  197. return IterationDecision::Continue;
  198. });
  199. if (result.paintable)
  200. return result;
  201. }
  202. // 2. the child stacking contexts with negative stack levels (most negative first).
  203. for (ssize_t i = m_children.size() - 1; i >= 0; --i) {
  204. auto const& child = *m_children[i];
  205. if (child.m_box.computed_values().z_index().value_or(0) < 0)
  206. break;
  207. auto result = child.hit_test(position, type);
  208. if (result.paintable)
  209. return result;
  210. }
  211. // 1. the background and borders of the element forming the stacking context.
  212. if (m_box.paint_box()->absolute_border_box_rect().contains(position.to_type<float>())) {
  213. return HitTestResult {
  214. .paintable = m_box.paintable(),
  215. };
  216. }
  217. return {};
  218. }
  219. void StackingContext::dump(int indent) const
  220. {
  221. StringBuilder builder;
  222. for (int i = 0; i < indent; ++i)
  223. builder.append(' ');
  224. builder.appendff("SC for {} {} [children: {}] (z-index: ", m_box.debug_description(), m_box.paint_box()->absolute_rect(), m_children.size());
  225. if (m_box.computed_values().z_index().has_value())
  226. builder.appendff("{}", m_box.computed_values().z_index().value());
  227. else
  228. builder.append("auto");
  229. builder.append(')');
  230. dbgln("{}", builder.string_view());
  231. for (auto& child : m_children)
  232. child->dump(indent + 1);
  233. }
  234. }