RecordingPainter.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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::Painter::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::Painter::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::Painter::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_signed_distance_field(Gfx::IntRect const& dst_rect, Color color, Gfx::GrayscaleBitmap const& sdf, float smoothing)
  218. {
  219. if (dst_rect.is_empty())
  220. return;
  221. append(DrawSignedDistanceField {
  222. .rect = state().translation.map(dst_rect),
  223. .color = color,
  224. .sdf = sdf,
  225. .smoothing = smoothing,
  226. });
  227. }
  228. void RecordingPainter::draw_text_run(Gfx::IntPoint baseline_start, Gfx::GlyphRun const& glyph_run, Color color, Gfx::IntRect const& rect, double scale)
  229. {
  230. if (rect.is_empty())
  231. return;
  232. auto transformed_baseline_start = state().translation.map(baseline_start).to_type<float>();
  233. append(DrawGlyphRun {
  234. .glyph_run = glyph_run,
  235. .color = color,
  236. .rect = state().translation.map(rect),
  237. .translation = transformed_baseline_start,
  238. .scale = scale,
  239. });
  240. }
  241. void RecordingPainter::add_clip_rect(Gfx::IntRect const& rect)
  242. {
  243. auto prev_clip_rect = state().clip_rect;
  244. if (!state().clip_rect.has_value()) {
  245. state().clip_rect = state().translation.map(rect);
  246. } else {
  247. state().clip_rect->intersect(state().translation.map(rect));
  248. }
  249. if (prev_clip_rect != state().clip_rect)
  250. append(SetClipRect { .rect = *state().clip_rect });
  251. }
  252. void RecordingPainter::translate(int dx, int dy)
  253. {
  254. m_state_stack.last().translation.translate(dx, dy);
  255. }
  256. void RecordingPainter::translate(Gfx::IntPoint delta)
  257. {
  258. m_state_stack.last().translation.translate(delta.to_type<float>());
  259. }
  260. void RecordingPainter::save()
  261. {
  262. m_state_stack.append(m_state_stack.last());
  263. }
  264. void RecordingPainter::restore()
  265. {
  266. auto prev_clip_rect = state().clip_rect;
  267. VERIFY(m_state_stack.size() > 1);
  268. m_state_stack.take_last();
  269. if (state().clip_rect != prev_clip_rect) {
  270. if (state().clip_rect.has_value())
  271. append(SetClipRect { .rect = *state().clip_rect });
  272. else
  273. append(ClearClipRect {});
  274. }
  275. }
  276. void RecordingPainter::push_stacking_context(PushStackingContextParams params)
  277. {
  278. append(PushStackingContext {
  279. .opacity = params.opacity,
  280. .is_fixed_position = params.is_fixed_position,
  281. .source_paintable_rect = params.source_paintable_rect,
  282. // No translations apply to fixed-position stacking contexts.
  283. .post_transform_translation = params.is_fixed_position
  284. ? Gfx::IntPoint {}
  285. : state().translation.translation().to_rounded<int>(),
  286. .image_rendering = params.image_rendering,
  287. .transform = {
  288. .origin = params.transform.origin,
  289. .matrix = params.transform.matrix,
  290. },
  291. .mask = params.mask });
  292. m_state_stack.append(State());
  293. }
  294. void RecordingPainter::pop_stacking_context()
  295. {
  296. m_state_stack.take_last();
  297. append(PopStackingContext {});
  298. }
  299. void RecordingPainter::apply_backdrop_filter(Gfx::IntRect const& backdrop_region, BorderRadiiData const& border_radii_data, CSS::ResolvedBackdropFilter const& backdrop_filter)
  300. {
  301. if (backdrop_region.is_empty())
  302. return;
  303. append(ApplyBackdropFilter {
  304. .backdrop_region = state().translation.map(backdrop_region),
  305. .border_radii_data = border_radii_data,
  306. .backdrop_filter = backdrop_filter,
  307. });
  308. }
  309. void RecordingPainter::paint_outer_box_shadow_params(PaintOuterBoxShadowParams params)
  310. {
  311. params.device_content_rect = state().translation.map(params.device_content_rect.to_type<int>()).to_type<DevicePixels>();
  312. append(PaintOuterBoxShadow {
  313. .outer_box_shadow_params = params,
  314. });
  315. }
  316. void RecordingPainter::paint_inner_box_shadow_params(PaintOuterBoxShadowParams params)
  317. {
  318. append(PaintInnerBoxShadow {
  319. .outer_box_shadow_params = params,
  320. });
  321. }
  322. 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)
  323. {
  324. append(PaintTextShadow {
  325. .blur_radius = blur_radius,
  326. .shadow_bounding_rect = bounding_rect,
  327. .text_rect = text_rect,
  328. .glyph_run = Vector<Gfx::DrawGlyphOrEmoji> { glyph_run },
  329. .color = color,
  330. .fragment_baseline = fragment_baseline,
  331. .draw_location = state().translation.map(draw_location) });
  332. }
  333. 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)
  334. {
  335. if (rect.is_empty())
  336. return;
  337. if (!top_left_radius && !top_right_radius && !bottom_right_radius && !bottom_left_radius) {
  338. fill_rect(rect, color, clip_paths);
  339. return;
  340. }
  341. append(FillRectWithRoundedCorners {
  342. .rect = state().translation.map(rect),
  343. .color = color,
  344. .top_left_radius = top_left_radius,
  345. .top_right_radius = top_right_radius,
  346. .bottom_left_radius = bottom_left_radius,
  347. .bottom_right_radius = bottom_right_radius,
  348. .clip_paths = clip_paths,
  349. });
  350. }
  351. void RecordingPainter::fill_rect_with_rounded_corners(Gfx::IntRect const& a_rect, Color color, int radius, Vector<Gfx::Path> const& clip_paths)
  352. {
  353. if (a_rect.is_empty())
  354. return;
  355. fill_rect_with_rounded_corners(a_rect, color, radius, radius, radius, radius, clip_paths);
  356. }
  357. 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)
  358. {
  359. if (a_rect.is_empty())
  360. return;
  361. fill_rect_with_rounded_corners(a_rect, color,
  362. { top_left_radius, top_left_radius },
  363. { top_right_radius, top_right_radius },
  364. { bottom_right_radius, bottom_right_radius },
  365. { bottom_left_radius, bottom_left_radius },
  366. clip_paths);
  367. }
  368. void RecordingPainter::draw_triangle_wave(Gfx::IntPoint a_p1, Gfx::IntPoint a_p2, Color color, int amplitude, int thickness = 1)
  369. {
  370. append(DrawTriangleWave {
  371. .p1 = state().translation.map(a_p1),
  372. .p2 = state().translation.map(a_p2),
  373. .color = color,
  374. .amplitude = amplitude,
  375. .thickness = thickness });
  376. }
  377. void RecordingPainter::paint_borders(DevicePixelRect const& border_rect, CornerRadii const& corner_radii, BordersDataDevicePixels const& borders_data)
  378. {
  379. if (borders_data.top.width == 0 && borders_data.right.width == 0 && borders_data.bottom.width == 0 && borders_data.left.width == 0)
  380. return;
  381. append(PaintBorders { border_rect, corner_radii, borders_data });
  382. }
  383. }