StackingContext.cpp 6.3 KB

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