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