RecordingPainter.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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/Painting/BorderRadiusCornerClipper.h>
  9. #include <LibWeb/Painting/FilterPainting.h>
  10. #include <LibWeb/Painting/RecordingPainter.h>
  11. #include <LibWeb/Painting/ShadowPainting.h>
  12. namespace Web::Painting {
  13. struct CommandExecutionState {
  14. struct StackingContext {
  15. Gfx::Painter painter;
  16. Gfx::IntRect destination;
  17. float opacity;
  18. };
  19. [[nodiscard]] Gfx::Painter const& painter() const { return stacking_contexts.last().painter; }
  20. [[nodiscard]] Gfx::Painter& painter() { return stacking_contexts.last().painter; }
  21. [[nodiscard]] bool would_be_fully_clipped_by_painter(Gfx::IntRect rect) const
  22. {
  23. return !painter().clip_rect().intersects(rect.translated(painter().translation()));
  24. }
  25. Vector<StackingContext> stacking_contexts;
  26. };
  27. CommandResult FillRectWithRoundedCorners::execute(CommandExecutionState& state) const
  28. {
  29. auto& painter = state.painter();
  30. Gfx::AntiAliasingPainter aa_painter(painter);
  31. if (aa_translation.has_value())
  32. aa_painter.translate(*aa_translation);
  33. aa_painter.fill_rect_with_rounded_corners(
  34. rect,
  35. color,
  36. top_left_radius,
  37. top_right_radius,
  38. bottom_right_radius,
  39. bottom_left_radius);
  40. return CommandResult::Continue;
  41. }
  42. CommandResult DrawText::execute(CommandExecutionState& state) const
  43. {
  44. if (state.would_be_fully_clipped_by_painter(rect))
  45. return CommandResult::Continue;
  46. auto& painter = state.painter();
  47. if (font.has_value()) {
  48. painter.draw_text(rect, raw_text, *font, alignment, color, elision, wrapping);
  49. } else {
  50. painter.draw_text(rect, raw_text, alignment, color, elision, wrapping);
  51. }
  52. return CommandResult::Continue;
  53. }
  54. CommandResult DrawTextRun::execute(CommandExecutionState& state) const
  55. {
  56. if (state.would_be_fully_clipped_by_painter(rect))
  57. return CommandResult::Continue;
  58. auto& painter = state.painter();
  59. painter.draw_text_run(baseline_start, Utf8View(string), font, color);
  60. return CommandResult::Continue;
  61. }
  62. CommandResult FillPathUsingColor::execute(CommandExecutionState& state) const
  63. {
  64. auto& painter = state.painter();
  65. Gfx::AntiAliasingPainter aa_painter(painter);
  66. if (aa_translation.has_value())
  67. aa_painter.translate(*aa_translation);
  68. aa_painter.fill_path(path, color, winding_rule);
  69. return CommandResult::Continue;
  70. }
  71. CommandResult FillPathUsingPaintStyle::execute(CommandExecutionState& state) const
  72. {
  73. auto& painter = state.painter();
  74. Gfx::AntiAliasingPainter aa_painter(painter);
  75. if (aa_translation.has_value())
  76. aa_painter.translate(*aa_translation);
  77. aa_painter.fill_path(path, paint_style, opacity, winding_rule);
  78. return CommandResult::Continue;
  79. }
  80. CommandResult StrokePathUsingColor::execute(CommandExecutionState& state) const
  81. {
  82. auto& painter = state.painter();
  83. Gfx::AntiAliasingPainter aa_painter(painter);
  84. if (aa_translation.has_value())
  85. aa_painter.translate(*aa_translation);
  86. aa_painter.stroke_path(path, color, thickness);
  87. return CommandResult::Continue;
  88. }
  89. CommandResult StrokePathUsingPaintStyle::execute(CommandExecutionState& state) const
  90. {
  91. auto& painter = state.painter();
  92. Gfx::AntiAliasingPainter aa_painter(painter);
  93. if (aa_translation.has_value())
  94. aa_painter.translate(*aa_translation);
  95. aa_painter.stroke_path(path, paint_style, thickness, opacity);
  96. return CommandResult::Continue;
  97. }
  98. CommandResult FillRect::execute(CommandExecutionState& state) const
  99. {
  100. auto& painter = state.painter();
  101. painter.fill_rect(rect, color);
  102. return CommandResult::Continue;
  103. }
  104. CommandResult DrawScaledBitmap::execute(CommandExecutionState& state) const
  105. {
  106. auto& painter = state.painter();
  107. painter.draw_scaled_bitmap(dst_rect, bitmap, src_rect, opacity, scaling_mode);
  108. return CommandResult::Continue;
  109. }
  110. CommandResult SetClipRect::execute(CommandExecutionState& state) const
  111. {
  112. auto& painter = state.painter();
  113. painter.clear_clip_rect();
  114. painter.add_clip_rect(rect);
  115. return CommandResult::Continue;
  116. }
  117. CommandResult ClearClipRect::execute(CommandExecutionState& state) const
  118. {
  119. auto& painter = state.painter();
  120. painter.clear_clip_rect();
  121. return CommandResult::Continue;
  122. }
  123. CommandResult SetFont::execute(CommandExecutionState& state) const
  124. {
  125. auto& painter = state.painter();
  126. painter.set_font(font);
  127. return CommandResult::Continue;
  128. }
  129. CommandResult PushStackingContext::execute(CommandExecutionState& state) const
  130. {
  131. auto& painter = state.painter();
  132. if (semitransparent_or_has_non_identity_transform) {
  133. auto destination_rect = transformed_destination_rect.to_rounded<int>();
  134. // FIXME: We should find a way to scale the paintable, rather than paint into a separate bitmap,
  135. // then scale it. This snippet now copies the background at the destination, then scales it down/up
  136. // to the size of the source (which could add some artefacts, though just scaling the bitmap already does that).
  137. // We need to copy the background at the destination because a bunch of our rendering effects now rely on
  138. // being able to sample the painter (see border radii, shadows, filters, etc).
  139. Gfx::FloatPoint destination_clipped_fixup {};
  140. auto try_get_scaled_destination_bitmap = [&]() -> ErrorOr<NonnullRefPtr<Gfx::Bitmap>> {
  141. Gfx::IntRect actual_destination_rect;
  142. auto bitmap = TRY(painter.get_region_bitmap(destination_rect, Gfx::BitmapFormat::BGRA8888, actual_destination_rect));
  143. // get_region_bitmap() may clip to a smaller region if the requested rect goes outside the painter, so we need to account for that.
  144. destination_clipped_fixup = Gfx::FloatPoint { destination_rect.location() - actual_destination_rect.location() };
  145. destination_rect = actual_destination_rect;
  146. if (source_rect.size() != transformed_destination_rect.size()) {
  147. auto sx = static_cast<float>(source_rect.width()) / transformed_destination_rect.width();
  148. auto sy = static_cast<float>(source_rect.height()) / transformed_destination_rect.height();
  149. bitmap = TRY(bitmap->scaled(sx, sy));
  150. destination_clipped_fixup.scale_by(sx, sy);
  151. }
  152. return bitmap;
  153. };
  154. auto bitmap_or_error = try_get_scaled_destination_bitmap();
  155. if (bitmap_or_error.is_error()) {
  156. // NOTE: If the creation of the bitmap fails, we need to skip all painting commands that belong to this stacking context.
  157. // We don't interrupt the execution of painting commands because get_region_bitmap() returns an error if the requested
  158. // region is outside of the viewport (mmap fails to allocate a zero-size region), which means we can safely proceed
  159. // with execution of commands outside of this stacking context.
  160. // FIXME: Change the get_region_bitmap() API to return ErrorOr<Optional<Bitmap>> and exit the execution of commands here
  161. // if we run out of memory.
  162. return CommandResult::SkipStackingContext;
  163. }
  164. auto bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  165. Gfx::Painter stacking_context_painter(bitmap);
  166. stacking_context_painter.translate(painter_location + destination_clipped_fixup.to_type<int>());
  167. state.stacking_contexts.append(CommandExecutionState::StackingContext {
  168. .painter = stacking_context_painter,
  169. .destination = destination_rect,
  170. .opacity = opacity,
  171. });
  172. } else {
  173. state.painter().save();
  174. }
  175. return CommandResult::Continue;
  176. }
  177. CommandResult PopStackingContext::execute(CommandExecutionState& state) const
  178. {
  179. if (semitransparent_or_has_non_identity_transform) {
  180. auto stacking_context = state.stacking_contexts.take_last();
  181. auto bitmap = stacking_context.painter.target();
  182. auto destination_rect = stacking_context.destination;
  183. if (destination_rect.size() == bitmap->size()) {
  184. state.painter().blit(destination_rect.location(), *bitmap, bitmap->rect(), stacking_context.opacity);
  185. } else {
  186. state.painter().draw_scaled_bitmap(destination_rect, *bitmap, bitmap->rect(), stacking_context.opacity, scaling_mode);
  187. }
  188. } else {
  189. state.painter().restore();
  190. }
  191. return CommandResult::Continue;
  192. }
  193. CommandResult PushStackingContextWithMask::execute(CommandExecutionState& state) const
  194. {
  195. auto bitmap_or_error = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, paint_rect.size());
  196. if (bitmap_or_error.is_error())
  197. return CommandResult::Continue;
  198. auto bitmap = bitmap_or_error.release_value();
  199. Gfx::Painter stacking_context_painter(bitmap);
  200. stacking_context_painter.translate(-paint_rect.location());
  201. state.stacking_contexts.append(CommandExecutionState::StackingContext {
  202. .painter = stacking_context_painter,
  203. .destination = {},
  204. .opacity = 1,
  205. });
  206. return CommandResult::Continue;
  207. }
  208. CommandResult PopStackingContextWithMask::execute(CommandExecutionState& state) const
  209. {
  210. auto stacking_context = state.stacking_contexts.take_last();
  211. auto bitmap = stacking_context.painter.target();
  212. if (mask_bitmap)
  213. bitmap->apply_mask(*mask_bitmap, mask_kind);
  214. state.painter().blit(paint_rect.location(), *bitmap, bitmap->rect(), opacity);
  215. return CommandResult::Continue;
  216. }
  217. CommandResult PaintLinearGradient::execute(CommandExecutionState& state) const
  218. {
  219. auto const& data = linear_gradient_data;
  220. state.painter().fill_rect_with_linear_gradient(
  221. gradient_rect, data.color_stops.list,
  222. data.gradient_angle, data.color_stops.repeat_length);
  223. return CommandResult::Continue;
  224. }
  225. CommandResult PaintRadialGradient::execute(CommandExecutionState& state) const
  226. {
  227. auto& painter = state.painter();
  228. painter.fill_rect_with_radial_gradient(rect, radial_gradient_data.color_stops.list, center, size, radial_gradient_data.color_stops.repeat_length);
  229. return CommandResult::Continue;
  230. }
  231. CommandResult PaintConicGradient::execute(CommandExecutionState& state) const
  232. {
  233. auto& painter = state.painter();
  234. 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);
  235. return CommandResult::Continue;
  236. }
  237. Gfx::IntRect PaintOuterBoxShadow::bounding_rect() const
  238. {
  239. return get_outer_box_shadow_bounding_rect(outer_box_shadow_params);
  240. }
  241. CommandResult PaintOuterBoxShadow::execute(CommandExecutionState& state) const
  242. {
  243. auto& painter = state.painter();
  244. paint_outer_box_shadow(painter, outer_box_shadow_params);
  245. return CommandResult::Continue;
  246. }
  247. CommandResult PaintInnerBoxShadow::execute(CommandExecutionState& state) const
  248. {
  249. auto& painter = state.painter();
  250. paint_inner_box_shadow(painter, outer_box_shadow_params);
  251. return CommandResult::Continue;
  252. }
  253. CommandResult PaintTextShadow::execute(CommandExecutionState& state) const
  254. {
  255. if (state.would_be_fully_clipped_by_painter(text_rect))
  256. return CommandResult::Continue;
  257. // FIXME: Figure out the maximum bitmap size for all shadows and then allocate it once and reuse it?
  258. auto maybe_shadow_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, shadow_bounding_rect.size());
  259. if (maybe_shadow_bitmap.is_error()) {
  260. dbgln("Unable to allocate temporary bitmap {} for text-shadow rendering: {}", shadow_bounding_rect.size(), maybe_shadow_bitmap.error());
  261. return CommandResult::Continue;
  262. }
  263. auto shadow_bitmap = maybe_shadow_bitmap.release_value();
  264. Gfx::Painter shadow_painter { *shadow_bitmap };
  265. // FIXME: "Spread" the shadow somehow.
  266. Gfx::IntPoint baseline_start(text_rect.x(), text_rect.y() + fragment_baseline);
  267. shadow_painter.draw_text_run(baseline_start, Utf8View(text), font, color);
  268. // Blur
  269. Gfx::StackBlurFilter filter(*shadow_bitmap);
  270. filter.process_rgba(blur_radius, color);
  271. auto& painter = state.painter();
  272. painter.blit(draw_location, *shadow_bitmap, shadow_bounding_rect);
  273. return CommandResult::Continue;
  274. }
  275. CommandResult DrawEllipse::execute(CommandExecutionState& state) const
  276. {
  277. auto& painter = state.painter();
  278. Gfx::AntiAliasingPainter aa_painter(painter);
  279. aa_painter.draw_ellipse(rect, color, thickness);
  280. return CommandResult::Continue;
  281. }
  282. CommandResult FillElipse::execute(CommandExecutionState& state) const
  283. {
  284. auto& painter = state.painter();
  285. Gfx::AntiAliasingPainter aa_painter(painter);
  286. aa_painter.fill_ellipse(rect, color, blend_mode);
  287. return CommandResult::Continue;
  288. }
  289. CommandResult DrawLine::execute(CommandExecutionState& state) const
  290. {
  291. if (style == Gfx::Painter::LineStyle::Dotted) {
  292. Gfx::AntiAliasingPainter aa_painter(state.painter());
  293. aa_painter.draw_line(from, to, color, thickness, style, alternate_color);
  294. } else {
  295. state.painter().draw_line(from, to, color, thickness, style, alternate_color);
  296. }
  297. return CommandResult::Continue;
  298. }
  299. CommandResult DrawSignedDistanceField::execute(CommandExecutionState& state) const
  300. {
  301. auto& painter = state.painter();
  302. painter.draw_signed_distance_field(rect, color, sdf, smoothing);
  303. return CommandResult::Continue;
  304. }
  305. CommandResult PaintProgressbar::execute(CommandExecutionState& state) const
  306. {
  307. auto& painter = state.painter();
  308. Gfx::StylePainter::paint_progressbar(painter, progress_rect, palette, min, max, value, text);
  309. Gfx::StylePainter::paint_frame(painter, frame_rect, palette, Gfx::FrameStyle::RaisedBox);
  310. return CommandResult::Continue;
  311. }
  312. CommandResult PaintFrame::execute(CommandExecutionState& state) const
  313. {
  314. auto& painter = state.painter();
  315. Gfx::StylePainter::paint_frame(painter, rect, palette, style);
  316. return CommandResult::Continue;
  317. }
  318. CommandResult ApplyBackdropFilter::execute(CommandExecutionState& state) const
  319. {
  320. auto& painter = state.painter();
  321. // This performs the backdrop filter operation: https://drafts.fxtf.org/filter-effects-2/#backdrop-filter-operation
  322. // Note: The region bitmap can be smaller than the backdrop_region if it's at the edge of canvas.
  323. // Note: This is in DevicePixels, but we use an IntRect because `get_region_bitmap()` below writes to it.
  324. // FIXME: Go through the steps to find the "Backdrop Root Image"
  325. // https://drafts.fxtf.org/filter-effects-2/#BackdropRoot
  326. // 1. Copy the Backdrop Root Image into a temporary buffer, such as a raster image. Call this buffer T’.
  327. Gfx::IntRect actual_region {};
  328. auto maybe_backdrop_bitmap = painter.get_region_bitmap(backdrop_region, Gfx::BitmapFormat::BGRA8888, actual_region);
  329. if (actual_region.is_empty())
  330. return CommandResult::Continue;
  331. if (maybe_backdrop_bitmap.is_error()) {
  332. dbgln("Failed get region bitmap for backdrop-filter");
  333. return CommandResult::Continue;
  334. }
  335. auto backdrop_bitmap = maybe_backdrop_bitmap.release_value();
  336. // 2. Apply the backdrop-filter’s filter operations to the entire contents of T'.
  337. apply_filter_list(*backdrop_bitmap, backdrop_filter.filters);
  338. // 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’.
  339. // 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.
  340. // FIXME: 5. Draw all of element B, including its background, border, and any children elements, into T’.
  341. // FXIME: 6. If element B has any transforms, effects, or clips, apply those to T’.
  342. // 7. Composite the contents of T’ into element B’s parent, using source-over compositing.
  343. painter.blit(actual_region.location(), *backdrop_bitmap, backdrop_bitmap->rect());
  344. return CommandResult::Continue;
  345. }
  346. CommandResult DrawRect::execute(CommandExecutionState& state) const
  347. {
  348. auto& painter = state.painter();
  349. painter.draw_rect(rect, color, rough);
  350. return CommandResult::Continue;
  351. }
  352. CommandResult DrawTriangleWave::execute(CommandExecutionState& state) const
  353. {
  354. auto& painter = state.painter();
  355. painter.draw_triangle_wave(p1, p2, color, amplitude, thickness);
  356. return CommandResult::Continue;
  357. }
  358. Gfx::IntRect SampleUnderCorners::bounding_rect() const
  359. {
  360. return corner_clipper->border_rect().to_type<int>();
  361. }
  362. CommandResult SampleUnderCorners::execute(CommandExecutionState& state) const
  363. {
  364. auto& painter = state.painter();
  365. corner_clipper->sample_under_corners(painter);
  366. return CommandResult::Continue;
  367. }
  368. Gfx::IntRect BlitCornerClipping::bounding_rect() const
  369. {
  370. return corner_clipper->border_rect().to_type<int>();
  371. }
  372. CommandResult BlitCornerClipping::execute(CommandExecutionState& state) const
  373. {
  374. auto& painter = state.painter();
  375. corner_clipper->blit_corner_clipping(painter);
  376. return CommandResult::Continue;
  377. }
  378. void RecordingPainter::sample_under_corners(NonnullRefPtr<BorderRadiusCornerClipper> corner_clipper)
  379. {
  380. push_command(SampleUnderCorners { corner_clipper });
  381. }
  382. void RecordingPainter::blit_corner_clipping(NonnullRefPtr<BorderRadiusCornerClipper> corner_clipper)
  383. {
  384. push_command(BlitCornerClipping { corner_clipper });
  385. }
  386. void RecordingPainter::fill_rect(Gfx::IntRect const& rect, Color color)
  387. {
  388. push_command(FillRect {
  389. .rect = rect,
  390. .color = color,
  391. });
  392. }
  393. void RecordingPainter::fill_path(FillPathUsingColorParams params)
  394. {
  395. auto aa_translation = state().translation.map(params.translation.value_or(Gfx::FloatPoint {}));
  396. auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
  397. push_command(FillPathUsingColor {
  398. .path_bounding_rect = path_bounding_rect,
  399. .path = params.path,
  400. .color = params.color,
  401. .winding_rule = params.winding_rule,
  402. .aa_translation = aa_translation,
  403. });
  404. }
  405. void RecordingPainter::fill_path(FillPathUsingPaintStyleParams params)
  406. {
  407. auto aa_translation = state().translation.map(params.translation.value_or(Gfx::FloatPoint {}));
  408. auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
  409. push_command(FillPathUsingPaintStyle {
  410. .path_bounding_rect = path_bounding_rect,
  411. .path = params.path,
  412. .paint_style = params.paint_style,
  413. .winding_rule = params.winding_rule,
  414. .opacity = params.opacity,
  415. .aa_translation = aa_translation,
  416. });
  417. }
  418. void RecordingPainter::stroke_path(StrokePathUsingColorParams params)
  419. {
  420. auto aa_translation = state().translation.map(params.translation.value_or(Gfx::FloatPoint {}));
  421. auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
  422. push_command(StrokePathUsingColor {
  423. .path_bounding_rect = path_bounding_rect,
  424. .path = params.path,
  425. .color = params.color,
  426. .thickness = params.thickness,
  427. .aa_translation = aa_translation,
  428. });
  429. }
  430. void RecordingPainter::stroke_path(StrokePathUsingPaintStyleParams params)
  431. {
  432. auto aa_translation = state().translation.map(params.translation.value_or(Gfx::FloatPoint {}));
  433. auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
  434. push_command(StrokePathUsingPaintStyle {
  435. .path_bounding_rect = path_bounding_rect,
  436. .path = params.path,
  437. .paint_style = params.paint_style,
  438. .thickness = params.thickness,
  439. .aa_translation = aa_translation,
  440. });
  441. }
  442. void RecordingPainter::draw_ellipse(Gfx::IntRect const& a_rect, Color color, int thickness)
  443. {
  444. push_command(DrawEllipse {
  445. .rect = state().translation.map(a_rect),
  446. .color = color,
  447. .thickness = thickness,
  448. });
  449. }
  450. void RecordingPainter::fill_ellipse(Gfx::IntRect const& a_rect, Color color, Gfx::AntiAliasingPainter::BlendMode blend_mode)
  451. {
  452. push_command(FillElipse {
  453. .rect = state().translation.map(a_rect),
  454. .color = color,
  455. .blend_mode = blend_mode,
  456. });
  457. }
  458. void RecordingPainter::fill_rect_with_linear_gradient(Gfx::IntRect const& gradient_rect, LinearGradientData const& data)
  459. {
  460. push_command(PaintLinearGradient {
  461. .gradient_rect = state().translation.map(gradient_rect),
  462. .linear_gradient_data = data,
  463. });
  464. }
  465. void RecordingPainter::fill_rect_with_conic_gradient(Gfx::IntRect const& rect, ConicGradientData const& data, Gfx::IntPoint const& position)
  466. {
  467. push_command(PaintConicGradient {
  468. .rect = state().translation.map(rect),
  469. .conic_gradient_data = data,
  470. .position = position });
  471. }
  472. void RecordingPainter::fill_rect_with_radial_gradient(Gfx::IntRect const& rect, RadialGradientData const& data, Gfx::IntPoint center, Gfx::IntSize size)
  473. {
  474. push_command(PaintRadialGradient {
  475. .rect = state().translation.map(rect),
  476. .radial_gradient_data = data,
  477. .center = center,
  478. .size = size });
  479. }
  480. void RecordingPainter::draw_rect(Gfx::IntRect const& rect, Color color, bool rough)
  481. {
  482. push_command(DrawRect {
  483. .rect = state().translation.map(rect),
  484. .color = color,
  485. .rough = rough });
  486. }
  487. void RecordingPainter::draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, float opacity, Gfx::Painter::ScalingMode scaling_mode)
  488. {
  489. push_command(DrawScaledBitmap {
  490. .dst_rect = state().translation.map(dst_rect),
  491. .bitmap = bitmap,
  492. .src_rect = src_rect,
  493. .opacity = opacity,
  494. .scaling_mode = scaling_mode,
  495. });
  496. }
  497. void RecordingPainter::draw_line(Gfx::IntPoint from, Gfx::IntPoint to, Color color, int thickness, Gfx::Painter::LineStyle style, Color alternate_color)
  498. {
  499. push_command(DrawLine {
  500. .color = color,
  501. .from = state().translation.map(from),
  502. .to = state().translation.map(to),
  503. .thickness = thickness,
  504. .style = style,
  505. .alternate_color = alternate_color,
  506. });
  507. }
  508. void RecordingPainter::draw_text(Gfx::IntRect const& rect, StringView raw_text, Gfx::TextAlignment alignment, Color color, Gfx::TextElision elision, Gfx::TextWrapping wrapping)
  509. {
  510. push_command(DrawText {
  511. .rect = state().translation.map(rect),
  512. .raw_text = String::from_utf8(raw_text).release_value_but_fixme_should_propagate_errors(),
  513. .alignment = alignment,
  514. .color = color,
  515. .elision = elision,
  516. .wrapping = wrapping,
  517. });
  518. }
  519. void RecordingPainter::draw_text(Gfx::IntRect const& rect, StringView raw_text, Gfx::Font const& font, Gfx::TextAlignment alignment, Color color, Gfx::TextElision elision, Gfx::TextWrapping wrapping)
  520. {
  521. push_command(DrawText {
  522. .rect = state().translation.map(rect),
  523. .raw_text = String::from_utf8(raw_text).release_value_but_fixme_should_propagate_errors(),
  524. .alignment = alignment,
  525. .color = color,
  526. .elision = elision,
  527. .wrapping = wrapping,
  528. .font = font,
  529. });
  530. }
  531. void RecordingPainter::draw_signed_distance_field(Gfx::IntRect const& dst_rect, Color color, Gfx::GrayscaleBitmap const& sdf, float smoothing)
  532. {
  533. push_command(DrawSignedDistanceField {
  534. .rect = state().translation.map(dst_rect),
  535. .color = color,
  536. .sdf = sdf,
  537. .smoothing = smoothing,
  538. });
  539. }
  540. void RecordingPainter::draw_text_run(Gfx::IntPoint baseline_start, Utf8View string, Gfx::Font const& font, Color color, Gfx::IntRect const& rect)
  541. {
  542. push_command(DrawTextRun {
  543. .color = color,
  544. .baseline_start = state().translation.map(baseline_start),
  545. .string = String::from_utf8(string.as_string()).release_value_but_fixme_should_propagate_errors(),
  546. .font = font,
  547. .rect = state().translation.map(rect),
  548. });
  549. }
  550. void RecordingPainter::add_clip_rect(Gfx::IntRect const& rect)
  551. {
  552. auto prev_clip_rect = state().clip_rect;
  553. if (!state().clip_rect.has_value()) {
  554. state().clip_rect = state().translation.map(rect);
  555. } else {
  556. state().clip_rect->intersect(state().translation.map(rect));
  557. }
  558. if (prev_clip_rect != state().clip_rect)
  559. push_command(SetClipRect { .rect = *state().clip_rect });
  560. }
  561. void RecordingPainter::translate(int dx, int dy)
  562. {
  563. m_state_stack.last().translation.translate(dx, dy);
  564. }
  565. void RecordingPainter::translate(Gfx::IntPoint delta)
  566. {
  567. m_state_stack.last().translation.translate(delta.to_type<float>());
  568. }
  569. void RecordingPainter::set_font(Gfx::Font const& font)
  570. {
  571. push_command(SetFont { .font = font });
  572. }
  573. void RecordingPainter::save()
  574. {
  575. m_state_stack.append(m_state_stack.last());
  576. }
  577. void RecordingPainter::restore()
  578. {
  579. auto prev_clip_rect = state().clip_rect;
  580. VERIFY(m_state_stack.size() > 1);
  581. m_state_stack.take_last();
  582. if (state().clip_rect != prev_clip_rect) {
  583. if (state().clip_rect.has_value())
  584. push_command(SetClipRect { .rect = *state().clip_rect });
  585. else
  586. push_command(ClearClipRect {});
  587. }
  588. }
  589. void RecordingPainter::push_stacking_context(PushStackingContextParams params)
  590. {
  591. push_command(PushStackingContext {
  592. .semitransparent_or_has_non_identity_transform = params.semitransparent_or_has_non_identity_transform,
  593. .has_fixed_position = params.has_fixed_position,
  594. .opacity = params.opacity,
  595. .source_rect = state().translation.map(params.source_rect),
  596. .transformed_destination_rect = state().translation.map(params.transformed_destination_rect),
  597. .painter_location = state().translation.map(params.painter_location),
  598. });
  599. if (params.has_fixed_position) {
  600. state().translation.set_translation(0, 0);
  601. }
  602. if (params.semitransparent_or_has_non_identity_transform) {
  603. m_state_stack.append(State());
  604. }
  605. }
  606. void RecordingPainter::pop_stacking_context(PopStackingContextParams params)
  607. {
  608. push_command(PopStackingContext {
  609. .semitransparent_or_has_non_identity_transform = params.semitransparent_or_has_non_identity_transform,
  610. .scaling_mode = params.scaling_mode,
  611. });
  612. if (params.semitransparent_or_has_non_identity_transform) {
  613. m_state_stack.take_last();
  614. }
  615. }
  616. void RecordingPainter::paint_progressbar(Gfx::IntRect frame_rect, Gfx::IntRect progress_rect, Palette palette, int min, int max, int value, StringView text)
  617. {
  618. push_command(PaintProgressbar {
  619. .frame_rect = state().translation.map(frame_rect),
  620. .progress_rect = state().translation.map(progress_rect),
  621. .palette = palette,
  622. .min = min,
  623. .max = max,
  624. .value = value,
  625. .text = text,
  626. });
  627. }
  628. void RecordingPainter::paint_frame(Gfx::IntRect rect, Palette palette, Gfx::FrameStyle style)
  629. {
  630. push_command(PaintFrame { state().translation.map(rect), palette, style });
  631. }
  632. void RecordingPainter::apply_backdrop_filter(Gfx::IntRect const& backdrop_region, BorderRadiiData const& border_radii_data, CSS::ResolvedBackdropFilter const& backdrop_filter)
  633. {
  634. push_command(ApplyBackdropFilter {
  635. .backdrop_region = state().translation.map(backdrop_region),
  636. .border_radii_data = border_radii_data,
  637. .backdrop_filter = backdrop_filter,
  638. });
  639. }
  640. void RecordingPainter::paint_outer_box_shadow_params(PaintOuterBoxShadowParams params)
  641. {
  642. push_command(PaintOuterBoxShadow {
  643. .outer_box_shadow_params = params,
  644. });
  645. }
  646. void RecordingPainter::paint_inner_box_shadow_params(PaintOuterBoxShadowParams params)
  647. {
  648. push_command(PaintInnerBoxShadow {
  649. .outer_box_shadow_params = params,
  650. });
  651. }
  652. void RecordingPainter::paint_text_shadow(int blur_radius, Gfx::IntRect bounding_rect, Gfx::IntRect text_rect, Utf8View text, Gfx::Font const& font, Color color, int fragment_baseline, Gfx::IntPoint draw_location)
  653. {
  654. push_command(PaintTextShadow {
  655. .blur_radius = blur_radius,
  656. .shadow_bounding_rect = state().translation.map(bounding_rect),
  657. .text_rect = state().translation.map(text_rect),
  658. .text = String::from_utf8(text.as_string()).release_value_but_fixme_should_propagate_errors(),
  659. .font = font,
  660. .color = color,
  661. .fragment_baseline = fragment_baseline,
  662. .draw_location = state().translation.map(draw_location) });
  663. }
  664. void RecordingPainter::fill_rect_with_rounded_corners(Gfx::IntRect const& rect, Color color, Gfx::AntiAliasingPainter::CornerRadius top_left_radius, Gfx::AntiAliasingPainter::CornerRadius top_right_radius, Gfx::AntiAliasingPainter::CornerRadius bottom_right_radius, Gfx::AntiAliasingPainter::CornerRadius bottom_left_radius)
  665. {
  666. push_command(FillRectWithRoundedCorners {
  667. .rect = state().translation.map(rect),
  668. .color = color,
  669. .top_left_radius = top_left_radius,
  670. .top_right_radius = top_right_radius,
  671. .bottom_left_radius = bottom_left_radius,
  672. .bottom_right_radius = bottom_right_radius,
  673. });
  674. }
  675. void RecordingPainter::fill_rect_with_rounded_corners(Gfx::IntRect const& a_rect, Color color, int radius)
  676. {
  677. fill_rect_with_rounded_corners(a_rect, color, radius, radius, radius, radius);
  678. }
  679. void RecordingPainter::fill_rect_with_rounded_corners(Gfx::IntRect const& a_rect, Color color, int top_left_radius, int top_right_radius, int bottom_right_radius, int bottom_left_radius)
  680. {
  681. fill_rect_with_rounded_corners(a_rect, color,
  682. { top_left_radius, top_left_radius },
  683. { top_right_radius, top_right_radius },
  684. { bottom_right_radius, bottom_right_radius },
  685. { bottom_left_radius, bottom_left_radius });
  686. }
  687. void RecordingPainter::push_stacking_context_with_mask(Gfx::IntRect paint_rect)
  688. {
  689. push_command(PushStackingContextWithMask { .paint_rect = state().translation.map(paint_rect) });
  690. }
  691. void RecordingPainter::pop_stacking_context_with_mask(RefPtr<Gfx::Bitmap> mask_bitmap, Gfx::Bitmap::MaskKind mask_kind, Gfx::IntRect paint_rect, float opacity)
  692. {
  693. push_command(PopStackingContextWithMask {
  694. .paint_rect = state().translation.map(paint_rect),
  695. .mask_bitmap = mask_bitmap,
  696. .mask_kind = mask_kind,
  697. .opacity = opacity });
  698. }
  699. void RecordingPainter::draw_triangle_wave(Gfx::IntPoint a_p1, Gfx::IntPoint a_p2, Color color, int amplitude, int thickness = 1)
  700. {
  701. push_command(DrawTriangleWave {
  702. .p1 = state().translation.map(a_p1),
  703. .p2 = state().translation.map(a_p2),
  704. .color = color,
  705. .amplitude = amplitude,
  706. .thickness = thickness });
  707. }
  708. static Optional<Gfx::IntRect> command_bounding_rectangle(PaintingCommand const& command)
  709. {
  710. return command.visit(
  711. [&](auto const& command) -> Optional<Gfx::IntRect> {
  712. if constexpr (requires { command.bounding_rect(); })
  713. return command.bounding_rect();
  714. else
  715. return {};
  716. });
  717. }
  718. void RecordingPainter::execute(Gfx::Bitmap& bitmap)
  719. {
  720. CommandExecutionState state;
  721. state.stacking_contexts.append(CommandExecutionState::StackingContext {
  722. .painter = Gfx::Painter(bitmap),
  723. .destination = Gfx::IntRect { 0, 0, 0, 0 },
  724. .opacity = 1,
  725. });
  726. size_t next_command_index = 0;
  727. while (next_command_index < m_painting_commands.size()) {
  728. auto& command = m_painting_commands[next_command_index++];
  729. auto bounding_rect = command_bounding_rectangle(command);
  730. if (bounding_rect.has_value() && state.would_be_fully_clipped_by_painter(*bounding_rect))
  731. continue;
  732. auto result = command.visit([&](auto const& command) { return command.execute(state); });
  733. if (result == CommandResult::SkipStackingContext) {
  734. auto stacking_context_nesting_level = 1;
  735. while (next_command_index < m_painting_commands.size()) {
  736. if (m_painting_commands[next_command_index].has<PushStackingContext>()) {
  737. stacking_context_nesting_level++;
  738. } else if (m_painting_commands[next_command_index].has<PopStackingContext>()) {
  739. stacking_context_nesting_level--;
  740. }
  741. next_command_index++;
  742. if (stacking_context_nesting_level == 0)
  743. break;
  744. }
  745. }
  746. }
  747. VERIFY(state.stacking_contexts.size() == 1);
  748. }
  749. }