DisplayList.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Painting/DisplayList.h>
  7. namespace Web::Painting {
  8. void DisplayList::append(Command&& command, Optional<i32> scroll_frame_id)
  9. {
  10. m_commands.append({ scroll_frame_id, move(command) });
  11. }
  12. static Optional<Gfx::IntRect> command_bounding_rectangle(Command const& command)
  13. {
  14. return command.visit(
  15. [&](auto const& command) -> Optional<Gfx::IntRect> {
  16. if constexpr (requires { command.bounding_rect(); })
  17. return command.bounding_rect();
  18. else
  19. return {};
  20. });
  21. }
  22. static bool command_is_clip_or_mask(Command const& command)
  23. {
  24. return command.visit(
  25. [&](auto const& command) -> bool {
  26. if constexpr (requires { command.is_clip_or_mask(); })
  27. return command.is_clip_or_mask();
  28. else
  29. return false;
  30. });
  31. }
  32. void DisplayListPlayer::execute(DisplayList& display_list)
  33. {
  34. auto const& commands = display_list.commands();
  35. auto const& scroll_state = display_list.scroll_state();
  36. auto device_pixels_per_css_pixel = display_list.device_pixels_per_css_pixel();
  37. size_t next_command_index = 0;
  38. while (next_command_index < commands.size()) {
  39. auto scroll_frame_id = commands[next_command_index].scroll_frame_id;
  40. auto command = commands[next_command_index++].command;
  41. if (command.has<PaintScrollBar>()) {
  42. auto& paint_scroll_bar = command.get<PaintScrollBar>();
  43. auto scroll_offset = scroll_state.own_offset_for_frame_with_id(paint_scroll_bar.scroll_frame_id);
  44. if (paint_scroll_bar.vertical) {
  45. auto offset = scroll_offset.y() * paint_scroll_bar.scroll_size;
  46. paint_scroll_bar.rect.translate_by(0, -offset.to_int() * device_pixels_per_css_pixel);
  47. } else {
  48. auto offset = scroll_offset.x() * paint_scroll_bar.scroll_size;
  49. paint_scroll_bar.rect.translate_by(-offset.to_int() * device_pixels_per_css_pixel, 0);
  50. }
  51. }
  52. if (scroll_frame_id.has_value()) {
  53. auto cumulative_offset = scroll_state.cumulative_offset_for_frame_with_id(scroll_frame_id.value());
  54. auto scroll_offset = cumulative_offset.to_type<double>().scaled(device_pixels_per_css_pixel).to_type<int>();
  55. command.visit(
  56. [&](auto& command) {
  57. if constexpr (requires { command.translate_by(scroll_offset); }) {
  58. command.translate_by(scroll_offset);
  59. }
  60. });
  61. }
  62. auto bounding_rect = command_bounding_rectangle(command);
  63. if (bounding_rect.has_value() && (bounding_rect->is_empty() || would_be_fully_clipped_by_painter(*bounding_rect))) {
  64. // Any clip or mask that's located outside of the visible region is equivalent to a simple clip-rect,
  65. // so replace it with one to avoid doing unnecessary work.
  66. if (command_is_clip_or_mask(command)) {
  67. if (command.has<AddClipRect>()) {
  68. add_clip_rect(command.get<AddClipRect>());
  69. } else {
  70. add_clip_rect({ bounding_rect.release_value() });
  71. }
  72. }
  73. continue;
  74. }
  75. #define HANDLE_COMMAND(command_type, executor_method) \
  76. if (command.has<command_type>()) { \
  77. executor_method(command.get<command_type>()); \
  78. }
  79. // clang-format off
  80. HANDLE_COMMAND(DrawGlyphRun, draw_glyph_run)
  81. else HANDLE_COMMAND(FillRect, fill_rect)
  82. else HANDLE_COMMAND(DrawPaintingSurface, draw_painting_surface)
  83. else HANDLE_COMMAND(DrawScaledImmutableBitmap, draw_scaled_immutable_bitmap)
  84. else HANDLE_COMMAND(DrawRepeatedImmutableBitmap, draw_repeated_immutable_bitmap)
  85. else HANDLE_COMMAND(AddClipRect, add_clip_rect)
  86. else HANDLE_COMMAND(Save, save)
  87. else HANDLE_COMMAND(Restore, restore)
  88. else HANDLE_COMMAND(Translate, translate)
  89. else HANDLE_COMMAND(PushStackingContext, push_stacking_context)
  90. else HANDLE_COMMAND(PopStackingContext, pop_stacking_context)
  91. else HANDLE_COMMAND(PaintLinearGradient, paint_linear_gradient)
  92. else HANDLE_COMMAND(PaintRadialGradient, paint_radial_gradient)
  93. else HANDLE_COMMAND(PaintConicGradient, paint_conic_gradient)
  94. else HANDLE_COMMAND(PaintOuterBoxShadow, paint_outer_box_shadow)
  95. else HANDLE_COMMAND(PaintInnerBoxShadow, paint_inner_box_shadow)
  96. else HANDLE_COMMAND(PaintTextShadow, paint_text_shadow)
  97. else HANDLE_COMMAND(FillRectWithRoundedCorners, fill_rect_with_rounded_corners)
  98. else HANDLE_COMMAND(FillPathUsingColor, fill_path_using_color)
  99. else HANDLE_COMMAND(FillPathUsingPaintStyle, fill_path_using_paint_style)
  100. else HANDLE_COMMAND(StrokePathUsingColor, stroke_path_using_color)
  101. else HANDLE_COMMAND(StrokePathUsingPaintStyle, stroke_path_using_paint_style)
  102. else HANDLE_COMMAND(DrawEllipse, draw_ellipse)
  103. else HANDLE_COMMAND(FillEllipse, fill_ellipse)
  104. else HANDLE_COMMAND(DrawLine, draw_line)
  105. else HANDLE_COMMAND(ApplyBackdropFilter, apply_backdrop_filter)
  106. else HANDLE_COMMAND(DrawRect, draw_rect)
  107. else HANDLE_COMMAND(DrawTriangleWave, draw_triangle_wave)
  108. else HANDLE_COMMAND(AddRoundedRectClip, add_rounded_rect_clip)
  109. else HANDLE_COMMAND(AddMask, add_mask)
  110. else HANDLE_COMMAND(PaintScrollBar, paint_scrollbar)
  111. else HANDLE_COMMAND(PaintNestedDisplayList, paint_nested_display_list)
  112. else HANDLE_COMMAND(ApplyOpacity, apply_opacity)
  113. else HANDLE_COMMAND(ApplyTransform, apply_transform)
  114. else HANDLE_COMMAND(ApplyMaskBitmap, apply_mask_bitmap)
  115. else VERIFY_NOT_REACHED();
  116. // clang-format on
  117. }
  118. }
  119. }