Florin Malita | 2687072 | 2018-09-20 14:35:30 -0400 | [diff] [blame^] | 1 | /* |
| 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 | |
| 15 | namespace { |
| 16 | |
| 17 | static constexpr char kWebFontResource[] = "fonts/Roboto-Regular.ttf"; |
| 18 | static constexpr char kSkottieResource[] = "skottie/skottie_sample_webfont.json"; |
| 19 | |
| 20 | // Dummy web font loader which serves a single local font (checked in under resources/). |
| 21 | class FakeWebFontProvider final : public skottie::ResourceProvider { |
| 22 | public: |
| 23 | FakeWebFontProvider() : fFontData(GetResourceAsData(kWebFontResource)) {} |
| 24 | |
| 25 | sk_sp<SkData> loadWebFont(const char[]) const { |
| 26 | return fFontData; |
| 27 | } |
| 28 | |
| 29 | private: |
| 30 | sk_sp<SkData> fFontData; |
| 31 | |
| 32 | using INHERITED = skottie::ResourceProvider; |
| 33 | }; |
| 34 | |
| 35 | } // namespace |
| 36 | |
| 37 | class SkottieWebFontGM : public skiagm::GM { |
| 38 | public: |
| 39 | protected: |
| 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 | |
| 75 | private: |
| 76 | static constexpr SkScalar kSize = 800; |
| 77 | |
| 78 | sk_sp<skottie::Animation> fAnimation; |
| 79 | |
| 80 | using INHERITED = skiagm::GM; |
| 81 | }; |
| 82 | |
| 83 | DEF_GM(return new SkottieWebFontGM;) |