RecordingPainter.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. append(SampleUnderCorners {
  26. id,
  27. corner_radii,
  28. border_rect = state().translation.map(border_rect),
  29. corner_clip });
  30. }
  31. void RecordingPainter::blit_corner_clipping(u32 id)
  32. {
  33. auto clip_state = m_corner_clip_state_stack.take_last();
  34. VERIFY(clip_state.id == id);
  35. append(BlitCornerClipping { id, state().translation.map(clip_state.rect) });
  36. }
  37. void RecordingPainter::fill_rect(Gfx::IntRect const& rect, Color color, Vector<Gfx::Path> const& clip_paths)
  38. {
  39. if (rect.is_empty())
  40. return;
  41. append(FillRect {
  42. .rect = state().translation.map(rect),
  43. .color = color,
  44. .clip_paths = clip_paths,
  45. });
  46. }
  47. void RecordingPainter::fill_path(FillPathUsingColorParams params)
  48. {
  49. auto aa_translation = state().translation.map(params.translation.value_or(Gfx::FloatPoint {}));
  50. auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
  51. if (path_bounding_rect.is_empty())
  52. return;
  53. append(FillPathUsingColor {
  54. .path_bounding_rect = path_bounding_rect,
  55. .path = params.path,
  56. .color = params.color,
  57. .winding_rule = params.winding_rule,
  58. .aa_translation = aa_translation,
  59. });
  60. }
  61. void RecordingPainter::fill_path(FillPathUsingPaintStyleParams params)
  62. {
  63. auto aa_translation = state().translation.map(params.translation.value_or(Gfx::FloatPoint {}));
  64. auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
  65. if (path_bounding_rect.is_empty())
  66. return;
  67. append(FillPathUsingPaintStyle {
  68. .path_bounding_rect = path_bounding_rect,
  69. .path = params.path,
  70. .paint_style = params.paint_style,
  71. .winding_rule = params.winding_rule,
  72. .opacity = params.opacity,
  73. .aa_translation = aa_translation,
  74. });
  75. }
  76. void RecordingPainter::stroke_path(StrokePathUsingColorParams params)
  77. {
  78. auto aa_translation = state().translation.map(params.translation.value_or(Gfx::FloatPoint {}));
  79. auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
  80. // Increase path bounding box by `thickness` to account for stroke.
  81. path_bounding_rect.inflate(params.thickness, params.thickness);
  82. if (path_bounding_rect.is_empty())
  83. return;
  84. append(StrokePathUsingColor {
  85. .path_bounding_rect = path_bounding_rect,
  86. .path = params.path,
  87. .color = params.color,
  88. .thickness = params.thickness,
  89. .aa_translation = aa_translation,
  90. });
  91. }
  92. void RecordingPainter::stroke_path(StrokePathUsingPaintStyleParams params)
  93. {
  94. auto aa_translation = state().translation.map(params.translation.value_or(Gfx::FloatPoint {}));
  95. auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
  96. // Increase path bounding box by `thickness` to account for stroke.
  97. path_bounding_rect.inflate(params.thickness, params.thickness);
  98. if (path_bounding_rect.is_empty())
  99. return;
  100. append(StrokePathUsingPaintStyle {
  101. .path_bounding_rect = path_bounding_rect,
  102. .path = params.path,
  103. .paint_style = params.paint_style,
  104. .thickness = params.thickness,
  105. .opacity = params.opacity,
  106. .aa_translation = aa_translation,
  107. });
  108. }
  109. void RecordingPainter::draw_ellipse(Gfx::IntRect const& a_rect, Color color, int thickness)
  110. {
  111. if (a_rect.is_empty())
  112. return;
  113. append(DrawEllipse {
  114. .rect = state().translation.map(a_rect),
  115. .color = color,
  116. .thickness = thickness,
  117. });
  118. }
  119. void RecordingPainter::fill_ellipse(Gfx::IntRect const& a_rect, Color color)
  120. {
  121. if (a_rect.is_empty())
  122. return;
  123. append(FillEllipse {
  124. .rect = state().translation.map(a_rect),
  125. .color = color,
  126. });
  127. }
  128. void RecordingPainter::fill_rect_with_linear_gradient(Gfx::IntRect const& gradient_rect, LinearGradientData const& data, Vector<Gfx::Path> const& clip_paths)
  129. {
  130. if (gradient_rect.is_empty())
  131. return;
  132. append(PaintLinearGradient {
  133. .gradient_rect = state().translation.map(gradient_rect),
  134. .linear_gradient_data = data,
  135. .clip_paths = clip_paths });
  136. }
  137. void RecordingPainter::fill_rect_with_conic_gradient(Gfx::IntRect const& rect, ConicGradientData const& data, Gfx::IntPoint const& position, Vector<Gfx::Path> const& clip_paths)
  138. {
  139. if (rect.is_empty())
  140. return;
  141. append(PaintConicGradient {
  142. .rect = state().translation.map(rect),
  143. .conic_gradient_data = data,
  144. .position = position,
  145. .clip_paths = clip_paths });
  146. }
  147. 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)
  148. {
  149. if (rect.is_empty())
  150. return;
  151. append(PaintRadialGradient {
  152. .rect = state().translation.map(rect),
  153. .radial_gradient_data = data,
  154. .center = center,
  155. .size = size,
  156. .clip_paths = clip_paths });
  157. }
  158. void RecordingPainter::draw_rect(Gfx::IntRect const& rect, Color color, bool rough)
  159. {
  160. if (rect.is_empty())
  161. return;
  162. append(DrawRect {
  163. .rect = state().translation.map(rect),
  164. .color = color,
  165. .rough = rough });
  166. }
  167. void RecordingPainter::draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::Painter::ScalingMode scaling_mode)
  168. {
  169. if (dst_rect.is_empty())
  170. return;
  171. append(DrawScaledBitmap {
  172. .dst_rect = state().translation.map(dst_rect),
  173. .bitmap = bitmap,
  174. .src_rect = src_rect,
  175. .scaling_mode = scaling_mode,
  176. });
  177. }
  178. 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)
  179. {
  180. if (dst_rect.is_empty())
  181. return;
  182. append(DrawScaledImmutableBitmap {
  183. .dst_rect = state().translation.map(dst_rect),
  184. .bitmap = bitmap,
  185. .src_rect = src_rect,
  186. .scaling_mode = scaling_mode,
  187. .clip_paths = clip_paths,
  188. });
  189. }
  190. void RecordingPainter::draw_line(Gfx::IntPoint from, Gfx::IntPoint to, Color color, int thickness, Gfx::Painter::LineStyle style, Color alternate_color)
  191. {
  192. append(DrawLine {
  193. .color = color,
  194. .from = state().translation.map(from),
  195. .to = state().translation.map(to),
  196. .thickness = thickness,
  197. .style = style,
  198. .alternate_color = alternate_color,
  199. });
  200. }
  201. 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)
  202. {
  203. if (rect.is_empty())
  204. return;
  205. append(DrawText {
  206. .rect = state().translation.map(rect),
  207. .raw_text = move(raw_text),
  208. .alignment = alignment,
  209. .color = color,
  210. .elision = elision,
  211. .wrapping = wrapping,
  212. .font = font,
  213. });
  214. }
  215. void RecordingPainter::draw_signed_distance_field(Gfx::IntRect const& dst_rect, Color color, Gfx::GrayscaleBitmap const& sdf, float smoothing)
  216. {
  217. if (dst_rect.is_empty())
  218. return;
  219. append(DrawSignedDistanceField {
  220. .rect = state().translation.map(dst_rect),
  221. .color = color,
  222. .sdf = sdf,
  223. .smoothing = smoothing,
  224. });
  225. }
  226. void RecordingPainter::draw_text_run(Gfx::IntPoint baseline_start, Gfx::GlyphRun const& glyph_run, Color color, Gfx::IntRect const& rect, double scale)
  227. {
  228. if (rect.is_empty())
  229. return;
  230. auto transformed_baseline_start = state().translation.map(baseline_start).to_type<float>();
  231. append(DrawGlyphRun {
  232. .glyph_run = glyph_run,
  233. .color = color,
  234. .rect = state().translation.map(rect),
  235. .translation = transformed_baseline_start,
  236. .scale = scale,
  237. });
  238. }
  239. void RecordingPainter::add_clip_rect(Gfx::IntRect const& rect)
  240. {
  241. auto prev_clip_rect = state().clip_rect;
  242. if (!state().clip_rect.has_value()) {
  243. state().clip_rect = state().translation.map(rect);
  244. } else {
  245. state().clip_rect->intersect(state().translation.map(rect));
  246. }
  247. if (prev_clip_rect != state().clip_rect)
  248. append(SetClipRect { .rect = *state().clip_rect });
  249. }
  250. void RecordingPainter::translate(int dx, int dy)
  251. {
  252. m_state_stack.last().translation.translate(dx, dy);
  253. }
  254. void RecordingPainter::translate(Gfx::IntPoint delta)
  255. {
  256. m_state_stack.last().translation.translate(delta.to_type<float>());
  257. }
  258. void RecordingPainter::save()
  259. {
  260. m_state_stack.append(m_state_stack.last());
  261. }
  262. void RecordingPainter::restore()
  263. {
  264. auto prev_clip_rect = state().clip_rect;
  265. VERIFY(m_state_stack.size() > 1);
  266. m_state_stack.take_last();
  267. if (state().clip_rect != prev_clip_rect) {
  268. if (state().clip_rect.has_value())
  269. append(SetClipRect { .rect = *state().clip_rect });
  270. else
  271. append(ClearClipRect {});
  272. }
  273. }
  274. void RecordingPainter::push_stacking_context(PushStackingContextParams params)
  275. {
  276. append(PushStackingContext {
  277. .opacity = params.opacity,
  278. .is_fixed_position = params.is_fixed_position,
  279. .source_paintable_rect = params.source_paintable_rect,
  280. // No translations apply to fixed-position stacking contexts.
  281. .post_transform_translation = params.is_fixed_position
  282. ? Gfx::IntPoint {}
  283. : state().translation.translation().to_rounded<int>(),
  284. .image_rendering = params.image_rendering,
  285. .transform = {
  286. .origin = params.transform.origin,
  287. .matrix = params.transform.matrix,
  288. },
  289. .mask = params.mask });
  290. m_state_stack.append(State());
  291. }
  292. void RecordingPainter::pop_stacking_context()
  293. {
  294. m_state_stack.take_last();
  295. append(PopStackingContext {});
  296. }
  297. void RecordingPainter::apply_backdrop_filter(Gfx::IntRect const& backdrop_region, BorderRadiiData const& border_radii_data, CSS::ResolvedBackdropFilter const& backdrop_filter)
  298. {
  299. if (backdrop_region.is_empty())
  300. return;
  301. append(ApplyBackdropFilter {
  302. .backdrop_region = state().translation.map(backdrop_region),
  303. .border_radii_data = border_radii_data,
  304. .backdrop_filter = backdrop_filter,
  305. });
  306. }
  307. void RecordingPainter::paint_outer_box_shadow_params(PaintOuterBoxShadowParams params)
  308. {
  309. params.device_content_rect = state().translation.map(params.device_content_rect.to_type<int>()).to_type<DevicePixels>();
  310. append(PaintOuterBoxShadow {
  311. .outer_box_shadow_params = params,
  312. });
  313. }
  314. void RecordingPainter::paint_inner_box_shadow_params(PaintOuterBoxShadowParams params)
  315. {
  316. append(PaintInnerBoxShadow {
  317. .outer_box_shadow_params = params,
  318. });
  319. }
  320. 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)
  321. {
  322. append(PaintTextShadow {
  323. .blur_radius = blur_radius,
  324. .shadow_bounding_rect = bounding_rect,
  325. .text_rect = text_rect,
  326. .glyph_run = Vector<Gfx::DrawGlyphOrEmoji> { glyph_run },
  327. .color = color,
  328. .fragment_baseline = fragment_baseline,
  329. .draw_location = state().translation.map(draw_location) });
  330. }
  331. 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)
  332. {
  333. if (rect.is_empty())
  334. return;
  335. if (!top_left_radius && !top_right_radius && !bottom_right_radius && !bottom_left_radius) {
  336. fill_rect(rect, color, clip_paths);
  337. return;
  338. }
  339. append(FillRectWithRoundedCorners {
  340. .rect = state().translation.map(rect),
  341. .color = color,
  342. .top_left_radius = top_left_radius,
  343. .top_right_radius = top_right_radius,
  344. .bottom_left_radius = bottom_left_radius,
  345. .bottom_right_radius = bottom_right_radius,
  346. .clip_paths = clip_paths,
  347. });
  348. }
  349. void RecordingPainter::fill_rect_with_rounded_corners(Gfx::IntRect const& a_rect, Color color, int radius, Vector<Gfx::Path> const& clip_paths)
  350. {
  351. if (a_rect.is_empty())
  352. return;
  353. fill_rect_with_rounded_corners(a_rect, color, radius, radius, radius, radius, clip_paths);
  354. }
  355. 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)
  356. {
  357. if (a_rect.is_empty())
  358. return;
  359. fill_rect_with_rounded_corners(a_rect, color,
  360. { top_left_radius, top_left_radius },
  361. { top_right_radius, top_right_radius },
  362. { bottom_right_radius, bottom_right_radius },
  363. { bottom_left_radius, bottom_left_radius },
  364. clip_paths);
  365. }
  366. void RecordingPainter::draw_triangle_wave(Gfx::IntPoint a_p1, Gfx::IntPoint a_p2, Color color, int amplitude, int thickness = 1)
  367. {
  368. append(DrawTriangleWave {
  369. .p1 = state().translation.map(a_p1),
  370. .p2 = state().translation.map(a_p2),
  371. .color = color,
  372. .amplitude = amplitude,
  373. .thickness = thickness });
  374. }
  375. void RecordingPainter::paint_borders(DevicePixelRect const& border_rect, CornerRadii const& corner_radii, BordersDataDevicePixels const& borders_data)
  376. {
  377. if (borders_data.top.width == 0 && borders_data.right.width == 0 && borders_data.bottom.width == 0 && borders_data.left.width == 0)
  378. return;
  379. append(PaintBorders { border_rect, corner_radii, borders_data });
  380. }
  381. }