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