1 /*
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
8 #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/SkPathPriv.h"
14 #include "src/core/SkScalerCache.h"
15 #include "src/core/SkStrikeCache.h"
16 #include "src/core/SkStrikeSpec.h"
17 #include "src/core/SkTaskGroup.h"
18 #include "tools/ToolUtils.h"
19
20 ////////////////////////////////////////////////////////////////////////////////////////////////////
21 // Static text from paths.
22 class PathText : public Sample {
23 public:
24 constexpr static int kNumPaths = 1500;
getName() const25 virtual const char* getName() const { return "PathText"; }
26
PathText()27 PathText() {}
28
reset()29 virtual void reset() {
30 for (Glyph& glyph : fGlyphs) {
31 glyph.reset(fRand, this->width(), this->height());
32 }
33 fGlyphAnimator->reset(&fRand, this->width(), this->height());
34 }
35
onOnceBeforeDraw()36 void onOnceBeforeDraw() final {
37 SkFont defaultFont;
38 SkStrikeSpec strikeSpec = SkStrikeSpec::MakeWithNoDevice(defaultFont);
39 auto strike = strikeSpec.findOrCreateStrike();
40 SkArenaAlloc alloc(1 << 12); // This is a mock SkStrikeCache.
41 SkPath glyphPaths[52];
42 for (int i = 0; i < 52; ++i) {
43 // I and l are rects on OS X ...
44 char c = "aQCDEFGH7JKLMNOPBRZTUVWXYSAbcdefghijk1mnopqrstuvwxyz"[i];
45 SkPackedGlyphID id(defaultFont.unicharToGlyph(c));
46 SkGlyph glyph = strike->getScalerContext()->makeGlyph(id, &alloc);
47 strike->getScalerContext()->getPath(glyph, &alloc);
48 if (glyph.path()) {
49 glyphPaths[i] = *glyph.path();
50 }
51 }
52
53 for (int i = 0; i < kNumPaths; ++i) {
54 const SkPath& p = glyphPaths[i % 52];
55 fGlyphs[i].init(fRand, p);
56 }
57
58 this->Sample::onOnceBeforeDraw();
59 this->reset();
60 }
onSizeChange()61 void onSizeChange() final { this->Sample::onSizeChange(); this->reset(); }
62
name()63 SkString name() override { return SkString(this->getName()); }
64
65 bool onChar(SkUnichar) override;
66
onAnimate(double nanos)67 bool onAnimate(double nanos) final {
68 return fGlyphAnimator->animate(nanos, this->width(), this->height());
69 }
70
onDrawContent(SkCanvas * canvas)71 void onDrawContent(SkCanvas* canvas) override {
72 if (fDoClip) {
73 SkPath deviceSpaceClipPath = fClipPath;
74 deviceSpaceClipPath.transform(SkMatrix::Scale(this->width(), this->height()));
75 canvas->save();
76 canvas->clipPath(deviceSpaceClipPath, SkClipOp::kDifference, true);
77 canvas->clear(SK_ColorBLACK);
78 canvas->restore();
79 canvas->clipPath(deviceSpaceClipPath, SkClipOp::kIntersect, true);
80 }
81 fGlyphAnimator->draw(canvas);
82 }
83
84 protected:
85 struct Glyph {
86 void init(SkRandom& rand, const SkPath& path);
87 void reset(SkRandom& rand, int w, int h);
88
89 SkPath fPath;
90 SkPaint fPaint;
91 SkPoint fPosition;
92 SkScalar fZoom;
93 SkScalar fSpin;
94 SkPoint fMidpt;
95 };
96
97 class GlyphAnimator {
98 public:
GlyphAnimator(Glyph * glyphs)99 GlyphAnimator(Glyph* glyphs) : fGlyphs(glyphs) {}
reset(SkRandom *,int screenWidth,int screenHeight)100 virtual void reset(SkRandom*, int screenWidth, int screenHeight) {}
animate(double nanos,int screenWidth,int screenHeight)101 virtual bool animate(double nanos, int screenWidth, int screenHeight) { return false; }
draw(SkCanvas * canvas)102 virtual void draw(SkCanvas* canvas) {
103 for (int i = 0; i < kNumPaths; ++i) {
104 Glyph& glyph = fGlyphs[i];
105 SkAutoCanvasRestore acr(canvas, true);
106 canvas->translate(glyph.fPosition.x(), glyph.fPosition.y());
107 canvas->scale(glyph.fZoom, glyph.fZoom);
108 canvas->rotate(glyph.fSpin);
109 canvas->translate(-glyph.fMidpt.x(), -glyph.fMidpt.y());
110 canvas->drawPath(glyph.fPath, glyph.fPaint);
111 }
112 }
~GlyphAnimator()113 virtual ~GlyphAnimator() {}
114
115 protected:
116 Glyph* const fGlyphs;
117 };
118
119 class MovingGlyphAnimator;
120 class WavyGlyphAnimator;
121
122 Glyph fGlyphs[kNumPaths];
123 SkRandom fRand{25};
124 SkPath fClipPath = ToolUtils::make_star(SkRect{0, 0, 1, 1}, 11, 3);
125 bool fDoClip = false;
126 std::unique_ptr<GlyphAnimator> fGlyphAnimator = std::make_unique<GlyphAnimator>(fGlyphs);
127 };
128
init(SkRandom & rand,const SkPath & path)129 void PathText::Glyph::init(SkRandom& rand, const SkPath& path) {
130 fPath = path;
131 fPaint.setAntiAlias(true);
132 fPaint.setColor(rand.nextU() | 0x80808080);
133 }
134
reset(SkRandom & rand,int w,int h)135 void PathText::Glyph::reset(SkRandom& rand, int w, int h) {
136 int screensize = std::max(w, h);
137 const SkRect& bounds = fPath.getBounds();
138 SkScalar t;
139
140 fPosition = {rand.nextF() * w, rand.nextF() * h};
141 t = pow(rand.nextF(), 100);
142 fZoom = ((1 - t) * screensize / 50 + t * screensize / 3) /
143 std::max(bounds.width(), bounds.height());
144 fSpin = rand.nextF() * 360;
145 fMidpt = {bounds.centerX(), bounds.centerY()};
146 }
147
148 ////////////////////////////////////////////////////////////////////////////////////////////////////
149 // Text from paths with animated transformation matrices.
150 class PathText::MovingGlyphAnimator : public PathText::GlyphAnimator {
151 public:
MovingGlyphAnimator(Glyph * glyphs)152 MovingGlyphAnimator(Glyph* glyphs)
153 : GlyphAnimator(glyphs)
154 , fFrontMatrices(kNumPaths)
155 , fBackMatrices(kNumPaths) {
156 }
157
~MovingGlyphAnimator()158 ~MovingGlyphAnimator() override {
159 fBackgroundAnimationTask.wait();
160 }
161
reset(SkRandom * rand,int screenWidth,int screenHeight)162 void reset(SkRandom* rand, int screenWidth, int screenHeight) override {
163 const SkScalar screensize = static_cast<SkScalar>(std::max(screenWidth, screenHeight));
164
165 for (auto& v : fVelocities) {
166 for (SkScalar* d : {&v.fDx, &v.fDy}) {
167 SkScalar t = pow(rand->nextF(), 3);
168 *d = ((1 - t) / 60 + t / 10) * (rand->nextBool() ? screensize : -screensize);
169 }
170
171 SkScalar t = pow(rand->nextF(), 25);
172 v.fDSpin = ((1 - t) * 360 / 7.5 + t * 360 / 1.5) * (rand->nextBool() ? 1 : -1);
173 }
174
175 // Get valid front data.
176 fBackgroundAnimationTask.wait();
177 this->runAnimationTask(0, 0, screenWidth, screenHeight);
178 std::copy_n(fBackMatrices.get(), kNumPaths, fFrontMatrices.get());
179 fLastTick = 0;
180 }
181
animate(double nanos,int screenWidth,int screenHeight)182 bool animate(double nanos, int screenWidth, int screenHeight) final {
183 fBackgroundAnimationTask.wait();
184 this->swapAnimationBuffers();
185
186 const double tsec = 1e-9 * nanos;
187 const double dt = fLastTick ? (1e-9 * nanos - fLastTick) : 0;
188 fBackgroundAnimationTask.add(std::bind(&MovingGlyphAnimator::runAnimationTask, this, tsec,
189 dt, screenWidth, screenHeight));
190 fLastTick = 1e-9 * nanos;
191 return true;
192 }
193
194 /**
195 * Called on a background thread. Here we can only modify fBackMatrices.
196 */
runAnimationTask(double t,double dt,int w,int h)197 virtual void runAnimationTask(double t, double dt, int w, int h) {
198 for (int idx = 0; idx < kNumPaths; ++idx) {
199 Velocity* v = &fVelocities[idx];
200 Glyph* glyph = &fGlyphs[idx];
201 SkMatrix* backMatrix = &fBackMatrices[idx];
202
203 glyph->fPosition.fX += v->fDx * dt;
204 if (glyph->fPosition.x() < 0) {
205 glyph->fPosition.fX -= 2 * glyph->fPosition.x();
206 v->fDx = -v->fDx;
207 } else if (glyph->fPosition.x() > w) {
208 glyph->fPosition.fX -= 2 * (glyph->fPosition.x() - w);
209 v->fDx = -v->fDx;
210 }
211
212 glyph->fPosition.fY += v->fDy * dt;
213 if (glyph->fPosition.y() < 0) {
214 glyph->fPosition.fY -= 2 * glyph->fPosition.y();
215 v->fDy = -v->fDy;
216 } else if (glyph->fPosition.y() > h) {
217 glyph->fPosition.fY -= 2 * (glyph->fPosition.y() - h);
218 v->fDy = -v->fDy;
219 }
220
221 glyph->fSpin += v->fDSpin * dt;
222
223 backMatrix->setTranslate(glyph->fPosition.x(), glyph->fPosition.y());
224 backMatrix->preScale(glyph->fZoom, glyph->fZoom);
225 backMatrix->preRotate(glyph->fSpin);
226 backMatrix->preTranslate(-glyph->fMidpt.x(), -glyph->fMidpt.y());
227 }
228 }
229
swapAnimationBuffers()230 virtual void swapAnimationBuffers() {
231 std::swap(fFrontMatrices, fBackMatrices);
232 }
233
draw(SkCanvas * canvas)234 void draw(SkCanvas* canvas) override {
235 for (int i = 0; i < kNumPaths; ++i) {
236 SkAutoCanvasRestore acr(canvas, true);
237 canvas->concat(fFrontMatrices[i]);
238 canvas->drawPath(fGlyphs[i].fPath, fGlyphs[i].fPaint);
239 }
240 }
241
242 protected:
243 struct Velocity {
244 SkScalar fDx, fDy;
245 SkScalar fDSpin;
246 };
247
248 Velocity fVelocities[kNumPaths];
249 SkAutoTArray<SkMatrix> fFrontMatrices;
250 SkAutoTArray<SkMatrix> fBackMatrices;
251 SkTaskGroup fBackgroundAnimationTask;
252 double fLastTick;
253 };
254
255
256 ////////////////////////////////////////////////////////////////////////////////////////////////////
257 // Text from paths with animated control points.
258 class PathText::WavyGlyphAnimator : public PathText::MovingGlyphAnimator {
259 public:
WavyGlyphAnimator(Glyph * glyphs)260 WavyGlyphAnimator(Glyph* glyphs)
261 : MovingGlyphAnimator(glyphs)
262 , fFrontPaths(kNumPaths)
263 , fBackPaths(kNumPaths) {
264 }
265
~WavyGlyphAnimator()266 ~WavyGlyphAnimator() override {
267 fBackgroundAnimationTask.wait();
268 }
269
reset(SkRandom * rand,int screenWidth,int screenHeight)270 void reset(SkRandom* rand, int screenWidth, int screenHeight) override {
271 fWaves.reset(*rand, screenWidth, screenHeight);
272 this->MovingGlyphAnimator::reset(rand, screenWidth, screenHeight);
273 std::copy(fBackPaths.get(), fBackPaths.get() + kNumPaths, fFrontPaths.get());
274 }
275
276 /**
277 * Called on a background thread. Here we can only modify fBackPaths.
278 */
runAnimationTask(double t,double dt,int width,int height)279 void runAnimationTask(double t, double dt, int width, int height) override {
280 const float tsec = static_cast<float>(t);
281 this->MovingGlyphAnimator::runAnimationTask(t, 0.5 * dt, width, height);
282
283 for (int i = 0; i < kNumPaths; ++i) {
284 const Glyph& glyph = fGlyphs[i];
285 const SkMatrix& backMatrix = fBackMatrices[i];
286
287 const Sk2f matrix[3] = {
288 Sk2f(backMatrix.getScaleX(), backMatrix.getSkewY()),
289 Sk2f(backMatrix.getSkewX(), backMatrix.getScaleY()),
290 Sk2f(backMatrix.getTranslateX(), backMatrix.getTranslateY())
291 };
292
293 SkPath* backpath = &fBackPaths[i];
294 backpath->reset();
295 backpath->setFillType(SkPathFillType::kEvenOdd);
296
297 for (auto [verb, pts, w] : SkPathPriv::Iterate(glyph.fPath)) {
298 switch (verb) {
299 case SkPathVerb::kMove: {
300 SkPoint pt = fWaves.apply(tsec, matrix, pts[0]);
301 backpath->moveTo(pt.x(), pt.y());
302 break;
303 }
304 case SkPathVerb::kLine: {
305 SkPoint endpt = fWaves.apply(tsec, matrix, pts[1]);
306 backpath->lineTo(endpt.x(), endpt.y());
307 break;
308 }
309 case SkPathVerb::kQuad: {
310 SkPoint controlPt = fWaves.apply(tsec, matrix, pts[1]);
311 SkPoint endpt = fWaves.apply(tsec, matrix, pts[2]);
312 backpath->quadTo(controlPt.x(), controlPt.y(), endpt.x(), endpt.y());
313 break;
314 }
315 case SkPathVerb::kClose: {
316 backpath->close();
317 break;
318 }
319 case SkPathVerb::kCubic:
320 case SkPathVerb::kConic:
321 SK_ABORT("Unexpected path verb");
322 break;
323 }
324 }
325 }
326 }
327
swapAnimationBuffers()328 void swapAnimationBuffers() override {
329 this->MovingGlyphAnimator::swapAnimationBuffers();
330 std::swap(fFrontPaths, fBackPaths);
331 }
332
draw(SkCanvas * canvas)333 void draw(SkCanvas* canvas) override {
334 for (int i = 0; i < kNumPaths; ++i) {
335 canvas->drawPath(fFrontPaths[i], fGlyphs[i].fPaint);
336 }
337 }
338
339 private:
340 /**
341 * Describes 4 stacked sine waves that can offset a point as a function of wall time.
342 */
343 class Waves {
344 public:
345 void reset(SkRandom& rand, int w, int h);
346 SkPoint apply(float tsec, const Sk2f matrix[3], const SkPoint& pt) const;
347
348 private:
349 constexpr static double kAverageAngle = SK_ScalarPI / 8.0;
350 constexpr static double kMaxOffsetAngle = SK_ScalarPI / 3.0;
351
352 float fAmplitudes[4];
353 float fFrequencies[4];
354 float fDirsX[4];
355 float fDirsY[4];
356 float fSpeeds[4];
357 float fOffsets[4];
358 };
359
360 SkAutoTArray<SkPath> fFrontPaths;
361 SkAutoTArray<SkPath> fBackPaths;
362 Waves fWaves;
363 };
364
reset(SkRandom & rand,int w,int h)365 void PathText::WavyGlyphAnimator::Waves::reset(SkRandom& rand, int w, int h) {
366 const double pixelsPerMeter = 0.06 * std::max(w, h);
367 const double medianWavelength = 8 * pixelsPerMeter;
368 const double medianWaveAmplitude = 0.05 * 4 * pixelsPerMeter;
369 const double gravity = 9.8 * pixelsPerMeter;
370
371 for (int i = 0; i < 4; ++i) {
372 const double offsetAngle = (rand.nextF() * 2 - 1) * kMaxOffsetAngle;
373 const double intensity = pow(2, rand.nextF() * 2 - 1);
374 const double wavelength = intensity * medianWavelength;
375
376 fAmplitudes[i] = intensity * medianWaveAmplitude;
377 fFrequencies[i] = 2 * SK_ScalarPI / wavelength;
378 fDirsX[i] = cosf(kAverageAngle + offsetAngle);
379 fDirsY[i] = sinf(kAverageAngle + offsetAngle);
380 fSpeeds[i] = -sqrt(gravity * 2 * SK_ScalarPI / wavelength);
381 fOffsets[i] = rand.nextF() * 2 * SK_ScalarPI;
382 }
383 }
384
apply(float tsec,const Sk2f matrix[3],const SkPoint & pt) const385 SkPoint PathText::WavyGlyphAnimator::Waves::apply(float tsec, const Sk2f matrix[3],
386 const SkPoint& pt) const {
387 constexpr static int kTablePeriod = 1 << 12;
388 static float sin2table[kTablePeriod + 1];
389 static SkOnce initTable;
390 initTable([]() {
391 for (int i = 0; i <= kTablePeriod; ++i) {
392 const double sintheta = sin(i * (SK_ScalarPI / kTablePeriod));
393 sin2table[i] = static_cast<float>(sintheta * sintheta - 0.5);
394 }
395 });
396
397 const Sk4f amplitudes = Sk4f::Load(fAmplitudes);
398 const Sk4f frequencies = Sk4f::Load(fFrequencies);
399 const Sk4f dirsX = Sk4f::Load(fDirsX);
400 const Sk4f dirsY = Sk4f::Load(fDirsY);
401 const Sk4f speeds = Sk4f::Load(fSpeeds);
402 const Sk4f offsets = Sk4f::Load(fOffsets);
403
404 float devicePt[2];
405 (matrix[0] * pt.x() + matrix[1] * pt.y() + matrix[2]).store(devicePt);
406
407 const Sk4f t = (frequencies * (dirsX * devicePt[0] + dirsY * devicePt[1]) +
408 speeds * tsec +
409 offsets).abs() * (float(kTablePeriod) / float(SK_ScalarPI));
410
411 const Sk4i ipart = SkNx_cast<int>(t);
412 const Sk4f fpart = t - SkNx_cast<float>(ipart);
413
414 int32_t indices[4];
415 (ipart & (kTablePeriod-1)).store(indices);
416
417 const Sk4f left(sin2table[indices[0]], sin2table[indices[1]],
418 sin2table[indices[2]], sin2table[indices[3]]);
419 const Sk4f right(sin2table[indices[0] + 1], sin2table[indices[1] + 1],
420 sin2table[indices[2] + 1], sin2table[indices[3] + 1]);
421 const Sk4f height = amplitudes * (left * (1.f - fpart) + right * fpart);
422
423 Sk4f dy = height * dirsY;
424 Sk4f dx = height * dirsX;
425
426 float offsetY[4], offsetX[4];
427 (dy + SkNx_shuffle<2,3,0,1>(dy)).store(offsetY); // accumulate.
428 (dx + SkNx_shuffle<2,3,0,1>(dx)).store(offsetX);
429
430 return {devicePt[0] + offsetY[0] + offsetY[1], devicePt[1] - offsetX[0] - offsetX[1]};
431 }
432
onChar(SkUnichar unichar)433 bool PathText::onChar(SkUnichar unichar) {
434 switch (unichar) {
435 case 'X':
436 fDoClip = !fDoClip;
437 return true;
438 case 'S':
439 fGlyphAnimator = std::make_unique<GlyphAnimator>(fGlyphs);
440 fGlyphAnimator->reset(&fRand, this->width(), this->height());
441 return true;
442 case 'M':
443 fGlyphAnimator = std::make_unique<MovingGlyphAnimator>(fGlyphs);
444 fGlyphAnimator->reset(&fRand, this->width(), this->height());
445 return true;
446 case 'W':
447 fGlyphAnimator = std::make_unique<WavyGlyphAnimator>(fGlyphs);
448 fGlyphAnimator->reset(&fRand, this->width(), this->height());
449 return true;
450 }
451 return false;
452 }
453
454 ////////////////////////////////////////////////////////////////////////////////////////////////////
455
MakePathTextSample()456 Sample* MakePathTextSample() { return new PathText; }
457 static SampleRegistry gPathTextSample(MakePathTextSample);
458