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 | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 31 | using RenderPass = GrCCCoverageProcessor::RenderPass; |
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 | 5183e64 | 2018-03-07 12:53:01 -0700 | [diff] [blame] | 59 | RenderPass fRenderPass = RenderPass::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 | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 66 | SkTArray<TriPointInstance> fTriPointInstances; |
| 67 | SkTArray<QuadPointInstance> fQuadPointInstances; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 68 | |
| 69 | typedef SampleView INHERITED; |
| 70 | }; |
| 71 | |
Chris Dalton | 8d38a7f | 2018-03-19 14:16:44 -0600 | [diff] [blame] | 72 | class CCPRGeometryView::DrawCoverageCountOp : public GrDrawOp { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 73 | DEFINE_OP_CLASS_ID |
| 74 | |
| 75 | public: |
Chris Dalton | 8d38a7f | 2018-03-19 14:16:44 -0600 | [diff] [blame] | 76 | DrawCoverageCountOp(CCPRGeometryView* view) : INHERITED(ClassID()), fView(view) { |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 77 | this->setBounds(SkRectPriv::MakeLargest(), GrOp::HasAABloat::kNo, GrOp::IsZeroArea::kNo); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 78 | } |
| 79 | |
Chris Dalton | 8d38a7f | 2018-03-19 14:16:44 -0600 | [diff] [blame] | 80 | const char* name() const override { |
| 81 | return "[Testing/Sample code] CCPRGeometryView::DrawCoverageCountOp"; |
| 82 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 83 | |
| 84 | private: |
| 85 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
Brian Osman | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 86 | RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*, |
| 87 | GrPixelConfigIsClamped) override { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 88 | return RequiresDstTexture::kNo; |
| 89 | } |
| 90 | bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; } |
| 91 | void onPrepare(GrOpFlushState*) override {} |
| 92 | void onExecute(GrOpFlushState*) override; |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 93 | void drawRenderPass(GrOpFlushState*, RenderPass); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 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 | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 152 | if (RenderPass::kCubics == fRenderPass) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 153 | outline.cubicTo(fPoints[1], fPoints[2], fPoints[3]); |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 154 | } else if (RenderPass::kQuadratics == fRenderPass) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 155 | outline.quadTo(fPoints[1], fPoints[3]); |
| 156 | } else { |
| 157 | outline.lineTo(fPoints[1]); |
| 158 | outline.lineTo(fPoints[3]); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 159 | outline.close(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 160 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 161 | |
| 162 | SkPaint outlinePaint; |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 163 | outlinePaint.setColor(0x80ffffff); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 164 | outlinePaint.setStyle(SkPaint::kStroke_Style); |
| 165 | outlinePaint.setStrokeWidth(0); |
| 166 | outlinePaint.setAntiAlias(true); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 167 | canvas->drawPath(outline, outlinePaint); |
| 168 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 169 | #if 0 |
| 170 | SkPaint gridPaint; |
| 171 | gridPaint.setColor(0x10000000); |
| 172 | gridPaint.setStyle(SkPaint::kStroke_Style); |
| 173 | gridPaint.setStrokeWidth(0); |
| 174 | gridPaint.setAntiAlias(true); |
Chris Dalton | a640c49 | 2017-09-11 22:04:03 -0700 | [diff] [blame] | 175 | for (int y = 0; y < this->height(); y += kDebugBloat) { |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 176 | canvas->drawLine(0, y, this->width(), y, gridPaint); |
| 177 | } |
Chris Dalton | a640c49 | 2017-09-11 22:04:03 -0700 | [diff] [blame] | 178 | for (int x = 0; x < this->width(); x += kDebugBloat) { |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 179 | canvas->drawLine(x, 0, x, this->height(), outlinePaint); |
| 180 | } |
| 181 | #endif |
| 182 | |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 183 | SkString caption; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 184 | if (GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext()) { |
Chris Dalton | 8d38a7f | 2018-03-19 14:16:44 -0600 | [diff] [blame] | 185 | // Render coverage count. |
| 186 | GrContext* ctx = canvas->getGrContext(); |
| 187 | SkASSERT(ctx); |
| 188 | sk_sp<GrRenderTargetContext> ccbuff = |
| 189 | ctx->contextPriv().makeDeferredRenderTargetContext(SkBackingFit::kApprox, |
| 190 | this->width(), this->height(), |
| 191 | kAlpha_half_GrPixelConfig, |
| 192 | nullptr); |
| 193 | SkASSERT(ccbuff); |
| 194 | ccbuff->clear(nullptr, 0, GrRenderTargetContext::CanClearFullscreen::kYes); |
| 195 | ccbuff->priv().testingOnly_addDrawOp(skstd::make_unique<DrawCoverageCountOp>(this)); |
| 196 | |
| 197 | // Visualize coverage count in main canvas. |
| 198 | GrPaint paint; |
| 199 | paint.addColorFragmentProcessor( |
| 200 | GrSimpleTextureEffect::Make(sk_ref_sp(ccbuff->asTextureProxy()), SkMatrix::I())); |
| 201 | paint.addColorFragmentProcessor( |
| 202 | skstd::make_unique<VisualizeCoverageCountFP>()); |
| 203 | paint.setPorterDuffXPFactory(SkBlendMode::kSrcOver); |
| 204 | rtc->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), |
| 205 | SkRect::MakeIWH(this->width(), this->height())); |
| 206 | |
| 207 | // Add label. |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 208 | caption.appendf("RenderPass_%s", GrCCCoverageProcessor::RenderPassName(fRenderPass)); |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 209 | if (RenderPass::kCubics == fRenderPass) { |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 210 | caption.appendf(" (%s)", SkCubicTypeName(fCubicType)); |
| 211 | } |
| 212 | } else { |
| 213 | caption = "Use GPU backend to visualize geometry."; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | SkPaint pointsPaint; |
| 217 | pointsPaint.setColor(SK_ColorBLUE); |
| 218 | pointsPaint.setStrokeWidth(8); |
| 219 | pointsPaint.setAntiAlias(true); |
| 220 | |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 221 | if (RenderPass::kCubics == fRenderPass) { |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 222 | int w = this->width(), h = this->height(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 223 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, fPoints, pointsPaint); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 224 | draw_klm_line(w, h, canvas, &fCubicKLM[0], SK_ColorYELLOW); |
| 225 | draw_klm_line(w, h, canvas, &fCubicKLM[3], SK_ColorBLUE); |
| 226 | draw_klm_line(w, h, canvas, &fCubicKLM[6], SK_ColorRED); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 227 | } else { |
| 228 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, fPoints, pointsPaint); |
| 229 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 1, fPoints + 3, pointsPaint); |
| 230 | } |
| 231 | |
| 232 | SkPaint captionPaint; |
| 233 | captionPaint.setTextSize(20); |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 234 | captionPaint.setColor(SK_ColorWHITE); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 235 | captionPaint.setAntiAlias(true); |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 236 | canvas->drawText(caption.c_str(), caption.size(), 10, 30, captionPaint); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void CCPRGeometryView::updateGpuData() { |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 240 | fTriPointInstances.reset(); |
| 241 | fQuadPointInstances.reset(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 242 | |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 243 | if (RenderPass::kCubics == fRenderPass) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 244 | double t[2], s[2]; |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 245 | fCubicType = GrPathUtils::getCubicKLM(fPoints, &fCubicKLM, t, s); |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 246 | GrCCGeometry geometry; |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 247 | geometry.beginContour(fPoints[0]); |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 248 | geometry.cubicTo(fPoints[1], fPoints[2], fPoints[3], kDebugBloat / 2, kDebugBloat / 2); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 249 | geometry.endContour(); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 250 | int ptsIdx = 0; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 251 | for (GrCCGeometry::Verb verb : geometry.verbs()) { |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 252 | switch (verb) { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 253 | case GrCCGeometry::Verb::kLineTo: |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 254 | ++ptsIdx; |
| 255 | continue; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 256 | case GrCCGeometry::Verb::kMonotonicQuadraticTo: |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 257 | ptsIdx += 2; |
| 258 | continue; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 259 | case GrCCGeometry::Verb::kMonotonicCubicTo: |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 260 | fQuadPointInstances.push_back().set(&geometry.points()[ptsIdx], 0, 0); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 261 | ptsIdx += 3; |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 262 | continue; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 263 | default: |
| 264 | continue; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 265 | } |
| 266 | } |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 267 | } else if (RenderPass::kQuadratics == fRenderPass) { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 268 | GrCCGeometry geometry; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 269 | geometry.beginContour(fPoints[0]); |
| 270 | geometry.quadraticTo(fPoints[1], fPoints[3]); |
| 271 | geometry.endContour(); |
Chris Dalton | a3e9271 | 2017-12-04 11:45:51 -0700 | [diff] [blame] | 272 | int ptsIdx = 0; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 273 | for (GrCCGeometry::Verb verb : geometry.verbs()) { |
| 274 | if (GrCCGeometry::Verb::kBeginContour == verb || |
| 275 | GrCCGeometry::Verb::kEndOpenContour == verb || |
| 276 | GrCCGeometry::Verb::kEndClosedContour == verb) { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 277 | continue; |
| 278 | } |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 279 | if (GrCCGeometry::Verb::kLineTo == verb) { |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 280 | ++ptsIdx; |
| 281 | continue; |
| 282 | } |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 283 | SkASSERT(GrCCGeometry::Verb::kMonotonicQuadraticTo == verb); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 284 | fTriPointInstances.push_back().set(&geometry.points()[ptsIdx], Sk2f(0, 0)); |
Chris Dalton | a3e9271 | 2017-12-04 11:45:51 -0700 | [diff] [blame] | 285 | ptsIdx += 2; |
Chris Dalton | b072bb6 | 2017-08-07 09:00:46 -0600 | [diff] [blame] | 286 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 287 | } else { |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 288 | 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] | 289 | } |
| 290 | } |
| 291 | |
Chris Dalton | 8d38a7f | 2018-03-19 14:16:44 -0600 | [diff] [blame] | 292 | void CCPRGeometryView::DrawCoverageCountOp::onExecute(GrOpFlushState* state) { |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 293 | this->drawRenderPass(state, fView->fRenderPass); |
| 294 | |
| 295 | RenderPass cornerPass = RenderPass((int)fView->fRenderPass + 1); |
| 296 | if (GrCCCoverageProcessor::DoesRenderPass(cornerPass, state->caps())) { |
| 297 | this->drawRenderPass(state, cornerPass); |
| 298 | } |
| 299 | } |
| 300 | |
Chris Dalton | 8d38a7f | 2018-03-19 14:16:44 -0600 | [diff] [blame] | 301 | void CCPRGeometryView::DrawCoverageCountOp::drawRenderPass(GrOpFlushState* state, |
| 302 | RenderPass renderPass) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 303 | GrResourceProvider* rp = state->resourceProvider(); |
| 304 | GrContext* context = state->gpu()->getContext(); |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 305 | GrGLGpu* glGpu = kOpenGL_GrBackend == context->contextPriv().getBackend() |
| 306 | ? static_cast<GrGLGpu*>(state->gpu()) |
| 307 | : nullptr; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 308 | |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 309 | GrCCCoverageProcessor proc(rp, renderPass, GrCCCoverageProcessor::WindMethod::kCrossProduct); |
Chris Dalton | 8d38a7f | 2018-03-19 14:16:44 -0600 | [diff] [blame] | 310 | SkDEBUGCODE(proc.enableDebugBloat(kDebugBloat)); |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 311 | |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 312 | SkSTArray<1, GrMesh> mesh; |
Chris Dalton | 52076d1 | 2018-03-21 12:14:10 -0600 | [diff] [blame] | 313 | if (GrCCCoverageProcessor::RenderPassIsCubic(renderPass)) { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 314 | sk_sp<GrBuffer> instBuff(rp->createBuffer( |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 315 | fView->fQuadPointInstances.count() * sizeof(QuadPointInstance), |
| 316 | kVertex_GrBufferType, kDynamic_GrAccessPattern, |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 317 | GrResourceProvider::kNoPendingIO_Flag | GrResourceProvider::kRequireGpuMemory_Flag, |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 318 | fView->fQuadPointInstances.begin())); |
| 319 | if (!fView->fQuadPointInstances.empty() && instBuff) { |
| 320 | proc.appendMesh(instBuff.get(), fView->fQuadPointInstances.count(), 0, &mesh); |
Chris Dalton | a3e9271 | 2017-12-04 11:45:51 -0700 | [diff] [blame] | 321 | } |
Chris Dalton | a3e9271 | 2017-12-04 11:45:51 -0700 | [diff] [blame] | 322 | } else { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 323 | sk_sp<GrBuffer> instBuff(rp->createBuffer( |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 324 | fView->fTriPointInstances.count() * sizeof(TriPointInstance), kVertex_GrBufferType, |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 325 | kDynamic_GrAccessPattern, |
| 326 | GrResourceProvider::kNoPendingIO_Flag | GrResourceProvider::kRequireGpuMemory_Flag, |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 327 | fView->fTriPointInstances.begin())); |
| 328 | if (!fView->fTriPointInstances.empty() && instBuff) { |
| 329 | proc.appendMesh(instBuff.get(), fView->fTriPointInstances.count(), 0, &mesh); |
Chris Dalton | a3e9271 | 2017-12-04 11:45:51 -0700 | [diff] [blame] | 330 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 331 | } |
| 332 | |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 333 | GrPipeline pipeline(state->drawOpArgs().fProxy, GrPipeline::ScissorState::kDisabled, |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 334 | SkBlendMode::kPlus); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 335 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 336 | if (glGpu) { |
| 337 | glGpu->handleDirtyContext(); |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 338 | // 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] | 339 | GR_GL_CALL(glGpu->glInterface(), Enable(GR_GL_LINE_SMOOTH)); |
| 340 | } |
| 341 | |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 342 | if (!mesh.empty()) { |
| 343 | SkASSERT(1 == mesh.count()); |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 344 | GrGpuRTCommandBuffer* cmdBuff = state->rtCommandBuffer(); |
| 345 | cmdBuff->draw(pipeline, proc, mesh.begin(), nullptr, 1, this->bounds()); |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 346 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 347 | |
| 348 | if (glGpu) { |
| 349 | context->resetContext(kMisc_GrGLBackendState); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | class CCPRGeometryView::Click : public SampleView::Click { |
| 354 | public: |
| 355 | Click(SkView* target, int ptIdx) : SampleView::Click(target), fPtIdx(ptIdx) {} |
| 356 | |
| 357 | void doClick(SkPoint points[]) { |
| 358 | if (fPtIdx >= 0) { |
| 359 | this->dragPoint(points, fPtIdx); |
| 360 | } else { |
| 361 | for (int i = 0; i < 4; ++i) { |
| 362 | this->dragPoint(points, i); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | private: |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 368 | void dragPoint(SkPoint points[], int idx) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 369 | SkIPoint delta = fICurr - fIPrev; |
| 370 | points[idx] += SkPoint::Make(delta.x(), delta.y()); |
| 371 | } |
| 372 | |
| 373 | int fPtIdx; |
| 374 | }; |
| 375 | |
| 376 | SkView::Click* CCPRGeometryView::onFindClickHandler(SkScalar x, SkScalar y, unsigned) { |
| 377 | for (int i = 0; i < 4; ++i) { |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 378 | if (RenderPass::kCubics != fRenderPass && 2 == i) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 379 | continue; |
| 380 | } |
| 381 | if (fabs(x - fPoints[i].x()) < 20 && fabsf(y - fPoints[i].y()) < 20) { |
| 382 | return new Click(this, i); |
| 383 | } |
| 384 | } |
| 385 | return new Click(this, -1); |
| 386 | } |
| 387 | |
| 388 | bool CCPRGeometryView::onClick(SampleView::Click* click) { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 389 | Click* myClick = (Click*)click; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 390 | myClick->doClick(fPoints); |
| 391 | this->updateAndInval(); |
| 392 | return true; |
| 393 | } |
| 394 | |
| 395 | bool CCPRGeometryView::onQuery(SkEvent* evt) { |
| 396 | if (SampleCode::TitleQ(*evt)) { |
| 397 | SampleCode::TitleR(evt, "CCPRGeometry"); |
| 398 | return true; |
| 399 | } |
| 400 | SkUnichar unichar; |
| 401 | if (SampleCode::CharQ(*evt, &unichar)) { |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 402 | if (unichar >= '1' && unichar <= '3') { |
| 403 | fRenderPass = RenderPass((unichar - '1') * 2); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 404 | this->updateAndInval(); |
| 405 | return true; |
| 406 | } |
| 407 | if (unichar == 'D') { |
| 408 | SkDebugf(" SkPoint fPoints[4] = {\n"); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 409 | SkDebugf(" {%ff, %ff},\n", fPoints[0].x(), fPoints[0].y()); |
| 410 | SkDebugf(" {%ff, %ff},\n", fPoints[1].x(), fPoints[1].y()); |
| 411 | SkDebugf(" {%ff, %ff},\n", fPoints[2].x(), fPoints[2].y()); |
| 412 | SkDebugf(" {%ff, %ff}\n", fPoints[3].x(), fPoints[3].y()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 413 | SkDebugf(" };\n"); |
| 414 | return true; |
| 415 | } |
| 416 | } |
| 417 | return this->INHERITED::onQuery(evt); |
| 418 | } |
| 419 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 420 | DEF_SAMPLE(return new CCPRGeometryView;) |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 421 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 422 | #endif // SK_SUPPORT_GPU |