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