blob: 0b8ef027b4c8a0ef540d0fa0d01305ef8494ce4e [file] [log] [blame]
csmartdaltoned4984b2017-02-13 14:57:28 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkPaint.h"
10#include "include/core/SkPath.h"
11#include "include/utils/SkRandom.h"
12#include "samplecode/Sample.h"
13#include "src/core/SkStrike.h"
14#include "src/core/SkStrikeCache.h"
Herb Derbybaf64782019-04-17 18:01:04 -040015#include "src/core/SkStrikeSpec.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkTaskGroup.h"
17#include "tools/ToolUtils.h"
csmartdaltoned4984b2017-02-13 14:57:28 -070018
19////////////////////////////////////////////////////////////////////////////////////////////////////
20// Static text from paths.
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040021class PathText : public Sample {
csmartdaltoned4984b2017-02-13 14:57:28 -070022public:
23 constexpr static int kNumPaths = 1500;
24 virtual const char* getName() const { return "PathText"; }
25
Ben Wagner9d289e22018-09-17 15:25:18 -040026 PathText() {}
27
28 virtual void reset() {
29 for (Glyph& glyph : fGlyphs) {
30 glyph.reset(fRand, this->width(), this->height());
31 }
32 }
33
34 void onOnceBeforeDraw() final {
Mike Reed32c60662018-11-28 10:28:07 -050035 SkFont defaultFont;
Herb Derbyac718272019-06-06 14:33:12 -040036 SkStrikeSpec strikeSpec = SkStrikeSpec::MakeWithNoDevice(defaultFont);
Herb Derbybaf64782019-04-17 18:01:04 -040037 auto cache = strikeSpec.findOrCreateExclusiveStrike();
csmartdaltoned4984b2017-02-13 14:57:28 -070038 SkPath glyphPaths[52];
39 for (int i = 0; i < 52; ++i) {
40 // I and l are rects on OS X ...
41 char c = "aQCDEFGH7JKLMNOPBRZTUVWXYSAbcdefghijk1mnopqrstuvwxyz"[i];
Mike Reedfdb876d2019-02-01 09:55:20 -050042 SkPackedGlyphID id(defaultFont.unicharToGlyph(c));
Ben Wagner5ddb3082018-03-29 11:18:06 -040043 sk_ignore_unused_variable(cache->getScalerContext()->getPath(id, &glyphPaths[i]));
csmartdaltoned4984b2017-02-13 14:57:28 -070044 }
45
46 for (int i = 0; i < kNumPaths; ++i) {
47 const SkPath& p = glyphPaths[i % 52];
48 fGlyphs[i].init(fRand, p);
49 }
csmartdaltoned4984b2017-02-13 14:57:28 -070050
Ben Wagner9d289e22018-09-17 15:25:18 -040051 this->INHERITED::onOnceBeforeDraw();
52 this->reset();
csmartdaltoned4984b2017-02-13 14:57:28 -070053 }
csmartdaltoned4984b2017-02-13 14:57:28 -070054 void onSizeChange() final { this->INHERITED::onSizeChange(); this->reset(); }
55
Hal Canary8a027312019-07-03 10:55:44 -040056 SkString name() override { return SkString(this->getName()); }
57
Hal Canary6cc65e12019-07-03 15:53:04 -040058 bool onChar(SkUnichar unichar) override {
Chris Dalton7c02cc72017-11-06 14:10:54 -070059 if (unichar == 'X') {
60 fDoClip = !fDoClip;
Chris Dalton7c02cc72017-11-06 14:10:54 -070061 return true;
62 }
Hal Canary6cc65e12019-07-03 15:53:04 -040063 return false;
csmartdaltoned4984b2017-02-13 14:57:28 -070064 }
65
66 void onDrawContent(SkCanvas* canvas) override {
Chris Dalton7c02cc72017-11-06 14:10:54 -070067 if (fDoClip) {
Chris Daltona32a3c32017-12-05 10:05:21 -070068 SkPath deviceSpaceClipPath = fClipPath;
69 deviceSpaceClipPath.transform(SkMatrix::MakeScale(this->width(), this->height()));
Chris Dalton7c02cc72017-11-06 14:10:54 -070070 canvas->save();
Chris Daltona32a3c32017-12-05 10:05:21 -070071 canvas->clipPath(deviceSpaceClipPath, SkClipOp::kDifference, true);
Chris Dalton7c02cc72017-11-06 14:10:54 -070072 canvas->clear(SK_ColorBLACK);
73 canvas->restore();
Chris Daltona32a3c32017-12-05 10:05:21 -070074 canvas->clipPath(deviceSpaceClipPath, SkClipOp::kIntersect, true);
Chris Dalton7c02cc72017-11-06 14:10:54 -070075 }
76 this->drawGlyphs(canvas);
77 }
78
79 virtual void drawGlyphs(SkCanvas* canvas) {
csmartdaltoned4984b2017-02-13 14:57:28 -070080 for (Glyph& glyph : fGlyphs) {
81 SkAutoCanvasRestore acr(canvas, true);
82 canvas->translate(glyph.fPosition.x(), glyph.fPosition.y());
83 canvas->scale(glyph.fZoom, glyph.fZoom);
84 canvas->rotate(glyph.fSpin);
85 canvas->translate(-glyph.fMidpt.x(), -glyph.fMidpt.y());
86 canvas->drawPath(glyph.fPath, glyph.fPaint);
87 }
88 }
89
90protected:
91 struct Glyph {
92 void init(SkRandom& rand, const SkPath& path);
93 void reset(SkRandom& rand, int w, int h);
94
95 SkPath fPath;
96 SkPaint fPaint;
97 SkPoint fPosition;
98 SkScalar fZoom;
99 SkScalar fSpin;
100 SkPoint fMidpt;
101 };
102
103 Glyph fGlyphs[kNumPaths];
Chris Dalton7c02cc72017-11-06 14:10:54 -0700104 SkRandom fRand{25};
Mike Kleinea3f0142019-03-20 11:12:10 -0500105 SkPath fClipPath = ToolUtils::make_star(SkRect{0, 0, 1, 1}, 11, 3);
Chris Dalton7c02cc72017-11-06 14:10:54 -0700106 bool fDoClip = false;
csmartdaltoned4984b2017-02-13 14:57:28 -0700107
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400108 typedef Sample INHERITED;
csmartdaltoned4984b2017-02-13 14:57:28 -0700109};
110
111void PathText::Glyph::init(SkRandom& rand, const SkPath& path) {
112 fPath = path;
113 fPaint.setAntiAlias(true);
114 fPaint.setColor(rand.nextU() | 0x80808080);
115}
116
117void PathText::Glyph::reset(SkRandom& rand, int w, int h) {
118 int screensize = SkTMax(w, h);
119 const SkRect& bounds = fPath.getBounds();
120 SkScalar t;
121
122 fPosition = {rand.nextF() * w, rand.nextF() * h};
123 t = pow(rand.nextF(), 100);
124 fZoom = ((1 - t) * screensize / 50 + t * screensize / 3) /
125 SkTMax(bounds.width(), bounds.height());
126 fSpin = rand.nextF() * 360;
127 fMidpt = {bounds.centerX(), bounds.centerY()};
128}
129
130////////////////////////////////////////////////////////////////////////////////////////////////////
131// Text from paths with animated transformation matrices.
132class MovingPathText : public PathText {
133public:
134 const char* getName() const override { return "MovingPathText"; }
135
136 MovingPathText()
137 : fFrontMatrices(kNumPaths)
138 , fBackMatrices(kNumPaths) {
139 }
140
Brian Salomond3b65972017-03-22 12:05:03 -0400141 ~MovingPathText() override {
csmartdaltoned4984b2017-02-13 14:57:28 -0700142 fBackgroundAnimationTask.wait();
143 }
144
145 void reset() override {
146 const SkScalar screensize = static_cast<SkScalar>(SkTMax(this->width(), this->height()));
147 this->INHERITED::reset();
148
149 for (auto& v : fVelocities) {
150 for (SkScalar* d : {&v.fDx, &v.fDy}) {
151 SkScalar t = pow(fRand.nextF(), 3);
152 *d = ((1 - t) / 60 + t / 10) * (fRand.nextBool() ? screensize : -screensize);
153 }
154
155 SkScalar t = pow(fRand.nextF(), 25);
156 v.fDSpin = ((1 - t) * 360 / 7.5 + t * 360 / 1.5) * (fRand.nextBool() ? 1 : -1);
157 }
158
159 // Get valid front data.
160 fBackgroundAnimationTask.wait();
161 this->runAnimationTask(0, 0, this->width(), this->height());
162 memcpy(fFrontMatrices, fBackMatrices, kNumPaths * sizeof(SkMatrix));
163 fLastTick = 0;
164 }
165
Hal Canary41248072019-07-11 16:32:53 -0400166 bool onAnimate(double nanos) final {
csmartdaltoned4984b2017-02-13 14:57:28 -0700167 fBackgroundAnimationTask.wait();
168 this->swapAnimationBuffers();
169
Hal Canary41248072019-07-11 16:32:53 -0400170 const double tsec = 1e-9 * nanos;
171 const double dt = fLastTick ? (1e-9 * nanos - fLastTick) : 0;
csmartdaltoned4984b2017-02-13 14:57:28 -0700172 fBackgroundAnimationTask.add(std::bind(&MovingPathText::runAnimationTask, this, tsec,
173 dt, this->width(), this->height()));
Hal Canary41248072019-07-11 16:32:53 -0400174 fLastTick = 1e-9 * nanos;
csmartdaltoned4984b2017-02-13 14:57:28 -0700175 return true;
176 }
177
178 /**
179 * Called on a background thread. Here we can only modify fBackMatrices.
180 */
181 virtual void runAnimationTask(double t, double dt, int w, int h) {
182 for (int idx = 0; idx < kNumPaths; ++idx) {
183 Velocity* v = &fVelocities[idx];
184 Glyph* glyph = &fGlyphs[idx];
185 SkMatrix* backMatrix = &fBackMatrices[idx];
186
187 glyph->fPosition.fX += v->fDx * dt;
188 if (glyph->fPosition.x() < 0) {
189 glyph->fPosition.fX -= 2 * glyph->fPosition.x();
190 v->fDx = -v->fDx;
191 } else if (glyph->fPosition.x() > w) {
192 glyph->fPosition.fX -= 2 * (glyph->fPosition.x() - w);
193 v->fDx = -v->fDx;
194 }
195
196 glyph->fPosition.fY += v->fDy * dt;
197 if (glyph->fPosition.y() < 0) {
198 glyph->fPosition.fY -= 2 * glyph->fPosition.y();
199 v->fDy = -v->fDy;
200 } else if (glyph->fPosition.y() > h) {
201 glyph->fPosition.fY -= 2 * (glyph->fPosition.y() - h);
202 v->fDy = -v->fDy;
203 }
204
205 glyph->fSpin += v->fDSpin * dt;
206
207 backMatrix->setTranslate(glyph->fPosition.x(), glyph->fPosition.y());
208 backMatrix->preScale(glyph->fZoom, glyph->fZoom);
209 backMatrix->preRotate(glyph->fSpin);
210 backMatrix->preTranslate(-glyph->fMidpt.x(), -glyph->fMidpt.y());
211 }
212 }
213
214 virtual void swapAnimationBuffers() {
215 std::swap(fFrontMatrices, fBackMatrices);
216 }
217
Chris Dalton7c02cc72017-11-06 14:10:54 -0700218 void drawGlyphs(SkCanvas* canvas) override {
csmartdaltoned4984b2017-02-13 14:57:28 -0700219 for (int i = 0; i < kNumPaths; ++i) {
220 SkAutoCanvasRestore acr(canvas, true);
221 canvas->concat(fFrontMatrices[i]);
222 canvas->drawPath(fGlyphs[i].fPath, fGlyphs[i].fPaint);
223 }
224 }
225
226protected:
227 struct Velocity {
228 SkScalar fDx, fDy;
229 SkScalar fDSpin;
230 };
231
232 Velocity fVelocities[kNumPaths];
233 SkAutoTMalloc<SkMatrix> fFrontMatrices;
234 SkAutoTMalloc<SkMatrix> fBackMatrices;
235 SkTaskGroup fBackgroundAnimationTask;
236 double fLastTick;
237
238 typedef PathText INHERITED;
239};
240
241
242////////////////////////////////////////////////////////////////////////////////////////////////////
243// Text from paths with animated control points.
244class WavyPathText : public MovingPathText {
245public:
246 const char* getName() const override { return "WavyPathText"; }
247
248 WavyPathText()
249 : fFrontPaths(kNumPaths)
250 , fBackPaths(kNumPaths) {}
251
Brian Salomond3b65972017-03-22 12:05:03 -0400252 ~WavyPathText() override {
csmartdaltoned4984b2017-02-13 14:57:28 -0700253 fBackgroundAnimationTask.wait();
254 }
255
256 void reset() override {
257 fWaves.reset(fRand, this->width(), this->height());
258 this->INHERITED::reset();
259 std::copy(fBackPaths.get(), fBackPaths.get() + kNumPaths, fFrontPaths.get());
260 }
261
262 /**
263 * Called on a background thread. Here we can only modify fBackPaths.
264 */
265 void runAnimationTask(double t, double dt, int w, int h) override {
266 const float tsec = static_cast<float>(t);
267 this->INHERITED::runAnimationTask(t, 0.5 * dt, w, h);
268
269 for (int i = 0; i < kNumPaths; ++i) {
270 const Glyph& glyph = fGlyphs[i];
271 const SkMatrix& backMatrix = fBackMatrices[i];
272
273 const Sk2f matrix[3] = {
274 Sk2f(backMatrix.getScaleX(), backMatrix.getSkewY()),
275 Sk2f(backMatrix.getSkewX(), backMatrix.getScaleY()),
276 Sk2f(backMatrix.getTranslateX(), backMatrix.getTranslateY())
277 };
278
279 SkPath* backpath = &fBackPaths[i];
280 backpath->reset();
Mike Reed7d34dc72019-11-26 12:17:17 -0500281 backpath->setFillType(SkPathFillType::kEvenOdd);
csmartdaltoned4984b2017-02-13 14:57:28 -0700282
283 SkPath::RawIter iter(glyph.fPath);
284 SkPath::Verb verb;
285 SkPoint pts[4];
286
287 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
288 switch (verb) {
289 case SkPath::kMove_Verb: {
290 SkPoint pt = fWaves.apply(tsec, matrix, pts[0]);
291 backpath->moveTo(pt.x(), pt.y());
292 break;
293 }
294 case SkPath::kLine_Verb: {
295 SkPoint endpt = fWaves.apply(tsec, matrix, pts[1]);
296 backpath->lineTo(endpt.x(), endpt.y());
297 break;
298 }
299 case SkPath::kQuad_Verb: {
300 SkPoint controlPt = fWaves.apply(tsec, matrix, pts[1]);
301 SkPoint endpt = fWaves.apply(tsec, matrix, pts[2]);
302 backpath->quadTo(controlPt.x(), controlPt.y(), endpt.x(), endpt.y());
303 break;
304 }
305 case SkPath::kClose_Verb: {
306 backpath->close();
307 break;
308 }
309 case SkPath::kCubic_Verb:
310 case SkPath::kConic_Verb:
311 case SkPath::kDone_Verb:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400312 SK_ABORT("Unexpected path verb");
csmartdaltoned4984b2017-02-13 14:57:28 -0700313 break;
314 }
315 }
316 }
317 }
318
319 void swapAnimationBuffers() override {
320 this->INHERITED::swapAnimationBuffers();
Hal Canary67362362018-04-24 13:58:37 -0400321 std::swap(fFrontPaths, fBackPaths);
csmartdaltoned4984b2017-02-13 14:57:28 -0700322 }
323
Chris Dalton7c02cc72017-11-06 14:10:54 -0700324 void drawGlyphs(SkCanvas* canvas) override {
csmartdaltoned4984b2017-02-13 14:57:28 -0700325 for (int i = 0; i < kNumPaths; ++i) {
326 canvas->drawPath(fFrontPaths[i], fGlyphs[i].fPaint);
327 }
328 }
329
330private:
331 /**
332 * Describes 4 stacked sine waves that can offset a point as a function of wall time.
333 */
334 class Waves {
335 public:
336 void reset(SkRandom& rand, int w, int h);
337 SkPoint apply(float tsec, const Sk2f matrix[3], const SkPoint& pt) const;
338
339 private:
340 constexpr static double kAverageAngle = SK_ScalarPI / 8.0;
341 constexpr static double kMaxOffsetAngle = SK_ScalarPI / 3.0;
342
343 float fAmplitudes[4];
344 float fFrequencies[4];
345 float fDirsX[4];
346 float fDirsY[4];
347 float fSpeeds[4];
348 float fOffsets[4];
349 };
350
351 SkAutoTArray<SkPath> fFrontPaths;
352 SkAutoTArray<SkPath> fBackPaths;
353 Waves fWaves;
354
355 typedef MovingPathText INHERITED;
356};
357
358void WavyPathText::Waves::reset(SkRandom& rand, int w, int h) {
359 const double pixelsPerMeter = 0.06 * SkTMax(w, h);
360 const double medianWavelength = 8 * pixelsPerMeter;
361 const double medianWaveAmplitude = 0.05 * 4 * pixelsPerMeter;
362 const double gravity = 9.8 * pixelsPerMeter;
363
364 for (int i = 0; i < 4; ++i) {
365 const double offsetAngle = (rand.nextF() * 2 - 1) * kMaxOffsetAngle;
366 const double intensity = pow(2, rand.nextF() * 2 - 1);
367 const double wavelength = intensity * medianWavelength;
368
369 fAmplitudes[i] = intensity * medianWaveAmplitude;
370 fFrequencies[i] = 2 * SK_ScalarPI / wavelength;
371 fDirsX[i] = cosf(kAverageAngle + offsetAngle);
372 fDirsY[i] = sinf(kAverageAngle + offsetAngle);
373 fSpeeds[i] = -sqrt(gravity * 2 * SK_ScalarPI / wavelength);
374 fOffsets[i] = rand.nextF() * 2 * SK_ScalarPI;
375 }
376}
377
378SkPoint WavyPathText::Waves::apply(float tsec, const Sk2f matrix[3], const SkPoint& pt) const {
Chris Dalton95cf1662017-06-20 16:32:59 -0700379 constexpr static int kTablePeriod = 1 << 12;
380 static float sin2table[kTablePeriod + 1];
csmartdaltoned4984b2017-02-13 14:57:28 -0700381 static SkOnce initTable;
382 initTable([]() {
Chris Dalton95cf1662017-06-20 16:32:59 -0700383 for (int i = 0; i <= kTablePeriod; ++i) {
384 const double sintheta = sin(i * (SK_ScalarPI / kTablePeriod));
csmartdaltoned4984b2017-02-13 14:57:28 -0700385 sin2table[i] = static_cast<float>(sintheta * sintheta - 0.5);
386 }
387 });
388
389 const Sk4f amplitudes = Sk4f::Load(fAmplitudes);
390 const Sk4f frequencies = Sk4f::Load(fFrequencies);
391 const Sk4f dirsX = Sk4f::Load(fDirsX);
392 const Sk4f dirsY = Sk4f::Load(fDirsY);
393 const Sk4f speeds = Sk4f::Load(fSpeeds);
394 const Sk4f offsets = Sk4f::Load(fOffsets);
395
396 float devicePt[2];
397 (matrix[0] * pt.x() + matrix[1] * pt.y() + matrix[2]).store(devicePt);
398
399 const Sk4f t = (frequencies * (dirsX * devicePt[0] + dirsY * devicePt[1]) +
400 speeds * tsec +
Chris Dalton95cf1662017-06-20 16:32:59 -0700401 offsets).abs() * (float(kTablePeriod) / float(SK_ScalarPI));
csmartdaltoned4984b2017-02-13 14:57:28 -0700402
403 const Sk4i ipart = SkNx_cast<int>(t);
404 const Sk4f fpart = t - SkNx_cast<float>(ipart);
405
406 int32_t indices[4];
Chris Dalton95cf1662017-06-20 16:32:59 -0700407 (ipart & (kTablePeriod-1)).store(indices);
csmartdaltoned4984b2017-02-13 14:57:28 -0700408
409 const Sk4f left(sin2table[indices[0]], sin2table[indices[1]],
410 sin2table[indices[2]], sin2table[indices[3]]);
411 const Sk4f right(sin2table[indices[0] + 1], sin2table[indices[1] + 1],
412 sin2table[indices[2] + 1], sin2table[indices[3] + 1]);
413 const Sk4f height = amplitudes * (left * (1.f - fpart) + right * fpart);
414
415 Sk4f dy = height * dirsY;
416 Sk4f dx = height * dirsX;
417
418 float offsetY[4], offsetX[4];
419 (dy + SkNx_shuffle<2,3,0,1>(dy)).store(offsetY); // accumulate.
Brian Salomon23356442018-11-30 15:33:19 -0500420 (dx + SkNx_shuffle<2,3,0,1>(dx)).store(offsetX);
csmartdaltoned4984b2017-02-13 14:57:28 -0700421
422 return {devicePt[0] + offsetY[0] + offsetY[1], devicePt[1] - offsetX[0] - offsetX[1]};
423}
424
425////////////////////////////////////////////////////////////////////////////////////////////////////
426
427DEF_SAMPLE( return new WavyPathText; )
428DEF_SAMPLE( return new MovingPathText; )
429DEF_SAMPLE( return new PathText; )