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