PainterSkia.cpp 8.8 KB

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