blob: a8f0d9e02cf1bf72691a90c3a2b4dff33db9b2cf [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;
Chris Daltona3e92712017-12-04 11:45:51 -070029using CubicInstance = GrCCPRCoverageProcessor::CubicInstance;
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 is_quadratic(RenderPass renderPass) {
35 return renderPass >= RenderPass::kQuadraticHulls && renderPass < RenderPass::kSerpentineHulls;
Chris Dalton1a325d22017-07-14 15:17:41 -060036}
37
38/**
39 * This sample visualizes the AA bloat geometry generated by the ccpr geometry shaders. It
40 * increases the AA bloat by 50x and outputs color instead of coverage (coverage=+1 -> green,
41 * coverage=0 -> black, coverage=-1 -> red). Use the keys 1-7 to cycle through the different
42 * geometry processors.
43 */
44class CCPRGeometryView : public SampleView {
45public:
46 CCPRGeometryView() { this->updateGpuData(); }
47 void onDrawContent(SkCanvas*) override;
48
49 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override;
50 bool onClick(SampleView::Click*) override;
51 bool onQuery(SkEvent* evt) override;
52
53private:
54 class Click;
55 class Op;
56
57 void updateAndInval() {
58 this->updateGpuData();
Chris Dalton1a325d22017-07-14 15:17:41 -060059 }
60
61 void updateGpuData();
62
Chris Dalton6a3dbee2017-10-16 10:44:41 -060063 RenderPass fRenderPass = RenderPass::kTriangleHulls;
Chris Dalton7f578bf2017-09-05 16:46:48 -060064 SkMatrix fCubicKLM;
Chris Dalton1a325d22017-07-14 15:17:41 -060065
66 SkPoint fPoints[4] = {
67 {100.05f, 100.05f},
Chris Daltonc17bf322017-10-24 10:59:03 -060068 {400.75f, 100.05f},
Chris Dalton1a325d22017-07-14 15:17:41 -060069 {400.75f, 300.95f},
Chris Daltonc17bf322017-10-24 10:59:03 -060070 {100.05f, 300.95f}
Chris Dalton1a325d22017-07-14 15:17:41 -060071 };
72
Chris Daltona3e92712017-12-04 11:45:51 -070073 SkTArray<TriangleInstance> fTriangleInstances;
74 SkTArray<CubicInstance> fCubicInstances;
Chris Dalton1a325d22017-07-14 15:17:41 -060075
76 typedef SampleView INHERITED;
77};
78
79class CCPRGeometryView::Op : public GrDrawOp {
80 DEFINE_OP_CLASS_ID
81
82public:
83 Op(CCPRGeometryView* view)
84 : INHERITED(ClassID())
85 , fView(view) {
86 this->setBounds(SkRect::MakeLargest(), GrOp::HasAABloat::kNo, GrOp::IsZeroArea::kNo);
87 }
88
89 const char* name() const override { return "[Testing/Sample code] CCPRGeometryView::Op"; }
90
91private:
92 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Brian Osman9a725dd2017-09-20 09:53:22 -040093 RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*,
94 GrPixelConfigIsClamped) override {
Chris Dalton1a325d22017-07-14 15:17:41 -060095 return RequiresDstTexture::kNo;
96 }
97 bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; }
98 void onPrepare(GrOpFlushState*) override {}
99 void onExecute(GrOpFlushState*) override;
100
101 CCPRGeometryView* fView;
102
103 typedef GrDrawOp INHERITED;
104};
105
Chris Dalton7f578bf2017-09-05 16:46:48 -0600106static void draw_klm_line(int w, int h, SkCanvas* canvas, const SkScalar line[3], SkColor color) {
107 SkPoint p1, p2;
108 if (SkScalarAbs(line[1]) > SkScalarAbs(line[0])) {
109 // Draw from vertical edge to vertical edge.
110 p1 = {0, -line[2] / line[1]};
111 p2 = {(SkScalar) w, (-line[2] - w * line[0]) / line[1]};
112 } else {
113 // Draw from horizontal edge to horizontal edge.
114 p1 = {-line[2] / line[0], 0};
115 p2 = {(-line[2] - h * line[1]) / line[0], (SkScalar) h};
116 }
117
118 SkPaint linePaint;
119 linePaint.setColor(color);
120 linePaint.setAlpha(128);
121 linePaint.setStyle(SkPaint::kStroke_Style);
122 linePaint.setStrokeWidth(0);
123 linePaint.setAntiAlias(true);
124 canvas->drawLine(p1, p2, linePaint);
125}
126
Chris Dalton1a325d22017-07-14 15:17:41 -0600127void CCPRGeometryView::onDrawContent(SkCanvas* canvas) {
128 SkAutoCanvasRestore acr(canvas, true);
129 canvas->setMatrix(SkMatrix::I());
130
131 SkPath outline;
132 outline.moveTo(fPoints[0]);
Chris Daltona3e92712017-12-04 11:45:51 -0700133 if (GrCCPRCoverageProcessor::RenderPassIsCubic(fRenderPass)) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600134 outline.cubicTo(fPoints[1], fPoints[2], fPoints[3]);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600135 } else if (is_quadratic(fRenderPass)) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600136 outline.quadTo(fPoints[1], fPoints[3]);
137 } else {
138 outline.lineTo(fPoints[1]);
139 outline.lineTo(fPoints[3]);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600140 outline.close();
Chris Dalton1a325d22017-07-14 15:17:41 -0600141 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600142
143 SkPaint outlinePaint;
144 outlinePaint.setColor(0x30000000);
145 outlinePaint.setStyle(SkPaint::kStroke_Style);
146 outlinePaint.setStrokeWidth(0);
147 outlinePaint.setAntiAlias(true);
Chris Dalton1a325d22017-07-14 15:17:41 -0600148 canvas->drawPath(outline, outlinePaint);
149
Chris Dalton7f578bf2017-09-05 16:46:48 -0600150#if 0
151 SkPaint gridPaint;
152 gridPaint.setColor(0x10000000);
153 gridPaint.setStyle(SkPaint::kStroke_Style);
154 gridPaint.setStrokeWidth(0);
155 gridPaint.setAntiAlias(true);
Chris Daltona640c492017-09-11 22:04:03 -0700156 for (int y = 0; y < this->height(); y += kDebugBloat) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600157 canvas->drawLine(0, y, this->width(), y, gridPaint);
158 }
Chris Daltona640c492017-09-11 22:04:03 -0700159 for (int x = 0; x < this->width(); x += kDebugBloat) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600160 canvas->drawLine(x, 0, x, this->height(), outlinePaint);
161 }
162#endif
163
Chris Dalton1a325d22017-07-14 15:17:41 -0600164 const char* caption = "Use GPU backend to visualize geometry.";
165
166 if (GrRenderTargetContext* rtc =
167 canvas->internal_private_accessTopLayerRenderTargetContext()) {
168 rtc->priv().testingOnly_addDrawOp(skstd::make_unique<Op>(this));
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600169 caption = GrCCPRCoverageProcessor::GetRenderPassName(fRenderPass);
Chris Dalton1a325d22017-07-14 15:17:41 -0600170 }
171
172 SkPaint pointsPaint;
173 pointsPaint.setColor(SK_ColorBLUE);
174 pointsPaint.setStrokeWidth(8);
175 pointsPaint.setAntiAlias(true);
176
Chris Daltona3e92712017-12-04 11:45:51 -0700177 if (GrCCPRCoverageProcessor::RenderPassIsCubic(fRenderPass)) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600178 int w = this->width(), h = this->height();
Chris Dalton1a325d22017-07-14 15:17:41 -0600179 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, fPoints, pointsPaint);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600180 draw_klm_line(w, h, canvas, &fCubicKLM[0], SK_ColorYELLOW);
181 draw_klm_line(w, h, canvas, &fCubicKLM[3], SK_ColorBLUE);
182 draw_klm_line(w, h, canvas, &fCubicKLM[6], SK_ColorRED);
Chris Dalton1a325d22017-07-14 15:17:41 -0600183 } else {
184 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, fPoints, pointsPaint);
185 canvas->drawPoints(SkCanvas::kPoints_PointMode, 1, fPoints + 3, pointsPaint);
186 }
187
188 SkPaint captionPaint;
189 captionPaint.setTextSize(20);
190 captionPaint.setColor(SK_ColorBLACK);
191 captionPaint.setAntiAlias(true);
192 canvas->drawText(caption, strlen(caption), 10, 30, captionPaint);
193}
194
195void CCPRGeometryView::updateGpuData() {
Chris Daltona3e92712017-12-04 11:45:51 -0700196 fTriangleInstances.reset();
197 fCubicInstances.reset();
Chris Dalton1a325d22017-07-14 15:17:41 -0600198
Chris Daltona3e92712017-12-04 11:45:51 -0700199 if (GrCCPRCoverageProcessor::RenderPassIsCubic(fRenderPass)) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600200 double t[2], s[2];
Chris Dalton7f578bf2017-09-05 16:46:48 -0600201 SkCubicType type = GrPathUtils::getCubicKLM(fPoints, &fCubicKLM, t, s);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600202 if (RenderPass::kSerpentineHulls == fRenderPass && SkCubicType::kLoop == type) {
203 fRenderPass = RenderPass::kLoopHulls;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600204 }
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600205 if (RenderPass::kSerpentineCorners == fRenderPass && SkCubicType::kLoop == type) {
206 fRenderPass = RenderPass::kLoopCorners;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600207 }
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600208 if (RenderPass::kLoopHulls == fRenderPass && SkCubicType::kLoop != type) {
209 fRenderPass = RenderPass::kSerpentineHulls;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600210 }
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600211 if (RenderPass::kLoopCorners == fRenderPass && SkCubicType::kLoop != type) {
212 fRenderPass = RenderPass::kSerpentineCorners;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600213 }
214
215 GrCCPRGeometry geometry;
216 geometry.beginContour(fPoints[0]);
Chris Daltona640c492017-09-11 22:04:03 -0700217 geometry.cubicTo(fPoints[1], fPoints[2], fPoints[3], kDebugBloat/2, kDebugBloat/2);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600218 geometry.endContour();
Chris Dalton7f578bf2017-09-05 16:46:48 -0600219 int ptsIdx = 0;
220 for (GrCCPRGeometry::Verb verb : geometry.verbs()) {
221 switch (verb) {
222 case GrCCPRGeometry::Verb::kLineTo:
223 ++ptsIdx;
224 continue;
225 case GrCCPRGeometry::Verb::kMonotonicQuadraticTo:
226 ptsIdx += 2;
227 continue;
228 case GrCCPRGeometry::Verb::kMonotonicSerpentineTo:
229 case GrCCPRGeometry::Verb::kMonotonicLoopTo:
Chris Daltona3e92712017-12-04 11:45:51 -0700230 fCubicInstances.push_back().set(&geometry.points()[ptsIdx], 0, 0);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600231 ptsIdx += 3;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600232 continue;
233 default: continue;
Chris Dalton1a325d22017-07-14 15:17:41 -0600234 }
235 }
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600236 } else if (is_quadratic(fRenderPass)) {
Chris Daltonc1e59632017-09-05 00:30:07 -0600237 GrCCPRGeometry geometry;
238 geometry.beginContour(fPoints[0]);
239 geometry.quadraticTo(fPoints[1], fPoints[3]);
240 geometry.endContour();
Chris Daltona3e92712017-12-04 11:45:51 -0700241 int ptsIdx = 0;
Chris Daltonc1e59632017-09-05 00:30:07 -0600242 for (GrCCPRGeometry::Verb verb : geometry.verbs()) {
243 if (GrCCPRGeometry::Verb::kBeginContour == verb ||
244 GrCCPRGeometry::Verb::kEndOpenContour == verb ||
245 GrCCPRGeometry::Verb::kEndClosedContour == verb) {
246 continue;
247 }
248 SkASSERT(GrCCPRGeometry::Verb::kMonotonicQuadraticTo == verb);
Chris Daltona3e92712017-12-04 11:45:51 -0700249 fTriangleInstances.push_back().set(&geometry.points()[ptsIdx], Sk2f(0, 0));
250 ptsIdx += 2;
Chris Daltonb072bb62017-08-07 09:00:46 -0600251 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600252 } else {
Chris Daltona3e92712017-12-04 11:45:51 -0700253 fTriangleInstances.push_back().set(fPoints[0], fPoints[1], fPoints[3], Sk2f(0, 0));
Chris Dalton1a325d22017-07-14 15:17:41 -0600254 }
255}
256
257void CCPRGeometryView::Op::onExecute(GrOpFlushState* state) {
258 GrResourceProvider* rp = state->resourceProvider();
259 GrContext* context = state->gpu()->getContext();
260 GrGLGpu* glGpu = kOpenGL_GrBackend == context->contextPriv().getBackend() ?
261 static_cast<GrGLGpu*>(state->gpu()) : nullptr;
Chris Dalton1a325d22017-07-14 15:17:41 -0600262
Chris Daltona3e92712017-12-04 11:45:51 -0700263 bool isCubic = GrCCPRCoverageProcessor::RenderPassIsCubic(fView->fRenderPass);
264 GrMesh mesh(isCubic ? GrPrimitiveType::kLinesAdjacency : GrPrimitiveType::kTriangles);
265 if (isCubic) {
266 if (fView->fCubicInstances.empty()) {
267 return;
268 }
269 sk_sp<GrBuffer> instBuff(rp->createBuffer(fView->fCubicInstances.count() *
270 sizeof(CubicInstance), kVertex_GrBufferType,
271 kDynamic_GrAccessPattern,
Chris Dalton1a325d22017-07-14 15:17:41 -0600272 GrResourceProvider::kNoPendingIO_Flag |
273 GrResourceProvider::kRequireGpuMemory_Flag,
Chris Daltona3e92712017-12-04 11:45:51 -0700274 fView->fCubicInstances.begin()));
275 if (!instBuff) {
276 return;
277 }
278 mesh.setInstanced(instBuff.get(), fView->fCubicInstances.count(), 0, 4);
279 } else {
280 if (fView->fTriangleInstances.empty()) {
281 return;
282 }
283 sk_sp<GrBuffer> instBuff(rp->createBuffer(fView->fTriangleInstances.count() *
284 sizeof(TriangleInstance), kVertex_GrBufferType,
285 kDynamic_GrAccessPattern,
286 GrResourceProvider::kNoPendingIO_Flag |
287 GrResourceProvider::kRequireGpuMemory_Flag,
288 fView->fTriangleInstances.begin()));
289 if (!instBuff) {
290 return;
291 }
292 mesh.setInstanced(instBuff.get(), fView->fTriangleInstances.count(), 0, 3);
Chris Dalton1a325d22017-07-14 15:17:41 -0600293 }
294
Robert Phillips2890fbf2017-07-26 15:48:41 -0400295 GrPipeline pipeline(state->drawOpArgs().fProxy, GrPipeline::ScissorState::kDisabled,
Chris Dalton1a325d22017-07-14 15:17:41 -0600296 SkBlendMode::kSrcOver);
297
Chris Daltona3e92712017-12-04 11:45:51 -0700298 GrCCPRCoverageProcessor ccprProc(fView->fRenderPass);
Chris Daltona640c492017-09-11 22:04:03 -0700299 SkDEBUGCODE(ccprProc.enableDebugVisualizations(kDebugBloat);)
Chris Dalton1a325d22017-07-14 15:17:41 -0600300
Chris Dalton1a325d22017-07-14 15:17:41 -0600301 if (glGpu) {
302 glGpu->handleDirtyContext();
303 GR_GL_CALL(glGpu->glInterface(), PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
304 GR_GL_CALL(glGpu->glInterface(), Enable(GR_GL_LINE_SMOOTH));
305 }
306
Greg Daniel500d58b2017-08-24 15:59:33 -0400307 state->rtCommandBuffer()->draw(pipeline, ccprProc, &mesh, nullptr, 1, this->bounds());
Chris Dalton1a325d22017-07-14 15:17:41 -0600308
309 if (glGpu) {
310 context->resetContext(kMisc_GrGLBackendState);
311 }
312}
313
314class CCPRGeometryView::Click : public SampleView::Click {
315public:
316 Click(SkView* target, int ptIdx) : SampleView::Click(target), fPtIdx(ptIdx) {}
317
318 void doClick(SkPoint points[]) {
319 if (fPtIdx >= 0) {
320 this->dragPoint(points, fPtIdx);
321 } else {
322 for (int i = 0; i < 4; ++i) {
323 this->dragPoint(points, i);
324 }
325 }
326 }
327
328private:
329 void dragPoint(SkPoint points[], int idx) {
330 SkIPoint delta = fICurr - fIPrev;
331 points[idx] += SkPoint::Make(delta.x(), delta.y());
332 }
333
334 int fPtIdx;
335};
336
337SkView::Click* CCPRGeometryView::onFindClickHandler(SkScalar x, SkScalar y, unsigned) {
338 for (int i = 0; i < 4; ++i) {
Chris Daltona3e92712017-12-04 11:45:51 -0700339 if (!GrCCPRCoverageProcessor::RenderPassIsCubic(fRenderPass) && 2 == i) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600340 continue;
341 }
342 if (fabs(x - fPoints[i].x()) < 20 && fabsf(y - fPoints[i].y()) < 20) {
343 return new Click(this, i);
344 }
345 }
346 return new Click(this, -1);
347}
348
349bool CCPRGeometryView::onClick(SampleView::Click* click) {
350 Click* myClick = (Click*) click;
351 myClick->doClick(fPoints);
352 this->updateAndInval();
353 return true;
354}
355
356bool CCPRGeometryView::onQuery(SkEvent* evt) {
357 if (SampleCode::TitleQ(*evt)) {
358 SampleCode::TitleR(evt, "CCPRGeometry");
359 return true;
360 }
361 SkUnichar unichar;
362 if (SampleCode::CharQ(*evt, &unichar)) {
363 if (unichar >= '1' && unichar <= '7') {
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600364 fRenderPass = RenderPass(unichar - '1');
365 if (fRenderPass >= RenderPass::kLoopHulls) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600366 // '6' -> kSerpentineHulls, '7' -> kSerpentineCorners. updateGpuData converts to
367 // kLoop* if needed.
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600368 fRenderPass = RenderPass(int(fRenderPass) + 1);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600369 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600370 this->updateAndInval();
371 return true;
372 }
373 if (unichar == 'D') {
374 SkDebugf(" SkPoint fPoints[4] = {\n");
Chris Dalton7f578bf2017-09-05 16:46:48 -0600375 SkDebugf(" {%ff, %ff},\n", fPoints[0].x(), fPoints[0].y());
376 SkDebugf(" {%ff, %ff},\n", fPoints[1].x(), fPoints[1].y());
377 SkDebugf(" {%ff, %ff},\n", fPoints[2].x(), fPoints[2].y());
378 SkDebugf(" {%ff, %ff}\n", fPoints[3].x(), fPoints[3].y());
Chris Dalton1a325d22017-07-14 15:17:41 -0600379 SkDebugf(" };\n");
380 return true;
381 }
382 }
383 return this->INHERITED::onQuery(evt);
384}
385
386DEF_SAMPLE( return new CCPRGeometryView; )
387
388#endif // SK_SUPPORT_GPU