DisplayListRecorder.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 = adopt_ref(*new Gfx::GlyphRun({}, font, Gfx::GlyphRun::TextType::Ltr));
  212. float glyph_run_width = 0;
  213. Gfx::for_each_glyph_position(
  214. { 0, 0 }, raw_text.code_points(), font, [&](Gfx::DrawGlyphOrEmoji const& glyph_or_emoji) {
  215. glyph_run->append(glyph_or_emoji);
  216. },
  217. glyph_run_width);
  218. float baseline_x = 0;
  219. if (alignment == Gfx::TextAlignment::CenterLeft) {
  220. baseline_x = rect.x();
  221. } else if (alignment == Gfx::TextAlignment::Center) {
  222. baseline_x = static_cast<float>(rect.x()) + (static_cast<float>(rect.width()) - glyph_run_width) / 2.0f;
  223. } else if (alignment == Gfx::TextAlignment::CenterRight) {
  224. baseline_x = static_cast<float>(rect.right()) - glyph_run_width;
  225. } else {
  226. // Unimplemented alignment.
  227. TODO();
  228. }
  229. auto metrics = font.pixel_metrics();
  230. float baseline_y = static_cast<float>(rect.y()) + metrics.ascent + (static_cast<float>(rect.height()) - (metrics.ascent + metrics.descent)) / 2.0f;
  231. draw_text_run(Gfx::IntPoint(roundf(baseline_x), roundf(baseline_y)), *glyph_run, color, rect, 1.0);
  232. }
  233. void DisplayListRecorder::draw_text_run(Gfx::IntPoint baseline_start, Gfx::GlyphRun const& glyph_run, Color color, Gfx::IntRect const& rect, double scale)
  234. {
  235. if (rect.is_empty())
  236. return;
  237. auto transformed_baseline_start = state().translation.map(baseline_start).to_type<float>();
  238. append(DrawGlyphRun {
  239. .glyph_run = glyph_run,
  240. .color = color,
  241. .rect = state().translation.map(rect),
  242. .translation = transformed_baseline_start,
  243. .scale = scale,
  244. });
  245. }
  246. void DisplayListRecorder::add_clip_rect(Gfx::IntRect const& rect)
  247. {
  248. append(AddClipRect { .rect = state().translation.map(rect) });
  249. }
  250. void DisplayListRecorder::translate(int dx, int dy)
  251. {
  252. m_state_stack.last().translation.translate(dx, dy);
  253. }
  254. void DisplayListRecorder::translate(Gfx::IntPoint delta)
  255. {
  256. m_state_stack.last().translation.translate(delta.to_type<float>());
  257. }
  258. void DisplayListRecorder::save()
  259. {
  260. append(Save {});
  261. m_state_stack.append(m_state_stack.last());
  262. }
  263. void DisplayListRecorder::restore()
  264. {
  265. append(Restore {});
  266. VERIFY(m_state_stack.size() > 1);
  267. m_state_stack.take_last();
  268. }
  269. void DisplayListRecorder::push_stacking_context(PushStackingContextParams params)
  270. {
  271. append(PushStackingContext {
  272. .opacity = params.opacity,
  273. .is_fixed_position = params.is_fixed_position,
  274. .source_paintable_rect = params.source_paintable_rect,
  275. // No translations apply to fixed-position stacking contexts.
  276. .post_transform_translation = params.is_fixed_position
  277. ? Gfx::IntPoint {}
  278. : state().translation.translation().to_rounded<int>(),
  279. .transform = {
  280. .origin = params.transform.origin,
  281. .matrix = params.transform.matrix,
  282. },
  283. .mask = params.mask,
  284. .clip_path = params.clip_path });
  285. m_state_stack.append(State());
  286. }
  287. void DisplayListRecorder::pop_stacking_context()
  288. {
  289. m_state_stack.take_last();
  290. append(PopStackingContext {});
  291. }
  292. void DisplayListRecorder::apply_backdrop_filter(Gfx::IntRect const& backdrop_region, BorderRadiiData const& border_radii_data, CSS::ResolvedBackdropFilter const& backdrop_filter)
  293. {
  294. if (backdrop_region.is_empty())
  295. return;
  296. append(ApplyBackdropFilter {
  297. .backdrop_region = state().translation.map(backdrop_region),
  298. .border_radii_data = border_radii_data,
  299. .backdrop_filter = backdrop_filter,
  300. });
  301. }
  302. void DisplayListRecorder::paint_outer_box_shadow_params(PaintBoxShadowParams params)
  303. {
  304. params.device_content_rect = state().translation.map(params.device_content_rect);
  305. append(PaintOuterBoxShadow { .box_shadow_params = params });
  306. }
  307. void DisplayListRecorder::paint_inner_box_shadow_params(PaintBoxShadowParams params)
  308. {
  309. append(PaintInnerBoxShadow { .box_shadow_params = params });
  310. }
  311. 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)
  312. {
  313. append(PaintTextShadow {
  314. .blur_radius = blur_radius,
  315. .shadow_bounding_rect = bounding_rect,
  316. .text_rect = text_rect,
  317. .glyph_run = glyph_run,
  318. .glyph_run_scale = glyph_run_scale,
  319. .color = color,
  320. .draw_location = state().translation.map(draw_location) });
  321. }
  322. 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)
  323. {
  324. if (rect.is_empty())
  325. return;
  326. if (!top_left_radius && !top_right_radius && !bottom_right_radius && !bottom_left_radius) {
  327. fill_rect(rect, color);
  328. return;
  329. }
  330. append(FillRectWithRoundedCorners {
  331. .rect = state().translation.map(rect),
  332. .color = color,
  333. .corner_radii = {
  334. .top_left = top_left_radius,
  335. .top_right = top_right_radius,
  336. .bottom_right = bottom_right_radius,
  337. .bottom_left = bottom_left_radius,
  338. },
  339. });
  340. }
  341. void DisplayListRecorder::fill_rect_with_rounded_corners(Gfx::IntRect const& a_rect, Color color, int radius)
  342. {
  343. if (a_rect.is_empty())
  344. return;
  345. fill_rect_with_rounded_corners(a_rect, color, radius, radius, radius, radius);
  346. }
  347. 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)
  348. {
  349. if (a_rect.is_empty())
  350. return;
  351. fill_rect_with_rounded_corners(a_rect, color,
  352. { top_left_radius, top_left_radius },
  353. { top_right_radius, top_right_radius },
  354. { bottom_right_radius, bottom_right_radius },
  355. { bottom_left_radius, bottom_left_radius });
  356. }
  357. void DisplayListRecorder::draw_triangle_wave(Gfx::IntPoint a_p1, Gfx::IntPoint a_p2, Color color, int amplitude, int thickness = 1)
  358. {
  359. append(DrawTriangleWave {
  360. .p1 = state().translation.map(a_p1),
  361. .p2 = state().translation.map(a_p2),
  362. .color = color,
  363. .amplitude = amplitude,
  364. .thickness = thickness });
  365. }
  366. void DisplayListRecorder::paint_scrollbar(int scroll_frame_id, Gfx::IntRect rect, CSSPixelFraction scroll_size, bool vertical)
  367. {
  368. append(PaintScrollBar {
  369. .scroll_frame_id = scroll_frame_id,
  370. .rect = rect,
  371. .scroll_size = scroll_size,
  372. .vertical = vertical });
  373. }
  374. }