Debian Codegraph @

src/gpu/ganesh/GrSWMaskHelper.cpp

raw
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 #include "src/gpu/ganesh/image/GrMippedBitmap.h"
29
30 #include <cstddef>
31 #include <tuple>
32
33 static SkPaint get_paint(GrAA aa, uint8_t alpha) {
34 SkPaint paint;
35 paint.setBlendMode(SkBlendMode::kSrc); // "Replace" mode
36 paint.setAntiAlias(GrAA::kYes == aa);
37 // SkPaint's color is unpremul so this will produce alpha in every channel.
38 paint.setColor(SkColorSetARGB(alpha, 255, 255, 255));
39 return paint;
40 }
41
42 /**
43 * Draw a single rect element of the clip stack into the accumulation bitmap
44 */
45 void GrSWMaskHelper::drawRect(const SkRect& rect, const SkMatrix& matrix, GrAA aa, uint8_t alpha) {
46 SkMatrix translatedMatrix = matrix;
47 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
48 fDraw.fCTM = &translatedMatrix;
49
50 fDraw.drawRect(rect, get_paint(aa, alpha));
51 }
52
53 void GrSWMaskHelper::drawRRect(const SkRRect& rrect, const SkMatrix& matrix,
54 GrAA aa, uint8_t alpha) {
55 SkMatrix translatedMatrix = matrix;
56 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
57 fDraw.fCTM = &translatedMatrix;
58
59 fDraw.drawRRect(rrect, get_paint(aa, alpha));
60 }
61
62 /**
63 * Draw a single path element of the clip stack into the accumulation bitmap
64 */
65 void GrSWMaskHelper::drawShape(const GrStyledShape& shape, const SkMatrix& matrix,
66 GrAA aa, uint8_t alpha) {
67 SkPaint paint = get_paint(aa, alpha);
68 paint.setPathEffect(shape.style().refPathEffect());
69 shape.style().strokeRec().applyToPaint(&paint);
70
71 SkMatrix translatedMatrix = matrix;
72 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
73 fDraw.fCTM = &translatedMatrix;
74
75 SkPath path = shape.asPath();
76 if (0xFF == alpha) {
77 SkASSERT(0xFF == paint.getAlpha());
78 fDraw.drawPathCoverage(path, paint);
79 } else {
80 fDraw.drawPath(path, paint, nullptr);
81 }
82 }
83
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 = shape.asPath();
112 if (0xFF == alpha) {
113 SkASSERT(0xFF == paint.getAlpha());
114 fDraw.drawPathCoverage(path, paint);
115 } else {
116 fDraw.drawPath(path, paint, nullptr);
117 }
118 }
119
120 bool GrSWMaskHelper::init(const SkIRect& resultBounds) {
121 // We will need to translate draws so the bound's UL corner is at the origin
122 fTranslate = {-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop)};
123 SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height());
124
125 const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height());
126 if (!fPixels->tryAlloc(bmImageInfo)) {
127 return false;
128 }
129 fPixels->erase(0);
130
131 fDraw.fBlitterChooser = SkA8Blitter_Choose;
132 fDraw.fDst = *fPixels;
133 fRasterClip.setRect(bounds);
134 fDraw.fRC = &fRasterClip;
135 return true;
136 }
137
138 GrSurfaceProxyView GrSWMaskHelper::toTextureView(GrRecordingContext* rContext, SkBackingFit fit) {
139 SkImageInfo ii = SkImageInfo::MakeA8(fPixels->width(), fPixels->height());
140 size_t rowBytes = fPixels->rowBytes();
141
142 std::optional<GrMippedBitmap> bitmap = GrMippedBitmap::Make(
143 ii,
144 fPixels->detachPixels(),
145 rowBytes,
146 [](void* addr, void* context) { sk_free(addr); },
147 nullptr);
148 if (!bitmap) {
149 return {};
150 }
151 return std::get<0>(
152 GrMakeUncachedBitmapProxyView(rContext, bitmap.value(), skgpu::Mipmapped::kNo, fit));
153 }