DisplayListPlayerSkia.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. /*
  2. * Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <core/SkBitmap.h>
  7. #include <core/SkBlurTypes.h>
  8. #include <core/SkCanvas.h>
  9. #include <core/SkColorFilter.h>
  10. #include <core/SkFont.h>
  11. #include <core/SkFontMgr.h>
  12. #include <core/SkMaskFilter.h>
  13. #include <core/SkPath.h>
  14. #include <core/SkPathBuilder.h>
  15. #include <core/SkPathEffect.h>
  16. #include <core/SkRRect.h>
  17. #include <core/SkSurface.h>
  18. #include <effects/SkDashPathEffect.h>
  19. #include <effects/SkGradientShader.h>
  20. #include <effects/SkImageFilters.h>
  21. #include <gpu/GrDirectContext.h>
  22. #include <gpu/ganesh/SkSurfaceGanesh.h>
  23. #include <pathops/SkPathOps.h>
  24. #include <LibGfx/Font/ScaledFont.h>
  25. #include <LibGfx/PathSkia.h>
  26. #include <LibWeb/CSS/ComputedValues.h>
  27. #include <LibWeb/Painting/DisplayListPlayerSkia.h>
  28. #include <LibWeb/Painting/ShadowPainting.h>
  29. #ifdef USE_VULKAN
  30. # include <gpu/ganesh/vk/GrVkDirectContext.h>
  31. # include <gpu/vk/GrVkBackendContext.h>
  32. # include <gpu/vk/VulkanBackendContext.h>
  33. # include <gpu/vk/VulkanExtensions.h>
  34. #endif
  35. #ifdef AK_OS_MACOS
  36. # define FixedPoint FixedPointMacOS
  37. # define Duration DurationMacOS
  38. # include <gpu/GrBackendSurface.h>
  39. # include <gpu/ganesh/mtl/GrMtlBackendContext.h>
  40. # include <gpu/ganesh/mtl/GrMtlDirectContext.h>
  41. # undef FixedPoint
  42. # undef Duration
  43. #endif
  44. namespace Web::Painting {
  45. class DisplayListPlayerSkia::SkiaSurface {
  46. public:
  47. SkCanvas& canvas() const { return *m_surface->getCanvas(); }
  48. SkiaSurface(sk_sp<SkSurface> surface)
  49. : m_surface(move(surface))
  50. {
  51. }
  52. void read_into_bitmap(Gfx::Bitmap& bitmap)
  53. {
  54. auto image_info = SkImageInfo::Make(bitmap.width(), bitmap.height(), kBGRA_8888_SkColorType, kPremul_SkAlphaType);
  55. SkPixmap pixmap(image_info, bitmap.begin(), bitmap.pitch());
  56. m_surface->readPixels(pixmap, 0, 0);
  57. }
  58. sk_sp<SkSurface> make_surface(int width, int height)
  59. {
  60. return m_surface->makeSurface(width, height);
  61. }
  62. private:
  63. sk_sp<SkSurface> m_surface;
  64. };
  65. #ifdef USE_VULKAN
  66. class SkiaVulkanBackendContext final : public SkiaBackendContext {
  67. AK_MAKE_NONCOPYABLE(SkiaVulkanBackendContext);
  68. AK_MAKE_NONMOVABLE(SkiaVulkanBackendContext);
  69. public:
  70. SkiaVulkanBackendContext(sk_sp<GrDirectContext> context, NonnullOwnPtr<skgpu::VulkanExtensions> extensions)
  71. : m_context(move(context))
  72. , m_extensions(move(extensions))
  73. {
  74. }
  75. ~SkiaVulkanBackendContext() override {};
  76. void flush_and_submit() override
  77. {
  78. m_context->flush();
  79. m_context->submit(GrSyncCpu::kYes);
  80. }
  81. sk_sp<SkSurface> create_surface(int width, int height)
  82. {
  83. auto image_info = SkImageInfo::Make(width, height, kBGRA_8888_SkColorType, kPremul_SkAlphaType);
  84. return SkSurfaces::RenderTarget(m_context.get(), skgpu::Budgeted::kYes, image_info);
  85. }
  86. skgpu::VulkanExtensions const* extensions() const { return m_extensions.ptr(); }
  87. private:
  88. sk_sp<GrDirectContext> m_context;
  89. NonnullOwnPtr<skgpu::VulkanExtensions> m_extensions;
  90. };
  91. OwnPtr<SkiaBackendContext> DisplayListPlayerSkia::create_vulkan_context(Core::VulkanContext& vulkan_context)
  92. {
  93. GrVkBackendContext backend_context;
  94. backend_context.fInstance = vulkan_context.instance;
  95. backend_context.fDevice = vulkan_context.logical_device;
  96. backend_context.fQueue = vulkan_context.graphics_queue;
  97. backend_context.fPhysicalDevice = vulkan_context.physical_device;
  98. backend_context.fMaxAPIVersion = vulkan_context.api_version;
  99. backend_context.fGetProc = [](char const* proc_name, VkInstance instance, VkDevice device) {
  100. if (device != VK_NULL_HANDLE) {
  101. return vkGetDeviceProcAddr(device, proc_name);
  102. }
  103. return vkGetInstanceProcAddr(instance, proc_name);
  104. };
  105. auto extensions = make<skgpu::VulkanExtensions>();
  106. backend_context.fVkExtensions = extensions.ptr();
  107. sk_sp<GrDirectContext> ctx = GrDirectContexts::MakeVulkan(backend_context);
  108. VERIFY(ctx);
  109. return make<SkiaVulkanBackendContext>(ctx, move(extensions));
  110. }
  111. DisplayListPlayerSkia::DisplayListPlayerSkia(SkiaBackendContext& context, Gfx::Bitmap& bitmap)
  112. {
  113. VERIFY(bitmap.format() == Gfx::BitmapFormat::BGRA8888);
  114. auto surface = static_cast<SkiaVulkanBackendContext&>(context).create_surface(bitmap.width(), bitmap.height());
  115. m_surface = make<SkiaSurface>(surface);
  116. m_flush_context = [&bitmap, &surface = m_surface, &context] {
  117. context.flush_and_submit();
  118. surface->read_into_bitmap(bitmap);
  119. };
  120. }
  121. #endif
  122. #ifdef AK_OS_MACOS
  123. class SkiaMetalBackendContext final : public SkiaBackendContext {
  124. AK_MAKE_NONCOPYABLE(SkiaMetalBackendContext);
  125. AK_MAKE_NONMOVABLE(SkiaMetalBackendContext);
  126. public:
  127. SkiaMetalBackendContext(sk_sp<GrDirectContext> context)
  128. : m_context(move(context))
  129. {
  130. }
  131. ~SkiaMetalBackendContext() override {};
  132. sk_sp<SkSurface> wrap_metal_texture(Core::MetalTexture& metal_texture)
  133. {
  134. GrMtlTextureInfo mtl_info;
  135. mtl_info.fTexture = sk_ret_cfp(metal_texture.texture());
  136. auto backend_render_target = GrBackendRenderTarget(metal_texture.width(), metal_texture.height(), mtl_info);
  137. return SkSurfaces::WrapBackendRenderTarget(m_context.get(), backend_render_target, kTopLeft_GrSurfaceOrigin, kBGRA_8888_SkColorType, nullptr, nullptr);
  138. }
  139. void flush_and_submit() override
  140. {
  141. m_context->flush();
  142. m_context->submit(GrSyncCpu::kYes);
  143. }
  144. private:
  145. sk_sp<GrDirectContext> m_context;
  146. };
  147. OwnPtr<SkiaBackendContext> DisplayListPlayerSkia::create_metal_context(Core::MetalContext const& metal_context)
  148. {
  149. GrMtlBackendContext backend_context;
  150. backend_context.fDevice.retain((GrMTLHandle)metal_context.device());
  151. backend_context.fQueue.retain((GrMTLHandle)metal_context.queue());
  152. sk_sp<GrDirectContext> ctx = GrDirectContexts::MakeMetal(backend_context);
  153. return make<SkiaMetalBackendContext>(ctx);
  154. }
  155. DisplayListPlayerSkia::DisplayListPlayerSkia(SkiaBackendContext& context, Core::MetalTexture& metal_texture)
  156. {
  157. auto image_info = SkImageInfo::Make(metal_texture.width(), metal_texture.height(), kBGRA_8888_SkColorType, kPremul_SkAlphaType);
  158. VERIFY(is<SkiaMetalBackendContext>(context));
  159. auto surface = static_cast<SkiaMetalBackendContext&>(context).wrap_metal_texture(metal_texture);
  160. if (!surface) {
  161. dbgln("Failed to create Skia surface from Metal texture");
  162. VERIFY_NOT_REACHED();
  163. }
  164. m_surface = make<SkiaSurface>(surface);
  165. m_flush_context = [&context] mutable {
  166. context.flush_and_submit();
  167. };
  168. }
  169. #endif
  170. DisplayListPlayerSkia::DisplayListPlayerSkia(Gfx::Bitmap& bitmap)
  171. {
  172. VERIFY(bitmap.format() == Gfx::BitmapFormat::BGRA8888);
  173. auto image_info = SkImageInfo::Make(bitmap.width(), bitmap.height(), kBGRA_8888_SkColorType, kPremul_SkAlphaType);
  174. auto surface = SkSurfaces::WrapPixels(image_info, bitmap.begin(), bitmap.pitch());
  175. VERIFY(surface);
  176. m_surface = make<SkiaSurface>(surface);
  177. }
  178. DisplayListPlayerSkia::~DisplayListPlayerSkia()
  179. {
  180. if (m_flush_context)
  181. m_flush_context();
  182. }
  183. static SkPoint to_skia_point(auto const& point)
  184. {
  185. return SkPoint::Make(point.x(), point.y());
  186. }
  187. static SkRect to_skia_rect(auto const& rect)
  188. {
  189. return SkRect::MakeXYWH(rect.x(), rect.y(), rect.width(), rect.height());
  190. }
  191. static SkColor to_skia_color(Gfx::Color const& color)
  192. {
  193. return SkColorSetARGB(color.alpha(), color.red(), color.green(), color.blue());
  194. }
  195. static SkColor4f to_skia_color4f(Gfx::Color const& color)
  196. {
  197. return {
  198. .fR = color.red() / 255.0f,
  199. .fG = color.green() / 255.0f,
  200. .fB = color.blue() / 255.0f,
  201. .fA = color.alpha() / 255.0f,
  202. };
  203. }
  204. static SkPath to_skia_path(Gfx::Path const& path)
  205. {
  206. return static_cast<Gfx::PathImplSkia const&>(path.impl()).sk_path();
  207. }
  208. static SkPathFillType to_skia_path_fill_type(Gfx::WindingRule winding_rule)
  209. {
  210. switch (winding_rule) {
  211. case Gfx::WindingRule::Nonzero:
  212. return SkPathFillType::kWinding;
  213. case Gfx::WindingRule::EvenOdd:
  214. return SkPathFillType::kEvenOdd;
  215. }
  216. VERIFY_NOT_REACHED();
  217. }
  218. static SkRRect to_skia_rrect(auto const& rect, CornerRadii const& corner_radii)
  219. {
  220. SkRRect rrect;
  221. SkVector radii[4];
  222. radii[0].set(corner_radii.top_left.horizontal_radius, corner_radii.top_left.vertical_radius);
  223. radii[1].set(corner_radii.top_right.horizontal_radius, corner_radii.top_right.vertical_radius);
  224. radii[2].set(corner_radii.bottom_right.horizontal_radius, corner_radii.bottom_right.vertical_radius);
  225. radii[3].set(corner_radii.bottom_left.horizontal_radius, corner_radii.bottom_left.vertical_radius);
  226. rrect.setRectRadii(to_skia_rect(rect), radii);
  227. return rrect;
  228. }
  229. static SkColorType to_skia_color_type(Gfx::BitmapFormat format)
  230. {
  231. switch (format) {
  232. case Gfx::BitmapFormat::Invalid:
  233. return kUnknown_SkColorType;
  234. case Gfx::BitmapFormat::BGRA8888:
  235. case Gfx::BitmapFormat::BGRx8888:
  236. return kBGRA_8888_SkColorType;
  237. case Gfx::BitmapFormat::RGBA8888:
  238. return kRGBA_8888_SkColorType;
  239. default:
  240. return kUnknown_SkColorType;
  241. }
  242. }
  243. static SkBitmap to_skia_bitmap(Gfx::Bitmap const& bitmap)
  244. {
  245. SkColorType color_type = to_skia_color_type(bitmap.format());
  246. SkAlphaType alpha_type = bitmap.alpha_type() == Gfx::AlphaType::Premultiplied ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
  247. SkImageInfo image_info = SkImageInfo::Make(bitmap.width(), bitmap.height(), color_type, alpha_type);
  248. SkBitmap sk_bitmap;
  249. sk_bitmap.setInfo(image_info);
  250. if (!sk_bitmap.installPixels(image_info, const_cast<Gfx::ARGB32*>(bitmap.begin()), bitmap.width() * 4)) {
  251. VERIFY_NOT_REACHED();
  252. }
  253. sk_bitmap.setImmutable();
  254. return sk_bitmap;
  255. }
  256. static SkMatrix to_skia_matrix(Gfx::AffineTransform const& affine_transform)
  257. {
  258. SkScalar affine[6];
  259. affine[0] = affine_transform.a();
  260. affine[1] = affine_transform.b();
  261. affine[2] = affine_transform.c();
  262. affine[3] = affine_transform.d();
  263. affine[4] = affine_transform.e();
  264. affine[5] = affine_transform.f();
  265. SkMatrix matrix;
  266. matrix.setAffine(affine);
  267. return matrix;
  268. }
  269. static SkSamplingOptions to_skia_sampling_options(Gfx::ScalingMode scaling_mode)
  270. {
  271. switch (scaling_mode) {
  272. case Gfx::ScalingMode::NearestNeighbor:
  273. case Gfx::ScalingMode::SmoothPixels:
  274. return SkSamplingOptions(SkFilterMode::kNearest);
  275. case Gfx::ScalingMode::BilinearBlend:
  276. return SkSamplingOptions(SkFilterMode::kLinear);
  277. case Gfx::ScalingMode::BoxSampling:
  278. return SkSamplingOptions(SkCubicResampler::Mitchell());
  279. default:
  280. VERIFY_NOT_REACHED();
  281. }
  282. }
  283. DisplayListPlayerSkia::SkiaSurface& DisplayListPlayerSkia::surface() const
  284. {
  285. return static_cast<SkiaSurface&>(*m_surface);
  286. }
  287. void DisplayListPlayerSkia::draw_glyph_run(DrawGlyphRun const& command)
  288. {
  289. auto const& gfx_font = static_cast<Gfx::ScaledFont const&>(command.glyph_run->font());
  290. auto sk_font = gfx_font.skia_font(command.scale);
  291. auto glyph_count = command.glyph_run->glyphs().size();
  292. Vector<SkGlyphID> glyphs;
  293. glyphs.ensure_capacity(glyph_count);
  294. Vector<SkPoint> positions;
  295. positions.ensure_capacity(glyph_count);
  296. auto font_ascent = gfx_font.pixel_metrics().ascent;
  297. for (auto const& glyph : command.glyph_run->glyphs()) {
  298. auto transformed_glyph = glyph;
  299. transformed_glyph.position.set_y(glyph.position.y() + font_ascent);
  300. transformed_glyph.position = transformed_glyph.position.scaled(command.scale);
  301. auto const& point = transformed_glyph.position;
  302. glyphs.append(transformed_glyph.glyph_id);
  303. positions.append(to_skia_point(point));
  304. }
  305. SkPaint paint;
  306. paint.setColor(to_skia_color(command.color));
  307. surface().canvas().drawGlyphs(glyphs.size(), glyphs.data(), positions.data(), to_skia_point(command.translation), sk_font, paint);
  308. }
  309. void DisplayListPlayerSkia::fill_rect(FillRect const& command)
  310. {
  311. auto const& rect = command.rect;
  312. auto& canvas = surface().canvas();
  313. SkPaint paint;
  314. paint.setColor(to_skia_color(command.color));
  315. canvas.drawRect(to_skia_rect(rect), paint);
  316. }
  317. void DisplayListPlayerSkia::draw_scaled_bitmap(DrawScaledBitmap const& command)
  318. {
  319. auto src_rect = to_skia_rect(command.src_rect);
  320. auto dst_rect = to_skia_rect(command.dst_rect);
  321. auto bitmap = to_skia_bitmap(command.bitmap);
  322. auto image = SkImages::RasterFromBitmap(bitmap);
  323. auto& canvas = surface().canvas();
  324. SkPaint paint;
  325. canvas.drawImageRect(image, src_rect, dst_rect, to_skia_sampling_options(command.scaling_mode), &paint, SkCanvas::kStrict_SrcRectConstraint);
  326. }
  327. void DisplayListPlayerSkia::draw_scaled_immutable_bitmap(DrawScaledImmutableBitmap const& command)
  328. {
  329. auto src_rect = to_skia_rect(command.src_rect);
  330. auto dst_rect = to_skia_rect(command.dst_rect);
  331. auto bitmap = to_skia_bitmap(command.bitmap->bitmap());
  332. auto image = SkImages::RasterFromBitmap(bitmap);
  333. auto& canvas = surface().canvas();
  334. SkPaint paint;
  335. canvas.drawImageRect(image, src_rect, dst_rect, to_skia_sampling_options(command.scaling_mode), &paint, SkCanvas::kStrict_SrcRectConstraint);
  336. }
  337. void DisplayListPlayerSkia::draw_repeated_immutable_bitmap(DrawRepeatedImmutableBitmap const& command)
  338. {
  339. auto bitmap = to_skia_bitmap(command.bitmap->bitmap());
  340. auto image = SkImages::RasterFromBitmap(bitmap);
  341. SkMatrix matrix;
  342. auto dst_rect = command.dst_rect.to_type<float>();
  343. auto src_size = command.bitmap->size().to_type<float>();
  344. matrix.setScale(dst_rect.width() / src_size.width(), dst_rect.height() / src_size.height());
  345. matrix.postTranslate(dst_rect.x(), dst_rect.y());
  346. auto sampling_options = to_skia_sampling_options(command.scaling_mode);
  347. auto tile_mode_x = command.repeat.x ? SkTileMode::kRepeat : SkTileMode::kDecal;
  348. auto tile_mode_y = command.repeat.y ? SkTileMode::kRepeat : SkTileMode::kDecal;
  349. auto shader = image->makeShader(tile_mode_x, tile_mode_y, sampling_options, matrix);
  350. SkPaint paint;
  351. paint.setShader(shader);
  352. auto& canvas = surface().canvas();
  353. canvas.drawPaint(paint);
  354. }
  355. void DisplayListPlayerSkia::add_clip_rect(AddClipRect const& command)
  356. {
  357. auto& canvas = surface().canvas();
  358. auto const& rect = command.rect;
  359. canvas.clipRect(to_skia_rect(rect));
  360. }
  361. void DisplayListPlayerSkia::save(Save const&)
  362. {
  363. auto& canvas = surface().canvas();
  364. canvas.save();
  365. }
  366. void DisplayListPlayerSkia::restore(Restore const&)
  367. {
  368. auto& canvas = surface().canvas();
  369. canvas.restore();
  370. }
  371. static SkBitmap alpha_mask_from_bitmap(Gfx::Bitmap const& bitmap, Gfx::Bitmap::MaskKind kind)
  372. {
  373. SkBitmap alpha_mask;
  374. alpha_mask.allocPixels(SkImageInfo::MakeA8(bitmap.width(), bitmap.height()));
  375. for (int y = 0; y < bitmap.height(); y++) {
  376. for (int x = 0; x < bitmap.width(); x++) {
  377. if (kind == Gfx::Bitmap::MaskKind::Luminance) {
  378. auto color = bitmap.get_pixel(x, y);
  379. *alpha_mask.getAddr8(x, y) = color.alpha() * color.luminosity() / 255;
  380. } else {
  381. VERIFY(kind == Gfx::Bitmap::MaskKind::Alpha);
  382. auto color = bitmap.get_pixel(x, y);
  383. *alpha_mask.getAddr8(x, y) = color.alpha();
  384. }
  385. }
  386. }
  387. return alpha_mask;
  388. }
  389. void DisplayListPlayerSkia::push_stacking_context(PushStackingContext const& command)
  390. {
  391. auto& canvas = surface().canvas();
  392. auto affine_transform = Gfx::extract_2d_affine_transform(command.transform.matrix);
  393. auto new_transform = Gfx::AffineTransform {}
  394. .set_translation(command.post_transform_translation.to_type<float>())
  395. .translate(command.transform.origin)
  396. .multiply(affine_transform)
  397. .translate(-command.transform.origin);
  398. auto matrix = to_skia_matrix(new_transform);
  399. if (command.opacity < 1) {
  400. auto source_paintable_rect = to_skia_rect(command.source_paintable_rect);
  401. SkRect dest;
  402. matrix.mapRect(&dest, source_paintable_rect);
  403. canvas.saveLayerAlphaf(&dest, command.opacity);
  404. } else {
  405. canvas.save();
  406. }
  407. if (command.clip_path.has_value()) {
  408. canvas.clipPath(to_skia_path(command.clip_path.value()), true);
  409. }
  410. if (command.mask.has_value()) {
  411. auto alpha_mask = alpha_mask_from_bitmap(*command.mask.value().mask_bitmap, command.mask.value().mask_kind);
  412. SkMatrix mask_matrix;
  413. auto mask_position = command.source_paintable_rect.location();
  414. mask_matrix.setTranslate(mask_position.x(), mask_position.y());
  415. auto shader = alpha_mask.makeShader(SkSamplingOptions(), mask_matrix);
  416. canvas.clipShader(shader);
  417. }
  418. if (command.is_fixed_position) {
  419. // FIXME: Resetting matrix is not correct when element is nested in a transformed stacking context
  420. canvas.resetMatrix();
  421. }
  422. canvas.concat(matrix);
  423. }
  424. void DisplayListPlayerSkia::pop_stacking_context(PopStackingContext const&)
  425. {
  426. surface().canvas().restore();
  427. }
  428. static ColorStopList replace_transition_hints_with_normal_color_stops(ColorStopList const& color_stop_list)
  429. {
  430. ColorStopList stops_with_replaced_transition_hints;
  431. auto const& first_color_stop = color_stop_list.first();
  432. // First color stop in the list should never have transition hint value
  433. VERIFY(!first_color_stop.transition_hint.has_value());
  434. stops_with_replaced_transition_hints.empend(first_color_stop.color, first_color_stop.position);
  435. // This loop replaces transition hints with five regular points, calculated using the
  436. // formula defined in the spec. After rendering using linear interpolation, this will
  437. // produce a result close enough to that obtained if the color of each point were calculated
  438. // using the non-linear formula from the spec.
  439. for (size_t i = 1; i < color_stop_list.size(); i++) {
  440. auto const& color_stop = color_stop_list[i];
  441. if (!color_stop.transition_hint.has_value()) {
  442. stops_with_replaced_transition_hints.empend(color_stop.color, color_stop.position);
  443. continue;
  444. }
  445. auto const& previous_color_stop = color_stop_list[i - 1];
  446. auto const& next_color_stop = color_stop_list[i];
  447. auto distance_between_stops = next_color_stop.position - previous_color_stop.position;
  448. auto transition_hint = color_stop.transition_hint.value();
  449. Array<float, 5> const transition_hint_relative_sampling_positions = {
  450. transition_hint * 0.33f,
  451. transition_hint * 0.66f,
  452. transition_hint,
  453. transition_hint + (1 - transition_hint) * 0.33f,
  454. transition_hint + (1 - transition_hint) * 0.66f,
  455. };
  456. for (auto const& transition_hint_relative_sampling_position : transition_hint_relative_sampling_positions) {
  457. auto position = previous_color_stop.position + transition_hint_relative_sampling_position * distance_between_stops;
  458. auto value = color_stop_step(previous_color_stop, next_color_stop, position);
  459. auto color = previous_color_stop.color.interpolate(next_color_stop.color, value);
  460. stops_with_replaced_transition_hints.empend(color, position);
  461. }
  462. stops_with_replaced_transition_hints.empend(color_stop.color, color_stop.position);
  463. }
  464. return stops_with_replaced_transition_hints;
  465. }
  466. static ColorStopList expand_repeat_length(ColorStopList const& color_stop_list, float repeat_length)
  467. {
  468. // https://drafts.csswg.org/css-images/#repeating-gradients
  469. // When rendered, however, the color-stops are repeated infinitely in both directions, with their
  470. // positions shifted by multiples of the difference between the last specified color-stop’s position
  471. // and the first specified color-stop’s position. For example, repeating-linear-gradient(red 10px, blue 50px)
  472. // is equivalent to linear-gradient(..., red -30px, blue 10px, red 10px, blue 50px, red 50px, blue 90px, ...).
  473. auto first_stop_position = color_stop_list.first().position;
  474. int const negative_repeat_count = AK::ceil(first_stop_position / repeat_length);
  475. int const positive_repeat_count = AK::ceil((1.0f - first_stop_position) / repeat_length);
  476. ColorStopList color_stop_list_with_expanded_repeat = color_stop_list;
  477. auto get_color_between_stops = [](float position, auto const& current_stop, auto const& previous_stop) {
  478. auto distance_between_stops = current_stop.position - previous_stop.position;
  479. auto percentage = (position - previous_stop.position) / distance_between_stops;
  480. return previous_stop.color.interpolate(current_stop.color, percentage);
  481. };
  482. for (auto repeat_count = 1; repeat_count <= negative_repeat_count; repeat_count++) {
  483. for (auto stop : color_stop_list.in_reverse()) {
  484. stop.position += repeat_length * static_cast<float>(-repeat_count);
  485. if (stop.position < 0) {
  486. stop.color = get_color_between_stops(0.0f, stop, color_stop_list_with_expanded_repeat.first());
  487. color_stop_list_with_expanded_repeat.prepend(stop);
  488. break;
  489. }
  490. color_stop_list_with_expanded_repeat.prepend(stop);
  491. }
  492. }
  493. for (auto repeat_count = 0; repeat_count < positive_repeat_count; repeat_count++) {
  494. for (auto stop : color_stop_list) {
  495. stop.position += repeat_length * static_cast<float>(repeat_count);
  496. if (stop.position > 1) {
  497. stop.color = get_color_between_stops(1.0f, stop, color_stop_list_with_expanded_repeat.last());
  498. color_stop_list_with_expanded_repeat.append(stop);
  499. break;
  500. }
  501. color_stop_list_with_expanded_repeat.append(stop);
  502. }
  503. }
  504. return color_stop_list_with_expanded_repeat;
  505. }
  506. void DisplayListPlayerSkia::paint_linear_gradient(PaintLinearGradient const& command)
  507. {
  508. auto const& linear_gradient_data = command.linear_gradient_data;
  509. auto color_stop_list = linear_gradient_data.color_stops.list;
  510. auto const& repeat_length = linear_gradient_data.color_stops.repeat_length;
  511. VERIFY(!color_stop_list.is_empty());
  512. if (repeat_length.has_value())
  513. color_stop_list = expand_repeat_length(color_stop_list, *repeat_length);
  514. auto stops_with_replaced_transition_hints = replace_transition_hints_with_normal_color_stops(color_stop_list);
  515. Vector<SkColor4f> colors;
  516. Vector<SkScalar> positions;
  517. for (size_t stop_index = 0; stop_index < stops_with_replaced_transition_hints.size(); stop_index++) {
  518. auto const& stop = stops_with_replaced_transition_hints[stop_index];
  519. if (stop_index > 0 && stop == stops_with_replaced_transition_hints[stop_index - 1])
  520. continue;
  521. colors.append(to_skia_color4f(stop.color));
  522. positions.append(stop.position);
  523. }
  524. auto const& rect = command.gradient_rect;
  525. auto length = calculate_gradient_length<int>(rect.size(), linear_gradient_data.gradient_angle);
  526. auto bottom = rect.center().translated(0, -length / 2);
  527. auto top = rect.center().translated(0, length / 2);
  528. Array<SkPoint, 2> points;
  529. points[0] = to_skia_point(top);
  530. points[1] = to_skia_point(bottom);
  531. auto center = to_skia_rect(rect).center();
  532. SkMatrix matrix;
  533. matrix.setRotate(linear_gradient_data.gradient_angle, center.x(), center.y());
  534. auto color_space = SkColorSpace::MakeSRGB();
  535. SkGradientShader::Interpolation interpolation = {};
  536. interpolation.fColorSpace = SkGradientShader::Interpolation::ColorSpace::kSRGB;
  537. interpolation.fInPremul = SkGradientShader::Interpolation::InPremul::kYes;
  538. auto shader = SkGradientShader::MakeLinear(points.data(), colors.data(), color_space, positions.data(), positions.size(), SkTileMode::kClamp, interpolation, &matrix);
  539. SkPaint paint;
  540. paint.setShader(shader);
  541. surface().canvas().drawRect(to_skia_rect(rect), paint);
  542. }
  543. static void add_spread_distance_to_border_radius(int& border_radius, int spread_distance)
  544. {
  545. if (border_radius == 0 || spread_distance == 0)
  546. return;
  547. // https://drafts.csswg.org/css-backgrounds/#shadow-shape
  548. // To preserve the box’s shape when spread is applied, the corner radii of the shadow are also increased (decreased,
  549. // for inner shadows) from the border-box (padding-box) radii by adding (subtracting) the spread distance (and flooring
  550. // at zero). However, in order to create a sharper corner when the border radius is small (and thus ensure continuity
  551. // between round and sharp corners), when the border radius is less than the spread distance (or in the case of an inner
  552. // shadow, less than the absolute value of a negative spread distance), the spread distance is first multiplied by the
  553. // proportion 1 + (r-1)^3, where r is the ratio of the border radius to the spread distance, in calculating the corner
  554. // radii of the spread shadow shape.
  555. if (border_radius > AK::abs(spread_distance)) {
  556. border_radius += spread_distance;
  557. } else {
  558. auto r = (float)border_radius / AK::abs(spread_distance);
  559. border_radius += spread_distance * (1 + AK::pow(r - 1, 3.0f));
  560. }
  561. }
  562. void DisplayListPlayerSkia::paint_outer_box_shadow(PaintOuterBoxShadow const& command)
  563. {
  564. auto const& outer_box_shadow_params = command.box_shadow_params;
  565. auto const& color = outer_box_shadow_params.color;
  566. auto const& spread_distance = outer_box_shadow_params.spread_distance;
  567. auto const& blur_radius = outer_box_shadow_params.blur_radius;
  568. auto content_rrect = to_skia_rrect(outer_box_shadow_params.device_content_rect, outer_box_shadow_params.corner_radii);
  569. auto shadow_rect = outer_box_shadow_params.device_content_rect;
  570. shadow_rect.inflate(spread_distance, spread_distance, spread_distance, spread_distance);
  571. auto offset_x = outer_box_shadow_params.offset_x;
  572. auto offset_y = outer_box_shadow_params.offset_y;
  573. shadow_rect.translate_by(offset_x, offset_y);
  574. auto add_spread_distance_to_corner_radius = [&](auto& corner_radius) {
  575. add_spread_distance_to_border_radius(corner_radius.horizontal_radius, spread_distance);
  576. add_spread_distance_to_border_radius(corner_radius.vertical_radius, spread_distance);
  577. };
  578. auto corner_radii = outer_box_shadow_params.corner_radii;
  579. add_spread_distance_to_corner_radius(corner_radii.top_left);
  580. add_spread_distance_to_corner_radius(corner_radii.top_right);
  581. add_spread_distance_to_corner_radius(corner_radii.bottom_right);
  582. add_spread_distance_to_corner_radius(corner_radii.bottom_left);
  583. auto& canvas = surface().canvas();
  584. canvas.save();
  585. canvas.clipRRect(content_rrect, SkClipOp::kDifference, true);
  586. SkPaint paint;
  587. paint.setAntiAlias(true);
  588. paint.setColor(to_skia_color(color));
  589. paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, blur_radius / 2));
  590. auto shadow_rounded_rect = to_skia_rrect(shadow_rect, corner_radii);
  591. canvas.drawRRect(shadow_rounded_rect, paint);
  592. canvas.restore();
  593. }
  594. void DisplayListPlayerSkia::paint_inner_box_shadow(PaintInnerBoxShadow const& command)
  595. {
  596. auto const& outer_box_shadow_params = command.box_shadow_params;
  597. auto color = outer_box_shadow_params.color;
  598. auto device_content_rect = outer_box_shadow_params.device_content_rect;
  599. auto offset_x = outer_box_shadow_params.offset_x;
  600. auto offset_y = outer_box_shadow_params.offset_y;
  601. auto blur_radius = outer_box_shadow_params.blur_radius;
  602. auto spread_distance = outer_box_shadow_params.spread_distance;
  603. auto const& corner_radii = outer_box_shadow_params.corner_radii;
  604. auto outer_shadow_rect = device_content_rect.translated({ offset_x, offset_y });
  605. auto inner_shadow_rect = outer_shadow_rect.inflated(-spread_distance, -spread_distance, -spread_distance, -spread_distance);
  606. outer_shadow_rect.inflate(
  607. blur_radius + offset_y,
  608. blur_radius + abs(offset_x),
  609. blur_radius + abs(offset_y),
  610. blur_radius + offset_x);
  611. auto inner_rect_corner_radii = corner_radii;
  612. auto add_spread_distance_to_corner_radius = [&](auto& corner_radius) {
  613. add_spread_distance_to_border_radius(corner_radius.horizontal_radius, -spread_distance);
  614. add_spread_distance_to_border_radius(corner_radius.vertical_radius, -spread_distance);
  615. };
  616. add_spread_distance_to_corner_radius(inner_rect_corner_radii.top_left);
  617. add_spread_distance_to_corner_radius(inner_rect_corner_radii.top_right);
  618. add_spread_distance_to_corner_radius(inner_rect_corner_radii.bottom_right);
  619. add_spread_distance_to_corner_radius(inner_rect_corner_radii.bottom_left);
  620. auto outer_rect = to_skia_rrect(outer_shadow_rect, corner_radii);
  621. auto inner_rect = to_skia_rrect(inner_shadow_rect, inner_rect_corner_radii);
  622. SkPath outer_path;
  623. outer_path.addRRect(outer_rect);
  624. SkPath inner_path;
  625. inner_path.addRRect(inner_rect);
  626. SkPath result_path;
  627. if (!Op(outer_path, inner_path, SkPathOp::kDifference_SkPathOp, &result_path)) {
  628. VERIFY_NOT_REACHED();
  629. }
  630. auto& canvas = surface().canvas();
  631. SkPaint path_paint;
  632. path_paint.setAntiAlias(true);
  633. path_paint.setColor(to_skia_color(color));
  634. path_paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, blur_radius / 2));
  635. canvas.save();
  636. canvas.clipRRect(to_skia_rrect(device_content_rect, corner_radii), true);
  637. canvas.drawPath(result_path, path_paint);
  638. canvas.restore();
  639. }
  640. void DisplayListPlayerSkia::paint_text_shadow(PaintTextShadow const& command)
  641. {
  642. auto& canvas = surface().canvas();
  643. auto blur_image_filter = SkImageFilters::Blur(command.blur_radius / 2, command.blur_radius / 2, nullptr);
  644. SkPaint blur_paint;
  645. blur_paint.setImageFilter(blur_image_filter);
  646. canvas.saveLayer(SkCanvas::SaveLayerRec(nullptr, &blur_paint, nullptr, 0));
  647. draw_glyph_run({
  648. .glyph_run = command.glyph_run,
  649. .color = command.color,
  650. .rect = command.text_rect,
  651. .translation = command.draw_location.to_type<float>() + command.text_rect.location().to_type<float>(),
  652. .scale = command.glyph_run_scale,
  653. });
  654. canvas.restore();
  655. }
  656. void DisplayListPlayerSkia::fill_rect_with_rounded_corners(FillRectWithRoundedCorners const& command)
  657. {
  658. auto const& rect = command.rect;
  659. auto& canvas = surface().canvas();
  660. SkPaint paint;
  661. paint.setColor(to_skia_color(command.color));
  662. paint.setAntiAlias(true);
  663. auto rounded_rect = to_skia_rrect(rect, command.corner_radii);
  664. canvas.drawRRect(rounded_rect, paint);
  665. }
  666. void DisplayListPlayerSkia::fill_path_using_color(FillPathUsingColor const& command)
  667. {
  668. auto& canvas = surface().canvas();
  669. SkPaint paint;
  670. paint.setAntiAlias(true);
  671. paint.setColor(to_skia_color(command.color));
  672. auto path = to_skia_path(command.path);
  673. path.setFillType(to_skia_path_fill_type(command.winding_rule));
  674. path.offset(command.aa_translation.x(), command.aa_translation.y());
  675. canvas.drawPath(path, paint);
  676. }
  677. static SkTileMode to_skia_tile_mode(SVGLinearGradientPaintStyle::SpreadMethod spread_method)
  678. {
  679. switch (spread_method) {
  680. case SVGLinearGradientPaintStyle::SpreadMethod::Pad:
  681. return SkTileMode::kClamp;
  682. case SVGLinearGradientPaintStyle::SpreadMethod::Reflect:
  683. return SkTileMode::kMirror;
  684. case SVGLinearGradientPaintStyle::SpreadMethod::Repeat:
  685. return SkTileMode::kRepeat;
  686. default:
  687. VERIFY_NOT_REACHED();
  688. }
  689. }
  690. static SkPaint paint_style_to_skia_paint(Painting::SVGGradientPaintStyle const& paint_style, Gfx::FloatRect bounding_rect)
  691. {
  692. SkPaint paint;
  693. auto const& color_stops = paint_style.color_stops();
  694. Vector<SkColor> colors;
  695. colors.ensure_capacity(color_stops.size());
  696. Vector<SkScalar> positions;
  697. positions.ensure_capacity(color_stops.size());
  698. for (auto const& color_stop : color_stops) {
  699. colors.append(to_skia_color(color_stop.color));
  700. positions.append(color_stop.position);
  701. }
  702. if (is<SVGLinearGradientPaintStyle>(paint_style)) {
  703. auto const& linear_gradient_paint_style = static_cast<SVGLinearGradientPaintStyle const&>(paint_style);
  704. SkMatrix matrix;
  705. auto scale = linear_gradient_paint_style.scale();
  706. auto start_point = linear_gradient_paint_style.start_point().scaled(scale);
  707. auto end_point = linear_gradient_paint_style.end_point().scaled(scale);
  708. start_point.translate_by(bounding_rect.location());
  709. end_point.translate_by(bounding_rect.location());
  710. Array<SkPoint, 2> points;
  711. points[0] = to_skia_point(start_point);
  712. points[1] = to_skia_point(end_point);
  713. auto shader = SkGradientShader::MakeLinear(points.data(), colors.data(), positions.data(), color_stops.size(), to_skia_tile_mode(paint_style.spread_method()), 0, &matrix);
  714. paint.setShader(shader);
  715. } else if (is<SVGRadialGradientPaintStyle>(paint_style)) {
  716. auto const& radial_gradient_paint_style = static_cast<SVGRadialGradientPaintStyle const&>(paint_style);
  717. SkMatrix matrix;
  718. auto scale = radial_gradient_paint_style.scale();
  719. auto start_center = radial_gradient_paint_style.start_center().scaled(scale);
  720. auto end_center = radial_gradient_paint_style.end_center().scaled(scale);
  721. auto start_radius = radial_gradient_paint_style.start_radius() * scale;
  722. auto end_radius = radial_gradient_paint_style.end_radius() * scale;
  723. start_center.translate_by(bounding_rect.location());
  724. end_center.translate_by(bounding_rect.location());
  725. auto start_sk_point = to_skia_point(start_center);
  726. auto end_sk_point = to_skia_point(end_center);
  727. auto shader = SkGradientShader::MakeTwoPointConical(start_sk_point, start_radius, end_sk_point, end_radius, colors.data(), positions.data(), color_stops.size(), to_skia_tile_mode(paint_style.spread_method()), 0, &matrix);
  728. paint.setShader(shader);
  729. }
  730. return paint;
  731. }
  732. void DisplayListPlayerSkia::fill_path_using_paint_style(FillPathUsingPaintStyle const& command)
  733. {
  734. auto path = to_skia_path(command.path);
  735. path.offset(command.aa_translation.x(), command.aa_translation.y());
  736. path.setFillType(to_skia_path_fill_type(command.winding_rule));
  737. auto paint = paint_style_to_skia_paint(*command.paint_style, command.bounding_rect().to_type<float>());
  738. paint.setAntiAlias(true);
  739. paint.setAlphaf(command.opacity);
  740. surface().canvas().drawPath(path, paint);
  741. }
  742. void DisplayListPlayerSkia::stroke_path_using_color(StrokePathUsingColor const& command)
  743. {
  744. // Skia treats zero thickness as a special case and will draw a hairline, while we want to draw nothing.
  745. if (!command.thickness)
  746. return;
  747. auto& canvas = surface().canvas();
  748. SkPaint paint;
  749. paint.setAntiAlias(true);
  750. paint.setStyle(SkPaint::kStroke_Style);
  751. paint.setStrokeWidth(command.thickness);
  752. paint.setColor(to_skia_color(command.color));
  753. auto path = to_skia_path(command.path);
  754. path.offset(command.aa_translation.x(), command.aa_translation.y());
  755. canvas.drawPath(path, paint);
  756. }
  757. void DisplayListPlayerSkia::stroke_path_using_paint_style(StrokePathUsingPaintStyle const& command)
  758. {
  759. // Skia treats zero thickness as a special case and will draw a hairline, while we want to draw nothing.
  760. if (!command.thickness)
  761. return;
  762. auto path = to_skia_path(command.path);
  763. path.offset(command.aa_translation.x(), command.aa_translation.y());
  764. auto paint = paint_style_to_skia_paint(*command.paint_style, command.bounding_rect().to_type<float>());
  765. paint.setAntiAlias(true);
  766. paint.setAlphaf(command.opacity);
  767. paint.setStyle(SkPaint::Style::kStroke_Style);
  768. paint.setStrokeWidth(command.thickness);
  769. surface().canvas().drawPath(path, paint);
  770. }
  771. void DisplayListPlayerSkia::draw_ellipse(DrawEllipse const& command)
  772. {
  773. // Skia treats zero thickness as a special case and will draw a hairline, while we want to draw nothing.
  774. if (!command.thickness)
  775. return;
  776. auto const& rect = command.rect;
  777. auto& canvas = surface().canvas();
  778. SkPaint paint;
  779. paint.setAntiAlias(true);
  780. paint.setStyle(SkPaint::kStroke_Style);
  781. paint.setStrokeWidth(command.thickness);
  782. paint.setColor(to_skia_color(command.color));
  783. canvas.drawOval(to_skia_rect(rect), paint);
  784. }
  785. void DisplayListPlayerSkia::fill_ellipse(FillEllipse const& command)
  786. {
  787. auto const& rect = command.rect;
  788. auto& canvas = surface().canvas();
  789. SkPaint paint;
  790. paint.setAntiAlias(true);
  791. paint.setColor(to_skia_color(command.color));
  792. canvas.drawOval(to_skia_rect(rect), paint);
  793. }
  794. void DisplayListPlayerSkia::draw_line(DrawLine const& command)
  795. {
  796. // Skia treats zero thickness as a special case and will draw a hairline, while we want to draw nothing.
  797. if (!command.thickness)
  798. return;
  799. auto from = to_skia_point(command.from);
  800. auto to = to_skia_point(command.to);
  801. auto& canvas = surface().canvas();
  802. SkPaint paint;
  803. paint.setAntiAlias(true);
  804. paint.setStrokeWidth(command.thickness);
  805. paint.setColor(to_skia_color(command.color));
  806. switch (command.style) {
  807. case Gfx::LineStyle::Solid:
  808. break;
  809. case Gfx::LineStyle::Dotted: {
  810. auto length = command.to.distance_from(command.from);
  811. auto dot_count = floor(length / (static_cast<float>(command.thickness) * 2));
  812. auto interval = length / dot_count;
  813. SkScalar intervals[] = { 0, interval };
  814. paint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 0));
  815. paint.setStrokeCap(SkPaint::Cap::kRound_Cap);
  816. // NOTE: As Skia doesn't render a dot exactly at the end of a line, we need
  817. // to extend it by less then an interval.
  818. auto direction = to - from;
  819. direction.normalize();
  820. to += direction * (interval / 2.0f);
  821. break;
  822. }
  823. case Gfx::LineStyle::Dashed: {
  824. auto length = command.to.distance_from(command.from) + command.thickness;
  825. auto dash_count = floor(length / static_cast<float>(command.thickness) / 4) * 2 + 1;
  826. auto interval = length / dash_count;
  827. SkScalar intervals[] = { interval, interval };
  828. paint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 0));
  829. auto direction = to - from;
  830. direction.normalize();
  831. from -= direction * (command.thickness / 2.0f);
  832. to += direction * (command.thickness / 2.0f);
  833. break;
  834. }
  835. }
  836. canvas.drawLine(from, to, paint);
  837. }
  838. void DisplayListPlayerSkia::apply_backdrop_filter(ApplyBackdropFilter const& command)
  839. {
  840. auto& canvas = surface().canvas();
  841. auto rect = to_skia_rect(command.backdrop_region);
  842. canvas.save();
  843. canvas.clipRect(rect);
  844. ScopeGuard guard = [&] { canvas.restore(); };
  845. for (auto const& filter_function : command.backdrop_filter.filters) {
  846. // See: https://drafts.fxtf.org/filter-effects-1/#supported-filter-functions
  847. filter_function.visit(
  848. [&](CSS::ResolvedBackdropFilter::Blur const& blur_filter) {
  849. auto blur_image_filter = SkImageFilters::Blur(blur_filter.radius, blur_filter.radius, nullptr);
  850. canvas.saveLayer(SkCanvas::SaveLayerRec(nullptr, nullptr, blur_image_filter.get(), 0));
  851. canvas.restore();
  852. },
  853. [&](CSS::ResolvedBackdropFilter::ColorOperation const& color) {
  854. auto amount = clamp(color.amount, 0.0f, 1.0f);
  855. // Matrices are taken from https://drafts.fxtf.org/filter-effects-1/#FilterPrimitiveRepresentation
  856. sk_sp<SkColorFilter> color_filter;
  857. switch (color.operation) {
  858. case CSS::Filter::Color::Operation::Grayscale: {
  859. float matrix[20] = {
  860. 0.2126f + 0.7874f * (1 - amount), 0.7152f - 0.7152f * (1 - amount), 0.0722f - 0.0722f * (1 - amount), 0, 0,
  861. 0.2126f - 0.2126f * (1 - amount), 0.7152f + 0.2848f * (1 - amount), 0.0722f - 0.0722f * (1 - amount), 0, 0,
  862. 0.2126f - 0.2126f * (1 - amount), 0.7152f - 0.7152f * (1 - amount), 0.0722f + 0.9278f * (1 - amount), 0, 0,
  863. 0, 0, 0, 1, 0
  864. };
  865. color_filter = SkColorFilters::Matrix(matrix);
  866. break;
  867. }
  868. case CSS::Filter::Color::Operation::Brightness: {
  869. float matrix[20] = {
  870. amount, 0, 0, 0, 0,
  871. 0, amount, 0, 0, 0,
  872. 0, 0, amount, 0, 0,
  873. 0, 0, 0, 1, 0
  874. };
  875. color_filter = SkColorFilters::Matrix(matrix);
  876. break;
  877. }
  878. case CSS::Filter::Color::Operation::Contrast: {
  879. float intercept = -(0.5f * amount) + 0.5f;
  880. float matrix[20] = {
  881. amount, 0, 0, 0, intercept,
  882. 0, amount, 0, 0, intercept,
  883. 0, 0, amount, 0, intercept,
  884. 0, 0, 0, 1, 0
  885. };
  886. color_filter = SkColorFilters::Matrix(matrix);
  887. break;
  888. }
  889. case CSS::Filter::Color::Operation::Invert: {
  890. float matrix[20] = {
  891. 1 - 2 * amount, 0, 0, 0, amount,
  892. 0, 1 - 2 * amount, 0, 0, amount,
  893. 0, 0, 1 - 2 * amount, 0, amount,
  894. 0, 0, 0, 1, 0
  895. };
  896. color_filter = SkColorFilters::Matrix(matrix);
  897. break;
  898. }
  899. case CSS::Filter::Color::Operation::Opacity: {
  900. float matrix[20] = {
  901. 1, 0, 0, 0, 0,
  902. 0, 1, 0, 0, 0,
  903. 0, 0, 1, 0, 0,
  904. 0, 0, 0, amount, 0
  905. };
  906. color_filter = SkColorFilters::Matrix(matrix);
  907. break;
  908. }
  909. case CSS::Filter::Color::Operation::Sepia: {
  910. float matrix[20] = {
  911. 0.393f + 0.607f * (1 - amount), 0.769f - 0.769f * (1 - amount), 0.189f - 0.189f * (1 - amount), 0, 0,
  912. 0.349f - 0.349f * (1 - amount), 0.686f + 0.314f * (1 - amount), 0.168f - 0.168f * (1 - amount), 0, 0,
  913. 0.272f - 0.272f * (1 - amount), 0.534f - 0.534f * (1 - amount), 0.131f + 0.869f * (1 - amount), 0, 0,
  914. 0, 0, 0, 1, 0
  915. };
  916. color_filter = SkColorFilters::Matrix(matrix);
  917. break;
  918. }
  919. case CSS::Filter::Color::Operation::Saturate: {
  920. float matrix[20] = {
  921. 0.213f + 0.787f * amount, 0.715f - 0.715f * amount, 0.072f - 0.072f * amount, 0, 0,
  922. 0.213f - 0.213f * amount, 0.715f + 0.285f * amount, 0.072f - 0.072f * amount, 0, 0,
  923. 0.213f - 0.213f * amount, 0.715f - 0.715f * amount, 0.072f + 0.928f * amount, 0, 0,
  924. 0, 0, 0, 1, 0
  925. };
  926. color_filter = SkColorFilters::Matrix(matrix);
  927. break;
  928. }
  929. default:
  930. VERIFY_NOT_REACHED();
  931. }
  932. auto image_filter = SkImageFilters::ColorFilter(color_filter, nullptr);
  933. canvas.saveLayer(SkCanvas::SaveLayerRec(nullptr, nullptr, image_filter.get(), 0));
  934. canvas.restore();
  935. },
  936. [&](CSS::ResolvedBackdropFilter::HueRotate const& hue_rotate) {
  937. float radians = AK::to_radians(hue_rotate.angle_degrees);
  938. auto cosA = cos(radians);
  939. auto sinA = sin(radians);
  940. auto a00 = 0.213f + cosA * 0.787f - sinA * 0.213f;
  941. auto a01 = 0.715f - cosA * 0.715f - sinA * 0.715f;
  942. auto a02 = 0.072f - cosA * 0.072f + sinA * 0.928f;
  943. auto a10 = 0.213f - cosA * 0.213f + sinA * 0.143f;
  944. auto a11 = 0.715f + cosA * 0.285f + sinA * 0.140f;
  945. auto a12 = 0.072f - cosA * 0.072f - sinA * 0.283f;
  946. auto a20 = 0.213f - cosA * 0.213f - sinA * 0.787f;
  947. auto a21 = 0.715f - cosA * 0.715f + sinA * 0.715f;
  948. auto a22 = 0.072f + cosA * 0.928f + sinA * 0.072f;
  949. float matrix[20] = {
  950. a00, a01, a02, 0, 0,
  951. a10, a11, a12, 0, 0,
  952. a20, a21, a22, 0, 0,
  953. 0, 0, 0, 1, 0
  954. };
  955. auto color_filter = SkColorFilters::Matrix(matrix);
  956. auto image_filter = SkImageFilters::ColorFilter(color_filter, nullptr);
  957. canvas.saveLayer(SkCanvas::SaveLayerRec(nullptr, nullptr, image_filter.get(), 0));
  958. canvas.restore();
  959. },
  960. [&](CSS::ResolvedBackdropFilter::DropShadow const&) {
  961. dbgln("TODO: Implement drop-shadow() filter function!");
  962. });
  963. }
  964. }
  965. void DisplayListPlayerSkia::draw_rect(DrawRect const& command)
  966. {
  967. auto const& rect = command.rect;
  968. auto& canvas = surface().canvas();
  969. SkPaint paint;
  970. paint.setAntiAlias(true);
  971. paint.setStyle(SkPaint::kStroke_Style);
  972. paint.setStrokeWidth(1);
  973. paint.setColor(to_skia_color(command.color));
  974. canvas.drawRect(to_skia_rect(rect), paint);
  975. }
  976. void DisplayListPlayerSkia::paint_radial_gradient(PaintRadialGradient const& command)
  977. {
  978. auto const& radial_gradient_data = command.radial_gradient_data;
  979. auto color_stop_list = radial_gradient_data.color_stops.list;
  980. VERIFY(!color_stop_list.is_empty());
  981. auto const& repeat_length = radial_gradient_data.color_stops.repeat_length;
  982. if (repeat_length.has_value())
  983. color_stop_list = expand_repeat_length(color_stop_list, repeat_length.value());
  984. auto stops_with_replaced_transition_hints = replace_transition_hints_with_normal_color_stops(color_stop_list);
  985. Vector<SkColor4f> colors;
  986. Vector<SkScalar> positions;
  987. for (size_t stop_index = 0; stop_index < stops_with_replaced_transition_hints.size(); stop_index++) {
  988. auto const& stop = stops_with_replaced_transition_hints[stop_index];
  989. if (stop_index > 0 && stop == stops_with_replaced_transition_hints[stop_index - 1])
  990. continue;
  991. colors.append(to_skia_color4f(stop.color));
  992. positions.append(stop.position);
  993. }
  994. auto const& rect = command.rect;
  995. auto center = to_skia_point(command.center.translated(command.rect.location()));
  996. auto const size = command.size.to_type<float>();
  997. SkMatrix matrix;
  998. // Skia does not support specifying of horizontal and vertical radius's separately,
  999. // so instead we apply scale matrix
  1000. matrix.setScale(size.width() / size.height(), 1.0f, center.x(), center.y());
  1001. SkTileMode tile_mode = SkTileMode::kClamp;
  1002. if (repeat_length.has_value())
  1003. tile_mode = SkTileMode::kRepeat;
  1004. auto color_space = SkColorSpace::MakeSRGB();
  1005. SkGradientShader::Interpolation interpolation = {};
  1006. interpolation.fColorSpace = SkGradientShader::Interpolation::ColorSpace::kSRGB;
  1007. interpolation.fInPremul = SkGradientShader::Interpolation::InPremul::kYes;
  1008. auto shader = SkGradientShader::MakeRadial(center, size.height(), colors.data(), color_space, positions.data(), positions.size(), tile_mode, interpolation, &matrix);
  1009. SkPaint paint;
  1010. paint.setAntiAlias(true);
  1011. paint.setShader(shader);
  1012. surface().canvas().drawRect(to_skia_rect(rect), paint);
  1013. }
  1014. void DisplayListPlayerSkia::paint_conic_gradient(PaintConicGradient const& command)
  1015. {
  1016. auto const& conic_gradient_data = command.conic_gradient_data;
  1017. auto color_stop_list = conic_gradient_data.color_stops.list;
  1018. auto const& repeat_length = conic_gradient_data.color_stops.repeat_length;
  1019. if (repeat_length.has_value())
  1020. color_stop_list = expand_repeat_length(color_stop_list, repeat_length.value());
  1021. VERIFY(!color_stop_list.is_empty());
  1022. auto stops_with_replaced_transition_hints = replace_transition_hints_with_normal_color_stops(color_stop_list);
  1023. Vector<SkColor4f> colors;
  1024. Vector<SkScalar> positions;
  1025. for (size_t stop_index = 0; stop_index < stops_with_replaced_transition_hints.size(); stop_index++) {
  1026. auto const& stop = stops_with_replaced_transition_hints[stop_index];
  1027. if (stop_index > 0 && stop == stops_with_replaced_transition_hints[stop_index - 1])
  1028. continue;
  1029. colors.append(to_skia_color4f(stop.color));
  1030. positions.append(stop.position);
  1031. }
  1032. auto const& rect = command.rect;
  1033. auto center = command.position.translated(rect.location()).to_type<float>();
  1034. SkMatrix matrix;
  1035. matrix.setRotate(-90 + conic_gradient_data.start_angle, center.x(), center.y());
  1036. auto color_space = SkColorSpace::MakeSRGB();
  1037. SkGradientShader::Interpolation interpolation = {};
  1038. interpolation.fColorSpace = SkGradientShader::Interpolation::ColorSpace::kSRGB;
  1039. interpolation.fInPremul = SkGradientShader::Interpolation::InPremul::kYes;
  1040. auto shader = SkGradientShader::MakeSweep(center.x(), center.y(), colors.data(), color_space, positions.data(), positions.size(), SkTileMode::kRepeat, 0, 360, interpolation, &matrix);
  1041. SkPaint paint;
  1042. paint.setAntiAlias(true);
  1043. paint.setShader(shader);
  1044. surface().canvas().drawRect(to_skia_rect(rect), paint);
  1045. }
  1046. void DisplayListPlayerSkia::draw_triangle_wave(DrawTriangleWave const&)
  1047. {
  1048. }
  1049. void DisplayListPlayerSkia::add_rounded_rect_clip(AddRoundedRectClip const& command)
  1050. {
  1051. auto rounded_rect = to_skia_rrect(command.border_rect, command.corner_radii);
  1052. auto& canvas = surface().canvas();
  1053. auto clip_op = command.corner_clip == CornerClip::Inside ? SkClipOp::kDifference : SkClipOp::kIntersect;
  1054. canvas.clipRRect(rounded_rect, clip_op, true);
  1055. }
  1056. void DisplayListPlayerSkia::add_mask(AddMask const& command)
  1057. {
  1058. auto const& rect = command.rect;
  1059. if (rect.is_empty())
  1060. return;
  1061. auto mask_surface = m_surface->make_surface(rect.width(), rect.height());
  1062. auto previous_surface = move(m_surface);
  1063. m_surface = make<SkiaSurface>(mask_surface);
  1064. execute(*command.display_list);
  1065. m_surface = move(previous_surface);
  1066. SkMatrix mask_matrix;
  1067. mask_matrix.setTranslate(rect.x(), rect.y());
  1068. auto image = mask_surface->makeImageSnapshot();
  1069. auto shader = image->makeShader(SkSamplingOptions(), mask_matrix);
  1070. surface().canvas().save();
  1071. surface().canvas().clipShader(shader);
  1072. }
  1073. void DisplayListPlayerSkia::paint_nested_display_list(PaintNestedDisplayList const& command)
  1074. {
  1075. auto& canvas = surface().canvas();
  1076. canvas.translate(command.rect.x(), command.rect.y());
  1077. execute(*command.display_list);
  1078. }
  1079. void DisplayListPlayerSkia::paint_scrollbar(PaintScrollBar const& command)
  1080. {
  1081. auto rect = to_skia_rect(command.rect);
  1082. auto radius = rect.width() / 2;
  1083. auto rrect = SkRRect::MakeRectXY(rect, radius, radius);
  1084. auto& canvas = surface().canvas();
  1085. auto fill_color = Color(Color::NamedColor::DarkGray).with_alpha(128);
  1086. SkPaint fill_paint;
  1087. fill_paint.setColor(to_skia_color(fill_color));
  1088. canvas.drawRRect(rrect, fill_paint);
  1089. auto stroke_color = Color(Color::NamedColor::LightGray).with_alpha(128);
  1090. SkPaint stroke_paint;
  1091. stroke_paint.setStroke(true);
  1092. stroke_paint.setStrokeWidth(1);
  1093. stroke_paint.setColor(to_skia_color(stroke_color));
  1094. canvas.drawRRect(rrect, stroke_paint);
  1095. }
  1096. bool DisplayListPlayerSkia::would_be_fully_clipped_by_painter(Gfx::IntRect rect) const
  1097. {
  1098. return surface().canvas().quickReject(to_skia_rect(rect));
  1099. }
  1100. }