RecordingPainter.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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)
  31. {
  32. append(FillRect {
  33. .rect = state().translation.map(rect),
  34. .color = color,
  35. });
  36. }
  37. void RecordingPainter::fill_path(FillPathUsingColorParams params)
  38. {
  39. auto aa_translation = state().translation.map(params.translation.value_or(Gfx::FloatPoint {}));
  40. auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
  41. append(FillPathUsingColor {
  42. .path_bounding_rect = path_bounding_rect,
  43. .path = params.path,
  44. .color = params.color,
  45. .winding_rule = params.winding_rule,
  46. .aa_translation = aa_translation,
  47. });
  48. }
  49. void RecordingPainter::fill_path(FillPathUsingPaintStyleParams 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. append(FillPathUsingPaintStyle {
  54. .path_bounding_rect = path_bounding_rect,
  55. .path = params.path,
  56. .paint_style = params.paint_style,
  57. .winding_rule = params.winding_rule,
  58. .opacity = params.opacity,
  59. .aa_translation = aa_translation,
  60. });
  61. }
  62. void RecordingPainter::stroke_path(StrokePathUsingColorParams params)
  63. {
  64. auto aa_translation = state().translation.map(params.translation.value_or(Gfx::FloatPoint {}));
  65. auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
  66. // Increase path bounding box by `thickness` to account for stroke.
  67. path_bounding_rect.inflate(params.thickness, params.thickness);
  68. append(StrokePathUsingColor {
  69. .path_bounding_rect = path_bounding_rect,
  70. .path = params.path,
  71. .color = params.color,
  72. .thickness = params.thickness,
  73. .aa_translation = aa_translation,
  74. });
  75. }
  76. void RecordingPainter::stroke_path(StrokePathUsingPaintStyleParams 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. append(StrokePathUsingPaintStyle {
  83. .path_bounding_rect = path_bounding_rect,
  84. .path = params.path,
  85. .paint_style = params.paint_style,
  86. .thickness = params.thickness,
  87. .opacity = params.opacity,
  88. .aa_translation = aa_translation,
  89. });
  90. }
  91. void RecordingPainter::draw_ellipse(Gfx::IntRect const& a_rect, Color color, int thickness)
  92. {
  93. append(DrawEllipse {
  94. .rect = state().translation.map(a_rect),
  95. .color = color,
  96. .thickness = thickness,
  97. });
  98. }
  99. void RecordingPainter::fill_ellipse(Gfx::IntRect const& a_rect, Color color, Gfx::AntiAliasingPainter::BlendMode blend_mode)
  100. {
  101. append(FillEllipse {
  102. .rect = state().translation.map(a_rect),
  103. .color = color,
  104. .blend_mode = blend_mode,
  105. });
  106. }
  107. void RecordingPainter::fill_rect_with_linear_gradient(Gfx::IntRect const& gradient_rect, LinearGradientData const& data)
  108. {
  109. append(PaintLinearGradient {
  110. .gradient_rect = state().translation.map(gradient_rect),
  111. .linear_gradient_data = data,
  112. });
  113. }
  114. void RecordingPainter::fill_rect_with_conic_gradient(Gfx::IntRect const& rect, ConicGradientData const& data, Gfx::IntPoint const& position)
  115. {
  116. append(PaintConicGradient {
  117. .rect = state().translation.map(rect),
  118. .conic_gradient_data = data,
  119. .position = position });
  120. }
  121. void RecordingPainter::fill_rect_with_radial_gradient(Gfx::IntRect const& rect, RadialGradientData const& data, Gfx::IntPoint center, Gfx::IntSize size)
  122. {
  123. append(PaintRadialGradient {
  124. .rect = state().translation.map(rect),
  125. .radial_gradient_data = data,
  126. .center = center,
  127. .size = size });
  128. }
  129. void RecordingPainter::draw_rect(Gfx::IntRect const& rect, Color color, bool rough)
  130. {
  131. append(DrawRect {
  132. .rect = state().translation.map(rect),
  133. .color = color,
  134. .rough = rough });
  135. }
  136. void RecordingPainter::draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::Painter::ScalingMode scaling_mode)
  137. {
  138. append(DrawScaledBitmap {
  139. .dst_rect = state().translation.map(dst_rect),
  140. .bitmap = bitmap,
  141. .src_rect = src_rect,
  142. .scaling_mode = scaling_mode,
  143. });
  144. }
  145. 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)
  146. {
  147. append(DrawScaledImmutableBitmap {
  148. .dst_rect = state().translation.map(dst_rect),
  149. .bitmap = bitmap,
  150. .src_rect = src_rect,
  151. .scaling_mode = scaling_mode,
  152. });
  153. }
  154. void RecordingPainter::draw_line(Gfx::IntPoint from, Gfx::IntPoint to, Color color, int thickness, Gfx::Painter::LineStyle style, Color alternate_color)
  155. {
  156. append(DrawLine {
  157. .color = color,
  158. .from = state().translation.map(from),
  159. .to = state().translation.map(to),
  160. .thickness = thickness,
  161. .style = style,
  162. .alternate_color = alternate_color,
  163. });
  164. }
  165. 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)
  166. {
  167. append(DrawText {
  168. .rect = state().translation.map(rect),
  169. .raw_text = move(raw_text),
  170. .alignment = alignment,
  171. .color = color,
  172. .elision = elision,
  173. .wrapping = wrapping,
  174. .font = font,
  175. });
  176. }
  177. void RecordingPainter::draw_signed_distance_field(Gfx::IntRect const& dst_rect, Color color, Gfx::GrayscaleBitmap const& sdf, float smoothing)
  178. {
  179. append(DrawSignedDistanceField {
  180. .rect = state().translation.map(dst_rect),
  181. .color = color,
  182. .sdf = sdf,
  183. .smoothing = smoothing,
  184. });
  185. }
  186. void RecordingPainter::draw_text_run(Gfx::IntPoint baseline_start, Span<Gfx::DrawGlyphOrEmoji const> glyph_run, Color color, Gfx::IntRect const& rect)
  187. {
  188. auto transformed_baseline_start = state().translation.map(baseline_start).to_type<float>();
  189. Vector<Gfx::DrawGlyphOrEmoji> translated_glyph_run;
  190. translated_glyph_run.ensure_capacity(glyph_run.size());
  191. for (auto glyph : glyph_run) {
  192. glyph.visit([&](auto& glyph) { glyph.position.translate_by(transformed_baseline_start); });
  193. translated_glyph_run.append(glyph);
  194. }
  195. append(DrawGlyphRun {
  196. .glyph_run = move(translated_glyph_run),
  197. .color = color,
  198. .rect = state().translation.map(rect),
  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)
  296. {
  297. if (!top_left_radius && !top_right_radius && !bottom_right_radius && !bottom_left_radius) {
  298. fill_rect(rect, color);
  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. });
  309. }
  310. void RecordingPainter::fill_rect_with_rounded_corners(Gfx::IntRect const& a_rect, Color color, int radius)
  311. {
  312. fill_rect_with_rounded_corners(a_rect, color, radius, radius, radius, radius);
  313. }
  314. 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)
  315. {
  316. fill_rect_with_rounded_corners(a_rect, color,
  317. { top_left_radius, top_left_radius },
  318. { top_right_radius, top_right_radius },
  319. { bottom_right_radius, bottom_right_radius },
  320. { bottom_left_radius, bottom_left_radius });
  321. }
  322. void RecordingPainter::draw_triangle_wave(Gfx::IntPoint a_p1, Gfx::IntPoint a_p2, Color color, int amplitude, int thickness = 1)
  323. {
  324. append(DrawTriangleWave {
  325. .p1 = state().translation.map(a_p1),
  326. .p2 = state().translation.map(a_p2),
  327. .color = color,
  328. .amplitude = amplitude,
  329. .thickness = thickness });
  330. }
  331. void RecordingPainter::paint_borders(DevicePixelRect const& border_rect, CornerRadii const& corner_radii, BordersDataDevicePixels const& borders_data)
  332. {
  333. if (borders_data.top.width == 0 && borders_data.right.width == 0 && borders_data.bottom.width == 0 && borders_data.left.width == 0)
  334. return;
  335. append(PaintBorders { border_rect, corner_radii, borders_data });
  336. }
  337. }