blob: f326a12ed8b11bc32dd3e05178d7131e22c8b9e8 [file] [log] [blame]
Florin Malita26870722018-09-20 14:35:30 -04001/*
2 * Copyright 2017 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
8#include "gm.h"
9#include "Resources.h"
10#include "SkAnimTimer.h"
11#include "Skottie.h"
12
13#include <cmath>
14
15namespace {
16
17static constexpr char kWebFontResource[] = "fonts/Roboto-Regular.ttf";
18static constexpr char kSkottieResource[] = "skottie/skottie_sample_webfont.json";
19
20// Dummy web font loader which serves a single local font (checked in under resources/).
21class FakeWebFontProvider final : public skottie::ResourceProvider {
22public:
23 FakeWebFontProvider() : fFontData(GetResourceAsData(kWebFontResource)) {}
24
25 sk_sp<SkData> loadWebFont(const char[]) const {
26 return fFontData;
27 }
28
29private:
30 sk_sp<SkData> fFontData;
31
32 using INHERITED = skottie::ResourceProvider;
33};
34
35} // namespace
36
37class SkottieWebFontGM : public skiagm::GM {
38public:
39protected:
40 SkString onShortName() override {
41 return SkString("skottie_webfont");
42 }
43
44 SkISize onISize() override {
45 return SkISize::Make(kSize, kSize);
46 }
47
48 void onOnceBeforeDraw() override {
49 if (auto stream = GetResourceAsStream(kSkottieResource)) {
50 fAnimation = skottie::Animation::Builder()
51 .setResourceProvider(sk_make_sp<FakeWebFontProvider>())
52 .make(stream.get());
53 }
54 }
55
56 void onDraw(SkCanvas* canvas) override {
57 if (!fAnimation) {
58 return;
59 }
60
61 auto dest = SkRect::MakeWH(kSize, kSize);
62 fAnimation->render(canvas, &dest);
63 }
64
65 bool onAnimate(const SkAnimTimer& timer) override {
66 if (!fAnimation) {
67 return false;
68 }
69
70 const auto duration = fAnimation->duration();
71 fAnimation->seek(std::fmod(timer.secs(), duration) / duration);
72 return true;
73 }
74
75private:
76 static constexpr SkScalar kSize = 800;
77
78 sk_sp<skottie::Animation> fAnimation;
79
80 using INHERITED = skiagm::GM;
81};
82
83DEF_GM(return new SkottieWebFontGM;)