RecordingPainter.cpp 31 KB

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