blob: 0aa8100c22b2f23eeb30923aaa35800a5b99f538 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTypes.h"
Chris Dalton1a325d22017-07-14 15:17:41 -06009
10#if SK_SUPPORT_GPU
11
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkCanvas.h"
13#include "include/core/SkPaint.h"
14#include "include/core/SkPath.h"
Robert Phillips30ebcf72020-07-09 13:25:17 -040015#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "samplecode/Sample.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkRectPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrContextPriv.h"
Brian Salomonf4ba4ec2020-03-19 15:54:28 -040019#include "src/gpu/GrGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrMemoryPool.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040021#include "src/gpu/GrOnFlushResourceProvider.h"
22#include "src/gpu/GrOpFlushState.h"
Robert Phillips30ebcf72020-07-09 13:25:17 -040023#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrRenderTargetContext.h"
25#include "src/gpu/GrRenderTargetContextPriv.h"
26#include "src/gpu/GrResourceProvider.h"
27#include "src/gpu/ccpr/GrCCCoverageProcessor.h"
28#include "src/gpu/ccpr/GrCCFillGeometry.h"
29#include "src/gpu/ccpr/GrCCStroker.h"
30#include "src/gpu/ccpr/GrGSCoverageProcessor.h"
31#include "src/gpu/ccpr/GrVSCoverageProcessor.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040032#include "src/gpu/geometry/GrPathUtils.h"
Robert Phillips03e4c952019-11-26 16:20:22 -050033#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/ops/GrDrawOp.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060035
Brian Salomonf4ba4ec2020-03-19 15:54:28 -040036#ifdef SK_GL
37#include "src/gpu/gl/GrGLGpu.h"
38#endif
39
Chris Dalton84403d72018-02-13 21:46:17 -050040using TriPointInstance = GrCCCoverageProcessor::TriPointInstance;
41using QuadPointInstance = GrCCCoverageProcessor::QuadPointInstance;
Chris Dalton8dfc70f2018-03-26 19:15:22 -060042using PrimitiveType = GrCCCoverageProcessor::PrimitiveType;
Chris Dalton1a325d22017-07-14 15:17:41 -060043
Chris Daltona640c492017-09-11 22:04:03 -070044static constexpr float kDebugBloat = 40;
45
Chris Dalton1a325d22017-07-14 15:17:41 -060046/**
47 * This sample visualizes the AA bloat geometry generated by the ccpr geometry shaders. It
48 * increases the AA bloat by 50x and outputs color instead of coverage (coverage=+1 -> green,
49 * coverage=0 -> black, coverage=-1 -> red). Use the keys 1-7 to cycle through the different
50 * geometry processors.
51 */
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040052class CCPRGeometryView : public Sample {
Hal Canaryd7639af2019-07-17 09:08:11 -040053 void onOnceBeforeDraw() override { this->updateGpuData(); }
Chris Dalton1a325d22017-07-14 15:17:41 -060054 void onDrawContent(SkCanvas*) override;
55
Hal Canaryb1f411a2019-08-29 10:39:22 -040056 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040057 bool onClick(Sample::Click*) override;
Hal Canary6cc65e12019-07-03 15:53:04 -040058 bool onChar(SkUnichar) override;
Hal Canary8a027312019-07-03 10:55:44 -040059 SkString name() override { return SkString("CCPRGeometry"); }
Chris Dalton1a325d22017-07-14 15:17:41 -060060
Chris Dalton1a325d22017-07-14 15:17:41 -060061 class Click;
Chris Dalton8d38a7f2018-03-19 14:16:44 -060062 class DrawCoverageCountOp;
63 class VisualizeCoverageCountFP;
Chris Dalton1a325d22017-07-14 15:17:41 -060064
Chris Dalton383a2ef2018-01-08 17:21:41 -050065 void updateAndInval() { this->updateGpuData(); }
Chris Dalton1a325d22017-07-14 15:17:41 -060066
67 void updateGpuData();
68
Chris Dalton8dfc70f2018-03-26 19:15:22 -060069 PrimitiveType fPrimitiveType = PrimitiveType::kTriangles;
Chris Daltonbe4ffab2017-12-08 10:59:58 -070070 SkCubicType fCubicType;
Chris Dalton7f578bf2017-09-05 16:46:48 -060071 SkMatrix fCubicKLM;
Chris Dalton1a325d22017-07-14 15:17:41 -060072
73 SkPoint fPoints[4] = {
Chris Dalton383a2ef2018-01-08 17:21:41 -050074 {100.05f, 100.05f}, {400.75f, 100.05f}, {400.75f, 300.95f}, {100.05f, 300.95f}};
Chris Dalton1a325d22017-07-14 15:17:41 -060075
Chris Dalton9f2dab02018-04-18 14:07:03 -060076 float fConicWeight = .5;
Chris Dalton09a7bb22018-08-31 19:53:15 +080077 float fStrokeWidth = 40;
78 bool fDoStroke = false;
Chris Dalton9f2dab02018-04-18 14:07:03 -060079
Chris Dalton84403d72018-02-13 21:46:17 -050080 SkTArray<TriPointInstance> fTriPointInstances;
81 SkTArray<QuadPointInstance> fQuadPointInstances;
Chris Dalton09a7bb22018-08-31 19:53:15 +080082 SkPath fPath;
Chris Dalton1a325d22017-07-14 15:17:41 -060083};
84
Chris Dalton8d38a7f2018-03-19 14:16:44 -060085class CCPRGeometryView::DrawCoverageCountOp : public GrDrawOp {
Chris Dalton1a325d22017-07-14 15:17:41 -060086 DEFINE_OP_CLASS_ID
87
88public:
Chris Dalton8d38a7f2018-03-19 14:16:44 -060089 DrawCoverageCountOp(CCPRGeometryView* view) : INHERITED(ClassID()), fView(view) {
Chris Daltone1639692018-08-20 14:00:30 -060090 this->setBounds(SkRect::MakeIWH(fView->width(), fView->height()), GrOp::HasAABloat::kNo,
Greg Daniel5faf4742019-10-01 15:14:44 -040091 GrOp::IsHairline::kNo);
Chris Dalton1a325d22017-07-14 15:17:41 -060092 }
93
Chris Dalton8d38a7f2018-03-19 14:16:44 -060094 const char* name() const override {
95 return "[Testing/Sample code] CCPRGeometryView::DrawCoverageCountOp";
96 }
Chris Dalton1a325d22017-07-14 15:17:41 -060097
98private:
99 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600100 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
101 bool hasMixedSampledCoverage, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -0700102 return GrProcessorSet::EmptySetAnalysis();
Chris Dalton1a325d22017-07-14 15:17:41 -0600103 }
Robert Phillipsc655c3a2020-03-18 13:23:45 -0400104 void onPrePrepare(GrRecordingContext*,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400105 const GrSurfaceProxyView* writeView,
Robert Phillipsc655c3a2020-03-18 13:23:45 -0400106 GrAppliedClip*,
107 const GrXferProcessor::DstProxyView&) override {}
Chris Dalton1a325d22017-07-14 15:17:41 -0600108 void onPrepare(GrOpFlushState*) override {}
Brian Salomon588cec72018-11-14 13:56:37 -0500109 void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
Chris Dalton1a325d22017-07-14 15:17:41 -0600110
111 CCPRGeometryView* fView;
112
113 typedef GrDrawOp INHERITED;
114};
115
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600116class CCPRGeometryView::VisualizeCoverageCountFP : public GrFragmentProcessor {
117public:
John Stiles39156c22020-07-21 12:05:47 -0400118 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> inputFP) {
119 return std::unique_ptr<GrFragmentProcessor>(
120 new VisualizeCoverageCountFP(std::move(inputFP)));
121 }
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600122
123private:
124 const char* name() const override {
John Stiles39156c22020-07-21 12:05:47 -0400125 return "VisualizeCoverageCountFP";
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600126 }
127 std::unique_ptr<GrFragmentProcessor> clone() const override {
John Stiles39156c22020-07-21 12:05:47 -0400128 return std::unique_ptr<GrFragmentProcessor>(new VisualizeCoverageCountFP(*this));
129 }
130 VisualizeCoverageCountFP(std::unique_ptr<GrFragmentProcessor> inputFP)
131 : GrFragmentProcessor(kTestFP_ClassID, kNone_OptimizationFlags) {
132 this->registerChild(std::move(inputFP));
133 }
134 VisualizeCoverageCountFP(const VisualizeCoverageCountFP& that)
135 : GrFragmentProcessor(kTestFP_ClassID, kNone_OptimizationFlags) {
136 this->cloneAndRegisterAllChildProcessors(that);
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600137 }
138 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
139 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
140
141 class Impl : public GrGLSLFragmentProcessor {
142 void emitCode(EmitArgs& args) override {
143 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
John Stiles39156c22020-07-21 12:05:47 -0400144 static constexpr int kInputFPIndex = 0;
145 SkString inputColor = this->invokeChild(kInputFPIndex, args);
146 f->codeAppendf("half count = %s.a;", inputColor.c_str());
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600147 f->codeAppendf("%s = half4(clamp(-count, 0, 1), clamp(+count, 0, 1), 0, abs(count));",
148 args.fOutputColor);
149 }
150 };
151
152 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new Impl; }
153};
154
Chris Dalton7f578bf2017-09-05 16:46:48 -0600155static void draw_klm_line(int w, int h, SkCanvas* canvas, const SkScalar line[3], SkColor color) {
156 SkPoint p1, p2;
157 if (SkScalarAbs(line[1]) > SkScalarAbs(line[0])) {
158 // Draw from vertical edge to vertical edge.
159 p1 = {0, -line[2] / line[1]};
Chris Dalton383a2ef2018-01-08 17:21:41 -0500160 p2 = {(SkScalar)w, (-line[2] - w * line[0]) / line[1]};
Chris Dalton7f578bf2017-09-05 16:46:48 -0600161 } else {
162 // Draw from horizontal edge to horizontal edge.
163 p1 = {-line[2] / line[0], 0};
Chris Dalton383a2ef2018-01-08 17:21:41 -0500164 p2 = {(-line[2] - h * line[1]) / line[0], (SkScalar)h};
Chris Dalton7f578bf2017-09-05 16:46:48 -0600165 }
166
167 SkPaint linePaint;
168 linePaint.setColor(color);
169 linePaint.setAlpha(128);
170 linePaint.setStyle(SkPaint::kStroke_Style);
171 linePaint.setStrokeWidth(0);
172 linePaint.setAntiAlias(true);
173 canvas->drawLine(p1, p2, linePaint);
174}
175
Chris Dalton1a325d22017-07-14 15:17:41 -0600176void CCPRGeometryView::onDrawContent(SkCanvas* canvas) {
Chris Dalton8738cf42018-03-09 11:57:40 -0700177 canvas->clear(SK_ColorBLACK);
Chris Dalton1a325d22017-07-14 15:17:41 -0600178
Chris Dalton09a7bb22018-08-31 19:53:15 +0800179 if (!fDoStroke) {
180 SkPaint outlinePaint;
181 outlinePaint.setColor(0x80ffffff);
182 outlinePaint.setStyle(SkPaint::kStroke_Style);
183 outlinePaint.setStrokeWidth(0);
184 outlinePaint.setAntiAlias(true);
185 canvas->drawPath(fPath, outlinePaint);
Chris Dalton1a325d22017-07-14 15:17:41 -0600186 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600187
Chris Dalton7f578bf2017-09-05 16:46:48 -0600188#if 0
189 SkPaint gridPaint;
190 gridPaint.setColor(0x10000000);
191 gridPaint.setStyle(SkPaint::kStroke_Style);
192 gridPaint.setStrokeWidth(0);
193 gridPaint.setAntiAlias(true);
Chris Daltona640c492017-09-11 22:04:03 -0700194 for (int y = 0; y < this->height(); y += kDebugBloat) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600195 canvas->drawLine(0, y, this->width(), y, gridPaint);
196 }
Chris Daltona640c492017-09-11 22:04:03 -0700197 for (int x = 0; x < this->width(); x += kDebugBloat) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600198 canvas->drawLine(x, 0, x, this->height(), outlinePaint);
199 }
200#endif
201
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700202 SkString caption;
Chris Dalton383a2ef2018-01-08 17:21:41 -0500203 if (GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext()) {
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600204 // Render coverage count.
Robert Phillips30ebcf72020-07-09 13:25:17 -0400205 auto ctx = canvas->recordingContext();
Chris Daltone1639692018-08-20 14:00:30 -0600206 SkASSERT(ctx);
207
Robert Phillips9da87e02019-02-04 13:26:26 -0500208 GrOpMemoryPool* pool = ctx->priv().opMemoryPool();
Ben Wagnerd4bffed2018-08-14 17:51:23 -0400209
Greg Daniele20fcad2020-01-08 11:52:34 -0500210 int width = this->width();
211 int height = this->height();
212 auto ccbuff = GrRenderTargetContext::Make(
213 ctx, GrColorType::kAlpha_F16, nullptr, SkBackingFit::kApprox, {width, height});
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600214 SkASSERT(ccbuff);
Michael Ludwig81d41722020-05-26 16:57:38 -0400215 ccbuff->clear(SK_PMColor4fTRANSPARENT);
Ben Wagnerd4bffed2018-08-14 17:51:23 -0400216 ccbuff->priv().testingOnly_addDrawOp(pool->allocate<DrawCoverageCountOp>(this));
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600217
218 // Visualize coverage count in main canvas.
219 GrPaint paint;
John Stiles5933d7d2020-07-21 12:28:35 -0400220 paint.setColorFragmentProcessor(VisualizeCoverageCountFP::Make(
John Stiles39156c22020-07-21 12:05:47 -0400221 GrTextureEffect::Make(ccbuff->readSurfaceView(), ccbuff->colorInfo().alphaType())));
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600222 paint.setPorterDuffXPFactory(SkBlendMode::kSrcOver);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400223 rtc->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600224 SkRect::MakeIWH(this->width(), this->height()));
225
226 // Add label.
Chris Dalton703b4762018-04-06 16:11:48 -0600227 caption.appendf("PrimitiveType_%s",
228 GrCCCoverageProcessor::PrimitiveTypeName(fPrimitiveType));
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600229 if (PrimitiveType::kCubics == fPrimitiveType) {
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700230 caption.appendf(" (%s)", SkCubicTypeName(fCubicType));
Chris Dalton9f2dab02018-04-18 14:07:03 -0600231 } else if (PrimitiveType::kConics == fPrimitiveType) {
232 caption.appendf(" (w=%f)", fConicWeight);
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700233 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800234 if (fDoStroke) {
235 caption.appendf(" (stroke_width=%f)", fStrokeWidth);
236 }
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700237 } else {
238 caption = "Use GPU backend to visualize geometry.";
Chris Dalton1a325d22017-07-14 15:17:41 -0600239 }
240
241 SkPaint pointsPaint;
242 pointsPaint.setColor(SK_ColorBLUE);
243 pointsPaint.setStrokeWidth(8);
244 pointsPaint.setAntiAlias(true);
245
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600246 if (PrimitiveType::kCubics == fPrimitiveType) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600247 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, fPoints, pointsPaint);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800248 if (!fDoStroke) {
249 int w = this->width(), h = this->height();
250 draw_klm_line(w, h, canvas, &fCubicKLM[0], SK_ColorYELLOW);
251 draw_klm_line(w, h, canvas, &fCubicKLM[3], SK_ColorBLUE);
252 draw_klm_line(w, h, canvas, &fCubicKLM[6], SK_ColorRED);
253 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600254 } else {
255 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, fPoints, pointsPaint);
256 canvas->drawPoints(SkCanvas::kPoints_PointMode, 1, fPoints + 3, pointsPaint);
257 }
258
Hal Canary4484b8f2019-01-08 14:00:08 -0500259 SkFont font(nullptr, 20);
Chris Dalton1a325d22017-07-14 15:17:41 -0600260 SkPaint captionPaint;
Chris Dalton8738cf42018-03-09 11:57:40 -0700261 captionPaint.setColor(SK_ColorWHITE);
Hal Canary4484b8f2019-01-08 14:00:08 -0500262 canvas->drawString(caption, 10, 30, font, captionPaint);
Chris Dalton1a325d22017-07-14 15:17:41 -0600263}
264
265void CCPRGeometryView::updateGpuData() {
Chris Daltone1639692018-08-20 14:00:30 -0600266 using Verb = GrCCFillGeometry::Verb;
Chris Dalton84403d72018-02-13 21:46:17 -0500267 fTriPointInstances.reset();
268 fQuadPointInstances.reset();
Chris Dalton1a325d22017-07-14 15:17:41 -0600269
Chris Dalton09a7bb22018-08-31 19:53:15 +0800270 fPath.reset();
271 fPath.moveTo(fPoints[0]);
272
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600273 if (PrimitiveType::kCubics == fPrimitiveType) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600274 double t[2], s[2];
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700275 fCubicType = GrPathUtils::getCubicKLM(fPoints, &fCubicKLM, t, s);
Chris Daltone1639692018-08-20 14:00:30 -0600276 GrCCFillGeometry geometry;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600277 geometry.beginContour(fPoints[0]);
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600278 geometry.cubicTo(fPoints, kDebugBloat / 2, kDebugBloat / 2);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600279 geometry.endContour();
Chris Dalton7f578bf2017-09-05 16:46:48 -0600280 int ptsIdx = 0;
Chris Daltone1639692018-08-20 14:00:30 -0600281 for (Verb verb : geometry.verbs()) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600282 switch (verb) {
Chris Daltone1639692018-08-20 14:00:30 -0600283 case Verb::kLineTo:
Chris Dalton7f578bf2017-09-05 16:46:48 -0600284 ++ptsIdx;
285 continue;
Chris Daltone1639692018-08-20 14:00:30 -0600286 case Verb::kMonotonicQuadraticTo:
Chris Dalton7f578bf2017-09-05 16:46:48 -0600287 ptsIdx += 2;
288 continue;
Chris Daltone1639692018-08-20 14:00:30 -0600289 case Verb::kMonotonicCubicTo:
Chris Dalton84403d72018-02-13 21:46:17 -0500290 fQuadPointInstances.push_back().set(&geometry.points()[ptsIdx], 0, 0);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600291 ptsIdx += 3;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600292 continue;
Chris Dalton383a2ef2018-01-08 17:21:41 -0500293 default:
294 continue;
Chris Dalton1a325d22017-07-14 15:17:41 -0600295 }
296 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800297 fPath.cubicTo(fPoints[1], fPoints[2], fPoints[3]);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600298 } else if (PrimitiveType::kTriangles != fPrimitiveType) {
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600299 SkPoint P3[3] = {fPoints[0], fPoints[1], fPoints[3]};
Chris Daltone1639692018-08-20 14:00:30 -0600300 GrCCFillGeometry geometry;
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600301 geometry.beginContour(P3[0]);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600302 if (PrimitiveType::kQuadratics == fPrimitiveType) {
303 geometry.quadraticTo(P3);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800304 fPath.quadTo(fPoints[1], fPoints[3]);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600305 } else {
306 SkASSERT(PrimitiveType::kConics == fPrimitiveType);
307 geometry.conicTo(P3, fConicWeight);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800308 fPath.conicTo(fPoints[1], fPoints[3], fConicWeight);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600309 }
Chris Daltonc1e59632017-09-05 00:30:07 -0600310 geometry.endContour();
Chris Dalton9f2dab02018-04-18 14:07:03 -0600311 int ptsIdx = 0, conicWeightIdx = 0;
Chris Daltone1639692018-08-20 14:00:30 -0600312 for (Verb verb : geometry.verbs()) {
313 if (Verb::kBeginContour == verb ||
314 Verb::kEndOpenContour == verb ||
315 Verb::kEndClosedContour == verb) {
Chris Daltonc1e59632017-09-05 00:30:07 -0600316 continue;
317 }
Chris Daltone1639692018-08-20 14:00:30 -0600318 if (Verb::kLineTo == verb) {
Chris Dalton90e8fb12017-12-22 02:24:53 -0700319 ++ptsIdx;
320 continue;
321 }
Chris Daltone1639692018-08-20 14:00:30 -0600322 SkASSERT(Verb::kMonotonicQuadraticTo == verb || Verb::kMonotonicConicTo == verb);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600323 if (PrimitiveType::kQuadratics == fPrimitiveType &&
Chris Daltone1639692018-08-20 14:00:30 -0600324 Verb::kMonotonicQuadraticTo == verb) {
Chris Daltonc3318f02019-07-19 14:20:53 -0600325 fTriPointInstances.push_back().set(
326 &geometry.points()[ptsIdx], Sk2f(0, 0),
327 TriPointInstance::Ordering::kXYTransposed);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600328 } else if (PrimitiveType::kConics == fPrimitiveType &&
Chris Daltone1639692018-08-20 14:00:30 -0600329 Verb::kMonotonicConicTo == verb) {
Chris Dalton9f2dab02018-04-18 14:07:03 -0600330 fQuadPointInstances.push_back().setW(&geometry.points()[ptsIdx], Sk2f(0, 0),
331 geometry.getConicWeight(conicWeightIdx++));
332 }
Chris Daltona3e92712017-12-04 11:45:51 -0700333 ptsIdx += 2;
Chris Daltonb072bb62017-08-07 09:00:46 -0600334 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600335 } else {
Chris Daltonc3318f02019-07-19 14:20:53 -0600336 fTriPointInstances.push_back().set(
337 fPoints[0], fPoints[1], fPoints[3], Sk2f(0, 0),
338 TriPointInstance::Ordering::kXYTransposed);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800339 fPath.lineTo(fPoints[1]);
340 fPath.lineTo(fPoints[3]);
341 fPath.close();
Chris Dalton1a325d22017-07-14 15:17:41 -0600342 }
343}
344
Brian Salomon588cec72018-11-14 13:56:37 -0500345void CCPRGeometryView::DrawCoverageCountOp::onExecute(GrOpFlushState* state,
346 const SkRect& chainBounds) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600347 GrResourceProvider* rp = state->resourceProvider();
Adlai Holler3d0359a2020-07-09 15:35:55 -0400348 auto direct = state->gpu()->getContext();
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400349#ifdef SK_GL
Robert Phillips30ebcf72020-07-09 13:25:17 -0400350 GrGLGpu* glGpu = GrBackendApi::kOpenGL == direct->backend()
Chris Dalton383a2ef2018-01-08 17:21:41 -0500351 ? static_cast<GrGLGpu*>(state->gpu())
352 : nullptr;
Chris Dalton1a325d22017-07-14 15:17:41 -0600353 if (glGpu) {
354 glGpu->handleDirtyContext();
Chris Dalton8738cf42018-03-09 11:57:40 -0700355 // GR_GL_CALL(glGpu->glInterface(), PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
Chris Dalton1a325d22017-07-14 15:17:41 -0600356 GR_GL_CALL(glGpu->glInterface(), Enable(GR_GL_LINE_SMOOTH));
357 }
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400358#endif
Chris Dalton1a325d22017-07-14 15:17:41 -0600359
Greg Daniel2c3398d2019-06-19 11:58:01 -0400360 GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kPlus,
Brian Salomon982f5462020-03-30 12:52:33 -0400361 state->drawOpArgs().writeSwizzle());
Chris Dalton09a7bb22018-08-31 19:53:15 +0800362
Chris Dalton2c5e0112019-03-29 13:14:18 -0500363 std::unique_ptr<GrCCCoverageProcessor> proc;
364 if (state->caps().shaderCaps()->geometryShaderSupport()) {
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500365 proc = std::make_unique<GrGSCoverageProcessor>();
Chris Dalton2c5e0112019-03-29 13:14:18 -0500366 } else {
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500367 proc = std::make_unique<GrVSCoverageProcessor>();
Chris Dalton2c5e0112019-03-29 13:14:18 -0500368 }
Chris Dalton39ca9732020-03-10 10:34:17 -0600369 SkDEBUGCODE(proc->enableDebugBloat(kDebugBloat));
370
371 GrOpsRenderPass* renderPass = state->opsRenderPass();
Chris Dalton2c5e0112019-03-29 13:14:18 -0500372
Chris Dalton09a7bb22018-08-31 19:53:15 +0800373 if (!fView->fDoStroke) {
Chris Dalton39ca9732020-03-10 10:34:17 -0600374 for (int i = 0; i < proc->numSubpasses(); ++i) {
375 proc->reset(fView->fPrimitiveType, i, rp);
376 proc->bindPipeline(state, pipeline, this->bounds());
Chris Dalton09a7bb22018-08-31 19:53:15 +0800377
Chris Dalton39ca9732020-03-10 10:34:17 -0600378 if (PrimitiveType::kCubics == fView->fPrimitiveType ||
379 PrimitiveType::kConics == fView->fPrimitiveType) {
380 sk_sp<GrGpuBuffer> instBuff(rp->createBuffer(
381 fView->fQuadPointInstances.count() * sizeof(QuadPointInstance),
382 GrGpuBufferType::kVertex, kDynamic_GrAccessPattern,
383 fView->fQuadPointInstances.begin()));
384 if (!fView->fQuadPointInstances.empty() && instBuff) {
Greg Daniel426274b2020-07-20 11:37:38 -0400385 proc->bindBuffers(renderPass, std::move(instBuff));
Chris Dalton39ca9732020-03-10 10:34:17 -0600386 proc->drawInstances(renderPass, fView->fQuadPointInstances.count(), 0);
387 }
388 } else {
389 sk_sp<GrGpuBuffer> instBuff(rp->createBuffer(
390 fView->fTriPointInstances.count() * sizeof(TriPointInstance),
391 GrGpuBufferType::kVertex, kDynamic_GrAccessPattern,
392 fView->fTriPointInstances.begin()));
393 if (!fView->fTriPointInstances.empty() && instBuff) {
Greg Daniel426274b2020-07-20 11:37:38 -0400394 proc->bindBuffers(renderPass, std::move(instBuff));
Chris Dalton39ca9732020-03-10 10:34:17 -0600395 proc->drawInstances(renderPass, fView->fTriPointInstances.count(), 0);
396 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800397 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800398 }
399 } else if (PrimitiveType::kConics != fView->fPrimitiveType) { // No conic stroke support yet.
400 GrCCStroker stroker(0,0,0);
401
402 SkPaint p;
403 p.setStyle(SkPaint::kStroke_Style);
404 p.setStrokeWidth(fView->fStrokeWidth);
405 p.setStrokeJoin(SkPaint::kMiter_Join);
406 p.setStrokeMiter(4);
407 // p.setStrokeCap(SkPaint::kRound_Cap);
408 stroker.parseDeviceSpaceStroke(fView->fPath, SkPathPriv::PointData(fView->fPath),
409 SkStrokeRec(p), p.getStrokeWidth(), GrScissorTest::kDisabled,
410 SkIRect::MakeWH(fView->width(), fView->height()), {0, 0});
411 GrCCStroker::BatchID batchID = stroker.closeCurrentBatch();
412
Robert Phillips30ebcf72020-07-09 13:25:17 -0400413 GrOnFlushResourceProvider onFlushRP(direct->priv().drawingManager());
Chris Dalton09a7bb22018-08-31 19:53:15 +0800414 stroker.prepareToDraw(&onFlushRP);
415
416 SkIRect ibounds;
417 this->bounds().roundOut(&ibounds);
Chris Dalton2c5e0112019-03-29 13:14:18 -0500418 stroker.drawStrokes(state, proc.get(), batchID, ibounds);
Chris Dalton23261772017-12-10 16:41:45 -0700419 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600420
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400421#ifdef SK_GL
Chris Dalton1a325d22017-07-14 15:17:41 -0600422 if (glGpu) {
Robert Phillips30ebcf72020-07-09 13:25:17 -0400423 direct->resetContext(kMisc_GrGLBackendState);
Chris Dalton1a325d22017-07-14 15:17:41 -0600424 }
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400425#endif
Chris Dalton1a325d22017-07-14 15:17:41 -0600426}
427
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400428class CCPRGeometryView::Click : public Sample::Click {
Chris Dalton1a325d22017-07-14 15:17:41 -0600429public:
Hal Canaryfcf63592019-07-12 11:32:43 -0400430 Click(int ptIdx) : fPtIdx(ptIdx) {}
Chris Dalton1a325d22017-07-14 15:17:41 -0600431
432 void doClick(SkPoint points[]) {
433 if (fPtIdx >= 0) {
Hal Canaryfcf63592019-07-12 11:32:43 -0400434 points[fPtIdx] += fCurr - fPrev;
Chris Dalton1a325d22017-07-14 15:17:41 -0600435 } else {
436 for (int i = 0; i < 4; ++i) {
Hal Canaryfcf63592019-07-12 11:32:43 -0400437 points[i] += fCurr - fPrev;
Chris Dalton1a325d22017-07-14 15:17:41 -0600438 }
439 }
440 }
441
442private:
Chris Dalton1a325d22017-07-14 15:17:41 -0600443 int fPtIdx;
444};
445
Hal Canaryb1f411a2019-08-29 10:39:22 -0400446Sample::Click* CCPRGeometryView::onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600447 for (int i = 0; i < 4; ++i) {
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600448 if (PrimitiveType::kCubics != fPrimitiveType && 2 == i) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600449 continue;
450 }
451 if (fabs(x - fPoints[i].x()) < 20 && fabsf(y - fPoints[i].y()) < 20) {
Hal Canaryfcf63592019-07-12 11:32:43 -0400452 return new Click(i);
Chris Dalton1a325d22017-07-14 15:17:41 -0600453 }
454 }
Hal Canaryfcf63592019-07-12 11:32:43 -0400455 return new Click(-1);
Chris Dalton1a325d22017-07-14 15:17:41 -0600456}
457
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400458bool CCPRGeometryView::onClick(Sample::Click* click) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500459 Click* myClick = (Click*)click;
Chris Dalton1a325d22017-07-14 15:17:41 -0600460 myClick->doClick(fPoints);
461 this->updateAndInval();
462 return true;
463}
464
Hal Canary6cc65e12019-07-03 15:53:04 -0400465bool CCPRGeometryView::onChar(SkUnichar unichar) {
Chris Dalton9f2dab02018-04-18 14:07:03 -0600466 if (unichar >= '1' && unichar <= '4') {
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600467 fPrimitiveType = PrimitiveType(unichar - '1');
Chris Dalton703b4762018-04-06 16:11:48 -0600468 if (fPrimitiveType >= PrimitiveType::kWeightedTriangles) {
469 fPrimitiveType = (PrimitiveType) ((int)fPrimitiveType + 1);
470 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600471 this->updateAndInval();
472 return true;
473 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800474 float* valueToScale = nullptr;
475 if (fDoStroke) {
476 valueToScale = &fStrokeWidth;
477 } else if (PrimitiveType::kConics == fPrimitiveType) {
478 valueToScale = &fConicWeight;
479 }
480 if (valueToScale) {
Chris Dalton9f2dab02018-04-18 14:07:03 -0600481 if (unichar == '+') {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800482 *valueToScale *= 2;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600483 this->updateAndInval();
484 return true;
485 }
486 if (unichar == '+' || unichar == '=') {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800487 *valueToScale *= 5/4.f;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600488 this->updateAndInval();
489 return true;
490 }
491 if (unichar == '-') {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800492 *valueToScale *= 4/5.f;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600493 this->updateAndInval();
494 return true;
495 }
496 if (unichar == '_') {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800497 *valueToScale *= .5f;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600498 this->updateAndInval();
499 return true;
500 }
501 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600502 if (unichar == 'D') {
503 SkDebugf(" SkPoint fPoints[4] = {\n");
Chris Dalton7f578bf2017-09-05 16:46:48 -0600504 SkDebugf(" {%ff, %ff},\n", fPoints[0].x(), fPoints[0].y());
505 SkDebugf(" {%ff, %ff},\n", fPoints[1].x(), fPoints[1].y());
506 SkDebugf(" {%ff, %ff},\n", fPoints[2].x(), fPoints[2].y());
507 SkDebugf(" {%ff, %ff}\n", fPoints[3].x(), fPoints[3].y());
Chris Dalton1a325d22017-07-14 15:17:41 -0600508 SkDebugf(" };\n");
509 return true;
510 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800511 if (unichar == 'S') {
512 fDoStroke = !fDoStroke;
513 this->updateAndInval();
514 }
Hal Canary6cc65e12019-07-03 15:53:04 -0400515 return false;
Chris Dalton1a325d22017-07-14 15:17:41 -0600516}
517
Chris Dalton383a2ef2018-01-08 17:21:41 -0500518DEF_SAMPLE(return new CCPRGeometryView;)
Chris Dalton1a325d22017-07-14 15:17:41 -0600519
Chris Dalton383a2ef2018-01-08 17:21:41 -0500520#endif // SK_SUPPORT_GPU