Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkTypes.h" |
| 9 | |
| 10 | #if SK_SUPPORT_GPU |
| 11 | |
| 12 | #include "GrContextPriv.h" |
Ben Wagner | d4bffed | 2018-08-14 17:51:23 -0400 | [diff] [blame] | 13 | #include "GrMemoryPool.h" |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 14 | #include "GrPathUtils.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 15 | #include "GrRenderTargetContext.h" |
| 16 | #include "GrRenderTargetContextPriv.h" |
| 17 | #include "GrResourceProvider.h" |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 18 | #include "Sample.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 19 | #include "SkCanvas.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 20 | #include "SkPaint.h" |
| 21 | #include "SkPath.h" |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 22 | #include "SkRectPriv.h" |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 23 | #include "ccpr/GrCCCoverageProcessor.h" |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 24 | #include "ccpr/GrCCFillGeometry.h" |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 25 | #include "ccpr/GrCCStroker.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 26 | #include "gl/GrGLGpu.cpp" |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 27 | #include "glsl/GrGLSLFragmentProcessor.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 28 | #include "ops/GrDrawOp.h" |
| 29 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 30 | using TriPointInstance = GrCCCoverageProcessor::TriPointInstance; |
| 31 | using QuadPointInstance = GrCCCoverageProcessor::QuadPointInstance; |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 32 | using PrimitiveType = GrCCCoverageProcessor::PrimitiveType; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 33 | |
Chris Dalton | a640c49 | 2017-09-11 22:04:03 -0700 | [diff] [blame] | 34 | static constexpr float kDebugBloat = 40; |
| 35 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 36 | /** |
| 37 | * This sample visualizes the AA bloat geometry generated by the ccpr geometry shaders. It |
| 38 | * increases the AA bloat by 50x and outputs color instead of coverage (coverage=+1 -> green, |
| 39 | * coverage=0 -> black, coverage=-1 -> red). Use the keys 1-7 to cycle through the different |
| 40 | * geometry processors. |
| 41 | */ |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 42 | class CCPRGeometryView : public Sample { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 43 | public: |
| 44 | CCPRGeometryView() { this->updateGpuData(); } |
| 45 | void onDrawContent(SkCanvas*) override; |
| 46 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 47 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override; |
| 48 | bool onClick(Sample::Click*) override; |
| 49 | bool onQuery(Sample::Event* evt) override; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 50 | |
| 51 | private: |
| 52 | class Click; |
Chris Dalton | 8d38a7f | 2018-03-19 14:16:44 -0600 | [diff] [blame] | 53 | class DrawCoverageCountOp; |
| 54 | class VisualizeCoverageCountFP; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 55 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 56 | void updateAndInval() { this->updateGpuData(); } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 57 | |
| 58 | void updateGpuData(); |
| 59 | |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 60 | PrimitiveType fPrimitiveType = PrimitiveType::kTriangles; |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 61 | SkCubicType fCubicType; |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 62 | SkMatrix fCubicKLM; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 63 | |
| 64 | SkPoint fPoints[4] = { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 65 | {100.05f, 100.05f}, {400.75f, 100.05f}, {400.75f, 300.95f}, {100.05f, 300.95f}}; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 66 | |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 67 | float fConicWeight = .5; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 68 | float fStrokeWidth = 40; |
| 69 | bool fDoStroke = false; |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 70 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 71 | SkTArray<TriPointInstance> fTriPointInstances; |
| 72 | SkTArray<QuadPointInstance> fQuadPointInstances; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 73 | SkPath fPath; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 74 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 75 | typedef Sample INHERITED; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 76 | }; |
| 77 | |
Chris Dalton | 8d38a7f | 2018-03-19 14:16:44 -0600 | [diff] [blame] | 78 | class CCPRGeometryView::DrawCoverageCountOp : public GrDrawOp { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 79 | DEFINE_OP_CLASS_ID |
| 80 | |
| 81 | public: |
Chris Dalton | 8d38a7f | 2018-03-19 14:16:44 -0600 | [diff] [blame] | 82 | DrawCoverageCountOp(CCPRGeometryView* view) : INHERITED(ClassID()), fView(view) { |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 83 | this->setBounds(SkRect::MakeIWH(fView->width(), fView->height()), GrOp::HasAABloat::kNo, |
| 84 | GrOp::IsZeroArea::kNo); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 85 | } |
| 86 | |
Chris Dalton | 8d38a7f | 2018-03-19 14:16:44 -0600 | [diff] [blame] | 87 | const char* name() const override { |
| 88 | return "[Testing/Sample code] CCPRGeometryView::DrawCoverageCountOp"; |
| 89 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 90 | |
| 91 | private: |
| 92 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 93 | RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*) override { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 94 | return RequiresDstTexture::kNo; |
| 95 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 96 | void onPrepare(GrOpFlushState*) override {} |
| 97 | void onExecute(GrOpFlushState*) override; |
| 98 | |
| 99 | CCPRGeometryView* fView; |
| 100 | |
| 101 | typedef GrDrawOp INHERITED; |
| 102 | }; |
| 103 | |
Chris Dalton | 8d38a7f | 2018-03-19 14:16:44 -0600 | [diff] [blame] | 104 | class CCPRGeometryView::VisualizeCoverageCountFP : public GrFragmentProcessor { |
| 105 | public: |
| 106 | VisualizeCoverageCountFP() : GrFragmentProcessor(kTestFP_ClassID, kNone_OptimizationFlags) {} |
| 107 | |
| 108 | private: |
| 109 | const char* name() const override { |
| 110 | return "[Testing/Sample code] CCPRGeometryView::VisualizeCoverageCountFP"; |
| 111 | } |
| 112 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 113 | return skstd::make_unique<VisualizeCoverageCountFP>(); |
| 114 | } |
| 115 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
| 116 | bool onIsEqual(const GrFragmentProcessor&) const override { return true; } |
| 117 | |
| 118 | class Impl : public GrGLSLFragmentProcessor { |
| 119 | void emitCode(EmitArgs& args) override { |
| 120 | GrGLSLFPFragmentBuilder* f = args.fFragBuilder; |
| 121 | f->codeAppendf("half count = %s.a;", args.fInputColor); |
| 122 | f->codeAppendf("%s = half4(clamp(-count, 0, 1), clamp(+count, 0, 1), 0, abs(count));", |
| 123 | args.fOutputColor); |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new Impl; } |
| 128 | }; |
| 129 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 130 | static void draw_klm_line(int w, int h, SkCanvas* canvas, const SkScalar line[3], SkColor color) { |
| 131 | SkPoint p1, p2; |
| 132 | if (SkScalarAbs(line[1]) > SkScalarAbs(line[0])) { |
| 133 | // Draw from vertical edge to vertical edge. |
| 134 | p1 = {0, -line[2] / line[1]}; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 135 | p2 = {(SkScalar)w, (-line[2] - w * line[0]) / line[1]}; |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 136 | } else { |
| 137 | // Draw from horizontal edge to horizontal edge. |
| 138 | p1 = {-line[2] / line[0], 0}; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 139 | p2 = {(-line[2] - h * line[1]) / line[0], (SkScalar)h}; |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | SkPaint linePaint; |
| 143 | linePaint.setColor(color); |
| 144 | linePaint.setAlpha(128); |
| 145 | linePaint.setStyle(SkPaint::kStroke_Style); |
| 146 | linePaint.setStrokeWidth(0); |
| 147 | linePaint.setAntiAlias(true); |
| 148 | canvas->drawLine(p1, p2, linePaint); |
| 149 | } |
| 150 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 151 | void CCPRGeometryView::onDrawContent(SkCanvas* canvas) { |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 152 | canvas->clear(SK_ColorBLACK); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 153 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 154 | if (!fDoStroke) { |
| 155 | SkPaint outlinePaint; |
| 156 | outlinePaint.setColor(0x80ffffff); |
| 157 | outlinePaint.setStyle(SkPaint::kStroke_Style); |
| 158 | outlinePaint.setStrokeWidth(0); |
| 159 | outlinePaint.setAntiAlias(true); |
| 160 | canvas->drawPath(fPath, outlinePaint); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 161 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 162 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 163 | #if 0 |
| 164 | SkPaint gridPaint; |
| 165 | gridPaint.setColor(0x10000000); |
| 166 | gridPaint.setStyle(SkPaint::kStroke_Style); |
| 167 | gridPaint.setStrokeWidth(0); |
| 168 | gridPaint.setAntiAlias(true); |
Chris Dalton | a640c49 | 2017-09-11 22:04:03 -0700 | [diff] [blame] | 169 | for (int y = 0; y < this->height(); y += kDebugBloat) { |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 170 | canvas->drawLine(0, y, this->width(), y, gridPaint); |
| 171 | } |
Chris Dalton | a640c49 | 2017-09-11 22:04:03 -0700 | [diff] [blame] | 172 | for (int x = 0; x < this->width(); x += kDebugBloat) { |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 173 | canvas->drawLine(x, 0, x, this->height(), outlinePaint); |
| 174 | } |
| 175 | #endif |
| 176 | |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 177 | SkString caption; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 178 | if (GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext()) { |
Chris Dalton | 8d38a7f | 2018-03-19 14:16:44 -0600 | [diff] [blame] | 179 | // Render coverage count. |
| 180 | GrContext* ctx = canvas->getGrContext(); |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 181 | SkASSERT(ctx); |
| 182 | |
Ben Wagner | d4bffed | 2018-08-14 17:51:23 -0400 | [diff] [blame] | 183 | GrOpMemoryPool* pool = ctx->contextPriv().opMemoryPool(); |
| 184 | |
Chris Dalton | 8d38a7f | 2018-03-19 14:16:44 -0600 | [diff] [blame] | 185 | sk_sp<GrRenderTargetContext> ccbuff = |
| 186 | ctx->contextPriv().makeDeferredRenderTargetContext(SkBackingFit::kApprox, |
| 187 | this->width(), this->height(), |
| 188 | kAlpha_half_GrPixelConfig, |
| 189 | nullptr); |
| 190 | SkASSERT(ccbuff); |
| 191 | ccbuff->clear(nullptr, 0, GrRenderTargetContext::CanClearFullscreen::kYes); |
Ben Wagner | d4bffed | 2018-08-14 17:51:23 -0400 | [diff] [blame] | 192 | ccbuff->priv().testingOnly_addDrawOp(pool->allocate<DrawCoverageCountOp>(this)); |
Chris Dalton | 8d38a7f | 2018-03-19 14:16:44 -0600 | [diff] [blame] | 193 | |
| 194 | // Visualize coverage count in main canvas. |
| 195 | GrPaint paint; |
| 196 | paint.addColorFragmentProcessor( |
| 197 | GrSimpleTextureEffect::Make(sk_ref_sp(ccbuff->asTextureProxy()), SkMatrix::I())); |
| 198 | paint.addColorFragmentProcessor( |
| 199 | skstd::make_unique<VisualizeCoverageCountFP>()); |
| 200 | paint.setPorterDuffXPFactory(SkBlendMode::kSrcOver); |
| 201 | rtc->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), |
| 202 | SkRect::MakeIWH(this->width(), this->height())); |
| 203 | |
| 204 | // Add label. |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame] | 205 | caption.appendf("PrimitiveType_%s", |
| 206 | GrCCCoverageProcessor::PrimitiveTypeName(fPrimitiveType)); |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 207 | if (PrimitiveType::kCubics == fPrimitiveType) { |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 208 | caption.appendf(" (%s)", SkCubicTypeName(fCubicType)); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 209 | } else if (PrimitiveType::kConics == fPrimitiveType) { |
| 210 | caption.appendf(" (w=%f)", fConicWeight); |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 211 | } |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 212 | if (fDoStroke) { |
| 213 | caption.appendf(" (stroke_width=%f)", fStrokeWidth); |
| 214 | } |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 215 | } else { |
| 216 | caption = "Use GPU backend to visualize geometry."; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | SkPaint pointsPaint; |
| 220 | pointsPaint.setColor(SK_ColorBLUE); |
| 221 | pointsPaint.setStrokeWidth(8); |
| 222 | pointsPaint.setAntiAlias(true); |
| 223 | |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 224 | if (PrimitiveType::kCubics == fPrimitiveType) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 225 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, fPoints, pointsPaint); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 226 | if (!fDoStroke) { |
| 227 | int w = this->width(), h = this->height(); |
| 228 | draw_klm_line(w, h, canvas, &fCubicKLM[0], SK_ColorYELLOW); |
| 229 | draw_klm_line(w, h, canvas, &fCubicKLM[3], SK_ColorBLUE); |
| 230 | draw_klm_line(w, h, canvas, &fCubicKLM[6], SK_ColorRED); |
| 231 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 232 | } else { |
| 233 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, fPoints, pointsPaint); |
| 234 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 1, fPoints + 3, pointsPaint); |
| 235 | } |
| 236 | |
| 237 | SkPaint captionPaint; |
| 238 | captionPaint.setTextSize(20); |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 239 | captionPaint.setColor(SK_ColorWHITE); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 240 | captionPaint.setAntiAlias(true); |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 241 | canvas->drawText(caption.c_str(), caption.size(), 10, 30, captionPaint); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | void CCPRGeometryView::updateGpuData() { |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 245 | using Verb = GrCCFillGeometry::Verb; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 246 | fTriPointInstances.reset(); |
| 247 | fQuadPointInstances.reset(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 248 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 249 | fPath.reset(); |
| 250 | fPath.moveTo(fPoints[0]); |
| 251 | |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 252 | if (PrimitiveType::kCubics == fPrimitiveType) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 253 | double t[2], s[2]; |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 254 | fCubicType = GrPathUtils::getCubicKLM(fPoints, &fCubicKLM, t, s); |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 255 | GrCCFillGeometry geometry; |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 256 | geometry.beginContour(fPoints[0]); |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 257 | geometry.cubicTo(fPoints, kDebugBloat / 2, kDebugBloat / 2); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 258 | geometry.endContour(); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 259 | int ptsIdx = 0; |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 260 | for (Verb verb : geometry.verbs()) { |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 261 | switch (verb) { |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 262 | case Verb::kLineTo: |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 263 | ++ptsIdx; |
| 264 | continue; |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 265 | case Verb::kMonotonicQuadraticTo: |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 266 | ptsIdx += 2; |
| 267 | continue; |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 268 | case Verb::kMonotonicCubicTo: |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 269 | fQuadPointInstances.push_back().set(&geometry.points()[ptsIdx], 0, 0); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 270 | ptsIdx += 3; |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 271 | continue; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 272 | default: |
| 273 | continue; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 274 | } |
| 275 | } |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 276 | fPath.cubicTo(fPoints[1], fPoints[2], fPoints[3]); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 277 | } else if (PrimitiveType::kTriangles != fPrimitiveType) { |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 278 | SkPoint P3[3] = {fPoints[0], fPoints[1], fPoints[3]}; |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 279 | GrCCFillGeometry geometry; |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 280 | geometry.beginContour(P3[0]); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 281 | if (PrimitiveType::kQuadratics == fPrimitiveType) { |
| 282 | geometry.quadraticTo(P3); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 283 | fPath.quadTo(fPoints[1], fPoints[3]); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 284 | } else { |
| 285 | SkASSERT(PrimitiveType::kConics == fPrimitiveType); |
| 286 | geometry.conicTo(P3, fConicWeight); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 287 | fPath.conicTo(fPoints[1], fPoints[3], fConicWeight); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 288 | } |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 289 | geometry.endContour(); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 290 | int ptsIdx = 0, conicWeightIdx = 0; |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 291 | for (Verb verb : geometry.verbs()) { |
| 292 | if (Verb::kBeginContour == verb || |
| 293 | Verb::kEndOpenContour == verb || |
| 294 | Verb::kEndClosedContour == verb) { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 295 | continue; |
| 296 | } |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 297 | if (Verb::kLineTo == verb) { |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 298 | ++ptsIdx; |
| 299 | continue; |
| 300 | } |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 301 | SkASSERT(Verb::kMonotonicQuadraticTo == verb || Verb::kMonotonicConicTo == verb); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 302 | if (PrimitiveType::kQuadratics == fPrimitiveType && |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 303 | Verb::kMonotonicQuadraticTo == verb) { |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 304 | fTriPointInstances.push_back().set(&geometry.points()[ptsIdx], Sk2f(0, 0)); |
| 305 | } else if (PrimitiveType::kConics == fPrimitiveType && |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 306 | Verb::kMonotonicConicTo == verb) { |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 307 | fQuadPointInstances.push_back().setW(&geometry.points()[ptsIdx], Sk2f(0, 0), |
| 308 | geometry.getConicWeight(conicWeightIdx++)); |
| 309 | } |
Chris Dalton | a3e9271 | 2017-12-04 11:45:51 -0700 | [diff] [blame] | 310 | ptsIdx += 2; |
Chris Dalton | b072bb6 | 2017-08-07 09:00:46 -0600 | [diff] [blame] | 311 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 312 | } else { |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 313 | fTriPointInstances.push_back().set(fPoints[0], fPoints[1], fPoints[3], Sk2f(0, 0)); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 314 | fPath.lineTo(fPoints[1]); |
| 315 | fPath.lineTo(fPoints[3]); |
| 316 | fPath.close(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
Chris Dalton | 8d38a7f | 2018-03-19 14:16:44 -0600 | [diff] [blame] | 320 | void CCPRGeometryView::DrawCoverageCountOp::onExecute(GrOpFlushState* state) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 321 | GrResourceProvider* rp = state->resourceProvider(); |
| 322 | GrContext* context = state->gpu()->getContext(); |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 323 | GrGLGpu* glGpu = GrBackendApi::kOpenGL == context->contextPriv().getBackend() |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 324 | ? static_cast<GrGLGpu*>(state->gpu()) |
| 325 | : nullptr; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 326 | if (glGpu) { |
| 327 | glGpu->handleDirtyContext(); |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 328 | // GR_GL_CALL(glGpu->glInterface(), PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE)); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 329 | GR_GL_CALL(glGpu->glInterface(), Enable(GR_GL_LINE_SMOOTH)); |
| 330 | } |
| 331 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 332 | GrPipeline pipeline(state->drawOpArgs().fProxy, GrScissorTest::kDisabled, SkBlendMode::kPlus); |
| 333 | |
| 334 | if (!fView->fDoStroke) { |
| 335 | GrCCCoverageProcessor proc(rp, fView->fPrimitiveType); |
| 336 | SkDEBUGCODE(proc.enableDebugBloat(kDebugBloat)); |
| 337 | |
| 338 | SkSTArray<1, GrMesh> mesh; |
| 339 | if (PrimitiveType::kCubics == fView->fPrimitiveType || |
| 340 | PrimitiveType::kConics == fView->fPrimitiveType) { |
| 341 | sk_sp<GrBuffer> instBuff(rp->createBuffer( |
| 342 | fView->fQuadPointInstances.count() * sizeof(QuadPointInstance), |
| 343 | kVertex_GrBufferType, kDynamic_GrAccessPattern, |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 344 | GrResourceProvider::Flags::kNoPendingIO | |
| 345 | GrResourceProvider::Flags::kRequireGpuMemory, |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 346 | fView->fQuadPointInstances.begin())); |
| 347 | if (!fView->fQuadPointInstances.empty() && instBuff) { |
| 348 | proc.appendMesh(instBuff.get(), fView->fQuadPointInstances.count(), 0, &mesh); |
| 349 | } |
| 350 | } else { |
| 351 | sk_sp<GrBuffer> instBuff(rp->createBuffer( |
| 352 | fView->fTriPointInstances.count() * sizeof(TriPointInstance), |
| 353 | kVertex_GrBufferType, kDynamic_GrAccessPattern, |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 354 | GrResourceProvider::Flags::kNoPendingIO | |
| 355 | GrResourceProvider::Flags::kRequireGpuMemory, fView->fTriPointInstances.begin())); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 356 | if (!fView->fTriPointInstances.empty() && instBuff) { |
| 357 | proc.appendMesh(instBuff.get(), fView->fTriPointInstances.count(), 0, &mesh); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | if (!mesh.empty()) { |
| 362 | SkASSERT(1 == mesh.count()); |
| 363 | proc.draw(state, pipeline, nullptr, mesh.begin(), 1, this->bounds()); |
| 364 | } |
| 365 | } else if (PrimitiveType::kConics != fView->fPrimitiveType) { // No conic stroke support yet. |
| 366 | GrCCStroker stroker(0,0,0); |
| 367 | |
| 368 | SkPaint p; |
| 369 | p.setStyle(SkPaint::kStroke_Style); |
| 370 | p.setStrokeWidth(fView->fStrokeWidth); |
| 371 | p.setStrokeJoin(SkPaint::kMiter_Join); |
| 372 | p.setStrokeMiter(4); |
| 373 | // p.setStrokeCap(SkPaint::kRound_Cap); |
| 374 | stroker.parseDeviceSpaceStroke(fView->fPath, SkPathPriv::PointData(fView->fPath), |
| 375 | SkStrokeRec(p), p.getStrokeWidth(), GrScissorTest::kDisabled, |
| 376 | SkIRect::MakeWH(fView->width(), fView->height()), {0, 0}); |
| 377 | GrCCStroker::BatchID batchID = stroker.closeCurrentBatch(); |
| 378 | |
| 379 | GrOnFlushResourceProvider onFlushRP(context->contextPriv().drawingManager()); |
| 380 | stroker.prepareToDraw(&onFlushRP); |
| 381 | |
| 382 | SkIRect ibounds; |
| 383 | this->bounds().roundOut(&ibounds); |
| 384 | stroker.drawStrokes(state, batchID, ibounds); |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 385 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 386 | |
| 387 | if (glGpu) { |
| 388 | context->resetContext(kMisc_GrGLBackendState); |
| 389 | } |
| 390 | } |
| 391 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 392 | class CCPRGeometryView::Click : public Sample::Click { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 393 | public: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 394 | Click(Sample* target, int ptIdx) : Sample::Click(target), fPtIdx(ptIdx) {} |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 395 | |
| 396 | void doClick(SkPoint points[]) { |
| 397 | if (fPtIdx >= 0) { |
| 398 | this->dragPoint(points, fPtIdx); |
| 399 | } else { |
| 400 | for (int i = 0; i < 4; ++i) { |
| 401 | this->dragPoint(points, i); |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | private: |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 407 | void dragPoint(SkPoint points[], int idx) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 408 | SkIPoint delta = fICurr - fIPrev; |
| 409 | points[idx] += SkPoint::Make(delta.x(), delta.y()); |
| 410 | } |
| 411 | |
| 412 | int fPtIdx; |
| 413 | }; |
| 414 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 415 | Sample::Click* CCPRGeometryView::onFindClickHandler(SkScalar x, SkScalar y, unsigned) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 416 | for (int i = 0; i < 4; ++i) { |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 417 | if (PrimitiveType::kCubics != fPrimitiveType && 2 == i) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 418 | continue; |
| 419 | } |
| 420 | if (fabs(x - fPoints[i].x()) < 20 && fabsf(y - fPoints[i].y()) < 20) { |
| 421 | return new Click(this, i); |
| 422 | } |
| 423 | } |
| 424 | return new Click(this, -1); |
| 425 | } |
| 426 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 427 | bool CCPRGeometryView::onClick(Sample::Click* click) { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 428 | Click* myClick = (Click*)click; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 429 | myClick->doClick(fPoints); |
| 430 | this->updateAndInval(); |
| 431 | return true; |
| 432 | } |
| 433 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 434 | bool CCPRGeometryView::onQuery(Sample::Event* evt) { |
| 435 | if (Sample::TitleQ(*evt)) { |
| 436 | Sample::TitleR(evt, "CCPRGeometry"); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 437 | return true; |
| 438 | } |
| 439 | SkUnichar unichar; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 440 | if (Sample::CharQ(*evt, &unichar)) { |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 441 | if (unichar >= '1' && unichar <= '4') { |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 442 | fPrimitiveType = PrimitiveType(unichar - '1'); |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame] | 443 | if (fPrimitiveType >= PrimitiveType::kWeightedTriangles) { |
| 444 | fPrimitiveType = (PrimitiveType) ((int)fPrimitiveType + 1); |
| 445 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 446 | this->updateAndInval(); |
| 447 | return true; |
| 448 | } |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 449 | float* valueToScale = nullptr; |
| 450 | if (fDoStroke) { |
| 451 | valueToScale = &fStrokeWidth; |
| 452 | } else if (PrimitiveType::kConics == fPrimitiveType) { |
| 453 | valueToScale = &fConicWeight; |
| 454 | } |
| 455 | if (valueToScale) { |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 456 | if (unichar == '+') { |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 457 | *valueToScale *= 2; |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 458 | this->updateAndInval(); |
| 459 | return true; |
| 460 | } |
| 461 | if (unichar == '+' || unichar == '=') { |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 462 | *valueToScale *= 5/4.f; |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 463 | this->updateAndInval(); |
| 464 | return true; |
| 465 | } |
| 466 | if (unichar == '-') { |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 467 | *valueToScale *= 4/5.f; |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 468 | this->updateAndInval(); |
| 469 | return true; |
| 470 | } |
| 471 | if (unichar == '_') { |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 472 | *valueToScale *= .5f; |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 473 | this->updateAndInval(); |
| 474 | return true; |
| 475 | } |
| 476 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 477 | if (unichar == 'D') { |
| 478 | SkDebugf(" SkPoint fPoints[4] = {\n"); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 479 | SkDebugf(" {%ff, %ff},\n", fPoints[0].x(), fPoints[0].y()); |
| 480 | SkDebugf(" {%ff, %ff},\n", fPoints[1].x(), fPoints[1].y()); |
| 481 | SkDebugf(" {%ff, %ff},\n", fPoints[2].x(), fPoints[2].y()); |
| 482 | SkDebugf(" {%ff, %ff}\n", fPoints[3].x(), fPoints[3].y()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 483 | SkDebugf(" };\n"); |
| 484 | return true; |
| 485 | } |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 486 | if (unichar == 'S') { |
| 487 | fDoStroke = !fDoStroke; |
| 488 | this->updateAndInval(); |
| 489 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 490 | } |
| 491 | return this->INHERITED::onQuery(evt); |
| 492 | } |
| 493 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 494 | DEF_SAMPLE(return new CCPRGeometryView;) |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 495 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 496 | #endif // SK_SUPPORT_GPU |