blob: c59d54f394b8a6ce75912ecd8616b27dbe7081a4 [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
Ben Wagner80634eb2019-02-07 14:10:42 -050012#include "GrClip.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060013#include "GrContextPriv.h"
Ben Wagnerd4bffed2018-08-14 17:51:23 -040014#include "GrMemoryPool.h"
Chris Dalton7f578bf2017-09-05 16:46:48 -060015#include "GrPathUtils.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060016#include "GrRenderTargetContext.h"
17#include "GrRenderTargetContextPriv.h"
18#include "GrResourceProvider.h"
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040019#include "Sample.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060020#include "SkCanvas.h"
Ben Wagner80634eb2019-02-07 14:10:42 -050021#include "SkMakeUnique.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060022#include "SkPaint.h"
23#include "SkPath.h"
Mike Reed274218e2018-01-08 15:05:02 -050024#include "SkRectPriv.h"
Chris Dalton383a2ef2018-01-08 17:21:41 -050025#include "ccpr/GrCCCoverageProcessor.h"
Chris Daltone1639692018-08-20 14:00:30 -060026#include "ccpr/GrCCFillGeometry.h"
Chris Dalton09a7bb22018-08-31 19:53:15 +080027#include "ccpr/GrCCStroker.h"
Ben Wagner80634eb2019-02-07 14:10:42 -050028#include "gl/GrGLGpu.h"
Brian Salomon1471df92018-06-08 10:49:00 -040029#include "glsl/GrGLSLFragmentProcessor.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060030#include "ops/GrDrawOp.h"
31
Chris Dalton84403d72018-02-13 21:46:17 -050032using TriPointInstance = GrCCCoverageProcessor::TriPointInstance;
33using QuadPointInstance = GrCCCoverageProcessor::QuadPointInstance;
Chris Dalton8dfc70f2018-03-26 19:15:22 -060034using PrimitiveType = GrCCCoverageProcessor::PrimitiveType;
Chris Dalton1a325d22017-07-14 15:17:41 -060035
Chris Daltona640c492017-09-11 22:04:03 -070036static constexpr float kDebugBloat = 40;
37
Chris Dalton1a325d22017-07-14 15:17:41 -060038/**
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 */
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040044class CCPRGeometryView : public Sample {
Chris Dalton1a325d22017-07-14 15:17:41 -060045public:
46 CCPRGeometryView() { this->updateGpuData(); }
47 void onDrawContent(SkCanvas*) override;
48
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040049 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override;
50 bool onClick(Sample::Click*) override;
51 bool onQuery(Sample::Event* evt) override;
Chris Dalton1a325d22017-07-14 15:17:41 -060052
53private:
54 class Click;
Chris Dalton8d38a7f2018-03-19 14:16:44 -060055 class DrawCoverageCountOp;
56 class VisualizeCoverageCountFP;
Chris Dalton1a325d22017-07-14 15:17:41 -060057
Chris Dalton383a2ef2018-01-08 17:21:41 -050058 void updateAndInval() { this->updateGpuData(); }
Chris Dalton1a325d22017-07-14 15:17:41 -060059
60 void updateGpuData();
61
Chris Dalton8dfc70f2018-03-26 19:15:22 -060062 PrimitiveType fPrimitiveType = PrimitiveType::kTriangles;
Chris Daltonbe4ffab2017-12-08 10:59:58 -070063 SkCubicType fCubicType;
Chris Dalton7f578bf2017-09-05 16:46:48 -060064 SkMatrix fCubicKLM;
Chris Dalton1a325d22017-07-14 15:17:41 -060065
66 SkPoint fPoints[4] = {
Chris Dalton383a2ef2018-01-08 17:21:41 -050067 {100.05f, 100.05f}, {400.75f, 100.05f}, {400.75f, 300.95f}, {100.05f, 300.95f}};
Chris Dalton1a325d22017-07-14 15:17:41 -060068
Chris Dalton9f2dab02018-04-18 14:07:03 -060069 float fConicWeight = .5;
Chris Dalton09a7bb22018-08-31 19:53:15 +080070 float fStrokeWidth = 40;
71 bool fDoStroke = false;
Chris Dalton9f2dab02018-04-18 14:07:03 -060072
Chris Dalton84403d72018-02-13 21:46:17 -050073 SkTArray<TriPointInstance> fTriPointInstances;
74 SkTArray<QuadPointInstance> fQuadPointInstances;
Chris Dalton09a7bb22018-08-31 19:53:15 +080075 SkPath fPath;
Chris Dalton1a325d22017-07-14 15:17:41 -060076
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040077 typedef Sample INHERITED;
Chris Dalton1a325d22017-07-14 15:17:41 -060078};
79
Chris Dalton8d38a7f2018-03-19 14:16:44 -060080class CCPRGeometryView::DrawCoverageCountOp : public GrDrawOp {
Chris Dalton1a325d22017-07-14 15:17:41 -060081 DEFINE_OP_CLASS_ID
82
83public:
Chris Dalton8d38a7f2018-03-19 14:16:44 -060084 DrawCoverageCountOp(CCPRGeometryView* view) : INHERITED(ClassID()), fView(view) {
Chris Daltone1639692018-08-20 14:00:30 -060085 this->setBounds(SkRect::MakeIWH(fView->width(), fView->height()), GrOp::HasAABloat::kNo,
86 GrOp::IsZeroArea::kNo);
Chris Dalton1a325d22017-07-14 15:17:41 -060087 }
88
Chris Dalton8d38a7f2018-03-19 14:16:44 -060089 const char* name() const override {
90 return "[Testing/Sample code] CCPRGeometryView::DrawCoverageCountOp";
91 }
Chris Dalton1a325d22017-07-14 15:17:41 -060092
93private:
94 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Chris Daltonb8fff0d2019-03-05 10:11:58 -070095 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, GrFSAAType) 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
Greg Daniel4065d452018-11-16 15:43:41 -0500187 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500188 ctx->priv().caps()->getBackendFormatFromGrColorType(GrColorType::kAlpha_F16,
Greg Daniel4065d452018-11-16 15:43:41 -0500189 GrSRGBEncoded::kNo);
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600190 sk_sp<GrRenderTargetContext> ccbuff =
Robert Phillips9da87e02019-02-04 13:26:26 -0500191 ctx->priv().makeDeferredRenderTargetContext(format, SkBackingFit::kApprox,
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600192 this->width(), this->height(),
193 kAlpha_half_GrPixelConfig,
194 nullptr);
195 SkASSERT(ccbuff);
Brian Osman9a9baae2018-11-05 15:06:26 -0500196 ccbuff->clear(nullptr, SK_PMColor4fTRANSPARENT,
197 GrRenderTargetContext::CanClearFullscreen::kYes);
Ben Wagnerd4bffed2018-08-14 17:51:23 -0400198 ccbuff->priv().testingOnly_addDrawOp(pool->allocate<DrawCoverageCountOp>(this));
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600199
200 // Visualize coverage count in main canvas.
201 GrPaint paint;
202 paint.addColorFragmentProcessor(
203 GrSimpleTextureEffect::Make(sk_ref_sp(ccbuff->asTextureProxy()), SkMatrix::I()));
204 paint.addColorFragmentProcessor(
205 skstd::make_unique<VisualizeCoverageCountFP>());
206 paint.setPorterDuffXPFactory(SkBlendMode::kSrcOver);
207 rtc->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
208 SkRect::MakeIWH(this->width(), this->height()));
209
210 // Add label.
Chris Dalton703b4762018-04-06 16:11:48 -0600211 caption.appendf("PrimitiveType_%s",
212 GrCCCoverageProcessor::PrimitiveTypeName(fPrimitiveType));
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600213 if (PrimitiveType::kCubics == fPrimitiveType) {
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700214 caption.appendf(" (%s)", SkCubicTypeName(fCubicType));
Chris Dalton9f2dab02018-04-18 14:07:03 -0600215 } else if (PrimitiveType::kConics == fPrimitiveType) {
216 caption.appendf(" (w=%f)", fConicWeight);
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700217 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800218 if (fDoStroke) {
219 caption.appendf(" (stroke_width=%f)", fStrokeWidth);
220 }
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700221 } else {
222 caption = "Use GPU backend to visualize geometry.";
Chris Dalton1a325d22017-07-14 15:17:41 -0600223 }
224
225 SkPaint pointsPaint;
226 pointsPaint.setColor(SK_ColorBLUE);
227 pointsPaint.setStrokeWidth(8);
228 pointsPaint.setAntiAlias(true);
229
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600230 if (PrimitiveType::kCubics == fPrimitiveType) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600231 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, fPoints, pointsPaint);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800232 if (!fDoStroke) {
233 int w = this->width(), h = this->height();
234 draw_klm_line(w, h, canvas, &fCubicKLM[0], SK_ColorYELLOW);
235 draw_klm_line(w, h, canvas, &fCubicKLM[3], SK_ColorBLUE);
236 draw_klm_line(w, h, canvas, &fCubicKLM[6], SK_ColorRED);
237 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600238 } else {
239 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, fPoints, pointsPaint);
240 canvas->drawPoints(SkCanvas::kPoints_PointMode, 1, fPoints + 3, pointsPaint);
241 }
242
Hal Canary4484b8f2019-01-08 14:00:08 -0500243 SkFont font(nullptr, 20);
Chris Dalton1a325d22017-07-14 15:17:41 -0600244 SkPaint captionPaint;
Chris Dalton8738cf42018-03-09 11:57:40 -0700245 captionPaint.setColor(SK_ColorWHITE);
Hal Canary4484b8f2019-01-08 14:00:08 -0500246 canvas->drawString(caption, 10, 30, font, captionPaint);
Chris Dalton1a325d22017-07-14 15:17:41 -0600247}
248
249void CCPRGeometryView::updateGpuData() {
Chris Daltone1639692018-08-20 14:00:30 -0600250 using Verb = GrCCFillGeometry::Verb;
Chris Dalton84403d72018-02-13 21:46:17 -0500251 fTriPointInstances.reset();
252 fQuadPointInstances.reset();
Chris Dalton1a325d22017-07-14 15:17:41 -0600253
Chris Dalton09a7bb22018-08-31 19:53:15 +0800254 fPath.reset();
255 fPath.moveTo(fPoints[0]);
256
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600257 if (PrimitiveType::kCubics == fPrimitiveType) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600258 double t[2], s[2];
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700259 fCubicType = GrPathUtils::getCubicKLM(fPoints, &fCubicKLM, t, s);
Chris Daltone1639692018-08-20 14:00:30 -0600260 GrCCFillGeometry geometry;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600261 geometry.beginContour(fPoints[0]);
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600262 geometry.cubicTo(fPoints, kDebugBloat / 2, kDebugBloat / 2);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600263 geometry.endContour();
Chris Dalton7f578bf2017-09-05 16:46:48 -0600264 int ptsIdx = 0;
Chris Daltone1639692018-08-20 14:00:30 -0600265 for (Verb verb : geometry.verbs()) {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600266 switch (verb) {
Chris Daltone1639692018-08-20 14:00:30 -0600267 case Verb::kLineTo:
Chris Dalton7f578bf2017-09-05 16:46:48 -0600268 ++ptsIdx;
269 continue;
Chris Daltone1639692018-08-20 14:00:30 -0600270 case Verb::kMonotonicQuadraticTo:
Chris Dalton7f578bf2017-09-05 16:46:48 -0600271 ptsIdx += 2;
272 continue;
Chris Daltone1639692018-08-20 14:00:30 -0600273 case Verb::kMonotonicCubicTo:
Chris Dalton84403d72018-02-13 21:46:17 -0500274 fQuadPointInstances.push_back().set(&geometry.points()[ptsIdx], 0, 0);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600275 ptsIdx += 3;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600276 continue;
Chris Dalton383a2ef2018-01-08 17:21:41 -0500277 default:
278 continue;
Chris Dalton1a325d22017-07-14 15:17:41 -0600279 }
280 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800281 fPath.cubicTo(fPoints[1], fPoints[2], fPoints[3]);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600282 } else if (PrimitiveType::kTriangles != fPrimitiveType) {
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600283 SkPoint P3[3] = {fPoints[0], fPoints[1], fPoints[3]};
Chris Daltone1639692018-08-20 14:00:30 -0600284 GrCCFillGeometry geometry;
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600285 geometry.beginContour(P3[0]);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600286 if (PrimitiveType::kQuadratics == fPrimitiveType) {
287 geometry.quadraticTo(P3);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800288 fPath.quadTo(fPoints[1], fPoints[3]);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600289 } else {
290 SkASSERT(PrimitiveType::kConics == fPrimitiveType);
291 geometry.conicTo(P3, fConicWeight);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800292 fPath.conicTo(fPoints[1], fPoints[3], fConicWeight);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600293 }
Chris Daltonc1e59632017-09-05 00:30:07 -0600294 geometry.endContour();
Chris Dalton9f2dab02018-04-18 14:07:03 -0600295 int ptsIdx = 0, conicWeightIdx = 0;
Chris Daltone1639692018-08-20 14:00:30 -0600296 for (Verb verb : geometry.verbs()) {
297 if (Verb::kBeginContour == verb ||
298 Verb::kEndOpenContour == verb ||
299 Verb::kEndClosedContour == verb) {
Chris Daltonc1e59632017-09-05 00:30:07 -0600300 continue;
301 }
Chris Daltone1639692018-08-20 14:00:30 -0600302 if (Verb::kLineTo == verb) {
Chris Dalton90e8fb12017-12-22 02:24:53 -0700303 ++ptsIdx;
304 continue;
305 }
Chris Daltone1639692018-08-20 14:00:30 -0600306 SkASSERT(Verb::kMonotonicQuadraticTo == verb || Verb::kMonotonicConicTo == verb);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600307 if (PrimitiveType::kQuadratics == fPrimitiveType &&
Chris Daltone1639692018-08-20 14:00:30 -0600308 Verb::kMonotonicQuadraticTo == verb) {
Chris Dalton9f2dab02018-04-18 14:07:03 -0600309 fTriPointInstances.push_back().set(&geometry.points()[ptsIdx], Sk2f(0, 0));
310 } else if (PrimitiveType::kConics == fPrimitiveType &&
Chris Daltone1639692018-08-20 14:00:30 -0600311 Verb::kMonotonicConicTo == verb) {
Chris Dalton9f2dab02018-04-18 14:07:03 -0600312 fQuadPointInstances.push_back().setW(&geometry.points()[ptsIdx], Sk2f(0, 0),
313 geometry.getConicWeight(conicWeightIdx++));
314 }
Chris Daltona3e92712017-12-04 11:45:51 -0700315 ptsIdx += 2;
Chris Daltonb072bb62017-08-07 09:00:46 -0600316 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600317 } else {
Chris Dalton84403d72018-02-13 21:46:17 -0500318 fTriPointInstances.push_back().set(fPoints[0], fPoints[1], fPoints[3], Sk2f(0, 0));
Chris Dalton09a7bb22018-08-31 19:53:15 +0800319 fPath.lineTo(fPoints[1]);
320 fPath.lineTo(fPoints[3]);
321 fPath.close();
Chris Dalton1a325d22017-07-14 15:17:41 -0600322 }
323}
324
Brian Salomon588cec72018-11-14 13:56:37 -0500325void CCPRGeometryView::DrawCoverageCountOp::onExecute(GrOpFlushState* state,
326 const SkRect& chainBounds) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600327 GrResourceProvider* rp = state->resourceProvider();
328 GrContext* context = state->gpu()->getContext();
Robert Phillips4217ea72019-01-30 13:08:28 -0500329 GrGLGpu* glGpu = GrBackendApi::kOpenGL == context->backend()
Chris Dalton383a2ef2018-01-08 17:21:41 -0500330 ? static_cast<GrGLGpu*>(state->gpu())
331 : nullptr;
Chris Dalton1a325d22017-07-14 15:17:41 -0600332 if (glGpu) {
333 glGpu->handleDirtyContext();
Chris Dalton8738cf42018-03-09 11:57:40 -0700334 // GR_GL_CALL(glGpu->glInterface(), PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
Chris Dalton1a325d22017-07-14 15:17:41 -0600335 GR_GL_CALL(glGpu->glInterface(), Enable(GR_GL_LINE_SMOOTH));
336 }
337
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500338 GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kPlus);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800339
340 if (!fView->fDoStroke) {
341 GrCCCoverageProcessor proc(rp, fView->fPrimitiveType);
342 SkDEBUGCODE(proc.enableDebugBloat(kDebugBloat));
343
344 SkSTArray<1, GrMesh> mesh;
345 if (PrimitiveType::kCubics == fView->fPrimitiveType ||
346 PrimitiveType::kConics == fView->fPrimitiveType) {
Brian Salomondbf70722019-02-07 11:31:24 -0500347 sk_sp<GrGpuBuffer> instBuff(
Brian Salomonae64c192019-02-05 09:41:37 -0500348 rp->createBuffer(fView->fQuadPointInstances.count() * sizeof(QuadPointInstance),
349 GrGpuBufferType::kVertex, kDynamic_GrAccessPattern,
Brian Salomonae64c192019-02-05 09:41:37 -0500350 fView->fQuadPointInstances.begin()));
Chris Dalton09a7bb22018-08-31 19:53:15 +0800351 if (!fView->fQuadPointInstances.empty() && instBuff) {
Brian Salomon12d22642019-01-29 14:38:50 -0500352 proc.appendMesh(std::move(instBuff), fView->fQuadPointInstances.count(), 0, &mesh);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800353 }
354 } else {
Brian Salomondbf70722019-02-07 11:31:24 -0500355 sk_sp<GrGpuBuffer> instBuff(
Brian Salomonae64c192019-02-05 09:41:37 -0500356 rp->createBuffer(fView->fTriPointInstances.count() * sizeof(TriPointInstance),
357 GrGpuBufferType::kVertex, kDynamic_GrAccessPattern,
Brian Salomonae64c192019-02-05 09:41:37 -0500358 fView->fTriPointInstances.begin()));
Chris Dalton09a7bb22018-08-31 19:53:15 +0800359 if (!fView->fTriPointInstances.empty() && instBuff) {
Brian Salomon12d22642019-01-29 14:38:50 -0500360 proc.appendMesh(std::move(instBuff), fView->fTriPointInstances.count(), 0, &mesh);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800361 }
362 }
363
364 if (!mesh.empty()) {
365 SkASSERT(1 == mesh.count());
366 proc.draw(state, pipeline, nullptr, mesh.begin(), 1, this->bounds());
367 }
368 } else if (PrimitiveType::kConics != fView->fPrimitiveType) { // No conic stroke support yet.
369 GrCCStroker stroker(0,0,0);
370
371 SkPaint p;
372 p.setStyle(SkPaint::kStroke_Style);
373 p.setStrokeWidth(fView->fStrokeWidth);
374 p.setStrokeJoin(SkPaint::kMiter_Join);
375 p.setStrokeMiter(4);
376 // p.setStrokeCap(SkPaint::kRound_Cap);
377 stroker.parseDeviceSpaceStroke(fView->fPath, SkPathPriv::PointData(fView->fPath),
378 SkStrokeRec(p), p.getStrokeWidth(), GrScissorTest::kDisabled,
379 SkIRect::MakeWH(fView->width(), fView->height()), {0, 0});
380 GrCCStroker::BatchID batchID = stroker.closeCurrentBatch();
381
Robert Phillips9da87e02019-02-04 13:26:26 -0500382 GrOnFlushResourceProvider onFlushRP(context->priv().drawingManager());
Chris Dalton09a7bb22018-08-31 19:53:15 +0800383 stroker.prepareToDraw(&onFlushRP);
384
385 SkIRect ibounds;
386 this->bounds().roundOut(&ibounds);
387 stroker.drawStrokes(state, batchID, ibounds);
Chris Dalton23261772017-12-10 16:41:45 -0700388 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600389
390 if (glGpu) {
391 context->resetContext(kMisc_GrGLBackendState);
392 }
393}
394
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400395class CCPRGeometryView::Click : public Sample::Click {
Chris Dalton1a325d22017-07-14 15:17:41 -0600396public:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400397 Click(Sample* target, int ptIdx) : Sample::Click(target), fPtIdx(ptIdx) {}
Chris Dalton1a325d22017-07-14 15:17:41 -0600398
399 void doClick(SkPoint points[]) {
400 if (fPtIdx >= 0) {
401 this->dragPoint(points, fPtIdx);
402 } else {
403 for (int i = 0; i < 4; ++i) {
404 this->dragPoint(points, i);
405 }
406 }
407 }
408
409private:
Chris Dalton383a2ef2018-01-08 17:21:41 -0500410 void dragPoint(SkPoint points[], int idx) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600411 SkIPoint delta = fICurr - fIPrev;
412 points[idx] += SkPoint::Make(delta.x(), delta.y());
413 }
414
415 int fPtIdx;
416};
417
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400418Sample::Click* CCPRGeometryView::onFindClickHandler(SkScalar x, SkScalar y, unsigned) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600419 for (int i = 0; i < 4; ++i) {
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600420 if (PrimitiveType::kCubics != fPrimitiveType && 2 == i) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600421 continue;
422 }
423 if (fabs(x - fPoints[i].x()) < 20 && fabsf(y - fPoints[i].y()) < 20) {
424 return new Click(this, i);
425 }
426 }
427 return new Click(this, -1);
428}
429
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400430bool CCPRGeometryView::onClick(Sample::Click* click) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500431 Click* myClick = (Click*)click;
Chris Dalton1a325d22017-07-14 15:17:41 -0600432 myClick->doClick(fPoints);
433 this->updateAndInval();
434 return true;
435}
436
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400437bool CCPRGeometryView::onQuery(Sample::Event* evt) {
438 if (Sample::TitleQ(*evt)) {
439 Sample::TitleR(evt, "CCPRGeometry");
Chris Dalton1a325d22017-07-14 15:17:41 -0600440 return true;
441 }
442 SkUnichar unichar;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400443 if (Sample::CharQ(*evt, &unichar)) {
Chris Dalton9f2dab02018-04-18 14:07:03 -0600444 if (unichar >= '1' && unichar <= '4') {
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600445 fPrimitiveType = PrimitiveType(unichar - '1');
Chris Dalton703b4762018-04-06 16:11:48 -0600446 if (fPrimitiveType >= PrimitiveType::kWeightedTriangles) {
447 fPrimitiveType = (PrimitiveType) ((int)fPrimitiveType + 1);
448 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600449 this->updateAndInval();
450 return true;
451 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800452 float* valueToScale = nullptr;
453 if (fDoStroke) {
454 valueToScale = &fStrokeWidth;
455 } else if (PrimitiveType::kConics == fPrimitiveType) {
456 valueToScale = &fConicWeight;
457 }
458 if (valueToScale) {
Chris Dalton9f2dab02018-04-18 14:07:03 -0600459 if (unichar == '+') {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800460 *valueToScale *= 2;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600461 this->updateAndInval();
462 return true;
463 }
464 if (unichar == '+' || unichar == '=') {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800465 *valueToScale *= 5/4.f;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600466 this->updateAndInval();
467 return true;
468 }
469 if (unichar == '-') {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800470 *valueToScale *= 4/5.f;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600471 this->updateAndInval();
472 return true;
473 }
474 if (unichar == '_') {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800475 *valueToScale *= .5f;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600476 this->updateAndInval();
477 return true;
478 }
479 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600480 if (unichar == 'D') {
481 SkDebugf(" SkPoint fPoints[4] = {\n");
Chris Dalton7f578bf2017-09-05 16:46:48 -0600482 SkDebugf(" {%ff, %ff},\n", fPoints[0].x(), fPoints[0].y());
483 SkDebugf(" {%ff, %ff},\n", fPoints[1].x(), fPoints[1].y());
484 SkDebugf(" {%ff, %ff},\n", fPoints[2].x(), fPoints[2].y());
485 SkDebugf(" {%ff, %ff}\n", fPoints[3].x(), fPoints[3].y());
Chris Dalton1a325d22017-07-14 15:17:41 -0600486 SkDebugf(" };\n");
487 return true;
488 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800489 if (unichar == 'S') {
490 fDoStroke = !fDoStroke;
491 this->updateAndInval();
492 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600493 }
494 return this->INHERITED::onQuery(evt);
495}
496
Chris Dalton383a2ef2018-01-08 17:21:41 -0500497DEF_SAMPLE(return new CCPRGeometryView;)
Chris Dalton1a325d22017-07-14 15:17:41 -0600498
Chris Dalton383a2ef2018-01-08 17:21:41 -0500499#endif // SK_SUPPORT_GPU