skia2/samplecode/SampleFlutterAnimate.cpp
Brian Salomon 9fa47cc1c6 Make class members that are static constexpr also be inline.
This is in prep for compiling with -std=c++14 and -Wno-c++17-extensions
when building with clang. Chrome has encountered problems with
third_party headers that are included both in Skia and other Chrome
sources that produce different code based on whether preprocessor macros
indicate a C++14 or C++17 compilation.

In C++17 they are already inline implicitly. When compiling with C++14
we can get linker errors unless they're explicitly inlined or defined
outside the class. With -Wno-c++17-extensions we can explicitly inline
them in the C++14 build because the warning that would be generated
about using a C++17 language extension is suppressed.

We cannot do this in public headers because we support compiling with
C++14 without suppressing the C++17 language extension warnings.

Bug: chromium:1257145
Change-Id: Iaf5f4c62a398f98dd4ca9b7dfb86f2d5cab21d66
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/457498
Reviewed-by: Ben Wagner <bungeman@google.com>
Reviewed-by: Herb Derby <herb@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
2021-10-11 16:22:59 +00:00

98 lines
3.0 KiB
C++

/*
* Copyright 2018 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#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"
// 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.
class FlutterAnimateView : public Sample {
public:
FlutterAnimateView() : fCurrTime(0), fResetTime(0) {}
protected:
void onOnceBeforeDraw() override {
fTypeface = SkTypeface::MakeFromFile("/skimages/samplefont.ttf");
initChars();
}
SkString name() override { return SkString("FlutterAnimate"); }
void onDrawContent(SkCanvas* canvas) override {
SkFont font(fTypeface, 50);
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);
canvas->drawString(fChars[i].fChar, 0, 0, font, paint);
canvas->restore();
}
}
bool onAnimate(double nanos) override {
fCurrTime = 1e-9 * nanos - fResetTime;
if (fCurrTime > kDuration) {
this->initChars();
fResetTime = 1e-9 * nanos;
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;
}
}
inline static constexpr double kDuration = 5.0;
double fCurrTime;
double fResetTime;
SkRandom fRand;
struct AnimatedChar {
char fChar[2];
SkPoint fPosition;
SkScalar fStartRotation;
SkScalar fEndRotation;
};
sk_sp<SkTypeface> fTypeface;
inline static constexpr int kNumChars = 40;
AnimatedChar fChars[kNumChars];
using INHERITED = Sample;
};
//////////////////////////////////////////////////////////////////////////////
DEF_SAMPLE( return new FlutterAnimateView(); )