DisplayListRecorder.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. .cap_style = params.cap_style,
  78. .join_style = params.join_style,
  79. .miter_limit = params.miter_limit,
  80. .dash_array = move(params.dash_array),
  81. .dash_offset = params.dash_offset,
  82. .path_bounding_rect = path_bounding_rect,
  83. .path = move(params.path),
  84. .color = params.color,
  85. .thickness = params.thickness,
  86. .aa_translation = aa_translation,
  87. });
  88. }
  89. void DisplayListRecorder::stroke_path(StrokePathUsingPaintStyleParams params)
  90. {
  91. auto aa_translation = params.translation.value_or(Gfx::FloatPoint {});
  92. auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
  93. // Increase path bounding box by `thickness` to account for stroke.
  94. path_bounding_rect.inflate(params.thickness, params.thickness);
  95. if (path_bounding_rect.is_empty())
  96. return;
  97. append(StrokePathUsingPaintStyle {
  98. .cap_style = params.cap_style,
  99. .join_style = params.join_style,
  100. .miter_limit = params.miter_limit,
  101. .dash_array = move(params.dash_array),
  102. .dash_offset = params.dash_offset,
  103. .path_bounding_rect = path_bounding_rect,
  104. .path = move(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 = 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 { a_rect, color });
  126. }
  127. void DisplayListRecorder::fill_rect_with_linear_gradient(Gfx::IntRect const& gradient_rect, LinearGradientData const& data)
  128. {
  129. if (gradient_rect.is_empty())
  130. return;
  131. append(PaintLinearGradient { gradient_rect, data });
  132. }
  133. void DisplayListRecorder::fill_rect_with_conic_gradient(Gfx::IntRect const& rect, ConicGradientData const& data, Gfx::IntPoint const& position)
  134. {
  135. if (rect.is_empty())
  136. return;
  137. append(PaintConicGradient {
  138. .rect = rect,
  139. .conic_gradient_data = data,
  140. .position = position });
  141. }
  142. void DisplayListRecorder::fill_rect_with_radial_gradient(Gfx::IntRect const& rect, RadialGradientData const& data, Gfx::IntPoint center, Gfx::IntSize size)
  143. {
  144. if (rect.is_empty())
  145. return;
  146. append(PaintRadialGradient {
  147. .rect = rect,
  148. .radial_gradient_data = data,
  149. .center = center,
  150. .size = size });
  151. }
  152. void DisplayListRecorder::draw_rect(Gfx::IntRect const& rect, Color color, bool rough)
  153. {
  154. if (rect.is_empty())
  155. return;
  156. append(DrawRect {
  157. .rect = rect,
  158. .color = color,
  159. .rough = rough });
  160. }
  161. void DisplayListRecorder::draw_painting_surface(Gfx::IntRect const& dst_rect, NonnullRefPtr<Gfx::PaintingSurface> surface, Gfx::IntRect const& src_rect, Gfx::ScalingMode scaling_mode)
  162. {
  163. if (dst_rect.is_empty())
  164. return;
  165. append(DrawPaintingSurface {
  166. .dst_rect = dst_rect,
  167. .surface = surface,
  168. .src_rect = src_rect,
  169. .scaling_mode = scaling_mode,
  170. });
  171. }
  172. void DisplayListRecorder::draw_scaled_immutable_bitmap(Gfx::IntRect const& dst_rect, Gfx::ImmutableBitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::ScalingMode scaling_mode)
  173. {
  174. if (dst_rect.is_empty())
  175. return;
  176. append(DrawScaledImmutableBitmap {
  177. .dst_rect = dst_rect,
  178. .bitmap = bitmap,
  179. .src_rect = src_rect,
  180. .scaling_mode = scaling_mode,
  181. });
  182. }
  183. 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)
  184. {
  185. append(DrawRepeatedImmutableBitmap {
  186. .dst_rect = dst_rect,
  187. .clip_rect = clip_rect,
  188. .bitmap = move(bitmap),
  189. .scaling_mode = scaling_mode,
  190. .repeat = repeat,
  191. });
  192. }
  193. void DisplayListRecorder::draw_line(Gfx::IntPoint from, Gfx::IntPoint to, Color color, int thickness, Gfx::LineStyle style, Color alternate_color)
  194. {
  195. append(DrawLine {
  196. .color = color,
  197. .from = from,
  198. .to = to,
  199. .thickness = thickness,
  200. .style = style,
  201. .alternate_color = alternate_color,
  202. });
  203. }
  204. void DisplayListRecorder::draw_text(Gfx::IntRect const& rect, String raw_text, Gfx::Font const& font, Gfx::TextAlignment alignment, Color color)
  205. {
  206. if (rect.is_empty())
  207. return;
  208. auto glyph_run = Gfx::shape_text({}, 0, raw_text.code_points(), font, Gfx::GlyphRun::TextType::Ltr);
  209. float baseline_x = 0;
  210. if (alignment == Gfx::TextAlignment::CenterLeft) {
  211. baseline_x = rect.x();
  212. } else if (alignment == Gfx::TextAlignment::Center) {
  213. baseline_x = static_cast<float>(rect.x()) + (static_cast<float>(rect.width()) - glyph_run->width()) / 2.0f;
  214. } else if (alignment == Gfx::TextAlignment::CenterRight) {
  215. baseline_x = static_cast<float>(rect.right()) - glyph_run->width();
  216. } else {
  217. // Unimplemented alignment.
  218. TODO();
  219. }
  220. auto metrics = font.pixel_metrics();
  221. float baseline_y = static_cast<float>(rect.y()) + metrics.ascent + (static_cast<float>(rect.height()) - (metrics.ascent + metrics.descent)) / 2.0f;
  222. draw_text_run(Gfx::IntPoint(roundf(baseline_x), roundf(baseline_y)), *glyph_run, color, rect, 1.0, Orientation::Horizontal);
  223. }
  224. void DisplayListRecorder::draw_text_run(Gfx::IntPoint baseline_start, Gfx::GlyphRun const& glyph_run, Color color, Gfx::IntRect const& rect, double scale, Orientation orientation)
  225. {
  226. if (rect.is_empty())
  227. return;
  228. append(DrawGlyphRun {
  229. .glyph_run = glyph_run,
  230. .scale = scale,
  231. .rect = rect,
  232. .translation = baseline_start.to_type<float>(),
  233. .color = color,
  234. .orientation = orientation,
  235. });
  236. }
  237. void DisplayListRecorder::add_clip_rect(Gfx::IntRect const& rect)
  238. {
  239. append(AddClipRect { rect });
  240. }
  241. void DisplayListRecorder::translate(Gfx::IntPoint delta)
  242. {
  243. append(Translate { delta });
  244. }
  245. void DisplayListRecorder::save()
  246. {
  247. append(Save {});
  248. }
  249. void DisplayListRecorder::restore()
  250. {
  251. append(Restore {});
  252. }
  253. void DisplayListRecorder::push_scroll_frame_id(Optional<i32> id)
  254. {
  255. m_scroll_frame_id_stack.append(id);
  256. }
  257. void DisplayListRecorder::pop_scroll_frame_id()
  258. {
  259. (void)m_scroll_frame_id_stack.take_last();
  260. }
  261. void DisplayListRecorder::push_stacking_context(PushStackingContextParams params)
  262. {
  263. append(PushStackingContext {
  264. .opacity = params.opacity,
  265. .source_paintable_rect = params.source_paintable_rect,
  266. .transform = {
  267. .origin = params.transform.origin,
  268. .matrix = params.transform.matrix,
  269. },
  270. .clip_path = params.clip_path });
  271. m_scroll_frame_id_stack.append({});
  272. }
  273. void DisplayListRecorder::pop_stacking_context()
  274. {
  275. (void)m_scroll_frame_id_stack.take_last();
  276. append(PopStackingContext {});
  277. }
  278. void DisplayListRecorder::apply_backdrop_filter(Gfx::IntRect const& backdrop_region, BorderRadiiData const& border_radii_data, CSS::ResolvedFilter const& backdrop_filter)
  279. {
  280. if (backdrop_region.is_empty())
  281. return;
  282. append(ApplyBackdropFilter {
  283. .backdrop_region = backdrop_region,
  284. .border_radii_data = border_radii_data,
  285. .backdrop_filter = backdrop_filter,
  286. });
  287. }
  288. void DisplayListRecorder::paint_outer_box_shadow_params(PaintBoxShadowParams params)
  289. {
  290. append(PaintOuterBoxShadow { .box_shadow_params = params });
  291. }
  292. void DisplayListRecorder::paint_inner_box_shadow_params(PaintBoxShadowParams params)
  293. {
  294. append(PaintInnerBoxShadow { .box_shadow_params = params });
  295. }
  296. 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)
  297. {
  298. append(PaintTextShadow {
  299. .glyph_run = glyph_run,
  300. .glyph_run_scale = glyph_run_scale,
  301. .shadow_bounding_rect = bounding_rect,
  302. .text_rect = text_rect,
  303. .draw_location = draw_location,
  304. .blur_radius = blur_radius,
  305. .color = color });
  306. }
  307. void DisplayListRecorder::fill_rect_with_rounded_corners(Gfx::IntRect const& rect, Color color, CornerRadius top_left_radius, CornerRadius top_right_radius, CornerRadius bottom_right_radius, CornerRadius bottom_left_radius)
  308. {
  309. if (rect.is_empty())
  310. return;
  311. if (!top_left_radius && !top_right_radius && !bottom_right_radius && !bottom_left_radius) {
  312. fill_rect(rect, color);
  313. return;
  314. }
  315. append(FillRectWithRoundedCorners {
  316. .rect = rect,
  317. .color = color,
  318. .corner_radii = {
  319. .top_left = top_left_radius,
  320. .top_right = top_right_radius,
  321. .bottom_right = bottom_right_radius,
  322. .bottom_left = bottom_left_radius,
  323. },
  324. });
  325. }
  326. void DisplayListRecorder::fill_rect_with_rounded_corners(Gfx::IntRect const& a_rect, Color color, int radius)
  327. {
  328. if (a_rect.is_empty())
  329. return;
  330. fill_rect_with_rounded_corners(a_rect, color, radius, radius, radius, radius);
  331. }
  332. 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)
  333. {
  334. if (a_rect.is_empty())
  335. return;
  336. fill_rect_with_rounded_corners(a_rect, color,
  337. { top_left_radius, top_left_radius },
  338. { top_right_radius, top_right_radius },
  339. { bottom_right_radius, bottom_right_radius },
  340. { bottom_left_radius, bottom_left_radius });
  341. }
  342. void DisplayListRecorder::draw_triangle_wave(Gfx::IntPoint a_p1, Gfx::IntPoint a_p2, Color color, int amplitude, int thickness = 1)
  343. {
  344. append(DrawTriangleWave {
  345. .p1 = a_p1,
  346. .p2 = a_p2,
  347. .color = color,
  348. .amplitude = amplitude,
  349. .thickness = thickness });
  350. }
  351. void DisplayListRecorder::paint_scrollbar(int scroll_frame_id, Gfx::IntRect rect, CSSPixelFraction scroll_size, bool vertical)
  352. {
  353. append(PaintScrollBar {
  354. .scroll_frame_id = scroll_frame_id,
  355. .rect = rect,
  356. .scroll_size = scroll_size,
  357. .vertical = vertical });
  358. }
  359. void DisplayListRecorder::apply_opacity(float opacity)
  360. {
  361. append(ApplyOpacity { .opacity = opacity });
  362. }
  363. void DisplayListRecorder::apply_filters(CSS::ResolvedFilter filter)
  364. {
  365. append(ApplyFilters { .filter = filter });
  366. }
  367. void DisplayListRecorder::apply_transform(Gfx::FloatPoint origin, Gfx::FloatMatrix4x4 matrix)
  368. {
  369. append(ApplyTransform {
  370. .origin = origin,
  371. .matrix = matrix,
  372. });
  373. }
  374. void DisplayListRecorder::apply_mask_bitmap(Gfx::IntPoint origin, Gfx::ImmutableBitmap const& bitmap, Gfx::Bitmap::MaskKind kind)
  375. {
  376. append(ApplyMaskBitmap {
  377. .origin = origin,
  378. .bitmap = bitmap,
  379. .kind = kind,
  380. });
  381. }
  382. }