epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkColorFilter.h" |
| 10 | #include "include/core/SkColorPriv.h" |
Hal Canary | 5f4a754 | 2019-07-03 22:17:24 -0400 | [diff] [blame] | 11 | #include "include/core/SkContourMeasure.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkGraphics.h" |
| 13 | #include "include/core/SkPath.h" |
| 14 | #include "include/core/SkRegion.h" |
| 15 | #include "include/core/SkShader.h" |
Hal Canary | 5f4a754 | 2019-07-03 22:17:24 -0400 | [diff] [blame] | 16 | #include "include/core/SkStream.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/core/SkTime.h" |
| 18 | #include "include/core/SkTypeface.h" |
| 19 | #include "include/core/SkVertices.h" |
| 20 | #include "include/effects/SkGradientShader.h" |
Hal Canary | 5f4a754 | 2019-07-03 22:17:24 -0400 | [diff] [blame] | 21 | #include "include/effects/SkOpPathEffect.h" |
| 22 | #include "include/private/SkTDArray.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "include/utils/SkRandom.h" |
| 24 | #include "samplecode/DecodeFile.h" |
| 25 | #include "samplecode/Sample.h" |
Hal Canary | 5f4a754 | 2019-07-03 22:17:24 -0400 | [diff] [blame] | 26 | #include "src/core/SkGeometry.h" |
| 27 | #include "src/core/SkOSFile.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "src/utils/SkUTF.h" |
Hal Canary | 5f4a754 | 2019-07-03 22:17:24 -0400 | [diff] [blame] | 29 | #include "tools/Resources.h" |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 30 | #include "tools/timer/TimeUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | |
Hal Canary | 5f4a754 | 2019-07-03 22:17:24 -0400 | [diff] [blame] | 32 | namespace { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 33 | static sk_sp<SkShader> make_shader0(SkIPoint* size) { |
Hal Canary | 5f4a754 | 2019-07-03 22:17:24 -0400 | [diff] [blame] | 34 | SkBitmap bm; |
| 35 | decode_file(GetResourceAsData("images/dog.jpg"), &bm); |
| 36 | *size = SkIPoint{bm.width(), bm.height()}; |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 37 | return bm.makeShader(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | } |
| 39 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 40 | static sk_sp<SkShader> make_shader1(const SkIPoint& size) { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 41 | SkPoint pts[] = { { 0, 0, }, |
| 42 | { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 44 | return SkGradientShader::MakeLinear(pts, colors, nullptr, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 45 | SK_ARRAY_COUNT(colors), SkTileMode::kMirror); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | } |
| 47 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | class Patch { |
| 49 | public: |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 50 | Patch() { sk_bzero(fPts, sizeof(fPts)); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | ~Patch() {} |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 52 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | void setPatch(const SkPoint pts[12]) { |
| 54 | memcpy(fPts, pts, 12 * sizeof(SkPoint)); |
| 55 | fPts[12] = pts[0]; // the last shall be first |
| 56 | } |
| 57 | void setBounds(int w, int h) { fW = w; fH = h; } |
| 58 | |
| 59 | void draw(SkCanvas*, const SkPaint&, int segsU, int segsV, |
| 60 | bool doTextures, bool doColors); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 61 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | private: |
| 63 | SkPoint fPts[13]; |
| 64 | int fW, fH; |
| 65 | }; |
| 66 | |
| 67 | static void eval_patch_edge(const SkPoint cubic[], SkPoint samples[], int segs) { |
| 68 | SkScalar t = 0; |
| 69 | SkScalar dt = SK_Scalar1 / segs; |
| 70 | |
| 71 | samples[0] = cubic[0]; |
| 72 | for (int i = 1; i < segs; i++) { |
| 73 | t += dt; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 74 | SkEvalCubicAt(cubic, t, &samples[i], nullptr, nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | static void eval_sheet(const SkPoint edge[], int nu, int nv, int iu, int iv, |
| 79 | SkPoint* pt) { |
| 80 | const int TL = 0; |
| 81 | const int TR = nu; |
| 82 | const int BR = TR + nv; |
| 83 | const int BL = BR + nu; |
| 84 | |
| 85 | SkScalar u = SkIntToScalar(iu) / nu; |
| 86 | SkScalar v = SkIntToScalar(iv) / nv; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 87 | |
Mike Reed | df85c38 | 2017-02-14 10:59:19 -0500 | [diff] [blame] | 88 | SkScalar uv = u * v; |
| 89 | SkScalar Uv = (1 - u) * v; |
| 90 | SkScalar uV = u * (1 - v); |
| 91 | SkScalar UV = (1 - u) * (1 - v); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 92 | |
Mike Reed | df85c38 | 2017-02-14 10:59:19 -0500 | [diff] [blame] | 93 | SkScalar x0 = UV * edge[TL].fX + uV * edge[TR].fX + Uv * edge[BL].fX + uv * edge[BR].fX; |
| 94 | SkScalar y0 = UV * edge[TL].fY + uV * edge[TR].fY + Uv * edge[BL].fY + uv * edge[BR].fY; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | |
Mike Reed | df85c38 | 2017-02-14 10:59:19 -0500 | [diff] [blame] | 96 | SkScalar x = (1 - v) * edge[TL+iu].fX + u * edge[TR+iv].fX + |
| 97 | v * edge[BR+nu-iu].fX + (1 - u) * edge[BL+nv-iv].fX - x0; |
| 98 | SkScalar y = (1 - v) * edge[TL+iu].fY + u * edge[TR+iv].fY + |
| 99 | v * edge[BR+nu-iu].fY + (1 - u) * edge[BL+nv-iv].fY - y0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | pt->set(x, y); |
| 101 | } |
| 102 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 103 | static SkColor make_color(SkScalar s, SkScalar t) { |
benjaminwagner | e7be3e5 | 2016-03-01 13:44:10 -0800 | [diff] [blame] | 104 | return SkColorSetARGB(0xFF, SkUnitScalarClampToByte(s), SkUnitScalarClampToByte(t), 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void Patch::draw(SkCanvas* canvas, const SkPaint& paint, int nu, int nv, |
| 108 | bool doTextures, bool doColors) { |
| 109 | if (nu < 1 || nv < 1) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | int i, npts = (nu + nv) * 2; |
| 114 | SkAutoSTMalloc<16, SkPoint> storage(npts + 1); |
| 115 | SkPoint* edge0 = storage.get(); |
| 116 | SkPoint* edge1 = edge0 + nu; |
| 117 | SkPoint* edge2 = edge1 + nv; |
| 118 | SkPoint* edge3 = edge2 + nu; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 119 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 120 | // evaluate the edge points |
| 121 | eval_patch_edge(fPts + 0, edge0, nu); |
| 122 | eval_patch_edge(fPts + 3, edge1, nv); |
| 123 | eval_patch_edge(fPts + 6, edge2, nu); |
| 124 | eval_patch_edge(fPts + 9, edge3, nv); |
| 125 | edge3[nv] = edge0[0]; // the last shall be first |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 126 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 127 | for (i = 0; i < npts; i++) { |
| 128 | // canvas->drawLine(edge0[i].fX, edge0[i].fY, edge0[i+1].fX, edge0[i+1].fY, paint); |
| 129 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 130 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | int row, vertCount = (nu + 1) * (nv + 1); |
| 132 | SkAutoTMalloc<SkPoint> vertStorage(vertCount); |
| 133 | SkPoint* verts = vertStorage.get(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 134 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | // first row |
| 136 | memcpy(verts, edge0, (nu + 1) * sizeof(SkPoint)); |
| 137 | // rows |
| 138 | SkPoint* r = verts; |
| 139 | for (row = 1; row < nv; row++) { |
| 140 | r += nu + 1; |
| 141 | r[0] = edge3[nv - row]; |
| 142 | for (int col = 1; col < nu; col++) { |
| 143 | eval_sheet(edge0, nu, nv, col, row, &r[col]); |
| 144 | } |
| 145 | r[nu] = edge1[row]; |
| 146 | } |
| 147 | // last row |
| 148 | SkPoint* last = verts + nv * (nu + 1); |
| 149 | for (i = 0; i <= nu; i++) { |
| 150 | last[i] = edge2[nu - i]; |
| 151 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 152 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | // canvas->drawPoints(verts, vertCount, paint); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 154 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 155 | int stripCount = (nu + 1) * 2; |
| 156 | SkAutoTMalloc<SkPoint> stripStorage(stripCount * 2); |
| 157 | SkAutoTMalloc<SkColor> colorStorage(stripCount); |
| 158 | SkPoint* strip = stripStorage.get(); |
| 159 | SkPoint* tex = strip + stripCount; |
| 160 | SkColor* colors = colorStorage.get(); |
| 161 | SkScalar t = 0; |
| 162 | const SkScalar ds = SK_Scalar1 * fW / nu; |
| 163 | const SkScalar dt = SK_Scalar1 * fH / nv; |
| 164 | r = verts; |
| 165 | for (row = 0; row < nv; row++) { |
| 166 | SkPoint* upper = r; |
| 167 | SkPoint* lower = r + nu + 1; |
| 168 | r = lower; |
| 169 | SkScalar s = 0; |
| 170 | for (i = 0; i <= nu; i++) { |
| 171 | strip[i*2 + 0] = *upper++; |
| 172 | strip[i*2 + 1] = *lower++; |
| 173 | tex[i*2 + 0].set(s, t); |
| 174 | tex[i*2 + 1].set(s, t + dt); |
| 175 | colors[i*2 + 0] = make_color(s/fW, t/fH); |
| 176 | colors[i*2 + 1] = make_color(s/fW, (t + dt)/fH); |
| 177 | s += ds; |
| 178 | } |
| 179 | t += dt; |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 180 | canvas->drawVertices(SkVertices::MakeCopy(SkVertices::kTriangleStrip_VertexMode, stripCount, |
| 181 | strip, doTextures ? tex : nullptr, |
| 182 | doColors ? colors : nullptr), |
| 183 | SkBlendMode::kModulate, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | static void drawpatches(SkCanvas* canvas, const SkPaint& paint, int nu, int nv, |
| 188 | Patch* patch) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 189 | SkAutoCanvasRestore ar(canvas, true); |
| 190 | |
reed | cdf2db9 | 2014-08-06 09:54:19 -0700 | [diff] [blame] | 191 | patch->draw(canvas, paint, nu, nv, false, false); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 192 | canvas->translate(SkIntToScalar(180), 0); |
reed | cdf2db9 | 2014-08-06 09:54:19 -0700 | [diff] [blame] | 193 | patch->draw(canvas, paint, nu, nv, true, false); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 194 | canvas->translate(SkIntToScalar(180), 0); |
reed | cdf2db9 | 2014-08-06 09:54:19 -0700 | [diff] [blame] | 195 | patch->draw(canvas, paint, nu, nv, false, true); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 196 | canvas->translate(SkIntToScalar(180), 0); |
reed | cdf2db9 | 2014-08-06 09:54:19 -0700 | [diff] [blame] | 197 | patch->draw(canvas, paint, nu, nv, true, true); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Hal Canary | 5f4a754 | 2019-07-03 22:17:24 -0400 | [diff] [blame] | 200 | static constexpr SkScalar DX = 20; |
| 201 | static constexpr SkScalar DY = 0; |
| 202 | static constexpr SkScalar kS = 50; |
| 203 | static constexpr SkScalar kT = 40; |
reed | 147476d | 2014-08-06 07:00:27 -0700 | [diff] [blame] | 204 | |
Hal Canary | 5f4a754 | 2019-07-03 22:17:24 -0400 | [diff] [blame] | 205 | struct PatchView : public Sample { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 206 | sk_sp<SkShader> fShader0; |
| 207 | sk_sp<SkShader> fShader1; |
Hal Canary | 5f4a754 | 2019-07-03 22:17:24 -0400 | [diff] [blame] | 208 | SkScalar fAngle = 0; |
| 209 | SkIPoint fSize0 = {0, 0}, |
| 210 | fSize1 = {0, 0}; |
| 211 | SkPoint fPts[12] = { |
| 212 | {kS * 0, kT * 1}, |
| 213 | {kS * 1, kT * 1}, |
| 214 | {kS * 2, kT * 1}, |
| 215 | {kS * 3, kT * 1}, |
| 216 | {kS * 3, kT * 2}, |
| 217 | {kS * 3, kT * 3}, |
| 218 | {kS * 3, kT * 4}, |
| 219 | {kS * 2, kT * 4}, |
| 220 | {kS * 1, kT * 4}, |
| 221 | {kS * 0, kT * 4}, |
| 222 | {kS * 0, kT * 3}, |
| 223 | {kS * 0, kT * 2}, |
| 224 | }; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 225 | |
Hal Canary | 5f4a754 | 2019-07-03 22:17:24 -0400 | [diff] [blame] | 226 | void onOnceBeforeDraw() override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 227 | fShader0 = make_shader0(&fSize0); |
| 228 | fSize1 = fSize0; |
| 229 | if (fSize0.fX == 0 || fSize0.fY == 0) { |
| 230 | fSize1.set(2, 2); |
| 231 | } |
| 232 | fShader1 = make_shader1(fSize1); |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 233 | this->setBGColor(SK_ColorGRAY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 234 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 235 | |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 236 | SkString name() override { return SkString("Patch"); } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 237 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 238 | void onDrawContent(SkCanvas* canvas) override { |
reed | cdf2db9 | 2014-08-06 09:54:19 -0700 | [diff] [blame] | 239 | const int nu = 10; |
| 240 | const int nv = 10; |
| 241 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 242 | SkPaint paint; |
| 243 | paint.setDither(true); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 244 | paint.setFilterQuality(kLow_SkFilterQuality); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 245 | |
reed | 147476d | 2014-08-06 07:00:27 -0700 | [diff] [blame] | 246 | canvas->translate(DX, DY); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 247 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 248 | Patch patch; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 249 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 250 | paint.setShader(fShader0); |
| 251 | if (fSize0.fX == 0) { |
| 252 | fSize0.fX = 1; |
| 253 | } |
| 254 | if (fSize0.fY == 0) { |
| 255 | fSize0.fY = 1; |
| 256 | } |
| 257 | patch.setBounds(fSize0.fX, fSize0.fY); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 258 | |
| 259 | patch.setPatch(fPts); |
reed | cdf2db9 | 2014-08-06 09:54:19 -0700 | [diff] [blame] | 260 | drawpatches(canvas, paint, nu, nv, &patch); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 261 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 262 | paint.setShader(nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 263 | paint.setAntiAlias(true); |
| 264 | paint.setStrokeWidth(SkIntToScalar(5)); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 265 | canvas->drawPoints(SkCanvas::kPoints_PointMode, SK_ARRAY_COUNT(fPts), fPts, paint); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 266 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 267 | canvas->translate(0, SkIntToScalar(300)); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 268 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 269 | paint.setAntiAlias(false); |
| 270 | paint.setShader(fShader1); |
Hal Canary | 5f4a754 | 2019-07-03 22:17:24 -0400 | [diff] [blame] | 271 | { |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 272 | SkMatrix m; |
| 273 | m.setSkew(1, 0); |
reed | 150835e | 2016-03-10 06:36:49 -0800 | [diff] [blame] | 274 | paint.setShader(paint.getShader()->makeWithLocalMatrix(m)); |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 275 | } |
Hal Canary | 5f4a754 | 2019-07-03 22:17:24 -0400 | [diff] [blame] | 276 | { |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 277 | SkMatrix m; |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 278 | m.setRotate(fAngle); |
reed | 150835e | 2016-03-10 06:36:49 -0800 | [diff] [blame] | 279 | paint.setShader(paint.getShader()->makeWithLocalMatrix(m)); |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 280 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 281 | patch.setBounds(fSize1.fX, fSize1.fY); |
reed | cdf2db9 | 2014-08-06 09:54:19 -0700 | [diff] [blame] | 282 | drawpatches(canvas, paint, nu, nv, &patch); |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 283 | } |
skia.committer@gmail.com | b2c82c9 | 2014-05-08 03:05:29 +0000 | [diff] [blame] | 284 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 285 | bool onAnimate(double nanos) override { |
| 286 | fAngle = TimeUtils::Scaled(1e-9 * nanos, 60, 360); |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 287 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 288 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 289 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 290 | class PtClick : public Click { |
| 291 | public: |
| 292 | int fIndex; |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 293 | PtClick(int index) : fIndex(index) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 294 | }; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 295 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 296 | static bool hittest(const SkPoint& pt, SkScalar x, SkScalar y) { |
| 297 | return SkPoint::Length(pt.fX - x, pt.fY - y) < SkIntToScalar(5); |
| 298 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 299 | |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 300 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override { |
reed | 147476d | 2014-08-06 07:00:27 -0700 | [diff] [blame] | 301 | x -= DX; |
| 302 | y -= DY; |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 303 | for (size_t i = 0; i < SK_ARRAY_COUNT(fPts); i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 304 | if (hittest(fPts[i], x, y)) { |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 305 | return new PtClick((int)i); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 308 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 309 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 310 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 311 | bool onClick(Click* click) override { |
reed | 147476d | 2014-08-06 07:00:27 -0700 | [diff] [blame] | 312 | fPts[((PtClick*)click)->fIndex].set(click->fCurr.fX - DX, click->fCurr.fY - DY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 313 | return true; |
| 314 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 315 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 316 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 317 | typedef Sample INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 318 | }; |
Hal Canary | 5f4a754 | 2019-07-03 22:17:24 -0400 | [diff] [blame] | 319 | } // namespace |
Mike Reed | 7254281 | 2019-03-04 17:00:39 -0500 | [diff] [blame] | 320 | DEF_SAMPLE( return new PatchView(); ) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 321 | |
| 322 | ////////////////////////////////////////////////////////////////////////////// |
| 323 | |
Hal Canary | 5f4a754 | 2019-07-03 22:17:24 -0400 | [diff] [blame] | 324 | namespace { |
Mike Reed | 7254281 | 2019-03-04 17:00:39 -0500 | [diff] [blame] | 325 | static sk_sp<SkVertices> make_verts(const SkPath& path, SkScalar width) { |
| 326 | auto meas = SkContourMeasureIter(path, false).next(); |
| 327 | if (!meas) { |
| 328 | return nullptr; |
| 329 | } |
| 330 | |
| 331 | const SkPoint src[2] = { |
| 332 | { 0, -width/2 }, { 0, width/2 }, |
| 333 | }; |
| 334 | SkTDArray<SkPoint> pts; |
| 335 | |
| 336 | const SkScalar step = 2; |
| 337 | for (SkScalar distance = 0; distance < meas->length(); distance += step) { |
| 338 | SkMatrix mx; |
| 339 | if (!meas->getMatrix(distance, &mx)) { |
| 340 | continue; |
| 341 | } |
| 342 | SkPoint* dst = pts.append(2); |
| 343 | mx.mapPoints(dst, src, 2); |
| 344 | } |
| 345 | |
| 346 | int vertCount = pts.count(); |
| 347 | int indexCount = 0; // no texture |
| 348 | unsigned flags = SkVertices::kHasColors_BuilderFlag | |
| 349 | SkVertices::kIsNonVolatile_BuilderFlag; |
| 350 | SkVertices::Builder builder(SkVertices::kTriangleStrip_VertexMode, |
| 351 | vertCount, indexCount, flags); |
| 352 | memcpy(builder.positions(), pts.begin(), vertCount * sizeof(SkPoint)); |
| 353 | SkRandom rand; |
| 354 | for (int i = 0; i < vertCount; ++i) { |
| 355 | builder.colors()[i] = rand.nextU() | 0xFF000000; |
| 356 | } |
| 357 | SkDebugf("vert count = %d\n", vertCount); |
| 358 | |
| 359 | return builder.detach(); |
| 360 | } |
| 361 | |
| 362 | class PseudoInkView : public Sample { |
Mike Reed | 541ae45 | 2019-03-05 14:29:50 -0500 | [diff] [blame] | 363 | enum { N = 100 }; |
Mike Reed | 7254281 | 2019-03-04 17:00:39 -0500 | [diff] [blame] | 364 | SkPath fPath; |
Mike Reed | 541ae45 | 2019-03-05 14:29:50 -0500 | [diff] [blame] | 365 | sk_sp<SkVertices> fVertices[N]; |
Mike Reed | 7254281 | 2019-03-04 17:00:39 -0500 | [diff] [blame] | 366 | SkPaint fSkeletonP, fStrokeP, fVertsP; |
Mike Reed | 541ae45 | 2019-03-05 14:29:50 -0500 | [diff] [blame] | 367 | bool fDirty = true; |
Mike Reed | 7254281 | 2019-03-04 17:00:39 -0500 | [diff] [blame] | 368 | |
| 369 | public: |
| 370 | PseudoInkView() { |
| 371 | fSkeletonP.setStyle(SkPaint::kStroke_Style); |
| 372 | fSkeletonP.setAntiAlias(true); |
| 373 | |
| 374 | fStrokeP.setStyle(SkPaint::kStroke_Style); |
| 375 | fStrokeP.setStrokeWidth(30); |
| 376 | fStrokeP.setColor(0x44888888); |
| 377 | } |
| 378 | |
| 379 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 380 | SkString name() override { return SkString("PseudoInk"); } |
Mike Reed | 7254281 | 2019-03-04 17:00:39 -0500 | [diff] [blame] | 381 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 382 | bool onAnimate(double nanos) override { return true; } |
Mike Reed | 7254281 | 2019-03-04 17:00:39 -0500 | [diff] [blame] | 383 | |
| 384 | void onDrawContent(SkCanvas* canvas) override { |
Mike Reed | 541ae45 | 2019-03-05 14:29:50 -0500 | [diff] [blame] | 385 | if (fDirty) { |
| 386 | for (int i = 0; i < N; ++i) { |
| 387 | fVertices[i] = make_verts(fPath, 30); |
| 388 | } |
| 389 | fDirty = false; |
Mike Reed | 7254281 | 2019-03-04 17:00:39 -0500 | [diff] [blame] | 390 | } |
Mike Reed | 541ae45 | 2019-03-05 14:29:50 -0500 | [diff] [blame] | 391 | for (int i = 0; i < N; ++i) { |
| 392 | canvas->drawVertices(fVertices[i], SkBlendMode::kSrc, fVertsP); |
| 393 | canvas->translate(1, 1); |
Mike Reed | 7254281 | 2019-03-04 17:00:39 -0500 | [diff] [blame] | 394 | } |
| 395 | // canvas->drawPath(fPath, fStrokeP); |
| 396 | // canvas->drawPath(fPath, fSkeletonP); |
| 397 | } |
| 398 | |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 399 | Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override { |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 400 | Click* click = new Click(); |
Mike Reed | 7254281 | 2019-03-04 17:00:39 -0500 | [diff] [blame] | 401 | fPath.reset(); |
| 402 | fPath.moveTo(x, y); |
| 403 | return click; |
| 404 | } |
| 405 | |
| 406 | bool onClick(Click* click) override { |
| 407 | switch (click->fState) { |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 408 | case skui::InputState::kMove: |
Mike Reed | 7254281 | 2019-03-04 17:00:39 -0500 | [diff] [blame] | 409 | fPath.lineTo(click->fCurr); |
Mike Reed | 541ae45 | 2019-03-05 14:29:50 -0500 | [diff] [blame] | 410 | fDirty = true; |
Mike Reed | 7254281 | 2019-03-04 17:00:39 -0500 | [diff] [blame] | 411 | break; |
| 412 | default: |
| 413 | break; |
| 414 | } |
| 415 | return true; |
| 416 | } |
| 417 | |
| 418 | private: |
| 419 | typedef Sample INHERITED; |
| 420 | }; |
Hal Canary | 5f4a754 | 2019-07-03 22:17:24 -0400 | [diff] [blame] | 421 | } // namespace |
Mike Reed | 7254281 | 2019-03-04 17:00:39 -0500 | [diff] [blame] | 422 | DEF_SAMPLE( return new PseudoInkView(); ) |
Mike Reed | 5ee611b | 2019-03-31 22:39:32 -0400 | [diff] [blame] | 423 | |
Hal Canary | 5f4a754 | 2019-07-03 22:17:24 -0400 | [diff] [blame] | 424 | namespace { |
Mike Reed | 5ee611b | 2019-03-31 22:39:32 -0400 | [diff] [blame] | 425 | // Show stroking options using patheffects (and pathops) |
| 426 | // and why strokeandfill is a hacks |
| 427 | class ManyStrokesView : public Sample { |
| 428 | SkPath fPath; |
| 429 | sk_sp<SkPathEffect> fPE[6]; |
| 430 | |
| 431 | public: |
| 432 | ManyStrokesView() { |
| 433 | fPE[0] = SkStrokePathEffect::Make(20, SkPaint::kRound_Join, SkPaint::kRound_Cap); |
| 434 | |
| 435 | auto p0 = SkStrokePathEffect::Make(25, SkPaint::kRound_Join, SkPaint::kRound_Cap); |
| 436 | auto p1 = SkStrokePathEffect::Make(20, SkPaint::kRound_Join, SkPaint::kRound_Cap); |
| 437 | fPE[1] = SkMergePathEffect::Make(p0, p1, SkPathOp::kDifference_SkPathOp); |
| 438 | |
| 439 | fPE[2] = SkMergePathEffect::Make(nullptr, p1, SkPathOp::kDifference_SkPathOp); |
| 440 | fPE[3] = SkMergePathEffect::Make(nullptr, p1, SkPathOp::kUnion_SkPathOp); |
| 441 | fPE[4] = SkMergePathEffect::Make(p0, nullptr, SkPathOp::kDifference_SkPathOp); |
| 442 | fPE[5] = SkMergePathEffect::Make(p0, nullptr, SkPathOp::kIntersect_SkPathOp); |
| 443 | } |
| 444 | |
| 445 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 446 | SkString name() override { return SkString("ManyStrokes"); } |
Mike Reed | 5ee611b | 2019-03-31 22:39:32 -0400 | [diff] [blame] | 447 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 448 | bool onAnimate(double nanos) override { return true; } |
Mike Reed | 5ee611b | 2019-03-31 22:39:32 -0400 | [diff] [blame] | 449 | |
| 450 | void dodraw(SkCanvas* canvas, sk_sp<SkPathEffect> pe, SkScalar x, SkScalar y, |
| 451 | const SkPaint* ptr = nullptr) { |
| 452 | SkPaint paint; |
| 453 | paint.setAntiAlias(true); |
| 454 | paint.setPathEffect(pe); |
| 455 | canvas->save(); |
| 456 | canvas->translate(x, y); |
| 457 | canvas->drawPath(fPath, ptr ? *ptr : paint); |
| 458 | |
| 459 | paint.setPathEffect(nullptr); |
| 460 | paint.setStyle(SkPaint::kStroke_Style); |
| 461 | paint.setColor(SK_ColorGREEN); |
| 462 | canvas->drawPath(fPath, paint); |
| 463 | |
| 464 | canvas->restore(); |
| 465 | } |
| 466 | |
| 467 | void onDrawContent(SkCanvas* canvas) override { |
| 468 | SkPaint p; |
| 469 | p.setColor(0); |
| 470 | this->dodraw(canvas, nullptr, 0, 0, &p); |
| 471 | |
| 472 | this->dodraw(canvas, fPE[0], 300, 0); |
| 473 | this->dodraw(canvas, fPE[1], 0, 300); |
| 474 | this->dodraw(canvas, fPE[2], 300, 300); |
| 475 | this->dodraw(canvas, fPE[3], 600, 300); |
| 476 | this->dodraw(canvas, fPE[4], 900, 0); |
| 477 | this->dodraw(canvas, fPE[5], 900, 300); |
| 478 | |
| 479 | p.setColor(SK_ColorBLACK); |
| 480 | p.setStyle(SkPaint::kStrokeAndFill_Style); |
| 481 | p.setStrokeJoin(SkPaint::kRound_Join); |
| 482 | p.setStrokeCap(SkPaint::kRound_Cap); |
| 483 | p.setStrokeWidth(20); |
| 484 | this->dodraw(canvas, nullptr, 600, 0, &p); |
| 485 | } |
| 486 | |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 487 | Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override { |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 488 | Click* click = new Click(); |
Mike Reed | 5ee611b | 2019-03-31 22:39:32 -0400 | [diff] [blame] | 489 | fPath.reset(); |
| 490 | fPath.moveTo(x, y); |
| 491 | return click; |
| 492 | } |
| 493 | |
| 494 | bool onClick(Click* click) override { |
| 495 | switch (click->fState) { |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 496 | case skui::InputState::kMove: |
Mike Reed | 5ee611b | 2019-03-31 22:39:32 -0400 | [diff] [blame] | 497 | fPath.lineTo(click->fCurr); |
| 498 | break; |
| 499 | default: |
| 500 | break; |
| 501 | } |
| 502 | return true; |
| 503 | } |
| 504 | |
| 505 | private: |
| 506 | typedef Sample INHERITED; |
| 507 | }; |
Hal Canary | 5f4a754 | 2019-07-03 22:17:24 -0400 | [diff] [blame] | 508 | } // namespace |
Mike Reed | 5ee611b | 2019-03-31 22:39:32 -0400 | [diff] [blame] | 509 | DEF_SAMPLE( return new ManyStrokesView(); ) |