blob: 56e9df2b42b8a64cf7f23bc6bb58d7b7543b5c87 [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"
15#include "samplecode/Sample.h"
16#include "src/core/SkMakeUnique.h"
17#include "src/core/SkRectPriv.h"
18#include "src/gpu/GrClip.h"
19#include "src/gpu/GrContextPriv.h"
20#include "src/gpu/GrMemoryPool.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrRenderTargetContext.h"
22#include "src/gpu/GrRenderTargetContextPriv.h"
23#include "src/gpu/GrResourceProvider.h"
24#include "src/gpu/ccpr/GrCCCoverageProcessor.h"
25#include "src/gpu/ccpr/GrCCFillGeometry.h"
26#include "src/gpu/ccpr/GrCCStroker.h"
27#include "src/gpu/ccpr/GrGSCoverageProcessor.h"
28#include "src/gpu/ccpr/GrVSCoverageProcessor.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040029#include "src/gpu/geometry/GrPathUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/gl/GrGLGpu.h"
31#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
32#include "src/gpu/ops/GrDrawOp.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060033
Chris Dalton84403d72018-02-13 21:46:17 -050034using TriPointInstance = GrCCCoverageProcessor::TriPointInstance;
35using QuadPointInstance = GrCCCoverageProcessor::QuadPointInstance;
Chris Dalton8dfc70f2018-03-26 19:15:22 -060036using PrimitiveType = GrCCCoverageProcessor::PrimitiveType;
Chris Dalton1a325d22017-07-14 15:17:41 -060037
Chris Daltona640c492017-09-11 22:04:03 -070038static constexpr float kDebugBloat = 40;
39
Chris Dalton1a325d22017-07-14 15:17:41 -060040/**
41 * This sample visualizes the AA bloat geometry generated by the ccpr geometry shaders. It
42 * increases the AA bloat by 50x and outputs color instead of coverage (coverage=+1 -> green,
43 * coverage=0 -> black, coverage=-1 -> red). Use the keys 1-7 to cycle through the different
44 * geometry processors.
45 */
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040046class CCPRGeometryView : public Sample {
Hal Canaryd7639af2019-07-17 09:08:11 -040047 void onOnceBeforeDraw() override { this->updateGpuData(); }
Chris Dalton1a325d22017-07-14 15:17:41 -060048 void onDrawContent(SkCanvas*) override;
49
Hal Canaryb1f411a2019-08-29 10:39:22 -040050 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040051 bool onClick(Sample::Click*) override;
Hal Canary6cc65e12019-07-03 15:53:04 -040052 bool onChar(SkUnichar) override;
Hal Canary8a027312019-07-03 10:55:44 -040053 SkString name() override { return SkString("CCPRGeometry"); }
Chris Dalton1a325d22017-07-14 15:17:41 -060054
Chris Dalton1a325d22017-07-14 15:17:41 -060055 class Click;
Chris Dalton8d38a7f2018-03-19 14:16:44 -060056 class DrawCoverageCountOp;
57 class VisualizeCoverageCountFP;
Chris Dalton1a325d22017-07-14 15:17:41 -060058
Chris Dalton383a2ef2018-01-08 17:21:41 -050059 void updateAndInval() { this->updateGpuData(); }
Chris Dalton1a325d22017-07-14 15:17:41 -060060
61 void updateGpuData();
62
Chris Dalton8dfc70f2018-03-26 19:15:22 -060063 PrimitiveType fPrimitiveType = PrimitiveType::kTriangles;
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] = {
Chris Dalton383a2ef2018-01-08 17:21:41 -050068 {100.05f, 100.05f}, {400.75f, 100.05f}, {400.75f, 300.95f}, {100.05f, 300.95f}};
Chris Dalton1a325d22017-07-14 15:17:41 -060069
Chris Dalton9f2dab02018-04-18 14:07:03 -060070 float fConicWeight = .5;
Chris Dalton09a7bb22018-08-31 19:53:15 +080071 float fStrokeWidth = 40;
72 bool fDoStroke = false;
Chris Dalton9f2dab02018-04-18 14:07:03 -060073
Chris Dalton84403d72018-02-13 21:46:17 -050074 SkTArray<TriPointInstance> fTriPointInstances;
75 SkTArray<QuadPointInstance> fQuadPointInstances;
Chris Dalton09a7bb22018-08-31 19:53:15 +080076 SkPath fPath;
Chris Dalton1a325d22017-07-14 15:17:41 -060077};
78
Chris Dalton8d38a7f2018-03-19 14:16:44 -060079class CCPRGeometryView::DrawCoverageCountOp : public GrDrawOp {
Chris Dalton1a325d22017-07-14 15:17:41 -060080 DEFINE_OP_CLASS_ID
81
82public:
Chris Dalton8d38a7f2018-03-19 14:16:44 -060083 DrawCoverageCountOp(CCPRGeometryView* view) : INHERITED(ClassID()), fView(view) {
Chris Daltone1639692018-08-20 14:00:30 -060084 this->setBounds(SkRect::MakeIWH(fView->width(), fView->height()), GrOp::HasAABloat::kNo,
Greg Daniel5faf4742019-10-01 15:14:44 -040085 GrOp::IsHairline::kNo);
Chris Dalton1a325d22017-07-14 15:17:41 -060086 }
87
Chris Dalton8d38a7f2018-03-19 14:16:44 -060088 const char* name() const override {
89 return "[Testing/Sample code] CCPRGeometryView::DrawCoverageCountOp";
90 }
Chris Dalton1a325d22017-07-14 15:17:41 -060091
92private:
93 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Chris Dalton6ce447a2019-06-23 18:07:38 -060094 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
95 bool hasMixedSampledCoverage, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -070096 return GrProcessorSet::EmptySetAnalysis();
Chris Dalton1a325d22017-07-14 15:17:41 -060097 }
Chris Dalton1a325d22017-07-14 15:17:41 -060098 void onPrepare(GrOpFlushState*) override {}
Brian Salomon588cec72018-11-14 13:56:37 -050099 void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
Chris Dalton1a325d22017-07-14 15:17:41 -0600100
101 CCPRGeometryView* fView;
102
103 typedef GrDrawOp INHERITED;
104};
105
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600106class CCPRGeometryView::VisualizeCoverageCountFP : public GrFragmentProcessor {
107public:
108 VisualizeCoverageCountFP() : GrFragmentProcessor(kTestFP_ClassID, kNone_OptimizationFlags) {}
109
110private:
111 const char* name() const override {
112 return "[Testing/Sample code] CCPRGeometryView::VisualizeCoverageCountFP";
113 }
114 std::unique_ptr<GrFragmentProcessor> clone() const override {
115 return skstd::make_unique<VisualizeCoverageCountFP>();
116 }
117 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
118 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
119
120 class Impl : public GrGLSLFragmentProcessor {
121 void emitCode(EmitArgs& args) override {
122 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
123 f->codeAppendf("half count = %s.a;", args.fInputColor);
124 f->codeAppendf("%s = half4(clamp(-count, 0, 1), clamp(+count, 0, 1), 0, abs(count));",
125 args.fOutputColor);
126 }
127 };
128
129 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new Impl; }
130};
131
Chris Dalton7f578bf2017-09-05 16:46:48 -0600132static void draw_klm_line(int w, int h, SkCanvas* canvas, const SkScalar line[3], SkColor color) {
133 SkPoint p1, p2;
134 if (SkScalarAbs(line[1]) > SkScalarAbs(line[0])) {
135 // Draw from vertical edge to vertical edge.
136 p1 = {0, -line[2] / line[1]};
Chris Dalton383a2ef2018-01-08 17:21:41 -0500137 p2 = {(SkScalar)w, (-line[2] - w * line[0]) / line[1]};
Chris Dalton7f578bf2017-09-05 16:46:48 -0600138 } else {
139 // Draw from horizontal edge to horizontal edge.
140 p1 = {-line[2] / line[0], 0};
Chris Dalton383a2ef2018-01-08 17:21:41 -0500141 p2 = {(-line[2] - h * line[1]) / line[0], (SkScalar)h};
Chris Dalton7f578bf2017-09-05 16:46:48 -0600142 }
143
144 SkPaint linePaint;
145 linePaint.setColor(color);
146 linePaint.setAlpha(128);
147 linePaint.setStyle(SkPaint::kStroke_Style);
148 linePaint.setStrokeWidth(0);
149 linePaint.setAntiAlias(true);
150 canvas->drawLine(p1, p2, linePaint);
151}
152
Chris Dalton1a325d22017-07-14 15:17:41 -0600153void CCPRGeometryView::onDrawContent(SkCanvas* canvas) {
Chris Dalton8738cf42018-03-09 11:57:40 -0700154 canvas->clear(SK_ColorBLACK);
Chris Dalton1a325d22017-07-14 15:17:41 -0600155
Chris Dalton09a7bb22018-08-31 19:53:15 +0800156 if (!fDoStroke) {
157 SkPaint outlinePaint;
158 outlinePaint.setColor(0x80ffffff);
159 outlinePaint.setStyle(SkPaint::kStroke_Style);
160 outlinePaint.setStrokeWidth(0);
161 outlinePaint.setAntiAlias(true);
162 canvas->drawPath(fPath, outlinePaint);
Chris Dalton1a325d22017-07-14 15:17:41 -0600163 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600164
Chris Dalton7f578bf2017-09-05 16:46:48 -0600165#if 0
166 SkPaint gridPaint;
167 gridPaint.setColor(0x10000000);
168 gridPaint.setStyle(SkPaint::kStroke_Style);
169 gridPaint.setStrokeWidth(0);
170 gridPaint.setAntiAlias(true);
Chris Daltona640c492017-09-11 22:04:03 -0700171 for (int y = 0; y < this->height(); y += kDebugBloat) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600172 canvas->drawLine(0, y, this->width(), y, gridPaint);
173 }
Chris Daltona640c492017-09-11 22:04:03 -0700174 for (int x = 0; x < this->width(); x += kDebugBloat) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600175 canvas->drawLine(x, 0, x, this->height(), outlinePaint);
176 }
177#endif
178
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700179 SkString caption;
Chris Dalton383a2ef2018-01-08 17:21:41 -0500180 if (GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext()) {
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600181 // Render coverage count.
182 GrContext* ctx = canvas->getGrContext();
Chris Daltone1639692018-08-20 14:00:30 -0600183 SkASSERT(ctx);
184
Robert Phillips9da87e02019-02-04 13:26:26 -0500185 GrOpMemoryPool* pool = ctx->priv().opMemoryPool();
Ben Wagnerd4bffed2018-08-14 17:51:23 -0400186
Brian Salomonbf6b9792019-08-21 09:38:10 -0400187 auto ccbuff = ctx->priv().makeDeferredRenderTargetContext(SkBackingFit::kApprox,
188 this->width(), this->height(),
189 GrColorType::kAlpha_F16, nullptr);
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600190 SkASSERT(ccbuff);
Brian Osman9a9baae2018-11-05 15:06:26 -0500191 ccbuff->clear(nullptr, SK_PMColor4fTRANSPARENT,
192 GrRenderTargetContext::CanClearFullscreen::kYes);
Ben Wagnerd4bffed2018-08-14 17:51:23 -0400193 ccbuff->priv().testingOnly_addDrawOp(pool->allocate<DrawCoverageCountOp>(this));
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600194
195 // Visualize coverage count in main canvas.
196 GrPaint paint;
197 paint.addColorFragmentProcessor(
198 GrSimpleTextureEffect::Make(sk_ref_sp(ccbuff->asTextureProxy()), SkMatrix::I()));
199 paint.addColorFragmentProcessor(
200 skstd::make_unique<VisualizeCoverageCountFP>());
201 paint.setPorterDuffXPFactory(SkBlendMode::kSrcOver);
202 rtc->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
203 SkRect::MakeIWH(this->width(), this->height()));
204
205 // Add label.
Chris Dalton703b4762018-04-06 16:11:48 -0600206 caption.appendf("PrimitiveType_%s",
207 GrCCCoverageProcessor::PrimitiveTypeName(fPrimitiveType));
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600208 if (PrimitiveType::kCubics == fPrimitiveType) {
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700209 caption.appendf(" (%s)", SkCubicTypeName(fCubicType));
Chris Dalton9f2dab02018-04-18 14:07:03 -0600210 } else if (PrimitiveType::kConics == fPrimitiveType) {
211 caption.appendf(" (w=%f)", fConicWeight);
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700212 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800213 if (fDoStroke) {
214 caption.appendf(" (stroke_width=%f)", fStrokeWidth);
215 }
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700216 } else {
217 caption = "Use GPU backend to visualize geometry.";
Chris Dalton1a325d22017-07-14 15:17:41 -0600218 }
219
220 SkPaint pointsPaint;
221 pointsPaint.setColor(SK_ColorBLUE);
222 pointsPaint.setStrokeWidth(8);
223 pointsPaint.setAntiAlias(true);
224
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600225 if (PrimitiveType::kCubics == fPrimitiveType) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600226 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, fPoints, pointsPaint);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800227 if (!fDoStroke) {
228 int w = this->width(), h = this->height();
229 draw_klm_line(w, h, canvas, &fCubicKLM[0], SK_ColorYELLOW);
230 draw_klm_line(w, h, canvas, &fCubicKLM[3], SK_ColorBLUE);
231 draw_klm_line(w, h, canvas, &fCubicKLM[6], SK_ColorRED);
232 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600233 } else {
234 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, fPoints, pointsPaint);
235 canvas->drawPoints(SkCanvas::kPoints_PointMode, 1, fPoints + 3, pointsPaint);
236 }
237
Hal Canary4484b8f2019-01-08 14:00:08 -0500238 SkFont font(nullptr, 20);
Chris Dalton1a325d22017-07-14 15:17:41 -0600239 SkPaint captionPaint;
Chris Dalton8738cf42018-03-09 11:57:40 -0700240 captionPaint.setColor(SK_ColorWHITE);
Hal Canary4484b8f2019-01-08 14:00:08 -0500241 canvas->drawString(caption, 10, 30, font, captionPaint);
Chris Dalton1a325d22017-07-14 15:17:41 -0600242}
243
244void CCPRGeometryView::updateGpuData() {
Chris Daltone1639692018-08-20 14:00:30 -0600245 using Verb = GrCCFillGeometry::Verb;
Chris Dalton84403d72018-02-13 21:46:17 -0500246 fTriPointInstances.reset();
247 fQuadPointInstances.reset();
Chris Dalton1a325d22017-07-14 15:17:41 -0600248
Chris Dalton09a7bb22018-08-31 19:53:15 +0800249 fPath.reset();
250 fPath.moveTo(fPoints[0]);
251
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600252 if (PrimitiveType::kCubics == fPrimitiveType) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600253 double t[2], s[2];
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700254 fCubicType = GrPathUtils::getCubicKLM(fPoints, &fCubicKLM, t, s);
Chris Daltone1639692018-08-20 14:00:30 -0600255 GrCCFillGeometry geometry;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600256 geometry.beginContour(fPoints[0]);
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600257 geometry.cubicTo(fPoints, kDebugBloat / 2, kDebugBloat / 2);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600258 geometry.endContour();
Chris Dalton7f578bf2017-09-05 16:46:48 -0600259 int ptsIdx = 0;
Chris Daltone1639692018-08-20 14:00:30 -0600260 for (Verb verb : geometry.verbs()) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600261 switch (verb) {
Chris Daltone1639692018-08-20 14:00:30 -0600262 case Verb::kLineTo:
Chris Dalton7f578bf2017-09-05 16:46:48 -0600263 ++ptsIdx;
264 continue;
Chris Daltone1639692018-08-20 14:00:30 -0600265 case Verb::kMonotonicQuadraticTo:
Chris Dalton7f578bf2017-09-05 16:46:48 -0600266 ptsIdx += 2;
267 continue;
Chris Daltone1639692018-08-20 14:00:30 -0600268 case Verb::kMonotonicCubicTo:
Chris Dalton84403d72018-02-13 21:46:17 -0500269 fQuadPointInstances.push_back().set(&geometry.points()[ptsIdx], 0, 0);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600270 ptsIdx += 3;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600271 continue;
Chris Dalton383a2ef2018-01-08 17:21:41 -0500272 default:
273 continue;
Chris Dalton1a325d22017-07-14 15:17:41 -0600274 }
275 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800276 fPath.cubicTo(fPoints[1], fPoints[2], fPoints[3]);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600277 } else if (PrimitiveType::kTriangles != fPrimitiveType) {
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600278 SkPoint P3[3] = {fPoints[0], fPoints[1], fPoints[3]};
Chris Daltone1639692018-08-20 14:00:30 -0600279 GrCCFillGeometry geometry;
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600280 geometry.beginContour(P3[0]);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600281 if (PrimitiveType::kQuadratics == fPrimitiveType) {
282 geometry.quadraticTo(P3);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800283 fPath.quadTo(fPoints[1], fPoints[3]);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600284 } else {
285 SkASSERT(PrimitiveType::kConics == fPrimitiveType);
286 geometry.conicTo(P3, fConicWeight);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800287 fPath.conicTo(fPoints[1], fPoints[3], fConicWeight);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600288 }
Chris Daltonc1e59632017-09-05 00:30:07 -0600289 geometry.endContour();
Chris Dalton9f2dab02018-04-18 14:07:03 -0600290 int ptsIdx = 0, conicWeightIdx = 0;
Chris Daltone1639692018-08-20 14:00:30 -0600291 for (Verb verb : geometry.verbs()) {
292 if (Verb::kBeginContour == verb ||
293 Verb::kEndOpenContour == verb ||
294 Verb::kEndClosedContour == verb) {
Chris Daltonc1e59632017-09-05 00:30:07 -0600295 continue;
296 }
Chris Daltone1639692018-08-20 14:00:30 -0600297 if (Verb::kLineTo == verb) {
Chris Dalton90e8fb12017-12-22 02:24:53 -0700298 ++ptsIdx;
299 continue;
300 }
Chris Daltone1639692018-08-20 14:00:30 -0600301 SkASSERT(Verb::kMonotonicQuadraticTo == verb || Verb::kMonotonicConicTo == verb);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600302 if (PrimitiveType::kQuadratics == fPrimitiveType &&
Chris Daltone1639692018-08-20 14:00:30 -0600303 Verb::kMonotonicQuadraticTo == verb) {
Chris Daltonc3318f02019-07-19 14:20:53 -0600304 fTriPointInstances.push_back().set(
305 &geometry.points()[ptsIdx], Sk2f(0, 0),
306 TriPointInstance::Ordering::kXYTransposed);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600307 } else if (PrimitiveType::kConics == fPrimitiveType &&
Chris Daltone1639692018-08-20 14:00:30 -0600308 Verb::kMonotonicConicTo == verb) {
Chris Dalton9f2dab02018-04-18 14:07:03 -0600309 fQuadPointInstances.push_back().setW(&geometry.points()[ptsIdx], Sk2f(0, 0),
310 geometry.getConicWeight(conicWeightIdx++));
311 }
Chris Daltona3e92712017-12-04 11:45:51 -0700312 ptsIdx += 2;
Chris Daltonb072bb62017-08-07 09:00:46 -0600313 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600314 } else {
Chris Daltonc3318f02019-07-19 14:20:53 -0600315 fTriPointInstances.push_back().set(
316 fPoints[0], fPoints[1], fPoints[3], Sk2f(0, 0),
317 TriPointInstance::Ordering::kXYTransposed);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800318 fPath.lineTo(fPoints[1]);
319 fPath.lineTo(fPoints[3]);
320 fPath.close();
Chris Dalton1a325d22017-07-14 15:17:41 -0600321 }
322}
323
Brian Salomon588cec72018-11-14 13:56:37 -0500324void CCPRGeometryView::DrawCoverageCountOp::onExecute(GrOpFlushState* state,
325 const SkRect& chainBounds) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600326 GrResourceProvider* rp = state->resourceProvider();
327 GrContext* context = state->gpu()->getContext();
Robert Phillips4217ea72019-01-30 13:08:28 -0500328 GrGLGpu* glGpu = GrBackendApi::kOpenGL == context->backend()
Chris Dalton383a2ef2018-01-08 17:21:41 -0500329 ? static_cast<GrGLGpu*>(state->gpu())
330 : nullptr;
Chris Dalton1a325d22017-07-14 15:17:41 -0600331 if (glGpu) {
332 glGpu->handleDirtyContext();
Chris Dalton8738cf42018-03-09 11:57:40 -0700333 // GR_GL_CALL(glGpu->glInterface(), PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
Chris Dalton1a325d22017-07-14 15:17:41 -0600334 GR_GL_CALL(glGpu->glInterface(), Enable(GR_GL_LINE_SMOOTH));
335 }
336
Greg Daniel2c3398d2019-06-19 11:58:01 -0400337 GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kPlus,
Robert Phillips405413f2019-10-04 10:39:28 -0400338 state->drawOpArgs().outputSwizzle());
Chris Dalton09a7bb22018-08-31 19:53:15 +0800339
Chris Dalton2c5e0112019-03-29 13:14:18 -0500340 std::unique_ptr<GrCCCoverageProcessor> proc;
341 if (state->caps().shaderCaps()->geometryShaderSupport()) {
342 proc = skstd::make_unique<GrGSCoverageProcessor>();
343 } else {
344 proc = skstd::make_unique<GrVSCoverageProcessor>();
345 }
346
Chris Dalton09a7bb22018-08-31 19:53:15 +0800347 if (!fView->fDoStroke) {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500348 proc->reset(fView->fPrimitiveType, rp);
349 SkDEBUGCODE(proc->enableDebugBloat(kDebugBloat));
Chris Dalton09a7bb22018-08-31 19:53:15 +0800350
351 SkSTArray<1, GrMesh> mesh;
352 if (PrimitiveType::kCubics == fView->fPrimitiveType ||
353 PrimitiveType::kConics == fView->fPrimitiveType) {
Brian Salomondbf70722019-02-07 11:31:24 -0500354 sk_sp<GrGpuBuffer> instBuff(
Brian Salomonae64c192019-02-05 09:41:37 -0500355 rp->createBuffer(fView->fQuadPointInstances.count() * sizeof(QuadPointInstance),
356 GrGpuBufferType::kVertex, kDynamic_GrAccessPattern,
Brian Salomonae64c192019-02-05 09:41:37 -0500357 fView->fQuadPointInstances.begin()));
Chris Dalton09a7bb22018-08-31 19:53:15 +0800358 if (!fView->fQuadPointInstances.empty() && instBuff) {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500359 proc->appendMesh(std::move(instBuff), fView->fQuadPointInstances.count(), 0, &mesh);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800360 }
361 } else {
Brian Salomondbf70722019-02-07 11:31:24 -0500362 sk_sp<GrGpuBuffer> instBuff(
Brian Salomonae64c192019-02-05 09:41:37 -0500363 rp->createBuffer(fView->fTriPointInstances.count() * sizeof(TriPointInstance),
364 GrGpuBufferType::kVertex, kDynamic_GrAccessPattern,
Brian Salomonae64c192019-02-05 09:41:37 -0500365 fView->fTriPointInstances.begin()));
Chris Dalton09a7bb22018-08-31 19:53:15 +0800366 if (!fView->fTriPointInstances.empty() && instBuff) {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500367 proc->appendMesh(std::move(instBuff), fView->fTriPointInstances.count(), 0, &mesh);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800368 }
369 }
370
371 if (!mesh.empty()) {
372 SkASSERT(1 == mesh.count());
Chris Dalton2c5e0112019-03-29 13:14:18 -0500373 proc->draw(state, pipeline, nullptr, mesh.begin(), 1, this->bounds());
Chris Dalton09a7bb22018-08-31 19:53:15 +0800374 }
375 } else if (PrimitiveType::kConics != fView->fPrimitiveType) { // No conic stroke support yet.
376 GrCCStroker stroker(0,0,0);
377
378 SkPaint p;
379 p.setStyle(SkPaint::kStroke_Style);
380 p.setStrokeWidth(fView->fStrokeWidth);
381 p.setStrokeJoin(SkPaint::kMiter_Join);
382 p.setStrokeMiter(4);
383 // p.setStrokeCap(SkPaint::kRound_Cap);
384 stroker.parseDeviceSpaceStroke(fView->fPath, SkPathPriv::PointData(fView->fPath),
385 SkStrokeRec(p), p.getStrokeWidth(), GrScissorTest::kDisabled,
386 SkIRect::MakeWH(fView->width(), fView->height()), {0, 0});
387 GrCCStroker::BatchID batchID = stroker.closeCurrentBatch();
388
Robert Phillips9da87e02019-02-04 13:26:26 -0500389 GrOnFlushResourceProvider onFlushRP(context->priv().drawingManager());
Chris Dalton09a7bb22018-08-31 19:53:15 +0800390 stroker.prepareToDraw(&onFlushRP);
391
392 SkIRect ibounds;
393 this->bounds().roundOut(&ibounds);
Chris Dalton2c5e0112019-03-29 13:14:18 -0500394 stroker.drawStrokes(state, proc.get(), batchID, ibounds);
Chris Dalton23261772017-12-10 16:41:45 -0700395 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600396
397 if (glGpu) {
398 context->resetContext(kMisc_GrGLBackendState);
399 }
400}
401
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400402class CCPRGeometryView::Click : public Sample::Click {
Chris Dalton1a325d22017-07-14 15:17:41 -0600403public:
Hal Canaryfcf63592019-07-12 11:32:43 -0400404 Click(int ptIdx) : fPtIdx(ptIdx) {}
Chris Dalton1a325d22017-07-14 15:17:41 -0600405
406 void doClick(SkPoint points[]) {
407 if (fPtIdx >= 0) {
Hal Canaryfcf63592019-07-12 11:32:43 -0400408 points[fPtIdx] += fCurr - fPrev;
Chris Dalton1a325d22017-07-14 15:17:41 -0600409 } else {
410 for (int i = 0; i < 4; ++i) {
Hal Canaryfcf63592019-07-12 11:32:43 -0400411 points[i] += fCurr - fPrev;
Chris Dalton1a325d22017-07-14 15:17:41 -0600412 }
413 }
414 }
415
416private:
Chris Dalton1a325d22017-07-14 15:17:41 -0600417 int fPtIdx;
418};
419
Hal Canaryb1f411a2019-08-29 10:39:22 -0400420Sample::Click* CCPRGeometryView::onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600421 for (int i = 0; i < 4; ++i) {
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600422 if (PrimitiveType::kCubics != fPrimitiveType && 2 == i) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600423 continue;
424 }
425 if (fabs(x - fPoints[i].x()) < 20 && fabsf(y - fPoints[i].y()) < 20) {
Hal Canaryfcf63592019-07-12 11:32:43 -0400426 return new Click(i);
Chris Dalton1a325d22017-07-14 15:17:41 -0600427 }
428 }
Hal Canaryfcf63592019-07-12 11:32:43 -0400429 return new Click(-1);
Chris Dalton1a325d22017-07-14 15:17:41 -0600430}
431
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400432bool CCPRGeometryView::onClick(Sample::Click* click) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500433 Click* myClick = (Click*)click;
Chris Dalton1a325d22017-07-14 15:17:41 -0600434 myClick->doClick(fPoints);
435 this->updateAndInval();
436 return true;
437}
438
Hal Canary6cc65e12019-07-03 15:53:04 -0400439bool CCPRGeometryView::onChar(SkUnichar unichar) {
Chris Dalton9f2dab02018-04-18 14:07:03 -0600440 if (unichar >= '1' && unichar <= '4') {
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600441 fPrimitiveType = PrimitiveType(unichar - '1');
Chris Dalton703b4762018-04-06 16:11:48 -0600442 if (fPrimitiveType >= PrimitiveType::kWeightedTriangles) {
443 fPrimitiveType = (PrimitiveType) ((int)fPrimitiveType + 1);
444 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600445 this->updateAndInval();
446 return true;
447 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800448 float* valueToScale = nullptr;
449 if (fDoStroke) {
450 valueToScale = &fStrokeWidth;
451 } else if (PrimitiveType::kConics == fPrimitiveType) {
452 valueToScale = &fConicWeight;
453 }
454 if (valueToScale) {
Chris Dalton9f2dab02018-04-18 14:07:03 -0600455 if (unichar == '+') {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800456 *valueToScale *= 2;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600457 this->updateAndInval();
458 return true;
459 }
460 if (unichar == '+' || unichar == '=') {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800461 *valueToScale *= 5/4.f;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600462 this->updateAndInval();
463 return true;
464 }
465 if (unichar == '-') {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800466 *valueToScale *= 4/5.f;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600467 this->updateAndInval();
468 return true;
469 }
470 if (unichar == '_') {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800471 *valueToScale *= .5f;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600472 this->updateAndInval();
473 return true;
474 }
475 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600476 if (unichar == 'D') {
477 SkDebugf(" SkPoint fPoints[4] = {\n");
Chris Dalton7f578bf2017-09-05 16:46:48 -0600478 SkDebugf(" {%ff, %ff},\n", fPoints[0].x(), fPoints[0].y());
479 SkDebugf(" {%ff, %ff},\n", fPoints[1].x(), fPoints[1].y());
480 SkDebugf(" {%ff, %ff},\n", fPoints[2].x(), fPoints[2].y());
481 SkDebugf(" {%ff, %ff}\n", fPoints[3].x(), fPoints[3].y());
Chris Dalton1a325d22017-07-14 15:17:41 -0600482 SkDebugf(" };\n");
483 return true;
484 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800485 if (unichar == 'S') {
486 fDoStroke = !fDoStroke;
487 this->updateAndInval();
488 }
Hal Canary6cc65e12019-07-03 15:53:04 -0400489 return false;
Chris Dalton1a325d22017-07-14 15:17:41 -0600490}
491
Chris Dalton383a2ef2018-01-08 17:21:41 -0500492DEF_SAMPLE(return new CCPRGeometryView;)
Chris Dalton1a325d22017-07-14 15:17:41 -0600493
Chris Dalton383a2ef2018-01-08 17:21:41 -0500494#endif // SK_SUPPORT_GPU