blob: 18e8f2ffab362cbf522d6d632f3dced33b91b60a [file] [log] [blame]
Chris Daltona9f759d2021-05-18 12:37:08 -06001/*
2 * Copyright 2019 Google LLC.
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 "include/core/SkCanvas.h"
9#include "samplecode/Sample.h"
10#include "src/core/SkPathPriv.h"
11
12#if SK_SUPPORT_GPU
13
14#include "src/core/SkCanvasPriv.h"
15#include "src/gpu/GrRecordingContextPriv.h"
16#include "src/gpu/GrSurfaceDrawContext.h"
Chris Daltond9bdc322021-06-01 19:22:05 -060017#include "src/gpu/tessellate/GrPathCurveTessellator.h"
Chris Daltond9bdc322021-06-01 19:22:05 -060018#include "src/gpu/tessellate/GrPathWedgeTessellator.h"
Chris Dalton3b412782021-06-01 13:40:03 -060019#include "src/gpu/tessellate/shaders/GrPathTessellationShader.h"
Chris Daltona9f759d2021-05-18 12:37:08 -060020
21namespace {
22
23enum class Mode {
Chris Daltond2b8ba32021-06-09 00:12:59 -060024 kWedgeMiddleOut,
Chris Daltona9f759d2021-05-18 12:37:08 -060025 kCurveMiddleOut,
26 kWedgeTessellate,
27 kCurveTessellate
28};
29
30static const char* ModeName(Mode mode) {
31 switch (mode) {
Chris Daltond2b8ba32021-06-09 00:12:59 -060032 case Mode::kWedgeMiddleOut:
33 return "MiddleOutShader (kWedges)";
Chris Daltona9f759d2021-05-18 12:37:08 -060034 case Mode::kCurveMiddleOut:
Chris Daltond2b8ba32021-06-09 00:12:59 -060035 return "MiddleOutShader (kCurves)";
Chris Daltona9f759d2021-05-18 12:37:08 -060036 case Mode::kWedgeTessellate:
Chris Daltond2b8ba32021-06-09 00:12:59 -060037 return "HardwareWedgeShader";
Chris Daltona9f759d2021-05-18 12:37:08 -060038 case Mode::kCurveTessellate:
Chris Daltond2b8ba32021-06-09 00:12:59 -060039 return "HardwareCurveShader";
Chris Daltona9f759d2021-05-18 12:37:08 -060040 }
41 SkUNREACHABLE;
42}
43
44// Draws a path directly to the screen using a specific tessellator.
45class SamplePathTessellatorOp : public GrDrawOp {
46private:
47 DEFINE_OP_CLASS_ID
48
49 SamplePathTessellatorOp(const SkRect& drawBounds, const SkPath& path, const SkMatrix& m,
50 GrPipeline::InputFlags pipelineFlags, Mode mode)
51 : GrDrawOp(ClassID())
52 , fPath(path)
53 , fMatrix(m)
54 , fPipelineFlags(pipelineFlags)
55 , fMode(mode) {
56 this->setBounds(drawBounds, HasAABloat::kNo, IsHairline::kNo);
57 }
58 const char* name() const override { return "SamplePathTessellatorOp"; }
59 void visitProxies(const VisitProxyFunc& fn) const override {}
60 FixedFunctionFlags fixedFunctionFlags() const override {
61 return FixedFunctionFlags::kUsesHWAA;
62 }
63 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
64 GrClampType clampType) override {
65 SkPMColor4f color;
66 return fProcessors.finalize(SK_PMColor4fWHITE, GrProcessorAnalysisCoverage::kNone, clip,
67 nullptr, caps, clampType, &color);
68 }
69 void onPrePrepare(GrRecordingContext*, const GrSurfaceProxyView&, GrAppliedClip*,
John Stiles52cb1d02021-06-02 11:58:05 -040070 const GrDstProxyView&, GrXferBarrierFlags, GrLoadOp colorLoadOp) override {}
Chris Daltona9f759d2021-05-18 12:37:08 -060071 void onPrepare(GrOpFlushState* flushState) override {
Chris Dalton2f733ec2021-06-01 12:11:57 -060072 constexpr static SkPMColor4f kCyan = {0,1,1,1};
Chris Daltona9f759d2021-05-18 12:37:08 -060073 auto alloc = flushState->allocator();
Chris Daltond2b8ba32021-06-09 00:12:59 -060074 const GrCaps& caps = flushState->caps();
75 int numVerbsToGetMiddleOut = 0;
76 int numVerbsToGetTessellation = caps.minPathVerbsForHwTessellation();
Chris Daltona9f759d2021-05-18 12:37:08 -060077 switch (fMode) {
Chris Daltond2b8ba32021-06-09 00:12:59 -060078 using DrawInnerFan = GrPathCurveTessellator::DrawInnerFan;
79 case Mode::kWedgeMiddleOut:
80 fTessellator = GrPathWedgeTessellator::Make(alloc, fMatrix, kCyan,
81 numVerbsToGetMiddleOut, caps);
82 break;
Chris Daltona9f759d2021-05-18 12:37:08 -060083 case Mode::kCurveMiddleOut:
Chris Dalton26666bd2021-06-08 16:25:46 -060084 fTessellator = GrPathCurveTessellator::Make(alloc, fMatrix, kCyan,
85 DrawInnerFan::kYes,
Chris Daltond2b8ba32021-06-09 00:12:59 -060086 numVerbsToGetMiddleOut, caps);
Chris Daltona9f759d2021-05-18 12:37:08 -060087 break;
88 case Mode::kWedgeTessellate:
Chris Daltond2b8ba32021-06-09 00:12:59 -060089 fTessellator = GrPathWedgeTessellator::Make(alloc, fMatrix, kCyan,
90 numVerbsToGetTessellation, caps);
Chris Daltona9f759d2021-05-18 12:37:08 -060091 break;
92 case Mode::kCurveTessellate:
Chris Daltond9bdc322021-06-01 19:22:05 -060093 fTessellator = GrPathCurveTessellator::Make(alloc, fMatrix, kCyan,
Chris Dalton26666bd2021-06-08 16:25:46 -060094 DrawInnerFan::kYes,
Chris Daltond2b8ba32021-06-09 00:12:59 -060095 numVerbsToGetTessellation, caps);
Chris Daltona9f759d2021-05-18 12:37:08 -060096 break;
97 }
Chris Dalton569c01b2021-05-25 10:11:46 -060098 fTessellator->prepare(flushState, this->bounds(), fPath);
Chris Daltona9f759d2021-05-18 12:37:08 -060099 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState, std::move(fProcessors),
100 fPipelineFlags);
Chris Dalton2f733ec2021-06-01 12:11:57 -0600101 fProgram = GrTessellationShader::MakeProgram({alloc, flushState->writeView(),
102 &flushState->dstProxyView(),
103 flushState->renderPassBarriers(),
104 GrLoadOp::kClear, &flushState->caps()},
105 fTessellator->shader(), pipeline,
106 &GrUserStencilSettings::kUnused);
Chris Daltona9f759d2021-05-18 12:37:08 -0600107 }
108 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
109 flushState->bindPipeline(*fProgram, chainBounds);
110 fTessellator->draw(flushState);
111 }
112
113 const SkPath fPath;
114 const SkMatrix fMatrix;
115 const GrPipeline::InputFlags fPipelineFlags;
116 const Mode fMode;
117 GrPathTessellator* fTessellator = nullptr;
118 GrProgramInfo* fProgram;
119 GrProcessorSet fProcessors{SkBlendMode::kSrcOver};
120
121 friend class GrOp; // For ctor.
122};
123
124} // namespace
125
126// This sample enables wireframe and visualizes the triangles generated by path tessellators.
127class SamplePathTessellators : public Sample {
128public:
129 SamplePathTessellators() {
130#if 0
131 // For viewing middle-out triangulations of the inner fan.
132 fPath.moveTo(1, 0);
133 int numSides = 32 * 3;
134 for (int i = 1; i < numSides; ++i) {
135 float theta = 2*3.1415926535897932384626433832785 * i / numSides;
136 fPath.lineTo(std::cos(theta), std::sin(theta));
137 }
138 fPath.transform(SkMatrix::Scale(200, 200));
139 fPath.transform(SkMatrix::Translate(300, 300));
140#else
141 fPath.moveTo(100, 500);
142 fPath.cubicTo(300, 400, -100, 300, 100, 200);
143 fPath.quadTo(250, 0, 400, 200);
144 fPath.conicTo(600, 350, 400, 500, fConicWeight);
145 fPath.close();
146#endif
147 }
148
149private:
150 void onDrawContent(SkCanvas*) override;
151 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override;
152 bool onClick(Sample::Click*) override;
153 bool onChar(SkUnichar) override;
154
155 SkString name() override { return SkString("PathTessellators"); }
156
157 SkPath fPath;
158 GrPipeline::InputFlags fPipelineFlags = GrPipeline::InputFlags::kHWAntialias |
159 GrPipeline::InputFlags::kWireframe;
Chris Daltond2b8ba32021-06-09 00:12:59 -0600160 Mode fMode = Mode::kWedgeMiddleOut;
Chris Daltona9f759d2021-05-18 12:37:08 -0600161
162 float fConicWeight = .5;
163
164 class Click;
165};
166
167void SamplePathTessellators::onDrawContent(SkCanvas* canvas) {
168 canvas->clear(SK_ColorBLACK);
169
170 auto ctx = canvas->recordingContext();
171 GrSurfaceDrawContext* sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(canvas);
172
173 SkString error;
174 if (!sdc || !ctx) {
175 error = "GPU Only.";
176 } else if (!GrTessellationPathRenderer::IsSupported(*ctx->priv().caps())) {
177 error = "GrTessellationPathRenderer not supported.";
178 } else if (fMode >= Mode::kWedgeTessellate &&
179 !ctx->priv().caps()->shaderCaps()->tessellationSupport()) {
180 error.printf("%s requires hardware tessellation support.", ModeName(fMode));
181 }
182 if (!error.isEmpty()) {
183 canvas->clear(SK_ColorRED);
184 SkFont font(nullptr, 20);
185 SkPaint captionPaint;
186 captionPaint.setColor(SK_ColorWHITE);
187 canvas->drawString(error.c_str(), 10, 30, font, captionPaint);
188 return;
189 }
190
191 sdc->addDrawOp(GrOp::Make<SamplePathTessellatorOp>(ctx,
192 sdc->asRenderTargetProxy()->getBoundsRect(),
193 fPath, canvas->getTotalMatrix(),
194 fPipelineFlags, fMode));
195
196 // Draw the path points.
197 SkPaint pointsPaint;
198 pointsPaint.setColor(SK_ColorBLUE);
199 pointsPaint.setStrokeWidth(8);
200 SkPath devPath = fPath;
201 devPath.transform(canvas->getTotalMatrix());
202 {
203 SkAutoCanvasRestore acr(canvas, true);
204 canvas->setMatrix(SkMatrix::I());
205 SkString caption(ModeName(fMode));
206 caption.appendf(" (w=%g)", fConicWeight);
207 SkFont font(nullptr, 20);
208 SkPaint captionPaint;
209 captionPaint.setColor(SK_ColorWHITE);
210 canvas->drawString(caption, 10, 30, font, captionPaint);
211 canvas->drawPoints(SkCanvas::kPoints_PointMode, devPath.countPoints(),
212 SkPathPriv::PointData(devPath), pointsPaint);
213 }
214}
215
216class SamplePathTessellators::Click : public Sample::Click {
217public:
218 Click(int ptIdx) : fPtIdx(ptIdx) {}
219
220 void doClick(SkPath* path) {
221 SkPoint pt = path->getPoint(fPtIdx);
222 SkPathPriv::UpdatePathPoint(path, fPtIdx, pt + fCurr - fPrev);
223 }
224
225private:
226 int fPtIdx;
227};
228
229Sample::Click* SamplePathTessellators::onFindClickHandler(SkScalar x, SkScalar y,
230 skui::ModifierKey) {
231 const SkPoint* pts = SkPathPriv::PointData(fPath);
232 float fuzz = 30;
233 for (int i = 0; i < fPath.countPoints(); ++i) {
234 if (fabs(x - pts[i].x()) < fuzz && fabsf(y - pts[i].y()) < fuzz) {
235 return new Click(i);
236 }
237 }
238 return nullptr;
239}
240
241bool SamplePathTessellators::onClick(Sample::Click* click) {
242 Click* myClick = (Click*)click;
243 myClick->doClick(&fPath);
244 return true;
245}
246
247static SkPath update_weight(const SkPath& path, float w) {
248 SkPath path_;
249 for (auto [verb, pts, _] : SkPathPriv::Iterate(path)) {
250 switch (verb) {
251 case SkPathVerb::kMove:
252 path_.moveTo(pts[0]);
253 break;
254 case SkPathVerb::kLine:
255 path_.lineTo(pts[1]);
256 break;
257 case SkPathVerb::kQuad:
258 path_.quadTo(pts[1], pts[2]);
259 break;
260 case SkPathVerb::kCubic:
261 path_.cubicTo(pts[1], pts[2], pts[3]);
262 break;
263 case SkPathVerb::kConic:
264 path_.conicTo(pts[1], pts[2], (w != 1) ? w : .99f);
265 break;
266 case SkPathVerb::kClose:
267 break;
268 }
269 }
270 return path_;
271}
272
273bool SamplePathTessellators::onChar(SkUnichar unichar) {
274 switch (unichar) {
275 case 'w':
276 fPipelineFlags = (GrPipeline::InputFlags)(
277 (int)fPipelineFlags ^ (int)GrPipeline::InputFlags::kWireframe);
278 return true;
279 case 'D': {
280 fPath.dump();
281 return true;
282 }
283 case '+':
284 fConicWeight *= 2;
285 fPath = update_weight(fPath, fConicWeight);
286 return true;
287 case '=':
288 fConicWeight *= 5/4.f;
289 fPath = update_weight(fPath, fConicWeight);
290 return true;
291 case '_':
292 fConicWeight *= .5f;
293 fPath = update_weight(fPath, fConicWeight);
294 return true;
295 case '-':
296 fConicWeight *= 4/5.f;
297 fPath = update_weight(fPath, fConicWeight);
298 return true;
299 case '1':
300 case '2':
301 case '3':
Chris Daltond2b8ba32021-06-09 00:12:59 -0600302 case '4':
Chris Daltona9f759d2021-05-18 12:37:08 -0600303 fMode = (Mode)(unichar - '1');
304 return true;
305 }
306 return false;
307}
308
309Sample* MakeTessellatedPathSample() { return new SamplePathTessellators; }
310static SampleRegistry gTessellatedPathSample(MakeTessellatedPathSample);
311
312#endif // SK_SUPPORT_GPU