Flutter Engine
The Flutter Engine
TestOps.cpp
Go to the documentation of this file.
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
12#include "src/gpu/KeyBuilder.h"
22
23namespace {
24
25class GP : public GrGeometryProcessor {
26public:
27 GP(const SkMatrix& localMatrix, bool wideColor)
28 : GrGeometryProcessor(kTestRectOp_ClassID), fLocalMatrix(localMatrix) {
29 fInColor = MakeColorAttribute("color", wideColor);
30 this->setVertexAttributesWithImplicitOffsets(&fInPosition, 3);
31 }
32
33 const char* name() const override { return "TestRectOp::GP"; }
34
35 std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const override {
36 class Impl : public ProgramImpl {
37 public:
38 void setData(const GrGLSLProgramDataManager& pdman,
39 const GrShaderCaps& shaderCaps,
40 const GrGeometryProcessor& geomProc) override {
41 const auto& gp = geomProc.cast<GP>();
42 SetTransform(pdman, shaderCaps, fLocalMatrixUni, gp.fLocalMatrix);
43 }
44
45 private:
46 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
47 const auto& gp = args.fGeomProc.cast<GP>();
48 args.fVaryingHandler->emitAttributes(gp);
49 GrGLSLVarying colorVarying(SkSLType::kHalf4);
50 args.fVaryingHandler->addVarying("color", &colorVarying,
52 args.fVertBuilder->codeAppendf("%s = %s;", colorVarying.vsOut(), gp.fInColor.name());
53 args.fFragBuilder->codeAppendf("half4 %s = %s;",
54 args.fOutputColor, colorVarying.fsIn());
55 args.fFragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage);
56 WriteOutputPosition(args.fVertBuilder, gpArgs, gp.fInPosition.name());
57 WriteLocalCoord(args.fVertBuilder,
58 args.fUniformHandler,
59 *args.fShaderCaps,
60 gpArgs,
61 gp.fInLocalCoords.asShaderVar(),
62 gp.fLocalMatrix,
63 &fLocalMatrixUni);
64 }
65
66 UniformHandle fLocalMatrixUni;
67 };
68
69 return std::make_unique<Impl>();
70 }
71
72 void addToKey(const GrShaderCaps& shaderCaps, skgpu::KeyBuilder* b) const override {
73 b->add32(ProgramImpl::ComputeMatrixKey(shaderCaps, fLocalMatrix));
74 }
75
76 bool wideColor() const { return fInColor.cpuType() != kUByte4_norm_GrVertexAttribType; }
77
78private:
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
86class TestRectOp final : public GrMeshDrawOp {
87public:
89 GrPaint&&,
90 const SkRect& drawRect,
91 const SkRect& localRect,
92 const SkMatrix& localM);
93
94 const char* name() const override { return "TestRectOp"; }
95
96 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
97
99 const GrAppliedClip*,
100 GrClampType) override;
101
102 void visitProxies(const GrVisitProxyFunc& func) const override {
103 if (fProgramInfo) {
104 fProgramInfo->visitFPProxies(func);
105 } else {
106 fProcessorSet.visitProxies(func);
107 }
108 }
109
110private:
112
113 TestRectOp(const GrCaps*,
114 GrPaint&&,
115 const SkRect& drawRect,
116 const SkRect& localRect,
117 const SkMatrix& localMatrix);
118
119 GrProgramInfo* programInfo() override { return fProgramInfo; }
120 void onCreateProgramInfo(const GrCaps*,
122 const GrSurfaceProxyView& writeView,
123 bool usesMSAASurface,
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
147 GrPaint&& paint,
148 const SkRect& drawRect,
149 const SkRect& localRect,
150 const SkMatrix& localM) {
151 const auto* caps = context->priv().caps();
152 return GrOp::Make<TestRectOp>(context, caps, std::move(paint), drawRect, localRect, localM);
153}
154
155GrProcessorSet::Analysis TestRectOp::finalize(const GrCaps& caps,
156 const GrAppliedClip* clip,
157 GrClampType clampType) {
160 &GrUserStencilSettings::kUnused, caps, clampType, &fColor);
161}
162
163static bool use_wide_color(const GrPaint& paint, const GrCaps* caps) {
164 return !paint.getColor4f().fitsInBytes() && caps->halfFloatVertexAttributeSupport();
165}
166TestRectOp::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 , fProcessorSet(std::move(paint)) {
177 this->setBounds(drawRect.makeSorted(), HasAABloat::kNo, IsHairline::kNo);
178}
179
180void 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 GrLoadOp colorLoadOp) {
189 arena,
190 writeView,
191 usesMSAASurface,
192 std::move(appliedClip),
193 dstProxyView,
194 &fGP,
195 std::move(fProcessorSet),
197 renderPassXferBarriers,
198 colorLoadOp,
200}
201
202void TestRectOp::onPrepareDraws(GrMeshDrawTarget* target) {
203 QuadHelper helper(target, fGP.vertexStride(), 1);
204 skgpu::VertexWriter writer{helper.vertices()};
207 skgpu::VertexColor color(fColor, fGP.wideColor());
208 writer.writeQuad(pos, local, color);
209
210 fMesh = helper.mesh();
211}
212
213void TestRectOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
214 if (!fProgramInfo) {
215 this->createProgramInfo(flushState);
216 }
217
218 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
219 flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
220 flushState->drawMesh(*fMesh);
221}
222
223} // anonymous namespace
224
226
228 GrPaint&& paint,
229 const SkRect& drawRect,
230 const SkRect& localRect,
231 const SkMatrix& localM) {
232 return TestRectOp::Make(context, std::move(paint), drawRect, localRect, localM);
233}
234
236 std::unique_ptr<GrFragmentProcessor> fp,
237 const SkRect& drawRect,
238 const SkRect& localRect,
239 const SkMatrix& localM) {
241 paint.setColorFragmentProcessor(std::move(fp));
242 return TestRectOp::Make(context, std::move(paint), drawRect, localRect, localM);
243}
244
246 GrPaint&& paint,
247 const SkRect& rect) {
248 return TestRectOp::Make(context, std::move(paint), rect, rect, SkMatrix::I());
249}
250
251} // namespace sk_gpu_test::test_ops
#define DEFINE_OP_CLASS_ID
Definition: GrOp.h:64
GrClampType
Definition: GrTypesPriv.h:228
std::function< void(GrSurfaceProxy *, skgpu::Mipmapped)> GrVisitProxyFunc
Definition: GrTypesPriv.h:943
GrLoadOp
Definition: GrTypesPriv.h:155
@ kFloat2_GrVertexAttribType
Definition: GrTypesPriv.h:314
@ kUByte4_norm_GrVertexAttribType
Definition: GrTypesPriv.h:334
GrXferBarrierFlags
SkPoint pos
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition: SkPath.cpp:3892
const GrCaps * caps() const
Definition: GrCaps.h:57
bool halfFloatVertexAttributeSupport() const
Definition: GrCaps.h:108
virtual FixedFunctionFlags fixedFunctionFlags() const
Definition: GrDrawOp.h:112
virtual GrProcessorSet::Analysis finalize(const GrCaps &, const GrAppliedClip *, GrClampType)=0
virtual std::unique_ptr< ProgramImpl > makeProgramImpl(const GrShaderCaps &) const =0
static Attribute MakeColorAttribute(const char *name, bool wideColor)
void setVertexAttributesWithImplicitOffsets(const Attribute *attrs, int attrCount)
virtual GrProgramInfo * programInfo()=0
virtual void onCreateProgramInfo(const GrCaps *, SkArenaAlloc *, const GrSurfaceProxyView &writeView, bool usesMSAASurface, GrAppliedClip &&, const GrDstProxyView &, GrXferBarrierFlags renderPassXferBarriers, GrLoadOp colorLoadOp)=0
virtual void onPrepareDraws(GrMeshDrawTarget *)=0
void drawMesh(const GrSimpleMesh &mesh)
void bindPipelineAndScissorClip(const GrProgramInfo &programInfo, const SkRect &drawBounds)
void bindTextures(const GrGeometryProcessor &geomProc, const GrSurfaceProxy &singleGeomProcTexture, const GrPipeline &pipeline)
virtual void onExecute(GrOpFlushState *, const SkRect &chainBounds)=0
static Owner Make(GrRecordingContext *context, Args &&... args)
Definition: GrOp.h:75
std::unique_ptr< GrOp > Owner
Definition: GrOp.h:72
virtual const char * name() const =0
virtual void visitProxies(const GrVisitProxyFunc &) const
Definition: GrOp.h:95
void visitProxies(const GrVisitProxyFunc &) const
Analysis finalize(const GrProcessorAnalysisColor &, const GrProcessorAnalysisCoverage, const GrAppliedClip *, const GrUserStencilSettings *, const GrCaps &, GrClampType, SkPMColor4f *inputColorOverride)
const T & cast() const
Definition: GrProcessor.h:127
virtual const char * name() const =0
const GrPipeline & pipeline() const
Definition: GrProgramInfo.h:39
const GrGeometryProcessor & geomProc() const
Definition: GrProgramInfo.h:40
void visitFPProxies(const GrVisitProxyFunc &func) const
Definition: GrProgramInfo.h:64
GrRecordingContextPriv priv()
static GrProgramInfo * CreateProgramInfo(const GrCaps *, SkArenaAlloc *, const GrPipeline *, const GrSurfaceProxyView &writeView, bool usesMSAASurface, GrGeometryProcessor *, GrPrimitiveType, GrXferBarrierFlags renderPassXferBarriers, GrLoadOp colorLoadOp, const GrUserStencilSettings *=&GrUserStencilSettings::kUnused)
static const SkMatrix & I()
Definition: SkMatrix.cpp:1544
const Paint & paint
Definition: color_source.cc:38
DlColor color
static bool b
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
uint32_t * target
SK_API sk_sp< SkDocument > Make(SkWStream *dst, const SkSerialProcs *=nullptr, std::function< void(const SkPicture *)> onEndPage=nullptr)
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
const uint32_t fp
GrOp::Owner MakeRect(GrRecordingContext *context, GrPaint &&paint, const SkRect &drawRect, const SkRect &localRect, const SkMatrix &localM)
Definition: TestOps.cpp:227
Definition: ref_ptr.h:256
static const GrUserStencilSettings & kUnused
SkRect makeSorted() const
Definition: SkRect.h:1330
static TriStrip< float > TriStripFromRect(const SkRect &r)
Definition: BufferWriter.h:217