blob: 480252b19cd3e03972d5e84f13255988bc52c5bd [file] [log] [blame]
Chris Dalton49d14e92018-07-27 12:38:35 -06001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/gpu/GrContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040020#include "include/gpu/GrTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/private/GrRecordingContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040022#include "include/private/GrTypesPriv.h"
23#include "include/private/SkColorData.h"
24#include "src/gpu/GrBuffer.h"
25#include "src/gpu/GrCaps.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040026#include "src/gpu/GrColorSpaceXform.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040028#include "src/gpu/GrGeometryProcessor.h"
29#include "src/gpu/GrGpuBuffer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrMemoryPool.h"
31#include "src/gpu/GrOpFlushState.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040032#include "src/gpu/GrOpsRenderPass.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040033#include "src/gpu/GrPipeline.h"
34#include "src/gpu/GrPrimitiveProcessor.h"
35#include "src/gpu/GrProcessor.h"
36#include "src/gpu/GrProcessorSet.h"
Robert Phillips901aff02019-10-08 12:32:56 -040037#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/GrRecordingContextPriv.h"
39#include "src/gpu/GrRenderTargetContext.h"
40#include "src/gpu/GrRenderTargetContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040041#include "src/gpu/GrResourceProvider.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040042#include "src/gpu/GrSamplerState.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040043#include "src/gpu/GrShaderCaps.h"
44#include "src/gpu/GrShaderVar.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040045#include "src/gpu/GrSurfaceProxy.h"
46#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
48#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040049#include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050050#include "src/gpu/glsl/GrGLSLVarying.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040051#include "src/gpu/ops/GrDrawOp.h"
52#include "src/gpu/ops/GrOp.h"
Robert Phillips34cea002019-11-21 16:02:34 -050053#include "tools/gpu/ProxyUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040054
55#include <memory>
56#include <utility>
57
58class GrAppliedClip;
59class GrGLSLProgramDataManager;
Chris Dalton49d14e92018-07-27 12:38:35 -060060
Hal Canaryfa3305a2019-07-18 12:36:54 -040061namespace {
Chris Dalton49d14e92018-07-27 12:38:35 -060062
Brian Osmand4c29702018-09-14 16:16:55 -040063static constexpr GrGeometryProcessor::Attribute gVertex =
Stephen White1e1ad8d2019-06-19 14:19:47 -040064 {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Chris Dalton49d14e92018-07-27 12:38:35 -060065
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 */
Hal Canaryfa3305a2019-07-18 12:36:54 -040071class ClockwiseGM : public skiagm::GpuGM {
72 SkString onShortName() override { return SkString("clockwise"); }
73 SkISize onISize() override { return {300, 200}; }
Chris Dalton3a778372019-02-07 15:23:36 -070074 void onDraw(GrContext*, GrRenderTargetContext*, SkCanvas*) override;
Chris Dalton49d14e92018-07-27 12:38:35 -060075};
76
77////////////////////////////////////////////////////////////////////////////////////////////////////
78// SkSL code.
79
80class ClockwiseTestProcessor : public GrGeometryProcessor {
81public:
Robert Phillips5d07c522019-11-18 11:19:51 -050082 static GrGeometryProcessor* Make(SkArenaAlloc* arena, bool readSkFragCoord) {
83 return arena->make<ClockwiseTestProcessor>(readSkFragCoord);
Robert Phillipsdf70f152019-11-15 14:57:05 -050084 }
85
86 const char* name() const final { return "ClockwiseTestProcessor"; }
87
88 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {
89 b->add32(fReadSkFragCoord);
90 }
91
92 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
93
Robert Phillips5d07c522019-11-18 11:19:51 -050094 bool readSkFragCoord() const { return fReadSkFragCoord; }
95
Robert Phillipsdf70f152019-11-15 14:57:05 -050096private:
Robert Phillips5d07c522019-11-18 11:19:51 -050097 friend class ::SkArenaAlloc; // for access to ctor
98
Chris Dalton49d14e92018-07-27 12:38:35 -060099 ClockwiseTestProcessor(bool readSkFragCoord)
100 : GrGeometryProcessor(kClockwiseTestProcessor_ClassID)
101 , fReadSkFragCoord(readSkFragCoord) {
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500102 this->setVertexAttributes(&gVertex, 1);
Chris Dalton49d14e92018-07-27 12:38:35 -0600103 }
Chris Dalton49d14e92018-07-27 12:38:35 -0600104
Chris Dalton49d14e92018-07-27 12:38:35 -0600105 const bool fReadSkFragCoord;
106
Robert Phillips5d07c522019-11-18 11:19:51 -0500107 typedef GrGeometryProcessor INHERITED;
Chris Dalton49d14e92018-07-27 12:38:35 -0600108};
109
110class GLSLClockwiseTestProcessor : public GrGLSLGeometryProcessor {
111 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
Brian Salomonc241b582019-11-27 08:57:17 -0500112 const CoordTransformRange&) override {}
Chris Dalton49d14e92018-07-27 12:38:35 -0600113
114 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
115 const ClockwiseTestProcessor& proc = args.fGP.cast<ClockwiseTestProcessor>();
116 args.fVaryingHandler->emitAttributes(proc);
Stephen White1e1ad8d2019-06-19 14:19:47 -0400117 gpArgs->fPositionVar.set(kFloat2_GrSLType, "position");
Chris Dalton49d14e92018-07-27 12:38:35 -0600118 args.fFragBuilder->codeAppendf(
119 "%s = sk_Clockwise ? half4(0,1,0,1) : half4(1,0,0,1);", args.fOutputColor);
Robert Phillips5d07c522019-11-18 11:19:51 -0500120 if (!proc.readSkFragCoord()) {
Chris Dalton49d14e92018-07-27 12:38:35 -0600121 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
122 } else {
123 // Verify layout(origin_upper_left) on gl_FragCoord does not affect gl_FrontFacing.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500124 args.fFragBuilder->codeAppendf("%s = half4(min(half(sk_FragCoord.y), 1));",
Chris Dalton49d14e92018-07-27 12:38:35 -0600125 args.fOutputCoverage);
126 }
127 }
128};
129
130GrGLSLPrimitiveProcessor* ClockwiseTestProcessor::createGLSLInstance(
131 const GrShaderCaps&) const {
132 return new GLSLClockwiseTestProcessor;
133}
134
135////////////////////////////////////////////////////////////////////////////////////////////////////
136// Draw Op.
137
138class ClockwiseTestOp : public GrDrawOp {
139public:
140 DEFINE_OP_CLASS_ID
141
Robert Phillipsbe9aff22019-02-15 11:33:22 -0500142 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
143 bool readSkFragCoord, int y = 0) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500144 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Chris Dalton49d14e92018-07-27 12:38:35 -0600145 return pool->allocate<ClockwiseTestOp>(readSkFragCoord, y);
146 }
147
148private:
149 ClockwiseTestOp(bool readSkFragCoord, float y)
Robert Phillips5d07c522019-11-18 11:19:51 -0500150 : GrDrawOp(ClassID())
151 , fReadSkFragCoord(readSkFragCoord)
152 , fY(y) {
Greg Daniel5faf4742019-10-01 15:14:44 -0400153 this->setBounds(SkRect::MakeXYWH(0, fY, 100, 100), HasAABloat::kNo, IsHairline::kNo);
Chris Dalton49d14e92018-07-27 12:38:35 -0600154 }
155
156 const char* name() const override { return "ClockwiseTestOp"; }
157 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600158 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
159 bool hasMixedSampledCoverage, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -0700160 return GrProcessorSet::EmptySetAnalysis();
Chris Dalton49d14e92018-07-27 12:38:35 -0600161 }
Robert Phillipsdf70f152019-11-15 14:57:05 -0500162
Robert Phillipsac6156c2020-02-28 16:02:40 -0500163 GrProgramInfo* createProgramInfo(const GrCaps* caps,
164 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400165 const GrSurfaceProxyView* writeView,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500166 GrAppliedClip&& appliedClip,
167 const GrXferProcessor::DstProxyView& dstProxyView) const {
168 GrGeometryProcessor* geomProc = ClockwiseTestProcessor::Make(arena, fReadSkFragCoord);
169
Brian Salomon8afde5f2020-04-01 16:22:00 -0400170 return sk_gpu_test::CreateProgramInfo(caps, arena, writeView,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500171 std::move(appliedClip), dstProxyView,
172 geomProc, SkBlendMode::kPlus,
173 GrPrimitiveType::kTriangleStrip);
174 }
175
176 GrProgramInfo* createProgramInfo(GrOpFlushState* flushState) const {
177 return this->createProgramInfo(&flushState->caps(),
178 flushState->allocator(),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400179 flushState->writeView(),
Robert Phillipsac6156c2020-02-28 16:02:40 -0500180 flushState->detachAppliedClip(),
181 flushState->dstProxyView());
182 }
183
Robert Phillipsdf70f152019-11-15 14:57:05 -0500184 void onPrePrepare(GrRecordingContext* context,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400185 const GrSurfaceProxyView* writeView,
Robert Phillips34cea002019-11-21 16:02:34 -0500186 GrAppliedClip* clip,
Robert Phillips8053c972019-11-21 10:44:53 -0500187 const GrXferProcessor::DstProxyView& dstProxyView) final {
Robert Phillipsd4fb7c72019-11-15 17:28:37 -0500188 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
Robert Phillipsdf70f152019-11-15 14:57:05 -0500189
Robert Phillips34cea002019-11-21 16:02:34 -0500190 // This is equivalent to a GrOpFlushState::detachAppliedClip
191 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip();
Robert Phillipsdf70f152019-11-15 14:57:05 -0500192
Brian Salomon8afde5f2020-04-01 16:22:00 -0400193 fProgramInfo = this->createProgramInfo(context->priv().caps(), arena, writeView,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500194 std::move(appliedClip), dstProxyView);
Robert Phillipsdf70f152019-11-15 14:57:05 -0500195
Robert Phillipsac6156c2020-02-28 16:02:40 -0500196 context->priv().recordProgramInfo(fProgramInfo);
Robert Phillipsdf70f152019-11-15 14:57:05 -0500197 }
198
Greg Danielf793de12019-09-05 13:23:23 -0400199 void onPrepare(GrOpFlushState* flushState) override {
Chris Dalton49d14e92018-07-27 12:38:35 -0600200 SkPoint vertices[4] = {
201 {100, fY},
202 {0, fY+100},
203 {0, fY},
204 {100, fY+100},
205 };
Greg Danielf793de12019-09-05 13:23:23 -0400206 fVertexBuffer = flushState->resourceProvider()->createBuffer(
207 sizeof(vertices), GrGpuBufferType::kVertex, kStatic_GrAccessPattern, vertices);
208 }
Robert Phillipsdf70f152019-11-15 14:57:05 -0500209
Greg Danielf793de12019-09-05 13:23:23 -0400210 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
211 if (!fVertexBuffer) {
Chris Dalton79f99f72018-07-27 14:15:10 -0600212 return;
213 }
Robert Phillipsdf70f152019-11-15 14:57:05 -0500214
Robert Phillips34cea002019-11-21 16:02:34 -0500215 if (!fProgramInfo) {
Robert Phillipsac6156c2020-02-28 16:02:40 -0500216 fProgramInfo = this->createProgramInfo(flushState);
Robert Phillips34cea002019-11-21 16:02:34 -0500217 }
218
Chris Daltonaa0e45c2020-03-16 10:05:11 -0600219 flushState->bindPipeline(*fProgramInfo, SkRect::MakeXYWH(0, fY, 100, 100));
220 flushState->bindBuffers(nullptr, nullptr, fVertexBuffer.get());
221 flushState->draw(4, 0);
Chris Dalton49d14e92018-07-27 12:38:35 -0600222 }
223
Robert Phillips5d07c522019-11-18 11:19:51 -0500224 sk_sp<GrBuffer> fVertexBuffer;
225 const bool fReadSkFragCoord;
226 const float fY;
Robert Phillipsdf70f152019-11-15 14:57:05 -0500227
Robert Phillips5d07c522019-11-18 11:19:51 -0500228 // The program info (and both the GrPipeline and GrPrimitiveProcessor it relies on), when
Robert Phillips34cea002019-11-21 16:02:34 -0500229 // allocated, are allocated in either the ddl-record-time or flush-time arena. It is the
230 // arena's job to free up their memory so we just have a bare programInfo pointer here. We
231 // don't even store the GrPipeline and GrPrimitiveProcessor pointers here bc they are
232 // guaranteed to have the same lifetime as the program info.
Robert Phillips5d07c522019-11-18 11:19:51 -0500233 GrProgramInfo* fProgramInfo = nullptr;
Chris Dalton49d14e92018-07-27 12:38:35 -0600234
235 friend class ::GrOpMemoryPool; // for ctor
Robert Phillips5d07c522019-11-18 11:19:51 -0500236
237 typedef GrDrawOp INHERITED;
Chris Dalton49d14e92018-07-27 12:38:35 -0600238};
239
240////////////////////////////////////////////////////////////////////////////////////////////////////
241// Test.
242
Chris Dalton3a778372019-02-07 15:23:36 -0700243void ClockwiseGM::onDraw(GrContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas) {
Michael Ludwig81d41722020-05-26 16:57:38 -0400244 rtc->clear(SK_PMColor4fBLACK);
Chris Dalton49d14e92018-07-27 12:38:35 -0600245
246 // Draw the test directly to the frame buffer.
247 rtc->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
248 rtc->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
249
250 // Draw the test to an off-screen, top-down render target.
Greg Danielc594e622019-10-15 14:01:49 -0400251 GrColorType rtcColorType = rtc->colorInfo().colorType();
Greg Daniele20fcad2020-01-08 11:52:34 -0500252 if (auto topLeftRTC = GrRenderTargetContext::Make(
253 ctx, rtcColorType, nullptr, SkBackingFit::kExact, {100, 200}, 1,
254 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin, SkBudgeted::kYes,
255 nullptr)) {
Michael Ludwig81d41722020-05-26 16:57:38 -0400256 topLeftRTC->clear(SK_PMColor4fTRANSPARENT);
Chris Dalton49d14e92018-07-27 12:38:35 -0600257 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
258 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
Michael Ludwig7c12e282020-05-29 09:54:07 -0400259 rtc->drawTexture(nullptr, topLeftRTC->readSurfaceView(), rtc->colorInfo().alphaType(),
Greg Daniel40903af2020-01-30 14:55:05 -0500260 GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver, SK_PMColor4fWHITE,
261 {0, 0, 100, 200}, {100, 0, 200, 200}, GrAA::kNo, GrQuadAAFlags::kNone,
Chris Dalton49d14e92018-07-27 12:38:35 -0600262 SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint, SkMatrix::I(),
Brian Osman3d139a42018-11-19 10:42:10 -0500263 nullptr);
Chris Dalton49d14e92018-07-27 12:38:35 -0600264 }
265
266 // Draw the test to an off-screen, bottom-up render target.
Greg Daniele20fcad2020-01-08 11:52:34 -0500267 if (auto topLeftRTC = GrRenderTargetContext::Make(
268 ctx, rtcColorType, nullptr, SkBackingFit::kExact, {100, 200}, 1,
269 GrMipMapped::kNo, GrProtected::kNo, kBottomLeft_GrSurfaceOrigin, SkBudgeted::kYes,
270 nullptr)) {
Michael Ludwig81d41722020-05-26 16:57:38 -0400271 topLeftRTC->clear(SK_PMColor4fTRANSPARENT);
Chris Dalton49d14e92018-07-27 12:38:35 -0600272 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
273 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
Michael Ludwig7c12e282020-05-29 09:54:07 -0400274 rtc->drawTexture(nullptr, topLeftRTC->readSurfaceView(), rtc->colorInfo().alphaType(),
Greg Daniel40903af2020-01-30 14:55:05 -0500275 GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver, SK_PMColor4fWHITE,
276 {0, 0, 100, 200}, {200, 0, 300, 200}, GrAA::kNo, GrQuadAAFlags::kNone,
Chris Dalton49d14e92018-07-27 12:38:35 -0600277 SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint, SkMatrix::I(),
Brian Osman3d139a42018-11-19 10:42:10 -0500278 nullptr);
Chris Dalton49d14e92018-07-27 12:38:35 -0600279 }
280}
281
282////////////////////////////////////////////////////////////////////////////////////////////////////
283
284DEF_GM( return new ClockwiseGM(); )
285
286}