CommandList.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Painting/CommandList.h>
  7. namespace Web::Painting {
  8. void CommandList::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 CommandList::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 CommandList::mark_unnecessary_commands()
  37. {
  38. // The pair sample_under_corners and blit_corner_clipping commands is not needed if there are no painting commands
  39. // in between them that produce visible output.
  40. struct SampleCornersBlitCornersRange {
  41. u32 sample_command_index;
  42. bool has_painting_commands_in_between { false };
  43. };
  44. // Stack of sample_under_corners commands that have not been matched with a blit_corner_clipping command yet.
  45. Vector<SampleCornersBlitCornersRange> sample_blit_ranges;
  46. for (u32 command_index = 0; command_index < m_commands.size(); ++command_index) {
  47. auto const& command = m_commands[command_index].command;
  48. if (command.has<SampleUnderCorners>()) {
  49. sample_blit_ranges.append({
  50. .sample_command_index = command_index,
  51. .has_painting_commands_in_between = false,
  52. });
  53. } else if (command.has<BlitCornerClipping>()) {
  54. auto range = sample_blit_ranges.take_last();
  55. if (!range.has_painting_commands_in_between) {
  56. m_commands[range.sample_command_index].skip = true;
  57. m_commands[command_index].skip = true;
  58. }
  59. } else {
  60. // Save, Restore and AddClipRect commands do not produce visible output
  61. auto update_clip_command = command.has<Save>() || command.has<Restore>() || command.has<AddClipRect>();
  62. if (sample_blit_ranges.size() > 0 && !update_clip_command) {
  63. // If painting command is found for sample_under_corners command on top of the stack, then all
  64. // sample_under_corners commands below should also not be skipped.
  65. for (auto& sample_blit_range : sample_blit_ranges)
  66. sample_blit_range.has_painting_commands_in_between = true;
  67. }
  68. }
  69. }
  70. VERIFY(sample_blit_ranges.is_empty());
  71. }
  72. void CommandList::execute(CommandExecutor& executor)
  73. {
  74. executor.prepare_to_execute(m_corner_clip_max_depth);
  75. if (executor.needs_prepare_glyphs_texture()) {
  76. HashMap<Gfx::Font const*, HashTable<u32>> unique_glyphs;
  77. for (auto& command_with_scroll_id : m_commands) {
  78. auto& command = command_with_scroll_id.command;
  79. if (command.has<DrawGlyphRun>()) {
  80. auto scale = command.get<DrawGlyphRun>().scale;
  81. for (auto const& glyph_or_emoji : command.get<DrawGlyphRun>().glyph_run->glyphs()) {
  82. if (glyph_or_emoji.has<Gfx::DrawGlyph>()) {
  83. auto const& glyph = glyph_or_emoji.get<Gfx::DrawGlyph>();
  84. auto font = glyph.font->with_size(glyph.font->point_size() * static_cast<float>(scale));
  85. unique_glyphs.ensure(font, [] { return HashTable<u32> {}; }).set(glyph.code_point);
  86. }
  87. }
  88. }
  89. }
  90. executor.prepare_glyph_texture(unique_glyphs);
  91. }
  92. if (executor.needs_update_immutable_bitmap_texture_cache()) {
  93. HashMap<u32, Gfx::ImmutableBitmap const*> immutable_bitmaps;
  94. for (auto const& command_with_scroll_id : m_commands) {
  95. auto& command = command_with_scroll_id.command;
  96. if (command.has<DrawScaledImmutableBitmap>()) {
  97. auto const& immutable_bitmap = command.get<DrawScaledImmutableBitmap>().bitmap;
  98. immutable_bitmaps.set(immutable_bitmap->id(), immutable_bitmap.ptr());
  99. }
  100. }
  101. executor.update_immutable_bitmap_texture_cache(immutable_bitmaps);
  102. }
  103. HashTable<u32> skipped_sample_corner_commands;
  104. size_t next_command_index = 0;
  105. while (next_command_index < m_commands.size()) {
  106. if (m_commands[next_command_index].skip) {
  107. next_command_index++;
  108. continue;
  109. }
  110. auto& command = m_commands[next_command_index++].command;
  111. auto bounding_rect = command_bounding_rectangle(command);
  112. if (bounding_rect.has_value() && (bounding_rect->is_empty() || executor.would_be_fully_clipped_by_painter(*bounding_rect))) {
  113. if (command.has<SampleUnderCorners>()) {
  114. auto const& sample_under_corners = command.get<SampleUnderCorners>();
  115. skipped_sample_corner_commands.set(sample_under_corners.id);
  116. }
  117. continue;
  118. }
  119. if (command.has<BlitCornerClipping>()) {
  120. auto const& blit_corner_clipping = command.get<BlitCornerClipping>();
  121. // FIXME: If a sampling command falls outside the viewport and is not executed, the associated blit
  122. // should also be skipped if it is within the viewport. In a properly generated list of
  123. // painting commands, sample and blit commands should have matching rectangles, preventing
  124. // this discrepancy.
  125. if (skipped_sample_corner_commands.contains(blit_corner_clipping.id)) {
  126. dbgln("Skipping blit_corner_clipping command because the sample_under_corners command was skipped.");
  127. continue;
  128. }
  129. }
  130. #define HANDLE_COMMAND(command_type, executor_method) \
  131. if (command.has<command_type>()) { \
  132. result = executor.executor_method(command.get<command_type>()); \
  133. }
  134. // clang-format off
  135. CommandResult result;
  136. HANDLE_COMMAND(DrawGlyphRun, draw_glyph_run)
  137. else HANDLE_COMMAND(DrawText, draw_text)
  138. else HANDLE_COMMAND(FillRect, fill_rect)
  139. else HANDLE_COMMAND(DrawScaledBitmap, draw_scaled_bitmap)
  140. else HANDLE_COMMAND(DrawScaledImmutableBitmap, draw_scaled_immutable_bitmap)
  141. else HANDLE_COMMAND(AddClipRect, add_clip_rect)
  142. else HANDLE_COMMAND(Save, save)
  143. else HANDLE_COMMAND(Restore, restore)
  144. else HANDLE_COMMAND(PushStackingContext, push_stacking_context)
  145. else HANDLE_COMMAND(PopStackingContext, pop_stacking_context)
  146. else HANDLE_COMMAND(PaintLinearGradient, paint_linear_gradient)
  147. else HANDLE_COMMAND(PaintRadialGradient, paint_radial_gradient)
  148. else HANDLE_COMMAND(PaintConicGradient, paint_conic_gradient)
  149. else HANDLE_COMMAND(PaintOuterBoxShadow, paint_outer_box_shadow)
  150. else HANDLE_COMMAND(PaintInnerBoxShadow, paint_inner_box_shadow)
  151. else HANDLE_COMMAND(PaintTextShadow, paint_text_shadow)
  152. else HANDLE_COMMAND(FillRectWithRoundedCorners, fill_rect_with_rounded_corners)
  153. else HANDLE_COMMAND(FillPathUsingColor, fill_path_using_color)
  154. else HANDLE_COMMAND(FillPathUsingPaintStyle, fill_path_using_paint_style)
  155. else HANDLE_COMMAND(StrokePathUsingColor, stroke_path_using_color)
  156. else HANDLE_COMMAND(StrokePathUsingPaintStyle, stroke_path_using_paint_style)
  157. else HANDLE_COMMAND(DrawEllipse, draw_ellipse)
  158. else HANDLE_COMMAND(FillEllipse, fill_ellipse)
  159. else HANDLE_COMMAND(DrawLine, draw_line)
  160. else HANDLE_COMMAND(ApplyBackdropFilter, apply_backdrop_filter)
  161. else HANDLE_COMMAND(DrawRect, draw_rect)
  162. else HANDLE_COMMAND(DrawTriangleWave, draw_triangle_wave)
  163. else HANDLE_COMMAND(SampleUnderCorners, sample_under_corners)
  164. else HANDLE_COMMAND(BlitCornerClipping, blit_corner_clipping)
  165. else VERIFY_NOT_REACHED();
  166. // clang-format on
  167. if (result == CommandResult::SkipStackingContext) {
  168. auto stacking_context_nesting_level = 1;
  169. while (next_command_index < m_commands.size()) {
  170. if (m_commands[next_command_index].command.has<PushStackingContext>()) {
  171. stacking_context_nesting_level++;
  172. } else if (m_commands[next_command_index].command.has<PopStackingContext>()) {
  173. stacking_context_nesting_level--;
  174. }
  175. next_command_index++;
  176. if (stacking_context_nesting_level == 0)
  177. break;
  178. }
  179. }
  180. }
  181. }
  182. }