RecordingPainter.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright (c) 2023-2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Painting/RecordingPainter.h>
  7. #include <LibWeb/Painting/ShadowPainting.h>
  8. namespace Web::Painting {
  9. RecordingPainter::RecordingPainter(CommandList& command_list)
  10. : m_command_list(command_list)
  11. {
  12. m_state_stack.append(State());
  13. }
  14. RecordingPainter::~RecordingPainter()
  15. {
  16. VERIFY(m_corner_clip_state_stack.is_empty());
  17. }
  18. void RecordingPainter::append(Command&& command)
  19. {
  20. m_command_list.append(move(command), state().scroll_frame_id);
  21. }
  22. void RecordingPainter::sample_under_corners(u32 id, CornerRadii corner_radii, Gfx::IntRect border_rect, CornerClip corner_clip)
  23. {
  24. m_corner_clip_state_stack.append({ id, border_rect });
  25. if (m_corner_clip_state_stack.size() > commands_list().corner_clip_max_depth())
  26. commands_list().set_corner_clip_max_depth(m_corner_clip_state_stack.size());
  27. append(SampleUnderCorners {
  28. id,
  29. corner_radii,
  30. border_rect = state().translation.map(border_rect),
  31. corner_clip });
  32. }
  33. void RecordingPainter::blit_corner_clipping(u32 id)
  34. {
  35. auto clip_state = m_corner_clip_state_stack.take_last();
  36. VERIFY(clip_state.id == id);
  37. append(BlitCornerClipping { id, state().translation.map(clip_state.rect) });
  38. }
  39. void RecordingPainter::fill_rect(Gfx::IntRect const& rect, Color color, Vector<Gfx::Path> const& clip_paths)
  40. {
  41. if (rect.is_empty())
  42. return;
  43. append(FillRect {
  44. .rect = state().translation.map(rect),
  45. .color = color,
  46. .clip_paths = clip_paths,
  47. });
  48. }
  49. void RecordingPainter::fill_path(FillPathUsingColorParams params)
  50. {
  51. auto aa_translation = state().translation.map(params.translation.value_or(Gfx::FloatPoint {}));
  52. auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
  53. if (path_bounding_rect.is_empty())
  54. return;
  55. append(FillPathUsingColor {
  56. .path_bounding_rect = path_bounding_rect,
  57. .path = params.path,
  58. .color = params.color,
  59. .winding_rule = params.winding_rule,
  60. .aa_translation = aa_translation,
  61. });
  62. }
  63. void RecordingPainter::fill_path(FillPathUsingPaintStyleParams params)
  64. {
  65. auto aa_translation = state().translation.map(params.translation.value_or(Gfx::FloatPoint {}));
  66. auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
  67. if (path_bounding_rect.is_empty())
  68. return;
  69. append(FillPathUsingPaintStyle {
  70. .path_bounding_rect = path_bounding_rect,
  71. .path = params.path,
  72. .paint_style = params.paint_style,
  73. .winding_rule = params.winding_rule,
  74. .opacity = params.opacity,
  75. .aa_translation = aa_translation,
  76. });
  77. }
  78. void RecordingPainter::stroke_path(StrokePathUsingColorParams params)
  79. {
  80. auto aa_translation = state().translation.map(params.translation.value_or(Gfx::FloatPoint {}));
  81. auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
  82. // Increase path bounding box by `thickness` to account for stroke.
  83. path_bounding_rect.inflate(params.thickness, params.thickness);
  84. if (path_bounding_rect.is_empty())
  85. return;
  86. append(StrokePathUsingColor {
  87. .path_bounding_rect = path_bounding_rect,
  88. .path = params.path,
  89. .color = params.color,
  90. .thickness = params.thickness,
  91. .aa_translation = aa_translation,
  92. });
  93. }
  94. void RecordingPainter::stroke_path(StrokePathUsingPaintStyleParams params)
  95. {
  96. auto aa_translation = state().translation.map(params.translation.value_or(Gfx::FloatPoint {}));
  97. auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
  98. // Increase path bounding box by `thickness` to account for stroke.
  99. path_bounding_rect.inflate(params.thickness, params.thickness);
  100. if (path_bounding_rect.is_empty())
  101. return;
  102. append(StrokePathUsingPaintStyle {
  103. .path_bounding_rect = path_bounding_rect,
  104. .path = params.path,
  105. .paint_style = params.paint_style,
  106. .thickness = params.thickness,
  107. .opacity = params.opacity,
  108. .aa_translation = aa_translation,
  109. });
  110. }
  111. void RecordingPainter::draw_ellipse(Gfx::IntRect const& a_rect, Color color, int thickness)
  112. {
  113. if (a_rect.is_empty())
  114. return;
  115. append(DrawEllipse {
  116. .rect = state().translation.map(a_rect),
  117. .color = color,
  118. .thickness = thickness,
  119. });
  120. }
  121. void RecordingPainter::fill_ellipse(Gfx::IntRect const& a_rect, Color color)
  122. {
  123. if (a_rect.is_empty())
  124. return;
  125. append(FillEllipse {
  126. .rect = state().translation.map(a_rect),
  127. .color = color,
  128. });
  129. }
  130. void RecordingPainter::fill_rect_with_linear_gradient(Gfx::IntRect const& gradient_rect, LinearGradientData const& data, Vector<Gfx::Path> const& clip_paths)
  131. {
  132. if (gradient_rect.is_empty())
  133. return;
  134. append(PaintLinearGradient {
  135. .gradient_rect = state().translation.map(gradient_rect),
  136. .linear_gradient_data = data,
  137. .clip_paths = clip_paths });
  138. }
  139. void RecordingPainter::fill_rect_with_conic_gradient(Gfx::IntRect const& rect, ConicGradientData const& data, Gfx::IntPoint const& position, Vector<Gfx::Path> const& clip_paths)
  140. {
  141. if (rect.is_empty())
  142. return;
  143. append(PaintConicGradient {
  144. .rect = state().translation.map(rect),
  145. .conic_gradient_data = data,
  146. .position = position,
  147. .clip_paths = clip_paths });
  148. }
  149. void RecordingPainter::fill_rect_with_radial_gradient(Gfx::IntRect const& rect, RadialGradientData const& data, Gfx::IntPoint center, Gfx::IntSize size, Vector<Gfx::Path> const& clip_paths)
  150. {
  151. if (rect.is_empty())
  152. return;
  153. append(PaintRadialGradient {
  154. .rect = state().translation.map(rect),
  155. .radial_gradient_data = data,
  156. .center = center,
  157. .size = size,
  158. .clip_paths = clip_paths });
  159. }
  160. void RecordingPainter::draw_rect(Gfx::IntRect const& rect, Color color, bool rough)
  161. {
  162. if (rect.is_empty())
  163. return;
  164. append(DrawRect {
  165. .rect = state().translation.map(rect),
  166. .color = color,
  167. .rough = rough });
  168. }
  169. void RecordingPainter::draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::ScalingMode scaling_mode)
  170. {
  171. if (dst_rect.is_empty())
  172. return;
  173. append(DrawScaledBitmap {
  174. .dst_rect = state().translation.map(dst_rect),
  175. .bitmap = bitmap,
  176. .src_rect = src_rect,
  177. .scaling_mode = scaling_mode,
  178. });
  179. }
  180. void RecordingPainter::draw_scaled_immutable_bitmap(Gfx::IntRect const& dst_rect, Gfx::ImmutableBitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::ScalingMode scaling_mode, Vector<Gfx::Path> const& clip_paths)
  181. {
  182. if (dst_rect.is_empty())
  183. return;
  184. append(DrawScaledImmutableBitmap {
  185. .dst_rect = state().translation.map(dst_rect),
  186. .bitmap = bitmap,
  187. .src_rect = src_rect,
  188. .scaling_mode = scaling_mode,
  189. .clip_paths = clip_paths,
  190. });
  191. }
  192. void RecordingPainter::draw_line(Gfx::IntPoint from, Gfx::IntPoint to, Color color, int thickness, Gfx::LineStyle style, Color alternate_color)
  193. {
  194. append(DrawLine {
  195. .color = color,
  196. .from = state().translation.map(from),
  197. .to = state().translation.map(to),
  198. .thickness = thickness,
  199. .style = style,
  200. .alternate_color = alternate_color,
  201. });
  202. }
  203. void RecordingPainter::draw_text(Gfx::IntRect const& rect, String raw_text, Gfx::Font const& font, Gfx::TextAlignment alignment, Color color, Gfx::TextElision elision, Gfx::TextWrapping wrapping)
  204. {
  205. if (rect.is_empty())
  206. return;
  207. append(DrawText {
  208. .rect = state().translation.map(rect),
  209. .raw_text = move(raw_text),
  210. .alignment = alignment,
  211. .color = color,
  212. .elision = elision,
  213. .wrapping = wrapping,
  214. .font = font,
  215. });
  216. }
  217. void RecordingPainter::draw_text_run(Gfx::IntPoint baseline_start, Gfx::GlyphRun const& glyph_run, Color color, Gfx::IntRect const& rect, double scale)
  218. {
  219. if (rect.is_empty())
  220. return;
  221. auto transformed_baseline_start = state().translation.map(baseline_start).to_type<float>();
  222. append(DrawGlyphRun {
  223. .glyph_run = glyph_run,
  224. .color = color,
  225. .rect = state().translation.map(rect),
  226. .translation = transformed_baseline_start,
  227. .scale = scale,
  228. });
  229. }
  230. void RecordingPainter::add_clip_rect(Gfx::IntRect const& rect)
  231. {
  232. append(AddClipRect { .rect = state().translation.map(rect) });
  233. }
  234. void RecordingPainter::translate(int dx, int dy)
  235. {
  236. m_state_stack.last().translation.translate(dx, dy);
  237. }
  238. void RecordingPainter::translate(Gfx::IntPoint delta)
  239. {
  240. m_state_stack.last().translation.translate(delta.to_type<float>());
  241. }
  242. void RecordingPainter::save()
  243. {
  244. append(Save {});
  245. m_state_stack.append(m_state_stack.last());
  246. }
  247. void RecordingPainter::restore()
  248. {
  249. append(Restore {});
  250. VERIFY(m_state_stack.size() > 1);
  251. m_state_stack.take_last();
  252. }
  253. void RecordingPainter::push_stacking_context(PushStackingContextParams params)
  254. {
  255. append(PushStackingContext {
  256. .opacity = params.opacity,
  257. .is_fixed_position = params.is_fixed_position,
  258. .source_paintable_rect = params.source_paintable_rect,
  259. // No translations apply to fixed-position stacking contexts.
  260. .post_transform_translation = params.is_fixed_position
  261. ? Gfx::IntPoint {}
  262. : state().translation.translation().to_rounded<int>(),
  263. .image_rendering = params.image_rendering,
  264. .transform = {
  265. .origin = params.transform.origin,
  266. .matrix = params.transform.matrix,
  267. },
  268. .mask = params.mask });
  269. m_state_stack.append(State());
  270. }
  271. void RecordingPainter::pop_stacking_context()
  272. {
  273. m_state_stack.take_last();
  274. append(PopStackingContext {});
  275. }
  276. void RecordingPainter::apply_backdrop_filter(Gfx::IntRect const& backdrop_region, BorderRadiiData const& border_radii_data, CSS::ResolvedBackdropFilter const& backdrop_filter)
  277. {
  278. if (backdrop_region.is_empty())
  279. return;
  280. append(ApplyBackdropFilter {
  281. .backdrop_region = state().translation.map(backdrop_region),
  282. .border_radii_data = border_radii_data,
  283. .backdrop_filter = backdrop_filter,
  284. });
  285. }
  286. void RecordingPainter::paint_outer_box_shadow_params(PaintBoxShadowParams params)
  287. {
  288. params.device_content_rect = state().translation.map(params.device_content_rect);
  289. append(PaintOuterBoxShadow { .box_shadow_params = params });
  290. }
  291. void RecordingPainter::paint_inner_box_shadow_params(PaintBoxShadowParams params)
  292. {
  293. append(PaintInnerBoxShadow { .box_shadow_params = params });
  294. }
  295. void RecordingPainter::paint_text_shadow(int blur_radius, Gfx::IntRect bounding_rect, Gfx::IntRect text_rect, Span<Gfx::DrawGlyphOrEmoji const> glyph_run, Color color, int fragment_baseline, Gfx::IntPoint draw_location)
  296. {
  297. append(PaintTextShadow {
  298. .blur_radius = blur_radius,
  299. .shadow_bounding_rect = bounding_rect,
  300. .text_rect = text_rect,
  301. .glyph_run = Vector<Gfx::DrawGlyphOrEmoji> { glyph_run },
  302. .color = color,
  303. .fragment_baseline = fragment_baseline,
  304. .draw_location = state().translation.map(draw_location) });
  305. }
  306. 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, Vector<Gfx::Path> const& clip_paths)
  307. {
  308. if (rect.is_empty())
  309. return;
  310. if (!top_left_radius && !top_right_radius && !bottom_right_radius && !bottom_left_radius) {
  311. fill_rect(rect, color, clip_paths);
  312. return;
  313. }
  314. append(FillRectWithRoundedCorners {
  315. .rect = state().translation.map(rect),
  316. .color = color,
  317. .top_left_radius = top_left_radius,
  318. .top_right_radius = top_right_radius,
  319. .bottom_left_radius = bottom_left_radius,
  320. .bottom_right_radius = bottom_right_radius,
  321. .clip_paths = clip_paths,
  322. });
  323. }
  324. void RecordingPainter::fill_rect_with_rounded_corners(Gfx::IntRect const& a_rect, Color color, int radius, Vector<Gfx::Path> const& clip_paths)
  325. {
  326. if (a_rect.is_empty())
  327. return;
  328. fill_rect_with_rounded_corners(a_rect, color, radius, radius, radius, radius, clip_paths);
  329. }
  330. 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, Vector<Gfx::Path> const& clip_paths)
  331. {
  332. if (a_rect.is_empty())
  333. return;
  334. fill_rect_with_rounded_corners(a_rect, color,
  335. { top_left_radius, top_left_radius },
  336. { top_right_radius, top_right_radius },
  337. { bottom_right_radius, bottom_right_radius },
  338. { bottom_left_radius, bottom_left_radius },
  339. clip_paths);
  340. }
  341. void RecordingPainter::draw_triangle_wave(Gfx::IntPoint a_p1, Gfx::IntPoint a_p2, Color color, int amplitude, int thickness = 1)
  342. {
  343. append(DrawTriangleWave {
  344. .p1 = state().translation.map(a_p1),
  345. .p2 = state().translation.map(a_p2),
  346. .color = color,
  347. .amplitude = amplitude,
  348. .thickness = thickness });
  349. }
  350. }