ShadowPainting.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2022, MacDue <macdue@dueutil.tech>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/NumericLimits.h>
  9. #include <LibGfx/DisjointRectSet.h>
  10. #include <LibGfx/Filters/StackBlurFilter.h>
  11. #include <LibGfx/Font/Font.h>
  12. #include <LibGfx/Painter.h>
  13. #include <LibWeb/Layout/LineBoxFragment.h>
  14. #include <LibWeb/Layout/Node.h>
  15. #include <LibWeb/Painting/BorderPainting.h>
  16. #include <LibWeb/Painting/BorderRadiusCornerClipper.h>
  17. #include <LibWeb/Painting/PaintContext.h>
  18. #include <LibWeb/Painting/PaintOuterBoxShadowParams.h>
  19. #include <LibWeb/Painting/PaintableBox.h>
  20. #include <LibWeb/Painting/ShadowPainting.h>
  21. namespace Web::Painting {
  22. void paint_inner_box_shadow(Gfx::Painter& painter, PaintOuterBoxShadowParams params)
  23. {
  24. auto device_content_rect = params.device_content_rect;
  25. DevicePixels offset_x = params.offset_x;
  26. DevicePixels offset_y = params.offset_y;
  27. DevicePixels blur_radius = params.blur_radius;
  28. DevicePixels spread_distance = params.spread_distance;
  29. auto shadows_bitmap_rect = device_content_rect.inflated(
  30. blur_radius.value() + offset_y.value(),
  31. blur_radius.value() + abs(offset_x.value()),
  32. blur_radius.value() + abs(offset_y.value()),
  33. blur_radius.value() + offset_x.value());
  34. auto shadows_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, shadows_bitmap_rect.size().to_type<int>());
  35. if (shadows_bitmap.is_error()) {
  36. dbgln("Unable to allocate temporary bitmap {} for box-shadow rendering: {}", device_content_rect, shadows_bitmap.error());
  37. return;
  38. }
  39. auto shadow_bitmap = shadows_bitmap.release_value();
  40. Gfx::Painter shadow_painter { *shadow_bitmap };
  41. Gfx::AntiAliasingPainter shadow_aa_painter { shadow_painter };
  42. auto device_content_rect_int = device_content_rect.to_type<int>();
  43. auto origin_device_content_rect = device_content_rect_int.translated(-device_content_rect_int.x(), -device_content_rect_int.y());
  44. auto outer_shadow_rect = origin_device_content_rect.translated({ offset_x + blur_radius.value(), offset_y + blur_radius.value() });
  45. auto spread_distance_value = spread_distance.value();
  46. auto inner_shadow_rect = outer_shadow_rect.inflated(-spread_distance_value, -spread_distance_value, -spread_distance_value, -spread_distance_value);
  47. outer_shadow_rect.inflate(
  48. blur_radius.value() + offset_y.value(),
  49. blur_radius.value() + abs(offset_x.value()),
  50. blur_radius.value() + abs(offset_y.value()),
  51. blur_radius.value() + offset_x.value());
  52. auto top_left_corner = params.corner_radii.top_left;
  53. auto top_right_corner = params.corner_radii.top_right;
  54. auto bottom_right_corner = params.corner_radii.bottom_right;
  55. auto bottom_left_corner = params.corner_radii.bottom_left;
  56. shadow_painter.fill_rect(outer_shadow_rect, params.box_shadow_data.color.with_alpha(0xff));
  57. if (params.border_radii.has_any_radius()) {
  58. shadow_aa_painter.fill_rect_with_rounded_corners(inner_shadow_rect, params.box_shadow_data.color.with_alpha(0xff),
  59. top_left_corner, top_right_corner, bottom_right_corner, bottom_left_corner,
  60. Gfx::AntiAliasingPainter::BlendMode::AlphaSubtract);
  61. } else {
  62. shadow_painter.clear_rect(inner_shadow_rect, Color::Transparent);
  63. }
  64. Gfx::StackBlurFilter filter(*shadow_bitmap);
  65. filter.process_rgba(blur_radius.value(), params.box_shadow_data.color);
  66. Gfx::PainterStateSaver save { painter };
  67. painter.add_clip_rect(device_content_rect_int);
  68. painter.blit({ device_content_rect_int.left() - blur_radius.value(), device_content_rect_int.top() - blur_radius.value() },
  69. *shadow_bitmap, shadow_bitmap->rect(), params.box_shadow_data.color.alpha() / 255.);
  70. }
  71. struct OuterBoxShadowMetrics {
  72. DevicePixelRect shadow_bitmap_rect;
  73. DevicePixelRect non_blurred_shadow_rect;
  74. DevicePixelRect inner_bounding_rect;
  75. DevicePixels blurred_edge_thickness;
  76. DevicePixels double_radius;
  77. DevicePixels blur_radius;
  78. DevicePixelRect top_left_corner_rect;
  79. DevicePixelRect top_right_corner_rect;
  80. DevicePixelRect bottom_right_corner_rect;
  81. DevicePixelRect bottom_left_corner_rect;
  82. DevicePixelPoint top_left_corner_blit_pos;
  83. DevicePixelPoint top_right_corner_blit_pos;
  84. DevicePixelPoint bottom_right_corner_blit_pos;
  85. DevicePixelPoint bottom_left_corner_blit_pos;
  86. DevicePixelSize top_left_corner_size;
  87. DevicePixelSize top_right_corner_size;
  88. DevicePixelSize bottom_right_corner_size;
  89. DevicePixelSize bottom_left_corner_size;
  90. DevicePixels left_start;
  91. DevicePixels top_start;
  92. DevicePixels right_start;
  93. DevicePixels bottom_start;
  94. DevicePixelRect left_edge_rect;
  95. DevicePixelRect right_edge_rect;
  96. DevicePixelRect top_edge_rect;
  97. DevicePixelRect bottom_edge_rect;
  98. CornerRadius top_left_shadow_corner;
  99. CornerRadius top_right_shadow_corner;
  100. CornerRadius bottom_right_shadow_corner;
  101. CornerRadius bottom_left_shadow_corner;
  102. };
  103. static OuterBoxShadowMetrics get_outer_box_shadow_configuration(PaintOuterBoxShadowParams params)
  104. {
  105. auto device_content_rect = params.device_content_rect;
  106. auto top_left_corner = params.corner_radii.top_left;
  107. auto top_right_corner = params.corner_radii.top_right;
  108. auto bottom_right_corner = params.corner_radii.bottom_right;
  109. auto bottom_left_corner = params.corner_radii.bottom_left;
  110. DevicePixels offset_x = params.offset_x;
  111. DevicePixels offset_y = params.offset_y;
  112. DevicePixels blur_radius = params.blur_radius;
  113. DevicePixels spread_distance = params.spread_distance;
  114. // Our blur cannot handle radii over 255 so there's no point trying (255 is silly big anyway)
  115. blur_radius = clamp(blur_radius, 0, 255);
  116. auto top_left_shadow_corner = top_left_corner;
  117. auto top_right_shadow_corner = top_right_corner;
  118. auto bottom_right_shadow_corner = bottom_right_corner;
  119. auto bottom_left_shadow_corner = bottom_left_corner;
  120. auto spread_corner = [&](auto& corner) {
  121. if (corner) {
  122. corner.horizontal_radius += spread_distance.value();
  123. corner.vertical_radius += spread_distance.value();
  124. }
  125. };
  126. spread_corner(top_left_shadow_corner);
  127. spread_corner(top_right_shadow_corner);
  128. spread_corner(bottom_right_shadow_corner);
  129. spread_corner(bottom_left_shadow_corner);
  130. auto expansion = spread_distance - (blur_radius * 2);
  131. DevicePixelRect inner_bounding_rect = {
  132. device_content_rect.x() + offset_x - expansion,
  133. device_content_rect.y() + offset_y - expansion,
  134. device_content_rect.width() + 2 * expansion,
  135. device_content_rect.height() + 2 * expansion
  136. };
  137. // Calculating and blurring the box-shadow full size is expensive, and wasteful - aside from the corners,
  138. // all vertical strips of the shadow are identical, and the same goes for horizontal ones.
  139. // So instead, we generate a shadow bitmap that is just large enough to include the corners and 1px of
  140. // non-corner, and then we repeatedly blit sections of it. This is similar to a NinePatch on Android.
  141. auto double_radius = blur_radius * 2;
  142. auto blurred_edge_thickness = blur_radius * 4;
  143. auto default_corner_size = Gfx::IntSize { double_radius, double_radius };
  144. auto top_left_corner_size = (top_left_shadow_corner ? top_left_shadow_corner.as_rect().size() : default_corner_size).to_type<DevicePixels>();
  145. auto top_right_corner_size = (top_right_shadow_corner ? top_right_shadow_corner.as_rect().size() : default_corner_size).to_type<DevicePixels>();
  146. auto bottom_left_corner_size = (bottom_left_shadow_corner ? bottom_left_shadow_corner.as_rect().size() : default_corner_size).to_type<DevicePixels>();
  147. auto bottom_right_corner_size = (bottom_right_shadow_corner ? bottom_right_shadow_corner.as_rect().size() : default_corner_size).to_type<DevicePixels>();
  148. auto non_blurred_shadow_rect = device_content_rect.inflated(spread_distance, spread_distance, spread_distance, spread_distance);
  149. auto max_edge_width = non_blurred_shadow_rect.width() / 2;
  150. auto max_edge_height = non_blurred_shadow_rect.height() / 2;
  151. auto extra_edge_width = non_blurred_shadow_rect.width() % 2;
  152. auto extra_edge_height = non_blurred_shadow_rect.height() % 2;
  153. auto clip_corner_size = [&](auto& size, auto const& corner, DevicePixels x_bonus = 0, DevicePixels y_bonus = 0) {
  154. auto max_x = (max_edge_width + x_bonus).value();
  155. auto max_y = (max_edge_height + y_bonus).value();
  156. auto min_x = max(corner.horizontal_radius, min(double_radius, max_x).value());
  157. auto min_y = max(corner.vertical_radius, min(double_radius, max_y).value());
  158. if (min_x <= max_x)
  159. size.set_width(clamp(size.width(), min_x, max_x));
  160. if (min_y <= max_y)
  161. size.set_height(clamp(size.height(), min_y, max_y));
  162. };
  163. clip_corner_size(top_left_corner_size, top_left_corner, extra_edge_width, extra_edge_height);
  164. clip_corner_size(top_right_corner_size, top_right_corner, 0, extra_edge_height);
  165. clip_corner_size(bottom_left_corner_size, bottom_left_corner, extra_edge_width);
  166. clip_corner_size(bottom_right_corner_size, bottom_right_corner);
  167. auto shadow_bitmap_rect = DevicePixelRect(
  168. 0, 0,
  169. max(max(
  170. top_left_corner_size.width() + top_right_corner_size.width(),
  171. bottom_left_corner_size.width() + bottom_right_corner_size.width()),
  172. max(top_left_corner_size.width() + bottom_right_corner_size.width(),
  173. bottom_left_corner_size.width() + top_right_corner_size.width()))
  174. + 1 + blurred_edge_thickness,
  175. max(max(
  176. top_left_corner_size.height() + bottom_left_corner_size.height(),
  177. top_right_corner_size.height() + bottom_right_corner_size.height()),
  178. max(top_left_corner_size.height() + bottom_right_corner_size.height(),
  179. bottom_left_corner_size.height() + top_right_corner_size.height()))
  180. + 1 + blurred_edge_thickness);
  181. auto top_left_corner_rect = DevicePixelRect {
  182. 0, 0,
  183. top_left_corner_size.width() + double_radius,
  184. top_left_corner_size.height() + double_radius
  185. };
  186. auto top_right_corner_rect = DevicePixelRect {
  187. shadow_bitmap_rect.width() - (top_right_corner_size.width() + double_radius), 0,
  188. top_right_corner_size.width() + double_radius,
  189. top_right_corner_size.height() + double_radius
  190. };
  191. auto bottom_right_corner_rect = DevicePixelRect {
  192. shadow_bitmap_rect.width() - (bottom_right_corner_size.width() + double_radius),
  193. shadow_bitmap_rect.height() - (bottom_right_corner_size.height() + double_radius),
  194. bottom_right_corner_size.width() + double_radius,
  195. bottom_right_corner_size.height() + double_radius
  196. };
  197. auto bottom_left_corner_rect = DevicePixelRect {
  198. 0, shadow_bitmap_rect.height() - (bottom_left_corner_size.height() + double_radius),
  199. bottom_left_corner_size.width() + double_radius,
  200. bottom_left_corner_size.height() + double_radius
  201. };
  202. auto horizontal_edge_width = min(max_edge_height, double_radius) + double_radius;
  203. auto vertical_edge_width = min(max_edge_width, double_radius) + double_radius;
  204. auto horizontal_top_edge_width = min(max_edge_height + extra_edge_height, double_radius) + double_radius;
  205. auto vertical_left_edge_width = min(max_edge_width + extra_edge_width, double_radius) + double_radius;
  206. DevicePixelRect left_edge_rect { 0, top_left_corner_rect.height(), vertical_left_edge_width, 1 };
  207. DevicePixelRect right_edge_rect { shadow_bitmap_rect.width() - vertical_edge_width, top_right_corner_rect.height(), vertical_edge_width, 1 };
  208. DevicePixelRect top_edge_rect { top_left_corner_rect.width(), 0, 1, horizontal_top_edge_width };
  209. DevicePixelRect bottom_edge_rect { bottom_left_corner_rect.width(), shadow_bitmap_rect.height() - horizontal_edge_width, 1, horizontal_edge_width };
  210. auto left_start = inner_bounding_rect.left() - blurred_edge_thickness;
  211. auto right_start = inner_bounding_rect.left() + inner_bounding_rect.width() + (blurred_edge_thickness - vertical_edge_width);
  212. auto top_start = inner_bounding_rect.top() - blurred_edge_thickness;
  213. auto bottom_start = inner_bounding_rect.top() + inner_bounding_rect.height() + (blurred_edge_thickness - horizontal_edge_width);
  214. auto top_left_corner_blit_pos = inner_bounding_rect.top_left().translated(-blurred_edge_thickness, -blurred_edge_thickness);
  215. auto top_right_corner_blit_pos = inner_bounding_rect.top_right().translated(-top_right_corner_size.width() + double_radius, -blurred_edge_thickness);
  216. auto bottom_left_corner_blit_pos = inner_bounding_rect.bottom_left().translated(-blurred_edge_thickness, -bottom_left_corner_size.height() + double_radius);
  217. auto bottom_right_corner_blit_pos = inner_bounding_rect.bottom_right().translated(-bottom_right_corner_size.width() + double_radius, -bottom_right_corner_size.height() + double_radius);
  218. return OuterBoxShadowMetrics {
  219. .shadow_bitmap_rect = shadow_bitmap_rect,
  220. .non_blurred_shadow_rect = non_blurred_shadow_rect,
  221. .inner_bounding_rect = inner_bounding_rect,
  222. .blurred_edge_thickness = blurred_edge_thickness,
  223. .double_radius = double_radius,
  224. .blur_radius = blur_radius,
  225. .top_left_corner_rect = top_left_corner_rect,
  226. .top_right_corner_rect = top_right_corner_rect,
  227. .bottom_right_corner_rect = bottom_right_corner_rect,
  228. .bottom_left_corner_rect = bottom_left_corner_rect,
  229. .top_left_corner_blit_pos = top_left_corner_blit_pos,
  230. .top_right_corner_blit_pos = top_right_corner_blit_pos,
  231. .bottom_right_corner_blit_pos = bottom_right_corner_blit_pos,
  232. .bottom_left_corner_blit_pos = bottom_left_corner_blit_pos,
  233. .top_left_corner_size = top_left_corner_size,
  234. .top_right_corner_size = top_right_corner_size,
  235. .bottom_right_corner_size = bottom_right_corner_size,
  236. .bottom_left_corner_size = bottom_left_corner_size,
  237. .left_start = left_start,
  238. .top_start = top_start,
  239. .right_start = right_start,
  240. .bottom_start = bottom_start,
  241. .left_edge_rect = left_edge_rect,
  242. .right_edge_rect = right_edge_rect,
  243. .top_edge_rect = top_edge_rect,
  244. .bottom_edge_rect = bottom_edge_rect,
  245. .top_left_shadow_corner = top_left_shadow_corner,
  246. .top_right_shadow_corner = top_right_shadow_corner,
  247. .bottom_right_shadow_corner = bottom_right_shadow_corner,
  248. .bottom_left_shadow_corner = bottom_left_shadow_corner,
  249. };
  250. }
  251. Gfx::IntRect get_outer_box_shadow_bounding_rect(PaintOuterBoxShadowParams params)
  252. {
  253. auto shadow_config = get_outer_box_shadow_configuration(params);
  254. auto const& top_left_corner_blit_pos = shadow_config.top_left_corner_blit_pos;
  255. auto const& top_right_corner_blit_pos = shadow_config.top_right_corner_blit_pos;
  256. auto const& bottom_left_corner_blit_pos = shadow_config.bottom_left_corner_blit_pos;
  257. auto const& top_right_corner_rect = shadow_config.top_right_corner_rect;
  258. auto const& bottom_left_corner_rect = shadow_config.bottom_left_corner_rect;
  259. return Gfx::IntRect {
  260. top_left_corner_blit_pos,
  261. { top_right_corner_blit_pos.x() - top_left_corner_blit_pos.x() + top_right_corner_rect.width(),
  262. bottom_left_corner_blit_pos.y() - top_left_corner_blit_pos.y() + bottom_left_corner_rect.height() }
  263. };
  264. }
  265. void paint_outer_box_shadow(Gfx::Painter& painter, PaintOuterBoxShadowParams params)
  266. {
  267. auto const& box_shadow_data = params.box_shadow_data;
  268. auto const& device_content_rect = params.device_content_rect;
  269. auto const& border_radii = params.border_radii;
  270. auto const& top_left_corner = params.corner_radii.top_left;
  271. auto const& top_right_corner = params.corner_radii.top_right;
  272. auto const& bottom_right_corner = params.corner_radii.bottom_right;
  273. auto const& bottom_left_corner = params.corner_radii.bottom_left;
  274. DevicePixels offset_x = params.offset_x;
  275. DevicePixels offset_y = params.offset_y;
  276. auto shadow_config = get_outer_box_shadow_configuration(params);
  277. auto const& shadow_bitmap_rect = shadow_config.shadow_bitmap_rect;
  278. auto const& non_blurred_shadow_rect = shadow_config.non_blurred_shadow_rect;
  279. auto const& inner_bounding_rect = shadow_config.inner_bounding_rect;
  280. auto const& blurred_edge_thickness = shadow_config.blurred_edge_thickness;
  281. auto const& double_radius = shadow_config.double_radius;
  282. auto const& blur_radius = shadow_config.blur_radius;
  283. auto const& top_left_corner_rect = shadow_config.top_left_corner_rect;
  284. auto const& top_right_corner_rect = shadow_config.top_right_corner_rect;
  285. auto const& bottom_right_corner_rect = shadow_config.bottom_right_corner_rect;
  286. auto const& bottom_left_corner_rect = shadow_config.bottom_left_corner_rect;
  287. auto const& top_left_corner_blit_pos = shadow_config.top_left_corner_blit_pos;
  288. auto const& top_right_corner_blit_pos = shadow_config.top_right_corner_blit_pos;
  289. auto const& bottom_right_corner_blit_pos = shadow_config.bottom_right_corner_blit_pos;
  290. auto const& bottom_left_corner_blit_pos = shadow_config.bottom_left_corner_blit_pos;
  291. auto const& top_left_corner_size = shadow_config.top_left_corner_size;
  292. auto const& top_right_corner_size = shadow_config.top_right_corner_size;
  293. auto const& bottom_right_corner_size = shadow_config.bottom_right_corner_size;
  294. auto const& bottom_left_corner_size = shadow_config.bottom_left_corner_size;
  295. auto const& left_start = shadow_config.left_start;
  296. auto const& top_start = shadow_config.top_start;
  297. auto const& right_start = shadow_config.right_start;
  298. auto const& bottom_start = shadow_config.bottom_start;
  299. auto const& left_edge_rect = shadow_config.left_edge_rect;
  300. auto const& right_edge_rect = shadow_config.right_edge_rect;
  301. auto const& top_edge_rect = shadow_config.top_edge_rect;
  302. auto const& bottom_edge_rect = shadow_config.bottom_edge_rect;
  303. auto const& top_left_shadow_corner = shadow_config.top_left_shadow_corner;
  304. auto const& top_right_shadow_corner = shadow_config.top_right_shadow_corner;
  305. auto const& bottom_right_shadow_corner = shadow_config.bottom_right_shadow_corner;
  306. auto const& bottom_left_shadow_corner = shadow_config.bottom_left_shadow_corner;
  307. auto fill_rect_masked = [](auto& painter, auto fill_rect, auto mask_rect, auto color) {
  308. Gfx::DisjointRectSet<DevicePixels> rect_set;
  309. rect_set.add(fill_rect);
  310. auto shattered = rect_set.shatter(mask_rect);
  311. for (auto& rect : shattered.rects())
  312. painter.fill_rect(rect.template to_type<int>(), color);
  313. };
  314. // If there's no blurring, nor rounded corners, we can save a lot of effort.
  315. if (blur_radius == 0 && !border_radii.has_any_radius()) {
  316. fill_rect_masked(painter, non_blurred_shadow_rect.translated(offset_x, offset_y), device_content_rect, box_shadow_data.color);
  317. return;
  318. }
  319. auto paint_shadow_infill = [&] {
  320. if (!params.border_radii.has_any_radius())
  321. return painter.fill_rect(inner_bounding_rect.to_type<int>(), box_shadow_data.color);
  322. auto top_left_inner_width = top_left_corner_rect.width() - blurred_edge_thickness;
  323. auto top_left_inner_height = top_left_corner_rect.height() - blurred_edge_thickness;
  324. auto top_right_inner_width = top_right_corner_rect.width() - blurred_edge_thickness;
  325. auto top_right_inner_height = top_right_corner_rect.height() - blurred_edge_thickness;
  326. auto bottom_right_inner_width = bottom_right_corner_rect.width() - blurred_edge_thickness;
  327. auto bottom_right_inner_height = bottom_right_corner_rect.height() - blurred_edge_thickness;
  328. auto bottom_left_inner_width = bottom_left_corner_rect.width() - blurred_edge_thickness;
  329. auto bottom_left_inner_height = bottom_left_corner_rect.height() - blurred_edge_thickness;
  330. DevicePixelRect top_rect {
  331. inner_bounding_rect.x() + top_left_inner_width,
  332. inner_bounding_rect.y(),
  333. inner_bounding_rect.width() - top_left_inner_width - top_right_inner_width,
  334. top_left_inner_height
  335. };
  336. DevicePixelRect right_rect {
  337. inner_bounding_rect.x() + inner_bounding_rect.width() - top_right_inner_width,
  338. inner_bounding_rect.y() + top_right_inner_height,
  339. top_right_inner_width,
  340. inner_bounding_rect.height() - top_right_inner_height - bottom_right_inner_height
  341. };
  342. DevicePixelRect bottom_rect {
  343. inner_bounding_rect.x() + bottom_left_inner_width,
  344. inner_bounding_rect.y() + inner_bounding_rect.height() - bottom_right_inner_height,
  345. inner_bounding_rect.width() - bottom_left_inner_width - bottom_right_inner_width,
  346. bottom_right_inner_height
  347. };
  348. DevicePixelRect left_rect {
  349. inner_bounding_rect.x(),
  350. inner_bounding_rect.y() + top_left_inner_height,
  351. bottom_left_inner_width,
  352. inner_bounding_rect.height() - top_left_inner_height - bottom_left_inner_height
  353. };
  354. DevicePixelRect inner = {
  355. left_rect.x() + left_rect.width(),
  356. left_rect.y(),
  357. inner_bounding_rect.width() - left_rect.width() - right_rect.width(),
  358. inner_bounding_rect.height() - top_rect.height() - bottom_rect.height()
  359. };
  360. painter.fill_rect(top_rect.to_type<int>(), box_shadow_data.color);
  361. painter.fill_rect(right_rect.to_type<int>(), box_shadow_data.color);
  362. painter.fill_rect(bottom_rect.to_type<int>(), box_shadow_data.color);
  363. painter.fill_rect(left_rect.to_type<int>(), box_shadow_data.color);
  364. painter.fill_rect(inner.to_type<int>(), box_shadow_data.color);
  365. };
  366. auto shadows_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, shadow_bitmap_rect.size().to_type<int>());
  367. if (shadows_bitmap.is_error()) {
  368. dbgln("Unable to allocate temporary bitmap {} for box-shadow rendering: {}", shadow_bitmap_rect, shadows_bitmap.error());
  369. return;
  370. }
  371. auto shadow_bitmap = shadows_bitmap.release_value();
  372. Gfx::Painter corner_painter { *shadow_bitmap };
  373. Gfx::AntiAliasingPainter aa_corner_painter { corner_painter };
  374. aa_corner_painter.fill_rect_with_rounded_corners(
  375. shadow_bitmap_rect.shrunken(double_radius, double_radius, double_radius, double_radius).to_type<int>(),
  376. box_shadow_data.color, top_left_shadow_corner, top_right_shadow_corner, bottom_right_shadow_corner, bottom_left_shadow_corner);
  377. Gfx::StackBlurFilter filter(*shadow_bitmap);
  378. filter.process_rgba(blur_radius.value(), box_shadow_data.color);
  379. auto paint_shadow = [&](DevicePixelRect clip_rect) {
  380. Gfx::PainterStateSaver save { painter };
  381. painter.add_clip_rect(clip_rect.to_type<int>());
  382. paint_shadow_infill();
  383. // Corners
  384. painter.blit(top_left_corner_blit_pos.to_type<int>(), shadow_bitmap, top_left_corner_rect.to_type<int>());
  385. painter.blit(top_right_corner_blit_pos.to_type<int>(), shadow_bitmap, top_right_corner_rect.to_type<int>());
  386. painter.blit(bottom_left_corner_blit_pos.to_type<int>(), shadow_bitmap, bottom_left_corner_rect.to_type<int>());
  387. painter.blit(bottom_right_corner_blit_pos.to_type<int>(), shadow_bitmap, bottom_right_corner_rect.to_type<int>());
  388. // Horizontal edges
  389. for (auto x = inner_bounding_rect.left() + (bottom_left_corner_size.width() - double_radius); x < inner_bounding_rect.right() - (bottom_right_corner_size.width() - double_radius); ++x)
  390. painter.blit({ x, bottom_start }, shadow_bitmap, bottom_edge_rect.to_type<int>());
  391. for (auto x = inner_bounding_rect.left() + (top_left_corner_size.width() - double_radius); x < inner_bounding_rect.right() - (top_right_corner_size.width() - double_radius); ++x)
  392. painter.blit({ x, top_start }, shadow_bitmap, top_edge_rect.to_type<int>());
  393. // Vertical edges
  394. for (auto y = inner_bounding_rect.top() + (top_right_corner_size.height() - double_radius); y < inner_bounding_rect.bottom() - (bottom_right_corner_size.height() - double_radius); ++y)
  395. painter.blit({ right_start, y }, shadow_bitmap, right_edge_rect.to_type<int>());
  396. for (auto y = inner_bounding_rect.top() + (top_left_corner_size.height() - double_radius); y < inner_bounding_rect.bottom() - (bottom_left_corner_size.height() - double_radius); ++y)
  397. painter.blit({ left_start, y }, shadow_bitmap, left_edge_rect.to_type<int>());
  398. };
  399. // FIXME: Painter only lets us define a clip-rect which discards drawing outside of it, whereas here we want
  400. // a rect which discards drawing inside it. So, we run the draw operations 4 to 8 times with clip-rects
  401. // covering each side of the content_rect exactly once.
  402. // If we were painting a shadow without a border radius we'd want to clip everything inside the box below.
  403. // If painting a shadow with rounded corners (but still rectangular) we want to clip everything inside
  404. // the box except the corners. This gives us an upper bound of 8 shadow paints now :^(.
  405. // (However, this does not seem to be the costly part in profiling).
  406. //
  407. // ┌───┬────────┬───┐
  408. // │ │xxxxxxxx│ │
  409. // ├───┼────────┼───┤
  410. // │xxx│xxxxxxxx│xxx│
  411. // │xxx│xxxxxxxx│xxx│
  412. // │xxx│xxxxxxxx│xxx│
  413. // │xxx│xxxxxxxx│xxx│
  414. // │xxx│xxxxxxxx│xxx│
  415. // ├───┼────────┼───┤
  416. // │ │ xxxxxx │ │
  417. // └───┴────────┴───┘
  418. // How many times would you like to paint the shadow sir?
  419. // Yes.
  420. // FIXME: Could reduce the shadow paints from 8 to 4 for shadows with all corner radii 50%.
  421. // FIXME: We use this since we want the clip rect to include everything after a certain x or y.
  422. // Note: Using painter.target()->width() or height() does not work, when the painter is a small
  423. // translated bitmap rather than full screen, as the clip rect may not intersect.
  424. constexpr auto really_large_number = NumericLimits<int>::max() / 2;
  425. // Everything above content_rect, including sides
  426. paint_shadow({ 0, 0, really_large_number, device_content_rect.top() });
  427. // Everything below content_rect, including sides
  428. paint_shadow({ 0, device_content_rect.bottom(), really_large_number, really_large_number });
  429. // Everything directly to the left of content_rect
  430. paint_shadow({ 0, device_content_rect.top(), device_content_rect.left(), device_content_rect.height() });
  431. // Everything directly to the right of content_rect
  432. paint_shadow({ device_content_rect.right(), device_content_rect.top(), really_large_number, device_content_rect.height() });
  433. if (top_left_corner) {
  434. // Inside the top left corner (the part outside the border radius)
  435. auto top_left = top_left_corner.as_rect().to_type<DevicePixels>().translated(device_content_rect.top_left());
  436. paint_shadow(top_left);
  437. }
  438. if (top_right_corner) {
  439. // Inside the top right corner (the part outside the border radius)
  440. auto top_right = top_right_corner.as_rect().to_type<DevicePixels>().translated(device_content_rect.top_right().translated(-top_right_corner.horizontal_radius, 0));
  441. paint_shadow(top_right);
  442. }
  443. if (bottom_right_corner) {
  444. // Inside the bottom right corner (the part outside the border radius)
  445. auto bottom_right = bottom_right_corner.as_rect().to_type<DevicePixels>().translated(device_content_rect.bottom_right().translated(-bottom_right_corner.horizontal_radius, -bottom_right_corner.vertical_radius));
  446. paint_shadow(bottom_right);
  447. }
  448. if (bottom_left_corner) {
  449. // Inside the bottom left corner (the part outside the border radius)
  450. auto bottom_left = bottom_left_corner.as_rect().to_type<DevicePixels>().translated(device_content_rect.bottom_left().translated(0, -bottom_left_corner.vertical_radius));
  451. paint_shadow(bottom_left);
  452. }
  453. }
  454. void paint_box_shadow(PaintContext& context,
  455. CSSPixelRect const& bordered_content_rect,
  456. CSSPixelRect const& borderless_content_rect,
  457. BordersData const& borders_data,
  458. BorderRadiiData const& border_radii,
  459. Vector<ShadowData> const& box_shadow_layers)
  460. {
  461. // Note: Box-shadow layers are ordered front-to-back, so we paint them in reverse
  462. for (auto& box_shadow_data : box_shadow_layers.in_reverse()) {
  463. DevicePixels offset_x = context.rounded_device_pixels(box_shadow_data.offset_x);
  464. DevicePixels offset_y = context.rounded_device_pixels(box_shadow_data.offset_y);
  465. DevicePixels blur_radius = context.rounded_device_pixels(box_shadow_data.blur_radius);
  466. DevicePixels spread_distance = context.rounded_device_pixels(box_shadow_data.spread_distance);
  467. DevicePixelRect device_content_rect;
  468. if (box_shadow_data.placement == ShadowPlacement::Inner) {
  469. device_content_rect = context.rounded_device_rect(borderless_content_rect);
  470. } else {
  471. device_content_rect = context.rounded_device_rect(bordered_content_rect);
  472. }
  473. auto params = PaintOuterBoxShadowParams {
  474. .painter = context.recording_painter(),
  475. .content_rect = bordered_content_rect,
  476. .border_radii = border_radii,
  477. .box_shadow_data = box_shadow_data,
  478. .corner_radii = CornerRadii {
  479. .top_left = border_radii.top_left.as_corner(context),
  480. .top_right = border_radii.top_right.as_corner(context),
  481. .bottom_right = border_radii.bottom_right.as_corner(context),
  482. .bottom_left = border_radii.bottom_left.as_corner(context) },
  483. .offset_x = offset_x,
  484. .offset_y = offset_y,
  485. .blur_radius = blur_radius,
  486. .spread_distance = spread_distance,
  487. .device_content_rect = device_content_rect,
  488. };
  489. if (box_shadow_data.placement == ShadowPlacement::Inner) {
  490. params.border_radii.shrink(borders_data.top.width, borders_data.right.width, borders_data.bottom.width, borders_data.left.width);
  491. ScopedCornerRadiusClip corner_clipper { context, device_content_rect, params.border_radii, CornerClip::Outside };
  492. context.recording_painter().paint_inner_box_shadow_params(params);
  493. } else {
  494. ScopedCornerRadiusClip corner_clipper { context, device_content_rect, border_radii, CornerClip::Inside };
  495. context.recording_painter().paint_outer_box_shadow_params(params);
  496. }
  497. }
  498. }
  499. void paint_text_shadow(PaintContext& context, PaintableFragment const& fragment, Vector<ShadowData> const& shadow_layers)
  500. {
  501. if (shadow_layers.is_empty() || fragment.glyph_run().is_empty())
  502. return;
  503. auto fragment_width = context.enclosing_device_pixels(fragment.width()).value();
  504. auto fragment_height = context.enclosing_device_pixels(fragment.height()).value();
  505. auto draw_rect = context.enclosing_device_rect(fragment.absolute_rect()).to_type<int>();
  506. auto fragment_baseline = context.rounded_device_pixels(fragment.baseline()).value();
  507. Vector<Gfx::DrawGlyphOrEmoji> scaled_glyph_run;
  508. scaled_glyph_run.ensure_capacity(fragment.glyph_run().size());
  509. for (auto glyph : fragment.glyph_run()) {
  510. glyph.visit([&](auto& glyph) {
  511. glyph.font = *glyph.font->with_size(glyph.font->point_size() * static_cast<float>(context.device_pixels_per_css_pixel()));
  512. glyph.position = glyph.position.scaled(context.device_pixels_per_css_pixel());
  513. });
  514. scaled_glyph_run.append(move(glyph));
  515. }
  516. // Note: Box-shadow layers are ordered front-to-back, so we paint them in reverse
  517. for (auto& layer : shadow_layers.in_reverse()) {
  518. int offset_x = context.rounded_device_pixels(layer.offset_x).value();
  519. int offset_y = context.rounded_device_pixels(layer.offset_y).value();
  520. int blur_radius = context.rounded_device_pixels(layer.blur_radius).value();
  521. // Space around the painted text to allow it to blur.
  522. // FIXME: Include spread in this once we use that.
  523. int margin = blur_radius * 2;
  524. Gfx::IntRect text_rect {
  525. margin, margin,
  526. fragment_width, fragment_height
  527. };
  528. Gfx::IntRect bounding_rect {
  529. 0, 0,
  530. text_rect.width() + margin + margin,
  531. text_rect.height() + margin + margin
  532. };
  533. Gfx::IntPoint draw_location {
  534. draw_rect.x() + offset_x - margin,
  535. draw_rect.y() + offset_y - margin
  536. };
  537. context.recording_painter().paint_text_shadow(blur_radius, bounding_rect, text_rect, scaled_glyph_run, layer.color, fragment_baseline, draw_location);
  538. }
  539. }
  540. }