DisplayList.cpp 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. void DisplayList::apply_scroll_offsets(Vector<Gfx::IntPoint> const& offsets_by_frame_id)
  23. {
  24. for (auto& command_with_scroll_id : m_commands) {
  25. if (command_with_scroll_id.scroll_frame_id.has_value()) {
  26. auto const& scroll_frame_id = command_with_scroll_id.scroll_frame_id.value();
  27. auto const& scroll_offset = offsets_by_frame_id[scroll_frame_id];
  28. command_with_scroll_id.command.visit(
  29. [&](auto& command) {
  30. if constexpr (requires { command.translate_by(scroll_offset); })
  31. command.translate_by(scroll_offset);
  32. });
  33. }
  34. }
  35. }
  36. void DisplayListPlayer::execute(DisplayList& display_list)
  37. {
  38. auto const& commands = display_list.commands();
  39. size_t next_command_index = 0;
  40. while (next_command_index < commands.size()) {
  41. auto const& command = commands[next_command_index++].command;
  42. auto bounding_rect = command_bounding_rectangle(command);
  43. if (bounding_rect.has_value() && (bounding_rect->is_empty() || would_be_fully_clipped_by_painter(*bounding_rect))) {
  44. continue;
  45. }
  46. #define HANDLE_COMMAND(command_type, executor_method) \
  47. if (command.has<command_type>()) { \
  48. executor_method(command.get<command_type>()); \
  49. }
  50. // clang-format off
  51. HANDLE_COMMAND(DrawGlyphRun, draw_glyph_run)
  52. else HANDLE_COMMAND(FillRect, fill_rect)
  53. else HANDLE_COMMAND(DrawScaledBitmap, draw_scaled_bitmap)
  54. else HANDLE_COMMAND(DrawScaledImmutableBitmap, draw_scaled_immutable_bitmap)
  55. else HANDLE_COMMAND(DrawRepeatedImmutableBitmap, draw_repeated_immutable_bitmap)
  56. else HANDLE_COMMAND(AddClipRect, add_clip_rect)
  57. else HANDLE_COMMAND(Save, save)
  58. else HANDLE_COMMAND(Restore, restore)
  59. else HANDLE_COMMAND(PushStackingContext, push_stacking_context)
  60. else HANDLE_COMMAND(PopStackingContext, pop_stacking_context)
  61. else HANDLE_COMMAND(PaintLinearGradient, paint_linear_gradient)
  62. else HANDLE_COMMAND(PaintRadialGradient, paint_radial_gradient)
  63. else HANDLE_COMMAND(PaintConicGradient, paint_conic_gradient)
  64. else HANDLE_COMMAND(PaintOuterBoxShadow, paint_outer_box_shadow)
  65. else HANDLE_COMMAND(PaintInnerBoxShadow, paint_inner_box_shadow)
  66. else HANDLE_COMMAND(PaintTextShadow, paint_text_shadow)
  67. else HANDLE_COMMAND(FillRectWithRoundedCorners, fill_rect_with_rounded_corners)
  68. else HANDLE_COMMAND(FillPathUsingColor, fill_path_using_color)
  69. else HANDLE_COMMAND(FillPathUsingPaintStyle, fill_path_using_paint_style)
  70. else HANDLE_COMMAND(StrokePathUsingColor, stroke_path_using_color)
  71. else HANDLE_COMMAND(StrokePathUsingPaintStyle, stroke_path_using_paint_style)
  72. else HANDLE_COMMAND(DrawEllipse, draw_ellipse)
  73. else HANDLE_COMMAND(FillEllipse, fill_ellipse)
  74. else HANDLE_COMMAND(DrawLine, draw_line)
  75. else HANDLE_COMMAND(ApplyBackdropFilter, apply_backdrop_filter)
  76. else HANDLE_COMMAND(DrawRect, draw_rect)
  77. else HANDLE_COMMAND(DrawTriangleWave, draw_triangle_wave)
  78. else HANDLE_COMMAND(AddRoundedRectClip, add_rounded_rect_clip)
  79. else HANDLE_COMMAND(AddMask, add_mask)
  80. else HANDLE_COMMAND(PaintNestedDisplayList, paint_nested_display_list)
  81. else VERIFY_NOT_REACHED();
  82. // clang-format on
  83. }
  84. }
  85. }