blob: 77d132d9af7c824aa287a6a9b442a5678f1232ad [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"
17#include "tools/timer/AnimTimer.h"
18#include "tools/timer/Timer.h"
Jim Van Verthfc4f7682018-01-25 16:26:25 -050019
20#if SK_SUPPORT_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/gpu/GrContext.h"
Jim Van Verthfc4f7682018-01-25 16:26:25 -050022#endif
23
24// Create an animation of a bunch of letters that rotate in place. This is intended to stress
25// the glyph atlas and test that we don't see corruption or bad slowdowns.
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040026class FlutterAnimateView : public Sample {
Jim Van Verthfc4f7682018-01-25 16:26:25 -050027public:
28 FlutterAnimateView() : fCurrTime(0), fResetTime(0) {}
29
30protected:
31 void onOnceBeforeDraw() override {
32 fTypeface = SkTypeface::MakeFromFile("/skimages/samplefont.ttf");
33 initChars();
34 }
35
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040036 bool onQuery(Sample::Event* evt) override {
37 if (Sample::TitleQ(*evt)) {
38 Sample::TitleR(evt, "FlutterAnimate");
Jim Van Verthfc4f7682018-01-25 16:26:25 -050039 return true;
40 }
41
42 return this->INHERITED::onQuery(evt);
43 }
44
45 void onDrawContent(SkCanvas* canvas) override {
Hal Canary4484b8f2019-01-08 14:00:08 -050046 SkFont font(fTypeface, 50);
Jim Van Verthfc4f7682018-01-25 16:26:25 -050047 SkPaint paint;
Jim Van Verthfc4f7682018-01-25 16:26:25 -050048 paint.setFilterQuality(kMedium_SkFilterQuality);
Jim Van Verthfc4f7682018-01-25 16:26:25 -050049
50 // rough center of each glyph
51 static constexpr auto kMidX = 35;
52 static constexpr auto kMidY = 50;
53
54 canvas->clear(SK_ColorWHITE);
55 for (int i = 0; i < kNumChars; ++i) {
56 canvas->save();
57 double rot = SkScalarInterp(fChars[i].fStartRotation, fChars[i].fEndRotation,
58 fCurrTime/kDuration);
59 canvas->translate(fChars[i].fPosition.fX + kMidX, fChars[i].fPosition.fY - kMidY);
60 canvas->rotate(SkRadiansToDegrees(rot));
61 canvas->translate(-35,+50);
Hal Canary4484b8f2019-01-08 14:00:08 -050062 canvas->drawString(fChars[i].fChar, 0, 0, font, paint);
Jim Van Verthfc4f7682018-01-25 16:26:25 -050063 canvas->restore();
64 }
65 }
66
Mike Kleincd5104e2019-03-20 11:55:08 -050067 bool onAnimate(const AnimTimer& timer) override {
Jim Van Verthfc4f7682018-01-25 16:26:25 -050068 fCurrTime = timer.secs() - fResetTime;
69 if (fCurrTime > kDuration) {
70 this->initChars();
71 fResetTime = timer.secs();
72 fCurrTime = 0;
73 }
74
75 return true;
76 }
77
78private:
79 void initChars() {
80 for (int i = 0; i < kNumChars; ++i) {
81 char c = fRand.nextULessThan(26) + 65;
82 fChars[i].fChar[0] = c;
83 fChars[i].fChar[1] = '\0';
84 fChars[i].fPosition = SkPoint::Make(fRand.nextF()*748 + 10, fRand.nextF()*1004 + 10);
85 fChars[i].fStartRotation = fRand.nextF();
86 fChars[i].fEndRotation = fRand.nextF() * 20 - 10;
87 }
88 }
89
90 static constexpr double kDuration = 5.0;
91 double fCurrTime;
92 double fResetTime;
93 SkRandom fRand;
94
95 struct AnimatedChar {
96 char fChar[2];
97 SkPoint fPosition;
98 SkScalar fStartRotation;
99 SkScalar fEndRotation;
100 };
101 sk_sp<SkTypeface> fTypeface;
102 static constexpr int kNumChars = 40;
103 AnimatedChar fChars[kNumChars];
104
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400105 typedef Sample INHERITED;
Jim Van Verthfc4f7682018-01-25 16:26:25 -0500106};
107
108//////////////////////////////////////////////////////////////////////////////
109
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400110DEF_SAMPLE( return new FlutterAnimateView(); )