blob: 1ec7c1a759d4790878fbd599f5c1c6c77f97e84d [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"
21#include "src/gpu/GrPathUtils.h"
22#include "src/gpu/GrRenderTargetContext.h"
23#include "src/gpu/GrRenderTargetContextPriv.h"
24#include "src/gpu/GrResourceProvider.h"
25#include "src/gpu/ccpr/GrCCCoverageProcessor.h"
26#include "src/gpu/ccpr/GrCCFillGeometry.h"
27#include "src/gpu/ccpr/GrCCStroker.h"
28#include "src/gpu/ccpr/GrGSCoverageProcessor.h"
29#include "src/gpu/ccpr/GrVSCoverageProcessor.h"
30#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 {
Chris Dalton1a325d22017-07-14 15:17:41 -060047public:
48 CCPRGeometryView() { this->updateGpuData(); }
49 void onDrawContent(SkCanvas*) override;
50
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040051 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override;
52 bool onClick(Sample::Click*) override;
53 bool onQuery(Sample::Event* evt) override;
Chris Dalton1a325d22017-07-14 15:17:41 -060054
55private:
56 class Click;
Chris Dalton8d38a7f2018-03-19 14:16:44 -060057 class DrawCoverageCountOp;
58 class VisualizeCoverageCountFP;
Chris Dalton1a325d22017-07-14 15:17:41 -060059
Chris Dalton383a2ef2018-01-08 17:21:41 -050060 void updateAndInval() { this->updateGpuData(); }
Chris Dalton1a325d22017-07-14 15:17:41 -060061
62 void updateGpuData();
63
Chris Dalton8dfc70f2018-03-26 19:15:22 -060064 PrimitiveType fPrimitiveType = PrimitiveType::kTriangles;
Chris Daltonbe4ffab2017-12-08 10:59:58 -070065 SkCubicType fCubicType;
Chris Dalton7f578bf2017-09-05 16:46:48 -060066 SkMatrix fCubicKLM;
Chris Dalton1a325d22017-07-14 15:17:41 -060067
68 SkPoint fPoints[4] = {
Chris Dalton383a2ef2018-01-08 17:21:41 -050069 {100.05f, 100.05f}, {400.75f, 100.05f}, {400.75f, 300.95f}, {100.05f, 300.95f}};
Chris Dalton1a325d22017-07-14 15:17:41 -060070
Chris Dalton9f2dab02018-04-18 14:07:03 -060071 float fConicWeight = .5;
Chris Dalton09a7bb22018-08-31 19:53:15 +080072 float fStrokeWidth = 40;
73 bool fDoStroke = false;
Chris Dalton9f2dab02018-04-18 14:07:03 -060074
Chris Dalton84403d72018-02-13 21:46:17 -050075 SkTArray<TriPointInstance> fTriPointInstances;
76 SkTArray<QuadPointInstance> fQuadPointInstances;
Chris Dalton09a7bb22018-08-31 19:53:15 +080077 SkPath fPath;
Chris Dalton1a325d22017-07-14 15:17:41 -060078
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040079 typedef Sample INHERITED;
Chris Dalton1a325d22017-07-14 15:17:41 -060080};
81
Chris Dalton8d38a7f2018-03-19 14:16:44 -060082class CCPRGeometryView::DrawCoverageCountOp : public GrDrawOp {
Chris Dalton1a325d22017-07-14 15:17:41 -060083 DEFINE_OP_CLASS_ID
84
85public:
Chris Dalton8d38a7f2018-03-19 14:16:44 -060086 DrawCoverageCountOp(CCPRGeometryView* view) : INHERITED(ClassID()), fView(view) {
Chris Daltone1639692018-08-20 14:00:30 -060087 this->setBounds(SkRect::MakeIWH(fView->width(), fView->height()), GrOp::HasAABloat::kNo,
88 GrOp::IsZeroArea::kNo);
Chris Dalton1a325d22017-07-14 15:17:41 -060089 }
90
Chris Dalton8d38a7f2018-03-19 14:16:44 -060091 const char* name() const override {
92 return "[Testing/Sample code] CCPRGeometryView::DrawCoverageCountOp";
93 }
Chris Dalton1a325d22017-07-14 15:17:41 -060094
95private:
96 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Brian Osman5ced0bf2019-03-15 10:15:29 -040097 GrProcessorSet::Analysis finalize(
98 const GrCaps&, const GrAppliedClip*, GrFSAAType, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -070099 return GrProcessorSet::EmptySetAnalysis();
Chris Dalton1a325d22017-07-14 15:17:41 -0600100 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600101 void onPrepare(GrOpFlushState*) override {}
Brian Salomon588cec72018-11-14 13:56:37 -0500102 void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
Chris Dalton1a325d22017-07-14 15:17:41 -0600103
104 CCPRGeometryView* fView;
105
106 typedef GrDrawOp INHERITED;
107};
108
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600109class CCPRGeometryView::VisualizeCoverageCountFP : public GrFragmentProcessor {
110public:
111 VisualizeCoverageCountFP() : GrFragmentProcessor(kTestFP_ClassID, kNone_OptimizationFlags) {}
112
113private:
114 const char* name() const override {
115 return "[Testing/Sample code] CCPRGeometryView::VisualizeCoverageCountFP";
116 }
117 std::unique_ptr<GrFragmentProcessor> clone() const override {
118 return skstd::make_unique<VisualizeCoverageCountFP>();
119 }
120 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
121 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
122
123 class Impl : public GrGLSLFragmentProcessor {
124 void emitCode(EmitArgs& args) override {
125 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
126 f->codeAppendf("half count = %s.a;", args.fInputColor);
127 f->codeAppendf("%s = half4(clamp(-count, 0, 1), clamp(+count, 0, 1), 0, abs(count));",
128 args.fOutputColor);
129 }
130 };
131
132 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new Impl; }
133};
134
Chris Dalton7f578bf2017-09-05 16:46:48 -0600135static void draw_klm_line(int w, int h, SkCanvas* canvas, const SkScalar line[3], SkColor color) {
136 SkPoint p1, p2;
137 if (SkScalarAbs(line[1]) > SkScalarAbs(line[0])) {
138 // Draw from vertical edge to vertical edge.
139 p1 = {0, -line[2] / line[1]};
Chris Dalton383a2ef2018-01-08 17:21:41 -0500140 p2 = {(SkScalar)w, (-line[2] - w * line[0]) / line[1]};
Chris Dalton7f578bf2017-09-05 16:46:48 -0600141 } else {
142 // Draw from horizontal edge to horizontal edge.
143 p1 = {-line[2] / line[0], 0};
Chris Dalton383a2ef2018-01-08 17:21:41 -0500144 p2 = {(-line[2] - h * line[1]) / line[0], (SkScalar)h};
Chris Dalton7f578bf2017-09-05 16:46:48 -0600145 }
146
147 SkPaint linePaint;
148 linePaint.setColor(color);
149 linePaint.setAlpha(128);
150 linePaint.setStyle(SkPaint::kStroke_Style);
151 linePaint.setStrokeWidth(0);
152 linePaint.setAntiAlias(true);
153 canvas->drawLine(p1, p2, linePaint);
154}
155
Chris Dalton1a325d22017-07-14 15:17:41 -0600156void CCPRGeometryView::onDrawContent(SkCanvas* canvas) {
Chris Dalton8738cf42018-03-09 11:57:40 -0700157 canvas->clear(SK_ColorBLACK);
Chris Dalton1a325d22017-07-14 15:17:41 -0600158
Chris Dalton09a7bb22018-08-31 19:53:15 +0800159 if (!fDoStroke) {
160 SkPaint outlinePaint;
161 outlinePaint.setColor(0x80ffffff);
162 outlinePaint.setStyle(SkPaint::kStroke_Style);
163 outlinePaint.setStrokeWidth(0);
164 outlinePaint.setAntiAlias(true);
165 canvas->drawPath(fPath, outlinePaint);
Chris Dalton1a325d22017-07-14 15:17:41 -0600166 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600167
Chris Dalton7f578bf2017-09-05 16:46:48 -0600168#if 0
169 SkPaint gridPaint;
170 gridPaint.setColor(0x10000000);
171 gridPaint.setStyle(SkPaint::kStroke_Style);
172 gridPaint.setStrokeWidth(0);
173 gridPaint.setAntiAlias(true);
Chris Daltona640c492017-09-11 22:04:03 -0700174 for (int y = 0; y < this->height(); y += kDebugBloat) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600175 canvas->drawLine(0, y, this->width(), y, gridPaint);
176 }
Chris Daltona640c492017-09-11 22:04:03 -0700177 for (int x = 0; x < this->width(); x += kDebugBloat) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600178 canvas->drawLine(x, 0, x, this->height(), outlinePaint);
179 }
180#endif
181
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700182 SkString caption;
Chris Dalton383a2ef2018-01-08 17:21:41 -0500183 if (GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext()) {
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600184 // Render coverage count.
185 GrContext* ctx = canvas->getGrContext();
Chris Daltone1639692018-08-20 14:00:30 -0600186 SkASSERT(ctx);
187
Robert Phillips9da87e02019-02-04 13:26:26 -0500188 GrOpMemoryPool* pool = ctx->priv().opMemoryPool();
Ben Wagnerd4bffed2018-08-14 17:51:23 -0400189
Greg Daniel4065d452018-11-16 15:43:41 -0500190 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500191 ctx->priv().caps()->getBackendFormatFromGrColorType(GrColorType::kAlpha_F16,
Greg Daniel4065d452018-11-16 15:43:41 -0500192 GrSRGBEncoded::kNo);
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600193 sk_sp<GrRenderTargetContext> ccbuff =
Robert Phillips9da87e02019-02-04 13:26:26 -0500194 ctx->priv().makeDeferredRenderTargetContext(format, SkBackingFit::kApprox,
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600195 this->width(), this->height(),
196 kAlpha_half_GrPixelConfig,
197 nullptr);
198 SkASSERT(ccbuff);
Brian Osman9a9baae2018-11-05 15:06:26 -0500199 ccbuff->clear(nullptr, SK_PMColor4fTRANSPARENT,
200 GrRenderTargetContext::CanClearFullscreen::kYes);
Ben Wagnerd4bffed2018-08-14 17:51:23 -0400201 ccbuff->priv().testingOnly_addDrawOp(pool->allocate<DrawCoverageCountOp>(this));
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600202
203 // Visualize coverage count in main canvas.
204 GrPaint paint;
205 paint.addColorFragmentProcessor(
206 GrSimpleTextureEffect::Make(sk_ref_sp(ccbuff->asTextureProxy()), SkMatrix::I()));
207 paint.addColorFragmentProcessor(
208 skstd::make_unique<VisualizeCoverageCountFP>());
209 paint.setPorterDuffXPFactory(SkBlendMode::kSrcOver);
210 rtc->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
211 SkRect::MakeIWH(this->width(), this->height()));
212
213 // Add label.
Chris Dalton703b4762018-04-06 16:11:48 -0600214 caption.appendf("PrimitiveType_%s",
215 GrCCCoverageProcessor::PrimitiveTypeName(fPrimitiveType));
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600216 if (PrimitiveType::kCubics == fPrimitiveType) {
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700217 caption.appendf(" (%s)", SkCubicTypeName(fCubicType));
Chris Dalton9f2dab02018-04-18 14:07:03 -0600218 } else if (PrimitiveType::kConics == fPrimitiveType) {
219 caption.appendf(" (w=%f)", fConicWeight);
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700220 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800221 if (fDoStroke) {
222 caption.appendf(" (stroke_width=%f)", fStrokeWidth);
223 }
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700224 } else {
225 caption = "Use GPU backend to visualize geometry.";
Chris Dalton1a325d22017-07-14 15:17:41 -0600226 }
227
228 SkPaint pointsPaint;
229 pointsPaint.setColor(SK_ColorBLUE);
230 pointsPaint.setStrokeWidth(8);
231 pointsPaint.setAntiAlias(true);
232
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600233 if (PrimitiveType::kCubics == fPrimitiveType) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600234 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, fPoints, pointsPaint);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800235 if (!fDoStroke) {
236 int w = this->width(), h = this->height();
237 draw_klm_line(w, h, canvas, &fCubicKLM[0], SK_ColorYELLOW);
238 draw_klm_line(w, h, canvas, &fCubicKLM[3], SK_ColorBLUE);
239 draw_klm_line(w, h, canvas, &fCubicKLM[6], SK_ColorRED);
240 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600241 } else {
242 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, fPoints, pointsPaint);
243 canvas->drawPoints(SkCanvas::kPoints_PointMode, 1, fPoints + 3, pointsPaint);
244 }
245
Hal Canary4484b8f2019-01-08 14:00:08 -0500246 SkFont font(nullptr, 20);
Chris Dalton1a325d22017-07-14 15:17:41 -0600247 SkPaint captionPaint;
Chris Dalton8738cf42018-03-09 11:57:40 -0700248 captionPaint.setColor(SK_ColorWHITE);
Hal Canary4484b8f2019-01-08 14:00:08 -0500249 canvas->drawString(caption, 10, 30, font, captionPaint);
Chris Dalton1a325d22017-07-14 15:17:41 -0600250}
251
252void CCPRGeometryView::updateGpuData() {
Chris Daltone1639692018-08-20 14:00:30 -0600253 using Verb = GrCCFillGeometry::Verb;
Chris Dalton84403d72018-02-13 21:46:17 -0500254 fTriPointInstances.reset();
255 fQuadPointInstances.reset();
Chris Dalton1a325d22017-07-14 15:17:41 -0600256
Chris Dalton09a7bb22018-08-31 19:53:15 +0800257 fPath.reset();
258 fPath.moveTo(fPoints[0]);
259
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600260 if (PrimitiveType::kCubics == fPrimitiveType) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600261 double t[2], s[2];
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700262 fCubicType = GrPathUtils::getCubicKLM(fPoints, &fCubicKLM, t, s);
Chris Daltone1639692018-08-20 14:00:30 -0600263 GrCCFillGeometry geometry;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600264 geometry.beginContour(fPoints[0]);
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600265 geometry.cubicTo(fPoints, kDebugBloat / 2, kDebugBloat / 2);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600266 geometry.endContour();
Chris Dalton7f578bf2017-09-05 16:46:48 -0600267 int ptsIdx = 0;
Chris Daltone1639692018-08-20 14:00:30 -0600268 for (Verb verb : geometry.verbs()) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600269 switch (verb) {
Chris Daltone1639692018-08-20 14:00:30 -0600270 case Verb::kLineTo:
Chris Dalton7f578bf2017-09-05 16:46:48 -0600271 ++ptsIdx;
272 continue;
Chris Daltone1639692018-08-20 14:00:30 -0600273 case Verb::kMonotonicQuadraticTo:
Chris Dalton7f578bf2017-09-05 16:46:48 -0600274 ptsIdx += 2;
275 continue;
Chris Daltone1639692018-08-20 14:00:30 -0600276 case Verb::kMonotonicCubicTo:
Chris Dalton84403d72018-02-13 21:46:17 -0500277 fQuadPointInstances.push_back().set(&geometry.points()[ptsIdx], 0, 0);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600278 ptsIdx += 3;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600279 continue;
Chris Dalton383a2ef2018-01-08 17:21:41 -0500280 default:
281 continue;
Chris Dalton1a325d22017-07-14 15:17:41 -0600282 }
283 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800284 fPath.cubicTo(fPoints[1], fPoints[2], fPoints[3]);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600285 } else if (PrimitiveType::kTriangles != fPrimitiveType) {
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600286 SkPoint P3[3] = {fPoints[0], fPoints[1], fPoints[3]};
Chris Daltone1639692018-08-20 14:00:30 -0600287 GrCCFillGeometry geometry;
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600288 geometry.beginContour(P3[0]);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600289 if (PrimitiveType::kQuadratics == fPrimitiveType) {
290 geometry.quadraticTo(P3);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800291 fPath.quadTo(fPoints[1], fPoints[3]);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600292 } else {
293 SkASSERT(PrimitiveType::kConics == fPrimitiveType);
294 geometry.conicTo(P3, fConicWeight);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800295 fPath.conicTo(fPoints[1], fPoints[3], fConicWeight);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600296 }
Chris Daltonc1e59632017-09-05 00:30:07 -0600297 geometry.endContour();
Chris Dalton9f2dab02018-04-18 14:07:03 -0600298 int ptsIdx = 0, conicWeightIdx = 0;
Chris Daltone1639692018-08-20 14:00:30 -0600299 for (Verb verb : geometry.verbs()) {
300 if (Verb::kBeginContour == verb ||
301 Verb::kEndOpenContour == verb ||
302 Verb::kEndClosedContour == verb) {
Chris Daltonc1e59632017-09-05 00:30:07 -0600303 continue;
304 }
Chris Daltone1639692018-08-20 14:00:30 -0600305 if (Verb::kLineTo == verb) {
Chris Dalton90e8fb12017-12-22 02:24:53 -0700306 ++ptsIdx;
307 continue;
308 }
Chris Daltone1639692018-08-20 14:00:30 -0600309 SkASSERT(Verb::kMonotonicQuadraticTo == verb || Verb::kMonotonicConicTo == verb);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600310 if (PrimitiveType::kQuadratics == fPrimitiveType &&
Chris Daltone1639692018-08-20 14:00:30 -0600311 Verb::kMonotonicQuadraticTo == verb) {
Chris Dalton9f2dab02018-04-18 14:07:03 -0600312 fTriPointInstances.push_back().set(&geometry.points()[ptsIdx], Sk2f(0, 0));
313 } else if (PrimitiveType::kConics == fPrimitiveType &&
Chris Daltone1639692018-08-20 14:00:30 -0600314 Verb::kMonotonicConicTo == verb) {
Chris Dalton9f2dab02018-04-18 14:07:03 -0600315 fQuadPointInstances.push_back().setW(&geometry.points()[ptsIdx], Sk2f(0, 0),
316 geometry.getConicWeight(conicWeightIdx++));
317 }
Chris Daltona3e92712017-12-04 11:45:51 -0700318 ptsIdx += 2;
Chris Daltonb072bb62017-08-07 09:00:46 -0600319 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600320 } else {
Chris Dalton84403d72018-02-13 21:46:17 -0500321 fTriPointInstances.push_back().set(fPoints[0], fPoints[1], fPoints[3], Sk2f(0, 0));
Chris Dalton09a7bb22018-08-31 19:53:15 +0800322 fPath.lineTo(fPoints[1]);
323 fPath.lineTo(fPoints[3]);
324 fPath.close();
Chris Dalton1a325d22017-07-14 15:17:41 -0600325 }
326}
327
Brian Salomon588cec72018-11-14 13:56:37 -0500328void CCPRGeometryView::DrawCoverageCountOp::onExecute(GrOpFlushState* state,
329 const SkRect& chainBounds) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600330 GrResourceProvider* rp = state->resourceProvider();
331 GrContext* context = state->gpu()->getContext();
Robert Phillips4217ea72019-01-30 13:08:28 -0500332 GrGLGpu* glGpu = GrBackendApi::kOpenGL == context->backend()
Chris Dalton383a2ef2018-01-08 17:21:41 -0500333 ? static_cast<GrGLGpu*>(state->gpu())
334 : nullptr;
Chris Dalton1a325d22017-07-14 15:17:41 -0600335 if (glGpu) {
336 glGpu->handleDirtyContext();
Chris Dalton8738cf42018-03-09 11:57:40 -0700337 // GR_GL_CALL(glGpu->glInterface(), PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
Chris Dalton1a325d22017-07-14 15:17:41 -0600338 GR_GL_CALL(glGpu->glInterface(), Enable(GR_GL_LINE_SMOOTH));
339 }
340
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500341 GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kPlus);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800342
Chris Dalton2c5e0112019-03-29 13:14:18 -0500343 std::unique_ptr<GrCCCoverageProcessor> proc;
344 if (state->caps().shaderCaps()->geometryShaderSupport()) {
345 proc = skstd::make_unique<GrGSCoverageProcessor>();
346 } else {
347 proc = skstd::make_unique<GrVSCoverageProcessor>();
348 }
349
Chris Dalton09a7bb22018-08-31 19:53:15 +0800350 if (!fView->fDoStroke) {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500351 proc->reset(fView->fPrimitiveType, rp);
352 SkDEBUGCODE(proc->enableDebugBloat(kDebugBloat));
Chris Dalton09a7bb22018-08-31 19:53:15 +0800353
354 SkSTArray<1, GrMesh> mesh;
355 if (PrimitiveType::kCubics == fView->fPrimitiveType ||
356 PrimitiveType::kConics == fView->fPrimitiveType) {
Brian Salomondbf70722019-02-07 11:31:24 -0500357 sk_sp<GrGpuBuffer> instBuff(
Brian Salomonae64c192019-02-05 09:41:37 -0500358 rp->createBuffer(fView->fQuadPointInstances.count() * sizeof(QuadPointInstance),
359 GrGpuBufferType::kVertex, kDynamic_GrAccessPattern,
Brian Salomonae64c192019-02-05 09:41:37 -0500360 fView->fQuadPointInstances.begin()));
Chris Dalton09a7bb22018-08-31 19:53:15 +0800361 if (!fView->fQuadPointInstances.empty() && instBuff) {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500362 proc->appendMesh(std::move(instBuff), fView->fQuadPointInstances.count(), 0, &mesh);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800363 }
364 } else {
Brian Salomondbf70722019-02-07 11:31:24 -0500365 sk_sp<GrGpuBuffer> instBuff(
Brian Salomonae64c192019-02-05 09:41:37 -0500366 rp->createBuffer(fView->fTriPointInstances.count() * sizeof(TriPointInstance),
367 GrGpuBufferType::kVertex, kDynamic_GrAccessPattern,
Brian Salomonae64c192019-02-05 09:41:37 -0500368 fView->fTriPointInstances.begin()));
Chris Dalton09a7bb22018-08-31 19:53:15 +0800369 if (!fView->fTriPointInstances.empty() && instBuff) {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500370 proc->appendMesh(std::move(instBuff), fView->fTriPointInstances.count(), 0, &mesh);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800371 }
372 }
373
374 if (!mesh.empty()) {
375 SkASSERT(1 == mesh.count());
Chris Dalton2c5e0112019-03-29 13:14:18 -0500376 proc->draw(state, pipeline, nullptr, mesh.begin(), 1, this->bounds());
Chris Dalton09a7bb22018-08-31 19:53:15 +0800377 }
378 } else if (PrimitiveType::kConics != fView->fPrimitiveType) { // No conic stroke support yet.
379 GrCCStroker stroker(0,0,0);
380
381 SkPaint p;
382 p.setStyle(SkPaint::kStroke_Style);
383 p.setStrokeWidth(fView->fStrokeWidth);
384 p.setStrokeJoin(SkPaint::kMiter_Join);
385 p.setStrokeMiter(4);
386 // p.setStrokeCap(SkPaint::kRound_Cap);
387 stroker.parseDeviceSpaceStroke(fView->fPath, SkPathPriv::PointData(fView->fPath),
388 SkStrokeRec(p), p.getStrokeWidth(), GrScissorTest::kDisabled,
389 SkIRect::MakeWH(fView->width(), fView->height()), {0, 0});
390 GrCCStroker::BatchID batchID = stroker.closeCurrentBatch();
391
Robert Phillips9da87e02019-02-04 13:26:26 -0500392 GrOnFlushResourceProvider onFlushRP(context->priv().drawingManager());
Chris Dalton09a7bb22018-08-31 19:53:15 +0800393 stroker.prepareToDraw(&onFlushRP);
394
395 SkIRect ibounds;
396 this->bounds().roundOut(&ibounds);
Chris Dalton2c5e0112019-03-29 13:14:18 -0500397 stroker.drawStrokes(state, proc.get(), batchID, ibounds);
Chris Dalton23261772017-12-10 16:41:45 -0700398 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600399
400 if (glGpu) {
401 context->resetContext(kMisc_GrGLBackendState);
402 }
403}
404
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400405class CCPRGeometryView::Click : public Sample::Click {
Chris Dalton1a325d22017-07-14 15:17:41 -0600406public:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400407 Click(Sample* target, int ptIdx) : Sample::Click(target), fPtIdx(ptIdx) {}
Chris Dalton1a325d22017-07-14 15:17:41 -0600408
409 void doClick(SkPoint points[]) {
410 if (fPtIdx >= 0) {
411 this->dragPoint(points, fPtIdx);
412 } else {
413 for (int i = 0; i < 4; ++i) {
414 this->dragPoint(points, i);
415 }
416 }
417 }
418
419private:
Chris Dalton383a2ef2018-01-08 17:21:41 -0500420 void dragPoint(SkPoint points[], int idx) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600421 SkIPoint delta = fICurr - fIPrev;
422 points[idx] += SkPoint::Make(delta.x(), delta.y());
423 }
424
425 int fPtIdx;
426};
427
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400428Sample::Click* CCPRGeometryView::onFindClickHandler(SkScalar x, SkScalar y, unsigned) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600429 for (int i = 0; i < 4; ++i) {
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600430 if (PrimitiveType::kCubics != fPrimitiveType && 2 == i) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600431 continue;
432 }
433 if (fabs(x - fPoints[i].x()) < 20 && fabsf(y - fPoints[i].y()) < 20) {
434 return new Click(this, i);
435 }
436 }
437 return new Click(this, -1);
438}
439
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400440bool CCPRGeometryView::onClick(Sample::Click* click) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500441 Click* myClick = (Click*)click;
Chris Dalton1a325d22017-07-14 15:17:41 -0600442 myClick->doClick(fPoints);
443 this->updateAndInval();
444 return true;
445}
446
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400447bool CCPRGeometryView::onQuery(Sample::Event* evt) {
448 if (Sample::TitleQ(*evt)) {
449 Sample::TitleR(evt, "CCPRGeometry");
Chris Dalton1a325d22017-07-14 15:17:41 -0600450 return true;
451 }
452 SkUnichar unichar;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400453 if (Sample::CharQ(*evt, &unichar)) {
Chris Dalton9f2dab02018-04-18 14:07:03 -0600454 if (unichar >= '1' && unichar <= '4') {
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600455 fPrimitiveType = PrimitiveType(unichar - '1');
Chris Dalton703b4762018-04-06 16:11:48 -0600456 if (fPrimitiveType >= PrimitiveType::kWeightedTriangles) {
457 fPrimitiveType = (PrimitiveType) ((int)fPrimitiveType + 1);
458 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600459 this->updateAndInval();
460 return true;
461 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800462 float* valueToScale = nullptr;
463 if (fDoStroke) {
464 valueToScale = &fStrokeWidth;
465 } else if (PrimitiveType::kConics == fPrimitiveType) {
466 valueToScale = &fConicWeight;
467 }
468 if (valueToScale) {
Chris Dalton9f2dab02018-04-18 14:07:03 -0600469 if (unichar == '+') {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800470 *valueToScale *= 2;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600471 this->updateAndInval();
472 return true;
473 }
474 if (unichar == '+' || unichar == '=') {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800475 *valueToScale *= 5/4.f;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600476 this->updateAndInval();
477 return true;
478 }
479 if (unichar == '-') {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800480 *valueToScale *= 4/5.f;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600481 this->updateAndInval();
482 return true;
483 }
484 if (unichar == '_') {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800485 *valueToScale *= .5f;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600486 this->updateAndInval();
487 return true;
488 }
489 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600490 if (unichar == 'D') {
491 SkDebugf(" SkPoint fPoints[4] = {\n");
Chris Dalton7f578bf2017-09-05 16:46:48 -0600492 SkDebugf(" {%ff, %ff},\n", fPoints[0].x(), fPoints[0].y());
493 SkDebugf(" {%ff, %ff},\n", fPoints[1].x(), fPoints[1].y());
494 SkDebugf(" {%ff, %ff},\n", fPoints[2].x(), fPoints[2].y());
495 SkDebugf(" {%ff, %ff}\n", fPoints[3].x(), fPoints[3].y());
Chris Dalton1a325d22017-07-14 15:17:41 -0600496 SkDebugf(" };\n");
497 return true;
498 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800499 if (unichar == 'S') {
500 fDoStroke = !fDoStroke;
501 this->updateAndInval();
502 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600503 }
504 return this->INHERITED::onQuery(evt);
505}
506
Chris Dalton383a2ef2018-01-08 17:21:41 -0500507DEF_SAMPLE(return new CCPRGeometryView;)
Chris Dalton1a325d22017-07-14 15:17:41 -0600508
Chris Dalton383a2ef2018-01-08 17:21:41 -0500509#endif // SK_SUPPORT_GPU