DisplayListRecorder.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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/DisplayListRecorder.h>
  7. #include <LibWeb/Painting/ShadowPainting.h>
  8. namespace Web::Painting {
  9. DisplayListRecorder::DisplayListRecorder(DisplayList& command_list)
  10. : m_command_list(command_list)
  11. {
  12. m_state_stack.append(State());
  13. }
  14. DisplayListRecorder::~DisplayListRecorder()
  15. {
  16. VERIFY(m_corner_clip_state_stack.is_empty());
  17. }
  18. void DisplayListRecorder::append(Command&& command)
  19. {
  20. m_command_list.append(move(command), state().scroll_frame_id);
  21. }
  22. void DisplayListRecorder::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 DisplayListRecorder::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 DisplayListRecorder::fill_rect(Gfx::IntRect const& rect, Color color, RefPtr<DisplayList> text_clip)
  38. {
  39. if (rect.is_empty())
  40. return;
  41. append(FillRect {
  42. .rect = state().translation.map(rect),
  43. .color = color,
  44. .text_clip = move(text_clip),
  45. });
  46. }
  47. void DisplayListRecorder::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 DisplayListRecorder::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 DisplayListRecorder::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 DisplayListRecorder::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 DisplayListRecorder::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 DisplayListRecorder::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 DisplayListRecorder::fill_rect_with_linear_gradient(Gfx::IntRect const& gradient_rect, LinearGradientData const& data, RefPtr<DisplayList> text_clip)
  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. .text_clip = move(text_clip) });
  136. }
  137. void DisplayListRecorder::fill_rect_with_conic_gradient(Gfx::IntRect const& rect, ConicGradientData const& data, Gfx::IntPoint const& position, RefPtr<DisplayList> text_clip)
  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. .text_clip = move(text_clip) });
  146. }
  147. void DisplayListRecorder::fill_rect_with_radial_gradient(Gfx::IntRect const& rect, RadialGradientData const& data, Gfx::IntPoint center, Gfx::IntSize size, RefPtr<DisplayList> text_clip)
  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. .text_clip = move(text_clip) });
  157. }
  158. void DisplayListRecorder::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 DisplayListRecorder::draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::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 DisplayListRecorder::draw_scaled_immutable_bitmap(Gfx::IntRect const& dst_rect, Gfx::ImmutableBitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::ScalingMode scaling_mode, RefPtr<DisplayList> text_clip)
  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. .text_clip = move(text_clip),
  188. });
  189. }
  190. void DisplayListRecorder::draw_repeated_immutable_bitmap(Gfx::IntRect dst_rect, Gfx::IntRect clip_rect, NonnullRefPtr<Gfx::ImmutableBitmap> bitmap, Gfx::ScalingMode scaling_mode, DrawRepeatedImmutableBitmap::Repeat repeat, RefPtr<DisplayList> text_clip)
  191. {
  192. append(DrawRepeatedImmutableBitmap {
  193. .dst_rect = dst_rect,
  194. .clip_rect = clip_rect,
  195. .bitmap = move(bitmap),
  196. .scaling_mode = scaling_mode,
  197. .repeat = repeat,
  198. .text_clip = move(text_clip),
  199. });
  200. }
  201. void DisplayListRecorder::draw_line(Gfx::IntPoint from, Gfx::IntPoint to, Color color, int thickness, Gfx::LineStyle style, Color alternate_color)
  202. {
  203. append(DrawLine {
  204. .color = color,
  205. .from = state().translation.map(from),
  206. .to = state().translation.map(to),
  207. .thickness = thickness,
  208. .style = style,
  209. .alternate_color = alternate_color,
  210. });
  211. }
  212. void DisplayListRecorder::draw_text(Gfx::IntRect const& rect, String raw_text, Gfx::Font const& font, Gfx::TextAlignment alignment, Color color)
  213. {
  214. if (rect.is_empty())
  215. return;
  216. auto glyph_run = adopt_ref(*new Gfx::GlyphRun({}, font));
  217. float glyph_run_width = 0;
  218. Gfx::for_each_glyph_position(
  219. { 0, 0 }, raw_text.code_points(), font, [&](Gfx::DrawGlyphOrEmoji const& glyph_or_emoji) {
  220. glyph_run->append(glyph_or_emoji);
  221. return IterationDecision::Continue;
  222. },
  223. Gfx::IncludeLeftBearing::No, glyph_run_width);
  224. float baseline_x = 0;
  225. if (alignment == Gfx::TextAlignment::CenterLeft) {
  226. baseline_x = rect.x();
  227. } else if (alignment == Gfx::TextAlignment::Center) {
  228. baseline_x = static_cast<float>(rect.x()) + (static_cast<float>(rect.width()) - glyph_run_width) / 2.0f;
  229. } else if (alignment == Gfx::TextAlignment::CenterRight) {
  230. baseline_x = static_cast<float>(rect.right()) - glyph_run_width;
  231. } else {
  232. // Unimplemented alignment.
  233. TODO();
  234. }
  235. auto metrics = font.pixel_metrics();
  236. float baseline_y = static_cast<float>(rect.y()) + metrics.ascent + (static_cast<float>(rect.height()) - (metrics.ascent + metrics.descent)) / 2.0f;
  237. draw_text_run(Gfx::IntPoint(roundf(baseline_x), roundf(baseline_y)), *glyph_run, color, rect, 1.0);
  238. }
  239. void DisplayListRecorder::draw_text_run(Gfx::IntPoint baseline_start, Gfx::GlyphRun const& glyph_run, Color color, Gfx::IntRect const& rect, double scale)
  240. {
  241. if (rect.is_empty())
  242. return;
  243. auto transformed_baseline_start = state().translation.map(baseline_start).to_type<float>();
  244. append(DrawGlyphRun {
  245. .glyph_run = glyph_run,
  246. .color = color,
  247. .rect = state().translation.map(rect),
  248. .translation = transformed_baseline_start,
  249. .scale = scale,
  250. });
  251. }
  252. void DisplayListRecorder::add_clip_rect(Gfx::IntRect const& rect)
  253. {
  254. append(AddClipRect { .rect = state().translation.map(rect) });
  255. }
  256. void DisplayListRecorder::translate(int dx, int dy)
  257. {
  258. m_state_stack.last().translation.translate(dx, dy);
  259. }
  260. void DisplayListRecorder::translate(Gfx::IntPoint delta)
  261. {
  262. m_state_stack.last().translation.translate(delta.to_type<float>());
  263. }
  264. void DisplayListRecorder::save()
  265. {
  266. append(Save {});
  267. m_state_stack.append(m_state_stack.last());
  268. }
  269. void DisplayListRecorder::restore()
  270. {
  271. append(Restore {});
  272. VERIFY(m_state_stack.size() > 1);
  273. m_state_stack.take_last();
  274. }
  275. void DisplayListRecorder::push_stacking_context(PushStackingContextParams params)
  276. {
  277. append(PushStackingContext {
  278. .opacity = params.opacity,
  279. .is_fixed_position = params.is_fixed_position,
  280. .source_paintable_rect = params.source_paintable_rect,
  281. // No translations apply to fixed-position stacking contexts.
  282. .post_transform_translation = params.is_fixed_position
  283. ? Gfx::IntPoint {}
  284. : state().translation.translation().to_rounded<int>(),
  285. .image_rendering = params.image_rendering,
  286. .transform = {
  287. .origin = params.transform.origin,
  288. .matrix = params.transform.matrix,
  289. },
  290. .mask = params.mask });
  291. m_state_stack.append(State());
  292. }
  293. void DisplayListRecorder::pop_stacking_context()
  294. {
  295. m_state_stack.take_last();
  296. append(PopStackingContext {});
  297. }
  298. void DisplayListRecorder::apply_backdrop_filter(Gfx::IntRect const& backdrop_region, BorderRadiiData const& border_radii_data, CSS::ResolvedBackdropFilter const& backdrop_filter)
  299. {
  300. if (backdrop_region.is_empty())
  301. return;
  302. append(ApplyBackdropFilter {
  303. .backdrop_region = state().translation.map(backdrop_region),
  304. .border_radii_data = border_radii_data,
  305. .backdrop_filter = backdrop_filter,
  306. });
  307. }
  308. void DisplayListRecorder::paint_outer_box_shadow_params(PaintBoxShadowParams params)
  309. {
  310. params.device_content_rect = state().translation.map(params.device_content_rect);
  311. append(PaintOuterBoxShadow { .box_shadow_params = params });
  312. }
  313. void DisplayListRecorder::paint_inner_box_shadow_params(PaintBoxShadowParams params)
  314. {
  315. append(PaintInnerBoxShadow { .box_shadow_params = params });
  316. }
  317. void DisplayListRecorder::paint_text_shadow(int blur_radius, Gfx::IntRect bounding_rect, Gfx::IntRect text_rect, Gfx::GlyphRun const& glyph_run, double glyph_run_scale, Color color, Gfx::IntPoint draw_location)
  318. {
  319. append(PaintTextShadow {
  320. .blur_radius = blur_radius,
  321. .shadow_bounding_rect = bounding_rect,
  322. .text_rect = text_rect,
  323. .glyph_run = glyph_run,
  324. .glyph_run_scale = glyph_run_scale,
  325. .color = color,
  326. .draw_location = state().translation.map(draw_location) });
  327. }
  328. void DisplayListRecorder::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, RefPtr<DisplayList> text_clip)
  329. {
  330. if (rect.is_empty())
  331. return;
  332. if (!top_left_radius && !top_right_radius && !bottom_right_radius && !bottom_left_radius) {
  333. fill_rect(rect, color, text_clip);
  334. return;
  335. }
  336. append(FillRectWithRoundedCorners {
  337. .rect = state().translation.map(rect),
  338. .color = color,
  339. .corner_radii = {
  340. .top_left = top_left_radius,
  341. .top_right = top_right_radius,
  342. .bottom_right = bottom_right_radius,
  343. .bottom_left = bottom_left_radius,
  344. },
  345. .text_clip = text_clip,
  346. });
  347. }
  348. void DisplayListRecorder::fill_rect_with_rounded_corners(Gfx::IntRect const& a_rect, Color color, int radius, RefPtr<DisplayList> text_clip)
  349. {
  350. if (a_rect.is_empty())
  351. return;
  352. fill_rect_with_rounded_corners(a_rect, color, radius, radius, radius, radius, move(text_clip));
  353. }
  354. void DisplayListRecorder::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, RefPtr<DisplayList> text_clip)
  355. {
  356. if (a_rect.is_empty())
  357. return;
  358. fill_rect_with_rounded_corners(a_rect, color,
  359. { top_left_radius, top_left_radius },
  360. { top_right_radius, top_right_radius },
  361. { bottom_right_radius, bottom_right_radius },
  362. { bottom_left_radius, bottom_left_radius },
  363. move(text_clip));
  364. }
  365. void DisplayListRecorder::draw_triangle_wave(Gfx::IntPoint a_p1, Gfx::IntPoint a_p2, Color color, int amplitude, int thickness = 1)
  366. {
  367. append(DrawTriangleWave {
  368. .p1 = state().translation.map(a_p1),
  369. .p2 = state().translation.map(a_p2),
  370. .color = color,
  371. .amplitude = amplitude,
  372. .thickness = thickness });
  373. }
  374. }