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 | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 35 | static int is_quadratic(RenderPass pass) { |
Chris Dalton | 5183e64 | 2018-03-07 12:53:01 -0700 | [diff] [blame] | 36 | return pass == RenderPass::kQuadratics || pass == RenderPass::kQuadraticCorners; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /** |
| 40 | * This sample visualizes the AA bloat geometry generated by the ccpr geometry shaders. It |
| 41 | * increases the AA bloat by 50x and outputs color instead of coverage (coverage=+1 -> green, |
| 42 | * coverage=0 -> black, coverage=-1 -> red). Use the keys 1-7 to cycle through the different |
| 43 | * geometry processors. |
| 44 | */ |
| 45 | class CCPRGeometryView : public SampleView { |
| 46 | public: |
| 47 | CCPRGeometryView() { this->updateGpuData(); } |
| 48 | void onDrawContent(SkCanvas*) override; |
| 49 | |
| 50 | SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override; |
| 51 | bool onClick(SampleView::Click*) override; |
| 52 | bool onQuery(SkEvent* evt) override; |
| 53 | |
| 54 | private: |
| 55 | class Click; |
| 56 | class Op; |
| 57 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 58 | void updateAndInval() { this->updateGpuData(); } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 59 | |
| 60 | void updateGpuData(); |
| 61 | |
Chris Dalton | 5183e64 | 2018-03-07 12:53:01 -0700 | [diff] [blame] | 62 | RenderPass fRenderPass = RenderPass::kTriangles; |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 63 | SkCubicType fCubicType; |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 64 | SkMatrix fCubicKLM; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 65 | |
| 66 | SkPoint fPoints[4] = { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 67 | {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] | 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 | |
| 75 | class CCPRGeometryView::Op : public GrDrawOp { |
| 76 | DEFINE_OP_CLASS_ID |
| 77 | |
| 78 | public: |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 79 | Op(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 | |
| 83 | const char* name() const override { return "[Testing/Sample code] CCPRGeometryView::Op"; } |
| 84 | |
| 85 | private: |
| 86 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
Brian Osman | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 87 | RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*, |
| 88 | GrPixelConfigIsClamped) override { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 89 | return RequiresDstTexture::kNo; |
| 90 | } |
| 91 | bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; } |
| 92 | void onPrepare(GrOpFlushState*) override {} |
| 93 | void onExecute(GrOpFlushState*) override; |
| 94 | |
| 95 | CCPRGeometryView* fView; |
| 96 | |
| 97 | typedef GrDrawOp INHERITED; |
| 98 | }; |
| 99 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 100 | static void draw_klm_line(int w, int h, SkCanvas* canvas, const SkScalar line[3], SkColor color) { |
| 101 | SkPoint p1, p2; |
| 102 | if (SkScalarAbs(line[1]) > SkScalarAbs(line[0])) { |
| 103 | // Draw from vertical edge to vertical edge. |
| 104 | p1 = {0, -line[2] / line[1]}; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 105 | p2 = {(SkScalar)w, (-line[2] - w * line[0]) / line[1]}; |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 106 | } else { |
| 107 | // Draw from horizontal edge to horizontal edge. |
| 108 | p1 = {-line[2] / line[0], 0}; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 109 | p2 = {(-line[2] - h * line[1]) / line[0], (SkScalar)h}; |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | SkPaint linePaint; |
| 113 | linePaint.setColor(color); |
| 114 | linePaint.setAlpha(128); |
| 115 | linePaint.setStyle(SkPaint::kStroke_Style); |
| 116 | linePaint.setStrokeWidth(0); |
| 117 | linePaint.setAntiAlias(true); |
| 118 | canvas->drawLine(p1, p2, linePaint); |
| 119 | } |
| 120 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 121 | void CCPRGeometryView::onDrawContent(SkCanvas* canvas) { |
| 122 | SkAutoCanvasRestore acr(canvas, true); |
| 123 | canvas->setMatrix(SkMatrix::I()); |
| 124 | |
| 125 | SkPath outline; |
| 126 | outline.moveTo(fPoints[0]); |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 127 | if (GrCCCoverageProcessor::RenderPassIsCubic(fRenderPass)) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 128 | outline.cubicTo(fPoints[1], fPoints[2], fPoints[3]); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 129 | } else if (is_quadratic(fRenderPass)) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 130 | outline.quadTo(fPoints[1], fPoints[3]); |
| 131 | } else { |
| 132 | outline.lineTo(fPoints[1]); |
| 133 | outline.lineTo(fPoints[3]); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 134 | outline.close(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 135 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 136 | |
| 137 | SkPaint outlinePaint; |
| 138 | outlinePaint.setColor(0x30000000); |
| 139 | outlinePaint.setStyle(SkPaint::kStroke_Style); |
| 140 | outlinePaint.setStrokeWidth(0); |
| 141 | outlinePaint.setAntiAlias(true); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 142 | canvas->drawPath(outline, outlinePaint); |
| 143 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 144 | #if 0 |
| 145 | SkPaint gridPaint; |
| 146 | gridPaint.setColor(0x10000000); |
| 147 | gridPaint.setStyle(SkPaint::kStroke_Style); |
| 148 | gridPaint.setStrokeWidth(0); |
| 149 | gridPaint.setAntiAlias(true); |
Chris Dalton | a640c49 | 2017-09-11 22:04:03 -0700 | [diff] [blame] | 150 | for (int y = 0; y < this->height(); y += kDebugBloat) { |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 151 | canvas->drawLine(0, y, this->width(), y, gridPaint); |
| 152 | } |
Chris Dalton | a640c49 | 2017-09-11 22:04:03 -0700 | [diff] [blame] | 153 | for (int x = 0; x < this->width(); x += kDebugBloat) { |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 154 | canvas->drawLine(x, 0, x, this->height(), outlinePaint); |
| 155 | } |
| 156 | #endif |
| 157 | |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 158 | SkString caption; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 159 | if (GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext()) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 160 | rtc->priv().testingOnly_addDrawOp(skstd::make_unique<Op>(this)); |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 161 | caption.appendf("RenderPass_%s", GrCCCoverageProcessor::RenderPassName(fRenderPass)); |
| 162 | if (GrCCCoverageProcessor::RenderPassIsCubic(fRenderPass)) { |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 163 | caption.appendf(" (%s)", SkCubicTypeName(fCubicType)); |
| 164 | } |
| 165 | } else { |
| 166 | caption = "Use GPU backend to visualize geometry."; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | SkPaint pointsPaint; |
| 170 | pointsPaint.setColor(SK_ColorBLUE); |
| 171 | pointsPaint.setStrokeWidth(8); |
| 172 | pointsPaint.setAntiAlias(true); |
| 173 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 174 | if (GrCCCoverageProcessor::RenderPassIsCubic(fRenderPass)) { |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 175 | int w = this->width(), h = this->height(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 176 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, fPoints, pointsPaint); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 177 | draw_klm_line(w, h, canvas, &fCubicKLM[0], SK_ColorYELLOW); |
| 178 | draw_klm_line(w, h, canvas, &fCubicKLM[3], SK_ColorBLUE); |
| 179 | draw_klm_line(w, h, canvas, &fCubicKLM[6], SK_ColorRED); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 180 | } else { |
| 181 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, fPoints, pointsPaint); |
| 182 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 1, fPoints + 3, pointsPaint); |
| 183 | } |
| 184 | |
| 185 | SkPaint captionPaint; |
| 186 | captionPaint.setTextSize(20); |
| 187 | captionPaint.setColor(SK_ColorBLACK); |
| 188 | captionPaint.setAntiAlias(true); |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 189 | canvas->drawText(caption.c_str(), caption.size(), 10, 30, captionPaint); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void CCPRGeometryView::updateGpuData() { |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 193 | fTriPointInstances.reset(); |
| 194 | fQuadPointInstances.reset(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 195 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 196 | if (GrCCCoverageProcessor::RenderPassIsCubic(fRenderPass)) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 197 | double t[2], s[2]; |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 198 | fCubicType = GrPathUtils::getCubicKLM(fPoints, &fCubicKLM, t, s); |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 199 | GrCCGeometry geometry; |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 200 | geometry.beginContour(fPoints[0]); |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 201 | geometry.cubicTo(fPoints[1], fPoints[2], fPoints[3], kDebugBloat / 2, kDebugBloat / 2); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 202 | geometry.endContour(); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 203 | int ptsIdx = 0; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 204 | for (GrCCGeometry::Verb verb : geometry.verbs()) { |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 205 | switch (verb) { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 206 | case GrCCGeometry::Verb::kLineTo: |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 207 | ++ptsIdx; |
| 208 | continue; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 209 | case GrCCGeometry::Verb::kMonotonicQuadraticTo: |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 210 | ptsIdx += 2; |
| 211 | continue; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 212 | case GrCCGeometry::Verb::kMonotonicCubicTo: |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 213 | fQuadPointInstances.push_back().set(&geometry.points()[ptsIdx], 0, 0); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 214 | ptsIdx += 3; |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 215 | continue; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 216 | default: |
| 217 | continue; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 218 | } |
| 219 | } |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 220 | } else if (is_quadratic(fRenderPass)) { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 221 | GrCCGeometry geometry; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 222 | geometry.beginContour(fPoints[0]); |
| 223 | geometry.quadraticTo(fPoints[1], fPoints[3]); |
| 224 | geometry.endContour(); |
Chris Dalton | a3e9271 | 2017-12-04 11:45:51 -0700 | [diff] [blame] | 225 | int ptsIdx = 0; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 226 | for (GrCCGeometry::Verb verb : geometry.verbs()) { |
| 227 | if (GrCCGeometry::Verb::kBeginContour == verb || |
| 228 | GrCCGeometry::Verb::kEndOpenContour == verb || |
| 229 | GrCCGeometry::Verb::kEndClosedContour == verb) { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 230 | continue; |
| 231 | } |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 232 | if (GrCCGeometry::Verb::kLineTo == verb) { |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 233 | ++ptsIdx; |
| 234 | continue; |
| 235 | } |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 236 | SkASSERT(GrCCGeometry::Verb::kMonotonicQuadraticTo == verb); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 237 | fTriPointInstances.push_back().set(&geometry.points()[ptsIdx], Sk2f(0, 0)); |
Chris Dalton | a3e9271 | 2017-12-04 11:45:51 -0700 | [diff] [blame] | 238 | ptsIdx += 2; |
Chris Dalton | b072bb6 | 2017-08-07 09:00:46 -0600 | [diff] [blame] | 239 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 240 | } else { |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 241 | 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] | 242 | } |
| 243 | } |
| 244 | |
| 245 | void CCPRGeometryView::Op::onExecute(GrOpFlushState* state) { |
| 246 | GrResourceProvider* rp = state->resourceProvider(); |
| 247 | GrContext* context = state->gpu()->getContext(); |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 248 | GrGLGpu* glGpu = kOpenGL_GrBackend == context->contextPriv().getBackend() |
| 249 | ? static_cast<GrGLGpu*>(state->gpu()) |
| 250 | : nullptr; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 251 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 252 | GrCCCoverageProcessor proc(rp, fView->fRenderPass, |
| 253 | GrCCCoverageProcessor::WindMethod::kCrossProduct); |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 254 | SkDEBUGCODE(proc.enableDebugVisualizations(kDebugBloat)); |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 255 | |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 256 | SkSTArray<1, GrMesh> mesh; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 257 | if (GrCCCoverageProcessor::RenderPassIsCubic(fView->fRenderPass)) { |
| 258 | sk_sp<GrBuffer> instBuff(rp->createBuffer( |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 259 | fView->fQuadPointInstances.count() * sizeof(QuadPointInstance), |
| 260 | kVertex_GrBufferType, kDynamic_GrAccessPattern, |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 261 | GrResourceProvider::kNoPendingIO_Flag | GrResourceProvider::kRequireGpuMemory_Flag, |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 262 | fView->fQuadPointInstances.begin())); |
| 263 | if (!fView->fQuadPointInstances.empty() && instBuff) { |
| 264 | proc.appendMesh(instBuff.get(), fView->fQuadPointInstances.count(), 0, &mesh); |
Chris Dalton | a3e9271 | 2017-12-04 11:45:51 -0700 | [diff] [blame] | 265 | } |
Chris Dalton | a3e9271 | 2017-12-04 11:45:51 -0700 | [diff] [blame] | 266 | } else { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 267 | sk_sp<GrBuffer> instBuff(rp->createBuffer( |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 268 | fView->fTriPointInstances.count() * sizeof(TriPointInstance), kVertex_GrBufferType, |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 269 | kDynamic_GrAccessPattern, |
| 270 | GrResourceProvider::kNoPendingIO_Flag | GrResourceProvider::kRequireGpuMemory_Flag, |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 271 | fView->fTriPointInstances.begin())); |
| 272 | if (!fView->fTriPointInstances.empty() && instBuff) { |
| 273 | proc.appendMesh(instBuff.get(), fView->fTriPointInstances.count(), 0, &mesh); |
Chris Dalton | a3e9271 | 2017-12-04 11:45:51 -0700 | [diff] [blame] | 274 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 275 | } |
| 276 | |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 277 | GrPipeline pipeline(state->drawOpArgs().fProxy, GrPipeline::ScissorState::kDisabled, |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 278 | SkBlendMode::kSrcOver); |
| 279 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 280 | if (glGpu) { |
| 281 | glGpu->handleDirtyContext(); |
| 282 | GR_GL_CALL(glGpu->glInterface(), PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE)); |
| 283 | GR_GL_CALL(glGpu->glInterface(), Enable(GR_GL_LINE_SMOOTH)); |
| 284 | } |
| 285 | |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 286 | if (!mesh.empty()) { |
| 287 | SkASSERT(1 == mesh.count()); |
| 288 | state->rtCommandBuffer()->draw(pipeline, proc, mesh.begin(), nullptr, 1, this->bounds()); |
| 289 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 290 | |
| 291 | if (glGpu) { |
| 292 | context->resetContext(kMisc_GrGLBackendState); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | class CCPRGeometryView::Click : public SampleView::Click { |
| 297 | public: |
| 298 | Click(SkView* target, int ptIdx) : SampleView::Click(target), fPtIdx(ptIdx) {} |
| 299 | |
| 300 | void doClick(SkPoint points[]) { |
| 301 | if (fPtIdx >= 0) { |
| 302 | this->dragPoint(points, fPtIdx); |
| 303 | } else { |
| 304 | for (int i = 0; i < 4; ++i) { |
| 305 | this->dragPoint(points, i); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | private: |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 311 | void dragPoint(SkPoint points[], int idx) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 312 | SkIPoint delta = fICurr - fIPrev; |
| 313 | points[idx] += SkPoint::Make(delta.x(), delta.y()); |
| 314 | } |
| 315 | |
| 316 | int fPtIdx; |
| 317 | }; |
| 318 | |
| 319 | SkView::Click* CCPRGeometryView::onFindClickHandler(SkScalar x, SkScalar y, unsigned) { |
| 320 | for (int i = 0; i < 4; ++i) { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 321 | if (!GrCCCoverageProcessor::RenderPassIsCubic(fRenderPass) && 2 == i) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 322 | continue; |
| 323 | } |
| 324 | if (fabs(x - fPoints[i].x()) < 20 && fabsf(y - fPoints[i].y()) < 20) { |
| 325 | return new Click(this, i); |
| 326 | } |
| 327 | } |
| 328 | return new Click(this, -1); |
| 329 | } |
| 330 | |
| 331 | bool CCPRGeometryView::onClick(SampleView::Click* click) { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 332 | Click* myClick = (Click*)click; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 333 | myClick->doClick(fPoints); |
| 334 | this->updateAndInval(); |
| 335 | return true; |
| 336 | } |
| 337 | |
| 338 | bool CCPRGeometryView::onQuery(SkEvent* evt) { |
| 339 | if (SampleCode::TitleQ(*evt)) { |
| 340 | SampleCode::TitleR(evt, "CCPRGeometry"); |
| 341 | return true; |
| 342 | } |
| 343 | SkUnichar unichar; |
| 344 | if (SampleCode::CharQ(*evt, &unichar)) { |
Chris Dalton | 5183e64 | 2018-03-07 12:53:01 -0700 | [diff] [blame] | 345 | if (unichar >= '1' && unichar <= '6') { |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 346 | fRenderPass = RenderPass(unichar - '1'); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 347 | this->updateAndInval(); |
| 348 | return true; |
| 349 | } |
| 350 | if (unichar == 'D') { |
| 351 | SkDebugf(" SkPoint fPoints[4] = {\n"); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 352 | SkDebugf(" {%ff, %ff},\n", fPoints[0].x(), fPoints[0].y()); |
| 353 | SkDebugf(" {%ff, %ff},\n", fPoints[1].x(), fPoints[1].y()); |
| 354 | SkDebugf(" {%ff, %ff},\n", fPoints[2].x(), fPoints[2].y()); |
| 355 | SkDebugf(" {%ff, %ff}\n", fPoints[3].x(), fPoints[3].y()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 356 | SkDebugf(" };\n"); |
| 357 | return true; |
| 358 | } |
| 359 | } |
| 360 | return this->INHERITED::onQuery(evt); |
| 361 | } |
| 362 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 363 | DEF_SAMPLE(return new CCPRGeometryView;) |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 364 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 365 | #endif // SK_SUPPORT_GPU |