StackingContext.cpp 9.7 KB

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