RecordingPainter.cpp 13 KB

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