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