Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
fwidth_squircle.cpp
Go to the documentation of this file.
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"
14#include "include/core/SkRect.h"
46
47#include <memory>
48#include <utility>
49
50class GrAppliedClip;
51
52/**
53 * This test ensures that fwidth() works properly on GPU configs by drawing a squircle.
54 */
55namespace {
56
57static constexpr GrGeometryProcessor::Attribute gVertex =
59
60////////////////////////////////////////////////////////////////////////////////////////////////////
61// SkSL code.
62
63class FwidthSquircleTestProcessor : public GrGeometryProcessor {
64public:
65 static GrGeometryProcessor* Make(SkArenaAlloc* arena, const SkMatrix& viewMatrix) {
66 return arena->make([&](void* ptr) {
67 return new (ptr) FwidthSquircleTestProcessor(viewMatrix);
68 });
69 }
70
71 const char* name() const override { return "FwidthSquircleTestProcessor"; }
72
73 void addToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const final {}
74
75 std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const final;
76
77private:
78 FwidthSquircleTestProcessor(const SkMatrix& viewMatrix)
79 : GrGeometryProcessor(kFwidthSquircleTestProcessor_ClassID)
80 , fViewMatrix(viewMatrix) {
81 this->setVertexAttributesWithImplicitOffsets(&gVertex, 1);
82 }
83
85
87};
88
89std::unique_ptr<GrGeometryProcessor::ProgramImpl> FwidthSquircleTestProcessor::makeProgramImpl(
90 const GrShaderCaps&) const {
91 class Impl : public ProgramImpl {
92 public:
93 void setData(const GrGLSLProgramDataManager& pdman,
94 const GrShaderCaps&,
95 const GrGeometryProcessor& geomProc) override {
96 const auto& proc = geomProc.cast<FwidthSquircleTestProcessor>();
97 pdman.setSkMatrix(fViewMatrixHandle, proc.fViewMatrix);
98 }
99
100 private:
101 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
102 const auto& proc = args.fGeomProc.cast<FwidthSquircleTestProcessor>();
103
104 auto* uniforms = args.fUniformHandler;
105 fViewMatrixHandle = uniforms->addUniform(nullptr,
108 "viewmatrix");
109
110 auto* varyings = args.fVaryingHandler;
111 varyings->emitAttributes(proc);
112
113 GrGLSLVarying squircleCoord(SkSLType::kFloat2);
114 varyings->addVarying("bboxcoord", &squircleCoord);
115
116 auto* v = args.fVertBuilder;
117 v->codeAppendf("float2x2 R = float2x2(cos(.05), sin(.05), -sin(.05), cos(.05));");
118
119 v->codeAppendf("%s = bboxcoord * 1.25;", squircleCoord.vsOut());
120 v->codeAppendf("float3 vertexpos = float3(bboxcoord * 100 * R + 100, 1);");
121 v->codeAppendf("vertexpos = %s * vertexpos;",
122 uniforms->getUniformCStr(fViewMatrixHandle));
123 gpArgs->fPositionVar.set(SkSLType::kFloat3, "vertexpos");
124
125 auto* f = args.fFragBuilder;
126 f->codeAppendf("float golden_ratio = 1.61803398875;");
127 f->codeAppendf("float pi = 3.141592653589793;");
128 f->codeAppendf("float x = abs(%s.x), y = abs(%s.y);",
129 squircleCoord.fsIn(), squircleCoord.fsIn());
130
131 // Squircle function!
132 f->codeAppendf("float fn = half(pow(x, golden_ratio*pi) + "
133 "pow(y, golden_ratio*pi) - 1);");
134 f->codeAppendf("float fnwidth = fwidth(fn);");
135 f->codeAppendf("fnwidth += 1e-10;"); // Guard against divide-by-zero.
136 f->codeAppendf("half coverage = clamp(half(.5 - fn/fnwidth), 0, 1);");
137
138 f->codeAppendf("half4 %s = half4(.51, .42, .71, 1) * .89;", args.fOutputColor);
139 f->codeAppendf("half4 %s = half4(coverage);", args.fOutputCoverage);
140 }
141
142 UniformHandle fViewMatrixHandle;
143 };
144
145 return std::make_unique<Impl>();
146}
147
148////////////////////////////////////////////////////////////////////////////////////////////////////
149// Draw Op.
150
151class FwidthSquircleTestOp : public GrDrawOp {
152public:
154
155 static GrOp::Owner Make(GrRecordingContext* ctx, const SkMatrix& viewMatrix) {
156 return GrOp::Make<FwidthSquircleTestOp>(ctx, viewMatrix);
157 }
158
159private:
160 FwidthSquircleTestOp(const SkMatrix& viewMatrix)
161 : GrDrawOp(ClassID())
162 , fViewMatrix(viewMatrix) {
163 this->setBounds(SkRect::MakeIWH(kWidth, kHeight), HasAABloat::kNo, IsHairline::kNo);
164 }
165
166 const char* name() const override { return "FwidthSquircleTestOp"; }
167 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
168 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, GrClampType) override {
170 }
171
172 GrProgramInfo* createProgramInfo(const GrCaps* caps,
173 SkArenaAlloc* arena,
174 const GrSurfaceProxyView& writeView,
175 bool usesMSAASurface,
176 GrAppliedClip&& appliedClip,
177 const GrDstProxyView& dstProxyView,
178 GrXferBarrierFlags renderPassXferBarriers,
179 GrLoadOp colorLoadOp) const {
180 GrGeometryProcessor* geomProc = FwidthSquircleTestProcessor::Make(arena, fViewMatrix);
181
182 return sk_gpu_test::CreateProgramInfo(caps, arena, writeView, usesMSAASurface,
183 std::move(appliedClip), dstProxyView,
184 geomProc, SkBlendMode::kSrcOver,
186 renderPassXferBarriers, colorLoadOp);
187 }
188
189 GrProgramInfo* createProgramInfo(GrOpFlushState* flushState) const {
190 return this->createProgramInfo(&flushState->caps(),
191 flushState->allocator(),
192 flushState->writeView(),
193 flushState->usesMSAASurface(),
194 flushState->detachAppliedClip(),
195 flushState->dstProxyView(),
196 flushState->renderPassBarriers(),
197 flushState->colorLoadOp());
198 }
199
200 void onPrePrepare(GrRecordingContext* context,
201 const GrSurfaceProxyView& writeView,
203 const GrDstProxyView& dstProxyView,
204 GrXferBarrierFlags renderPassXferBarriers,
205 GrLoadOp colorLoadOp) final {
206 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
207
208 // DMSAA is not supported on DDL.
209 bool usesMSAASurface = writeView.asRenderTargetProxy()->numSamples() > 1;
210
211 // This is equivalent to a GrOpFlushState::detachAppliedClip
212 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip::Disabled();
213
214 fProgramInfo = this->createProgramInfo(context->priv().caps(), arena, writeView,
215 usesMSAASurface, std::move(appliedClip),
216 dstProxyView, renderPassXferBarriers, colorLoadOp);
217
218 context->priv().recordProgramInfo(fProgramInfo);
219 }
220
221 void onPrepare(GrOpFlushState* flushState) final {
222 SkPoint vertices[4] = {
223 {-1, -1},
224 {+1, -1},
225 {-1, +1},
226 {+1, +1},
227 };
228 fVertexBuffer = flushState->resourceProvider()->createBuffer(vertices,
229 sizeof(vertices),
232 }
233
234 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) final {
235 if (!fVertexBuffer) {
236 return;
237 }
238
239 if (!fProgramInfo) {
240 fProgramInfo = this->createProgramInfo(flushState);
241 }
242
243 flushState->bindPipeline(*fProgramInfo, SkRect::MakeIWH(kWidth, kHeight));
244 flushState->bindBuffers(nullptr, nullptr, std::move(fVertexBuffer));
245 flushState->draw(4, 0);
246
247 }
248
249 static const int kWidth = 200;
250 static const int kHeight = 200;
251
252 sk_sp<GrBuffer> fVertexBuffer;
253 const SkMatrix fViewMatrix;
254
255 // The program info (and both the GrPipeline and GrGeometryProcessor it relies on), when
256 // allocated, are allocated in either the ddl-record-time or flush-time arena. It is the
257 // arena's job to free up their memory so we just have a bare programInfo pointer here. We
258 // don't even store the GrPipeline and GrGeometryProcessor pointers here bc they are
259 // guaranteed to have the same lifetime as the program info.
260 GrProgramInfo* fProgramInfo = nullptr;
261
262 friend class ::GrOp; // for ctor
263
264 using INHERITED = GrDrawOp;
265};
266
267} // namespace
268
269////////////////////////////////////////////////////////////////////////////////////////////////////
270// Test.
271
272namespace skiagm {
273
274DEF_SIMPLE_GPU_GM_CAN_FAIL(fwidth_squircle, rContext, canvas, errorMsg, 200, 200) {
275 if (!rContext->priv().caps()->shaderCaps()->fShaderDerivativeSupport) {
276 *errorMsg = "Shader derivatives not supported.";
277 return DrawResult::kSkip;
278 }
279
281 if (!sdc) {
283 return DrawResult::kSkip;
284 }
285
286 // Draw the test directly to the frame buffer.
287 canvas->clear(SK_ColorWHITE);
288 sdc->addDrawOp(FwidthSquircleTestOp::Make(rContext, canvas->getTotalMatrix()));
290}
291
292} // namespace skiagm
SkMatrix fViewMatrix
#define DEFINE_OP_CLASS_ID
Definition GrOp.h:64
GrClampType
@ kVertex_GrShaderFlag
@ kStatic_GrAccessPattern
GrLoadOp
@ kFloat2_GrVertexAttribType
GrXferBarrierFlags
@ kSrcOver
r = s + (1-sa)*d
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
static std::unique_ptr< SkEncoder > Make(SkWStream *dst, const SkPixmap *src, const SkYUVAPixmaps *srcYUVA, const SkColorSpace *srcYUVAColorSpace, const SkJpegEncoder::Options &options)
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition SkPath.cpp:3824
#define INHERITED(method,...)
void setSkMatrix(UniformHandle, const SkMatrix &) const
GrLoadOp colorLoadOp() const final
const GrDstProxyView & dstProxyView() const final
GrXferBarrierFlags renderPassBarriers() const final
SkArenaAlloc * allocator() override
const GrSurfaceProxyView & writeView() const final
GrAppliedClip detachAppliedClip() final
const GrCaps & caps() const final
bool usesMSAASurface() const final
std::unique_ptr< GrOp > Owner
Definition GrOp.h:72
static constexpr Analysis EmptySetAnalysis()
const T & cast() const
GrRenderTargetProxy * asRenderTargetProxy() const
auto make(Ctor &&ctor) -> decltype(ctor(nullptr))
static constexpr char kErrorMsg_DrawSkippedGpuOnly[]
Definition gm.h:127
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
const char * name
Definition fuchsia.cc:50
SurfaceDrawContext * TopDeviceSurfaceDrawContext(const SkCanvas *canvas)
Definition GrCanvas.cpp:20
DEF_SIMPLE_GPU_GM_CAN_FAIL(clear_swizzle, rContext, canvas, errorMsg, 6 *kSize, 2 *kSize)
static SkRect MakeIWH(int w, int h)
Definition SkRect.h:623
constexpr size_t kHeight
constexpr size_t kWidth