blob: 86333983372785c3d7d2ba0bf8f3d0e86311090e [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 Dalton6a3dbee2017-10-16 10:44:41 -060030using RenderPass = GrCCPRCoverageProcessor::RenderPass;
Chris Dalton1a325d22017-07-14 15:17:41 -060031
Chris Daltona640c492017-09-11 22:04:03 -070032static constexpr float kDebugBloat = 40;
33
Chris Dalton6a3dbee2017-10-16 10:44:41 -060034static int num_points(RenderPass renderPass) {
35 return renderPass >= RenderPass::kSerpentineHulls ? 4 : 3;
Chris Dalton1a325d22017-07-14 15:17:41 -060036}
37
Chris Dalton6a3dbee2017-10-16 10:44:41 -060038static int is_quadratic(RenderPass renderPass) {
39 return renderPass >= RenderPass::kQuadraticHulls && renderPass < RenderPass::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
Chris Dalton6a3dbee2017-10-16 10:44:41 -060068 RenderPass fRenderPass = RenderPass::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},
Chris Daltonc17bf322017-10-24 10:59:03 -060073 {400.75f, 100.05f},
Chris Dalton1a325d22017-07-14 15:17:41 -060074 {400.75f, 300.95f},
Chris Daltonc17bf322017-10-24 10:59:03 -060075 {100.05f, 300.95f}
Chris Dalton1a325d22017-07-14 15:17:41 -060076 };
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; }
Brian Osman9a725dd2017-09-20 09:53:22 -040099 RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*,
100 GrPixelConfigIsClamped) override {
Chris Dalton1a325d22017-07-14 15:17:41 -0600101 return RequiresDstTexture::kNo;
102 }
103 bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; }
104 void onPrepare(GrOpFlushState*) override {}
105 void onExecute(GrOpFlushState*) override;
106
107 CCPRGeometryView* fView;
108
109 typedef GrDrawOp INHERITED;
110};
111
Chris Dalton7f578bf2017-09-05 16:46:48 -0600112static void draw_klm_line(int w, int h, SkCanvas* canvas, const SkScalar line[3], SkColor color) {
113 SkPoint p1, p2;
114 if (SkScalarAbs(line[1]) > SkScalarAbs(line[0])) {
115 // Draw from vertical edge to vertical edge.
116 p1 = {0, -line[2] / line[1]};
117 p2 = {(SkScalar) w, (-line[2] - w * line[0]) / line[1]};
118 } else {
119 // Draw from horizontal edge to horizontal edge.
120 p1 = {-line[2] / line[0], 0};
121 p2 = {(-line[2] - h * line[1]) / line[0], (SkScalar) h};
122 }
123
124 SkPaint linePaint;
125 linePaint.setColor(color);
126 linePaint.setAlpha(128);
127 linePaint.setStyle(SkPaint::kStroke_Style);
128 linePaint.setStrokeWidth(0);
129 linePaint.setAntiAlias(true);
130 canvas->drawLine(p1, p2, linePaint);
131}
132
Chris Dalton1a325d22017-07-14 15:17:41 -0600133void CCPRGeometryView::onDrawContent(SkCanvas* canvas) {
134 SkAutoCanvasRestore acr(canvas, true);
135 canvas->setMatrix(SkMatrix::I());
136
137 SkPath outline;
138 outline.moveTo(fPoints[0]);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600139 if (4 == num_points(fRenderPass)) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600140 outline.cubicTo(fPoints[1], fPoints[2], fPoints[3]);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600141 } else if (is_quadratic(fRenderPass)) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600142 outline.quadTo(fPoints[1], fPoints[3]);
143 } else {
144 outline.lineTo(fPoints[1]);
145 outline.lineTo(fPoints[3]);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600146 outline.close();
Chris Dalton1a325d22017-07-14 15:17:41 -0600147 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600148
149 SkPaint outlinePaint;
150 outlinePaint.setColor(0x30000000);
151 outlinePaint.setStyle(SkPaint::kStroke_Style);
152 outlinePaint.setStrokeWidth(0);
153 outlinePaint.setAntiAlias(true);
Chris Dalton1a325d22017-07-14 15:17:41 -0600154 canvas->drawPath(outline, outlinePaint);
155
Chris Dalton7f578bf2017-09-05 16:46:48 -0600156#if 0
157 SkPaint gridPaint;
158 gridPaint.setColor(0x10000000);
159 gridPaint.setStyle(SkPaint::kStroke_Style);
160 gridPaint.setStrokeWidth(0);
161 gridPaint.setAntiAlias(true);
Chris Daltona640c492017-09-11 22:04:03 -0700162 for (int y = 0; y < this->height(); y += kDebugBloat) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600163 canvas->drawLine(0, y, this->width(), y, gridPaint);
164 }
Chris Daltona640c492017-09-11 22:04:03 -0700165 for (int x = 0; x < this->width(); x += kDebugBloat) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600166 canvas->drawLine(x, 0, x, this->height(), outlinePaint);
167 }
168#endif
169
Chris Dalton1a325d22017-07-14 15:17:41 -0600170 const char* caption = "Use GPU backend to visualize geometry.";
171
172 if (GrRenderTargetContext* rtc =
173 canvas->internal_private_accessTopLayerRenderTargetContext()) {
174 rtc->priv().testingOnly_addDrawOp(skstd::make_unique<Op>(this));
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600175 caption = GrCCPRCoverageProcessor::GetRenderPassName(fRenderPass);
Chris Dalton1a325d22017-07-14 15:17:41 -0600176 }
177
178 SkPaint pointsPaint;
179 pointsPaint.setColor(SK_ColorBLUE);
180 pointsPaint.setStrokeWidth(8);
181 pointsPaint.setAntiAlias(true);
182
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600183 if (4 == num_points(fRenderPass)) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600184 int w = this->width(), h = this->height();
Chris Dalton1a325d22017-07-14 15:17:41 -0600185 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, fPoints, pointsPaint);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600186 draw_klm_line(w, h, canvas, &fCubicKLM[0], SK_ColorYELLOW);
187 draw_klm_line(w, h, canvas, &fCubicKLM[3], SK_ColorBLUE);
188 draw_klm_line(w, h, canvas, &fCubicKLM[6], SK_ColorRED);
Chris Dalton1a325d22017-07-14 15:17:41 -0600189 } else {
190 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, fPoints, pointsPaint);
191 canvas->drawPoints(SkCanvas::kPoints_PointMode, 1, fPoints + 3, pointsPaint);
192 }
193
194 SkPaint captionPaint;
195 captionPaint.setTextSize(20);
196 captionPaint.setColor(SK_ColorBLACK);
197 captionPaint.setAntiAlias(true);
198 canvas->drawText(caption, strlen(caption), 10, 30, captionPaint);
199}
200
201void CCPRGeometryView::updateGpuData() {
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600202 int vertexCount = num_points(fRenderPass);
Chris Dalton1a325d22017-07-14 15:17:41 -0600203
204 fGpuPoints.reset();
Chris Daltonc1e59632017-09-05 00:30:07 -0600205 fInstanceData.reset();
206 fInstanceCount = 0;
Chris Dalton1a325d22017-07-14 15:17:41 -0600207
208 if (4 == vertexCount) {
209 double t[2], s[2];
Chris Dalton7f578bf2017-09-05 16:46:48 -0600210 SkCubicType type = GrPathUtils::getCubicKLM(fPoints, &fCubicKLM, t, s);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600211 if (RenderPass::kSerpentineHulls == fRenderPass && SkCubicType::kLoop == type) {
212 fRenderPass = RenderPass::kLoopHulls;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600213 }
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600214 if (RenderPass::kSerpentineCorners == fRenderPass && SkCubicType::kLoop == type) {
215 fRenderPass = RenderPass::kLoopCorners;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600216 }
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600217 if (RenderPass::kLoopHulls == fRenderPass && SkCubicType::kLoop != type) {
218 fRenderPass = RenderPass::kSerpentineHulls;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600219 }
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600220 if (RenderPass::kLoopCorners == fRenderPass && SkCubicType::kLoop != type) {
221 fRenderPass = RenderPass::kSerpentineCorners;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600222 }
223
224 GrCCPRGeometry geometry;
225 geometry.beginContour(fPoints[0]);
Chris Daltona640c492017-09-11 22:04:03 -0700226 geometry.cubicTo(fPoints[1], fPoints[2], fPoints[3], kDebugBloat/2, kDebugBloat/2);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600227 geometry.endContour();
228 fGpuPoints.push_back_n(geometry.points().count(), geometry.points().begin());
229 int ptsIdx = 0;
230 for (GrCCPRGeometry::Verb verb : geometry.verbs()) {
231 switch (verb) {
232 case GrCCPRGeometry::Verb::kLineTo:
233 ++ptsIdx;
234 continue;
235 case GrCCPRGeometry::Verb::kMonotonicQuadraticTo:
236 ptsIdx += 2;
237 continue;
238 case GrCCPRGeometry::Verb::kMonotonicSerpentineTo:
239 case GrCCPRGeometry::Verb::kMonotonicLoopTo:
240 fInstanceData.push_back(ptsIdx);
241 fInstanceData.push_back(0); // Atlas offset.
242 ptsIdx += 3;
243 ++fInstanceCount;
244 continue;
245 default: continue;
Chris Dalton1a325d22017-07-14 15:17:41 -0600246 }
247 }
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600248 } else if (is_quadratic(fRenderPass)) {
Chris Daltonc1e59632017-09-05 00:30:07 -0600249 GrCCPRGeometry geometry;
250 geometry.beginContour(fPoints[0]);
251 geometry.quadraticTo(fPoints[1], fPoints[3]);
252 geometry.endContour();
253 fGpuPoints.push_back_n(geometry.points().count(), geometry.points().begin());
254 for (GrCCPRGeometry::Verb verb : geometry.verbs()) {
255 if (GrCCPRGeometry::Verb::kBeginContour == verb ||
256 GrCCPRGeometry::Verb::kEndOpenContour == verb ||
257 GrCCPRGeometry::Verb::kEndClosedContour == verb) {
258 continue;
259 }
260 SkASSERT(GrCCPRGeometry::Verb::kMonotonicQuadraticTo == verb);
261 fInstanceData.push_back(2 * fInstanceCount++); // Pts idx.
262 fInstanceData.push_back(0); // Atlas offset.
Chris Daltonb072bb62017-08-07 09:00:46 -0600263 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600264 } else {
Chris Daltonb072bb62017-08-07 09:00:46 -0600265 fGpuPoints.push_back(fPoints[0]);
Chris Daltonb072bb62017-08-07 09:00:46 -0600266 fGpuPoints.push_back(fPoints[1]);
Chris Daltonc1e59632017-09-05 00:30:07 -0600267 fGpuPoints.push_back(fPoints[3]);
268 fInstanceData.push_back(0);
269 fInstanceData.push_back(1);
270 fInstanceData.push_back(2);
271 fInstanceData.push_back(0); // Atlas offset.
272 fInstanceCount = 1;
Chris Dalton1a325d22017-07-14 15:17:41 -0600273 }
274}
275
276void CCPRGeometryView::Op::onExecute(GrOpFlushState* state) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600277 if (fView->fInstanceData.empty()) {
278 return;
279 }
280
Chris Dalton1a325d22017-07-14 15:17:41 -0600281 GrResourceProvider* rp = state->resourceProvider();
282 GrContext* context = state->gpu()->getContext();
283 GrGLGpu* glGpu = kOpenGL_GrBackend == context->contextPriv().getBackend() ?
284 static_cast<GrGLGpu*>(state->gpu()) : nullptr;
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600285 int vertexCount = num_points(fView->fRenderPass);
Chris Dalton1a325d22017-07-14 15:17:41 -0600286
287 sk_sp<GrBuffer> pointsBuffer(rp->createBuffer(fView->fGpuPoints.count() * sizeof(SkPoint),
288 kTexel_GrBufferType, kDynamic_GrAccessPattern,
289 GrResourceProvider::kNoPendingIO_Flag |
290 GrResourceProvider::kRequireGpuMemory_Flag,
291 fView->fGpuPoints.begin()));
292 if (!pointsBuffer) {
293 return;
294 }
295
Chris Daltonc1e59632017-09-05 00:30:07 -0600296 sk_sp<GrBuffer> instanceBuffer(rp->createBuffer(fView->fInstanceData.count() * sizeof(int),
Chris Dalton1a325d22017-07-14 15:17:41 -0600297 kVertex_GrBufferType, kDynamic_GrAccessPattern,
298 GrResourceProvider::kNoPendingIO_Flag |
299 GrResourceProvider::kRequireGpuMemory_Flag,
Chris Daltonc1e59632017-09-05 00:30:07 -0600300 fView->fInstanceData.begin()));
Chris Dalton1a325d22017-07-14 15:17:41 -0600301 if (!instanceBuffer) {
302 return;
303 }
304
Robert Phillips2890fbf2017-07-26 15:48:41 -0400305 GrPipeline pipeline(state->drawOpArgs().fProxy, GrPipeline::ScissorState::kDisabled,
Chris Dalton1a325d22017-07-14 15:17:41 -0600306 SkBlendMode::kSrcOver);
307
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600308 GrCCPRCoverageProcessor ccprProc(fView->fRenderPass, pointsBuffer.get());
Chris Daltona640c492017-09-11 22:04:03 -0700309 SkDEBUGCODE(ccprProc.enableDebugVisualizations(kDebugBloat);)
Chris Dalton1a325d22017-07-14 15:17:41 -0600310
311 GrMesh mesh(4 == vertexCount ? GrPrimitiveType::kLinesAdjacency : GrPrimitiveType::kTriangles);
Chris Daltonc1e59632017-09-05 00:30:07 -0600312 mesh.setInstanced(instanceBuffer.get(), fView->fInstanceCount, 0, vertexCount);
Chris Dalton1a325d22017-07-14 15:17:41 -0600313
314 if (glGpu) {
315 glGpu->handleDirtyContext();
316 GR_GL_CALL(glGpu->glInterface(), PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
317 GR_GL_CALL(glGpu->glInterface(), Enable(GR_GL_LINE_SMOOTH));
318 }
319
Greg Daniel500d58b2017-08-24 15:59:33 -0400320 state->rtCommandBuffer()->draw(pipeline, ccprProc, &mesh, nullptr, 1, this->bounds());
Chris Dalton1a325d22017-07-14 15:17:41 -0600321
322 if (glGpu) {
323 context->resetContext(kMisc_GrGLBackendState);
324 }
325}
326
327class CCPRGeometryView::Click : public SampleView::Click {
328public:
329 Click(SkView* target, int ptIdx) : SampleView::Click(target), fPtIdx(ptIdx) {}
330
331 void doClick(SkPoint points[]) {
332 if (fPtIdx >= 0) {
333 this->dragPoint(points, fPtIdx);
334 } else {
335 for (int i = 0; i < 4; ++i) {
336 this->dragPoint(points, i);
337 }
338 }
339 }
340
341private:
342 void dragPoint(SkPoint points[], int idx) {
343 SkIPoint delta = fICurr - fIPrev;
344 points[idx] += SkPoint::Make(delta.x(), delta.y());
345 }
346
347 int fPtIdx;
348};
349
350SkView::Click* CCPRGeometryView::onFindClickHandler(SkScalar x, SkScalar y, unsigned) {
351 for (int i = 0; i < 4; ++i) {
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600352 if (4 != num_points(fRenderPass) && 2 == i) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600353 continue;
354 }
355 if (fabs(x - fPoints[i].x()) < 20 && fabsf(y - fPoints[i].y()) < 20) {
356 return new Click(this, i);
357 }
358 }
359 return new Click(this, -1);
360}
361
362bool CCPRGeometryView::onClick(SampleView::Click* click) {
363 Click* myClick = (Click*) click;
364 myClick->doClick(fPoints);
365 this->updateAndInval();
366 return true;
367}
368
369bool CCPRGeometryView::onQuery(SkEvent* evt) {
370 if (SampleCode::TitleQ(*evt)) {
371 SampleCode::TitleR(evt, "CCPRGeometry");
372 return true;
373 }
374 SkUnichar unichar;
375 if (SampleCode::CharQ(*evt, &unichar)) {
376 if (unichar >= '1' && unichar <= '7') {
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600377 fRenderPass = RenderPass(unichar - '1');
378 if (fRenderPass >= RenderPass::kLoopHulls) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600379 // '6' -> kSerpentineHulls, '7' -> kSerpentineCorners. updateGpuData converts to
380 // kLoop* if needed.
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600381 fRenderPass = RenderPass(int(fRenderPass) + 1);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600382 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600383 this->updateAndInval();
384 return true;
385 }
386 if (unichar == 'D') {
387 SkDebugf(" SkPoint fPoints[4] = {\n");
Chris Dalton7f578bf2017-09-05 16:46:48 -0600388 SkDebugf(" {%ff, %ff},\n", fPoints[0].x(), fPoints[0].y());
389 SkDebugf(" {%ff, %ff},\n", fPoints[1].x(), fPoints[1].y());
390 SkDebugf(" {%ff, %ff},\n", fPoints[2].x(), fPoints[2].y());
391 SkDebugf(" {%ff, %ff}\n", fPoints[3].x(), fPoints[3].y());
Chris Dalton1a325d22017-07-14 15:17:41 -0600392 SkDebugf(" };\n");
393 return true;
394 }
395 }
396 return this->INHERITED::onQuery(evt);
397}
398
399DEF_SAMPLE( return new CCPRGeometryView; )
400
401#endif // SK_SUPPORT_GPU