Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC. |
| 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 "include/core/SkCanvas.h" |
| 9 | #include "samplecode/Sample.h" |
Chris Dalton | 0363f05 | 2020-11-24 13:43:11 -0700 | [diff] [blame] | 10 | #include "src/core/SkGeometry.h" |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 11 | #include "src/core/SkPathPriv.h" |
| 12 | #include "tools/ToolUtils.h" |
| 13 | |
| 14 | #if SK_SUPPORT_GPU |
| 15 | |
Robert Phillips | 30ebcf7 | 2020-07-09 13:25:17 -0400 | [diff] [blame] | 16 | #include "include/gpu/GrRecordingContext.h" |
Brian Salomon | 8f7d953 | 2020-12-23 09:16:59 -0500 | [diff] [blame] | 17 | #include "src/core/SkCanvasPriv.h" |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 18 | #include "src/gpu/GrClip.h" |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 19 | #include "src/gpu/GrMemoryPool.h" |
Robert Phillips | 30ebcf7 | 2020-07-09 13:25:17 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrRecordingContextPriv.h" |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 21 | #include "src/gpu/GrSurfaceDrawContext.h" |
Chris Dalton | b03f4a1 | 2021-01-27 17:45:52 -0700 | [diff] [blame] | 22 | #include "src/gpu/tessellate/GrTessellatingStencilFillOp.h" |
Chris Dalton | 0363f05 | 2020-11-24 13:43:11 -0700 | [diff] [blame] | 23 | #include "src/gpu/tessellate/GrWangsFormula.h" |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 24 | |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 25 | static float kConicWeight = .5; |
| 26 | |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 27 | // This sample enables wireframe and visualizes the triangulation generated by |
| 28 | // GrTessellateWedgeShader. |
Chris Dalton | 0d0758e | 2020-05-29 10:51:08 -0600 | [diff] [blame] | 29 | class TessellatedWedge : public Sample { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 30 | public: |
Chris Dalton | 0d0758e | 2020-05-29 10:51:08 -0600 | [diff] [blame] | 31 | TessellatedWedge() { |
Chris Dalton | f9aea7f | 2020-01-21 11:19:26 -0700 | [diff] [blame] | 32 | #if 0 |
| 33 | fPath.moveTo(1, 0); |
| 34 | int numSides = 32 * 3; |
| 35 | for (int i = 1; i < numSides; ++i) { |
| 36 | float theta = 2*3.1415926535897932384626433832785 * i / numSides; |
| 37 | fPath.lineTo(std::cos(theta), std::sin(theta)); |
| 38 | } |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 39 | fPath.transform(SkMatrix::Scale(200, 200)); |
| 40 | fPath.transform(SkMatrix::Translate(300, 300)); |
Chris Dalton | f9aea7f | 2020-01-21 11:19:26 -0700 | [diff] [blame] | 41 | #else |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 42 | fPath.moveTo(100, 300); |
| 43 | fPath.conicTo(300, 100, 500, 300, kConicWeight); |
| 44 | fPath.cubicTo(433, 366, 366, 433, 300, 500); |
Chris Dalton | f9aea7f | 2020-01-21 11:19:26 -0700 | [diff] [blame] | 45 | #endif |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | private: |
| 49 | void onDrawContent(SkCanvas*) override; |
| 50 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override; |
| 51 | bool onClick(Sample::Click*) override; |
| 52 | bool onChar(SkUnichar) override; |
| 53 | |
| 54 | SkString name() override { return SkString("TessellatedWedge"); } |
| 55 | |
| 56 | SkMatrix fLastViewMatrix = SkMatrix::I(); |
| 57 | SkPath fPath; |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 58 | GrTessellationPathRenderer::OpFlags fOpFlags = GrTessellationPathRenderer::OpFlags::kWireframe; |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 59 | |
| 60 | class Click; |
| 61 | }; |
| 62 | |
Chris Dalton | 0d0758e | 2020-05-29 10:51:08 -0600 | [diff] [blame] | 63 | void TessellatedWedge::onDrawContent(SkCanvas* canvas) { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 64 | canvas->clear(SK_ColorBLACK); |
| 65 | |
Robert Phillips | 30ebcf7 | 2020-07-09 13:25:17 -0400 | [diff] [blame] | 66 | auto ctx = canvas->recordingContext(); |
Brian Salomon | 8f7d953 | 2020-12-23 09:16:59 -0500 | [diff] [blame] | 67 | GrSurfaceDrawContext* sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(canvas); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 68 | |
| 69 | SkString error; |
Brian Salomon | 8f7d953 | 2020-12-23 09:16:59 -0500 | [diff] [blame] | 70 | if (!sdc || !ctx) { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 71 | error = "GPU Only."; |
Chris Dalton | 0d0758e | 2020-05-29 10:51:08 -0600 | [diff] [blame] | 72 | } else if (!ctx->priv().caps()->drawInstancedSupport()) { |
| 73 | error = "Instanced rendering not supported."; |
Brian Salomon | 8f7d953 | 2020-12-23 09:16:59 -0500 | [diff] [blame] | 74 | } else if (sdc->numSamples() == 1 && !ctx->priv().caps()->mixedSamplesSupport()) { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 75 | error = "MSAA/mixed samples only."; |
| 76 | } |
| 77 | if (!error.isEmpty()) { |
| 78 | SkFont font(nullptr, 20); |
| 79 | SkPaint captionPaint; |
| 80 | captionPaint.setColor(SK_ColorWHITE); |
| 81 | canvas->drawString(error.c_str(), 10, 30, font, captionPaint); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | GrPaint paint; |
| 86 | paint.setColor4f({1,0,1,1}); |
| 87 | |
| 88 | GrAAType aa; |
Brian Salomon | 8f7d953 | 2020-12-23 09:16:59 -0500 | [diff] [blame] | 89 | if (sdc->numSamples() > 1) { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 90 | aa = GrAAType::kMSAA; |
Brian Salomon | 8f7d953 | 2020-12-23 09:16:59 -0500 | [diff] [blame] | 91 | } else if (sdc->asRenderTargetProxy()->canUseMixedSamples(*ctx->priv().caps())) { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 92 | aa = GrAAType::kCoverage; |
| 93 | } else { |
| 94 | aa = GrAAType::kNone; |
| 95 | } |
| 96 | |
Chris Dalton | b03f4a1 | 2021-01-27 17:45:52 -0700 | [diff] [blame] | 97 | sdc->addDrawOp(GrOp::Make<GrTessellatingStencilFillOp>(ctx, canvas->getTotalMatrix(), fPath, |
| 98 | std::move(paint), aa, fOpFlags)); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 99 | |
| 100 | // Draw the path points. |
| 101 | SkPaint pointsPaint; |
| 102 | pointsPaint.setColor(SK_ColorBLUE); |
| 103 | pointsPaint.setStrokeWidth(8); |
Chris Dalton | f9aea7f | 2020-01-21 11:19:26 -0700 | [diff] [blame] | 104 | SkPath devPath = fPath; |
| 105 | devPath.transform(canvas->getTotalMatrix()); |
| 106 | { |
| 107 | SkAutoCanvasRestore acr(canvas, true); |
| 108 | canvas->setMatrix(SkMatrix::I()); |
| 109 | canvas->drawPoints(SkCanvas::kPoints_PointMode, devPath.countPoints(), |
| 110 | SkPathPriv::PointData(devPath), pointsPaint); |
| 111 | } |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 112 | |
| 113 | fLastViewMatrix = canvas->getTotalMatrix(); |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 114 | |
| 115 | |
| 116 | SkString caption; |
| 117 | caption.printf("w=%f (=/- and +/_ to change)", kConicWeight); |
| 118 | SkFont font(nullptr, 20); |
| 119 | SkPaint captionPaint; |
| 120 | captionPaint.setColor(SK_ColorWHITE); |
| 121 | canvas->drawString(caption, 10, 30, font, captionPaint); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Chris Dalton | 0d0758e | 2020-05-29 10:51:08 -0600 | [diff] [blame] | 124 | class TessellatedWedge::Click : public Sample::Click { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 125 | public: |
| 126 | Click(int ptIdx) : fPtIdx(ptIdx) {} |
| 127 | |
| 128 | void doClick(SkPath* path) { |
| 129 | if (fPtIdx >= 0) { |
| 130 | SkPoint pt = path->getPoint(fPtIdx); |
Chris Dalton | 8d3eb24 | 2020-05-04 10:43:33 -0600 | [diff] [blame] | 131 | SkPathPriv::UpdatePathPoint(path, fPtIdx, pt + fCurr - fPrev); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 132 | } else { |
| 133 | path->transform( |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 134 | SkMatrix::Translate(fCurr.x() - fPrev.x(), fCurr.y() - fPrev.y()), path); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | private: |
| 139 | int fPtIdx; |
| 140 | }; |
| 141 | |
Chris Dalton | 0d0758e | 2020-05-29 10:51:08 -0600 | [diff] [blame] | 142 | Sample::Click* TessellatedWedge::onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 143 | const SkPoint* pts = SkPathPriv::PointData(fPath); |
| 144 | float fuzz = 20 / fLastViewMatrix.getMaxScale(); |
| 145 | for (int i = 0; i < fPath.countPoints(); ++i) { |
| 146 | SkPoint screenPoint = pts[i]; |
| 147 | if (fabs(x - screenPoint.x()) < fuzz && fabsf(y - screenPoint.y()) < fuzz) { |
| 148 | return new Click(i); |
| 149 | } |
| 150 | } |
| 151 | return new Click(-1); |
| 152 | } |
| 153 | |
Chris Dalton | 0363f05 | 2020-11-24 13:43:11 -0700 | [diff] [blame] | 154 | static float find_conic_max_error(const SkConic& conic, int numChops) { |
| 155 | if (numChops > 1) { |
| 156 | int leftChops = numChops / 2; |
| 157 | SkConic halves[2]; |
| 158 | if (conic.chopAt((float)leftChops/numChops, halves)) { |
| 159 | return std::max(find_conic_max_error(halves[0], leftChops), |
| 160 | find_conic_max_error(halves[1], numChops - leftChops)); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | const SkPoint* p = conic.fPts; |
| 165 | float w = conic.fW; |
| 166 | SkVector n = {p[2].fY - p[0].fY, p[0].fX - p[2].fX}; |
| 167 | float h1 = (p[1] - p[0]).dot(n) / n.length(); |
| 168 | float h = h1*w / (1 + w); |
| 169 | return h; |
| 170 | } |
| 171 | |
| 172 | static void dump_conic_max_errors(const SkPath& path) { |
| 173 | SkPath path_; |
| 174 | for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) { |
| 175 | if (verb == SkPathVerb::kConic) { |
| 176 | int n = GrWangsFormula::quadratic(4, pts); |
| 177 | float err = find_conic_max_error(SkConic(pts, *w), n); |
| 178 | SkDebugf("CONIC MAX ERROR: %f\n", err); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
Chris Dalton | 0d0758e | 2020-05-29 10:51:08 -0600 | [diff] [blame] | 183 | bool TessellatedWedge::onClick(Sample::Click* click) { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 184 | Click* myClick = (Click*)click; |
| 185 | myClick->doClick(&fPath); |
Chris Dalton | 0363f05 | 2020-11-24 13:43:11 -0700 | [diff] [blame] | 186 | dump_conic_max_errors(fPath); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 187 | return true; |
| 188 | } |
| 189 | |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 190 | static SkPath update_weight(const SkPath& path) { |
| 191 | SkPath path_; |
| 192 | for (auto [verb, pts, _] : SkPathPriv::Iterate(path)) { |
| 193 | switch (verb) { |
| 194 | case SkPathVerb::kMove: |
| 195 | path_.moveTo(pts[0]); |
| 196 | break; |
| 197 | case SkPathVerb::kLine: |
| 198 | path_.lineTo(pts[1]); |
| 199 | break; |
| 200 | case SkPathVerb::kQuad: |
| 201 | path_.quadTo(pts[1], pts[2]); |
| 202 | break; |
| 203 | case SkPathVerb::kCubic: |
| 204 | path_.cubicTo(pts[1], pts[2], pts[3]); |
| 205 | break; |
| 206 | case SkPathVerb::kConic: |
| 207 | path_.conicTo(pts[1], pts[2], (kConicWeight != 1) ? kConicWeight : .99f); |
| 208 | break; |
| 209 | default: |
| 210 | SkUNREACHABLE; |
| 211 | } |
| 212 | } |
Chris Dalton | 0363f05 | 2020-11-24 13:43:11 -0700 | [diff] [blame] | 213 | dump_conic_max_errors(path); |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 214 | return path_; |
| 215 | } |
| 216 | |
Chris Dalton | 0d0758e | 2020-05-29 10:51:08 -0600 | [diff] [blame] | 217 | bool TessellatedWedge::onChar(SkUnichar unichar) { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 218 | switch (unichar) { |
| 219 | case 'w': |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 220 | fOpFlags = (GrTessellationPathRenderer::OpFlags)( |
| 221 | (int)fOpFlags ^ (int)GrTessellationPathRenderer::OpFlags::kWireframe); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 222 | return true; |
| 223 | case 'D': { |
| 224 | fPath.dump(); |
| 225 | return true; |
| 226 | } |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 227 | case '+': |
| 228 | kConicWeight *= 2; |
| 229 | fPath = update_weight(fPath); |
| 230 | return true; |
| 231 | case '=': |
| 232 | kConicWeight *= 5/4.f; |
| 233 | fPath = update_weight(fPath); |
| 234 | return true; |
| 235 | case '_': |
| 236 | kConicWeight *= .5f; |
| 237 | fPath = update_weight(fPath); |
| 238 | return true; |
| 239 | case '-': |
| 240 | kConicWeight *= 4/5.f; |
| 241 | fPath = update_weight(fPath); |
| 242 | return true; |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 243 | } |
| 244 | return false; |
| 245 | } |
| 246 | |
Chris Dalton | 0d0758e | 2020-05-29 10:51:08 -0600 | [diff] [blame] | 247 | Sample* MakeTessellatedWedgeSample() { return new TessellatedWedge; } |
| 248 | static SampleRegistry gTessellatedWedgeSample(MakeTessellatedWedgeSample); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 249 | |
| 250 | #endif // SK_SUPPORT_GPU |