• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012 Google Inc.
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/ganesh/GrSWMaskHelper.h"
9 
10 #include "include/core/SkBitmap.h"
11 #include "include/gpu/GrRecordingContext.h"
12 #include "src/core/SkMatrixProvider.h"
13 #include "src/gpu/ganesh/GrCaps.h"
14 #include "src/gpu/ganesh/GrProxyProvider.h"
15 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
16 #include "src/gpu/ganesh/GrTextureProxy.h"
17 #include "src/gpu/ganesh/SkGr.h"
18 #include "src/gpu/ganesh/SurfaceContext.h"
19 #include "src/gpu/ganesh/geometry/GrStyledShape.h"
20 
21 /*
22  * Convert a boolean operation into a transfer mode code
23  */
op_to_mode(SkRegion::Op op)24 static SkBlendMode op_to_mode(SkRegion::Op op) {
25 
26     static const SkBlendMode modeMap[] = {
27         SkBlendMode::kDstOut,   // kDifference_Op
28         SkBlendMode::kModulate, // kIntersect_Op
29         SkBlendMode::kSrcOver,  // kUnion_Op
30         SkBlendMode::kXor,      // kXOR_Op
31         SkBlendMode::kClear,    // kReverseDifference_Op
32         SkBlendMode::kSrc,      // kReplace_Op
33     };
34 
35     return modeMap[op];
36 }
37 
get_paint(SkRegion::Op op,GrAA aa,uint8_t alpha)38 static SkPaint get_paint(SkRegion::Op op, GrAA aa, uint8_t alpha) {
39     SkPaint paint;
40     paint.setBlendMode(op_to_mode(op));
41     paint.setAntiAlias(GrAA::kYes == aa);
42     // SkPaint's color is unpremul so this will produce alpha in every channel.
43     paint.setColor(SkColorSetARGB(alpha, 255, 255, 255));
44     return paint;
45 }
46 
47 /**
48  * Draw a single rect element of the clip stack into the accumulation bitmap
49  */
drawRect(const SkRect & rect,const SkMatrix & matrix,SkRegion::Op op,GrAA aa,uint8_t alpha)50 void GrSWMaskHelper::drawRect(const SkRect& rect, const SkMatrix& matrix, SkRegion::Op op, GrAA aa,
51                               uint8_t alpha) {
52     SkMatrix translatedMatrix = matrix;
53     translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
54     SkMatrixProvider matrixProvider(translatedMatrix);
55     fDraw.fMatrixProvider = &matrixProvider;
56 
57     fDraw.drawRect(rect, get_paint(op, aa, alpha));
58 }
59 
drawRRect(const SkRRect & rrect,const SkMatrix & matrix,SkRegion::Op op,GrAA aa,uint8_t alpha)60 void GrSWMaskHelper::drawRRect(const SkRRect& rrect, const SkMatrix& matrix, SkRegion::Op op,
61                                GrAA aa, uint8_t alpha) {
62     SkMatrix translatedMatrix = matrix;
63     translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
64     SkMatrixProvider matrixProvider(translatedMatrix);
65     fDraw.fMatrixProvider = &matrixProvider;
66 
67     fDraw.drawRRect(rrect, get_paint(op, aa, alpha));
68 }
69 
70 /**
71  * Draw a single path element of the clip stack into the accumulation bitmap
72  */
drawShape(const GrStyledShape & shape,const SkMatrix & matrix,SkRegion::Op op,GrAA aa,uint8_t alpha)73 void GrSWMaskHelper::drawShape(const GrStyledShape& shape, const SkMatrix& matrix, SkRegion::Op op,
74                                GrAA aa, uint8_t alpha) {
75     SkPaint paint = get_paint(op, aa, alpha);
76     paint.setPathEffect(shape.style().refPathEffect());
77     shape.style().strokeRec().applyToPaint(&paint);
78 
79     SkMatrix translatedMatrix = matrix;
80     translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
81     SkMatrixProvider matrixProvider(translatedMatrix);
82     fDraw.fMatrixProvider = &matrixProvider;
83 
84     SkPath path;
85     shape.asPath(&path);
86     if (SkRegion::kReplace_Op == op && 0xFF == alpha) {
87         SkASSERT(0xFF == paint.getAlpha());
88         fDraw.drawPathCoverage(path, paint);
89     } else {
90         fDraw.drawPath(path, paint);
91     }
92 }
93 
drawShape(const GrShape & shape,const SkMatrix & matrix,SkRegion::Op op,GrAA aa,uint8_t alpha)94 void GrSWMaskHelper::drawShape(const GrShape& shape, const SkMatrix& matrix, SkRegion::Op op,
95                                GrAA aa, uint8_t alpha) {
96     SkPaint paint = get_paint(op, aa, alpha);
97 
98     SkMatrix translatedMatrix = matrix;
99     translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
100     SkMatrixProvider matrixProvider(translatedMatrix);
101     fDraw.fMatrixProvider = &matrixProvider;
102 
103     if (shape.inverted()) {
104         if (shape.isEmpty() || shape.isLine() || shape.isPoint()) {
105             // These shapes are empty for simple fills, so when inverted, cover everything
106             fDraw.drawPaint(paint);
107             return;
108         }
109         // Else fall through to the draw method using asPath(), which will toggle fill type properly
110     } else if (shape.isEmpty() || shape.isLine() || shape.isPoint()) {
111         // Do nothing, these shapes do not cover any pixels for simple fills
112         return;
113     } else if (shape.isRect()) {
114         fDraw.drawRect(shape.rect(), paint);
115         return;
116     } else if (shape.isRRect()) {
117         fDraw.drawRRect(shape.rrect(), paint);
118         return;
119     }
120 
121     // A complex, or inverse-filled shape, so go through drawPath.
122     SkPath path;
123     shape.asPath(&path);
124     if (SkRegion::kReplace_Op == op && 0xFF == alpha) {
125         SkASSERT(0xFF == paint.getAlpha());
126         fDraw.drawPathCoverage(path, paint);
127     } else {
128         fDraw.drawPath(path, paint);
129     }
130 }
131 
init(const SkIRect & resultBounds)132 bool GrSWMaskHelper::init(const SkIRect& resultBounds) {
133     // We will need to translate draws so the bound's UL corner is at the origin
134     fTranslate = {-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop)};
135     SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height());
136 
137     const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height());
138     if (!fPixels->tryAlloc(bmImageInfo)) {
139         return false;
140     }
141     fPixels->erase(0);
142 
143     fDraw.fDst      = *fPixels;
144     fRasterClip.setRect(bounds);
145     fDraw.fRC       = &fRasterClip;
146     return true;
147 }
148 
toTextureView(GrRecordingContext * rContext,SkBackingFit fit)149 GrSurfaceProxyView GrSWMaskHelper::toTextureView(GrRecordingContext* rContext, SkBackingFit fit) {
150     SkImageInfo ii = SkImageInfo::MakeA8(fPixels->width(), fPixels->height());
151     size_t rowBytes = fPixels->rowBytes();
152 
153     SkBitmap bitmap;
154     SkAssertResult(bitmap.installPixels(ii, fPixels->detachPixels(), rowBytes,
155                                         [](void* addr, void* context) { sk_free(addr); },
156                                         nullptr));
157     bitmap.setImmutable();
158 
159     return std::get<0>(GrMakeUncachedBitmapProxyView(rContext, bitmap, GrMipmapped::kNo, fit));
160 }
161