blob: 67506d28e97922616b77e8dc57634a9869b5a109 [file] [log] [blame]
Jim Van Verthfc4f7682018-01-25 16:26:25 -05001/*
2 * Copyright 2018 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/SkColorFilter.h"
10#include "include/core/SkColorPriv.h"
11#include "include/core/SkFont.h"
12#include "include/core/SkImage.h"
13#include "include/core/SkTime.h"
14#include "include/core/SkTypeface.h"
15#include "include/utils/SkRandom.h"
16#include "samplecode/Sample.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "tools/timer/Timer.h"
Jim Van Verthfc4f7682018-01-25 16:26:25 -050018
Jim Van Verthfc4f7682018-01-25 16:26:25 -050019// Create an animation of a bunch of letters that rotate in place. This is intended to stress
20// the glyph atlas and test that we don't see corruption or bad slowdowns.
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040021class FlutterAnimateView : public Sample {
Jim Van Verthfc4f7682018-01-25 16:26:25 -050022public:
23 FlutterAnimateView() : fCurrTime(0), fResetTime(0) {}
24
25protected:
26 void onOnceBeforeDraw() override {
27 fTypeface = SkTypeface::MakeFromFile("/skimages/samplefont.ttf");
28 initChars();
29 }
30
Hal Canary8a027312019-07-03 10:55:44 -040031 SkString name() override { return SkString("FlutterAnimate"); }
Jim Van Verthfc4f7682018-01-25 16:26:25 -050032
33 void onDrawContent(SkCanvas* canvas) override {
Hal Canary4484b8f2019-01-08 14:00:08 -050034 SkFont font(fTypeface, 50);
Jim Van Verthfc4f7682018-01-25 16:26:25 -050035 SkPaint paint;
Jim Van Verthfc4f7682018-01-25 16:26:25 -050036 paint.setFilterQuality(kMedium_SkFilterQuality);
Jim Van Verthfc4f7682018-01-25 16:26:25 -050037
38 // rough center of each glyph
39 static constexpr auto kMidX = 35;
40 static constexpr auto kMidY = 50;
41
42 canvas->clear(SK_ColorWHITE);
43 for (int i = 0; i < kNumChars; ++i) {
44 canvas->save();
45 double rot = SkScalarInterp(fChars[i].fStartRotation, fChars[i].fEndRotation,
46 fCurrTime/kDuration);
47 canvas->translate(fChars[i].fPosition.fX + kMidX, fChars[i].fPosition.fY - kMidY);
48 canvas->rotate(SkRadiansToDegrees(rot));
49 canvas->translate(-35,+50);
Hal Canary4484b8f2019-01-08 14:00:08 -050050 canvas->drawString(fChars[i].fChar, 0, 0, font, paint);
Jim Van Verthfc4f7682018-01-25 16:26:25 -050051 canvas->restore();
52 }
53 }
54
Hal Canary41248072019-07-11 16:32:53 -040055 bool onAnimate(double nanos) override {
56 fCurrTime = 1e-9 * nanos - fResetTime;
Jim Van Verthfc4f7682018-01-25 16:26:25 -050057 if (fCurrTime > kDuration) {
58 this->initChars();
Hal Canary41248072019-07-11 16:32:53 -040059 fResetTime = 1e-9 * nanos;
Jim Van Verthfc4f7682018-01-25 16:26:25 -050060 fCurrTime = 0;
61 }
62
63 return true;
64 }
65
66private:
67 void initChars() {
68 for (int i = 0; i < kNumChars; ++i) {
69 char c = fRand.nextULessThan(26) + 65;
70 fChars[i].fChar[0] = c;
71 fChars[i].fChar[1] = '\0';
72 fChars[i].fPosition = SkPoint::Make(fRand.nextF()*748 + 10, fRand.nextF()*1004 + 10);
73 fChars[i].fStartRotation = fRand.nextF();
74 fChars[i].fEndRotation = fRand.nextF() * 20 - 10;
75 }
76 }
77
78 static constexpr double kDuration = 5.0;
79 double fCurrTime;
80 double fResetTime;
81 SkRandom fRand;
82
83 struct AnimatedChar {
84 char fChar[2];
85 SkPoint fPosition;
86 SkScalar fStartRotation;
87 SkScalar fEndRotation;
88 };
89 sk_sp<SkTypeface> fTypeface;
90 static constexpr int kNumChars = 40;
91 AnimatedChar fChars[kNumChars];
92
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040093 typedef Sample INHERITED;
Jim Van Verthfc4f7682018-01-25 16:26:25 -050094};
95
96//////////////////////////////////////////////////////////////////////////////
97
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040098DEF_SAMPLE( return new FlutterAnimateView(); )