StackingContext.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2020, 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/Painting/StackingContext.h>
  12. namespace Web::Layout {
  13. StackingContext::StackingContext(Box& box, StackingContext* parent)
  14. : m_box(box)
  15. , m_parent(parent)
  16. {
  17. VERIFY(m_parent != this);
  18. if (m_parent) {
  19. m_parent->m_children.append(this);
  20. // FIXME: Don't sort on every append..
  21. quick_sort(m_parent->m_children, [](auto& a, auto& b) {
  22. auto a_z_index = a->m_box.computed_values().z_index().value_or(0);
  23. auto b_z_index = b->m_box.computed_values().z_index().value_or(0);
  24. if (a_z_index == b_z_index)
  25. return a->m_box.is_before(b->m_box);
  26. return a_z_index < b_z_index;
  27. });
  28. }
  29. }
  30. void StackingContext::paint_descendants(PaintContext& context, Node& box, StackingContextPaintPhase phase)
  31. {
  32. if (phase == StackingContextPaintPhase::Foreground)
  33. box.before_children_paint(context, PaintPhase::Foreground);
  34. box.for_each_child([&](auto& child) {
  35. if (child.establishes_stacking_context())
  36. return;
  37. switch (phase) {
  38. case StackingContextPaintPhase::BackgroundAndBorders:
  39. if (!child.is_floating() && !child.is_positioned()) {
  40. child.paint(context, PaintPhase::Background);
  41. child.paint(context, PaintPhase::Border);
  42. paint_descendants(context, child, phase);
  43. }
  44. break;
  45. case StackingContextPaintPhase::Floats:
  46. if (!child.is_positioned()) {
  47. if (child.is_floating()) {
  48. child.paint(context, PaintPhase::Background);
  49. child.paint(context, PaintPhase::Border);
  50. paint_descendants(context, child, StackingContextPaintPhase::BackgroundAndBorders);
  51. }
  52. paint_descendants(context, child, phase);
  53. }
  54. break;
  55. case StackingContextPaintPhase::Foreground:
  56. if (!child.is_positioned()) {
  57. child.paint(context, PaintPhase::Foreground);
  58. paint_descendants(context, child, phase);
  59. }
  60. break;
  61. case StackingContextPaintPhase::FocusAndOverlay:
  62. if (context.has_focus()) {
  63. child.paint(context, PaintPhase::FocusOutline);
  64. }
  65. child.paint(context, PaintPhase::Overlay);
  66. paint_descendants(context, child, phase);
  67. break;
  68. }
  69. });
  70. if (phase == StackingContextPaintPhase::Foreground)
  71. box.after_children_paint(context, PaintPhase::Foreground);
  72. }
  73. void StackingContext::paint_internal(PaintContext& context)
  74. {
  75. // For a more elaborate description of the algorithm, see CSS 2.1 Appendix E
  76. // Draw the background and borders for the context root (steps 1, 2)
  77. m_box.paint(context, PaintPhase::Background);
  78. m_box.paint(context, PaintPhase::Border);
  79. // Draw positioned descendants with negative z-indices (step 3)
  80. for (auto* child : m_children) {
  81. if (child->m_box.computed_values().z_index().has_value() && child->m_box.computed_values().z_index().value() < 0)
  82. child->paint(context);
  83. }
  84. // Draw the background and borders for block-level children (step 4)
  85. paint_descendants(context, m_box, StackingContextPaintPhase::BackgroundAndBorders);
  86. // Draw the non-positioned floats (step 5)
  87. paint_descendants(context, m_box, StackingContextPaintPhase::Floats);
  88. // Draw inline content, replaced content, etc. (steps 6, 7)
  89. m_box.paint(context, PaintPhase::Foreground);
  90. paint_descendants(context, m_box, StackingContextPaintPhase::Foreground);
  91. // Draw other positioned descendants (steps 8, 9)
  92. for (auto* child : m_children) {
  93. if (child->m_box.computed_values().z_index().has_value() && child->m_box.computed_values().z_index().value() < 0)
  94. continue;
  95. child->paint(context);
  96. }
  97. m_box.paint(context, PaintPhase::FocusOutline);
  98. m_box.paint(context, PaintPhase::Overlay);
  99. paint_descendants(context, m_box, StackingContextPaintPhase::FocusAndOverlay);
  100. }
  101. void StackingContext::paint(PaintContext& context)
  102. {
  103. Gfx::PainterStateSaver saver(context.painter());
  104. if (m_box.is_fixed_position()) {
  105. context.painter().translate(context.scroll_offset());
  106. }
  107. auto opacity = m_box.computed_values().opacity();
  108. if (opacity == 0.0f)
  109. return;
  110. if (opacity < 1.0f) {
  111. auto bitmap_or_error = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, context.painter().target()->size());
  112. if (bitmap_or_error.is_error())
  113. return;
  114. auto bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  115. Gfx::Painter painter(bitmap);
  116. PaintContext paint_context(painter, context.palette(), context.scroll_offset());
  117. paint_internal(paint_context);
  118. context.painter().blit(Gfx::IntPoint(m_box.absolute_position()), bitmap, Gfx::IntRect(m_box.absolute_rect()), opacity);
  119. } else {
  120. paint_internal(context);
  121. }
  122. }
  123. HitTestResult StackingContext::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  124. {
  125. HitTestResult result;
  126. if (!is<InitialContainingBlock>(m_box)) {
  127. result = m_box.hit_test(position, type);
  128. } else {
  129. // NOTE: InitialContainingBlock::hit_test() merely calls StackingContext::hit_test()
  130. // so we call its base class instead.
  131. result = verify_cast<InitialContainingBlock>(m_box).BlockContainer::hit_test(position, type);
  132. }
  133. int z_index = m_box.computed_values().z_index().value_or(0);
  134. for (auto* child : m_children) {
  135. int child_z_index = child->m_box.computed_values().z_index().value_or(0);
  136. if (result.layout_node && (child_z_index < z_index))
  137. continue;
  138. auto result_here = child->hit_test(position, type);
  139. if (result_here.layout_node)
  140. result = result_here;
  141. }
  142. return result;
  143. }
  144. void StackingContext::dump(int indent) const
  145. {
  146. StringBuilder builder;
  147. for (int i = 0; i < indent; ++i)
  148. builder.append(' ');
  149. builder.appendff("SC for {}({}) {} [children: {}]", m_box.class_name(), m_box.dom_node() ? m_box.dom_node()->node_name().characters() : "(anonymous)", m_box.absolute_rect().to_string().characters(), m_children.size());
  150. dbgln("{}", builder.string_view());
  151. for (auto& child : m_children)
  152. child->dump(indent + 1);
  153. }
  154. }