blob: e154694b6666d21b85832082f5b8fbb9e10c03f5 [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 Dalton198ac152021-06-09 13:49:43 -060077 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState, std::move(fProcessors),
78 fPipelineFlags);
Chris Daltona9f759d2021-05-18 12:37:08 -060079 switch (fMode) {
Chris Daltond2b8ba32021-06-09 00:12:59 -060080 using DrawInnerFan = GrPathCurveTessellator::DrawInnerFan;
81 case Mode::kWedgeMiddleOut:
82 fTessellator = GrPathWedgeTessellator::Make(alloc, fMatrix, kCyan,
Chris Dalton198ac152021-06-09 13:49:43 -060083 numVerbsToGetMiddleOut, *pipeline,
84 caps);
Chris Daltond2b8ba32021-06-09 00:12:59 -060085 break;
Chris Daltona9f759d2021-05-18 12:37:08 -060086 case Mode::kCurveMiddleOut:
Chris Dalton26666bd2021-06-08 16:25:46 -060087 fTessellator = GrPathCurveTessellator::Make(alloc, fMatrix, kCyan,
88 DrawInnerFan::kYes,
Chris Dalton198ac152021-06-09 13:49:43 -060089 numVerbsToGetMiddleOut, *pipeline,
90 caps);
Chris Daltona9f759d2021-05-18 12:37:08 -060091 break;
92 case Mode::kWedgeTessellate:
Chris Daltond2b8ba32021-06-09 00:12:59 -060093 fTessellator = GrPathWedgeTessellator::Make(alloc, fMatrix, kCyan,
Chris Dalton198ac152021-06-09 13:49:43 -060094 numVerbsToGetTessellation, *pipeline,
95 caps);
Chris Daltona9f759d2021-05-18 12:37:08 -060096 break;
97 case Mode::kCurveTessellate:
Chris Daltond9bdc322021-06-01 19:22:05 -060098 fTessellator = GrPathCurveTessellator::Make(alloc, fMatrix, kCyan,
Chris Dalton26666bd2021-06-08 16:25:46 -060099 DrawInnerFan::kYes,
Chris Dalton198ac152021-06-09 13:49:43 -0600100 numVerbsToGetTessellation, *pipeline,
101 caps);
Chris Daltona9f759d2021-05-18 12:37:08 -0600102 break;
103 }
Chris Dalton569c01b2021-05-25 10:11:46 -0600104 fTessellator->prepare(flushState, this->bounds(), fPath);
Chris Dalton2f733ec2021-06-01 12:11:57 -0600105 fProgram = GrTessellationShader::MakeProgram({alloc, flushState->writeView(),
106 &flushState->dstProxyView(),
107 flushState->renderPassBarriers(),
108 GrLoadOp::kClear, &flushState->caps()},
109 fTessellator->shader(), pipeline,
110 &GrUserStencilSettings::kUnused);
Chris Daltona9f759d2021-05-18 12:37:08 -0600111 }
112 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
113 flushState->bindPipeline(*fProgram, chainBounds);
114 fTessellator->draw(flushState);
115 }
116
117 const SkPath fPath;
118 const SkMatrix fMatrix;
119 const GrPipeline::InputFlags fPipelineFlags;
120 const Mode fMode;
121 GrPathTessellator* fTessellator = nullptr;
122 GrProgramInfo* fProgram;
123 GrProcessorSet fProcessors{SkBlendMode::kSrcOver};
124
125 friend class GrOp; // For ctor.
126};
127
128} // namespace
129
130// This sample enables wireframe and visualizes the triangles generated by path tessellators.
131class SamplePathTessellators : public Sample {
132public:
133 SamplePathTessellators() {
134#if 0
135 // For viewing middle-out triangulations of the inner fan.
136 fPath.moveTo(1, 0);
137 int numSides = 32 * 3;
138 for (int i = 1; i < numSides; ++i) {
139 float theta = 2*3.1415926535897932384626433832785 * i / numSides;
140 fPath.lineTo(std::cos(theta), std::sin(theta));
141 }
142 fPath.transform(SkMatrix::Scale(200, 200));
143 fPath.transform(SkMatrix::Translate(300, 300));
144#else
145 fPath.moveTo(100, 500);
146 fPath.cubicTo(300, 400, -100, 300, 100, 200);
147 fPath.quadTo(250, 0, 400, 200);
148 fPath.conicTo(600, 350, 400, 500, fConicWeight);
149 fPath.close();
150#endif
151 }
152
153private:
154 void onDrawContent(SkCanvas*) override;
155 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override;
156 bool onClick(Sample::Click*) override;
157 bool onChar(SkUnichar) override;
158
159 SkString name() override { return SkString("PathTessellators"); }
160
161 SkPath fPath;
162 GrPipeline::InputFlags fPipelineFlags = GrPipeline::InputFlags::kHWAntialias |
163 GrPipeline::InputFlags::kWireframe;
Chris Daltond2b8ba32021-06-09 00:12:59 -0600164 Mode fMode = Mode::kWedgeMiddleOut;
Chris Daltona9f759d2021-05-18 12:37:08 -0600165
166 float fConicWeight = .5;
167
168 class Click;
169};
170
171void SamplePathTessellators::onDrawContent(SkCanvas* canvas) {
172 canvas->clear(SK_ColorBLACK);
173
174 auto ctx = canvas->recordingContext();
175 GrSurfaceDrawContext* sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(canvas);
176
177 SkString error;
178 if (!sdc || !ctx) {
179 error = "GPU Only.";
180 } else if (!GrTessellationPathRenderer::IsSupported(*ctx->priv().caps())) {
181 error = "GrTessellationPathRenderer not supported.";
182 } else if (fMode >= Mode::kWedgeTessellate &&
183 !ctx->priv().caps()->shaderCaps()->tessellationSupport()) {
184 error.printf("%s requires hardware tessellation support.", ModeName(fMode));
185 }
186 if (!error.isEmpty()) {
187 canvas->clear(SK_ColorRED);
188 SkFont font(nullptr, 20);
189 SkPaint captionPaint;
190 captionPaint.setColor(SK_ColorWHITE);
191 canvas->drawString(error.c_str(), 10, 30, font, captionPaint);
192 return;
193 }
194
195 sdc->addDrawOp(GrOp::Make<SamplePathTessellatorOp>(ctx,
196 sdc->asRenderTargetProxy()->getBoundsRect(),
197 fPath, canvas->getTotalMatrix(),
198 fPipelineFlags, fMode));
199
200 // Draw the path points.
201 SkPaint pointsPaint;
202 pointsPaint.setColor(SK_ColorBLUE);
203 pointsPaint.setStrokeWidth(8);
204 SkPath devPath = fPath;
205 devPath.transform(canvas->getTotalMatrix());
206 {
207 SkAutoCanvasRestore acr(canvas, true);
208 canvas->setMatrix(SkMatrix::I());
209 SkString caption(ModeName(fMode));
210 caption.appendf(" (w=%g)", fConicWeight);
211 SkFont font(nullptr, 20);
212 SkPaint captionPaint;
213 captionPaint.setColor(SK_ColorWHITE);
214 canvas->drawString(caption, 10, 30, font, captionPaint);
215 canvas->drawPoints(SkCanvas::kPoints_PointMode, devPath.countPoints(),
216 SkPathPriv::PointData(devPath), pointsPaint);
217 }
218}
219
220class SamplePathTessellators::Click : public Sample::Click {
221public:
222 Click(int ptIdx) : fPtIdx(ptIdx) {}
223
224 void doClick(SkPath* path) {
225 SkPoint pt = path->getPoint(fPtIdx);
226 SkPathPriv::UpdatePathPoint(path, fPtIdx, pt + fCurr - fPrev);
227 }
228
229private:
230 int fPtIdx;
231};
232
233Sample::Click* SamplePathTessellators::onFindClickHandler(SkScalar x, SkScalar y,
234 skui::ModifierKey) {
235 const SkPoint* pts = SkPathPriv::PointData(fPath);
236 float fuzz = 30;
237 for (int i = 0; i < fPath.countPoints(); ++i) {
238 if (fabs(x - pts[i].x()) < fuzz && fabsf(y - pts[i].y()) < fuzz) {
239 return new Click(i);
240 }
241 }
242 return nullptr;
243}
244
245bool SamplePathTessellators::onClick(Sample::Click* click) {
246 Click* myClick = (Click*)click;
247 myClick->doClick(&fPath);
248 return true;
249}
250
251static SkPath update_weight(const SkPath& path, float w) {
252 SkPath path_;
253 for (auto [verb, pts, _] : SkPathPriv::Iterate(path)) {
254 switch (verb) {
255 case SkPathVerb::kMove:
256 path_.moveTo(pts[0]);
257 break;
258 case SkPathVerb::kLine:
259 path_.lineTo(pts[1]);
260 break;
261 case SkPathVerb::kQuad:
262 path_.quadTo(pts[1], pts[2]);
263 break;
264 case SkPathVerb::kCubic:
265 path_.cubicTo(pts[1], pts[2], pts[3]);
266 break;
267 case SkPathVerb::kConic:
268 path_.conicTo(pts[1], pts[2], (w != 1) ? w : .99f);
269 break;
270 case SkPathVerb::kClose:
271 break;
272 }
273 }
274 return path_;
275}
276
277bool SamplePathTessellators::onChar(SkUnichar unichar) {
278 switch (unichar) {
279 case 'w':
280 fPipelineFlags = (GrPipeline::InputFlags)(
281 (int)fPipelineFlags ^ (int)GrPipeline::InputFlags::kWireframe);
282 return true;
283 case 'D': {
284 fPath.dump();
285 return true;
286 }
287 case '+':
288 fConicWeight *= 2;
289 fPath = update_weight(fPath, fConicWeight);
290 return true;
291 case '=':
292 fConicWeight *= 5/4.f;
293 fPath = update_weight(fPath, fConicWeight);
294 return true;
295 case '_':
296 fConicWeight *= .5f;
297 fPath = update_weight(fPath, fConicWeight);
298 return true;
299 case '-':
300 fConicWeight *= 4/5.f;
301 fPath = update_weight(fPath, fConicWeight);
302 return true;
303 case '1':
304 case '2':
305 case '3':
Chris Daltond2b8ba32021-06-09 00:12:59 -0600306 case '4':
Chris Daltona9f759d2021-05-18 12:37:08 -0600307 fMode = (Mode)(unichar - '1');
308 return true;
309 }
310 return false;
311}
312
313Sample* MakeTessellatedPathSample() { return new SamplePathTessellators; }
314static SampleRegistry gTessellatedPathSample(MakeTessellatedPathSample);
315
316#endif // SK_SUPPORT_GPU