Flutter Engine
The Flutter Engine
FlutterAnimateSlide.cpp
Go to the documentation of this file.
1/*
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
11#include "include/core/SkFont.h"
15#include "src/base/SkRandom.h"
16#include "src/base/SkTime.h"
18#include "tools/timer/Timer.h"
19#include "tools/viewer/Slide.h"
20
21// Create an animation of a bunch of letters that rotate in place. This is intended to stress
22// the glyph atlas and test that we don't see corruption or bad slowdowns.
23class FlutterAnimateView : public Slide {
24public:
25 FlutterAnimateView() : fCurrTime(0), fResetTime(0) { fName = "FlutterAnimate"; }
26
27public:
28 void load(SkScalar w, SkScalar h) override {
29 fTypeface = ToolUtils::TestFontMgr()->makeFromFile("/skimages/samplefont.ttf");
30 initChars();
31 }
32
33 void draw(SkCanvas* canvas) override {
34 SkFont font(fTypeface, 50);
36
37 // rough center of each glyph
38 static constexpr auto kMidX = 35;
39 static constexpr auto kMidY = 50;
40
41 canvas->clear(SK_ColorWHITE);
42 for (int i = 0; i < kNumChars; ++i) {
43 canvas->save();
44 double rot = SkScalarInterp(fChars[i].fStartRotation, fChars[i].fEndRotation,
45 fCurrTime/kDuration);
46 canvas->translate(fChars[i].fPosition.fX + kMidX, fChars[i].fPosition.fY - kMidY);
47 canvas->rotate(SkRadiansToDegrees(rot));
48 canvas->translate(-35,+50);
49 canvas->drawString(fChars[i].fChar, 0, 0, font, paint);
50 canvas->restore();
51 }
52 }
53
54 bool animate(double nanos) override {
55 fCurrTime = 1e-9 * nanos - fResetTime;
56 if (fCurrTime > kDuration) {
57 this->initChars();
58 fResetTime = 1e-9 * nanos;
59 fCurrTime = 0;
60 }
61
62 return true;
63 }
64
65private:
66 void initChars() {
67 for (int i = 0; i < kNumChars; ++i) {
68 char c = fRand.nextULessThan(26) + 65;
69 fChars[i].fChar[0] = c;
70 fChars[i].fChar[1] = '\0';
71 fChars[i].fPosition = SkPoint::Make(fRand.nextF()*748 + 10, fRand.nextF()*1004 + 10);
72 fChars[i].fStartRotation = fRand.nextF();
73 fChars[i].fEndRotation = fRand.nextF() * 20 - 10;
74 }
75 }
76
77 inline static constexpr double kDuration = 5.0;
78 double fCurrTime;
79 double fResetTime;
80 SkRandom fRand;
81
82 struct AnimatedChar {
83 char fChar[2];
84 SkPoint fPosition;
85 SkScalar fStartRotation;
86 SkScalar fEndRotation;
87 };
88 sk_sp<SkTypeface> fTypeface;
89 inline static constexpr int kNumChars = 40;
90 AnimatedChar fChars[kNumChars];
91};
92
93//////////////////////////////////////////////////////////////////////////////
94
95DEF_SLIDE( return new FlutterAnimateView(); )
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
#define SkRadiansToDegrees(radians)
Definition: SkScalar.h:78
static SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t)
Definition: SkScalar.h:131
#define DEF_SLIDE(code)
Definition: Slide.h:25
void draw(SkCanvas *canvas) override
void load(SkScalar w, SkScalar h) override
bool animate(double nanos) override
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void clear(SkColor color)
Definition: SkCanvas.h:1199
void rotate(SkScalar degrees)
Definition: SkCanvas.cpp:1300
int save()
Definition: SkCanvas.cpp:447
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.h:1803
sk_sp< SkTypeface > makeFromFile(const char path[], int ttcIndex=0) const
Definition: SkFontMgr.cpp:143
Definition: SkFont.h:35
float nextF()
Definition: SkRandom.h:55
uint32_t nextULessThan(uint32_t count)
Definition: SkRandom.h:93
Definition: Slide.h:29
SkString fName
Definition: Slide.h:54
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
sk_sp< SkFontMgr > TestFontMgr()
font
Font Metadata and Metrics.
SkScalar w
SkScalar h
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173