PainterSkia.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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/DeprecatedPath.h>
  9. #include <LibGfx/PainterSkia.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::DeprecatedPath const& path)
  49. {
  50. Optional<Gfx::FloatPoint> subpath_start_point;
  51. Optional<Gfx::FloatPoint> subpath_last_point;
  52. SkPathBuilder path_builder;
  53. auto close_subpath_if_needed = [&](auto last_point) {
  54. if (subpath_start_point == last_point)
  55. path_builder.close();
  56. };
  57. for (auto const& segment : path) {
  58. auto point = segment.point();
  59. switch (segment.command()) {
  60. case Gfx::DeprecatedPathSegment::Command::MoveTo: {
  61. if (subpath_start_point.has_value() && subpath_last_point.has_value())
  62. close_subpath_if_needed(subpath_last_point.value());
  63. subpath_start_point = point;
  64. path_builder.moveTo({ point.x(), point.y() });
  65. break;
  66. }
  67. case Gfx::DeprecatedPathSegment::Command::LineTo: {
  68. if (!subpath_start_point.has_value())
  69. subpath_start_point = Gfx::FloatPoint { 0.0f, 0.0f };
  70. path_builder.lineTo({ point.x(), point.y() });
  71. break;
  72. }
  73. case Gfx::DeprecatedPathSegment::Command::QuadraticBezierCurveTo: {
  74. if (!subpath_start_point.has_value())
  75. subpath_start_point = Gfx::FloatPoint { 0.0f, 0.0f };
  76. SkPoint pt1 = { segment.through().x(), segment.through().y() };
  77. SkPoint pt2 = { segment.point().x(), segment.point().y() };
  78. path_builder.quadTo(pt1, pt2);
  79. break;
  80. }
  81. case Gfx::DeprecatedPathSegment::Command::CubicBezierCurveTo: {
  82. if (!subpath_start_point.has_value())
  83. subpath_start_point = Gfx::FloatPoint { 0.0f, 0.0f };
  84. SkPoint pt1 = { segment.through_0().x(), segment.through_0().y() };
  85. SkPoint pt2 = { segment.through_1().x(), segment.through_1().y() };
  86. SkPoint pt3 = { segment.point().x(), segment.point().y() };
  87. path_builder.cubicTo(pt1, pt2, pt3);
  88. break;
  89. }
  90. default:
  91. VERIFY_NOT_REACHED();
  92. }
  93. subpath_last_point = point;
  94. }
  95. close_subpath_if_needed(subpath_last_point);
  96. return path_builder.snapshot();
  97. }
  98. static SkPathFillType to_skia_path_fill_type(Gfx::WindingRule winding_rule)
  99. {
  100. switch (winding_rule) {
  101. case Gfx::WindingRule::Nonzero:
  102. return SkPathFillType::kWinding;
  103. case Gfx::WindingRule::EvenOdd:
  104. return SkPathFillType::kEvenOdd;
  105. }
  106. VERIFY_NOT_REACHED();
  107. }
  108. PainterSkia::PainterSkia(NonnullRefPtr<Gfx::Bitmap> target_bitmap)
  109. : m_impl(adopt_own(*new Impl { move(target_bitmap) }))
  110. {
  111. }
  112. PainterSkia::~PainterSkia() = default;
  113. void PainterSkia::clear_rect(Gfx::FloatRect const& rect, Gfx::Color color)
  114. {
  115. SkPaint paint;
  116. paint.setColor(to_skia_color(color));
  117. paint.setBlendMode(SkBlendMode::kClear);
  118. impl().canvas()->drawRect(to_skia_rect(rect), paint);
  119. }
  120. void PainterSkia::fill_rect(Gfx::FloatRect const& rect, Color color)
  121. {
  122. SkPaint paint;
  123. paint.setColor(to_skia_color(color));
  124. impl().canvas()->drawRect(to_skia_rect(rect), paint);
  125. }
  126. static SkSamplingOptions to_skia_sampling_options(Gfx::ScalingMode scaling_mode)
  127. {
  128. switch (scaling_mode) {
  129. case Gfx::ScalingMode::NearestNeighbor:
  130. return SkSamplingOptions(SkFilterMode::kNearest);
  131. case Gfx::ScalingMode::BilinearBlend:
  132. case Gfx::ScalingMode::SmoothPixels:
  133. return SkSamplingOptions(SkFilterMode::kLinear);
  134. case Gfx::ScalingMode::BoxSampling:
  135. return SkSamplingOptions(SkCubicResampler::Mitchell());
  136. default:
  137. VERIFY_NOT_REACHED();
  138. }
  139. }
  140. static SkColorType to_skia_color_type(Gfx::BitmapFormat format)
  141. {
  142. switch (format) {
  143. case Gfx::BitmapFormat::Invalid:
  144. return kUnknown_SkColorType;
  145. case Gfx::BitmapFormat::BGRA8888:
  146. case Gfx::BitmapFormat::BGRx8888:
  147. return kBGRA_8888_SkColorType;
  148. case Gfx::BitmapFormat::RGBA8888:
  149. return kRGBA_8888_SkColorType;
  150. default:
  151. return kUnknown_SkColorType;
  152. }
  153. }
  154. 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)
  155. {
  156. SkBitmap sk_bitmap;
  157. SkImageInfo info = SkImageInfo::Make(src_bitmap.width(), src_bitmap.height(), to_skia_color_type(src_bitmap.format()), kUnpremul_SkAlphaType);
  158. sk_bitmap.installPixels(info, const_cast<void*>(static_cast<void const*>(src_bitmap.scanline(0))), src_bitmap.pitch());
  159. SkPaint paint;
  160. paint.setAlpha(static_cast<u8>(global_alpha * 255));
  161. impl().canvas()->drawImageRect(
  162. sk_bitmap.asImage(),
  163. to_skia_rect(src_rect),
  164. to_skia_rect(dst_rect),
  165. to_skia_sampling_options(scaling_mode),
  166. &paint,
  167. SkCanvas::kStrict_SrcRectConstraint);
  168. }
  169. void PainterSkia::set_transform(Gfx::AffineTransform const& transform)
  170. {
  171. auto matrix = SkMatrix::MakeAll(
  172. transform.a(), transform.c(), transform.e(),
  173. transform.b(), transform.d(), transform.f(),
  174. 0, 0, 1);
  175. impl().canvas()->setMatrix(matrix);
  176. }
  177. void PainterSkia::stroke_path(Gfx::DeprecatedPath const& path, Gfx::Color color, float thickness)
  178. {
  179. // Skia treats zero thickness as a special case and will draw a hairline, while we want to draw nothing.
  180. if (!thickness)
  181. return;
  182. SkPaint paint;
  183. paint.setAntiAlias(true);
  184. paint.setStyle(SkPaint::kStroke_Style);
  185. paint.setStrokeWidth(thickness);
  186. paint.setColor(to_skia_color(color));
  187. auto sk_path = to_skia_path(path);
  188. impl().canvas()->drawPath(sk_path, paint);
  189. }
  190. static SkPoint to_skia_point(auto const& point)
  191. {
  192. return SkPoint::Make(point.x(), point.y());
  193. }
  194. static SkPaint to_skia_paint(Gfx::PaintStyle const& style, Gfx::FloatRect const& bounding_rect)
  195. {
  196. if (is<Gfx::CanvasLinearGradientPaintStyle>(style)) {
  197. auto const& linear_gradient = static_cast<Gfx::CanvasLinearGradientPaintStyle const&>(style);
  198. auto const& color_stops = linear_gradient.color_stops();
  199. SkPaint paint;
  200. Vector<SkColor> colors;
  201. colors.ensure_capacity(color_stops.size());
  202. Vector<SkScalar> positions;
  203. positions.ensure_capacity(color_stops.size());
  204. for (auto const& color_stop : color_stops) {
  205. colors.append(to_skia_color(color_stop.color));
  206. positions.append(color_stop.position);
  207. }
  208. Array<SkPoint, 2> points;
  209. points[0] = to_skia_point(linear_gradient.start_point());
  210. points[1] = to_skia_point(linear_gradient.end_point());
  211. SkMatrix matrix;
  212. auto shader = SkGradientShader::MakeLinear(points.data(), colors.data(), positions.data(), color_stops.size(), SkTileMode::kClamp, 0, &matrix);
  213. paint.setShader(shader);
  214. return paint;
  215. }
  216. if (is<Gfx::CanvasRadialGradientPaintStyle>(style)) {
  217. auto const& radial_gradient = static_cast<Gfx::CanvasRadialGradientPaintStyle const&>(style);
  218. auto const& color_stops = radial_gradient.color_stops();
  219. SkPaint paint;
  220. Vector<SkColor> colors;
  221. colors.ensure_capacity(color_stops.size());
  222. Vector<SkScalar> positions;
  223. positions.ensure_capacity(color_stops.size());
  224. for (auto const& color_stop : color_stops) {
  225. colors.append(to_skia_color(color_stop.color));
  226. positions.append(color_stop.position);
  227. }
  228. auto start_center = radial_gradient.start_center();
  229. auto end_center = radial_gradient.end_center();
  230. auto start_radius = radial_gradient.start_radius();
  231. auto end_radius = radial_gradient.end_radius();
  232. start_center.translate_by(bounding_rect.location());
  233. end_center.translate_by(bounding_rect.location());
  234. auto start_sk_point = to_skia_point(start_center);
  235. auto end_sk_point = to_skia_point(end_center);
  236. SkMatrix matrix;
  237. 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);
  238. paint.setShader(shader);
  239. }
  240. return {};
  241. }
  242. void PainterSkia::stroke_path(Gfx::DeprecatedPath const& path, Gfx::PaintStyle const& paint_style, float thickness, float global_alpha)
  243. {
  244. // Skia treats zero thickness as a special case and will draw a hairline, while we want to draw nothing.
  245. if (!thickness)
  246. return;
  247. auto sk_path = to_skia_path(path);
  248. auto paint = to_skia_paint(paint_style, path.bounding_box());
  249. paint.setAntiAlias(true);
  250. paint.setAlphaf(global_alpha);
  251. paint.setStyle(SkPaint::Style::kStroke_Style);
  252. paint.setStrokeWidth(thickness);
  253. impl().canvas()->drawPath(sk_path, paint);
  254. }
  255. void PainterSkia::fill_path(Gfx::DeprecatedPath const& path, Gfx::Color color, Gfx::WindingRule winding_rule)
  256. {
  257. SkPaint paint;
  258. paint.setAntiAlias(true);
  259. paint.setColor(to_skia_color(color));
  260. auto sk_path = to_skia_path(path);
  261. sk_path.setFillType(to_skia_path_fill_type(winding_rule));
  262. impl().canvas()->drawPath(sk_path, paint);
  263. }
  264. void PainterSkia::fill_path(Gfx::DeprecatedPath const& path, Gfx::PaintStyle const& paint_style, float global_alpha, Gfx::WindingRule winding_rule)
  265. {
  266. auto sk_path = to_skia_path(path);
  267. sk_path.setFillType(to_skia_path_fill_type(winding_rule));
  268. auto paint = to_skia_paint(paint_style, path.bounding_box());
  269. paint.setAntiAlias(true);
  270. paint.setAlphaf(global_alpha);
  271. impl().canvas()->drawPath(sk_path, paint);
  272. }
  273. void PainterSkia::save()
  274. {
  275. impl().canvas()->save();
  276. }
  277. void PainterSkia::restore()
  278. {
  279. impl().canvas()->restore();
  280. }
  281. }