CommandList.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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::execute(PaintingCommandExecutor& executor)
  37. {
  38. executor.prepare_to_execute();
  39. if (executor.needs_prepare_glyphs_texture()) {
  40. HashMap<Gfx::Font const*, HashTable<u32>> unique_glyphs;
  41. for (auto& command_with_scroll_id : m_commands) {
  42. auto& command = command_with_scroll_id.command;
  43. if (command.has<DrawGlyphRun>()) {
  44. for (auto const& glyph_or_emoji : command.get<DrawGlyphRun>().glyph_run) {
  45. if (glyph_or_emoji.has<Gfx::DrawGlyph>()) {
  46. auto const& glyph = glyph_or_emoji.get<Gfx::DrawGlyph>();
  47. unique_glyphs.ensure(glyph.font, [] { return HashTable<u32> {}; }).set(glyph.code_point);
  48. }
  49. }
  50. }
  51. }
  52. executor.prepare_glyph_texture(unique_glyphs);
  53. }
  54. if (executor.needs_update_immutable_bitmap_texture_cache()) {
  55. HashMap<u32, Gfx::ImmutableBitmap const*> immutable_bitmaps;
  56. for (auto const& command_with_scroll_id : m_commands) {
  57. auto& command = command_with_scroll_id.command;
  58. if (command.has<DrawScaledImmutableBitmap>()) {
  59. auto const& immutable_bitmap = command.get<DrawScaledImmutableBitmap>().bitmap;
  60. immutable_bitmaps.set(immutable_bitmap->id(), immutable_bitmap.ptr());
  61. }
  62. }
  63. executor.update_immutable_bitmap_texture_cache(immutable_bitmaps);
  64. }
  65. HashTable<u32> skipped_sample_corner_commands;
  66. size_t next_command_index = 0;
  67. while (next_command_index < m_commands.size()) {
  68. auto& command_with_scroll_id = m_commands[next_command_index++];
  69. auto& command = command_with_scroll_id.command;
  70. auto bounding_rect = command_bounding_rectangle(command);
  71. if (bounding_rect.has_value() && (bounding_rect->is_empty() || executor.would_be_fully_clipped_by_painter(*bounding_rect))) {
  72. if (command.has<SampleUnderCorners>()) {
  73. auto const& sample_under_corners = command.get<SampleUnderCorners>();
  74. skipped_sample_corner_commands.set(sample_under_corners.id);
  75. }
  76. continue;
  77. }
  78. auto result = command.visit(
  79. [&](DrawGlyphRun const& command) {
  80. return executor.draw_glyph_run(command.glyph_run, command.color);
  81. },
  82. [&](DrawText const& command) {
  83. return executor.draw_text(command.rect, command.raw_text, command.alignment, command.color,
  84. command.elision, command.wrapping, command.font);
  85. },
  86. [&](FillRect const& command) {
  87. return executor.fill_rect(command.rect, command.color);
  88. },
  89. [&](DrawScaledBitmap const& command) {
  90. return executor.draw_scaled_bitmap(command.dst_rect, command.bitmap, command.src_rect,
  91. command.scaling_mode);
  92. },
  93. [&](DrawScaledImmutableBitmap const& command) {
  94. return executor.draw_scaled_immutable_bitmap(command.dst_rect, command.bitmap, command.src_rect,
  95. command.scaling_mode);
  96. },
  97. [&](SetClipRect const& command) {
  98. return executor.set_clip_rect(command.rect);
  99. },
  100. [&](ClearClipRect const&) {
  101. return executor.clear_clip_rect();
  102. },
  103. [&](PushStackingContext const& command) {
  104. return executor.push_stacking_context(command.opacity, command.is_fixed_position,
  105. command.source_paintable_rect,
  106. command.post_transform_translation,
  107. command.image_rendering, command.transform, command.mask);
  108. },
  109. [&](PopStackingContext const&) {
  110. return executor.pop_stacking_context();
  111. },
  112. [&](PaintLinearGradient const& command) {
  113. return executor.paint_linear_gradient(command.gradient_rect, command.linear_gradient_data);
  114. },
  115. [&](PaintRadialGradient const& command) {
  116. return executor.paint_radial_gradient(command.rect, command.radial_gradient_data,
  117. command.center, command.size);
  118. },
  119. [&](PaintConicGradient const& command) {
  120. return executor.paint_conic_gradient(command.rect, command.conic_gradient_data,
  121. command.position);
  122. },
  123. [&](PaintOuterBoxShadow const& command) {
  124. return executor.paint_outer_box_shadow(command.outer_box_shadow_params);
  125. },
  126. [&](PaintInnerBoxShadow const& command) {
  127. return executor.paint_inner_box_shadow(command.outer_box_shadow_params);
  128. },
  129. [&](PaintTextShadow const& command) {
  130. return executor.paint_text_shadow(command.blur_radius, command.shadow_bounding_rect,
  131. command.text_rect, command.glyph_run, command.color,
  132. command.fragment_baseline, command.draw_location);
  133. },
  134. [&](FillRectWithRoundedCorners const& command) {
  135. return executor.fill_rect_with_rounded_corners(command.rect, command.color,
  136. command.top_left_radius,
  137. command.top_right_radius,
  138. command.bottom_left_radius,
  139. command.bottom_right_radius);
  140. },
  141. [&](FillPathUsingColor const& command) {
  142. return executor.fill_path_using_color(command.path, command.color, command.winding_rule,
  143. command.aa_translation);
  144. },
  145. [&](FillPathUsingPaintStyle const& command) {
  146. return executor.fill_path_using_paint_style(command.path, command.paint_style,
  147. command.winding_rule, command.opacity,
  148. command.aa_translation);
  149. },
  150. [&](StrokePathUsingColor const& command) {
  151. return executor.stroke_path_using_color(command.path, command.color, command.thickness,
  152. command.aa_translation);
  153. },
  154. [&](StrokePathUsingPaintStyle const& command) {
  155. return executor.stroke_path_using_paint_style(command.path, command.paint_style,
  156. command.thickness, command.opacity,
  157. command.aa_translation);
  158. },
  159. [&](DrawEllipse const& command) {
  160. return executor.draw_ellipse(command.rect, command.color, command.thickness);
  161. },
  162. [&](FillEllipse const& command) {
  163. return executor.fill_ellipse(command.rect, command.color, command.blend_mode);
  164. },
  165. [&](DrawLine const& command) {
  166. return executor.draw_line(command.color, command.from, command.to, command.thickness,
  167. command.style, command.alternate_color);
  168. },
  169. [&](DrawSignedDistanceField const& command) {
  170. return executor.draw_signed_distance_field(command.rect, command.color, command.sdf,
  171. command.smoothing);
  172. },
  173. [&](PaintFrame const& command) {
  174. return executor.paint_frame(command.rect, command.palette, command.style);
  175. },
  176. [&](ApplyBackdropFilter const& command) {
  177. return executor.apply_backdrop_filter(command.backdrop_region, command.backdrop_filter);
  178. },
  179. [&](DrawRect const& command) {
  180. return executor.draw_rect(command.rect, command.color, command.rough);
  181. },
  182. [&](DrawTriangleWave const& command) {
  183. return executor.draw_triangle_wave(command.p1, command.p2, command.color, command.amplitude,
  184. command.thickness);
  185. },
  186. [&](SampleUnderCorners const& command) {
  187. return executor.sample_under_corners(command.id, command.corner_radii, command.border_rect,
  188. command.corner_clip);
  189. },
  190. [&](BlitCornerClipping const& command) {
  191. if (skipped_sample_corner_commands.contains(command.id)) {
  192. // FIXME: If a sampling command falls outside the viewport and is not executed, the associated blit
  193. // should also be skipped if it is within the viewport. In a properly generated list of
  194. // painting commands, sample and blit commands should have matching rectangles, preventing
  195. // this discrepancy.
  196. dbgln("Skipping blit_corner_clipping command because the sample_under_corners command was skipped.");
  197. return CommandResult::Continue;
  198. }
  199. return executor.blit_corner_clipping(command.id);
  200. },
  201. [&](PaintBorders const& command) {
  202. return executor.paint_borders(command.border_rect, command.corner_radii, command.borders_data);
  203. });
  204. if (result == CommandResult::SkipStackingContext) {
  205. auto stacking_context_nesting_level = 1;
  206. while (next_command_index < m_commands.size()) {
  207. if (m_commands[next_command_index].command.has<PushStackingContext>()) {
  208. stacking_context_nesting_level++;
  209. } else if (m_commands[next_command_index].command.has<PopStackingContext>()) {
  210. stacking_context_nesting_level--;
  211. }
  212. next_command_index++;
  213. if (stacking_context_nesting_level == 0)
  214. break;
  215. }
  216. }
  217. }
  218. }
  219. }