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