CommandList.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. // SetClipRect and ClearClipRect commands do not produce visible output
  61. auto update_clip_command = command.has<SetClipRect>() || command.has<ClearClipRect>();
  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. auto result = command.visit(
  120. [&](DrawGlyphRun const& command) {
  121. return executor.draw_glyph_run(command.glyph_run->glyphs(), command.color, command.translation, command.scale);
  122. },
  123. [&](DrawText const& command) {
  124. return executor.draw_text(command.rect, command.raw_text, command.alignment, command.color,
  125. command.elision, command.wrapping, command.font);
  126. },
  127. [&](FillRect const& command) {
  128. return executor.fill_rect(command.rect, command.color, command.clip_paths);
  129. },
  130. [&](DrawScaledBitmap const& command) {
  131. return executor.draw_scaled_bitmap(command.dst_rect, command.bitmap, command.src_rect,
  132. command.scaling_mode);
  133. },
  134. [&](DrawScaledImmutableBitmap const& command) {
  135. return executor.draw_scaled_immutable_bitmap(command.dst_rect, command.bitmap, command.src_rect,
  136. command.scaling_mode, command.clip_paths);
  137. },
  138. [&](SetClipRect const& command) {
  139. return executor.set_clip_rect(command.rect);
  140. },
  141. [&](ClearClipRect const&) {
  142. return executor.clear_clip_rect();
  143. },
  144. [&](PushStackingContext const& command) {
  145. return executor.push_stacking_context(command.opacity, command.is_fixed_position,
  146. command.source_paintable_rect,
  147. command.post_transform_translation,
  148. command.image_rendering, command.transform, command.mask);
  149. },
  150. [&](PopStackingContext const&) {
  151. return executor.pop_stacking_context();
  152. },
  153. [&](PaintLinearGradient const& command) {
  154. return executor.paint_linear_gradient(command.gradient_rect, command.linear_gradient_data, command.clip_paths);
  155. },
  156. [&](PaintRadialGradient const& command) {
  157. return executor.paint_radial_gradient(command.rect, command.radial_gradient_data,
  158. command.center, command.size, command.clip_paths);
  159. },
  160. [&](PaintConicGradient const& command) {
  161. return executor.paint_conic_gradient(command.rect, command.conic_gradient_data,
  162. command.position, command.clip_paths);
  163. },
  164. [&](PaintOuterBoxShadow const& command) {
  165. return executor.paint_outer_box_shadow(command.outer_box_shadow_params);
  166. },
  167. [&](PaintInnerBoxShadow const& command) {
  168. return executor.paint_inner_box_shadow(command.outer_box_shadow_params);
  169. },
  170. [&](PaintTextShadow const& command) {
  171. return executor.paint_text_shadow(command.blur_radius, command.shadow_bounding_rect,
  172. command.text_rect, command.glyph_run, command.color,
  173. command.fragment_baseline, command.draw_location);
  174. },
  175. [&](FillRectWithRoundedCorners const& command) {
  176. return executor.fill_rect_with_rounded_corners(command.rect, command.color,
  177. command.top_left_radius,
  178. command.top_right_radius,
  179. command.bottom_left_radius,
  180. command.bottom_right_radius,
  181. command.clip_paths);
  182. },
  183. [&](FillPathUsingColor const& command) {
  184. return executor.fill_path_using_color(command.path, command.color, command.winding_rule,
  185. command.aa_translation);
  186. },
  187. [&](FillPathUsingPaintStyle const& command) {
  188. return executor.fill_path_using_paint_style(command.path, command.paint_style,
  189. command.winding_rule, command.opacity,
  190. command.aa_translation);
  191. },
  192. [&](StrokePathUsingColor const& command) {
  193. return executor.stroke_path_using_color(command.path, command.color, command.thickness,
  194. command.aa_translation);
  195. },
  196. [&](StrokePathUsingPaintStyle const& command) {
  197. return executor.stroke_path_using_paint_style(command.path, command.paint_style,
  198. command.thickness, command.opacity,
  199. command.aa_translation);
  200. },
  201. [&](DrawEllipse const& command) {
  202. return executor.draw_ellipse(command.rect, command.color, command.thickness);
  203. },
  204. [&](FillEllipse const& command) {
  205. return executor.fill_ellipse(command.rect, command.color);
  206. },
  207. [&](DrawLine const& command) {
  208. return executor.draw_line(command.color, command.from, command.to, command.thickness,
  209. command.style, command.alternate_color);
  210. },
  211. [&](DrawSignedDistanceField const& command) {
  212. return executor.draw_signed_distance_field(command.rect, command.color, command.sdf,
  213. command.smoothing);
  214. },
  215. [&](ApplyBackdropFilter const& command) {
  216. return executor.apply_backdrop_filter(command.backdrop_region, command.backdrop_filter);
  217. },
  218. [&](DrawRect const& command) {
  219. return executor.draw_rect(command.rect, command.color, command.rough);
  220. },
  221. [&](DrawTriangleWave const& command) {
  222. return executor.draw_triangle_wave(command.p1, command.p2, command.color, command.amplitude,
  223. command.thickness);
  224. },
  225. [&](SampleUnderCorners const& command) {
  226. return executor.sample_under_corners(command.id, command.corner_radii, command.border_rect,
  227. command.corner_clip);
  228. },
  229. [&](BlitCornerClipping const& command) {
  230. if (skipped_sample_corner_commands.contains(command.id)) {
  231. // FIXME: If a sampling command falls outside the viewport and is not executed, the associated blit
  232. // should also be skipped if it is within the viewport. In a properly generated list of
  233. // painting commands, sample and blit commands should have matching rectangles, preventing
  234. // this discrepancy.
  235. dbgln("Skipping blit_corner_clipping command because the sample_under_corners command was skipped.");
  236. return CommandResult::Continue;
  237. }
  238. return executor.blit_corner_clipping(command.id);
  239. },
  240. [&](PaintBorders const& command) {
  241. return executor.paint_borders(command.border_rect, command.corner_radii, command.borders_data);
  242. });
  243. if (result == CommandResult::SkipStackingContext) {
  244. auto stacking_context_nesting_level = 1;
  245. while (next_command_index < m_commands.size()) {
  246. if (m_commands[next_command_index].command.has<PushStackingContext>()) {
  247. stacking_context_nesting_level++;
  248. } else if (m_commands[next_command_index].command.has<PopStackingContext>()) {
  249. stacking_context_nesting_level--;
  250. }
  251. next_command_index++;
  252. if (stacking_context_nesting_level == 0)
  253. break;
  254. }
  255. }
  256. }
  257. }
  258. }