blob: a9715bc1d4706095268a8c6b2bcc96deb74ed229 [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"
Robert Phillips06273bc2021-08-11 15:43:50 -040015#include "src/gpu/GrOpFlushState.h"
Chris Daltona9f759d2021-05-18 12:37:08 -060016#include "src/gpu/GrRecordingContextPriv.h"
Robert Phillips06273bc2021-08-11 15:43:50 -040017#include "src/gpu/ops/GrDrawOp.h"
18#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
Robert Phillipse453fa02021-08-19 14:57:05 -040019#include "src/gpu/ops/TessellationPathRenderer.h"
Chris Dalton0de8a962021-10-13 10:09:50 -060020#include "src/gpu/tessellate/PathCurveTessellator.h"
21#include "src/gpu/tessellate/PathWedgeTessellator.h"
Chris Dalton3b412782021-06-01 13:40:03 -060022#include "src/gpu/tessellate/shaders/GrPathTessellationShader.h"
Robert Phillips4dca8312021-07-28 15:13:20 -040023#include "src/gpu/v1/SurfaceDrawContext_v1.h"
Chris Daltona9f759d2021-05-18 12:37:08 -060024
Chris Dalton49c76672021-10-20 09:45:03 -060025namespace skgpu {
26
Chris Daltona9f759d2021-05-18 12:37:08 -060027namespace {
28
29enum class Mode {
Chris Daltond2b8ba32021-06-09 00:12:59 -060030 kWedgeMiddleOut,
Chris Daltona9f759d2021-05-18 12:37:08 -060031 kCurveMiddleOut,
32 kWedgeTessellate,
33 kCurveTessellate
34};
35
36static const char* ModeName(Mode mode) {
37 switch (mode) {
Chris Daltond2b8ba32021-06-09 00:12:59 -060038 case Mode::kWedgeMiddleOut:
39 return "MiddleOutShader (kWedges)";
Chris Daltona9f759d2021-05-18 12:37:08 -060040 case Mode::kCurveMiddleOut:
Chris Daltond2b8ba32021-06-09 00:12:59 -060041 return "MiddleOutShader (kCurves)";
Chris Daltona9f759d2021-05-18 12:37:08 -060042 case Mode::kWedgeTessellate:
Chris Daltond2b8ba32021-06-09 00:12:59 -060043 return "HardwareWedgeShader";
Chris Daltona9f759d2021-05-18 12:37:08 -060044 case Mode::kCurveTessellate:
Chris Daltond2b8ba32021-06-09 00:12:59 -060045 return "HardwareCurveShader";
Chris Daltona9f759d2021-05-18 12:37:08 -060046 }
47 SkUNREACHABLE;
48}
49
50// Draws a path directly to the screen using a specific tessellator.
51class SamplePathTessellatorOp : public GrDrawOp {
52private:
53 DEFINE_OP_CLASS_ID
54
55 SamplePathTessellatorOp(const SkRect& drawBounds, const SkPath& path, const SkMatrix& m,
56 GrPipeline::InputFlags pipelineFlags, Mode mode)
57 : GrDrawOp(ClassID())
58 , fPath(path)
59 , fMatrix(m)
60 , fPipelineFlags(pipelineFlags)
61 , fMode(mode) {
62 this->setBounds(drawBounds, HasAABloat::kNo, IsHairline::kNo);
63 }
64 const char* name() const override { return "SamplePathTessellatorOp"; }
Robert Phillips294723d2021-06-17 09:23:58 -040065 void visitProxies(const GrVisitProxyFunc&) const override {}
Chris Daltona9f759d2021-05-18 12:37:08 -060066 FixedFunctionFlags fixedFunctionFlags() const override {
67 return FixedFunctionFlags::kUsesHWAA;
68 }
69 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
70 GrClampType clampType) override {
71 SkPMColor4f color;
72 return fProcessors.finalize(SK_PMColor4fWHITE, GrProcessorAnalysisCoverage::kNone, clip,
73 nullptr, caps, clampType, &color);
74 }
75 void onPrePrepare(GrRecordingContext*, const GrSurfaceProxyView&, GrAppliedClip*,
John Stiles52cb1d02021-06-02 11:58:05 -040076 const GrDstProxyView&, GrXferBarrierFlags, GrLoadOp colorLoadOp) override {}
Chris Daltona9f759d2021-05-18 12:37:08 -060077 void onPrepare(GrOpFlushState* flushState) override {
Chris Dalton2f733ec2021-06-01 12:11:57 -060078 constexpr static SkPMColor4f kCyan = {0,1,1,1};
Chris Daltona9f759d2021-05-18 12:37:08 -060079 auto alloc = flushState->allocator();
Chris Dalton69669812021-07-27 10:00:12 -060080 const SkMatrix& shaderMatrix = SkMatrix::I();
81 const SkMatrix& pathMatrix = fMatrix;
Chris Daltond2b8ba32021-06-09 00:12:59 -060082 const GrCaps& caps = flushState->caps();
83 int numVerbsToGetMiddleOut = 0;
84 int numVerbsToGetTessellation = caps.minPathVerbsForHwTessellation();
Chris Dalton198ac152021-06-09 13:49:43 -060085 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState, std::move(fProcessors),
86 fPipelineFlags);
Chris Daltona9f759d2021-05-18 12:37:08 -060087 switch (fMode) {
Chris Dalton49c76672021-10-20 09:45:03 -060088 using DrawInnerFan = PathCurveTessellator::DrawInnerFan;
Chris Daltond2b8ba32021-06-09 00:12:59 -060089 case Mode::kWedgeMiddleOut:
Chris Dalton49c76672021-10-20 09:45:03 -060090 fTessellator = PathWedgeTessellator::Make(alloc,
91 shaderMatrix,
92 kCyan,
93 numVerbsToGetMiddleOut,
94 *pipeline,
Chris Dalton0de8a962021-10-13 10:09:50 -060095
Chris Dalton49c76672021-10-20 09:45:03 -060096 caps);
Chris Daltond2b8ba32021-06-09 00:12:59 -060097 break;
Chris Daltona9f759d2021-05-18 12:37:08 -060098 case Mode::kCurveMiddleOut:
Chris Dalton49c76672021-10-20 09:45:03 -060099 fTessellator = PathCurveTessellator::Make(alloc,
100 shaderMatrix,
101 kCyan,
102 DrawInnerFan::kYes,
103 numVerbsToGetMiddleOut,
104 *pipeline,
105 caps);
Chris Daltona9f759d2021-05-18 12:37:08 -0600106 break;
107 case Mode::kWedgeTessellate:
Chris Dalton49c76672021-10-20 09:45:03 -0600108 fTessellator = PathWedgeTessellator::Make(alloc,
109 shaderMatrix,
110 kCyan,
111 numVerbsToGetTessellation,
112 *pipeline,
113 caps);
Chris Daltona9f759d2021-05-18 12:37:08 -0600114 break;
115 case Mode::kCurveTessellate:
Chris Dalton49c76672021-10-20 09:45:03 -0600116 fTessellator = PathCurveTessellator::Make(alloc,
117 shaderMatrix,
118 kCyan,
119 DrawInnerFan::kYes,
120 numVerbsToGetTessellation,
121 *pipeline,
122 caps);
Chris Daltona9f759d2021-05-18 12:37:08 -0600123 break;
124 }
Chris Dalton08a97112021-11-11 11:32:12 -0700125 fTessellator->prepare(flushState, {pathMatrix, fPath, kCyan}, fPath.countVerbs());
Chris Dalton2f733ec2021-06-01 12:11:57 -0600126 fProgram = GrTessellationShader::MakeProgram({alloc, flushState->writeView(),
Chris Dalton2a26c502021-08-26 10:05:11 -0600127 flushState->usesMSAASurface(),
Chris Dalton2f733ec2021-06-01 12:11:57 -0600128 &flushState->dstProxyView(),
129 flushState->renderPassBarriers(),
130 GrLoadOp::kClear, &flushState->caps()},
131 fTessellator->shader(), pipeline,
132 &GrUserStencilSettings::kUnused);
Chris Daltona9f759d2021-05-18 12:37:08 -0600133 }
134 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
135 flushState->bindPipeline(*fProgram, chainBounds);
136 fTessellator->draw(flushState);
137 }
138
139 const SkPath fPath;
140 const SkMatrix fMatrix;
141 const GrPipeline::InputFlags fPipelineFlags;
142 const Mode fMode;
Chris Dalton49c76672021-10-20 09:45:03 -0600143 PathTessellator* fTessellator = nullptr;
Chris Daltona9f759d2021-05-18 12:37:08 -0600144 GrProgramInfo* fProgram;
145 GrProcessorSet fProcessors{SkBlendMode::kSrcOver};
146
147 friend class GrOp; // For ctor.
148};
149
150} // namespace
151
152// This sample enables wireframe and visualizes the triangles generated by path tessellators.
153class SamplePathTessellators : public Sample {
154public:
155 SamplePathTessellators() {
156#if 0
157 // For viewing middle-out triangulations of the inner fan.
158 fPath.moveTo(1, 0);
159 int numSides = 32 * 3;
160 for (int i = 1; i < numSides; ++i) {
161 float theta = 2*3.1415926535897932384626433832785 * i / numSides;
162 fPath.lineTo(std::cos(theta), std::sin(theta));
163 }
164 fPath.transform(SkMatrix::Scale(200, 200));
165 fPath.transform(SkMatrix::Translate(300, 300));
166#else
167 fPath.moveTo(100, 500);
168 fPath.cubicTo(300, 400, -100, 300, 100, 200);
169 fPath.quadTo(250, 0, 400, 200);
170 fPath.conicTo(600, 350, 400, 500, fConicWeight);
171 fPath.close();
172#endif
173 }
174
175private:
176 void onDrawContent(SkCanvas*) override;
177 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override;
178 bool onClick(Sample::Click*) override;
179 bool onChar(SkUnichar) override;
180
181 SkString name() override { return SkString("PathTessellators"); }
182
183 SkPath fPath;
Chris Daltoneb0195e2021-08-18 21:39:02 -0600184 GrPipeline::InputFlags fPipelineFlags = GrPipeline::InputFlags::kWireframe;
Chris Daltond2b8ba32021-06-09 00:12:59 -0600185 Mode fMode = Mode::kWedgeMiddleOut;
Chris Daltona9f759d2021-05-18 12:37:08 -0600186
187 float fConicWeight = .5;
188
189 class Click;
190};
191
192void SamplePathTessellators::onDrawContent(SkCanvas* canvas) {
193 canvas->clear(SK_ColorBLACK);
194
195 auto ctx = canvas->recordingContext();
Robert Phillips4dca8312021-07-28 15:13:20 -0400196 auto sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(canvas);
Chris Daltona9f759d2021-05-18 12:37:08 -0600197
198 SkString error;
199 if (!sdc || !ctx) {
200 error = "GPU Only.";
Robert Phillips24d622d2021-08-19 17:04:05 -0400201 } else if (!skgpu::v1::TessellationPathRenderer::IsSupported(*ctx->priv().caps())) {
202 error = "TessellationPathRenderer not supported.";
Chris Daltona9f759d2021-05-18 12:37:08 -0600203 } else if (fMode >= Mode::kWedgeTessellate &&
204 !ctx->priv().caps()->shaderCaps()->tessellationSupport()) {
205 error.printf("%s requires hardware tessellation support.", ModeName(fMode));
206 }
207 if (!error.isEmpty()) {
208 canvas->clear(SK_ColorRED);
209 SkFont font(nullptr, 20);
210 SkPaint captionPaint;
211 captionPaint.setColor(SK_ColorWHITE);
212 canvas->drawString(error.c_str(), 10, 30, font, captionPaint);
213 return;
214 }
215
216 sdc->addDrawOp(GrOp::Make<SamplePathTessellatorOp>(ctx,
217 sdc->asRenderTargetProxy()->getBoundsRect(),
218 fPath, canvas->getTotalMatrix(),
219 fPipelineFlags, fMode));
220
221 // Draw the path points.
222 SkPaint pointsPaint;
223 pointsPaint.setColor(SK_ColorBLUE);
224 pointsPaint.setStrokeWidth(8);
225 SkPath devPath = fPath;
226 devPath.transform(canvas->getTotalMatrix());
227 {
228 SkAutoCanvasRestore acr(canvas, true);
229 canvas->setMatrix(SkMatrix::I());
230 SkString caption(ModeName(fMode));
231 caption.appendf(" (w=%g)", fConicWeight);
232 SkFont font(nullptr, 20);
233 SkPaint captionPaint;
234 captionPaint.setColor(SK_ColorWHITE);
235 canvas->drawString(caption, 10, 30, font, captionPaint);
236 canvas->drawPoints(SkCanvas::kPoints_PointMode, devPath.countPoints(),
237 SkPathPriv::PointData(devPath), pointsPaint);
238 }
239}
240
241class SamplePathTessellators::Click : public Sample::Click {
242public:
243 Click(int ptIdx) : fPtIdx(ptIdx) {}
244
245 void doClick(SkPath* path) {
246 SkPoint pt = path->getPoint(fPtIdx);
247 SkPathPriv::UpdatePathPoint(path, fPtIdx, pt + fCurr - fPrev);
248 }
249
250private:
251 int fPtIdx;
252};
253
254Sample::Click* SamplePathTessellators::onFindClickHandler(SkScalar x, SkScalar y,
255 skui::ModifierKey) {
256 const SkPoint* pts = SkPathPriv::PointData(fPath);
257 float fuzz = 30;
258 for (int i = 0; i < fPath.countPoints(); ++i) {
259 if (fabs(x - pts[i].x()) < fuzz && fabsf(y - pts[i].y()) < fuzz) {
260 return new Click(i);
261 }
262 }
263 return nullptr;
264}
265
266bool SamplePathTessellators::onClick(Sample::Click* click) {
267 Click* myClick = (Click*)click;
268 myClick->doClick(&fPath);
269 return true;
270}
271
272static SkPath update_weight(const SkPath& path, float w) {
273 SkPath path_;
274 for (auto [verb, pts, _] : SkPathPriv::Iterate(path)) {
275 switch (verb) {
276 case SkPathVerb::kMove:
277 path_.moveTo(pts[0]);
278 break;
279 case SkPathVerb::kLine:
280 path_.lineTo(pts[1]);
281 break;
282 case SkPathVerb::kQuad:
283 path_.quadTo(pts[1], pts[2]);
284 break;
285 case SkPathVerb::kCubic:
286 path_.cubicTo(pts[1], pts[2], pts[3]);
287 break;
288 case SkPathVerb::kConic:
289 path_.conicTo(pts[1], pts[2], (w != 1) ? w : .99f);
290 break;
291 case SkPathVerb::kClose:
292 break;
293 }
294 }
295 return path_;
296}
297
298bool SamplePathTessellators::onChar(SkUnichar unichar) {
299 switch (unichar) {
300 case 'w':
301 fPipelineFlags = (GrPipeline::InputFlags)(
302 (int)fPipelineFlags ^ (int)GrPipeline::InputFlags::kWireframe);
303 return true;
304 case 'D': {
305 fPath.dump();
306 return true;
307 }
308 case '+':
309 fConicWeight *= 2;
310 fPath = update_weight(fPath, fConicWeight);
311 return true;
312 case '=':
313 fConicWeight *= 5/4.f;
314 fPath = update_weight(fPath, fConicWeight);
315 return true;
316 case '_':
317 fConicWeight *= .5f;
318 fPath = update_weight(fPath, fConicWeight);
319 return true;
320 case '-':
321 fConicWeight *= 4/5.f;
322 fPath = update_weight(fPath, fConicWeight);
323 return true;
324 case '1':
325 case '2':
326 case '3':
Chris Daltond2b8ba32021-06-09 00:12:59 -0600327 case '4':
Chris Daltona9f759d2021-05-18 12:37:08 -0600328 fMode = (Mode)(unichar - '1');
329 return true;
330 }
331 return false;
332}
333
334Sample* MakeTessellatedPathSample() { return new SamplePathTessellators; }
335static SampleRegistry gTessellatedPathSample(MakeTessellatedPathSample);
336
Chris Dalton49c76672021-10-20 09:45:03 -0600337} // namespace skgpu
338
Chris Daltona9f759d2021-05-18 12:37:08 -0600339#endif // SK_SUPPORT_GPU