blob: c3cd4707dcf2e33a003f0d2e84364f1eaaa3e77f [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
Chris Daltona640c492017-09-11 22:04:03 -070032static constexpr float kDebugBloat = 40;
33
Chris Dalton1a325d22017-07-14 15:17:41 -060034static int num_points(Mode mode) {
Chris Dalton7f578bf2017-09-05 16:46:48 -060035 return mode >= Mode::kSerpentineHulls ? 4 : 3;
Chris Dalton1a325d22017-07-14 15:17:41 -060036}
37
Chris Daltonc1e59632017-09-05 00:30:07 -060038static int is_quadratic(Mode mode) {
Chris Dalton7f578bf2017-09-05 16:46:48 -060039 return mode >= Mode::kQuadraticHulls && mode < Mode::kSerpentineHulls;
Chris Dalton1a325d22017-07-14 15:17:41 -060040}
41
42/**
43 * This sample visualizes the AA bloat geometry generated by the ccpr geometry shaders. It
44 * increases the AA bloat by 50x and outputs color instead of coverage (coverage=+1 -> green,
45 * coverage=0 -> black, coverage=-1 -> red). Use the keys 1-7 to cycle through the different
46 * geometry processors.
47 */
48class CCPRGeometryView : public SampleView {
49public:
50 CCPRGeometryView() { this->updateGpuData(); }
51 void onDrawContent(SkCanvas*) override;
52
53 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override;
54 bool onClick(SampleView::Click*) override;
55 bool onQuery(SkEvent* evt) override;
56
57private:
58 class Click;
59 class Op;
60
61 void updateAndInval() {
62 this->updateGpuData();
63 this->inval(nullptr);
64 }
65
66 void updateGpuData();
67
68 Mode fMode = Mode::kTriangleHulls;
Chris Dalton7f578bf2017-09-05 16:46:48 -060069 SkMatrix fCubicKLM;
Chris Dalton1a325d22017-07-14 15:17:41 -060070
71 SkPoint fPoints[4] = {
72 {100.05f, 100.05f},
73 {100.05f, 300.95f},
74 {400.75f, 300.95f},
75 {400.75f, 100.05f}
76 };
77
Chris Daltonc1e59632017-09-05 00:30:07 -060078 SkTArray<SkPoint> fGpuPoints;
79 SkTArray<int32_t> fInstanceData;
80 int fInstanceCount;
Chris Dalton1a325d22017-07-14 15:17:41 -060081
82 typedef SampleView INHERITED;
83};
84
85class CCPRGeometryView::Op : public GrDrawOp {
86 DEFINE_OP_CLASS_ID
87
88public:
89 Op(CCPRGeometryView* view)
90 : INHERITED(ClassID())
91 , fView(view) {
92 this->setBounds(SkRect::MakeLargest(), GrOp::HasAABloat::kNo, GrOp::IsZeroArea::kNo);
93 }
94
95 const char* name() const override { return "[Testing/Sample code] CCPRGeometryView::Op"; }
96
97private:
98 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
99 RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*) override {
100 return RequiresDstTexture::kNo;
101 }
102 bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; }
103 void onPrepare(GrOpFlushState*) override {}
104 void onExecute(GrOpFlushState*) override;
105
106 CCPRGeometryView* fView;
107
108 typedef GrDrawOp INHERITED;
109};
110
Chris Dalton7f578bf2017-09-05 16:46:48 -0600111static void draw_klm_line(int w, int h, SkCanvas* canvas, const SkScalar line[3], SkColor color) {
112 SkPoint p1, p2;
113 if (SkScalarAbs(line[1]) > SkScalarAbs(line[0])) {
114 // Draw from vertical edge to vertical edge.
115 p1 = {0, -line[2] / line[1]};
116 p2 = {(SkScalar) w, (-line[2] - w * line[0]) / line[1]};
117 } else {
118 // Draw from horizontal edge to horizontal edge.
119 p1 = {-line[2] / line[0], 0};
120 p2 = {(-line[2] - h * line[1]) / line[0], (SkScalar) h};
121 }
122
123 SkPaint linePaint;
124 linePaint.setColor(color);
125 linePaint.setAlpha(128);
126 linePaint.setStyle(SkPaint::kStroke_Style);
127 linePaint.setStrokeWidth(0);
128 linePaint.setAntiAlias(true);
129 canvas->drawLine(p1, p2, linePaint);
130}
131
Chris Dalton1a325d22017-07-14 15:17:41 -0600132void CCPRGeometryView::onDrawContent(SkCanvas* canvas) {
133 SkAutoCanvasRestore acr(canvas, true);
134 canvas->setMatrix(SkMatrix::I());
135
136 SkPath outline;
137 outline.moveTo(fPoints[0]);
138 if (4 == num_points(fMode)) {
139 outline.cubicTo(fPoints[1], fPoints[2], fPoints[3]);
Chris Daltonc1e59632017-09-05 00:30:07 -0600140 } else if (is_quadratic(fMode)) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600141 outline.quadTo(fPoints[1], fPoints[3]);
142 } else {
143 outline.lineTo(fPoints[1]);
144 outline.lineTo(fPoints[3]);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600145 outline.close();
Chris Dalton1a325d22017-07-14 15:17:41 -0600146 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600147
148 SkPaint outlinePaint;
149 outlinePaint.setColor(0x30000000);
150 outlinePaint.setStyle(SkPaint::kStroke_Style);
151 outlinePaint.setStrokeWidth(0);
152 outlinePaint.setAntiAlias(true);
Chris Dalton1a325d22017-07-14 15:17:41 -0600153 canvas->drawPath(outline, outlinePaint);
154
Chris Dalton7f578bf2017-09-05 16:46:48 -0600155#if 0
156 SkPaint gridPaint;
157 gridPaint.setColor(0x10000000);
158 gridPaint.setStyle(SkPaint::kStroke_Style);
159 gridPaint.setStrokeWidth(0);
160 gridPaint.setAntiAlias(true);
Chris Daltona640c492017-09-11 22:04:03 -0700161 for (int y = 0; y < this->height(); y += kDebugBloat) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600162 canvas->drawLine(0, y, this->width(), y, gridPaint);
163 }
Chris Daltona640c492017-09-11 22:04:03 -0700164 for (int x = 0; x < this->width(); x += kDebugBloat) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600165 canvas->drawLine(x, 0, x, this->height(), outlinePaint);
166 }
167#endif
168
Chris Dalton1a325d22017-07-14 15:17:41 -0600169 const char* caption = "Use GPU backend to visualize geometry.";
170
171 if (GrRenderTargetContext* rtc =
172 canvas->internal_private_accessTopLayerRenderTargetContext()) {
173 rtc->priv().testingOnly_addDrawOp(skstd::make_unique<Op>(this));
174 caption = GrCCPRCoverageProcessor::GetProcessorName(fMode);
175 }
176
177 SkPaint pointsPaint;
178 pointsPaint.setColor(SK_ColorBLUE);
179 pointsPaint.setStrokeWidth(8);
180 pointsPaint.setAntiAlias(true);
181
182 if (4 == num_points(fMode)) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600183 int w = this->width(), h = this->height();
Chris Dalton1a325d22017-07-14 15:17:41 -0600184 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, fPoints, pointsPaint);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600185 draw_klm_line(w, h, canvas, &fCubicKLM[0], SK_ColorYELLOW);
186 draw_klm_line(w, h, canvas, &fCubicKLM[3], SK_ColorBLUE);
187 draw_klm_line(w, h, canvas, &fCubicKLM[6], SK_ColorRED);
Chris Dalton1a325d22017-07-14 15:17:41 -0600188 } else {
189 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, fPoints, pointsPaint);
190 canvas->drawPoints(SkCanvas::kPoints_PointMode, 1, fPoints + 3, pointsPaint);
191 }
192
193 SkPaint captionPaint;
194 captionPaint.setTextSize(20);
195 captionPaint.setColor(SK_ColorBLACK);
196 captionPaint.setAntiAlias(true);
197 canvas->drawText(caption, strlen(caption), 10, 30, captionPaint);
198}
199
200void CCPRGeometryView::updateGpuData() {
201 int vertexCount = num_points(fMode);
Chris Dalton1a325d22017-07-14 15:17:41 -0600202
203 fGpuPoints.reset();
Chris Daltonc1e59632017-09-05 00:30:07 -0600204 fInstanceData.reset();
205 fInstanceCount = 0;
Chris Dalton1a325d22017-07-14 15:17:41 -0600206
207 if (4 == vertexCount) {
208 double t[2], s[2];
Chris Dalton7f578bf2017-09-05 16:46:48 -0600209 SkCubicType type = GrPathUtils::getCubicKLM(fPoints, &fCubicKLM, t, s);
210 if (Mode::kSerpentineHulls == fMode && SkCubicType::kLoop == type) {
211 fMode = Mode::kLoopHulls;
212 }
213 if (Mode::kSerpentineCorners == fMode && SkCubicType::kLoop == type) {
214 fMode = Mode::kLoopCorners;
215 }
216 if (Mode::kLoopHulls == fMode && SkCubicType::kLoop != type) {
217 fMode = Mode::kSerpentineHulls;
218 }
219 if (Mode::kLoopCorners == fMode && SkCubicType::kLoop != type) {
220 fMode = Mode::kSerpentineCorners;
221 }
222
223 GrCCPRGeometry geometry;
224 geometry.beginContour(fPoints[0]);
Chris Daltona640c492017-09-11 22:04:03 -0700225 geometry.cubicTo(fPoints[1], fPoints[2], fPoints[3], kDebugBloat/2, kDebugBloat/2);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600226 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());
Chris Daltona640c492017-09-11 22:04:03 -0700308 SkDEBUGCODE(ccprProc.enableDebugVisualizations(kDebugBloat);)
Chris Dalton1a325d22017-07-14 15:17:41 -0600309
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