blob: 3fb4fb6cbfe44714fa46b2de512dba28c90ef86d [file] [log] [blame]
Chris Dalton1a325d22017-07-14 15:17:41 -06001/*
2 * Copyright 2017 Google Inc.
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 "SkTypes.h"
9
10#if SK_SUPPORT_GPU
11
12#include "GrContextPriv.h"
Chris Dalton7f578bf2017-09-05 16:46:48 -060013#include "GrPathUtils.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060014#include "GrRenderTargetContext.h"
15#include "GrRenderTargetContextPriv.h"
16#include "GrResourceProvider.h"
17#include "SampleCode.h"
18#include "SkCanvas.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060019#include "SkMakeUnique.h"
20#include "SkPaint.h"
21#include "SkPath.h"
22#include "SkView.h"
23#include "ccpr/GrCCPRCoverageProcessor.h"
Chris Dalton419a94d2017-08-28 10:24:22 -060024#include "ccpr/GrCCPRGeometry.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060025#include "gl/GrGLGpu.cpp"
26#include "ops/GrDrawOp.h"
27
Chris Daltonc1e59632017-09-05 00:30:07 -060028using TriangleInstance = GrCCPRCoverageProcessor::TriangleInstance;
29using CurveInstance = GrCCPRCoverageProcessor::CurveInstance;
Chris Dalton1a325d22017-07-14 15:17:41 -060030using Mode = GrCCPRCoverageProcessor::Mode;
31
32static int num_points(Mode mode) {
Chris Dalton7f578bf2017-09-05 16:46:48 -060033 return mode >= Mode::kSerpentineHulls ? 4 : 3;
Chris Dalton1a325d22017-07-14 15:17:41 -060034}
35
Chris Daltonc1e59632017-09-05 00:30:07 -060036static int is_quadratic(Mode mode) {
Chris Dalton7f578bf2017-09-05 16:46:48 -060037 return mode >= Mode::kQuadraticHulls && mode < Mode::kSerpentineHulls;
Chris Dalton1a325d22017-07-14 15:17:41 -060038}
39
40/**
41 * This sample visualizes the AA bloat geometry generated by the ccpr geometry shaders. It
42 * increases the AA bloat by 50x and outputs color instead of coverage (coverage=+1 -> green,
43 * coverage=0 -> black, coverage=-1 -> red). Use the keys 1-7 to cycle through the different
44 * geometry processors.
45 */
46class CCPRGeometryView : public SampleView {
47public:
48 CCPRGeometryView() { this->updateGpuData(); }
49 void onDrawContent(SkCanvas*) override;
50
51 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override;
52 bool onClick(SampleView::Click*) override;
53 bool onQuery(SkEvent* evt) override;
54
55private:
56 class Click;
57 class Op;
58
59 void updateAndInval() {
60 this->updateGpuData();
61 this->inval(nullptr);
62 }
63
64 void updateGpuData();
65
66 Mode fMode = Mode::kTriangleHulls;
Chris Dalton7f578bf2017-09-05 16:46:48 -060067 SkMatrix fCubicKLM;
Chris Dalton1a325d22017-07-14 15:17:41 -060068
69 SkPoint fPoints[4] = {
70 {100.05f, 100.05f},
71 {100.05f, 300.95f},
72 {400.75f, 300.95f},
73 {400.75f, 100.05f}
74 };
75
Chris Daltonc1e59632017-09-05 00:30:07 -060076 SkTArray<SkPoint> fGpuPoints;
77 SkTArray<int32_t> fInstanceData;
78 int fInstanceCount;
Chris Dalton1a325d22017-07-14 15:17:41 -060079
80 typedef SampleView INHERITED;
81};
82
83class CCPRGeometryView::Op : public GrDrawOp {
84 DEFINE_OP_CLASS_ID
85
86public:
87 Op(CCPRGeometryView* view)
88 : INHERITED(ClassID())
89 , fView(view) {
90 this->setBounds(SkRect::MakeLargest(), GrOp::HasAABloat::kNo, GrOp::IsZeroArea::kNo);
91 }
92
93 const char* name() const override { return "[Testing/Sample code] CCPRGeometryView::Op"; }
94
95private:
96 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
97 RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*) override {
98 return RequiresDstTexture::kNo;
99 }
100 bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; }
101 void onPrepare(GrOpFlushState*) override {}
102 void onExecute(GrOpFlushState*) override;
103
104 CCPRGeometryView* fView;
105
106 typedef GrDrawOp INHERITED;
107};
108
Chris Dalton7f578bf2017-09-05 16:46:48 -0600109static void draw_klm_line(int w, int h, SkCanvas* canvas, const SkScalar line[3], SkColor color) {
110 SkPoint p1, p2;
111 if (SkScalarAbs(line[1]) > SkScalarAbs(line[0])) {
112 // Draw from vertical edge to vertical edge.
113 p1 = {0, -line[2] / line[1]};
114 p2 = {(SkScalar) w, (-line[2] - w * line[0]) / line[1]};
115 } else {
116 // Draw from horizontal edge to horizontal edge.
117 p1 = {-line[2] / line[0], 0};
118 p2 = {(-line[2] - h * line[1]) / line[0], (SkScalar) h};
119 }
120
121 SkPaint linePaint;
122 linePaint.setColor(color);
123 linePaint.setAlpha(128);
124 linePaint.setStyle(SkPaint::kStroke_Style);
125 linePaint.setStrokeWidth(0);
126 linePaint.setAntiAlias(true);
127 canvas->drawLine(p1, p2, linePaint);
128}
129
Chris Dalton1a325d22017-07-14 15:17:41 -0600130void CCPRGeometryView::onDrawContent(SkCanvas* canvas) {
131 SkAutoCanvasRestore acr(canvas, true);
132 canvas->setMatrix(SkMatrix::I());
133
134 SkPath outline;
135 outline.moveTo(fPoints[0]);
136 if (4 == num_points(fMode)) {
137 outline.cubicTo(fPoints[1], fPoints[2], fPoints[3]);
Chris Daltonc1e59632017-09-05 00:30:07 -0600138 } else if (is_quadratic(fMode)) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600139 outline.quadTo(fPoints[1], fPoints[3]);
140 } else {
141 outline.lineTo(fPoints[1]);
142 outline.lineTo(fPoints[3]);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600143 outline.close();
Chris Dalton1a325d22017-07-14 15:17:41 -0600144 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600145
146 SkPaint outlinePaint;
147 outlinePaint.setColor(0x30000000);
148 outlinePaint.setStyle(SkPaint::kStroke_Style);
149 outlinePaint.setStrokeWidth(0);
150 outlinePaint.setAntiAlias(true);
Chris Dalton1a325d22017-07-14 15:17:41 -0600151 canvas->drawPath(outline, outlinePaint);
152
Chris Dalton7f578bf2017-09-05 16:46:48 -0600153#if 0
154 SkPaint gridPaint;
155 gridPaint.setColor(0x10000000);
156 gridPaint.setStyle(SkPaint::kStroke_Style);
157 gridPaint.setStrokeWidth(0);
158 gridPaint.setAntiAlias(true);
159 for (int y = 0; y < this->height(); y += GrCCPRCoverageProcessor::kDebugBloat) {
160 canvas->drawLine(0, y, this->width(), y, gridPaint);
161 }
162 for (int x = 0; x < this->width(); x += GrCCPRCoverageProcessor::kDebugBloat) {
163 canvas->drawLine(x, 0, x, this->height(), outlinePaint);
164 }
165#endif
166
Chris Dalton1a325d22017-07-14 15:17:41 -0600167 const char* caption = "Use GPU backend to visualize geometry.";
168
169 if (GrRenderTargetContext* rtc =
170 canvas->internal_private_accessTopLayerRenderTargetContext()) {
171 rtc->priv().testingOnly_addDrawOp(skstd::make_unique<Op>(this));
172 caption = GrCCPRCoverageProcessor::GetProcessorName(fMode);
173 }
174
175 SkPaint pointsPaint;
176 pointsPaint.setColor(SK_ColorBLUE);
177 pointsPaint.setStrokeWidth(8);
178 pointsPaint.setAntiAlias(true);
179
180 if (4 == num_points(fMode)) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600181 int w = this->width(), h = this->height();
Chris Dalton1a325d22017-07-14 15:17:41 -0600182 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, fPoints, pointsPaint);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600183 draw_klm_line(w, h, canvas, &fCubicKLM[0], SK_ColorYELLOW);
184 draw_klm_line(w, h, canvas, &fCubicKLM[3], SK_ColorBLUE);
185 draw_klm_line(w, h, canvas, &fCubicKLM[6], SK_ColorRED);
Chris Dalton1a325d22017-07-14 15:17:41 -0600186 } else {
187 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, fPoints, pointsPaint);
188 canvas->drawPoints(SkCanvas::kPoints_PointMode, 1, fPoints + 3, pointsPaint);
189 }
190
191 SkPaint captionPaint;
192 captionPaint.setTextSize(20);
193 captionPaint.setColor(SK_ColorBLACK);
194 captionPaint.setAntiAlias(true);
195 canvas->drawText(caption, strlen(caption), 10, 30, captionPaint);
196}
197
198void CCPRGeometryView::updateGpuData() {
199 int vertexCount = num_points(fMode);
Chris Dalton1a325d22017-07-14 15:17:41 -0600200
201 fGpuPoints.reset();
Chris Daltonc1e59632017-09-05 00:30:07 -0600202 fInstanceData.reset();
203 fInstanceCount = 0;
Chris Dalton1a325d22017-07-14 15:17:41 -0600204
205 if (4 == vertexCount) {
206 double t[2], s[2];
Chris Dalton7f578bf2017-09-05 16:46:48 -0600207 SkCubicType type = GrPathUtils::getCubicKLM(fPoints, &fCubicKLM, t, s);
208 if (Mode::kSerpentineHulls == fMode && SkCubicType::kLoop == type) {
209 fMode = Mode::kLoopHulls;
210 }
211 if (Mode::kSerpentineCorners == fMode && SkCubicType::kLoop == type) {
212 fMode = Mode::kLoopCorners;
213 }
214 if (Mode::kLoopHulls == fMode && SkCubicType::kLoop != type) {
215 fMode = Mode::kSerpentineHulls;
216 }
217 if (Mode::kLoopCorners == fMode && SkCubicType::kLoop != type) {
218 fMode = Mode::kSerpentineCorners;
219 }
220
221 GrCCPRGeometry geometry;
222 geometry.beginContour(fPoints[0]);
223 geometry.cubicTo(fPoints[1], fPoints[2], fPoints[3],
224 GrCCPRCoverageProcessor::kDebugBloat / 2,
225 GrCCPRCoverageProcessor::kDebugBloat / 2);
226 geometry.endContour();
227 fGpuPoints.push_back_n(geometry.points().count(), geometry.points().begin());
228 int ptsIdx = 0;
229 for (GrCCPRGeometry::Verb verb : geometry.verbs()) {
230 switch (verb) {
231 case GrCCPRGeometry::Verb::kLineTo:
232 ++ptsIdx;
233 continue;
234 case GrCCPRGeometry::Verb::kMonotonicQuadraticTo:
235 ptsIdx += 2;
236 continue;
237 case GrCCPRGeometry::Verb::kMonotonicSerpentineTo:
238 case GrCCPRGeometry::Verb::kMonotonicLoopTo:
239 fInstanceData.push_back(ptsIdx);
240 fInstanceData.push_back(0); // Atlas offset.
241 ptsIdx += 3;
242 ++fInstanceCount;
243 continue;
244 default: continue;
Chris Dalton1a325d22017-07-14 15:17:41 -0600245 }
246 }
Chris Daltonc1e59632017-09-05 00:30:07 -0600247 } else if (is_quadratic(fMode)) {
248 GrCCPRGeometry geometry;
249 geometry.beginContour(fPoints[0]);
250 geometry.quadraticTo(fPoints[1], fPoints[3]);
251 geometry.endContour();
252 fGpuPoints.push_back_n(geometry.points().count(), geometry.points().begin());
253 for (GrCCPRGeometry::Verb verb : geometry.verbs()) {
254 if (GrCCPRGeometry::Verb::kBeginContour == verb ||
255 GrCCPRGeometry::Verb::kEndOpenContour == verb ||
256 GrCCPRGeometry::Verb::kEndClosedContour == verb) {
257 continue;
258 }
259 SkASSERT(GrCCPRGeometry::Verb::kMonotonicQuadraticTo == verb);
260 fInstanceData.push_back(2 * fInstanceCount++); // Pts idx.
261 fInstanceData.push_back(0); // Atlas offset.
Chris Daltonb072bb62017-08-07 09:00:46 -0600262 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600263 } else {
Chris Daltonb072bb62017-08-07 09:00:46 -0600264 fGpuPoints.push_back(fPoints[0]);
Chris Daltonb072bb62017-08-07 09:00:46 -0600265 fGpuPoints.push_back(fPoints[1]);
Chris Daltonc1e59632017-09-05 00:30:07 -0600266 fGpuPoints.push_back(fPoints[3]);
267 fInstanceData.push_back(0);
268 fInstanceData.push_back(1);
269 fInstanceData.push_back(2);
270 fInstanceData.push_back(0); // Atlas offset.
271 fInstanceCount = 1;
Chris Dalton1a325d22017-07-14 15:17:41 -0600272 }
273}
274
275void CCPRGeometryView::Op::onExecute(GrOpFlushState* state) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600276 if (fView->fInstanceData.empty()) {
277 return;
278 }
279
Chris Dalton1a325d22017-07-14 15:17:41 -0600280 GrResourceProvider* rp = state->resourceProvider();
281 GrContext* context = state->gpu()->getContext();
282 GrGLGpu* glGpu = kOpenGL_GrBackend == context->contextPriv().getBackend() ?
283 static_cast<GrGLGpu*>(state->gpu()) : nullptr;
284 int vertexCount = num_points(fView->fMode);
285
286 sk_sp<GrBuffer> pointsBuffer(rp->createBuffer(fView->fGpuPoints.count() * sizeof(SkPoint),
287 kTexel_GrBufferType, kDynamic_GrAccessPattern,
288 GrResourceProvider::kNoPendingIO_Flag |
289 GrResourceProvider::kRequireGpuMemory_Flag,
290 fView->fGpuPoints.begin()));
291 if (!pointsBuffer) {
292 return;
293 }
294
Chris Daltonc1e59632017-09-05 00:30:07 -0600295 sk_sp<GrBuffer> instanceBuffer(rp->createBuffer(fView->fInstanceData.count() * sizeof(int),
Chris Dalton1a325d22017-07-14 15:17:41 -0600296 kVertex_GrBufferType, kDynamic_GrAccessPattern,
297 GrResourceProvider::kNoPendingIO_Flag |
298 GrResourceProvider::kRequireGpuMemory_Flag,
Chris Daltonc1e59632017-09-05 00:30:07 -0600299 fView->fInstanceData.begin()));
Chris Dalton1a325d22017-07-14 15:17:41 -0600300 if (!instanceBuffer) {
301 return;
302 }
303
Robert Phillips2890fbf2017-07-26 15:48:41 -0400304 GrPipeline pipeline(state->drawOpArgs().fProxy, GrPipeline::ScissorState::kDisabled,
Chris Dalton1a325d22017-07-14 15:17:41 -0600305 SkBlendMode::kSrcOver);
306
307 GrCCPRCoverageProcessor ccprProc(fView->fMode, pointsBuffer.get());
308 SkDEBUGCODE(ccprProc.enableDebugVisualizations();)
309
310 GrMesh mesh(4 == vertexCount ? GrPrimitiveType::kLinesAdjacency : GrPrimitiveType::kTriangles);
Chris Daltonc1e59632017-09-05 00:30:07 -0600311 mesh.setInstanced(instanceBuffer.get(), fView->fInstanceCount, 0, vertexCount);
Chris Dalton1a325d22017-07-14 15:17:41 -0600312
313 if (glGpu) {
314 glGpu->handleDirtyContext();
315 GR_GL_CALL(glGpu->glInterface(), PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
316 GR_GL_CALL(glGpu->glInterface(), Enable(GR_GL_LINE_SMOOTH));
317 }
318
Greg Daniel500d58b2017-08-24 15:59:33 -0400319 state->rtCommandBuffer()->draw(pipeline, ccprProc, &mesh, nullptr, 1, this->bounds());
Chris Dalton1a325d22017-07-14 15:17:41 -0600320
321 if (glGpu) {
322 context->resetContext(kMisc_GrGLBackendState);
323 }
324}
325
326class CCPRGeometryView::Click : public SampleView::Click {
327public:
328 Click(SkView* target, int ptIdx) : SampleView::Click(target), fPtIdx(ptIdx) {}
329
330 void doClick(SkPoint points[]) {
331 if (fPtIdx >= 0) {
332 this->dragPoint(points, fPtIdx);
333 } else {
334 for (int i = 0; i < 4; ++i) {
335 this->dragPoint(points, i);
336 }
337 }
338 }
339
340private:
341 void dragPoint(SkPoint points[], int idx) {
342 SkIPoint delta = fICurr - fIPrev;
343 points[idx] += SkPoint::Make(delta.x(), delta.y());
344 }
345
346 int fPtIdx;
347};
348
349SkView::Click* CCPRGeometryView::onFindClickHandler(SkScalar x, SkScalar y, unsigned) {
350 for (int i = 0; i < 4; ++i) {
351 if (4 != num_points(fMode) && 2 == i) {
352 continue;
353 }
354 if (fabs(x - fPoints[i].x()) < 20 && fabsf(y - fPoints[i].y()) < 20) {
355 return new Click(this, i);
356 }
357 }
358 return new Click(this, -1);
359}
360
361bool CCPRGeometryView::onClick(SampleView::Click* click) {
362 Click* myClick = (Click*) click;
363 myClick->doClick(fPoints);
364 this->updateAndInval();
365 return true;
366}
367
368bool CCPRGeometryView::onQuery(SkEvent* evt) {
369 if (SampleCode::TitleQ(*evt)) {
370 SampleCode::TitleR(evt, "CCPRGeometry");
371 return true;
372 }
373 SkUnichar unichar;
374 if (SampleCode::CharQ(*evt, &unichar)) {
375 if (unichar >= '1' && unichar <= '7') {
376 fMode = Mode(unichar - '1');
377 if (fMode >= Mode::kCombinedTriangleHullsAndEdges) {
378 fMode = Mode(int(fMode) + 1);
379 }
Chris Dalton7f578bf2017-09-05 16:46:48 -0600380 if (fMode >= Mode::kLoopHulls) {
381 // '6' -> kSerpentineHulls, '7' -> kSerpentineCorners. updateGpuData converts to
382 // kLoop* if needed.
383 fMode = Mode(int(fMode) + 1);
384 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600385 this->updateAndInval();
386 return true;
387 }
388 if (unichar == 'D') {
389 SkDebugf(" SkPoint fPoints[4] = {\n");
Chris Dalton7f578bf2017-09-05 16:46:48 -0600390 SkDebugf(" {%ff, %ff},\n", fPoints[0].x(), fPoints[0].y());
391 SkDebugf(" {%ff, %ff},\n", fPoints[1].x(), fPoints[1].y());
392 SkDebugf(" {%ff, %ff},\n", fPoints[2].x(), fPoints[2].y());
393 SkDebugf(" {%ff, %ff}\n", fPoints[3].x(), fPoints[3].y());
Chris Dalton1a325d22017-07-14 15:17:41 -0600394 SkDebugf(" };\n");
395 return true;
396 }
397 }
398 return this->INHERITED::onQuery(evt);
399}
400
401DEF_SAMPLE( return new CCPRGeometryView; )
402
403#endif // SK_SUPPORT_GPU