DisplayListRecorder.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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() = default;
  15. void DisplayListRecorder::append(Command&& command)
  16. {
  17. m_command_list.append(move(command), state().scroll_frame_id);
  18. }
  19. void DisplayListRecorder::paint_nested_display_list(RefPtr<DisplayList> display_list, Gfx::IntRect rect)
  20. {
  21. append(PaintNestedDisplayList {
  22. .display_list = move(display_list),
  23. .rect = state().translation.map(rect) });
  24. }
  25. void DisplayListRecorder::add_rounded_rect_clip(CornerRadii corner_radii, Gfx::IntRect border_rect, CornerClip corner_clip)
  26. {
  27. append(AddRoundedRectClip {
  28. corner_radii,
  29. border_rect = state().translation.map(border_rect),
  30. corner_clip });
  31. }
  32. void DisplayListRecorder::add_mask(RefPtr<DisplayList> display_list, Gfx::IntRect rect)
  33. {
  34. append(AddMask {
  35. .display_list = move(display_list),
  36. .rect = state().translation.map(rect) });
  37. }
  38. void DisplayListRecorder::fill_rect(Gfx::IntRect const& rect, Color color)
  39. {
  40. if (rect.is_empty())
  41. return;
  42. append(FillRect {
  43. .rect = state().translation.map(rect),
  44. .color = color,
  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 = move(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 = move(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 = move(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 = move(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)
  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. }
  136. void DisplayListRecorder::fill_rect_with_conic_gradient(Gfx::IntRect const& rect, ConicGradientData const& data, Gfx::IntPoint const& position)
  137. {
  138. if (rect.is_empty())
  139. return;
  140. append(PaintConicGradient {
  141. .rect = state().translation.map(rect),
  142. .conic_gradient_data = data,
  143. .position = position });
  144. }
  145. void DisplayListRecorder::fill_rect_with_radial_gradient(Gfx::IntRect const& rect, RadialGradientData const& data, Gfx::IntPoint center, Gfx::IntSize size)
  146. {
  147. if (rect.is_empty())
  148. return;
  149. append(PaintRadialGradient {
  150. .rect = state().translation.map(rect),
  151. .radial_gradient_data = data,
  152. .center = center,
  153. .size = size });
  154. }
  155. void DisplayListRecorder::draw_rect(Gfx::IntRect const& rect, Color color, bool rough)
  156. {
  157. if (rect.is_empty())
  158. return;
  159. append(DrawRect {
  160. .rect = state().translation.map(rect),
  161. .color = color,
  162. .rough = rough });
  163. }
  164. void DisplayListRecorder::draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::ScalingMode scaling_mode)
  165. {
  166. if (dst_rect.is_empty())
  167. return;
  168. append(DrawScaledBitmap {
  169. .dst_rect = state().translation.map(dst_rect),
  170. .bitmap = bitmap,
  171. .src_rect = src_rect,
  172. .scaling_mode = scaling_mode,
  173. });
  174. }
  175. void DisplayListRecorder::draw_scaled_immutable_bitmap(Gfx::IntRect const& dst_rect, Gfx::ImmutableBitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::ScalingMode scaling_mode)
  176. {
  177. if (dst_rect.is_empty())
  178. return;
  179. append(DrawScaledImmutableBitmap {
  180. .dst_rect = state().translation.map(dst_rect),
  181. .bitmap = bitmap,
  182. .src_rect = src_rect,
  183. .scaling_mode = scaling_mode,
  184. });
  185. }
  186. 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)
  187. {
  188. append(DrawRepeatedImmutableBitmap {
  189. .dst_rect = dst_rect,
  190. .clip_rect = clip_rect,
  191. .bitmap = move(bitmap),
  192. .scaling_mode = scaling_mode,
  193. .repeat = repeat,
  194. });
  195. }
  196. void DisplayListRecorder::draw_line(Gfx::IntPoint from, Gfx::IntPoint to, Color color, int thickness, Gfx::LineStyle style, Color alternate_color)
  197. {
  198. append(DrawLine {
  199. .color = color,
  200. .from = state().translation.map(from),
  201. .to = state().translation.map(to),
  202. .thickness = thickness,
  203. .style = style,
  204. .alternate_color = alternate_color,
  205. });
  206. }
  207. void DisplayListRecorder::draw_text(Gfx::IntRect const& rect, String raw_text, Gfx::Font const& font, Gfx::TextAlignment alignment, Color color)
  208. {
  209. if (rect.is_empty())
  210. return;
  211. auto glyph_run = Gfx::shape_text({}, raw_text.code_points(), font, Gfx::GlyphRun::TextType::Ltr);
  212. float baseline_x = 0;
  213. if (alignment == Gfx::TextAlignment::CenterLeft) {
  214. baseline_x = rect.x();
  215. } else if (alignment == Gfx::TextAlignment::Center) {
  216. baseline_x = static_cast<float>(rect.x()) + (static_cast<float>(rect.width()) - glyph_run->width()) / 2.0f;
  217. } else if (alignment == Gfx::TextAlignment::CenterRight) {
  218. baseline_x = static_cast<float>(rect.right()) - glyph_run->width();
  219. } else {
  220. // Unimplemented alignment.
  221. TODO();
  222. }
  223. auto metrics = font.pixel_metrics();
  224. float baseline_y = static_cast<float>(rect.y()) + metrics.ascent + (static_cast<float>(rect.height()) - (metrics.ascent + metrics.descent)) / 2.0f;
  225. draw_text_run(Gfx::IntPoint(roundf(baseline_x), roundf(baseline_y)), *glyph_run, color, rect, 1.0);
  226. }
  227. void DisplayListRecorder::draw_text_run(Gfx::IntPoint baseline_start, Gfx::GlyphRun const& glyph_run, Color color, Gfx::IntRect const& rect, double scale)
  228. {
  229. if (rect.is_empty())
  230. return;
  231. auto transformed_baseline_start = state().translation.map(baseline_start).to_type<float>();
  232. append(DrawGlyphRun {
  233. .glyph_run = glyph_run,
  234. .color = color,
  235. .rect = state().translation.map(rect),
  236. .translation = transformed_baseline_start,
  237. .scale = scale,
  238. });
  239. }
  240. void DisplayListRecorder::add_clip_rect(Gfx::IntRect const& rect)
  241. {
  242. append(AddClipRect { .rect = state().translation.map(rect) });
  243. }
  244. void DisplayListRecorder::translate(int dx, int dy)
  245. {
  246. m_state_stack.last().translation.translate(dx, dy);
  247. }
  248. void DisplayListRecorder::translate(Gfx::IntPoint delta)
  249. {
  250. m_state_stack.last().translation.translate(delta.to_type<float>());
  251. }
  252. void DisplayListRecorder::save()
  253. {
  254. append(Save {});
  255. m_state_stack.append(m_state_stack.last());
  256. }
  257. void DisplayListRecorder::restore()
  258. {
  259. append(Restore {});
  260. VERIFY(m_state_stack.size() > 1);
  261. m_state_stack.take_last();
  262. }
  263. void DisplayListRecorder::push_stacking_context(PushStackingContextParams params)
  264. {
  265. append(PushStackingContext {
  266. .opacity = params.opacity,
  267. .is_fixed_position = params.is_fixed_position,
  268. .source_paintable_rect = params.source_paintable_rect,
  269. // No translations apply to fixed-position stacking contexts.
  270. .post_transform_translation = params.is_fixed_position
  271. ? Gfx::IntPoint {}
  272. : state().translation.translation().to_rounded<int>(),
  273. .transform = {
  274. .origin = params.transform.origin,
  275. .matrix = params.transform.matrix,
  276. },
  277. .mask = params.mask,
  278. .clip_path = params.clip_path });
  279. m_state_stack.append(State());
  280. }
  281. void DisplayListRecorder::pop_stacking_context()
  282. {
  283. m_state_stack.take_last();
  284. append(PopStackingContext {});
  285. }
  286. void DisplayListRecorder::apply_backdrop_filter(Gfx::IntRect const& backdrop_region, BorderRadiiData const& border_radii_data, CSS::ResolvedBackdropFilter const& backdrop_filter)
  287. {
  288. if (backdrop_region.is_empty())
  289. return;
  290. append(ApplyBackdropFilter {
  291. .backdrop_region = state().translation.map(backdrop_region),
  292. .border_radii_data = border_radii_data,
  293. .backdrop_filter = backdrop_filter,
  294. });
  295. }
  296. void DisplayListRecorder::paint_outer_box_shadow_params(PaintBoxShadowParams params)
  297. {
  298. params.device_content_rect = state().translation.map(params.device_content_rect);
  299. append(PaintOuterBoxShadow { .box_shadow_params = params });
  300. }
  301. void DisplayListRecorder::paint_inner_box_shadow_params(PaintBoxShadowParams params)
  302. {
  303. append(PaintInnerBoxShadow { .box_shadow_params = params });
  304. }
  305. 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)
  306. {
  307. append(PaintTextShadow {
  308. .blur_radius = blur_radius,
  309. .shadow_bounding_rect = bounding_rect,
  310. .text_rect = text_rect,
  311. .glyph_run = glyph_run,
  312. .glyph_run_scale = glyph_run_scale,
  313. .color = color,
  314. .draw_location = state().translation.map(draw_location) });
  315. }
  316. void DisplayListRecorder::fill_rect_with_rounded_corners(Gfx::IntRect const& rect, Color color, Gfx::CornerRadius top_left_radius, Gfx::CornerRadius top_right_radius, Gfx::CornerRadius bottom_right_radius, Gfx::CornerRadius bottom_left_radius)
  317. {
  318. if (rect.is_empty())
  319. return;
  320. if (!top_left_radius && !top_right_radius && !bottom_right_radius && !bottom_left_radius) {
  321. fill_rect(rect, color);
  322. return;
  323. }
  324. append(FillRectWithRoundedCorners {
  325. .rect = state().translation.map(rect),
  326. .color = color,
  327. .corner_radii = {
  328. .top_left = top_left_radius,
  329. .top_right = top_right_radius,
  330. .bottom_right = bottom_right_radius,
  331. .bottom_left = bottom_left_radius,
  332. },
  333. });
  334. }
  335. void DisplayListRecorder::fill_rect_with_rounded_corners(Gfx::IntRect const& a_rect, Color color, int radius)
  336. {
  337. if (a_rect.is_empty())
  338. return;
  339. fill_rect_with_rounded_corners(a_rect, color, radius, radius, radius, radius);
  340. }
  341. 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)
  342. {
  343. if (a_rect.is_empty())
  344. return;
  345. fill_rect_with_rounded_corners(a_rect, color,
  346. { top_left_radius, top_left_radius },
  347. { top_right_radius, top_right_radius },
  348. { bottom_right_radius, bottom_right_radius },
  349. { bottom_left_radius, bottom_left_radius });
  350. }
  351. void DisplayListRecorder::draw_triangle_wave(Gfx::IntPoint a_p1, Gfx::IntPoint a_p2, Color color, int amplitude, int thickness = 1)
  352. {
  353. append(DrawTriangleWave {
  354. .p1 = state().translation.map(a_p1),
  355. .p2 = state().translation.map(a_p2),
  356. .color = color,
  357. .amplitude = amplitude,
  358. .thickness = thickness });
  359. }
  360. void DisplayListRecorder::paint_scrollbar(int scroll_frame_id, Gfx::IntRect rect, CSSPixelFraction scroll_size, bool vertical)
  361. {
  362. append(PaintScrollBar {
  363. .scroll_frame_id = scroll_frame_id,
  364. .rect = rect,
  365. .scroll_size = scroll_size,
  366. .vertical = vertical });
  367. }
  368. }