CommandExecutorCPU.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Filters/StackBlurFilter.h>
  7. #include <LibGfx/Painter.h>
  8. #include <LibWeb/CSS/ComputedValues.h>
  9. #include <LibWeb/Painting/BorderRadiusCornerClipper.h>
  10. #include <LibWeb/Painting/CommandExecutorCPU.h>
  11. #include <LibWeb/Painting/DisplayListRecorder.h>
  12. #include <LibWeb/Painting/FilterPainting.h>
  13. #include <LibWeb/Painting/ShadowPainting.h>
  14. namespace Web::Painting {
  15. CommandExecutorCPU::CommandExecutorCPU(Gfx::Bitmap& bitmap)
  16. : m_target_bitmap(bitmap)
  17. {
  18. stacking_contexts.append({ .painter = AK::make<Gfx::Painter>(bitmap),
  19. .opacity = 1.0f,
  20. .destination = {},
  21. .scaling_mode = {} });
  22. }
  23. CommandExecutorCPU::~CommandExecutorCPU() = default;
  24. CommandResult CommandExecutorCPU::draw_glyph_run(DrawGlyphRun const& command)
  25. {
  26. auto& painter = this->painter();
  27. auto const& glyphs = command.glyph_run->glyphs();
  28. for (auto& glyph_or_emoji : glyphs) {
  29. auto transformed_glyph = glyph_or_emoji;
  30. transformed_glyph.visit([&](auto& glyph) {
  31. glyph.position = glyph.position.scaled(command.scale).translated(command.translation);
  32. glyph.font = glyph.font->with_size(glyph.font->point_size() * static_cast<float>(command.scale));
  33. });
  34. if (glyph_or_emoji.has<Gfx::DrawGlyph>()) {
  35. auto& glyph = transformed_glyph.get<Gfx::DrawGlyph>();
  36. painter.draw_glyph(glyph.position, glyph.code_point, *glyph.font, command.color);
  37. } else {
  38. auto& emoji = transformed_glyph.get<Gfx::DrawEmoji>();
  39. painter.draw_emoji(emoji.position.to_type<int>(), *emoji.emoji, *emoji.font);
  40. }
  41. }
  42. return CommandResult::Continue;
  43. }
  44. template<typename Callback>
  45. void apply_clip_paths_to_painter(Gfx::IntRect const& rect, Callback callback, Vector<Gfx::Path> const& clip_paths, Gfx::Painter& target_painter)
  46. {
  47. // Setup a painter for a background canvas that we will paint to first.
  48. auto background_canvas = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, rect.size()).release_value_but_fixme_should_propagate_errors();
  49. Gfx::Painter painter(*background_canvas);
  50. // Offset the painter to paint in the correct location.
  51. painter.translate(-rect.location());
  52. // Paint the background canvas.
  53. callback(painter);
  54. // Apply the clip path to the target painter.
  55. Gfx::AntiAliasingPainter aa_painter(target_painter);
  56. for (auto const& clip_path : clip_paths) {
  57. auto fill_offset = clip_path.bounding_box().location().to_type<int>() - rect.location();
  58. auto paint_style = Gfx::BitmapPaintStyle::create(*background_canvas, fill_offset).release_value_but_fixme_should_propagate_errors();
  59. aa_painter.fill_path(clip_path, paint_style);
  60. }
  61. }
  62. CommandResult CommandExecutorCPU::fill_rect(FillRect const& command)
  63. {
  64. auto paint_op = [&](Gfx::Painter& painter) {
  65. painter.fill_rect(command.rect, command.color);
  66. };
  67. if (command.clip_paths.is_empty()) {
  68. paint_op(painter());
  69. } else {
  70. apply_clip_paths_to_painter(command.rect, paint_op, command.clip_paths, painter());
  71. }
  72. return CommandResult::Continue;
  73. }
  74. CommandResult CommandExecutorCPU::draw_scaled_bitmap(DrawScaledBitmap const& command)
  75. {
  76. auto& painter = this->painter();
  77. painter.draw_scaled_bitmap(command.dst_rect, command.bitmap, command.src_rect, 1, command.scaling_mode);
  78. return CommandResult::Continue;
  79. }
  80. CommandResult CommandExecutorCPU::draw_scaled_immutable_bitmap(DrawScaledImmutableBitmap const& command)
  81. {
  82. auto paint_op = [&](Gfx::Painter& painter) {
  83. painter.draw_scaled_bitmap(command.dst_rect, command.bitmap->bitmap(), command.src_rect, 1, command.scaling_mode);
  84. };
  85. if (command.clip_paths.is_empty()) {
  86. paint_op(painter());
  87. } else {
  88. apply_clip_paths_to_painter(command.dst_rect, paint_op, command.clip_paths, painter());
  89. }
  90. return CommandResult::Continue;
  91. }
  92. CommandResult CommandExecutorCPU::save(Save const&)
  93. {
  94. painter().save();
  95. return CommandResult::Continue;
  96. }
  97. CommandResult CommandExecutorCPU::restore(Restore const&)
  98. {
  99. painter().restore();
  100. return CommandResult::Continue;
  101. }
  102. CommandResult CommandExecutorCPU::add_clip_rect(AddClipRect const& command)
  103. {
  104. painter().add_clip_rect(command.rect);
  105. return CommandResult::Continue;
  106. }
  107. CommandResult CommandExecutorCPU::push_stacking_context(PushStackingContext const& command)
  108. {
  109. // FIXME: This extracts the affine 2D part of the full transformation matrix.
  110. // Use the whole matrix when we get better transformation support in LibGfx or use LibGL for drawing the bitmap
  111. auto affine_transform = Gfx::extract_2d_affine_transform(command.transform.matrix);
  112. painter().save();
  113. if (command.is_fixed_position)
  114. painter().translate(-painter().translation());
  115. if (command.mask.has_value()) {
  116. // TODO: Support masks and other stacking context features at the same time.
  117. // Note: Currently only SVG masking is implemented (which does not use CSS transforms anyway).
  118. auto bitmap_or_error = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, command.mask->mask_bitmap->size());
  119. if (bitmap_or_error.is_error())
  120. return CommandResult::Continue;
  121. auto bitmap = bitmap_or_error.release_value();
  122. stacking_contexts.append(StackingContext {
  123. .painter = AK::make<Gfx::Painter>(bitmap),
  124. .opacity = 1,
  125. .destination = command.source_paintable_rect.translated(command.post_transform_translation),
  126. .scaling_mode = Gfx::ScalingMode::None,
  127. .mask = command.mask });
  128. painter().translate(-command.source_paintable_rect.location());
  129. return CommandResult::Continue;
  130. }
  131. if (command.opacity == 1.0f && affine_transform.is_identity_or_translation()) {
  132. // OPTIMIZATION: This is a simple translation use previous stacking context's painter.
  133. painter().translate(affine_transform.translation().to_rounded<int>() + command.post_transform_translation);
  134. stacking_contexts.append(StackingContext {
  135. .painter = MaybeOwned(painter()),
  136. .opacity = 1,
  137. .destination = {},
  138. .scaling_mode = {} });
  139. return CommandResult::Continue;
  140. }
  141. auto& current_painter = this->painter();
  142. auto source_rect = command.source_paintable_rect.to_type<float>().translated(-command.transform.origin);
  143. auto transformed_destination_rect = affine_transform.map(source_rect).translated(command.transform.origin);
  144. auto destination_rect = transformed_destination_rect.to_rounded<int>();
  145. // FIXME: We should find a way to scale the paintable, rather than paint into a separate bitmap,
  146. // then scale it. This snippet now copies the background at the destination, then scales it down/up
  147. // to the size of the source (which could add some artefacts, though just scaling the bitmap already does that).
  148. // We need to copy the background at the destination because a bunch of our rendering effects now rely on
  149. // being able to sample the painter (see border radii, shadows, filters, etc).
  150. Gfx::FloatPoint destination_clipped_fixup {};
  151. auto try_get_scaled_destination_bitmap = [&]() -> ErrorOr<NonnullRefPtr<Gfx::Bitmap>> {
  152. Gfx::IntRect actual_destination_rect;
  153. auto bitmap = TRY(current_painter.get_region_bitmap(destination_rect, Gfx::BitmapFormat::BGRA8888, actual_destination_rect));
  154. // get_region_bitmap() may clip to a smaller region if the requested rect goes outside the painter, so we need to account for that.
  155. destination_clipped_fixup = Gfx::FloatPoint { destination_rect.location() - actual_destination_rect.location() };
  156. destination_rect = actual_destination_rect;
  157. if (source_rect.size() != transformed_destination_rect.size()) {
  158. auto sx = static_cast<float>(source_rect.width()) / transformed_destination_rect.width();
  159. auto sy = static_cast<float>(source_rect.height()) / transformed_destination_rect.height();
  160. bitmap = TRY(bitmap->scaled(sx, sy));
  161. destination_clipped_fixup.scale_by(sx, sy);
  162. }
  163. return bitmap;
  164. };
  165. auto bitmap_or_error = try_get_scaled_destination_bitmap();
  166. if (bitmap_or_error.is_error()) {
  167. // NOTE: If the creation of the bitmap fails, we need to skip all painting commands that belong to this stacking context.
  168. // We don't interrupt the execution of painting commands because get_region_bitmap() returns an error if the requested
  169. // region is outside of the viewport (mmap fails to allocate a zero-size region), which means we can safely proceed
  170. // with execution of commands outside of this stacking context.
  171. // FIXME: Change the get_region_bitmap() API to return ErrorOr<Optional<Bitmap>> and exit the execution of commands here
  172. // if we run out of memory.
  173. painter().restore();
  174. return CommandResult::SkipStackingContext;
  175. }
  176. auto bitmap = bitmap_or_error.release_value();
  177. stacking_contexts.append(StackingContext {
  178. .painter = AK::make<Gfx::Painter>(bitmap),
  179. .opacity = command.opacity,
  180. .destination = destination_rect.translated(command.post_transform_translation),
  181. .scaling_mode = CSS::to_gfx_scaling_mode(command.image_rendering, destination_rect, destination_rect) });
  182. painter().translate(-command.source_paintable_rect.location() + destination_clipped_fixup.to_type<int>());
  183. return CommandResult::Continue;
  184. }
  185. CommandResult CommandExecutorCPU::pop_stacking_context(PopStackingContext const&)
  186. {
  187. ScopeGuard restore_painter = [&] {
  188. painter().restore();
  189. };
  190. auto stacking_context = stacking_contexts.take_last();
  191. // Stacking contexts that don't own their painter are simple translations, and don't need to blit anything back.
  192. if (stacking_context.painter.is_owned()) {
  193. auto& bitmap = stacking_context.painter->target();
  194. if (stacking_context.mask.has_value())
  195. bitmap.apply_mask(*stacking_context.mask->mask_bitmap, stacking_context.mask->mask_kind);
  196. auto destination_rect = stacking_context.destination;
  197. if (destination_rect.size() == bitmap.size()) {
  198. painter().blit(destination_rect.location(), bitmap, bitmap.rect(), stacking_context.opacity);
  199. } else {
  200. painter().draw_scaled_bitmap(destination_rect, bitmap, bitmap.rect(), stacking_context.opacity, stacking_context.scaling_mode);
  201. }
  202. }
  203. return CommandResult::Continue;
  204. }
  205. CommandResult CommandExecutorCPU::paint_linear_gradient(PaintLinearGradient const& command)
  206. {
  207. auto const& linear_gradient_data = command.linear_gradient_data;
  208. auto paint_op = [&](Gfx::Painter& painter) {
  209. painter.fill_rect_with_linear_gradient(
  210. command.gradient_rect, linear_gradient_data.color_stops.list,
  211. linear_gradient_data.gradient_angle, linear_gradient_data.color_stops.repeat_length);
  212. };
  213. if (command.clip_paths.is_empty()) {
  214. paint_op(painter());
  215. } else {
  216. apply_clip_paths_to_painter(command.gradient_rect, paint_op, command.clip_paths, painter());
  217. }
  218. return CommandResult::Continue;
  219. }
  220. CommandResult CommandExecutorCPU::paint_outer_box_shadow(PaintOuterBoxShadow const& command)
  221. {
  222. auto& painter = this->painter();
  223. Web::Painting::paint_outer_box_shadow(painter, command.box_shadow_params);
  224. return CommandResult::Continue;
  225. }
  226. CommandResult CommandExecutorCPU::paint_inner_box_shadow(PaintInnerBoxShadow const& command)
  227. {
  228. auto& painter = this->painter();
  229. Web::Painting::paint_inner_box_shadow(painter, command.box_shadow_params);
  230. return CommandResult::Continue;
  231. }
  232. CommandResult CommandExecutorCPU::paint_text_shadow(PaintTextShadow const& command)
  233. {
  234. // FIXME: Figure out the maximum bitmap size for all shadows and then allocate it once and reuse it?
  235. auto maybe_shadow_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, command.shadow_bounding_rect.size());
  236. if (maybe_shadow_bitmap.is_error()) {
  237. dbgln("Unable to allocate temporary bitmap {} for text-shadow rendering: {}", command.shadow_bounding_rect.size(), maybe_shadow_bitmap.error());
  238. return CommandResult::Continue;
  239. }
  240. auto shadow_bitmap = maybe_shadow_bitmap.release_value();
  241. Gfx::Painter shadow_painter { *shadow_bitmap };
  242. // FIXME: "Spread" the shadow somehow.
  243. Gfx::IntPoint const baseline_start(command.text_rect.x(), command.text_rect.y() + command.fragment_baseline);
  244. shadow_painter.translate(baseline_start);
  245. for (auto const& glyph_or_emoji : command.glyph_run) {
  246. if (glyph_or_emoji.has<Gfx::DrawGlyph>()) {
  247. auto const& glyph = glyph_or_emoji.get<Gfx::DrawGlyph>();
  248. shadow_painter.draw_glyph(glyph.position, glyph.code_point, *glyph.font, command.color);
  249. } else {
  250. auto const& emoji = glyph_or_emoji.get<Gfx::DrawEmoji>();
  251. shadow_painter.draw_emoji(emoji.position.to_type<int>(), *emoji.emoji, *emoji.font);
  252. }
  253. }
  254. // Blur
  255. Gfx::StackBlurFilter filter(*shadow_bitmap);
  256. filter.process_rgba(command.blur_radius, command.color);
  257. painter().blit(command.draw_location, *shadow_bitmap, command.shadow_bounding_rect);
  258. return CommandResult::Continue;
  259. }
  260. CommandResult CommandExecutorCPU::fill_rect_with_rounded_corners(FillRectWithRoundedCorners const& command)
  261. {
  262. auto paint_op = [&](Gfx::Painter& painter) {
  263. Gfx::AntiAliasingPainter aa_painter(painter);
  264. aa_painter.fill_rect_with_rounded_corners(
  265. command.rect,
  266. command.color,
  267. command.top_left_radius,
  268. command.top_right_radius,
  269. command.bottom_right_radius,
  270. command.bottom_left_radius);
  271. };
  272. if (command.clip_paths.is_empty()) {
  273. paint_op(painter());
  274. } else {
  275. apply_clip_paths_to_painter(command.rect, paint_op, command.clip_paths, painter());
  276. }
  277. return CommandResult::Continue;
  278. }
  279. CommandResult CommandExecutorCPU::fill_path_using_color(FillPathUsingColor const& command)
  280. {
  281. Gfx::AntiAliasingPainter aa_painter(painter());
  282. aa_painter.translate(command.aa_translation);
  283. aa_painter.fill_path(command.path, command.color, command.winding_rule);
  284. return CommandResult::Continue;
  285. }
  286. CommandResult CommandExecutorCPU::fill_path_using_paint_style(FillPathUsingPaintStyle const& command)
  287. {
  288. Gfx::AntiAliasingPainter aa_painter(painter());
  289. auto gfx_paint_style = command.paint_style->create_gfx_paint_style();
  290. aa_painter.translate(command.aa_translation);
  291. aa_painter.fill_path(command.path, gfx_paint_style, command.opacity, command.winding_rule);
  292. return CommandResult::Continue;
  293. }
  294. CommandResult CommandExecutorCPU::stroke_path_using_color(StrokePathUsingColor const& command)
  295. {
  296. Gfx::AntiAliasingPainter aa_painter(painter());
  297. aa_painter.translate(command.aa_translation);
  298. aa_painter.stroke_path(command.path, command.color, command.thickness);
  299. return CommandResult::Continue;
  300. }
  301. CommandResult CommandExecutorCPU::stroke_path_using_paint_style(StrokePathUsingPaintStyle const& command)
  302. {
  303. Gfx::AntiAliasingPainter aa_painter(painter());
  304. auto gfx_paint_style = command.paint_style->create_gfx_paint_style();
  305. aa_painter.translate(command.aa_translation);
  306. aa_painter.stroke_path(command.path, gfx_paint_style, command.thickness, command.opacity);
  307. return CommandResult::Continue;
  308. }
  309. CommandResult CommandExecutorCPU::draw_ellipse(DrawEllipse const& command)
  310. {
  311. Gfx::AntiAliasingPainter aa_painter(painter());
  312. aa_painter.draw_ellipse(command.rect, command.color, command.thickness);
  313. return CommandResult::Continue;
  314. }
  315. CommandResult CommandExecutorCPU::fill_ellipse(FillEllipse const& command)
  316. {
  317. Gfx::AntiAliasingPainter aa_painter(painter());
  318. aa_painter.fill_ellipse(command.rect, command.color);
  319. return CommandResult::Continue;
  320. }
  321. CommandResult CommandExecutorCPU::draw_line(DrawLine const& command)
  322. {
  323. if (command.style == Gfx::LineStyle::Dotted) {
  324. Gfx::AntiAliasingPainter aa_painter(painter());
  325. aa_painter.draw_line(command.from, command.to, command.color, command.thickness, command.style, command.alternate_color);
  326. } else {
  327. painter().draw_line(command.from, command.to, command.color, command.thickness, command.style, command.alternate_color);
  328. }
  329. return CommandResult::Continue;
  330. }
  331. CommandResult CommandExecutorCPU::apply_backdrop_filter(ApplyBackdropFilter const& command)
  332. {
  333. auto& painter = this->painter();
  334. // This performs the backdrop filter operation: https://drafts.fxtf.org/filter-effects-2/#backdrop-filter-operation
  335. // Note: The region bitmap can be smaller than the backdrop_region if it's at the edge of canvas.
  336. // Note: This is in DevicePixels, but we use an IntRect because `get_region_bitmap()` below writes to it.
  337. // FIXME: Go through the steps to find the "Backdrop Root Image"
  338. // https://drafts.fxtf.org/filter-effects-2/#BackdropRoot
  339. // 1. Copy the Backdrop Root Image into a temporary buffer, such as a raster image. Call this buffer T’.
  340. Gfx::IntRect actual_region {};
  341. auto maybe_backdrop_bitmap = painter.get_region_bitmap(command.backdrop_region, Gfx::BitmapFormat::BGRA8888, actual_region);
  342. if (actual_region.is_empty())
  343. return CommandResult::Continue;
  344. if (maybe_backdrop_bitmap.is_error()) {
  345. dbgln("Failed get region bitmap for backdrop-filter");
  346. return CommandResult::Continue;
  347. }
  348. auto backdrop_bitmap = maybe_backdrop_bitmap.release_value();
  349. // 2. Apply the backdrop-filter’s filter operations to the entire contents of T'.
  350. apply_filter_list(*backdrop_bitmap, command.backdrop_filter.filters);
  351. // FIXME: 3. If element B has any transforms (between B and the Backdrop Root), apply the inverse of those transforms to the contents of T’.
  352. // 4. Apply a clip to the contents of T’, using the border box of element B, including border-radius if specified. Note that the children of B are not considered for the sizing or location of this clip.
  353. // FIXME: 5. Draw all of element B, including its background, border, and any children elements, into T’.
  354. // FXIME: 6. If element B has any transforms, effects, or clips, apply those to T’.
  355. // 7. Composite the contents of T’ into element B’s parent, using source-over compositing.
  356. painter.blit(actual_region.location(), *backdrop_bitmap, backdrop_bitmap->rect());
  357. return CommandResult::Continue;
  358. }
  359. CommandResult CommandExecutorCPU::draw_rect(DrawRect const& command)
  360. {
  361. painter().draw_rect(command.rect, command.color, command.rough);
  362. return CommandResult::Continue;
  363. }
  364. CommandResult CommandExecutorCPU::paint_radial_gradient(PaintRadialGradient const& command)
  365. {
  366. auto paint_op = [&](Gfx::Painter& painter) {
  367. painter.fill_rect_with_radial_gradient(command.rect, command.radial_gradient_data.color_stops.list, command.center, command.size, command.radial_gradient_data.color_stops.repeat_length);
  368. };
  369. if (command.clip_paths.is_empty()) {
  370. paint_op(painter());
  371. } else {
  372. apply_clip_paths_to_painter(command.rect, paint_op, command.clip_paths, painter());
  373. }
  374. return CommandResult::Continue;
  375. }
  376. CommandResult CommandExecutorCPU::paint_conic_gradient(PaintConicGradient const& command)
  377. {
  378. auto paint_op = [&](Gfx::Painter& painter) {
  379. painter.fill_rect_with_conic_gradient(command.rect, command.conic_gradient_data.color_stops.list, command.position, command.conic_gradient_data.start_angle, command.conic_gradient_data.color_stops.repeat_length);
  380. };
  381. if (command.clip_paths.is_empty()) {
  382. paint_op(painter());
  383. } else {
  384. apply_clip_paths_to_painter(command.rect, paint_op, command.clip_paths, painter());
  385. }
  386. return CommandResult::Continue;
  387. }
  388. CommandResult CommandExecutorCPU::draw_triangle_wave(DrawTriangleWave const& command)
  389. {
  390. painter().draw_triangle_wave(command.p1, command.p2, command.color, command.amplitude, command.thickness);
  391. return CommandResult::Continue;
  392. }
  393. void CommandExecutorCPU::prepare_to_execute(size_t corner_clip_max_depth)
  394. {
  395. m_corner_clippers_stack.ensure_capacity(corner_clip_max_depth);
  396. }
  397. CommandResult CommandExecutorCPU::sample_under_corners(SampleUnderCorners const& command)
  398. {
  399. auto clipper = BorderRadiusCornerClipper::create(command.corner_radii, command.border_rect.to_type<DevicePixels>(), command.corner_clip).release_value();
  400. clipper->sample_under_corners(painter());
  401. m_corner_clippers_stack.append(clipper);
  402. return CommandResult::Continue;
  403. }
  404. CommandResult CommandExecutorCPU::blit_corner_clipping(BlitCornerClipping const&)
  405. {
  406. auto clipper = m_corner_clippers_stack.take_last();
  407. clipper->blit_corner_clipping(painter());
  408. return CommandResult::Continue;
  409. }
  410. bool CommandExecutorCPU::would_be_fully_clipped_by_painter(Gfx::IntRect rect) const
  411. {
  412. return !painter().clip_rect().intersects(rect.translated(painter().translation()));
  413. }
  414. }