2018-09-20 18:35:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-03-20 16:55:08 +00:00
|
|
|
#include "AnimTimer.h"
|
2018-09-20 18:35:30 +00:00
|
|
|
#include "Resources.h"
|
2018-10-03 18:39:56 +00:00
|
|
|
#include "SkAnimCodecPlayer.h"
|
2018-09-25 17:10:12 +00:00
|
|
|
#include "SkColor.h"
|
2018-10-03 18:39:56 +00:00
|
|
|
#include "SkMakeUnique.h"
|
2018-09-20 18:35:30 +00:00
|
|
|
#include "Skottie.h"
|
2018-09-25 17:10:12 +00:00
|
|
|
#include "SkottieProperty.h"
|
2018-11-09 21:19:44 +00:00
|
|
|
#include "SkottieUtils.h"
|
2019-03-20 16:55:08 +00:00
|
|
|
#include "gm.h"
|
2018-09-20 18:35:30 +00:00
|
|
|
|
|
|
|
#include <cmath>
|
2018-09-25 17:10:12 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace skottie;
|
2018-09-20 18:35:30 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
static constexpr char kWebFontResource[] = "fonts/Roboto-Regular.ttf";
|
|
|
|
static constexpr char kSkottieResource[] = "skottie/skottie_sample_webfont.json";
|
|
|
|
|
|
|
|
// Dummy web font loader which serves a single local font (checked in under resources/).
|
2018-09-25 17:10:12 +00:00
|
|
|
class FakeWebFontProvider final : public ResourceProvider {
|
2018-09-20 18:35:30 +00:00
|
|
|
public:
|
|
|
|
FakeWebFontProvider() : fFontData(GetResourceAsData(kWebFontResource)) {}
|
|
|
|
|
2018-09-27 14:59:52 +00:00
|
|
|
sk_sp<SkData> loadFont(const char[], const char[]) const override {
|
2018-09-20 18:35:30 +00:00
|
|
|
return fFontData;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
sk_sp<SkData> fFontData;
|
|
|
|
|
2018-09-25 17:10:12 +00:00
|
|
|
using INHERITED = ResourceProvider;
|
2018-09-20 18:35:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
class SkottieWebFontGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("skottie_webfont");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(kSize, kSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
if (auto stream = GetResourceAsStream(kSkottieResource)) {
|
2018-09-25 17:10:12 +00:00
|
|
|
fAnimation = Animation::Builder()
|
2018-09-20 18:35:30 +00:00
|
|
|
.setResourceProvider(sk_make_sp<FakeWebFontProvider>())
|
|
|
|
.make(stream.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
|
2018-09-20 18:35:30 +00:00
|
|
|
if (!fAnimation) {
|
2019-02-07 23:20:09 +00:00
|
|
|
*errorMsg = "No animation";
|
|
|
|
return DrawResult::kFail;
|
2018-09-20 18:35:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto dest = SkRect::MakeWH(kSize, kSize);
|
|
|
|
fAnimation->render(canvas, &dest);
|
2019-02-07 23:20:09 +00:00
|
|
|
return DrawResult::kOk;
|
2018-09-20 18:35:30 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 16:55:08 +00:00
|
|
|
bool onAnimate(const AnimTimer& timer) override {
|
2018-09-20 18:35:30 +00:00
|
|
|
if (!fAnimation) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto duration = fAnimation->duration();
|
|
|
|
fAnimation->seek(std::fmod(timer.secs(), duration) / duration);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr SkScalar kSize = 800;
|
|
|
|
|
2018-09-25 17:10:12 +00:00
|
|
|
sk_sp<Animation> fAnimation;
|
2018-09-20 18:35:30 +00:00
|
|
|
|
|
|
|
using INHERITED = skiagm::GM;
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM(return new SkottieWebFontGM;)
|
2018-09-25 17:10:12 +00:00
|
|
|
|
2018-11-28 16:39:39 +00:00
|
|
|
using namespace skottie_utils;
|
|
|
|
|
2018-09-25 17:10:12 +00:00
|
|
|
class SkottieColorizeGM : public skiagm::GM {
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("skottie_colorize");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(kSize, kSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
if (auto stream = GetResourceAsStream("skottie/skottie_sample_search.json")) {
|
2018-11-30 21:46:45 +00:00
|
|
|
fPropManager = skstd::make_unique<CustomPropertyManager>();
|
2018-11-28 16:39:39 +00:00
|
|
|
fAnimation = Animation::Builder()
|
2018-11-30 21:46:45 +00:00
|
|
|
.setPropertyObserver(fPropManager->getPropertyObserver())
|
2018-11-28 16:39:39 +00:00
|
|
|
.make(stream.get());
|
|
|
|
fColors = fPropManager->getColorProps();
|
2018-09-25 17:10:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
|
2018-09-25 17:10:12 +00:00
|
|
|
if (!fAnimation) {
|
2019-02-07 23:20:09 +00:00
|
|
|
*errorMsg = "No animation";
|
|
|
|
return DrawResult::kFail;
|
2018-09-25 17:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto dest = SkRect::MakeWH(kSize, kSize);
|
|
|
|
fAnimation->render(canvas, &dest);
|
2019-02-07 23:20:09 +00:00
|
|
|
return DrawResult::kOk;
|
2018-09-25 17:10:12 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 16:55:08 +00:00
|
|
|
bool onAnimate(const AnimTimer& timer) override {
|
2018-09-25 17:10:12 +00:00
|
|
|
if (!fAnimation) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto duration = fAnimation->duration();
|
|
|
|
fAnimation->seek(std::fmod(timer.secs(), duration) / duration);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onHandleKey(SkUnichar uni) override {
|
|
|
|
static constexpr SkColor kColors[] = {
|
|
|
|
SK_ColorBLACK,
|
|
|
|
SK_ColorRED,
|
|
|
|
SK_ColorGREEN,
|
|
|
|
SK_ColorYELLOW,
|
|
|
|
SK_ColorCYAN,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (uni == 'c') {
|
|
|
|
fColorIndex = (fColorIndex + 1) % SK_ARRAY_COUNT(kColors);
|
2018-11-28 16:39:39 +00:00
|
|
|
for (const auto& prop : fColors) {
|
|
|
|
fPropManager->setColor(prop, kColors[fColorIndex]);
|
|
|
|
}
|
2018-09-25 17:10:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr SkScalar kSize = 800;
|
|
|
|
|
2018-11-28 16:39:39 +00:00
|
|
|
sk_sp<Animation> fAnimation;
|
|
|
|
std::unique_ptr<CustomPropertyManager> fPropManager;
|
|
|
|
std::vector<CustomPropertyManager::PropKey> fColors;
|
|
|
|
size_t fColorIndex = 0;
|
2018-09-25 17:10:12 +00:00
|
|
|
|
|
|
|
using INHERITED = skiagm::GM;
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM(return new SkottieColorizeGM;)
|
2018-10-03 18:39:56 +00:00
|
|
|
|
|
|
|
class SkottieMultiFrameGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("skottie_multiframe");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(kSize, kSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
if (auto stream = GetResourceAsStream("skottie/skottie_sample_multiframe.json")) {
|
|
|
|
fAnimation = Animation::Builder()
|
|
|
|
.setResourceProvider(sk_make_sp<MultiFrameResourceProvider>())
|
|
|
|
.make(stream.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
|
2018-10-03 18:39:56 +00:00
|
|
|
if (!fAnimation) {
|
2019-02-07 23:20:09 +00:00
|
|
|
*errorMsg = "No animation";
|
|
|
|
return DrawResult::kFail;
|
2018-10-03 18:39:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto dest = SkRect::MakeWH(kSize, kSize);
|
|
|
|
fAnimation->render(canvas, &dest);
|
2019-02-07 23:20:09 +00:00
|
|
|
return DrawResult::kOk;
|
2018-10-03 18:39:56 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 16:55:08 +00:00
|
|
|
bool onAnimate(const AnimTimer& timer) override {
|
2018-10-03 18:39:56 +00:00
|
|
|
if (!fAnimation) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto duration = fAnimation->duration();
|
|
|
|
fAnimation->seek(std::fmod(timer.secs(), duration) / duration);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
class MultiFrameResourceProvider final : public skottie::ResourceProvider {
|
|
|
|
public:
|
|
|
|
sk_sp<ImageAsset> loadImageAsset(const char[], const char[]) const override {
|
2018-11-09 21:19:44 +00:00
|
|
|
return skottie_utils::MultiFrameImageAsset::Make(
|
|
|
|
GetResourceAsData("images/flightAnim.gif"));
|
2018-10-03 18:39:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr SkScalar kSize = 800;
|
|
|
|
|
|
|
|
sk_sp<Animation> fAnimation;
|
|
|
|
|
|
|
|
using INHERITED = skiagm::GM;
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM(return new SkottieMultiFrameGM;)
|