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