blob: 92892c239b5a301b6bb1f17a111d7c171e6dd0f0 [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 Daltonbe4ffab2017-12-08 10:59:58 -070034static int is_quadratic(RenderPass pass) {
35 return pass == RenderPass::kQuadraticHulls || pass == RenderPass::kQuadraticCorners;
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 Daltonbe4ffab2017-12-08 10:59:58 -070064 SkCubicType fCubicType;
Chris Dalton7f578bf2017-09-05 16:46:48 -060065 SkMatrix fCubicKLM;
Chris Dalton1a325d22017-07-14 15:17:41 -060066
67 SkPoint fPoints[4] = {
68 {100.05f, 100.05f},
Chris Daltonc17bf322017-10-24 10:59:03 -060069 {400.75f, 100.05f},
Chris Dalton1a325d22017-07-14 15:17:41 -060070 {400.75f, 300.95f},
Chris Daltonc17bf322017-10-24 10:59:03 -060071 {100.05f, 300.95f}
Chris Dalton1a325d22017-07-14 15:17:41 -060072 };
73
Chris Daltona3e92712017-12-04 11:45:51 -070074 SkTArray<TriangleInstance> fTriangleInstances;
75 SkTArray<CubicInstance> fCubicInstances;
Chris Dalton1a325d22017-07-14 15:17:41 -060076
77 typedef SampleView INHERITED;
78};
79
80class CCPRGeometryView::Op : public GrDrawOp {
81 DEFINE_OP_CLASS_ID
82
83public:
84 Op(CCPRGeometryView* view)
85 : INHERITED(ClassID())
86 , fView(view) {
87 this->setBounds(SkRect::MakeLargest(), GrOp::HasAABloat::kNo, GrOp::IsZeroArea::kNo);
88 }
89
90 const char* name() const override { return "[Testing/Sample code] CCPRGeometryView::Op"; }
91
92private:
93 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Brian Osman9a725dd2017-09-20 09:53:22 -040094 RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*,
95 GrPixelConfigIsClamped) override {
Chris Dalton1a325d22017-07-14 15:17:41 -060096 return RequiresDstTexture::kNo;
97 }
98 bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; }
99 void onPrepare(GrOpFlushState*) override {}
100 void onExecute(GrOpFlushState*) override;
101
102 CCPRGeometryView* fView;
103
104 typedef GrDrawOp INHERITED;
105};
106
Chris Dalton7f578bf2017-09-05 16:46:48 -0600107static void draw_klm_line(int w, int h, SkCanvas* canvas, const SkScalar line[3], SkColor color) {
108 SkPoint p1, p2;
109 if (SkScalarAbs(line[1]) > SkScalarAbs(line[0])) {
110 // Draw from vertical edge to vertical edge.
111 p1 = {0, -line[2] / line[1]};
112 p2 = {(SkScalar) w, (-line[2] - w * line[0]) / line[1]};
113 } else {
114 // Draw from horizontal edge to horizontal edge.
115 p1 = {-line[2] / line[0], 0};
116 p2 = {(-line[2] - h * line[1]) / line[0], (SkScalar) h};
117 }
118
119 SkPaint linePaint;
120 linePaint.setColor(color);
121 linePaint.setAlpha(128);
122 linePaint.setStyle(SkPaint::kStroke_Style);
123 linePaint.setStrokeWidth(0);
124 linePaint.setAntiAlias(true);
125 canvas->drawLine(p1, p2, linePaint);
126}
127
Chris Dalton1a325d22017-07-14 15:17:41 -0600128void CCPRGeometryView::onDrawContent(SkCanvas* canvas) {
129 SkAutoCanvasRestore acr(canvas, true);
130 canvas->setMatrix(SkMatrix::I());
131
132 SkPath outline;
133 outline.moveTo(fPoints[0]);
Chris Daltona3e92712017-12-04 11:45:51 -0700134 if (GrCCPRCoverageProcessor::RenderPassIsCubic(fRenderPass)) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600135 outline.cubicTo(fPoints[1], fPoints[2], fPoints[3]);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600136 } else if (is_quadratic(fRenderPass)) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600137 outline.quadTo(fPoints[1], fPoints[3]);
138 } else {
139 outline.lineTo(fPoints[1]);
140 outline.lineTo(fPoints[3]);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600141 outline.close();
Chris Dalton1a325d22017-07-14 15:17:41 -0600142 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600143
144 SkPaint outlinePaint;
145 outlinePaint.setColor(0x30000000);
146 outlinePaint.setStyle(SkPaint::kStroke_Style);
147 outlinePaint.setStrokeWidth(0);
148 outlinePaint.setAntiAlias(true);
Chris Dalton1a325d22017-07-14 15:17:41 -0600149 canvas->drawPath(outline, outlinePaint);
150
Chris Dalton7f578bf2017-09-05 16:46:48 -0600151#if 0
152 SkPaint gridPaint;
153 gridPaint.setColor(0x10000000);
154 gridPaint.setStyle(SkPaint::kStroke_Style);
155 gridPaint.setStrokeWidth(0);
156 gridPaint.setAntiAlias(true);
Chris Daltona640c492017-09-11 22:04:03 -0700157 for (int y = 0; y < this->height(); y += kDebugBloat) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600158 canvas->drawLine(0, y, this->width(), y, gridPaint);
159 }
Chris Daltona640c492017-09-11 22:04:03 -0700160 for (int x = 0; x < this->width(); x += kDebugBloat) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600161 canvas->drawLine(x, 0, x, this->height(), outlinePaint);
162 }
163#endif
164
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700165 SkString caption;
Chris Dalton1a325d22017-07-14 15:17:41 -0600166 if (GrRenderTargetContext* rtc =
167 canvas->internal_private_accessTopLayerRenderTargetContext()) {
168 rtc->priv().testingOnly_addDrawOp(skstd::make_unique<Op>(this));
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700169 caption.appendf("RenderPass_%s", GrCCPRCoverageProcessor::RenderPassName(fRenderPass));
170 if (GrCCPRCoverageProcessor::RenderPassIsCubic(fRenderPass)) {
171 caption.appendf(" (%s)", SkCubicTypeName(fCubicType));
172 }
173 } else {
174 caption = "Use GPU backend to visualize geometry.";
Chris Dalton1a325d22017-07-14 15:17:41 -0600175 }
176
177 SkPaint pointsPaint;
178 pointsPaint.setColor(SK_ColorBLUE);
179 pointsPaint.setStrokeWidth(8);
180 pointsPaint.setAntiAlias(true);
181
Chris Daltona3e92712017-12-04 11:45:51 -0700182 if (GrCCPRCoverageProcessor::RenderPassIsCubic(fRenderPass)) {
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);
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700197 canvas->drawText(caption.c_str(), caption.size(), 10, 30, captionPaint);
Chris Dalton1a325d22017-07-14 15:17:41 -0600198}
199
200void CCPRGeometryView::updateGpuData() {
Chris Daltona3e92712017-12-04 11:45:51 -0700201 fTriangleInstances.reset();
202 fCubicInstances.reset();
Chris Dalton1a325d22017-07-14 15:17:41 -0600203
Chris Daltona3e92712017-12-04 11:45:51 -0700204 if (GrCCPRCoverageProcessor::RenderPassIsCubic(fRenderPass)) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600205 double t[2], s[2];
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700206 fCubicType = GrPathUtils::getCubicKLM(fPoints, &fCubicKLM, t, s);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600207 GrCCPRGeometry geometry;
208 geometry.beginContour(fPoints[0]);
Chris Daltona640c492017-09-11 22:04:03 -0700209 geometry.cubicTo(fPoints[1], fPoints[2], fPoints[3], kDebugBloat/2, kDebugBloat/2);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600210 geometry.endContour();
Chris Dalton7f578bf2017-09-05 16:46:48 -0600211 int ptsIdx = 0;
212 for (GrCCPRGeometry::Verb verb : geometry.verbs()) {
213 switch (verb) {
214 case GrCCPRGeometry::Verb::kLineTo:
215 ++ptsIdx;
216 continue;
217 case GrCCPRGeometry::Verb::kMonotonicQuadraticTo:
218 ptsIdx += 2;
219 continue;
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700220 case GrCCPRGeometry::Verb::kMonotonicCubicTo:
Chris Daltona3e92712017-12-04 11:45:51 -0700221 fCubicInstances.push_back().set(&geometry.points()[ptsIdx], 0, 0);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600222 ptsIdx += 3;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600223 continue;
224 default: continue;
Chris Dalton1a325d22017-07-14 15:17:41 -0600225 }
226 }
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600227 } else if (is_quadratic(fRenderPass)) {
Chris Daltonc1e59632017-09-05 00:30:07 -0600228 GrCCPRGeometry geometry;
229 geometry.beginContour(fPoints[0]);
230 geometry.quadraticTo(fPoints[1], fPoints[3]);
231 geometry.endContour();
Chris Daltona3e92712017-12-04 11:45:51 -0700232 int ptsIdx = 0;
Chris Daltonc1e59632017-09-05 00:30:07 -0600233 for (GrCCPRGeometry::Verb verb : geometry.verbs()) {
234 if (GrCCPRGeometry::Verb::kBeginContour == verb ||
235 GrCCPRGeometry::Verb::kEndOpenContour == verb ||
236 GrCCPRGeometry::Verb::kEndClosedContour == verb) {
237 continue;
238 }
239 SkASSERT(GrCCPRGeometry::Verb::kMonotonicQuadraticTo == verb);
Chris Daltona3e92712017-12-04 11:45:51 -0700240 fTriangleInstances.push_back().set(&geometry.points()[ptsIdx], Sk2f(0, 0));
241 ptsIdx += 2;
Chris Daltonb072bb62017-08-07 09:00:46 -0600242 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600243 } else {
Chris Daltona3e92712017-12-04 11:45:51 -0700244 fTriangleInstances.push_back().set(fPoints[0], fPoints[1], fPoints[3], Sk2f(0, 0));
Chris Dalton1a325d22017-07-14 15:17:41 -0600245 }
246}
247
248void CCPRGeometryView::Op::onExecute(GrOpFlushState* state) {
249 GrResourceProvider* rp = state->resourceProvider();
250 GrContext* context = state->gpu()->getContext();
251 GrGLGpu* glGpu = kOpenGL_GrBackend == context->contextPriv().getBackend() ?
252 static_cast<GrGLGpu*>(state->gpu()) : nullptr;
Chris Dalton1a325d22017-07-14 15:17:41 -0600253
Chris Dalton23261772017-12-10 16:41:45 -0700254 GrCCPRCoverageProcessor proc(fView->fRenderPass);
255 SkDEBUGCODE(proc.enableDebugVisualizations(kDebugBloat);)
256
257 SkSTArray<1, GrMesh, true> mesh;
258 if (GrCCPRCoverageProcessor::RenderPassIsCubic(fView->fRenderPass)) {
Chris Daltona3e92712017-12-04 11:45:51 -0700259 sk_sp<GrBuffer> instBuff(rp->createBuffer(fView->fCubicInstances.count() *
260 sizeof(CubicInstance), kVertex_GrBufferType,
261 kDynamic_GrAccessPattern,
Chris Dalton1a325d22017-07-14 15:17:41 -0600262 GrResourceProvider::kNoPendingIO_Flag |
263 GrResourceProvider::kRequireGpuMemory_Flag,
Chris Daltona3e92712017-12-04 11:45:51 -0700264 fView->fCubicInstances.begin()));
Chris Dalton23261772017-12-10 16:41:45 -0700265 if (!fView->fCubicInstances.empty() && instBuff) {
266 proc.appendMesh(instBuff.get(), fView->fCubicInstances.count(), 0, &mesh);
Chris Daltona3e92712017-12-04 11:45:51 -0700267 }
Chris Daltona3e92712017-12-04 11:45:51 -0700268 } else {
Chris Daltona3e92712017-12-04 11:45:51 -0700269 sk_sp<GrBuffer> instBuff(rp->createBuffer(fView->fTriangleInstances.count() *
270 sizeof(TriangleInstance), kVertex_GrBufferType,
271 kDynamic_GrAccessPattern,
272 GrResourceProvider::kNoPendingIO_Flag |
273 GrResourceProvider::kRequireGpuMemory_Flag,
274 fView->fTriangleInstances.begin()));
Chris Dalton23261772017-12-10 16:41:45 -0700275 if (!fView->fTriangleInstances.empty() && instBuff) {
276 proc.appendMesh(instBuff.get(), fView->fTriangleInstances.count(), 0, &mesh);
Chris Daltona3e92712017-12-04 11:45:51 -0700277 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600278 }
279
Robert Phillips2890fbf2017-07-26 15:48:41 -0400280 GrPipeline pipeline(state->drawOpArgs().fProxy, GrPipeline::ScissorState::kDisabled,
Chris Dalton1a325d22017-07-14 15:17:41 -0600281 SkBlendMode::kSrcOver);
282
Chris Dalton1a325d22017-07-14 15:17:41 -0600283 if (glGpu) {
284 glGpu->handleDirtyContext();
285 GR_GL_CALL(glGpu->glInterface(), PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
286 GR_GL_CALL(glGpu->glInterface(), Enable(GR_GL_LINE_SMOOTH));
287 }
288
Chris Dalton23261772017-12-10 16:41:45 -0700289 if (!mesh.empty()) {
290 SkASSERT(1 == mesh.count());
291 state->rtCommandBuffer()->draw(pipeline, proc, mesh.begin(), nullptr, 1, this->bounds());
292 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600293
294 if (glGpu) {
295 context->resetContext(kMisc_GrGLBackendState);
296 }
297}
298
299class CCPRGeometryView::Click : public SampleView::Click {
300public:
301 Click(SkView* target, int ptIdx) : SampleView::Click(target), fPtIdx(ptIdx) {}
302
303 void doClick(SkPoint points[]) {
304 if (fPtIdx >= 0) {
305 this->dragPoint(points, fPtIdx);
306 } else {
307 for (int i = 0; i < 4; ++i) {
308 this->dragPoint(points, i);
309 }
310 }
311 }
312
313private:
314 void dragPoint(SkPoint points[], int idx) {
315 SkIPoint delta = fICurr - fIPrev;
316 points[idx] += SkPoint::Make(delta.x(), delta.y());
317 }
318
319 int fPtIdx;
320};
321
322SkView::Click* CCPRGeometryView::onFindClickHandler(SkScalar x, SkScalar y, unsigned) {
323 for (int i = 0; i < 4; ++i) {
Chris Daltona3e92712017-12-04 11:45:51 -0700324 if (!GrCCPRCoverageProcessor::RenderPassIsCubic(fRenderPass) && 2 == i) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600325 continue;
326 }
327 if (fabs(x - fPoints[i].x()) < 20 && fabsf(y - fPoints[i].y()) < 20) {
328 return new Click(this, i);
329 }
330 }
331 return new Click(this, -1);
332}
333
334bool CCPRGeometryView::onClick(SampleView::Click* click) {
335 Click* myClick = (Click*) click;
336 myClick->doClick(fPoints);
337 this->updateAndInval();
338 return true;
339}
340
341bool CCPRGeometryView::onQuery(SkEvent* evt) {
342 if (SampleCode::TitleQ(*evt)) {
343 SampleCode::TitleR(evt, "CCPRGeometry");
344 return true;
345 }
346 SkUnichar unichar;
347 if (SampleCode::CharQ(*evt, &unichar)) {
348 if (unichar >= '1' && unichar <= '7') {
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600349 fRenderPass = RenderPass(unichar - '1');
Chris Dalton1a325d22017-07-14 15:17:41 -0600350 this->updateAndInval();
351 return true;
352 }
353 if (unichar == 'D') {
354 SkDebugf(" SkPoint fPoints[4] = {\n");
Chris Dalton7f578bf2017-09-05 16:46:48 -0600355 SkDebugf(" {%ff, %ff},\n", fPoints[0].x(), fPoints[0].y());
356 SkDebugf(" {%ff, %ff},\n", fPoints[1].x(), fPoints[1].y());
357 SkDebugf(" {%ff, %ff},\n", fPoints[2].x(), fPoints[2].y());
358 SkDebugf(" {%ff, %ff}\n", fPoints[3].x(), fPoints[3].y());
Chris Dalton1a325d22017-07-14 15:17:41 -0600359 SkDebugf(" };\n");
360 return true;
361 }
362 }
363 return this->INHERITED::onQuery(evt);
364}
365
366DEF_SAMPLE( return new CCPRGeometryView; )
367
368#endif // SK_SUPPORT_GPU