Flutter Engine
The Flutter Engine
PrimitiveProcessorTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2016 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// This is a GPU-backend specific test. It relies on static initializers to work
9
11#include "include/core/SkRect.h"
20#include "src/gpu/KeyBuilder.h"
39#include "tests/Test.h"
40
41#include <cstddef>
42#include <memory>
43#include <utility>
44
45class GrDstProxyView;
50enum class GrXferBarrierFlags;
51struct GrContextOptions;
52struct GrShaderCaps;
53struct GrSimpleMesh;
54struct SkPoint;
55
56namespace {
57class Op : public GrMeshDrawOp {
58public:
60
61 const char* name() const override { return "Test Op"; }
62
63 static GrOp::Owner Make(GrRecordingContext* rContext, int numAttribs) {
64 return GrOp::Make<Op>(rContext, numAttribs);
65 }
66
67 FixedFunctionFlags fixedFunctionFlags() const override {
69 }
70
71 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, GrClampType) override {
73 }
74
75private:
76 friend class ::GrOp;
77
78 Op(int numAttribs) : INHERITED(ClassID()), fNumAttribs(numAttribs) {
79 this->setBounds(SkRect::MakeWH(1.f, 1.f), HasAABloat::kNo, IsHairline::kNo);
80 }
81
82 GrProgramInfo* programInfo() override { return fProgramInfo; }
83
84 void onCreateProgramInfo(const GrCaps* caps,
85 SkArenaAlloc* arena,
86 const GrSurfaceProxyView& writeView,
87 bool usesMSAASurface,
88 GrAppliedClip&& appliedClip,
89 const GrDstProxyView& dstProxyView,
90 GrXferBarrierFlags renderPassXferBarriers,
91 GrLoadOp colorLoadOp) override {
92 class GP : public GrGeometryProcessor {
93 public:
94 static GrGeometryProcessor* Make(SkArenaAlloc* arena, int numAttribs) {
95 return arena->make([&](void* ptr) {
96 return new (ptr) GP(numAttribs);
97 });
98 }
99
100 const char* name() const override { return "Test GP"; }
101
102 std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const override {
103 class Impl : public ProgramImpl {
104 public:
105 void setData(const GrGLSLProgramDataManager&,
106 const GrShaderCaps&,
107 const GrGeometryProcessor&) override {}
108
109 private:
110 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
111 const GP& gp = args.fGeomProc.cast<GP>();
112 args.fVaryingHandler->emitAttributes(gp);
113 WriteOutputPosition(args.fVertBuilder, gpArgs, gp.fAttributes[0].name());
114 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
115 fragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputColor);
116 fragBuilder->codeAppendf("const half4 %s = half4(1);",
117 args.fOutputCoverage);
118 }
119 };
120
121 return std::make_unique<Impl>();
122 }
123 void addToKey(const GrShaderCaps&, skgpu::KeyBuilder* builder) const override {
124 builder->add32(fNumAttribs);
125 }
126
127 private:
128 GP(int numAttribs) : INHERITED(kGP_ClassID), fNumAttribs(numAttribs) {
129 SkASSERT(numAttribs > 1);
130 fAttribNames = std::make_unique<SkString[]>(numAttribs);
131 fAttributes = std::make_unique<Attribute[]>(numAttribs);
132 for (auto i = 0; i < numAttribs; ++i) {
133 fAttribNames[i].printf("attr%d", i);
134 // This gives us more of a mix of attribute types, and allows the
135 // component count to fit within the limits for iOS Metal.
136 if (i & 0x1) {
137 fAttributes[i] = {fAttribNames[i].c_str(), kFloat_GrVertexAttribType,
139 } else {
140 fAttributes[i] = {fAttribNames[i].c_str(), kFloat2_GrVertexAttribType,
142 }
143 }
144 this->setVertexAttributesWithImplicitOffsets(fAttributes.get(), numAttribs);
145 }
146
147 int fNumAttribs;
148 std::unique_ptr<SkString[]> fAttribNames;
149 std::unique_ptr<Attribute[]> fAttributes;
150
152 };
153
154 GrGeometryProcessor* gp = GP::Make(arena, fNumAttribs);
155
157 arena,
158 writeView,
159 usesMSAASurface,
160 std::move(appliedClip),
161 dstProxyView,
162 gp,
165 renderPassXferBarriers,
166 colorLoadOp,
168 }
169
170 void onPrepareDraws(GrMeshDrawTarget* target) override {
171 if (!fProgramInfo) {
172 this->createProgramInfo(target);
173 }
174
175 size_t vertexStride = fProgramInfo->geomProc().vertexStride();
176 QuadHelper helper(target, vertexStride, 1);
177 SkPoint* vertices = reinterpret_cast<SkPoint*>(helper.vertices());
178 SkPointPriv::SetRectTriStrip(vertices, 0.f, 0.f, 1.f, 1.f, vertexStride);
179 fMesh = helper.mesh();
180 }
181
182 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
183 if (!fProgramInfo || !fMesh) {
184 return;
185 }
186
187 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
188 flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
189 flushState->drawMesh(*fMesh);
190 }
191
192 int fNumAttribs;
193 GrSimpleMesh* fMesh = nullptr;
194 GrProgramInfo* fProgramInfo = nullptr;
195
196 using INHERITED = GrMeshDrawOp;
197};
198} // namespace
199
201 reporter,
202 ctxInfo,
204 auto dContext = ctxInfo.directContext();
205#if GR_GPU_STATS
206 GrGpu* gpu = dContext->priv().getGpu();
207#endif
208
209 auto sdc = skgpu::ganesh::SurfaceDrawContext::Make(dContext,
211 nullptr,
213 {1, 1},
215 /*label=*/{});
216 if (!sdc) {
217 ERRORF(reporter, "Could not create render target context.");
218 return;
219 }
220 int attribCnt = dContext->priv().caps()->maxVertexAttributes();
221 if (!attribCnt) {
222 ERRORF(reporter, "No attributes allowed?!");
223 return;
224 }
225 dContext->flushAndSubmit();
226 dContext->priv().resetGpuStats();
227#if GR_GPU_STATS
228 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0);
229 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0);
230#endif
231 // Adding discard to appease vulkan validation warning about loading uninitialized data on draw
232 sdc->discard();
233
234 GrPaint grPaint;
235 // This one should succeed.
236 sdc->addDrawOp(Op::Make(dContext, attribCnt));
237 dContext->flushAndSubmit();
238#if GR_GPU_STATS
239 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 1);
240 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0);
241#endif
242 dContext->priv().resetGpuStats();
243 sdc->addDrawOp(Op::Make(dContext, attribCnt + 1));
244 dContext->flushAndSubmit();
245#if GR_GPU_STATS
246 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0);
247 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 1);
248#endif
249}
reporter
Definition: FontMgrTest.cpp:39
#define DEFINE_OP_CLASS_ID
Definition: GrOp.h:64
GrClampType
Definition: GrTypesPriv.h:228
GrLoadOp
Definition: GrTypesPriv.h:155
@ kFloat2_GrVertexAttribType
Definition: GrTypesPriv.h:314
@ kFloat_GrVertexAttribType
Definition: GrTypesPriv.h:313
GrXferBarrierFlags
DEF_GANESH_TEST_FOR_ALL_CONTEXTS(VertexAttributeCount, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
#define SkASSERT(cond)
Definition: SkAssert.h:116
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
#define ERRORF(r,...)
Definition: Test.h:293
Definition: GrCaps.h:57
void codeAppendf(const char format[],...) SK_PRINTF_LIKE(2
size_t vertexStride() const
Definition: GrGpu.h:62
Stats * stats()
Definition: GrGpu.h:551
void drawMesh(const GrSimpleMesh &mesh)
void bindPipelineAndScissorClip(const GrProgramInfo &programInfo, const SkRect &drawBounds)
void bindTextures(const GrGeometryProcessor &geomProc, const GrSurfaceProxy &singleGeomProcTexture, const GrPipeline &pipeline)
std::unique_ptr< GrOp > Owner
Definition: GrOp.h:72
static constexpr Analysis EmptySetAnalysis()
static GrProcessorSet MakeEmptySet()
const GrPipeline & pipeline() const
Definition: GrProgramInfo.h:39
const GrGeometryProcessor & geomProc() const
Definition: GrProgramInfo.h:40
static GrProgramInfo * CreateProgramInfo(const GrCaps *, SkArenaAlloc *, const GrPipeline *, const GrSurfaceProxyView &writeView, bool usesMSAASurface, GrGeometryProcessor *, GrPrimitiveType, GrXferBarrierFlags renderPassXferBarriers, GrLoadOp colorLoadOp, const GrUserStencilSettings *=&GrUserStencilSettings::kUnused)
auto make(Ctor &&ctor) -> decltype(ctor(nullptr))
Definition: SkArenaAlloc.h:120
static void SetRectTriStrip(SkPoint v[], SkScalar l, SkScalar t, SkScalar r, SkScalar b, size_t stride)
Definition: SkPointPriv.h:113
static std::unique_ptr< SurfaceDrawContext > Make(GrRecordingContext *, GrColorType, sk_sp< GrSurfaceProxy >, sk_sp< SkColorSpace >, GrSurfaceOrigin, const SkSurfaceProps &)
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)
@ kNone
Definition: layer.h:53
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609