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