Coverage Report

Created: 2021-08-22 09:07

/src/skia/src/gpu/GrDefaultGeoProcFactory.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2014 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/GrDefaultGeoProcFactory.h"
9
10
#include "include/core/SkRefCnt.h"
11
#include "src/core/SkArenaAlloc.h"
12
#include "src/gpu/GrCaps.h"
13
#include "src/gpu/GrGeometryProcessor.h"
14
#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
15
#include "src/gpu/glsl/GrGLSLUniformHandler.h"
16
#include "src/gpu/glsl/GrGLSLVarying.h"
17
#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
18
19
/*
20
 * The default Geometry Processor simply takes position and multiplies it by the uniform view
21
 * matrix. It also leaves coverage untouched.  Behind the scenes, we may add per vertex color or
22
 * local coords.
23
 */
24
25
enum GPFlag {
26
    kColorAttribute_GPFlag              = 0x1,
27
    kColorAttributeIsWide_GPFlag        = 0x2,
28
    kLocalCoordAttribute_GPFlag         = 0x4,
29
    kCoverageAttribute_GPFlag           = 0x8,
30
    kCoverageAttributeTweak_GPFlag      = 0x10,
31
    kCoverageAttributeUnclamped_GPFlag  = 0x20,
32
};
33
34
class DefaultGeoProc : public GrGeometryProcessor {
35
public:
36
    static GrGeometryProcessor* Make(SkArenaAlloc* arena,
37
                                     uint32_t gpTypeFlags,
38
                                     const SkPMColor4f& color,
39
                                     const SkMatrix& viewMatrix,
40
                                     const SkMatrix& localMatrix,
41
                                     bool localCoordsWillBeRead,
42
19.4k
                                     uint8_t coverage) {
43
19.4k
        return arena->make([&](void* ptr) {
44
19.4k
            return new (ptr) DefaultGeoProc(gpTypeFlags, color, viewMatrix, localMatrix, coverage,
45
19.4k
                                            localCoordsWillBeRead);
46
19.4k
        });
47
19.4k
    }
48
49
0
    const char* name() const override { return "DefaultGeometryProcessor"; }
50
51
0
    void addToKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override {
52
0
        uint32_t key = fFlags;
53
0
        key |= fCoverage == 0xff      ?  0x80 : 0;
54
0
        key |= fLocalCoordsWillBeRead ? 0x100 : 0;
55
56
0
        bool usesLocalMatrix = fLocalCoordsWillBeRead && !fInLocalCoords.isInitialized();
57
0
        key = ProgramImpl::AddMatrixKeys(caps,
58
0
                                         key,
59
0
                                         fViewMatrix,
60
0
                                         usesLocalMatrix ? fLocalMatrix : SkMatrix::I());
61
0
        b->add32(key);
62
0
    }
63
64
0
    std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const override {
65
0
        return std::make_unique<Impl>();
66
0
    }
67
68
private:
69
    class Impl : public ProgramImpl {
70
    public:
71
        void setData(const GrGLSLProgramDataManager& pdman,
72
                     const GrShaderCaps& shaderCaps,
73
0
                     const GrGeometryProcessor& geomProc) override {
74
0
            const DefaultGeoProc& dgp = geomProc.cast<DefaultGeoProc>();
75
76
0
            SetTransform(pdman, shaderCaps, fViewMatrixUniform, dgp.fViewMatrix, &fViewMatrixPrev);
77
0
            SetTransform(pdman,
78
0
                         shaderCaps,
79
0
                         fLocalMatrixUniform,
80
0
                         dgp.fLocalMatrix,
81
0
                         &fLocalMatrixPrev);
82
83
0
            if (!dgp.hasVertexColor() && dgp.fColor != fColor) {
84
0
                pdman.set4fv(fColorUniform, 1, dgp.fColor.vec());
85
0
                fColor = dgp.fColor;
86
0
            }
87
88
0
            if (dgp.fCoverage != fCoverage && !dgp.hasVertexCoverage()) {
89
0
                pdman.set1f(fCoverageUniform, GrNormalizeByteToFloat(dgp.fCoverage));
90
0
                fCoverage = dgp.fCoverage;
91
0
            }
92
0
        }
93
94
    private:
95
0
        void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
96
0
            const DefaultGeoProc& gp = args.fGeomProc.cast<DefaultGeoProc>();
97
0
            GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
98
0
            GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
99
0
            GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
100
0
            GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
101
102
            // emit attributes
103
0
            varyingHandler->emitAttributes(gp);
104
105
0
            bool tweakAlpha = SkToBool(gp.fFlags & kCoverageAttributeTweak_GPFlag);
106
0
            bool coverageNeedsSaturate = SkToBool(gp.fFlags & kCoverageAttributeUnclamped_GPFlag);
107
0
            SkASSERT(!tweakAlpha || gp.hasVertexCoverage());
108
0
            SkASSERT(!tweakAlpha || !coverageNeedsSaturate);
109
110
            // Setup pass through color
111
0
            fragBuilder->codeAppendf("half4 %s;", args.fOutputColor);
112
0
            if (gp.hasVertexColor() || tweakAlpha) {
113
0
                GrGLSLVarying varying(kHalf4_GrSLType);
114
0
                varyingHandler->addVarying("color", &varying);
115
116
                // Start with the attribute or with uniform color
117
0
                if (gp.hasVertexColor()) {
118
0
                    vertBuilder->codeAppendf("half4 color = %s;", gp.fInColor.name());
119
0
                } else {
120
0
                    const char* colorUniformName;
121
0
                    fColorUniform = uniformHandler->addUniform(nullptr,
122
0
                                                               kVertex_GrShaderFlag,
123
0
                                                               kHalf4_GrSLType,
124
0
                                                               "Color",
125
0
                                                               &colorUniformName);
126
0
                    vertBuilder->codeAppendf("half4 color = %s;", colorUniformName);
127
0
                }
128
129
                // Optionally fold coverage into alpha (color).
130
0
                if (tweakAlpha) {
131
0
                    vertBuilder->codeAppendf("color = color * %s;", gp.fInCoverage.name());
132
0
                }
133
0
                vertBuilder->codeAppendf("%s = color;\n", varying.vsOut());
134
0
                fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, varying.fsIn());
135
0
            } else {
136
0
                this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor,
137
0
                                        &fColorUniform);
138
0
            }
139
140
            // Setup position
141
0
            WriteOutputPosition(vertBuilder,
142
0
                                uniformHandler,
143
0
                                *args.fShaderCaps,
144
0
                                gpArgs,
145
0
                                gp.fInPosition.name(),
146
0
                                gp.fViewMatrix,
147
0
                                &fViewMatrixUniform);
148
149
            // emit transforms using either explicit local coords or positions
150
0
            if (gp.fInLocalCoords.isInitialized()) {
151
0
                SkASSERT(gp.fLocalMatrix.isIdentity());
152
0
                gpArgs->fLocalCoordVar = gp.fInLocalCoords.asShaderVar();
153
0
            } else if (gp.fLocalCoordsWillBeRead) {
154
0
                WriteLocalCoord(vertBuilder,
155
0
                                uniformHandler,
156
0
                                *args.fShaderCaps,
157
0
                                gpArgs,
158
0
                                gp.fInPosition.asShaderVar(),
159
0
                                gp.fLocalMatrix,
160
0
                                &fLocalMatrixUniform);
161
0
            }
162
163
            // Setup coverage as pass through
164
0
            if (gp.hasVertexCoverage() && !tweakAlpha) {
165
0
                fragBuilder->codeAppendf("half alpha = 1.0;");
166
0
                varyingHandler->addPassThroughAttribute(gp.fInCoverage.asShaderVar(), "alpha");
167
0
                if (coverageNeedsSaturate) {
168
0
                    fragBuilder->codeAppendf("half4 %s = half4(saturate(alpha));",
169
0
                                             args.fOutputCoverage);
170
0
                } else {
171
0
                    fragBuilder->codeAppendf("half4 %s = half4(alpha);", args.fOutputCoverage);
172
0
                }
173
0
            } else if (gp.fCoverage == 0xff) {
174
0
                fragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage);
175
0
            } else {
176
0
                const char* fragCoverage;
177
0
                fCoverageUniform = uniformHandler->addUniform(nullptr,
178
0
                                                              kFragment_GrShaderFlag,
179
0
                                                              kHalf_GrSLType,
180
0
                                                              "Coverage",
181
0
                                                              &fragCoverage);
182
0
                fragBuilder->codeAppendf("half4 %s = half4(%s);",
183
0
                                         args.fOutputCoverage, fragCoverage);
184
0
            }
185
0
        }
Unexecuted instantiation: DefaultGeoProc::Impl::onEmitCode(GrGeometryProcessor::ProgramImpl::EmitArgs&, GrGeometryProcessor::ProgramImpl::GrGPArgs*)
Unexecuted instantiation: DefaultGeoProc::Impl::onEmitCode(GrGeometryProcessor::ProgramImpl::EmitArgs&, GrGeometryProcessor::ProgramImpl::GrGPArgs*)
186
187
        SkMatrix    fViewMatrixPrev  = SkMatrix::InvalidMatrix();
188
        SkMatrix    fLocalMatrixPrev = SkMatrix::InvalidMatrix();
189
        SkPMColor4f fColor           = SK_PMColor4fILLEGAL;
190
        uint8_t     fCoverage        = 0xFF;
191
192
        UniformHandle fViewMatrixUniform;
193
        UniformHandle fLocalMatrixUniform;
194
        UniformHandle fColorUniform;
195
        UniformHandle fCoverageUniform;
196
    };
197
198
0
    bool hasVertexColor() const { return fInColor.isInitialized(); }
199
0
    bool hasVertexCoverage() const { return fInCoverage.isInitialized(); }
200
201
    DefaultGeoProc(uint32_t gpTypeFlags,
202
                   const SkPMColor4f& color,
203
                   const SkMatrix& viewMatrix,
204
                   const SkMatrix& localMatrix,
205
                   uint8_t coverage,
206
                   bool localCoordsWillBeRead)
207
            : INHERITED(kDefaultGeoProc_ClassID)
208
            , fColor(color)
209
            , fViewMatrix(viewMatrix)
210
            , fLocalMatrix(localMatrix)
211
            , fCoverage(coverage)
212
            , fFlags(gpTypeFlags)
213
19.4k
            , fLocalCoordsWillBeRead(localCoordsWillBeRead) {
214
19.4k
        fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
215
19.4k
        if (fFlags & kColorAttribute_GPFlag) {
216
250
            fInColor = MakeColorAttribute("inColor",
217
250
                                          SkToBool(fFlags & kColorAttributeIsWide_GPFlag));
218
250
        }
219
19.4k
        if (fFlags & kLocalCoordAttribute_GPFlag) {
220
6
            fInLocalCoords = {"inLocalCoord", kFloat2_GrVertexAttribType,
221
6
                                              kFloat2_GrSLType};
222
6
        }
223
19.4k
        if (fFlags & kCoverageAttribute_GPFlag) {
224
11.3k
            fInCoverage = {"inCoverage", kFloat_GrVertexAttribType, kHalf_GrSLType};
225
11.3k
        }
226
19.4k
        this->setVertexAttributes(&fInPosition, 4);
227
19.4k
    }
228
229
    Attribute fInPosition;
230
    Attribute fInColor;
231
    Attribute fInLocalCoords;
232
    Attribute fInCoverage;
233
    SkPMColor4f fColor;
234
    SkMatrix fViewMatrix;
235
    SkMatrix fLocalMatrix;
236
    uint8_t fCoverage;
237
    uint32_t fFlags;
238
    bool fLocalCoordsWillBeRead;
239
240
    GR_DECLARE_GEOMETRY_PROCESSOR_TEST
241
242
    using INHERITED = GrGeometryProcessor;
243
};
244
245
GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DefaultGeoProc);
246
247
#if GR_TEST_UTILS
248
0
GrGeometryProcessor* DefaultGeoProc::TestCreate(GrProcessorTestData* d) {
249
0
    uint32_t flags = 0;
250
0
    if (d->fRandom->nextBool()) {
251
0
        flags |= kColorAttribute_GPFlag;
252
0
    }
253
0
    if (d->fRandom->nextBool()) {
254
0
        flags |= kColorAttributeIsWide_GPFlag;
255
0
    }
256
0
    if (d->fRandom->nextBool()) {
257
0
        flags |= kCoverageAttribute_GPFlag;
258
0
        if (d->fRandom->nextBool()) {
259
0
            flags |= (d->fRandom->nextBool()) ? kCoverageAttributeTweak_GPFlag
260
0
                                              : kCoverageAttributeUnclamped_GPFlag;
261
0
        }
262
0
    }
263
0
    if (d->fRandom->nextBool()) {
264
0
        flags |= kLocalCoordAttribute_GPFlag;
265
0
    }
266
267
0
    GrColor color = GrTest::RandomColor(d->fRandom);
268
0
    SkMatrix viewMtx = GrTest::TestMatrix(d->fRandom);
269
0
    SkMatrix localMtx = GrTest::TestMatrix(d->fRandom);
270
0
    bool readsLocalCoords = d->fRandom->nextBool();
271
0
    uint8_t coverage = GrTest::RandomCoverage(d->fRandom);
272
0
    return DefaultGeoProc::Make(d->allocator(),
273
0
                                flags,
274
0
                                SkPMColor4f::FromBytes_RGBA(color),
275
0
                                viewMtx,
276
0
                                localMtx,
277
0
                                readsLocalCoords,
278
0
                                coverage);
279
0
}
280
#endif
281
282
GrGeometryProcessor* GrDefaultGeoProcFactory::Make(SkArenaAlloc* arena,
283
                                                   const Color& color,
284
                                                   const Coverage& coverage,
285
                                                   const LocalCoords& localCoords,
286
19.4k
                                                   const SkMatrix& viewMatrix) {
287
19.4k
    uint32_t flags = 0;
288
19.4k
    if (Color::kPremulGrColorAttribute_Type == color.fType) {
289
250
        flags |= kColorAttribute_GPFlag;
290
19.1k
    } else if (Color::kPremulWideColorAttribute_Type == color.fType) {
291
0
        flags |= kColorAttribute_GPFlag | kColorAttributeIsWide_GPFlag;
292
0
    }
293
19.4k
    if (Coverage::kAttribute_Type == coverage.fType) {
294
11.0k
        flags |= kCoverageAttribute_GPFlag;
295
8.41k
    } else if (Coverage::kAttributeTweakAlpha_Type == coverage.fType) {
296
339
        flags |= kCoverageAttribute_GPFlag | kCoverageAttributeTweak_GPFlag;
297
8.07k
    } else if (Coverage::kAttributeUnclamped_Type == coverage.fType) {
298
0
        flags |= kCoverageAttribute_GPFlag | kCoverageAttributeUnclamped_GPFlag;
299
0
    }
300
19.4k
    flags |= localCoords.fType == LocalCoords::kHasExplicit_Type ? kLocalCoordAttribute_GPFlag : 0;
301
302
19.4k
    uint8_t inCoverage = coverage.fCoverage;
303
19.4k
    bool localCoordsWillBeRead = localCoords.fType != LocalCoords::kUnused_Type;
304
305
19.4k
    return DefaultGeoProc::Make(arena,
306
19.4k
                                flags,
307
19.4k
                                color.fColor,
308
19.4k
                                viewMatrix,
309
11.2k
                                localCoords.fMatrix ? *localCoords.fMatrix : SkMatrix::I(),
310
19.4k
                                localCoordsWillBeRead,
311
19.4k
                                inCoverage);
312
19.4k
}
313
314
GrGeometryProcessor* GrDefaultGeoProcFactory::MakeForDeviceSpace(SkArenaAlloc* arena,
315
                                                                 const Color& color,
316
                                                                 const Coverage& coverage,
317
                                                                 const LocalCoords& localCoords,
318
8.86k
                                                                 const SkMatrix& viewMatrix) {
319
8.86k
    SkMatrix invert = SkMatrix::I();
320
8.86k
    if (LocalCoords::kUnused_Type != localCoords.fType) {
321
1.60k
        SkASSERT(LocalCoords::kUsePosition_Type == localCoords.fType);
322
1.60k
        if (!viewMatrix.isIdentity() && !viewMatrix.invert(&invert)) {
323
0
            return nullptr;
324
0
        }
325
326
1.60k
        if (localCoords.hasLocalMatrix()) {
327
0
            invert.postConcat(*localCoords.fMatrix);
328
0
        }
329
1.60k
    }
330
331
8.86k
    LocalCoords inverted(LocalCoords::kUsePosition_Type, &invert);
332
8.86k
    return Make(arena, color, coverage, inverted, SkMatrix::I());
333
8.86k
}