PainterSkia.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #define AK_DONT_REPLACE_STD
  7. #include <AK/OwnPtr.h>
  8. #include <LibGfx/PainterSkia.h>
  9. #include <LibGfx/PathSkia.h>
  10. #include <AK/TypeCasts.h>
  11. #include <core/SkBitmap.h>
  12. #include <core/SkBlurTypes.h>
  13. #include <core/SkCanvas.h>
  14. #include <core/SkColorFilter.h>
  15. #include <core/SkMaskFilter.h>
  16. #include <core/SkPath.h>
  17. #include <core/SkPathBuilder.h>
  18. #include <core/SkRRect.h>
  19. #include <core/SkSurface.h>
  20. #include <effects/SkGradientShader.h>
  21. #include <effects/SkImageFilters.h>
  22. #include <gpu/GrDirectContext.h>
  23. #include <gpu/ganesh/SkSurfaceGanesh.h>
  24. #include <pathops/SkPathOps.h>
  25. namespace Gfx {
  26. struct PainterSkia::Impl {
  27. NonnullRefPtr<Gfx::Bitmap> gfx_bitmap;
  28. OwnPtr<SkBitmap> sk_bitmap;
  29. OwnPtr<SkCanvas> sk_canvas;
  30. Impl(NonnullRefPtr<Gfx::Bitmap> target_bitmap)
  31. : gfx_bitmap(move(target_bitmap))
  32. {
  33. sk_bitmap = make<SkBitmap>();
  34. SkImageInfo info = SkImageInfo::Make(gfx_bitmap->width(), gfx_bitmap->height(), kBGRA_8888_SkColorType, kUnpremul_SkAlphaType);
  35. sk_bitmap->installPixels(info, gfx_bitmap->scanline(0), gfx_bitmap->pitch());
  36. sk_canvas = make<SkCanvas>(*sk_bitmap);
  37. }
  38. SkCanvas* canvas() { return sk_canvas; }
  39. };
  40. static constexpr SkRect to_skia_rect(auto const& rect)
  41. {
  42. return SkRect::MakeXYWH(rect.x(), rect.y(), rect.width(), rect.height());
  43. }
  44. static constexpr SkColor to_skia_color(Gfx::Color const& color)
  45. {
  46. return SkColorSetARGB(color.alpha(), color.red(), color.green(), color.blue());
  47. }
  48. static SkPath to_skia_path(Gfx::Path const& path)
  49. {
  50. return static_cast<PathImplSkia const&>(path.impl()).sk_path();
  51. }
  52. static SkPathFillType to_skia_path_fill_type(Gfx::WindingRule winding_rule)
  53. {
  54. switch (winding_rule) {
  55. case Gfx::WindingRule::Nonzero:
  56. return SkPathFillType::kWinding;
  57. case Gfx::WindingRule::EvenOdd:
  58. return SkPathFillType::kEvenOdd;
  59. }
  60. VERIFY_NOT_REACHED();
  61. }
  62. PainterSkia::PainterSkia(NonnullRefPtr<Gfx::Bitmap> target_bitmap)
  63. : m_impl(adopt_own(*new Impl { move(target_bitmap) }))
  64. {
  65. }
  66. PainterSkia::~PainterSkia() = default;
  67. void PainterSkia::clear_rect(Gfx::FloatRect const& rect, Gfx::Color color)
  68. {
  69. SkPaint paint;
  70. paint.setColor(to_skia_color(color));
  71. paint.setBlendMode(SkBlendMode::kClear);
  72. impl().canvas()->drawRect(to_skia_rect(rect), paint);
  73. }
  74. void PainterSkia::fill_rect(Gfx::FloatRect const& rect, Color color)
  75. {
  76. SkPaint paint;
  77. paint.setColor(to_skia_color(color));
  78. impl().canvas()->drawRect(to_skia_rect(rect), paint);
  79. }
  80. static SkSamplingOptions to_skia_sampling_options(Gfx::ScalingMode scaling_mode)
  81. {
  82. switch (scaling_mode) {
  83. case Gfx::ScalingMode::NearestNeighbor:
  84. return SkSamplingOptions(SkFilterMode::kNearest);
  85. case Gfx::ScalingMode::BilinearBlend:
  86. case Gfx::ScalingMode::SmoothPixels:
  87. return SkSamplingOptions(SkFilterMode::kLinear);
  88. case Gfx::ScalingMode::BoxSampling:
  89. return SkSamplingOptions(SkCubicResampler::Mitchell());
  90. default:
  91. VERIFY_NOT_REACHED();
  92. }
  93. }
  94. static SkColorType to_skia_color_type(Gfx::BitmapFormat format)
  95. {
  96. switch (format) {
  97. case Gfx::BitmapFormat::Invalid:
  98. return kUnknown_SkColorType;
  99. case Gfx::BitmapFormat::BGRA8888:
  100. case Gfx::BitmapFormat::BGRx8888:
  101. return kBGRA_8888_SkColorType;
  102. case Gfx::BitmapFormat::RGBA8888:
  103. return kRGBA_8888_SkColorType;
  104. default:
  105. return kUnknown_SkColorType;
  106. }
  107. }
  108. void PainterSkia::draw_bitmap(Gfx::FloatRect const& dst_rect, Gfx::Bitmap const& src_bitmap, Gfx::IntRect const& src_rect, Gfx::ScalingMode scaling_mode, float global_alpha)
  109. {
  110. SkBitmap sk_bitmap;
  111. SkImageInfo info = SkImageInfo::Make(src_bitmap.width(), src_bitmap.height(), to_skia_color_type(src_bitmap.format()), kUnpremul_SkAlphaType);
  112. sk_bitmap.installPixels(info, const_cast<void*>(static_cast<void const*>(src_bitmap.scanline(0))), src_bitmap.pitch());
  113. SkPaint paint;
  114. paint.setAlpha(static_cast<u8>(global_alpha * 255));
  115. impl().canvas()->drawImageRect(
  116. sk_bitmap.asImage(),
  117. to_skia_rect(src_rect),
  118. to_skia_rect(dst_rect),
  119. to_skia_sampling_options(scaling_mode),
  120. &paint,
  121. SkCanvas::kStrict_SrcRectConstraint);
  122. }
  123. void PainterSkia::set_transform(Gfx::AffineTransform const& transform)
  124. {
  125. auto matrix = SkMatrix::MakeAll(
  126. transform.a(), transform.c(), transform.e(),
  127. transform.b(), transform.d(), transform.f(),
  128. 0, 0, 1);
  129. impl().canvas()->setMatrix(matrix);
  130. }
  131. void PainterSkia::stroke_path(Gfx::Path const& path, Gfx::Color color, float thickness)
  132. {
  133. // Skia treats zero thickness as a special case and will draw a hairline, while we want to draw nothing.
  134. if (!thickness)
  135. return;
  136. SkPaint paint;
  137. paint.setAntiAlias(true);
  138. paint.setStyle(SkPaint::kStroke_Style);
  139. paint.setStrokeWidth(thickness);
  140. paint.setColor(to_skia_color(color));
  141. auto sk_path = to_skia_path(path);
  142. impl().canvas()->drawPath(sk_path, paint);
  143. }
  144. static SkPoint to_skia_point(auto const& point)
  145. {
  146. return SkPoint::Make(point.x(), point.y());
  147. }
  148. static SkPaint to_skia_paint(Gfx::PaintStyle const& style, Gfx::FloatRect const& bounding_rect)
  149. {
  150. if (is<Gfx::CanvasLinearGradientPaintStyle>(style)) {
  151. auto const& linear_gradient = static_cast<Gfx::CanvasLinearGradientPaintStyle const&>(style);
  152. auto const& color_stops = linear_gradient.color_stops();
  153. SkPaint paint;
  154. Vector<SkColor> colors;
  155. colors.ensure_capacity(color_stops.size());
  156. Vector<SkScalar> positions;
  157. positions.ensure_capacity(color_stops.size());
  158. for (auto const& color_stop : color_stops) {
  159. colors.append(to_skia_color(color_stop.color));
  160. positions.append(color_stop.position);
  161. }
  162. Array<SkPoint, 2> points;
  163. points[0] = to_skia_point(linear_gradient.start_point());
  164. points[1] = to_skia_point(linear_gradient.end_point());
  165. SkMatrix matrix;
  166. auto shader = SkGradientShader::MakeLinear(points.data(), colors.data(), positions.data(), color_stops.size(), SkTileMode::kClamp, 0, &matrix);
  167. paint.setShader(shader);
  168. return paint;
  169. }
  170. if (is<Gfx::CanvasRadialGradientPaintStyle>(style)) {
  171. auto const& radial_gradient = static_cast<Gfx::CanvasRadialGradientPaintStyle const&>(style);
  172. auto const& color_stops = radial_gradient.color_stops();
  173. SkPaint paint;
  174. Vector<SkColor> colors;
  175. colors.ensure_capacity(color_stops.size());
  176. Vector<SkScalar> positions;
  177. positions.ensure_capacity(color_stops.size());
  178. for (auto const& color_stop : color_stops) {
  179. colors.append(to_skia_color(color_stop.color));
  180. positions.append(color_stop.position);
  181. }
  182. auto start_center = radial_gradient.start_center();
  183. auto end_center = radial_gradient.end_center();
  184. auto start_radius = radial_gradient.start_radius();
  185. auto end_radius = radial_gradient.end_radius();
  186. start_center.translate_by(bounding_rect.location());
  187. end_center.translate_by(bounding_rect.location());
  188. auto start_sk_point = to_skia_point(start_center);
  189. auto end_sk_point = to_skia_point(end_center);
  190. SkMatrix matrix;
  191. auto shader = SkGradientShader::MakeTwoPointConical(start_sk_point, start_radius, end_sk_point, end_radius, colors.data(), positions.data(), color_stops.size(), SkTileMode::kClamp, 0, &matrix);
  192. paint.setShader(shader);
  193. }
  194. return {};
  195. }
  196. void PainterSkia::stroke_path(Gfx::Path const& path, Gfx::PaintStyle const& paint_style, float thickness, float global_alpha)
  197. {
  198. // Skia treats zero thickness as a special case and will draw a hairline, while we want to draw nothing.
  199. if (!thickness)
  200. return;
  201. auto sk_path = to_skia_path(path);
  202. auto paint = to_skia_paint(paint_style, path.bounding_box());
  203. paint.setAntiAlias(true);
  204. paint.setAlphaf(global_alpha);
  205. paint.setStyle(SkPaint::Style::kStroke_Style);
  206. paint.setStrokeWidth(thickness);
  207. impl().canvas()->drawPath(sk_path, paint);
  208. }
  209. void PainterSkia::fill_path(Gfx::Path const& path, Gfx::Color color, Gfx::WindingRule winding_rule)
  210. {
  211. SkPaint paint;
  212. paint.setAntiAlias(true);
  213. paint.setColor(to_skia_color(color));
  214. auto sk_path = to_skia_path(path);
  215. sk_path.setFillType(to_skia_path_fill_type(winding_rule));
  216. impl().canvas()->drawPath(sk_path, paint);
  217. }
  218. void PainterSkia::fill_path(Gfx::Path const& path, Gfx::PaintStyle const& paint_style, float global_alpha, Gfx::WindingRule winding_rule)
  219. {
  220. auto sk_path = to_skia_path(path);
  221. sk_path.setFillType(to_skia_path_fill_type(winding_rule));
  222. auto paint = to_skia_paint(paint_style, path.bounding_box());
  223. paint.setAntiAlias(true);
  224. paint.setAlphaf(global_alpha);
  225. impl().canvas()->drawPath(sk_path, paint);
  226. }
  227. void PainterSkia::save()
  228. {
  229. impl().canvas()->save();
  230. }
  231. void PainterSkia::restore()
  232. {
  233. impl().canvas()->restore();
  234. }
  235. }