blob: 01ac28abf49b8968460a7216c9a8dc4d323abb04 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrClip.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040027#include "src/gpu/GrColorSpaceXform.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040029#include "src/gpu/GrGeometryProcessor.h"
30#include "src/gpu/GrGpuBuffer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrMemoryPool.h"
32#include "src/gpu/GrOpFlushState.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040033#include "src/gpu/GrOpsRenderPass.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040034#include "src/gpu/GrPipeline.h"
35#include "src/gpu/GrPrimitiveProcessor.h"
36#include "src/gpu/GrProcessor.h"
37#include "src/gpu/GrProcessorSet.h"
Robert Phillips901aff02019-10-08 12:32:56 -040038#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/gpu/GrRecordingContextPriv.h"
40#include "src/gpu/GrRenderTargetContext.h"
41#include "src/gpu/GrRenderTargetContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040042#include "src/gpu/GrResourceProvider.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040043#include "src/gpu/GrSamplerState.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040044#include "src/gpu/GrShaderCaps.h"
45#include "src/gpu/GrShaderVar.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040046#include "src/gpu/GrSurfaceProxy.h"
47#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050048#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
49#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040050#include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050051#include "src/gpu/glsl/GrGLSLVarying.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040052#include "src/gpu/ops/GrDrawOp.h"
53#include "src/gpu/ops/GrOp.h"
Robert Phillips34cea002019-11-21 16:02:34 -050054#include "tools/gpu/ProxyUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040055
56#include <memory>
57#include <utility>
58
59class GrAppliedClip;
60class GrGLSLProgramDataManager;
Chris Dalton49d14e92018-07-27 12:38:35 -060061
Hal Canaryfa3305a2019-07-18 12:36:54 -040062namespace {
Chris Dalton49d14e92018-07-27 12:38:35 -060063
Brian Osmand4c29702018-09-14 16:16:55 -040064static constexpr GrGeometryProcessor::Attribute gVertex =
Stephen White1e1ad8d2019-06-19 14:19:47 -040065 {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Chris Dalton49d14e92018-07-27 12:38:35 -060066
67/**
68 * This is a GPU-backend specific test. It ensures that SkSL properly identifies clockwise-winding
69 * triangles (sk_Clockwise), in terms of to Skia device space, in all backends and with all render
70 * target origins. We draw clockwise triangles green and counter-clockwise red.
71 */
Hal Canaryfa3305a2019-07-18 12:36:54 -040072class ClockwiseGM : public skiagm::GpuGM {
73 SkString onShortName() override { return SkString("clockwise"); }
74 SkISize onISize() override { return {300, 200}; }
Chris Dalton3a778372019-02-07 15:23:36 -070075 void onDraw(GrContext*, GrRenderTargetContext*, SkCanvas*) override;
Chris Dalton49d14e92018-07-27 12:38:35 -060076};
77
78////////////////////////////////////////////////////////////////////////////////////////////////////
79// SkSL code.
80
81class ClockwiseTestProcessor : public GrGeometryProcessor {
82public:
Robert Phillips5d07c522019-11-18 11:19:51 -050083 static GrGeometryProcessor* Make(SkArenaAlloc* arena, bool readSkFragCoord) {
84 return arena->make<ClockwiseTestProcessor>(readSkFragCoord);
Robert Phillipsdf70f152019-11-15 14:57:05 -050085 }
86
87 const char* name() const final { return "ClockwiseTestProcessor"; }
88
89 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {
90 b->add32(fReadSkFragCoord);
91 }
92
93 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
94
Robert Phillips5d07c522019-11-18 11:19:51 -050095 bool readSkFragCoord() const { return fReadSkFragCoord; }
96
Robert Phillipsdf70f152019-11-15 14:57:05 -050097private:
Robert Phillips5d07c522019-11-18 11:19:51 -050098 friend class ::SkArenaAlloc; // for access to ctor
99
Chris Dalton49d14e92018-07-27 12:38:35 -0600100 ClockwiseTestProcessor(bool readSkFragCoord)
101 : GrGeometryProcessor(kClockwiseTestProcessor_ClassID)
102 , fReadSkFragCoord(readSkFragCoord) {
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500103 this->setVertexAttributes(&gVertex, 1);
Chris Dalton49d14e92018-07-27 12:38:35 -0600104 }
Chris Dalton49d14e92018-07-27 12:38:35 -0600105
Chris Dalton49d14e92018-07-27 12:38:35 -0600106 const bool fReadSkFragCoord;
107
Robert Phillips5d07c522019-11-18 11:19:51 -0500108 typedef GrGeometryProcessor INHERITED;
Chris Dalton49d14e92018-07-27 12:38:35 -0600109};
110
111class GLSLClockwiseTestProcessor : public GrGLSLGeometryProcessor {
112 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
Brian Salomonc241b582019-11-27 08:57:17 -0500113 const CoordTransformRange&) override {}
Chris Dalton49d14e92018-07-27 12:38:35 -0600114
115 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
116 const ClockwiseTestProcessor& proc = args.fGP.cast<ClockwiseTestProcessor>();
117 args.fVaryingHandler->emitAttributes(proc);
Stephen White1e1ad8d2019-06-19 14:19:47 -0400118 gpArgs->fPositionVar.set(kFloat2_GrSLType, "position");
Chris Dalton49d14e92018-07-27 12:38:35 -0600119 args.fFragBuilder->codeAppendf(
120 "%s = sk_Clockwise ? half4(0,1,0,1) : half4(1,0,0,1);", args.fOutputColor);
Robert Phillips5d07c522019-11-18 11:19:51 -0500121 if (!proc.readSkFragCoord()) {
Chris Dalton49d14e92018-07-27 12:38:35 -0600122 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
123 } else {
124 // Verify layout(origin_upper_left) on gl_FragCoord does not affect gl_FrontFacing.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500125 args.fFragBuilder->codeAppendf("%s = half4(min(half(sk_FragCoord.y), 1));",
Chris Dalton49d14e92018-07-27 12:38:35 -0600126 args.fOutputCoverage);
127 }
128 }
129};
130
131GrGLSLPrimitiveProcessor* ClockwiseTestProcessor::createGLSLInstance(
132 const GrShaderCaps&) const {
133 return new GLSLClockwiseTestProcessor;
134}
135
136////////////////////////////////////////////////////////////////////////////////////////////////////
137// Draw Op.
138
139class ClockwiseTestOp : public GrDrawOp {
140public:
141 DEFINE_OP_CLASS_ID
142
Robert Phillipsbe9aff22019-02-15 11:33:22 -0500143 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
144 bool readSkFragCoord, int y = 0) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500145 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Chris Dalton49d14e92018-07-27 12:38:35 -0600146 return pool->allocate<ClockwiseTestOp>(readSkFragCoord, y);
147 }
148
149private:
150 ClockwiseTestOp(bool readSkFragCoord, float y)
Robert Phillips5d07c522019-11-18 11:19:51 -0500151 : GrDrawOp(ClassID())
152 , fReadSkFragCoord(readSkFragCoord)
153 , fY(y) {
Greg Daniel5faf4742019-10-01 15:14:44 -0400154 this->setBounds(SkRect::MakeXYWH(0, fY, 100, 100), HasAABloat::kNo, IsHairline::kNo);
Chris Dalton49d14e92018-07-27 12:38:35 -0600155 }
156
157 const char* name() const override { return "ClockwiseTestOp"; }
158 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600159 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
160 bool hasMixedSampledCoverage, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -0700161 return GrProcessorSet::EmptySetAnalysis();
Chris Dalton49d14e92018-07-27 12:38:35 -0600162 }
Robert Phillipsdf70f152019-11-15 14:57:05 -0500163
Robert Phillipsac6156c2020-02-28 16:02:40 -0500164 GrProgramInfo* createProgramInfo(const GrCaps* caps,
165 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400166 const GrSurfaceProxyView* writeView,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500167 GrAppliedClip&& appliedClip,
168 const GrXferProcessor::DstProxyView& dstProxyView) const {
169 GrGeometryProcessor* geomProc = ClockwiseTestProcessor::Make(arena, fReadSkFragCoord);
170
Brian Salomon8afde5f2020-04-01 16:22:00 -0400171 return sk_gpu_test::CreateProgramInfo(caps, arena, writeView,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500172 std::move(appliedClip), dstProxyView,
173 geomProc, SkBlendMode::kPlus,
174 GrPrimitiveType::kTriangleStrip);
175 }
176
177 GrProgramInfo* createProgramInfo(GrOpFlushState* flushState) const {
178 return this->createProgramInfo(&flushState->caps(),
179 flushState->allocator(),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400180 flushState->writeView(),
Robert Phillipsac6156c2020-02-28 16:02:40 -0500181 flushState->detachAppliedClip(),
182 flushState->dstProxyView());
183 }
184
Robert Phillipsdf70f152019-11-15 14:57:05 -0500185 void onPrePrepare(GrRecordingContext* context,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400186 const GrSurfaceProxyView* writeView,
Robert Phillips34cea002019-11-21 16:02:34 -0500187 GrAppliedClip* clip,
Robert Phillips8053c972019-11-21 10:44:53 -0500188 const GrXferProcessor::DstProxyView& dstProxyView) final {
Robert Phillipsd4fb7c72019-11-15 17:28:37 -0500189 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
Robert Phillipsdf70f152019-11-15 14:57:05 -0500190
Robert Phillips34cea002019-11-21 16:02:34 -0500191 // This is equivalent to a GrOpFlushState::detachAppliedClip
192 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip();
Robert Phillipsdf70f152019-11-15 14:57:05 -0500193
Brian Salomon8afde5f2020-04-01 16:22:00 -0400194 fProgramInfo = this->createProgramInfo(context->priv().caps(), arena, writeView,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500195 std::move(appliedClip), dstProxyView);
Robert Phillipsdf70f152019-11-15 14:57:05 -0500196
Robert Phillipsac6156c2020-02-28 16:02:40 -0500197 context->priv().recordProgramInfo(fProgramInfo);
Robert Phillipsdf70f152019-11-15 14:57:05 -0500198 }
199
Greg Danielf793de12019-09-05 13:23:23 -0400200 void onPrepare(GrOpFlushState* flushState) override {
Chris Dalton49d14e92018-07-27 12:38:35 -0600201 SkPoint vertices[4] = {
202 {100, fY},
203 {0, fY+100},
204 {0, fY},
205 {100, fY+100},
206 };
Greg Danielf793de12019-09-05 13:23:23 -0400207 fVertexBuffer = flushState->resourceProvider()->createBuffer(
208 sizeof(vertices), GrGpuBufferType::kVertex, kStatic_GrAccessPattern, vertices);
209 }
Robert Phillipsdf70f152019-11-15 14:57:05 -0500210
Greg Danielf793de12019-09-05 13:23:23 -0400211 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
212 if (!fVertexBuffer) {
Chris Dalton79f99f72018-07-27 14:15:10 -0600213 return;
214 }
Robert Phillipsdf70f152019-11-15 14:57:05 -0500215
Robert Phillips34cea002019-11-21 16:02:34 -0500216 if (!fProgramInfo) {
Robert Phillipsac6156c2020-02-28 16:02:40 -0500217 fProgramInfo = this->createProgramInfo(flushState);
Robert Phillips34cea002019-11-21 16:02:34 -0500218 }
219
Chris Daltonaa0e45c2020-03-16 10:05:11 -0600220 flushState->bindPipeline(*fProgramInfo, SkRect::MakeXYWH(0, fY, 100, 100));
221 flushState->bindBuffers(nullptr, nullptr, fVertexBuffer.get());
222 flushState->draw(4, 0);
Chris Dalton49d14e92018-07-27 12:38:35 -0600223 }
224
Robert Phillips5d07c522019-11-18 11:19:51 -0500225 sk_sp<GrBuffer> fVertexBuffer;
226 const bool fReadSkFragCoord;
227 const float fY;
Robert Phillipsdf70f152019-11-15 14:57:05 -0500228
Robert Phillips5d07c522019-11-18 11:19:51 -0500229 // The program info (and both the GrPipeline and GrPrimitiveProcessor it relies on), when
Robert Phillips34cea002019-11-21 16:02:34 -0500230 // allocated, are allocated in either the ddl-record-time or flush-time arena. It is the
231 // arena's job to free up their memory so we just have a bare programInfo pointer here. We
232 // don't even store the GrPipeline and GrPrimitiveProcessor pointers here bc they are
233 // guaranteed to have the same lifetime as the program info.
Robert Phillips5d07c522019-11-18 11:19:51 -0500234 GrProgramInfo* fProgramInfo = nullptr;
Chris Dalton49d14e92018-07-27 12:38:35 -0600235
236 friend class ::GrOpMemoryPool; // for ctor
Robert Phillips5d07c522019-11-18 11:19:51 -0500237
238 typedef GrDrawOp INHERITED;
Chris Dalton49d14e92018-07-27 12:38:35 -0600239};
240
241////////////////////////////////////////////////////////////////////////////////////////////////////
242// Test.
243
Chris Dalton3a778372019-02-07 15:23:36 -0700244void ClockwiseGM::onDraw(GrContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas) {
Brian Osman9a9baae2018-11-05 15:06:26 -0500245 rtc->clear(nullptr, { 0, 0, 0, 1 }, GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Dalton49d14e92018-07-27 12:38:35 -0600246
247 // Draw the test directly to the frame buffer.
248 rtc->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
249 rtc->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
250
251 // Draw the test to an off-screen, top-down render target.
Greg Danielc594e622019-10-15 14:01:49 -0400252 GrColorType rtcColorType = rtc->colorInfo().colorType();
Greg Daniele20fcad2020-01-08 11:52:34 -0500253 if (auto topLeftRTC = GrRenderTargetContext::Make(
254 ctx, rtcColorType, nullptr, SkBackingFit::kExact, {100, 200}, 1,
255 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin, SkBudgeted::kYes,
256 nullptr)) {
Brian Osman9a9baae2018-11-05 15:06:26 -0500257 topLeftRTC->clear(nullptr, SK_PMColor4fTRANSPARENT,
258 GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Dalton49d14e92018-07-27 12:38:35 -0600259 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
260 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
Greg Daniel40903af2020-01-30 14:55:05 -0500261 rtc->drawTexture(GrNoClip(), topLeftRTC->readSurfaceView(), rtc->colorInfo().alphaType(),
262 GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver, SK_PMColor4fWHITE,
263 {0, 0, 100, 200}, {100, 0, 200, 200}, GrAA::kNo, GrQuadAAFlags::kNone,
Chris Dalton49d14e92018-07-27 12:38:35 -0600264 SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint, SkMatrix::I(),
Brian Osman3d139a42018-11-19 10:42:10 -0500265 nullptr);
Chris Dalton49d14e92018-07-27 12:38:35 -0600266 }
267
268 // Draw the test to an off-screen, bottom-up render target.
Greg Daniele20fcad2020-01-08 11:52:34 -0500269 if (auto topLeftRTC = GrRenderTargetContext::Make(
270 ctx, rtcColorType, nullptr, SkBackingFit::kExact, {100, 200}, 1,
271 GrMipMapped::kNo, GrProtected::kNo, kBottomLeft_GrSurfaceOrigin, SkBudgeted::kYes,
272 nullptr)) {
Brian Osman9a9baae2018-11-05 15:06:26 -0500273 topLeftRTC->clear(nullptr, SK_PMColor4fTRANSPARENT,
274 GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Dalton49d14e92018-07-27 12:38:35 -0600275 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
276 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
Greg Daniel40903af2020-01-30 14:55:05 -0500277 rtc->drawTexture(GrNoClip(), topLeftRTC->readSurfaceView(), rtc->colorInfo().alphaType(),
278 GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver, SK_PMColor4fWHITE,
279 {0, 0, 100, 200}, {200, 0, 300, 200}, GrAA::kNo, GrQuadAAFlags::kNone,
Chris Dalton49d14e92018-07-27 12:38:35 -0600280 SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint, SkMatrix::I(),
Brian Osman3d139a42018-11-19 10:42:10 -0500281 nullptr);
Chris Dalton49d14e92018-07-27 12:38:35 -0600282 }
283}
284
285////////////////////////////////////////////////////////////////////////////////////////////////////
286
287DEF_GM( return new ClockwiseGM(); )
288
289}