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