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