• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "src/gpu/graphite/RasterPathUtils.h"
9 
10 #include "include/core/SkStrokeRec.h"
11 #include "include/private/base/SkFixed.h"
12 #include "src/base/SkFloatBits.h"
13 #include "src/core/SkBlitter_A8.h"
14 #include "src/gpu/graphite/geom/Shape.h"
15 #include "src/gpu/graphite/geom/Transform_graphite.h"
16 
17 namespace skgpu::graphite {
18 
init(SkISize pixmapSize)19 bool RasterMaskHelper::init(SkISize pixmapSize) {
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 
35     fDraw.fBlitterChooser = SkA8Blitter_Choose;
36     fDraw.fDst      = *fPixels;
37     fDraw.fRC       = &fRasterClip;
38     return true;
39 }
40 
drawShape(const Shape & shape,const Transform & transform,const SkStrokeRec & strokeRec,const SkIRect & resultBounds)41 void RasterMaskHelper::drawShape(const Shape& shape,
42                                  const Transform& transform,
43                                  const SkStrokeRec& strokeRec,
44                                  const SkIRect& resultBounds) {
45     fRasterClip.setRect(resultBounds);
46 
47     SkPaint paint;
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     }
66     fDraw.drawPathCoverage(path, paint);
67 }
68 
GeneratePathMaskKey(const Shape & shape,const Transform & transform,const SkStrokeRec & strokeRec,skvx::half2 maskOrigin,skvx::half2 maskSize)69 skgpu::UniqueKey GeneratePathMaskKey(const Shape& shape,
70                                      const Transform& transform,
71                                      const SkStrokeRec& strokeRec,
72                                      skvx::half2 maskOrigin,
73                                      skvx::half2 maskSize) {
74     skgpu::UniqueKey maskKey;
75     {
76         static const skgpu::UniqueKey::Domain kDomain = skgpu::UniqueKey::GenerateDomain();
77         int styleKeySize = 7;
78         if (!strokeRec.isHairlineStyle() && !strokeRec.isFillStyle()) {
79             // Add space for width and miter if needed
80             styleKeySize += 2;
81         }
82         skgpu::UniqueKey::Builder builder(&maskKey, kDomain, styleKeySize + shape.keySize(),
83                                           "Raster Path Mask");
84         builder[0] = maskOrigin.x() | (maskOrigin.y() << 16);
85         builder[1] = maskSize.x() | (maskSize.y() << 16);
86 
87         // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
88         SkMatrix mat = transform.matrix().asM33();
89         SkScalar sx = mat.get(SkMatrix::kMScaleX);
90         SkScalar sy = mat.get(SkMatrix::kMScaleY);
91         SkScalar kx = mat.get(SkMatrix::kMSkewX);
92         SkScalar ky = mat.get(SkMatrix::kMSkewY);
93 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
94         // Fractional translate does not affect caching on Android. This is done for better cache
95         // hit ratio and speed and is matching HWUI behavior, which didn't consider the matrix
96         // at all when caching paths.
97         SkFixed fracX = 0;
98         SkFixed fracY = 0;
99 #else
100         SkScalar tx = mat.get(SkMatrix::kMTransX);
101         SkScalar ty = mat.get(SkMatrix::kMTransY);
102         // Allow 8 bits each in x and y of subpixel positioning.
103         SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
104         SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
105 #endif
106         builder[2] = SkFloat2Bits(sx);
107         builder[3] = SkFloat2Bits(sy);
108         builder[4] = SkFloat2Bits(kx);
109         builder[5] = SkFloat2Bits(ky);
110         // FracX and fracY are &ed with 0x0000ff00, so need to shift one down to fill 16 bits.
111         uint32_t fracBits = fracX | (fracY >> 8);
112         // Distinguish between path styles. For anything but fill, we also need to include
113         // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). For stroke
114         // or fill-and-stroke we need to include the join, width, and miter.
115         static_assert(SkStrokeRec::kStyleCount <= (1 << 2));
116         static_assert(SkPaint::kCapCount <= (1 << 2));
117         static_assert(SkPaint::kJoinCount <= (1 << 2));
118         uint32_t styleBits = strokeRec.getStyle();
119         if (!strokeRec.isFillStyle()) {
120             styleBits |= (strokeRec.getCap() << 2);
121         }
122         if (!strokeRec.isHairlineStyle() && !strokeRec.isFillStyle()) {
123             styleBits |= (strokeRec.getJoin() << 4);
124             builder[6] = SkFloat2Bits(strokeRec.getWidth());
125             builder[7] = SkFloat2Bits(strokeRec.getMiter());
126         }
127         builder[styleKeySize-1] = fracBits | (styleBits << 16);
128         shape.writeKey(&builder[styleKeySize], /*includeInverted=*/false);
129     }
130     return maskKey;
131 }
132 
133 }  // namespace skgpu::graphite
134