blob: a8b65e34da7a09acb90da18c1bf7a9780253661e [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"
Chris Daltonde500372020-05-05 15:06:30 -060013#include "src/core/SkPathPriv.h"
Herb Derby81e84a62020-02-14 11:47:35 -050014#include "src/core/SkScalerCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkStrikeCache.h"
Herb Derbybaf64782019-04-17 18:01:04 -040016#include "src/core/SkStrikeSpec.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkTaskGroup.h"
18#include "tools/ToolUtils.h"
csmartdaltoned4984b2017-02-13 14:57:28 -070019
20////////////////////////////////////////////////////////////////////////////////////////////////////
21// Static text from paths.
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040022class PathText : public Sample {
csmartdaltoned4984b2017-02-13 14:57:28 -070023public:
24 constexpr static int kNumPaths = 1500;
25 virtual const char* getName() const { return "PathText"; }
26
Ben Wagner9d289e22018-09-17 15:25:18 -040027 PathText() {}
28
29 virtual void reset() {
30 for (Glyph& glyph : fGlyphs) {
31 glyph.reset(fRand, this->width(), this->height());
32 }
33 }
34
35 void onOnceBeforeDraw() final {
Mike Reed32c60662018-11-28 10:28:07 -050036 SkFont defaultFont;
Herb Derbyac718272019-06-06 14:33:12 -040037 SkStrikeSpec strikeSpec = SkStrikeSpec::MakeWithNoDevice(defaultFont);
Herb Derbya64f5b22020-02-24 14:35:51 -050038 auto strike = strikeSpec.findOrCreateStrike();
csmartdaltoned4984b2017-02-13 14:57:28 -070039 SkPath glyphPaths[52];
40 for (int i = 0; i < 52; ++i) {
41 // I and l are rects on OS X ...
42 char c = "aQCDEFGH7JKLMNOPBRZTUVWXYSAbcdefghijk1mnopqrstuvwxyz"[i];
Mike Reedfdb876d2019-02-01 09:55:20 -050043 SkPackedGlyphID id(defaultFont.unicharToGlyph(c));
Herb Derbya64f5b22020-02-24 14:35:51 -050044 sk_ignore_unused_variable(strike->getScalerContext()->getPath(id, &glyphPaths[i]));
csmartdaltoned4984b2017-02-13 14:57:28 -070045 }
46
47 for (int i = 0; i < kNumPaths; ++i) {
48 const SkPath& p = glyphPaths[i % 52];
49 fGlyphs[i].init(fRand, p);
50 }
csmartdaltoned4984b2017-02-13 14:57:28 -070051
Ben Wagner9d289e22018-09-17 15:25:18 -040052 this->INHERITED::onOnceBeforeDraw();
53 this->reset();
csmartdaltoned4984b2017-02-13 14:57:28 -070054 }
csmartdaltoned4984b2017-02-13 14:57:28 -070055 void onSizeChange() final { this->INHERITED::onSizeChange(); this->reset(); }
56
Hal Canary8a027312019-07-03 10:55:44 -040057 SkString name() override { return SkString(this->getName()); }
58
Hal Canary6cc65e12019-07-03 15:53:04 -040059 bool onChar(SkUnichar unichar) override {
Chris Dalton7c02cc72017-11-06 14:10:54 -070060 if (unichar == 'X') {
61 fDoClip = !fDoClip;
Chris Dalton7c02cc72017-11-06 14:10:54 -070062 return true;
63 }
Hal Canary6cc65e12019-07-03 15:53:04 -040064 return false;
csmartdaltoned4984b2017-02-13 14:57:28 -070065 }
66
67 void onDrawContent(SkCanvas* canvas) override {
Chris Dalton7c02cc72017-11-06 14:10:54 -070068 if (fDoClip) {
Chris Daltona32a3c32017-12-05 10:05:21 -070069 SkPath deviceSpaceClipPath = fClipPath;
70 deviceSpaceClipPath.transform(SkMatrix::MakeScale(this->width(), this->height()));
Chris Dalton7c02cc72017-11-06 14:10:54 -070071 canvas->save();
Chris Daltona32a3c32017-12-05 10:05:21 -070072 canvas->clipPath(deviceSpaceClipPath, SkClipOp::kDifference, true);
Chris Dalton7c02cc72017-11-06 14:10:54 -070073 canvas->clear(SK_ColorBLACK);
74 canvas->restore();
Chris Daltona32a3c32017-12-05 10:05:21 -070075 canvas->clipPath(deviceSpaceClipPath, SkClipOp::kIntersect, true);
Chris Dalton7c02cc72017-11-06 14:10:54 -070076 }
77 this->drawGlyphs(canvas);
78 }
79
80 virtual void drawGlyphs(SkCanvas* canvas) {
csmartdaltoned4984b2017-02-13 14:57:28 -070081 for (Glyph& glyph : fGlyphs) {
82 SkAutoCanvasRestore acr(canvas, true);
83 canvas->translate(glyph.fPosition.x(), glyph.fPosition.y());
84 canvas->scale(glyph.fZoom, glyph.fZoom);
85 canvas->rotate(glyph.fSpin);
86 canvas->translate(-glyph.fMidpt.x(), -glyph.fMidpt.y());
87 canvas->drawPath(glyph.fPath, glyph.fPaint);
88 }
89 }
90
91protected:
92 struct Glyph {
93 void init(SkRandom& rand, const SkPath& path);
94 void reset(SkRandom& rand, int w, int h);
95
96 SkPath fPath;
97 SkPaint fPaint;
98 SkPoint fPosition;
99 SkScalar fZoom;
100 SkScalar fSpin;
101 SkPoint fMidpt;
102 };
103
104 Glyph fGlyphs[kNumPaths];
Chris Dalton7c02cc72017-11-06 14:10:54 -0700105 SkRandom fRand{25};
Mike Kleinea3f0142019-03-20 11:12:10 -0500106 SkPath fClipPath = ToolUtils::make_star(SkRect{0, 0, 1, 1}, 11, 3);
Chris Dalton7c02cc72017-11-06 14:10:54 -0700107 bool fDoClip = false;
csmartdaltoned4984b2017-02-13 14:57:28 -0700108
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400109 typedef Sample INHERITED;
csmartdaltoned4984b2017-02-13 14:57:28 -0700110};
111
112void PathText::Glyph::init(SkRandom& rand, const SkPath& path) {
113 fPath = path;
114 fPaint.setAntiAlias(true);
115 fPaint.setColor(rand.nextU() | 0x80808080);
116}
117
118void PathText::Glyph::reset(SkRandom& rand, int w, int h) {
Brian Osman788b9162020-02-07 10:36:46 -0500119 int screensize = std::max(w, h);
csmartdaltoned4984b2017-02-13 14:57:28 -0700120 const SkRect& bounds = fPath.getBounds();
121 SkScalar t;
122
123 fPosition = {rand.nextF() * w, rand.nextF() * h};
124 t = pow(rand.nextF(), 100);
125 fZoom = ((1 - t) * screensize / 50 + t * screensize / 3) /
Brian Osman788b9162020-02-07 10:36:46 -0500126 std::max(bounds.width(), bounds.height());
csmartdaltoned4984b2017-02-13 14:57:28 -0700127 fSpin = rand.nextF() * 360;
128 fMidpt = {bounds.centerX(), bounds.centerY()};
129}
130
131////////////////////////////////////////////////////////////////////////////////////////////////////
132// Text from paths with animated transformation matrices.
133class MovingPathText : public PathText {
134public:
135 const char* getName() const override { return "MovingPathText"; }
136
137 MovingPathText()
138 : fFrontMatrices(kNumPaths)
139 , fBackMatrices(kNumPaths) {
140 }
141
Brian Salomond3b65972017-03-22 12:05:03 -0400142 ~MovingPathText() override {
csmartdaltoned4984b2017-02-13 14:57:28 -0700143 fBackgroundAnimationTask.wait();
144 }
145
146 void reset() override {
Brian Osman788b9162020-02-07 10:36:46 -0500147 const SkScalar screensize = static_cast<SkScalar>(std::max(this->width(), this->height()));
csmartdaltoned4984b2017-02-13 14:57:28 -0700148 this->INHERITED::reset();
149
150 for (auto& v : fVelocities) {
151 for (SkScalar* d : {&v.fDx, &v.fDy}) {
152 SkScalar t = pow(fRand.nextF(), 3);
153 *d = ((1 - t) / 60 + t / 10) * (fRand.nextBool() ? screensize : -screensize);
154 }
155
156 SkScalar t = pow(fRand.nextF(), 25);
157 v.fDSpin = ((1 - t) * 360 / 7.5 + t * 360 / 1.5) * (fRand.nextBool() ? 1 : -1);
158 }
159
160 // Get valid front data.
161 fBackgroundAnimationTask.wait();
162 this->runAnimationTask(0, 0, this->width(), this->height());
163 memcpy(fFrontMatrices, fBackMatrices, kNumPaths * sizeof(SkMatrix));
164 fLastTick = 0;
165 }
166
Hal Canary41248072019-07-11 16:32:53 -0400167 bool onAnimate(double nanos) final {
csmartdaltoned4984b2017-02-13 14:57:28 -0700168 fBackgroundAnimationTask.wait();
169 this->swapAnimationBuffers();
170
Hal Canary41248072019-07-11 16:32:53 -0400171 const double tsec = 1e-9 * nanos;
172 const double dt = fLastTick ? (1e-9 * nanos - fLastTick) : 0;
csmartdaltoned4984b2017-02-13 14:57:28 -0700173 fBackgroundAnimationTask.add(std::bind(&MovingPathText::runAnimationTask, this, tsec,
174 dt, this->width(), this->height()));
Hal Canary41248072019-07-11 16:32:53 -0400175 fLastTick = 1e-9 * nanos;
csmartdaltoned4984b2017-02-13 14:57:28 -0700176 return true;
177 }
178
179 /**
180 * Called on a background thread. Here we can only modify fBackMatrices.
181 */
182 virtual void runAnimationTask(double t, double dt, int w, int h) {
183 for (int idx = 0; idx < kNumPaths; ++idx) {
184 Velocity* v = &fVelocities[idx];
185 Glyph* glyph = &fGlyphs[idx];
186 SkMatrix* backMatrix = &fBackMatrices[idx];
187
188 glyph->fPosition.fX += v->fDx * dt;
189 if (glyph->fPosition.x() < 0) {
190 glyph->fPosition.fX -= 2 * glyph->fPosition.x();
191 v->fDx = -v->fDx;
192 } else if (glyph->fPosition.x() > w) {
193 glyph->fPosition.fX -= 2 * (glyph->fPosition.x() - w);
194 v->fDx = -v->fDx;
195 }
196
197 glyph->fPosition.fY += v->fDy * dt;
198 if (glyph->fPosition.y() < 0) {
199 glyph->fPosition.fY -= 2 * glyph->fPosition.y();
200 v->fDy = -v->fDy;
201 } else if (glyph->fPosition.y() > h) {
202 glyph->fPosition.fY -= 2 * (glyph->fPosition.y() - h);
203 v->fDy = -v->fDy;
204 }
205
206 glyph->fSpin += v->fDSpin * dt;
207
208 backMatrix->setTranslate(glyph->fPosition.x(), glyph->fPosition.y());
209 backMatrix->preScale(glyph->fZoom, glyph->fZoom);
210 backMatrix->preRotate(glyph->fSpin);
211 backMatrix->preTranslate(-glyph->fMidpt.x(), -glyph->fMidpt.y());
212 }
213 }
214
215 virtual void swapAnimationBuffers() {
216 std::swap(fFrontMatrices, fBackMatrices);
217 }
218
Chris Dalton7c02cc72017-11-06 14:10:54 -0700219 void drawGlyphs(SkCanvas* canvas) override {
csmartdaltoned4984b2017-02-13 14:57:28 -0700220 for (int i = 0; i < kNumPaths; ++i) {
221 SkAutoCanvasRestore acr(canvas, true);
222 canvas->concat(fFrontMatrices[i]);
223 canvas->drawPath(fGlyphs[i].fPath, fGlyphs[i].fPaint);
224 }
225 }
226
227protected:
228 struct Velocity {
229 SkScalar fDx, fDy;
230 SkScalar fDSpin;
231 };
232
233 Velocity fVelocities[kNumPaths];
234 SkAutoTMalloc<SkMatrix> fFrontMatrices;
235 SkAutoTMalloc<SkMatrix> fBackMatrices;
236 SkTaskGroup fBackgroundAnimationTask;
237 double fLastTick;
238
239 typedef PathText INHERITED;
240};
241
242
243////////////////////////////////////////////////////////////////////////////////////////////////////
244// Text from paths with animated control points.
245class WavyPathText : public MovingPathText {
246public:
247 const char* getName() const override { return "WavyPathText"; }
248
249 WavyPathText()
250 : fFrontPaths(kNumPaths)
251 , fBackPaths(kNumPaths) {}
252
Brian Salomond3b65972017-03-22 12:05:03 -0400253 ~WavyPathText() override {
csmartdaltoned4984b2017-02-13 14:57:28 -0700254 fBackgroundAnimationTask.wait();
255 }
256
257 void reset() override {
258 fWaves.reset(fRand, this->width(), this->height());
259 this->INHERITED::reset();
260 std::copy(fBackPaths.get(), fBackPaths.get() + kNumPaths, fFrontPaths.get());
261 }
262
263 /**
264 * Called on a background thread. Here we can only modify fBackPaths.
265 */
266 void runAnimationTask(double t, double dt, int w, int h) override {
267 const float tsec = static_cast<float>(t);
268 this->INHERITED::runAnimationTask(t, 0.5 * dt, w, h);
269
270 for (int i = 0; i < kNumPaths; ++i) {
271 const Glyph& glyph = fGlyphs[i];
272 const SkMatrix& backMatrix = fBackMatrices[i];
273
274 const Sk2f matrix[3] = {
275 Sk2f(backMatrix.getScaleX(), backMatrix.getSkewY()),
276 Sk2f(backMatrix.getSkewX(), backMatrix.getScaleY()),
277 Sk2f(backMatrix.getTranslateX(), backMatrix.getTranslateY())
278 };
279
280 SkPath* backpath = &fBackPaths[i];
281 backpath->reset();
Mike Reed7d34dc72019-11-26 12:17:17 -0500282 backpath->setFillType(SkPathFillType::kEvenOdd);
csmartdaltoned4984b2017-02-13 14:57:28 -0700283
Chris Daltonde500372020-05-05 15:06:30 -0600284 for (auto [verb, pts, w] : SkPathPriv::Iterate(glyph.fPath)) {
csmartdaltoned4984b2017-02-13 14:57:28 -0700285 switch (verb) {
Chris Daltonde500372020-05-05 15:06:30 -0600286 case SkPathVerb::kMove: {
csmartdaltoned4984b2017-02-13 14:57:28 -0700287 SkPoint pt = fWaves.apply(tsec, matrix, pts[0]);
288 backpath->moveTo(pt.x(), pt.y());
289 break;
290 }
Chris Daltonde500372020-05-05 15:06:30 -0600291 case SkPathVerb::kLine: {
csmartdaltoned4984b2017-02-13 14:57:28 -0700292 SkPoint endpt = fWaves.apply(tsec, matrix, pts[1]);
293 backpath->lineTo(endpt.x(), endpt.y());
294 break;
295 }
Chris Daltonde500372020-05-05 15:06:30 -0600296 case SkPathVerb::kQuad: {
csmartdaltoned4984b2017-02-13 14:57:28 -0700297 SkPoint controlPt = fWaves.apply(tsec, matrix, pts[1]);
298 SkPoint endpt = fWaves.apply(tsec, matrix, pts[2]);
299 backpath->quadTo(controlPt.x(), controlPt.y(), endpt.x(), endpt.y());
300 break;
301 }
Chris Daltonde500372020-05-05 15:06:30 -0600302 case SkPathVerb::kClose: {
csmartdaltoned4984b2017-02-13 14:57:28 -0700303 backpath->close();
304 break;
305 }
Chris Daltonde500372020-05-05 15:06:30 -0600306 case SkPathVerb::kCubic:
307 case SkPathVerb::kConic:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400308 SK_ABORT("Unexpected path verb");
csmartdaltoned4984b2017-02-13 14:57:28 -0700309 break;
310 }
311 }
312 }
313 }
314
315 void swapAnimationBuffers() override {
316 this->INHERITED::swapAnimationBuffers();
Hal Canary67362362018-04-24 13:58:37 -0400317 std::swap(fFrontPaths, fBackPaths);
csmartdaltoned4984b2017-02-13 14:57:28 -0700318 }
319
Chris Dalton7c02cc72017-11-06 14:10:54 -0700320 void drawGlyphs(SkCanvas* canvas) override {
csmartdaltoned4984b2017-02-13 14:57:28 -0700321 for (int i = 0; i < kNumPaths; ++i) {
322 canvas->drawPath(fFrontPaths[i], fGlyphs[i].fPaint);
323 }
324 }
325
326private:
327 /**
328 * Describes 4 stacked sine waves that can offset a point as a function of wall time.
329 */
330 class Waves {
331 public:
332 void reset(SkRandom& rand, int w, int h);
333 SkPoint apply(float tsec, const Sk2f matrix[3], const SkPoint& pt) const;
334
335 private:
336 constexpr static double kAverageAngle = SK_ScalarPI / 8.0;
337 constexpr static double kMaxOffsetAngle = SK_ScalarPI / 3.0;
338
339 float fAmplitudes[4];
340 float fFrequencies[4];
341 float fDirsX[4];
342 float fDirsY[4];
343 float fSpeeds[4];
344 float fOffsets[4];
345 };
346
347 SkAutoTArray<SkPath> fFrontPaths;
348 SkAutoTArray<SkPath> fBackPaths;
349 Waves fWaves;
350
351 typedef MovingPathText INHERITED;
352};
353
354void WavyPathText::Waves::reset(SkRandom& rand, int w, int h) {
Brian Osman788b9162020-02-07 10:36:46 -0500355 const double pixelsPerMeter = 0.06 * std::max(w, h);
csmartdaltoned4984b2017-02-13 14:57:28 -0700356 const double medianWavelength = 8 * pixelsPerMeter;
357 const double medianWaveAmplitude = 0.05 * 4 * pixelsPerMeter;
358 const double gravity = 9.8 * pixelsPerMeter;
359
360 for (int i = 0; i < 4; ++i) {
361 const double offsetAngle = (rand.nextF() * 2 - 1) * kMaxOffsetAngle;
362 const double intensity = pow(2, rand.nextF() * 2 - 1);
363 const double wavelength = intensity * medianWavelength;
364
365 fAmplitudes[i] = intensity * medianWaveAmplitude;
366 fFrequencies[i] = 2 * SK_ScalarPI / wavelength;
367 fDirsX[i] = cosf(kAverageAngle + offsetAngle);
368 fDirsY[i] = sinf(kAverageAngle + offsetAngle);
369 fSpeeds[i] = -sqrt(gravity * 2 * SK_ScalarPI / wavelength);
370 fOffsets[i] = rand.nextF() * 2 * SK_ScalarPI;
371 }
372}
373
374SkPoint WavyPathText::Waves::apply(float tsec, const Sk2f matrix[3], const SkPoint& pt) const {
Chris Dalton95cf1662017-06-20 16:32:59 -0700375 constexpr static int kTablePeriod = 1 << 12;
376 static float sin2table[kTablePeriod + 1];
csmartdaltoned4984b2017-02-13 14:57:28 -0700377 static SkOnce initTable;
378 initTable([]() {
Chris Dalton95cf1662017-06-20 16:32:59 -0700379 for (int i = 0; i <= kTablePeriod; ++i) {
380 const double sintheta = sin(i * (SK_ScalarPI / kTablePeriod));
csmartdaltoned4984b2017-02-13 14:57:28 -0700381 sin2table[i] = static_cast<float>(sintheta * sintheta - 0.5);
382 }
383 });
384
385 const Sk4f amplitudes = Sk4f::Load(fAmplitudes);
386 const Sk4f frequencies = Sk4f::Load(fFrequencies);
387 const Sk4f dirsX = Sk4f::Load(fDirsX);
388 const Sk4f dirsY = Sk4f::Load(fDirsY);
389 const Sk4f speeds = Sk4f::Load(fSpeeds);
390 const Sk4f offsets = Sk4f::Load(fOffsets);
391
392 float devicePt[2];
393 (matrix[0] * pt.x() + matrix[1] * pt.y() + matrix[2]).store(devicePt);
394
395 const Sk4f t = (frequencies * (dirsX * devicePt[0] + dirsY * devicePt[1]) +
396 speeds * tsec +
Chris Dalton95cf1662017-06-20 16:32:59 -0700397 offsets).abs() * (float(kTablePeriod) / float(SK_ScalarPI));
csmartdaltoned4984b2017-02-13 14:57:28 -0700398
399 const Sk4i ipart = SkNx_cast<int>(t);
400 const Sk4f fpart = t - SkNx_cast<float>(ipart);
401
402 int32_t indices[4];
Chris Dalton95cf1662017-06-20 16:32:59 -0700403 (ipart & (kTablePeriod-1)).store(indices);
csmartdaltoned4984b2017-02-13 14:57:28 -0700404
405 const Sk4f left(sin2table[indices[0]], sin2table[indices[1]],
406 sin2table[indices[2]], sin2table[indices[3]]);
407 const Sk4f right(sin2table[indices[0] + 1], sin2table[indices[1] + 1],
408 sin2table[indices[2] + 1], sin2table[indices[3] + 1]);
409 const Sk4f height = amplitudes * (left * (1.f - fpart) + right * fpart);
410
411 Sk4f dy = height * dirsY;
412 Sk4f dx = height * dirsX;
413
414 float offsetY[4], offsetX[4];
415 (dy + SkNx_shuffle<2,3,0,1>(dy)).store(offsetY); // accumulate.
Brian Salomon23356442018-11-30 15:33:19 -0500416 (dx + SkNx_shuffle<2,3,0,1>(dx)).store(offsetX);
csmartdaltoned4984b2017-02-13 14:57:28 -0700417
418 return {devicePt[0] + offsetY[0] + offsetY[1], devicePt[1] - offsetX[0] - offsetX[1]};
419}
420
421////////////////////////////////////////////////////////////////////////////////////////////////////
422
csmartdaltoned4984b2017-02-13 14:57:28 -0700423DEF_SAMPLE( return new PathText; )
Chris Dalton2b598902020-03-25 10:50:35 -0600424DEF_SAMPLE( return new MovingPathText; )
425
426Sample* MakeWavyPathTextSample() { return new WavyPathText; }
427static SampleRegistry WavyPathText(MakeWavyPathTextSample);