blob: 5bf5a9dac6a58b01d9db4676723411fb62ef154b [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;
168 GrPipeline::InputFlags fPipelineFlags = GrPipeline::InputFlags::kHWAntialias |
169 GrPipeline::InputFlags::kWireframe;
Chris Daltond2b8ba32021-06-09 00:12:59 -0600170 Mode fMode = Mode::kWedgeMiddleOut;
Chris Daltona9f759d2021-05-18 12:37:08 -0600171
172 float fConicWeight = .5;
173
174 class Click;
175};
176
177void SamplePathTessellators::onDrawContent(SkCanvas* canvas) {
178 canvas->clear(SK_ColorBLACK);
179
180 auto ctx = canvas->recordingContext();
Robert Phillips4dca8312021-07-28 15:13:20 -0400181 auto sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(canvas);
Chris Daltona9f759d2021-05-18 12:37:08 -0600182
183 SkString error;
184 if (!sdc || !ctx) {
185 error = "GPU Only.";
186 } else if (!GrTessellationPathRenderer::IsSupported(*ctx->priv().caps())) {
187 error = "GrTessellationPathRenderer not supported.";
188 } else if (fMode >= Mode::kWedgeTessellate &&
189 !ctx->priv().caps()->shaderCaps()->tessellationSupport()) {
190 error.printf("%s requires hardware tessellation support.", ModeName(fMode));
191 }
192 if (!error.isEmpty()) {
193 canvas->clear(SK_ColorRED);
194 SkFont font(nullptr, 20);
195 SkPaint captionPaint;
196 captionPaint.setColor(SK_ColorWHITE);
197 canvas->drawString(error.c_str(), 10, 30, font, captionPaint);
198 return;
199 }
200
201 sdc->addDrawOp(GrOp::Make<SamplePathTessellatorOp>(ctx,
202 sdc->asRenderTargetProxy()->getBoundsRect(),
203 fPath, canvas->getTotalMatrix(),
204 fPipelineFlags, fMode));
205
206 // Draw the path points.
207 SkPaint pointsPaint;
208 pointsPaint.setColor(SK_ColorBLUE);
209 pointsPaint.setStrokeWidth(8);
210 SkPath devPath = fPath;
211 devPath.transform(canvas->getTotalMatrix());
212 {
213 SkAutoCanvasRestore acr(canvas, true);
214 canvas->setMatrix(SkMatrix::I());
215 SkString caption(ModeName(fMode));
216 caption.appendf(" (w=%g)", fConicWeight);
217 SkFont font(nullptr, 20);
218 SkPaint captionPaint;
219 captionPaint.setColor(SK_ColorWHITE);
220 canvas->drawString(caption, 10, 30, font, captionPaint);
221 canvas->drawPoints(SkCanvas::kPoints_PointMode, devPath.countPoints(),
222 SkPathPriv::PointData(devPath), pointsPaint);
223 }
224}
225
226class SamplePathTessellators::Click : public Sample::Click {
227public:
228 Click(int ptIdx) : fPtIdx(ptIdx) {}
229
230 void doClick(SkPath* path) {
231 SkPoint pt = path->getPoint(fPtIdx);
232 SkPathPriv::UpdatePathPoint(path, fPtIdx, pt + fCurr - fPrev);
233 }
234
235private:
236 int fPtIdx;
237};
238
239Sample::Click* SamplePathTessellators::onFindClickHandler(SkScalar x, SkScalar y,
240 skui::ModifierKey) {
241 const SkPoint* pts = SkPathPriv::PointData(fPath);
242 float fuzz = 30;
243 for (int i = 0; i < fPath.countPoints(); ++i) {
244 if (fabs(x - pts[i].x()) < fuzz && fabsf(y - pts[i].y()) < fuzz) {
245 return new Click(i);
246 }
247 }
248 return nullptr;
249}
250
251bool SamplePathTessellators::onClick(Sample::Click* click) {
252 Click* myClick = (Click*)click;
253 myClick->doClick(&fPath);
254 return true;
255}
256
257static SkPath update_weight(const SkPath& path, float w) {
258 SkPath path_;
259 for (auto [verb, pts, _] : SkPathPriv::Iterate(path)) {
260 switch (verb) {
261 case SkPathVerb::kMove:
262 path_.moveTo(pts[0]);
263 break;
264 case SkPathVerb::kLine:
265 path_.lineTo(pts[1]);
266 break;
267 case SkPathVerb::kQuad:
268 path_.quadTo(pts[1], pts[2]);
269 break;
270 case SkPathVerb::kCubic:
271 path_.cubicTo(pts[1], pts[2], pts[3]);
272 break;
273 case SkPathVerb::kConic:
274 path_.conicTo(pts[1], pts[2], (w != 1) ? w : .99f);
275 break;
276 case SkPathVerb::kClose:
277 break;
278 }
279 }
280 return path_;
281}
282
283bool SamplePathTessellators::onChar(SkUnichar unichar) {
284 switch (unichar) {
285 case 'w':
286 fPipelineFlags = (GrPipeline::InputFlags)(
287 (int)fPipelineFlags ^ (int)GrPipeline::InputFlags::kWireframe);
288 return true;
289 case 'D': {
290 fPath.dump();
291 return true;
292 }
293 case '+':
294 fConicWeight *= 2;
295 fPath = update_weight(fPath, fConicWeight);
296 return true;
297 case '=':
298 fConicWeight *= 5/4.f;
299 fPath = update_weight(fPath, fConicWeight);
300 return true;
301 case '_':
302 fConicWeight *= .5f;
303 fPath = update_weight(fPath, fConicWeight);
304 return true;
305 case '-':
306 fConicWeight *= 4/5.f;
307 fPath = update_weight(fPath, fConicWeight);
308 return true;
309 case '1':
310 case '2':
311 case '3':
Chris Daltond2b8ba32021-06-09 00:12:59 -0600312 case '4':
Chris Daltona9f759d2021-05-18 12:37:08 -0600313 fMode = (Mode)(unichar - '1');
314 return true;
315 }
316 return false;
317}
318
319Sample* MakeTessellatedPathSample() { return new SamplePathTessellators; }
320static SampleRegistry gTessellatedPathSample(MakeTessellatedPathSample);
321
322#endif // SK_SUPPORT_GPU