Flutter Engine
The Flutter Engine
RasterPathUtils.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2024 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
9
16
17namespace skgpu::graphite {
18
20 if (!fPixels) {
21 return false;
22 }
23
24 // Allocate pixmap if needed
25 if (!fPixels->addr()) {
26 const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(pixmapSize);
27 if (!fPixels->tryAlloc(bmImageInfo)) {
28 return false;
29 }
30 fPixels->erase(0);
31 } else if (fPixels->dimensions() != pixmapSize) {
32 return false;
33 }
34
36 fDraw.fDst = *fPixels;
37 fDraw.fRC = &fRasterClip;
38 return true;
39}
40
42 const Transform& transform,
43 const SkStrokeRec& strokeRec,
44 const SkIRect& resultBounds) {
45 fRasterClip.setRect(resultBounds);
46
48 paint.setBlendMode(SkBlendMode::kSrc); // "Replace" mode
49 paint.setAntiAlias(true);
50 // SkPaint's color is unpremul so this will produce alpha in every channel.
51 paint.setColor(SK_ColorWHITE);
52 strokeRec.applyToPaint(&paint);
53
54 SkMatrix translatedMatrix = SkMatrix(transform);
55 // The atlas transform of the shape is the linear-components (scale, rotation, skew) of
56 // `localToDevice` translated by the top-left offset of the resultBounds.
57 // We will need to translate draws so the bound's UL corner is at the origin
58 translatedMatrix.postTranslate(resultBounds.x(), resultBounds.y());
59
60 fDraw.fCTM = &translatedMatrix;
61 SkPath path = shape.asPath();
62 if (path.isInverseFillType()) {
63 // The shader will handle the inverse fill in this case
64 path.toggleInverseFillType();
65 }
67}
68
70 const Transform& transform,
71 const SkStrokeRec& strokeRec,
72 skvx::half2 maskSize) {
73 skgpu::UniqueKey maskKey;
74 {
76 int styleKeySize = 6;
77 if (!strokeRec.isHairlineStyle() && !strokeRec.isFillStyle()) {
78 // Add space for width and miter if needed
79 styleKeySize += 2;
80 }
81 skgpu::UniqueKey::Builder builder(&maskKey, kDomain, styleKeySize + shape.keySize(),
82 "Raster Path Mask");
83 builder[0] = maskSize.x() | (maskSize.y() << 16);
84
85 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
86 SkMatrix mat = transform.matrix().asM33();
91#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
92 // Fractional translate does not affect caching on Android. This is done for better cache
93 // hit ratio and speed and is matching HWUI behavior, which didn't consider the matrix
94 // at all when caching paths.
95 SkFixed fracX = 0;
96 SkFixed fracY = 0;
97#else
100 // Allow 8 bits each in x and y of subpixel positioning.
101 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
102 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
103#endif
104 builder[1] = SkFloat2Bits(sx);
105 builder[2] = SkFloat2Bits(sy);
106 builder[3] = SkFloat2Bits(kx);
107 builder[4] = SkFloat2Bits(ky);
108 // FracX and fracY are &ed with 0x0000ff00, so need to shift one down to fill 16 bits.
109 uint32_t fracBits = fracX | (fracY >> 8);
110 // Distinguish between path styles. For anything but fill, we also need to include
111 // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). For stroke
112 // or fill-and-stroke we need to include the join, width, and miter.
113 static_assert(SkStrokeRec::kStyleCount <= (1 << 2));
114 static_assert(SkPaint::kCapCount <= (1 << 2));
115 static_assert(SkPaint::kJoinCount <= (1 << 2));
116 uint32_t styleBits = strokeRec.getStyle();
117 if (!strokeRec.isFillStyle()) {
118 styleBits |= (strokeRec.getCap() << 2);
119 }
120 if (!strokeRec.isHairlineStyle() && !strokeRec.isFillStyle()) {
121 styleBits |= (strokeRec.getJoin() << 4);
122 builder[5] = SkFloat2Bits(strokeRec.getWidth());
123 builder[6] = SkFloat2Bits(strokeRec.getMiter());
124 }
125 builder[styleKeySize-1] = fracBits | (styleBits << 16);
126 shape.writeKey(&builder[styleKeySize], /*includeInverted=*/false);
127 }
128 return maskKey;
129}
130
131} // namespace skgpu::graphite
SkBlitter * SkA8Blitter_Choose(const SkPixmap &dst, const SkMatrix &ctm, const SkPaint &paint, SkArenaAlloc *alloc, bool drawCoverage, sk_sp< SkShader > clipShader, const SkSurfaceProps &)
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
int32_t SkFixed
Definition: SkFixed.h:25
#define SkScalarToFixed(x)
Definition: SkFixed.h:125
static uint32_t SkFloat2Bits(float value)
Definition: SkFloatBits.h:41
static SkScalar SkScalarFraction(SkScalar x)
Definition: SkScalar.h:67
bool tryAlloc(const SkImageInfo &)
BlitterChooser * fBlitterChooser
Definition: SkDrawBase.h:154
const SkRasterClip * fRC
Definition: SkDrawBase.h:156
void drawPathCoverage(const SkPath &src, const SkPaint &paint, SkBlitter *customBlitter=nullptr) const
Definition: SkDrawBase.h:71
SkPixmap fDst
Definition: SkDrawBase.h:153
const SkMatrix * fCTM
Definition: SkDrawBase.h:155
SkMatrix & postTranslate(SkScalar dx, SkScalar dy)
Definition: SkMatrix.cpp:281
static constexpr int kMScaleX
horizontal scale factor
Definition: SkMatrix.h:353
static constexpr int kMTransY
vertical translation
Definition: SkMatrix.h:358
static constexpr int kMTransX
horizontal translation
Definition: SkMatrix.h:355
static constexpr int kMSkewY
vertical skew factor
Definition: SkMatrix.h:356
SkScalar get(int index) const
Definition: SkMatrix.h:392
static constexpr int kMScaleY
vertical scale factor
Definition: SkMatrix.h:357
static constexpr int kMSkewX
horizontal skew factor
Definition: SkMatrix.h:354
static constexpr int kCapCount
Definition: SkPaint.h:343
static constexpr int kJoinCount
Definition: SkPaint.h:368
Definition: SkPath.h:59
bool erase(SkColor color, const SkIRect &subset) const
Definition: SkPixmap.cpp:742
const void * addr() const
Definition: SkPixmap.h:153
SkISize dimensions() const
Definition: SkPixmap.h:171
bool setRect(const SkIRect &)
static constexpr int kStyleCount
Definition: SkStrokeRec.h:39
Style getStyle() const
Definition: SkStrokeRec.cpp:71
bool isHairlineStyle() const
Definition: SkStrokeRec.h:47
SkScalar getWidth() const
Definition: SkStrokeRec.h:42
SkPaint::Join getJoin() const
Definition: SkStrokeRec.h:45
SkPaint::Cap getCap() const
Definition: SkStrokeRec.h:44
bool isFillStyle() const
Definition: SkStrokeRec.h:51
void applyToPaint(SkPaint *paint) const
SkScalar getMiter() const
Definition: SkStrokeRec.h:43
static Domain GenerateDomain()
Definition: ResourceKey.cpp:27
bool init(SkISize pixmapSize)
void drawShape(const Shape &shape, const Transform &transform, const SkStrokeRec &strokeRec, const SkIRect &resultBounds)
int keySize() const
Definition: Shape.cpp:140
void writeKey(uint32_t *key, bool includeInverted) const
Definition: Shape.cpp:177
SkPath asPath() const
Definition: Shape.cpp:80
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
skgpu::UniqueKey GeneratePathMaskKey(const Shape &shape, const Transform &transform, const SkStrokeRec &strokeRec, skvx::half2 maskSize)
static SkColor4f transform(SkColor4f c, SkColorSpace *src, SkColorSpace *dst)
Definition: p3.cpp:47
Definition: SkRect.h:32
constexpr int32_t x() const
Definition: SkRect.h:141
constexpr int32_t y() const
Definition: SkRect.h:148
Definition: SkSize.h:16
static SkImageInfo MakeA8(int width, int height)
Definition: SkVx.h:83