2018-01-25 21:26:25 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkColorPriv.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkTime.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "tools/timer/Timer.h"
|
2018-01-25 21:26:25 +00:00
|
|
|
|
|
|
|
// Create an animation of a bunch of letters that rotate in place. This is intended to stress
|
|
|
|
// the glyph atlas and test that we don't see corruption or bad slowdowns.
|
2018-08-08 15:36:17 +00:00
|
|
|
class FlutterAnimateView : public Sample {
|
2018-01-25 21:26:25 +00:00
|
|
|
public:
|
|
|
|
FlutterAnimateView() : fCurrTime(0), fResetTime(0) {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
fTypeface = SkTypeface::MakeFromFile("/skimages/samplefont.ttf");
|
|
|
|
initChars();
|
|
|
|
}
|
|
|
|
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("FlutterAnimate"); }
|
2018-01-25 21:26:25 +00:00
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2019-01-08 19:00:08 +00:00
|
|
|
SkFont font(fTypeface, 50);
|
2018-01-25 21:26:25 +00:00
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
// rough center of each glyph
|
|
|
|
static constexpr auto kMidX = 35;
|
|
|
|
static constexpr auto kMidY = 50;
|
|
|
|
|
|
|
|
canvas->clear(SK_ColorWHITE);
|
|
|
|
for (int i = 0; i < kNumChars; ++i) {
|
|
|
|
canvas->save();
|
|
|
|
double rot = SkScalarInterp(fChars[i].fStartRotation, fChars[i].fEndRotation,
|
|
|
|
fCurrTime/kDuration);
|
|
|
|
canvas->translate(fChars[i].fPosition.fX + kMidX, fChars[i].fPosition.fY - kMidY);
|
|
|
|
canvas->rotate(SkRadiansToDegrees(rot));
|
|
|
|
canvas->translate(-35,+50);
|
2019-01-08 19:00:08 +00:00
|
|
|
canvas->drawString(fChars[i].fChar, 0, 0, font, paint);
|
2018-01-25 21:26:25 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
bool onAnimate(double nanos) override {
|
|
|
|
fCurrTime = 1e-9 * nanos - fResetTime;
|
2018-01-25 21:26:25 +00:00
|
|
|
if (fCurrTime > kDuration) {
|
|
|
|
this->initChars();
|
2019-07-11 20:32:53 +00:00
|
|
|
fResetTime = 1e-9 * nanos;
|
2018-01-25 21:26:25 +00:00
|
|
|
fCurrTime = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void initChars() {
|
|
|
|
for (int i = 0; i < kNumChars; ++i) {
|
|
|
|
char c = fRand.nextULessThan(26) + 65;
|
|
|
|
fChars[i].fChar[0] = c;
|
|
|
|
fChars[i].fChar[1] = '\0';
|
|
|
|
fChars[i].fPosition = SkPoint::Make(fRand.nextF()*748 + 10, fRand.nextF()*1004 + 10);
|
|
|
|
fChars[i].fStartRotation = fRand.nextF();
|
|
|
|
fChars[i].fEndRotation = fRand.nextF() * 20 - 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 22:48:26 +00:00
|
|
|
inline static constexpr double kDuration = 5.0;
|
2018-01-25 21:26:25 +00:00
|
|
|
double fCurrTime;
|
|
|
|
double fResetTime;
|
|
|
|
SkRandom fRand;
|
|
|
|
|
|
|
|
struct AnimatedChar {
|
|
|
|
char fChar[2];
|
|
|
|
SkPoint fPosition;
|
|
|
|
SkScalar fStartRotation;
|
|
|
|
SkScalar fEndRotation;
|
|
|
|
};
|
|
|
|
sk_sp<SkTypeface> fTypeface;
|
2021-10-08 22:48:26 +00:00
|
|
|
inline static constexpr int kNumChars = 40;
|
2018-01-25 21:26:25 +00:00
|
|
|
AnimatedChar fChars[kNumChars];
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Sample;
|
2018-01-25 21:26:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new FlutterAnimateView(); )
|