PaintingCommandExecutorCPU.cpp 20 KB

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