1 /*
2 * Copyright 2018 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 "gm.h"
9
10 #include "GrClip.h"
11 #include "GrContext.h"
12 #include "GrGpuCommandBuffer.h"
13 #include "GrMemoryPool.h"
14 #include "GrOpFlushState.h"
15 #include "GrRenderTargetContext.h"
16 #include "GrRenderTargetContextPriv.h"
17 #include "GrRenderTarget.h"
18 #include "glsl/GrGLSLFragmentShaderBuilder.h"
19 #include "glsl/GrGLSLGeometryProcessor.h"
20 #include "glsl/GrGLSLVarying.h"
21 #include "glsl/GrGLSLVertexGeoBuilder.h"
22
23 namespace skiagm {
24
25 static constexpr GrGeometryProcessor::Attribute gVertex =
26 {"vertex", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
27
28 /**
29 * This is a GPU-backend specific test. It ensures that SkSL properly identifies clockwise-winding
30 * triangles (sk_Clockwise), in terms of to Skia device space, in all backends and with all render
31 * target origins. We draw clockwise triangles green and counter-clockwise red.
32 */
33 class ClockwiseGM : public GM {
34 private:
onShortName()35 SkString onShortName() final { return SkString("clockwise"); }
onISize()36 SkISize onISize() override { return SkISize::Make(300, 200); }
37 void onDraw(SkCanvas*) override;
38 };
39
40 ////////////////////////////////////////////////////////////////////////////////////////////////////
41 // SkSL code.
42
43 class ClockwiseTestProcessor : public GrGeometryProcessor {
44 public:
ClockwiseTestProcessor(bool readSkFragCoord)45 ClockwiseTestProcessor(bool readSkFragCoord)
46 : GrGeometryProcessor(kClockwiseTestProcessor_ClassID)
47 , fReadSkFragCoord(readSkFragCoord) {
48 this->setVertexAttributes(&gVertex, 1);
49 }
name() const50 const char* name() const override { return "ClockwiseTestProcessor"; }
getGLSLProcessorKey(const GrShaderCaps &,GrProcessorKeyBuilder * b) const51 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {
52 b->add32(fReadSkFragCoord);
53 }
54 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
55
56 private:
57 const bool fReadSkFragCoord;
58
59 friend class GLSLClockwiseTestProcessor;
60 };
61
62 class GLSLClockwiseTestProcessor : public GrGLSLGeometryProcessor {
setData(const GrGLSLProgramDataManager & pdman,const GrPrimitiveProcessor &,FPCoordTransformIter && transformIter)63 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
64 FPCoordTransformIter&& transformIter) override {}
65
onEmitCode(EmitArgs & args,GrGPArgs * gpArgs)66 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
67 const ClockwiseTestProcessor& proc = args.fGP.cast<ClockwiseTestProcessor>();
68 args.fVaryingHandler->emitAttributes(proc);
69 gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex");
70 args.fFragBuilder->codeAppendf(
71 "%s = sk_Clockwise ? half4(0,1,0,1) : half4(1,0,0,1);", args.fOutputColor);
72 if (!proc.fReadSkFragCoord) {
73 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
74 } else {
75 // Verify layout(origin_upper_left) on gl_FragCoord does not affect gl_FrontFacing.
76 args.fFragBuilder->codeAppendf("%s = half4(min(sk_FragCoord.y, 1));",
77 args.fOutputCoverage);
78 }
79 }
80 };
81
createGLSLInstance(const GrShaderCaps &) const82 GrGLSLPrimitiveProcessor* ClockwiseTestProcessor::createGLSLInstance(
83 const GrShaderCaps&) const {
84 return new GLSLClockwiseTestProcessor;
85 }
86
87 ////////////////////////////////////////////////////////////////////////////////////////////////////
88 // Draw Op.
89
90 class ClockwiseTestOp : public GrDrawOp {
91 public:
92 DEFINE_OP_CLASS_ID
93
Make(GrContext * context,bool readSkFragCoord,int y=0)94 static std::unique_ptr<GrDrawOp> Make(GrContext* context, bool readSkFragCoord, int y = 0) {
95 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
96 return pool->allocate<ClockwiseTestOp>(readSkFragCoord, y);
97 }
98
99 private:
ClockwiseTestOp(bool readSkFragCoord,float y)100 ClockwiseTestOp(bool readSkFragCoord, float y)
101 : GrDrawOp(ClassID()), fReadSkFragCoord(readSkFragCoord), fY(y) {
102 this->setBounds(SkRect::MakeIWH(300, 100), HasAABloat::kNo, IsZeroArea::kNo);
103 }
104
name() const105 const char* name() const override { return "ClockwiseTestOp"; }
fixedFunctionFlags() const106 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
finalize(const GrCaps &,const GrAppliedClip *)107 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*) override {
108 return GrProcessorSet::EmptySetAnalysis();
109 }
onPrepare(GrOpFlushState *)110 void onPrepare(GrOpFlushState*) override {}
onExecute(GrOpFlushState * flushState,const SkRect & chainBounds)111 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
112 SkPoint vertices[4] = {
113 {100, fY},
114 {0, fY+100},
115 {0, fY},
116 {100, fY+100},
117 };
118 sk_sp<const GrBuffer> vertexBuffer(flushState->resourceProvider()->createBuffer(
119 sizeof(vertices), kVertex_GrBufferType, kStatic_GrAccessPattern,
120 GrResourceProvider::Flags::kNone, vertices));
121 if (!vertexBuffer) {
122 return;
123 }
124 GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kPlus);
125 GrMesh mesh(GrPrimitiveType::kTriangleStrip);
126 mesh.setNonIndexedNonInstanced(4);
127 mesh.setVertexData(std::move(vertexBuffer));
128 flushState->rtCommandBuffer()->draw(ClockwiseTestProcessor(fReadSkFragCoord), pipeline,
129 nullptr, nullptr, &mesh, 1, SkRect::MakeIWH(100, 100));
130 }
131
132 const bool fReadSkFragCoord;
133 const float fY;
134
135 friend class ::GrOpMemoryPool; // for ctor
136 };
137
138 ////////////////////////////////////////////////////////////////////////////////////////////////////
139 // Test.
140
onDraw(SkCanvas * canvas)141 void ClockwiseGM::onDraw(SkCanvas* canvas) {
142 GrContext* ctx = canvas->getGrContext();
143 GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext();
144 if (!ctx || !rtc) {
145 DrawGpuOnlyMessage(canvas);
146 return;
147 }
148
149 rtc->clear(nullptr, { 0, 0, 0, 1 }, GrRenderTargetContext::CanClearFullscreen::kYes);
150
151 // Draw the test directly to the frame buffer.
152 rtc->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
153 rtc->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
154
155 // Draw the test to an off-screen, top-down render target.
156 if (auto topLeftRTC = ctx->contextPriv().makeDeferredRenderTargetContext(
157 rtc->asSurfaceProxy()->backendFormat(), SkBackingFit::kExact, 100, 200,
158 rtc->asSurfaceProxy()->config(), nullptr, 1, GrMipMapped::kNo,
159 kTopLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes)) {
160 topLeftRTC->clear(nullptr, SK_PMColor4fTRANSPARENT,
161 GrRenderTargetContext::CanClearFullscreen::kYes);
162 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
163 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
164 rtc->drawTexture(GrNoClip(), sk_ref_sp(topLeftRTC->asTextureProxy()),
165 GrSamplerState::Filter::kNearest, SK_PMColor4fWHITE, {0, 0, 100, 200},
166 {100, 0, 200, 200}, GrQuadAAFlags::kNone,
167 SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint, SkMatrix::I(),
168 nullptr);
169 }
170
171 // Draw the test to an off-screen, bottom-up render target.
172 if (auto topLeftRTC = ctx->contextPriv().makeDeferredRenderTargetContext(
173 rtc->asSurfaceProxy()->backendFormat(), SkBackingFit::kExact, 100, 200,
174 rtc->asSurfaceProxy()->config(), nullptr, 1, GrMipMapped::kNo,
175 kBottomLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes)) {
176 topLeftRTC->clear(nullptr, SK_PMColor4fTRANSPARENT,
177 GrRenderTargetContext::CanClearFullscreen::kYes);
178 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
179 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
180 rtc->drawTexture(GrNoClip(), sk_ref_sp(topLeftRTC->asTextureProxy()),
181 GrSamplerState::Filter::kNearest, SK_PMColor4fWHITE, {0, 0, 100, 200},
182 {200, 0, 300, 200}, GrQuadAAFlags::kNone,
183 SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint, SkMatrix::I(),
184 nullptr);
185 }
186 }
187
188 ////////////////////////////////////////////////////////////////////////////////////////////////////
189
190 DEF_GM( return new ClockwiseGM(); )
191
192 }
193