Coverage Report

Created: 2024-05-20 07:14

/src/skia/tools/gpu/TestOps.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019 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 "tools/gpu/TestOps.h"
9
10
#include "src/core/SkPointPriv.h"
11
#include "src/gpu/BufferWriter.h"
12
#include "src/gpu/KeyBuilder.h"
13
#include "src/gpu/ganesh/GrCaps.h"
14
#include "src/gpu/ganesh/GrGeometryProcessor.h"
15
#include "src/gpu/ganesh/GrMemoryPool.h"
16
#include "src/gpu/ganesh/GrOpFlushState.h"
17
#include "src/gpu/ganesh/GrProgramInfo.h"
18
#include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h"
19
#include "src/gpu/ganesh/glsl/GrGLSLVarying.h"
20
#include "src/gpu/ganesh/glsl/GrGLSLVertexGeoBuilder.h"
21
#include "src/gpu/ganesh/ops/GrSimpleMeshDrawOpHelper.h"
22
23
namespace {
24
25
class GP : public GrGeometryProcessor {
26
public:
27
    GP(const SkMatrix& localMatrix, bool wideColor)
28
0
            : GrGeometryProcessor(kTestRectOp_ClassID), fLocalMatrix(localMatrix) {
29
0
        fInColor = MakeColorAttribute("color", wideColor);
30
0
        this->setVertexAttributesWithImplicitOffsets(&fInPosition, 3);
31
0
    }
32
33
0
    const char* name() const override { return "TestRectOp::GP"; }
34
35
0
    std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const override {
36
0
        class Impl : public ProgramImpl {
37
0
        public:
38
0
            void setData(const GrGLSLProgramDataManager& pdman,
39
0
                         const GrShaderCaps& shaderCaps,
40
0
                         const GrGeometryProcessor& geomProc) override {
41
0
                const auto& gp = geomProc.cast<GP>();
42
0
                SetTransform(pdman, shaderCaps, fLocalMatrixUni, gp.fLocalMatrix);
43
0
            }
44
45
0
        private:
46
0
            void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
47
0
                const auto& gp = args.fGeomProc.cast<GP>();
48
0
                args.fVaryingHandler->emitAttributes(gp);
49
0
                GrGLSLVarying colorVarying(SkSLType::kHalf4);
50
0
                args.fVaryingHandler->addVarying("color", &colorVarying,
51
0
                                                 GrGLSLVaryingHandler::Interpolation::kCanBeFlat);
52
0
                args.fVertBuilder->codeAppendf("%s = %s;", colorVarying.vsOut(), gp.fInColor.name());
53
0
                args.fFragBuilder->codeAppendf("half4 %s = %s;",
54
0
                                               args.fOutputColor, colorVarying.fsIn());
55
0
                args.fFragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage);
56
0
                WriteOutputPosition(args.fVertBuilder, gpArgs, gp.fInPosition.name());
57
0
                WriteLocalCoord(args.fVertBuilder,
58
0
                                args.fUniformHandler,
59
0
                                *args.fShaderCaps,
60
0
                                gpArgs,
61
0
                                gp.fInLocalCoords.asShaderVar(),
62
0
                                gp.fLocalMatrix,
63
0
                                &fLocalMatrixUni);
64
0
            }
65
66
0
            UniformHandle fLocalMatrixUni;
67
0
        };
68
69
0
        return std::make_unique<Impl>();
70
0
    }
71
72
0
    void addToKey(const GrShaderCaps& shaderCaps, skgpu::KeyBuilder* b) const override {
73
0
        b->add32(ProgramImpl::ComputeMatrixKey(shaderCaps, fLocalMatrix));
74
0
    }
75
76
0
    bool wideColor() const { return fInColor.cpuType() != kUByte4_norm_GrVertexAttribType; }
77
78
private:
79
    Attribute fInPosition    = {   "inPosition", kFloat2_GrVertexAttribType, SkSLType::kFloat2};
80
    Attribute fInLocalCoords = {"inLocalCoords", kFloat2_GrVertexAttribType, SkSLType::kFloat2};
81
    Attribute fInColor;
82
83
    SkMatrix fLocalMatrix;
84
};
85
86
class TestRectOp final : public GrMeshDrawOp {
87
public:
88
    static GrOp::Owner Make(GrRecordingContext*,
89
                            GrPaint&&,
90
                            const SkRect& drawRect,
91
                            const SkRect& localRect,
92
                            const SkMatrix& localM);
93
94
0
    const char* name() const override { return "TestRectOp"; }
95
96
0
    FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
97
98
    GrProcessorSet::Analysis finalize(const GrCaps&,
99
                                      const GrAppliedClip*,
100
                                      GrClampType) override;
101
102
0
    void visitProxies(const GrVisitProxyFunc& func) const override {
103
0
        if (fProgramInfo) {
104
0
            fProgramInfo->visitFPProxies(func);
105
0
        } else {
106
0
            fProcessorSet.visitProxies(func);
107
0
        }
108
0
    }
109
110
private:
111
    DEFINE_OP_CLASS_ID
112
113
    TestRectOp(const GrCaps*,
114
               GrPaint&&,
115
               const SkRect& drawRect,
116
               const SkRect& localRect,
117
               const SkMatrix& localMatrix);
118
119
0
    GrProgramInfo* programInfo() override { return fProgramInfo; }
120
    void onCreateProgramInfo(const GrCaps*,
121
                             SkArenaAlloc*,
122
                             const GrSurfaceProxyView& writeView,
123
                             bool usesMSAASurface,
124
                             GrAppliedClip&&,
125
                             const GrDstProxyView&,
126
                             GrXferBarrierFlags renderPassXferBarriers,
127
                             GrLoadOp colorLoadOp) override;
128
129
    void onPrepareDraws(GrMeshDrawTarget*) override;
130
    void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
131
132
    SkRect         fDrawRect;
133
    SkRect         fLocalRect;
134
    SkPMColor4f    fColor;
135
    GP             fGP;
136
    GrProcessorSet fProcessorSet;
137
138
    // If this op is prePrepared the created programInfo will be stored here for use in
139
    // onExecute. In the prePrepared case it will have been stored in the record-time arena.
140
    GrProgramInfo* fProgramInfo = nullptr;
141
    GrSimpleMesh*  fMesh        = nullptr;
142
143
    friend class ::GrOp;
144
};
145
146
GrOp::Owner TestRectOp::Make(GrRecordingContext* context,
147
                             GrPaint&& paint,
148
                             const SkRect& drawRect,
149
                             const SkRect& localRect,
150
0
                             const SkMatrix& localM) {
151
0
    const auto* caps = context->priv().caps();
152
0
    return GrOp::Make<TestRectOp>(context, caps, std::move(paint), drawRect, localRect, localM);
153
0
}
154
155
GrProcessorSet::Analysis TestRectOp::finalize(const GrCaps& caps,
156
                                              const GrAppliedClip* clip,
157
0
                                              GrClampType clampType) {
158
0
    return fProcessorSet.finalize(GrProcessorAnalysisColor::Opaque::kYes,
159
0
                                  GrProcessorAnalysisCoverage::kSingleChannel, clip,
160
0
                                  &GrUserStencilSettings::kUnused, caps, clampType, &fColor);
161
0
}
162
163
0
static bool use_wide_color(const GrPaint& paint, const GrCaps* caps) {
164
0
    return !paint.getColor4f().fitsInBytes() && caps->halfFloatVertexAttributeSupport();
165
0
}
166
TestRectOp::TestRectOp(const GrCaps* caps,
167
                       GrPaint&& paint,
168
                       const SkRect& drawRect,
169
                       const SkRect& localRect,
170
                       const SkMatrix& localMatrix)
171
        : GrMeshDrawOp(ClassID())
172
        , fDrawRect(drawRect)
173
        , fLocalRect(localRect)
174
        , fColor(paint.getColor4f())
175
        , fGP(localMatrix, use_wide_color(paint, caps))
176
0
        , fProcessorSet(std::move(paint)) {
177
0
    this->setBounds(drawRect.makeSorted(), HasAABloat::kNo, IsHairline::kNo);
178
0
}
179
180
void TestRectOp::onCreateProgramInfo(const GrCaps* caps,
181
                                     SkArenaAlloc* arena,
182
                                     const GrSurfaceProxyView& writeView,
183
                                     bool usesMSAASurface,
184
                                     GrAppliedClip&& appliedClip,
185
                                     const GrDstProxyView& dstProxyView,
186
                                     GrXferBarrierFlags renderPassXferBarriers,
187
0
                                     GrLoadOp colorLoadOp) {
188
0
    fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps,
189
0
                                                               arena,
190
0
                                                               writeView,
191
0
                                                               usesMSAASurface,
192
0
                                                               std::move(appliedClip),
193
0
                                                               dstProxyView,
194
0
                                                               &fGP,
195
0
                                                               std::move(fProcessorSet),
196
0
                                                               GrPrimitiveType::kTriangles,
197
0
                                                               renderPassXferBarriers,
198
0
                                                               colorLoadOp,
199
0
                                                               GrPipeline::InputFlags::kNone);
200
0
}
201
202
0
void TestRectOp::onPrepareDraws(GrMeshDrawTarget* target) {
203
0
    QuadHelper helper(target, fGP.vertexStride(), 1);
204
0
    skgpu::VertexWriter writer{helper.vertices()};
205
0
    auto pos = skgpu::VertexWriter::TriStripFromRect(fDrawRect);
206
0
    auto local = skgpu::VertexWriter::TriStripFromRect(fLocalRect);
207
0
    skgpu::VertexColor color(fColor, fGP.wideColor());
208
0
    writer.writeQuad(pos, local, color);
209
210
0
    fMesh = helper.mesh();
211
0
}
212
213
0
void TestRectOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
214
0
    if (!fProgramInfo) {
215
0
        this->createProgramInfo(flushState);
216
0
    }
217
218
0
    flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
219
0
    flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
220
0
    flushState->drawMesh(*fMesh);
221
0
}
222
223
}  // anonymous namespace
224
225
namespace sk_gpu_test::test_ops {
226
227
GrOp::Owner MakeRect(GrRecordingContext* context,
228
                     GrPaint&& paint,
229
                     const SkRect& drawRect,
230
                     const SkRect& localRect,
231
0
                     const SkMatrix& localM) {
232
0
    return TestRectOp::Make(context, std::move(paint), drawRect, localRect, localM);
233
0
}
234
235
GrOp::Owner MakeRect(GrRecordingContext* context,
236
                     std::unique_ptr<GrFragmentProcessor> fp,
237
                     const SkRect& drawRect,
238
                     const SkRect& localRect,
239
0
                     const SkMatrix& localM) {
240
0
    GrPaint paint;
241
0
    paint.setColorFragmentProcessor(std::move(fp));
242
0
    return TestRectOp::Make(context, std::move(paint), drawRect, localRect, localM);
243
0
}
244
245
GrOp::Owner MakeRect(GrRecordingContext* context,
246
                     GrPaint&& paint,
247
0
                     const SkRect& rect) {
248
0
    return TestRectOp::Make(context, std::move(paint), rect, rect, SkMatrix::I());
249
0
}
250
251
}  // namespace sk_gpu_test::test_ops