CommandList.cpp 11 KB

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