DisplayListRecorder.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. }
  13. DisplayListRecorder::~DisplayListRecorder() = default;
  14. void DisplayListRecorder::append(Command&& command)
  15. {
  16. Optional<i32> scroll_frame_id;
  17. if (!m_scroll_frame_id_stack.is_empty())
  18. scroll_frame_id = m_scroll_frame_id_stack.last();
  19. m_command_list.append(move(command), scroll_frame_id);
  20. }
  21. void DisplayListRecorder::paint_nested_display_list(RefPtr<DisplayList> display_list, Gfx::IntRect rect)
  22. {
  23. append(PaintNestedDisplayList { move(display_list), rect });
  24. }
  25. void DisplayListRecorder::add_rounded_rect_clip(CornerRadii corner_radii, Gfx::IntRect border_rect, CornerClip corner_clip)
  26. {
  27. append(AddRoundedRectClip { corner_radii, border_rect, corner_clip });
  28. }
  29. void DisplayListRecorder::add_mask(RefPtr<DisplayList> display_list, Gfx::IntRect rect)
  30. {
  31. append(AddMask { move(display_list), rect });
  32. }
  33. void DisplayListRecorder::fill_rect(Gfx::IntRect const& rect, Color color)
  34. {
  35. if (rect.is_empty())
  36. return;
  37. append(FillRect { rect, color });
  38. }
  39. void DisplayListRecorder::fill_path(FillPathUsingColorParams params)
  40. {
  41. auto aa_translation = params.translation.value_or(Gfx::FloatPoint {});
  42. auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
  43. if (path_bounding_rect.is_empty())
  44. return;
  45. append(FillPathUsingColor {
  46. .path_bounding_rect = path_bounding_rect,
  47. .path = move(params.path),
  48. .color = params.color,
  49. .winding_rule = params.winding_rule,
  50. .aa_translation = aa_translation,
  51. });
  52. }
  53. void DisplayListRecorder::fill_path(FillPathUsingPaintStyleParams params)
  54. {
  55. auto aa_translation = params.translation.value_or(Gfx::FloatPoint {});
  56. auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
  57. if (path_bounding_rect.is_empty())
  58. return;
  59. append(FillPathUsingPaintStyle {
  60. .path_bounding_rect = path_bounding_rect,
  61. .path = move(params.path),
  62. .paint_style = params.paint_style,
  63. .winding_rule = params.winding_rule,
  64. .opacity = params.opacity,
  65. .aa_translation = aa_translation,
  66. });
  67. }
  68. void DisplayListRecorder::stroke_path(StrokePathUsingColorParams params)
  69. {
  70. auto aa_translation = params.translation.value_or(Gfx::FloatPoint {});
  71. auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
  72. // Increase path bounding box by `thickness` to account for stroke.
  73. path_bounding_rect.inflate(params.thickness, params.thickness);
  74. if (path_bounding_rect.is_empty())
  75. return;
  76. append(StrokePathUsingColor {
  77. .path_bounding_rect = path_bounding_rect,
  78. .path = move(params.path),
  79. .color = params.color,
  80. .thickness = params.thickness,
  81. .aa_translation = aa_translation,
  82. });
  83. }
  84. void DisplayListRecorder::stroke_path(StrokePathUsingPaintStyleParams params)
  85. {
  86. auto aa_translation = params.translation.value_or(Gfx::FloatPoint {});
  87. auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
  88. // Increase path bounding box by `thickness` to account for stroke.
  89. path_bounding_rect.inflate(params.thickness, params.thickness);
  90. if (path_bounding_rect.is_empty())
  91. return;
  92. append(StrokePathUsingPaintStyle {
  93. .path_bounding_rect = path_bounding_rect,
  94. .path = move(params.path),
  95. .paint_style = params.paint_style,
  96. .thickness = params.thickness,
  97. .opacity = params.opacity,
  98. .aa_translation = aa_translation,
  99. });
  100. }
  101. void DisplayListRecorder::draw_ellipse(Gfx::IntRect const& a_rect, Color color, int thickness)
  102. {
  103. if (a_rect.is_empty())
  104. return;
  105. append(DrawEllipse {
  106. .rect = a_rect,
  107. .color = color,
  108. .thickness = thickness,
  109. });
  110. }
  111. void DisplayListRecorder::fill_ellipse(Gfx::IntRect const& a_rect, Color color)
  112. {
  113. if (a_rect.is_empty())
  114. return;
  115. append(FillEllipse { a_rect, color });
  116. }
  117. void DisplayListRecorder::fill_rect_with_linear_gradient(Gfx::IntRect const& gradient_rect, LinearGradientData const& data)
  118. {
  119. if (gradient_rect.is_empty())
  120. return;
  121. append(PaintLinearGradient { gradient_rect, data });
  122. }
  123. void DisplayListRecorder::fill_rect_with_conic_gradient(Gfx::IntRect const& rect, ConicGradientData const& data, Gfx::IntPoint const& position)
  124. {
  125. if (rect.is_empty())
  126. return;
  127. append(PaintConicGradient {
  128. .rect = rect,
  129. .conic_gradient_data = data,
  130. .position = position });
  131. }
  132. void DisplayListRecorder::fill_rect_with_radial_gradient(Gfx::IntRect const& rect, RadialGradientData const& data, Gfx::IntPoint center, Gfx::IntSize size)
  133. {
  134. if (rect.is_empty())
  135. return;
  136. append(PaintRadialGradient {
  137. .rect = rect,
  138. .radial_gradient_data = data,
  139. .center = center,
  140. .size = size });
  141. }
  142. void DisplayListRecorder::draw_rect(Gfx::IntRect const& rect, Color color, bool rough)
  143. {
  144. if (rect.is_empty())
  145. return;
  146. append(DrawRect {
  147. .rect = rect,
  148. .color = color,
  149. .rough = rough });
  150. }
  151. void DisplayListRecorder::draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::ScalingMode scaling_mode)
  152. {
  153. if (dst_rect.is_empty())
  154. return;
  155. append(DrawScaledBitmap {
  156. .dst_rect = dst_rect,
  157. .bitmap = bitmap,
  158. .src_rect = src_rect,
  159. .scaling_mode = scaling_mode,
  160. });
  161. }
  162. void DisplayListRecorder::draw_scaled_immutable_bitmap(Gfx::IntRect const& dst_rect, Gfx::ImmutableBitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::ScalingMode scaling_mode)
  163. {
  164. if (dst_rect.is_empty())
  165. return;
  166. append(DrawScaledImmutableBitmap {
  167. .dst_rect = dst_rect,
  168. .bitmap = bitmap,
  169. .src_rect = src_rect,
  170. .scaling_mode = scaling_mode,
  171. });
  172. }
  173. 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)
  174. {
  175. append(DrawRepeatedImmutableBitmap {
  176. .dst_rect = dst_rect,
  177. .clip_rect = clip_rect,
  178. .bitmap = move(bitmap),
  179. .scaling_mode = scaling_mode,
  180. .repeat = repeat,
  181. });
  182. }
  183. void DisplayListRecorder::draw_line(Gfx::IntPoint from, Gfx::IntPoint to, Color color, int thickness, Gfx::LineStyle style, Color alternate_color)
  184. {
  185. append(DrawLine {
  186. .color = color,
  187. .from = from,
  188. .to = to,
  189. .thickness = thickness,
  190. .style = style,
  191. .alternate_color = alternate_color,
  192. });
  193. }
  194. void DisplayListRecorder::draw_text(Gfx::IntRect const& rect, String raw_text, Gfx::Font const& font, Gfx::TextAlignment alignment, Color color)
  195. {
  196. if (rect.is_empty())
  197. return;
  198. auto glyph_run = Gfx::shape_text({}, raw_text.code_points(), font, Gfx::GlyphRun::TextType::Ltr);
  199. float baseline_x = 0;
  200. if (alignment == Gfx::TextAlignment::CenterLeft) {
  201. baseline_x = rect.x();
  202. } else if (alignment == Gfx::TextAlignment::Center) {
  203. baseline_x = static_cast<float>(rect.x()) + (static_cast<float>(rect.width()) - glyph_run->width()) / 2.0f;
  204. } else if (alignment == Gfx::TextAlignment::CenterRight) {
  205. baseline_x = static_cast<float>(rect.right()) - glyph_run->width();
  206. } else {
  207. // Unimplemented alignment.
  208. TODO();
  209. }
  210. auto metrics = font.pixel_metrics();
  211. float baseline_y = static_cast<float>(rect.y()) + metrics.ascent + (static_cast<float>(rect.height()) - (metrics.ascent + metrics.descent)) / 2.0f;
  212. draw_text_run(Gfx::IntPoint(roundf(baseline_x), roundf(baseline_y)), *glyph_run, color, rect, 1.0);
  213. }
  214. void DisplayListRecorder::draw_text_run(Gfx::IntPoint baseline_start, Gfx::GlyphRun const& glyph_run, Color color, Gfx::IntRect const& rect, double scale)
  215. {
  216. if (rect.is_empty())
  217. return;
  218. append(DrawGlyphRun {
  219. .glyph_run = glyph_run,
  220. .color = color,
  221. .rect = rect,
  222. .translation = baseline_start.to_type<float>(),
  223. .scale = scale,
  224. });
  225. }
  226. void DisplayListRecorder::add_clip_rect(Gfx::IntRect const& rect)
  227. {
  228. append(AddClipRect { rect });
  229. }
  230. void DisplayListRecorder::translate(Gfx::IntPoint delta)
  231. {
  232. append(Translate { delta });
  233. }
  234. void DisplayListRecorder::save()
  235. {
  236. append(Save {});
  237. }
  238. void DisplayListRecorder::restore()
  239. {
  240. append(Restore {});
  241. }
  242. void DisplayListRecorder::push_scroll_frame_id(Optional<i32> id)
  243. {
  244. m_scroll_frame_id_stack.append(id);
  245. }
  246. void DisplayListRecorder::pop_scroll_frame_id()
  247. {
  248. (void)m_scroll_frame_id_stack.take_last();
  249. }
  250. void DisplayListRecorder::push_stacking_context(PushStackingContextParams params)
  251. {
  252. append(PushStackingContext {
  253. .opacity = params.opacity,
  254. .filter = params.filter,
  255. .source_paintable_rect = params.source_paintable_rect,
  256. .transform = {
  257. .origin = params.transform.origin,
  258. .matrix = params.transform.matrix,
  259. },
  260. .clip_path = params.clip_path });
  261. m_scroll_frame_id_stack.append({});
  262. }
  263. void DisplayListRecorder::pop_stacking_context()
  264. {
  265. (void)m_scroll_frame_id_stack.take_last();
  266. append(PopStackingContext {});
  267. }
  268. void DisplayListRecorder::apply_backdrop_filter(Gfx::IntRect const& backdrop_region, BorderRadiiData const& border_radii_data, CSS::ResolvedFilter const& backdrop_filter)
  269. {
  270. if (backdrop_region.is_empty())
  271. return;
  272. append(ApplyBackdropFilter {
  273. .backdrop_region = backdrop_region,
  274. .border_radii_data = border_radii_data,
  275. .backdrop_filter = backdrop_filter,
  276. });
  277. }
  278. void DisplayListRecorder::paint_outer_box_shadow_params(PaintBoxShadowParams params)
  279. {
  280. params.device_content_rect = params.device_content_rect;
  281. append(PaintOuterBoxShadow { .box_shadow_params = params });
  282. }
  283. void DisplayListRecorder::paint_inner_box_shadow_params(PaintBoxShadowParams params)
  284. {
  285. append(PaintInnerBoxShadow { .box_shadow_params = params });
  286. }
  287. 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)
  288. {
  289. append(PaintTextShadow {
  290. .blur_radius = blur_radius,
  291. .shadow_bounding_rect = bounding_rect,
  292. .text_rect = text_rect,
  293. .glyph_run = glyph_run,
  294. .glyph_run_scale = glyph_run_scale,
  295. .color = color,
  296. .draw_location = draw_location });
  297. }
  298. 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)
  299. {
  300. if (rect.is_empty())
  301. return;
  302. if (!top_left_radius && !top_right_radius && !bottom_right_radius && !bottom_left_radius) {
  303. fill_rect(rect, color);
  304. return;
  305. }
  306. append(FillRectWithRoundedCorners {
  307. .rect = rect,
  308. .color = color,
  309. .corner_radii = {
  310. .top_left = top_left_radius,
  311. .top_right = top_right_radius,
  312. .bottom_right = bottom_right_radius,
  313. .bottom_left = bottom_left_radius,
  314. },
  315. });
  316. }
  317. void DisplayListRecorder::fill_rect_with_rounded_corners(Gfx::IntRect const& a_rect, Color color, int radius)
  318. {
  319. if (a_rect.is_empty())
  320. return;
  321. fill_rect_with_rounded_corners(a_rect, color, radius, radius, radius, radius);
  322. }
  323. 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)
  324. {
  325. if (a_rect.is_empty())
  326. return;
  327. fill_rect_with_rounded_corners(a_rect, color,
  328. { top_left_radius, top_left_radius },
  329. { top_right_radius, top_right_radius },
  330. { bottom_right_radius, bottom_right_radius },
  331. { bottom_left_radius, bottom_left_radius });
  332. }
  333. void DisplayListRecorder::draw_triangle_wave(Gfx::IntPoint a_p1, Gfx::IntPoint a_p2, Color color, int amplitude, int thickness = 1)
  334. {
  335. append(DrawTriangleWave {
  336. .p1 = a_p1,
  337. .p2 = a_p2,
  338. .color = color,
  339. .amplitude = amplitude,
  340. .thickness = thickness });
  341. }
  342. void DisplayListRecorder::paint_scrollbar(int scroll_frame_id, Gfx::IntRect rect, CSSPixelFraction scroll_size, bool vertical)
  343. {
  344. append(PaintScrollBar {
  345. .scroll_frame_id = scroll_frame_id,
  346. .rect = rect,
  347. .scroll_size = scroll_size,
  348. .vertical = vertical });
  349. }
  350. void DisplayListRecorder::apply_opacity(float opacity)
  351. {
  352. append(ApplyOpacity { .opacity = opacity });
  353. }
  354. void DisplayListRecorder::apply_transform(Gfx::FloatPoint origin, Gfx::FloatMatrix4x4 matrix)
  355. {
  356. append(ApplyTransform {
  357. .origin = origin,
  358. .matrix = matrix,
  359. });
  360. }
  361. void DisplayListRecorder::apply_mask_bitmap(Gfx::IntPoint origin, Gfx::Bitmap const& bitmap, Gfx::Bitmap::MaskKind kind)
  362. {
  363. append(ApplyMaskBitmap {
  364. .origin = origin,
  365. .bitmap = bitmap,
  366. .kind = kind,
  367. });
  368. }
  369. }