DisplayListRecorder.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. if (m_corner_clip_state_stack.size() > display_list().corner_clip_max_depth())
  26. display_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 DisplayListRecorder::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 DisplayListRecorder::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 DisplayListRecorder::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 DisplayListRecorder::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 DisplayListRecorder::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 DisplayListRecorder::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 DisplayListRecorder::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 DisplayListRecorder::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 DisplayListRecorder::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 DisplayListRecorder::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 DisplayListRecorder::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 DisplayListRecorder::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 DisplayListRecorder::draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::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 DisplayListRecorder::draw_scaled_immutable_bitmap(Gfx::IntRect const& dst_rect, Gfx::ImmutableBitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::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 DisplayListRecorder::draw_repeated_immutable_bitmap(Gfx::IntRect dst_rect, NonnullRefPtr<Gfx::ImmutableBitmap> bitmap, Gfx::ScalingMode scaling_mode, DrawRepeatedImmutableBitmap::Repeat repeat, Vector<Gfx::Path> const& clip_paths)
  193. {
  194. append(DrawRepeatedImmutableBitmap {
  195. .dst_rect = dst_rect,
  196. .bitmap = move(bitmap),
  197. .scaling_mode = scaling_mode,
  198. .repeat = repeat,
  199. .clip_paths = clip_paths,
  200. });
  201. }
  202. void DisplayListRecorder::draw_line(Gfx::IntPoint from, Gfx::IntPoint to, Color color, int thickness, Gfx::LineStyle style, Color alternate_color)
  203. {
  204. append(DrawLine {
  205. .color = color,
  206. .from = state().translation.map(from),
  207. .to = state().translation.map(to),
  208. .thickness = thickness,
  209. .style = style,
  210. .alternate_color = alternate_color,
  211. });
  212. }
  213. void DisplayListRecorder::draw_text(Gfx::IntRect const& rect, String raw_text, Gfx::Font const& font, Gfx::TextAlignment alignment, Color color)
  214. {
  215. if (rect.is_empty())
  216. return;
  217. auto glyph_run = adopt_ref(*new Gfx::GlyphRun({}, font));
  218. float glyph_run_width = 0;
  219. Gfx::for_each_glyph_position(
  220. { 0, 0 }, raw_text.code_points(), font, [&](Gfx::DrawGlyphOrEmoji const& glyph_or_emoji) {
  221. glyph_run->append(glyph_or_emoji);
  222. return IterationDecision::Continue;
  223. },
  224. Gfx::IncludeLeftBearing::No, glyph_run_width);
  225. float baseline_x = 0;
  226. if (alignment == Gfx::TextAlignment::CenterLeft) {
  227. baseline_x = rect.x();
  228. } else if (alignment == Gfx::TextAlignment::Center) {
  229. baseline_x = static_cast<float>(rect.x()) + (static_cast<float>(rect.width()) - glyph_run_width) / 2.0f;
  230. } else if (alignment == Gfx::TextAlignment::CenterRight) {
  231. baseline_x = static_cast<float>(rect.right()) - glyph_run_width;
  232. } else {
  233. // Unimplemented alignment.
  234. TODO();
  235. }
  236. auto metrics = font.pixel_metrics();
  237. float baseline_y = static_cast<float>(rect.y()) + metrics.ascent + (static_cast<float>(rect.height()) - (metrics.ascent + metrics.descent)) / 2.0f;
  238. draw_text_run(Gfx::IntPoint(roundf(baseline_x), roundf(baseline_y)), *glyph_run, color, rect, 1.0);
  239. }
  240. void DisplayListRecorder::draw_text_run(Gfx::IntPoint baseline_start, Gfx::GlyphRun const& glyph_run, Color color, Gfx::IntRect const& rect, double scale)
  241. {
  242. if (rect.is_empty())
  243. return;
  244. auto transformed_baseline_start = state().translation.map(baseline_start).to_type<float>();
  245. append(DrawGlyphRun {
  246. .glyph_run = glyph_run,
  247. .color = color,
  248. .rect = state().translation.map(rect),
  249. .translation = transformed_baseline_start,
  250. .scale = scale,
  251. });
  252. }
  253. void DisplayListRecorder::add_clip_rect(Gfx::IntRect const& rect)
  254. {
  255. append(AddClipRect { .rect = state().translation.map(rect) });
  256. }
  257. void DisplayListRecorder::translate(int dx, int dy)
  258. {
  259. m_state_stack.last().translation.translate(dx, dy);
  260. }
  261. void DisplayListRecorder::translate(Gfx::IntPoint delta)
  262. {
  263. m_state_stack.last().translation.translate(delta.to_type<float>());
  264. }
  265. void DisplayListRecorder::save()
  266. {
  267. append(Save {});
  268. m_state_stack.append(m_state_stack.last());
  269. }
  270. void DisplayListRecorder::restore()
  271. {
  272. append(Restore {});
  273. VERIFY(m_state_stack.size() > 1);
  274. m_state_stack.take_last();
  275. }
  276. void DisplayListRecorder::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 DisplayListRecorder::pop_stacking_context()
  295. {
  296. m_state_stack.take_last();
  297. append(PopStackingContext {});
  298. }
  299. void DisplayListRecorder::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 DisplayListRecorder::paint_outer_box_shadow_params(PaintBoxShadowParams params)
  310. {
  311. params.device_content_rect = state().translation.map(params.device_content_rect);
  312. append(PaintOuterBoxShadow { .box_shadow_params = params });
  313. }
  314. void DisplayListRecorder::paint_inner_box_shadow_params(PaintBoxShadowParams params)
  315. {
  316. append(PaintInnerBoxShadow { .box_shadow_params = params });
  317. }
  318. 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)
  319. {
  320. append(PaintTextShadow {
  321. .blur_radius = blur_radius,
  322. .shadow_bounding_rect = bounding_rect,
  323. .text_rect = text_rect,
  324. .glyph_run = glyph_run,
  325. .glyph_run_scale = glyph_run_scale,
  326. .color = color,
  327. .draw_location = state().translation.map(draw_location) });
  328. }
  329. 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, Vector<Gfx::Path> const& clip_paths)
  330. {
  331. if (rect.is_empty())
  332. return;
  333. if (!top_left_radius && !top_right_radius && !bottom_right_radius && !bottom_left_radius) {
  334. fill_rect(rect, color, clip_paths);
  335. return;
  336. }
  337. append(FillRectWithRoundedCorners {
  338. .rect = state().translation.map(rect),
  339. .color = color,
  340. .corner_radii = {
  341. .top_left = top_left_radius,
  342. .top_right = top_right_radius,
  343. .bottom_right = bottom_right_radius,
  344. .bottom_left = bottom_left_radius,
  345. },
  346. .clip_paths = clip_paths,
  347. });
  348. }
  349. void DisplayListRecorder::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 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, 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 DisplayListRecorder::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. }