• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 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 "include/core/SkTypes.h"
9 #include "tests/Test.h"
10 
11 #include "include/gpu/GrDirectContext.h"
12 #include "include/gpu/GrRecordingContext.h"
13 #include "src/gpu/GrColor.h"
14 #include "src/gpu/GrDirectContextPriv.h"
15 #include "src/gpu/GrGeometryProcessor.h"
16 #include "src/gpu/GrImageInfo.h"
17 #include "src/gpu/GrMemoryPool.h"
18 #include "src/gpu/GrOpFlushState.h"
19 #include "src/gpu/GrOpsRenderPass.h"
20 #include "src/gpu/GrProgramInfo.h"
21 #include "src/gpu/GrRecordingContextPriv.h"
22 #include "src/gpu/GrResourceProvider.h"
23 #include "src/gpu/GrSurfaceDrawContext.h"
24 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
25 #include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
26 #include "src/gpu/glsl/GrGLSLVarying.h"
27 #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
28 
29 /**
30  * This is a GPU-backend specific test for dynamic pipeline state. It draws boxes using dynamic
31  * scissor rectangles then reads back the result to verify a successful test.
32  */
33 
34 static constexpr int kScreenSize = 6;
35 static constexpr int kNumMeshes = 4;
36 static constexpr int kScreenSplitX = kScreenSize/2;
37 static constexpr int kScreenSplitY = kScreenSize/2;
38 
39 static const SkIRect kDynamicScissors[kNumMeshes] = {
40     SkIRect::MakeLTRB(0,              0,              kScreenSplitX,  kScreenSplitY),
41     SkIRect::MakeLTRB(0,              kScreenSplitY,  kScreenSplitX,  kScreenSize),
42     SkIRect::MakeLTRB(kScreenSplitX,  0,              kScreenSize,    kScreenSplitY),
43     SkIRect::MakeLTRB(kScreenSplitX,  kScreenSplitY,  kScreenSize,    kScreenSize),
44 };
45 
46 static const GrColor kMeshColors[kNumMeshes] {
47     GrColorPackRGBA(255, 0, 0, 255),
48     GrColorPackRGBA(0, 255, 0, 255),
49     GrColorPackRGBA(0, 0, 255, 255),
50     GrColorPackRGBA(0, 0, 0, 255)
51 };
52 
53 struct Vertex {
54     float     fX;
55     float     fY;
56     GrColor   fColor;
57 };
58 
59 class GrPipelineDynamicStateTestProcessor : public GrGeometryProcessor {
60 public:
Make(SkArenaAlloc * arena)61     static GrGeometryProcessor* Make(SkArenaAlloc* arena) {
62         return arena->make([&](void* ptr) {
63             return new (ptr) GrPipelineDynamicStateTestProcessor();
64         });
65     }
66 
name() const67     const char* name() const override { return "GrPipelineDynamicStateTest Processor"; }
68 
getGLSLProcessorKey(const GrShaderCaps &,GrProcessorKeyBuilder *) const69     void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const final {}
70 
71     GrGLSLGeometryProcessor* createGLSLInstance(const GrShaderCaps&) const final;
72 
inVertex() const73     const Attribute& inVertex() const { return kAttributes[0]; }
inColor() const74     const Attribute& inColor() const { return kAttributes[1]; }
75 
76 private:
GrPipelineDynamicStateTestProcessor()77     GrPipelineDynamicStateTestProcessor()
78             : INHERITED(kGrPipelineDynamicStateTestProcessor_ClassID) {
79         this->setVertexAttributes(kAttributes, SK_ARRAY_COUNT(kAttributes));
80     }
81 
82     static constexpr Attribute kAttributes[] = {
83         {"vertex", kFloat2_GrVertexAttribType, kHalf2_GrSLType},
84         {"color", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType},
85     };
86 
87     friend class GLSLPipelineDynamicStateTestProcessor;
88     using INHERITED = GrGeometryProcessor;
89 };
90 constexpr GrGeometryProcessor::Attribute GrPipelineDynamicStateTestProcessor::kAttributes[];
91 
92 class GLSLPipelineDynamicStateTestProcessor : public GrGLSLGeometryProcessor {
setData(const GrGLSLProgramDataManager &,const GrShaderCaps &,const GrGeometryProcessor &)93     void setData(const GrGLSLProgramDataManager&,
94                  const GrShaderCaps&,
95                  const GrGeometryProcessor&) final {}
96 
onEmitCode(EmitArgs & args,GrGPArgs * gpArgs)97     void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final {
98         const GrPipelineDynamicStateTestProcessor& mp =
99             args.fGeomProc.cast<GrPipelineDynamicStateTestProcessor>();
100         GrGLSLVertexBuilder* v = args.fVertBuilder;
101         GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
102 
103         GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
104         varyingHandler->emitAttributes(mp);
105         f->codeAppendf("half4 %s;", args.fOutputColor);
106         varyingHandler->addPassThroughAttribute(mp.inColor(), args.fOutputColor);
107 
108         v->codeAppendf("float2 vertex = %s;", mp.inVertex().name());
109         gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex");
110         f->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage);
111     }
112 };
113 
114 GrGLSLGeometryProcessor*
createGLSLInstance(const GrShaderCaps &) const115 GrPipelineDynamicStateTestProcessor::createGLSLInstance(const GrShaderCaps&) const {
116     return new GLSLPipelineDynamicStateTestProcessor;
117 }
118 
119 class GrPipelineDynamicStateTestOp : public GrDrawOp {
120 public:
121     DEFINE_OP_CLASS_ID
122 
Make(GrRecordingContext * context,GrScissorTest scissorTest,sk_sp<const GrBuffer> vbuff)123     static GrOp::Owner Make(GrRecordingContext* context,
124                             GrScissorTest scissorTest,
125                             sk_sp<const GrBuffer> vbuff) {
126         return GrOp::Make<GrPipelineDynamicStateTestOp>(context, scissorTest, std::move(vbuff));
127     }
128 
129 private:
130     friend class GrOp;
131 
GrPipelineDynamicStateTestOp(GrScissorTest scissorTest,sk_sp<const GrBuffer> vbuff)132     GrPipelineDynamicStateTestOp(GrScissorTest scissorTest, sk_sp<const GrBuffer> vbuff)
133         : INHERITED(ClassID())
134         , fScissorTest(scissorTest)
135         , fVertexBuffer(std::move(vbuff)) {
136         this->setBounds(SkRect::MakeIWH(kScreenSize, kScreenSize),
137                         HasAABloat::kNo, IsHairline::kNo);
138     }
139 
name() const140     const char* name() const override { return "GrPipelineDynamicStateTestOp"; }
fixedFunctionFlags() const141     FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
finalize(const GrCaps &,const GrAppliedClip *,GrClampType)142     GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, GrClampType) override {
143         return GrProcessorSet::EmptySetAnalysis();
144     }
onPrePrepare(GrRecordingContext *,const GrSurfaceProxyView & writeView,GrAppliedClip *,const GrXferProcessor::DstProxyView &,GrXferBarrierFlags renderPassXferBarriers,GrLoadOp colorLoadOp)145     void onPrePrepare(GrRecordingContext*,
146                       const GrSurfaceProxyView& writeView,
147                       GrAppliedClip*,
148                       const GrXferProcessor::DstProxyView&,
149                       GrXferBarrierFlags renderPassXferBarriers,
150                       GrLoadOp colorLoadOp) override {}
onPrepare(GrOpFlushState *)151     void onPrepare(GrOpFlushState*) override {}
onExecute(GrOpFlushState * flushState,const SkRect & chainBounds)152     void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
153         GrPipeline pipeline(fScissorTest, SkBlendMode::kSrc,
154                             flushState->drawOpArgs().writeView().swizzle());
155         SkSTArray<kNumMeshes, GrSimpleMesh> meshes;
156         for (int i = 0; i < kNumMeshes; ++i) {
157             GrSimpleMesh& mesh = meshes.push_back();
158             mesh.set(fVertexBuffer, 4, 4 * i);
159         }
160 
161         auto geomProc = GrPipelineDynamicStateTestProcessor::Make(flushState->allocator());
162 
163         GrProgramInfo programInfo(flushState->writeView(),
164                                   &pipeline,
165                                   &GrUserStencilSettings::kUnused,
166                                   geomProc,
167                                   GrPrimitiveType::kTriangleStrip, 0,
168                                   flushState->renderPassBarriers(),
169                                   flushState->colorLoadOp());
170 
171         flushState->bindPipeline(programInfo, SkRect::MakeIWH(kScreenSize, kScreenSize));
172         for (int i = 0; i < 4; ++i) {
173             if (fScissorTest == GrScissorTest::kEnabled) {
174                 flushState->setScissorRect(kDynamicScissors[i]);
175             }
176             flushState->drawMesh(meshes[i]);
177         }
178     }
179 
180     GrScissorTest               fScissorTest;
181     const sk_sp<const GrBuffer> fVertexBuffer;
182 
183     using INHERITED = GrDrawOp;
184 };
185 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrPipelineDynamicStateTest,reporter,ctxInfo)186 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrPipelineDynamicStateTest, reporter, ctxInfo) {
187     auto dContext = ctxInfo.directContext();
188     GrResourceProvider* rp = dContext->priv().resourceProvider();
189 
190     auto rtc = GrSurfaceDrawContext::Make(
191             dContext, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact,
192             {kScreenSize, kScreenSize}, SkSurfaceProps());
193     if (!rtc) {
194         ERRORF(reporter, "could not create render target context.");
195         return;
196     }
197 
198     constexpr float d = (float) kScreenSize;
199     Vertex vdata[kNumMeshes * 4] = {
200         {0, 0, kMeshColors[0]},
201         {0, d, kMeshColors[0]},
202         {d, 0, kMeshColors[0]},
203         {d, d, kMeshColors[0]},
204 
205         {0, 0, kMeshColors[1]},
206         {0, d, kMeshColors[1]},
207         {d, 0, kMeshColors[1]},
208         {d, d, kMeshColors[1]},
209 
210         {0, 0, kMeshColors[2]},
211         {0, d, kMeshColors[2]},
212         {d, 0, kMeshColors[2]},
213         {d, d, kMeshColors[2]},
214 
215         {0, 0, kMeshColors[3]},
216         {0, d, kMeshColors[3]},
217         {d, 0, kMeshColors[3]},
218         {d, d, kMeshColors[3]}
219     };
220 
221     sk_sp<const GrBuffer> vbuff(rp->createBuffer(sizeof(vdata), GrGpuBufferType::kVertex,
222                                                  kDynamic_GrAccessPattern, vdata));
223     if (!vbuff) {
224         ERRORF(reporter, "vbuff is null.");
225         return;
226     }
227 
228     uint32_t resultPx[kScreenSize * kScreenSize];
229 
230     for (GrScissorTest scissorTest : {GrScissorTest::kEnabled, GrScissorTest::kDisabled}) {
231         rtc->clear(SkPMColor4f::FromBytes_RGBA(0xbaaaaaad));
232         rtc->addDrawOp(GrPipelineDynamicStateTestOp::Make(dContext, scissorTest, vbuff));
233         auto ii = SkImageInfo::Make(kScreenSize, kScreenSize,
234                                     kRGBA_8888_SkColorType, kPremul_SkAlphaType);
235         GrPixmap resultPM(ii, resultPx, kScreenSize*sizeof(uint32_t));
236         rtc->readPixels(dContext, resultPM, {0, 0});
237         for (int y = 0; y < kScreenSize; ++y) {
238             for (int x = 0; x < kScreenSize; ++x) {
239                 int expectedColorIdx;
240                 if (GrScissorTest::kEnabled == scissorTest) {
241                     expectedColorIdx = (x < kScreenSplitX ? 0 : 2) + (y < kScreenSplitY ? 0 : 1);
242                 } else {
243                     expectedColorIdx = kNumMeshes - 1;
244                 }
245                 uint32_t expected = kMeshColors[expectedColorIdx];
246                 uint32_t actual = resultPx[y * kScreenSize + x];
247                 if (expected != actual) {
248                     ERRORF(reporter, "[scissor=%s] pixel (%i,%i): got 0x%x expected 0x%x",
249                            GrScissorTest::kEnabled == scissorTest ? "enabled" : "disabled", x, y,
250                            actual, expected);
251                     return;
252                 }
253             }
254         }
255     }
256 }
257