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" |
Florin Malita | 62c6bd9 | 2018-10-03 14:39:56 -0400 | [diff] [blame] | 10 | #include "SkAnimCodecPlayer.h" |
Florin Malita | 2687072 | 2018-09-20 14:35:30 -0400 | [diff] [blame] | 11 | #include "SkAnimTimer.h" |
Florin Malita | 17282da | 2018-09-25 13:10:12 -0400 | [diff] [blame] | 12 | #include "SkColor.h" |
Florin Malita | 62c6bd9 | 2018-10-03 14:39:56 -0400 | [diff] [blame] | 13 | #include "SkMakeUnique.h" |
Florin Malita | 2687072 | 2018-09-20 14:35:30 -0400 | [diff] [blame] | 14 | #include "Skottie.h" |
Florin Malita | 17282da | 2018-09-25 13:10:12 -0400 | [diff] [blame] | 15 | #include "SkottieProperty.h" |
Florin Malita | a831655 | 2018-11-09 16:19:44 -0500 | [diff] [blame] | 16 | #include "SkottieUtils.h" |
Florin Malita | 2687072 | 2018-09-20 14:35:30 -0400 | [diff] [blame] | 17 | |
| 18 | #include <cmath> |
Florin Malita | 17282da | 2018-09-25 13:10:12 -0400 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
| 21 | using namespace skottie; |
Florin Malita | 2687072 | 2018-09-20 14:35:30 -0400 | [diff] [blame] | 22 | |
| 23 | namespace { |
| 24 | |
| 25 | static constexpr char kWebFontResource[] = "fonts/Roboto-Regular.ttf"; |
| 26 | static constexpr char kSkottieResource[] = "skottie/skottie_sample_webfont.json"; |
| 27 | |
| 28 | // Dummy web font loader which serves a single local font (checked in under resources/). |
Florin Malita | 17282da | 2018-09-25 13:10:12 -0400 | [diff] [blame] | 29 | class FakeWebFontProvider final : public ResourceProvider { |
Florin Malita | 2687072 | 2018-09-20 14:35:30 -0400 | [diff] [blame] | 30 | public: |
| 31 | FakeWebFontProvider() : fFontData(GetResourceAsData(kWebFontResource)) {} |
| 32 | |
Florin Malita | 97b150e | 2018-09-27 10:59:52 -0400 | [diff] [blame] | 33 | sk_sp<SkData> loadFont(const char[], const char[]) const override { |
Florin Malita | 2687072 | 2018-09-20 14:35:30 -0400 | [diff] [blame] | 34 | return fFontData; |
| 35 | } |
| 36 | |
| 37 | private: |
| 38 | sk_sp<SkData> fFontData; |
| 39 | |
Florin Malita | 17282da | 2018-09-25 13:10:12 -0400 | [diff] [blame] | 40 | using INHERITED = ResourceProvider; |
Florin Malita | 2687072 | 2018-09-20 14:35:30 -0400 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | } // namespace |
| 44 | |
| 45 | class SkottieWebFontGM : public skiagm::GM { |
| 46 | public: |
| 47 | protected: |
| 48 | SkString onShortName() override { |
| 49 | return SkString("skottie_webfont"); |
| 50 | } |
| 51 | |
| 52 | SkISize onISize() override { |
| 53 | return SkISize::Make(kSize, kSize); |
| 54 | } |
| 55 | |
| 56 | void onOnceBeforeDraw() override { |
| 57 | if (auto stream = GetResourceAsStream(kSkottieResource)) { |
Florin Malita | 17282da | 2018-09-25 13:10:12 -0400 | [diff] [blame] | 58 | fAnimation = Animation::Builder() |
Florin Malita | 2687072 | 2018-09-20 14:35:30 -0400 | [diff] [blame] | 59 | .setResourceProvider(sk_make_sp<FakeWebFontProvider>()) |
| 60 | .make(stream.get()); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void onDraw(SkCanvas* canvas) override { |
| 65 | if (!fAnimation) { |
Chris Dalton | d7a5a1d | 2019-02-04 01:20:52 -0700 | [diff] [blame^] | 66 | DrawFailureMessage(canvas, "No animation"); |
Florin Malita | 2687072 | 2018-09-20 14:35:30 -0400 | [diff] [blame] | 67 | return; |
| 68 | } |
| 69 | |
| 70 | auto dest = SkRect::MakeWH(kSize, kSize); |
| 71 | fAnimation->render(canvas, &dest); |
| 72 | } |
| 73 | |
| 74 | bool onAnimate(const SkAnimTimer& timer) override { |
| 75 | if (!fAnimation) { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | const auto duration = fAnimation->duration(); |
| 80 | fAnimation->seek(std::fmod(timer.secs(), duration) / duration); |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | static constexpr SkScalar kSize = 800; |
| 86 | |
Florin Malita | 17282da | 2018-09-25 13:10:12 -0400 | [diff] [blame] | 87 | sk_sp<Animation> fAnimation; |
Florin Malita | 2687072 | 2018-09-20 14:35:30 -0400 | [diff] [blame] | 88 | |
| 89 | using INHERITED = skiagm::GM; |
| 90 | }; |
| 91 | |
| 92 | DEF_GM(return new SkottieWebFontGM;) |
Florin Malita | 17282da | 2018-09-25 13:10:12 -0400 | [diff] [blame] | 93 | |
Florin Malita | 8ac81b7 | 2018-11-28 11:39:39 -0500 | [diff] [blame] | 94 | using namespace skottie_utils; |
| 95 | |
Florin Malita | 17282da | 2018-09-25 13:10:12 -0400 | [diff] [blame] | 96 | class SkottieColorizeGM : public skiagm::GM { |
Florin Malita | 17282da | 2018-09-25 13:10:12 -0400 | [diff] [blame] | 97 | protected: |
| 98 | SkString onShortName() override { |
| 99 | return SkString("skottie_colorize"); |
| 100 | } |
| 101 | |
| 102 | SkISize onISize() override { |
| 103 | return SkISize::Make(kSize, kSize); |
| 104 | } |
| 105 | |
| 106 | void onOnceBeforeDraw() override { |
| 107 | if (auto stream = GetResourceAsStream("skottie/skottie_sample_search.json")) { |
Florin Malita | 91af8d8 | 2018-11-30 16:46:45 -0500 | [diff] [blame] | 108 | fPropManager = skstd::make_unique<CustomPropertyManager>(); |
Florin Malita | 8ac81b7 | 2018-11-28 11:39:39 -0500 | [diff] [blame] | 109 | fAnimation = Animation::Builder() |
Florin Malita | 91af8d8 | 2018-11-30 16:46:45 -0500 | [diff] [blame] | 110 | .setPropertyObserver(fPropManager->getPropertyObserver()) |
Florin Malita | 8ac81b7 | 2018-11-28 11:39:39 -0500 | [diff] [blame] | 111 | .make(stream.get()); |
Florin Malita | 8ac81b7 | 2018-11-28 11:39:39 -0500 | [diff] [blame] | 112 | fColors = fPropManager->getColorProps(); |
Florin Malita | 17282da | 2018-09-25 13:10:12 -0400 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | void onDraw(SkCanvas* canvas) override { |
| 117 | if (!fAnimation) { |
Chris Dalton | d7a5a1d | 2019-02-04 01:20:52 -0700 | [diff] [blame^] | 118 | DrawFailureMessage(canvas, "No animation"); |
Florin Malita | 17282da | 2018-09-25 13:10:12 -0400 | [diff] [blame] | 119 | return; |
| 120 | } |
| 121 | |
| 122 | auto dest = SkRect::MakeWH(kSize, kSize); |
| 123 | fAnimation->render(canvas, &dest); |
| 124 | } |
| 125 | |
| 126 | bool onAnimate(const SkAnimTimer& timer) override { |
| 127 | if (!fAnimation) { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | const auto duration = fAnimation->duration(); |
| 132 | fAnimation->seek(std::fmod(timer.secs(), duration) / duration); |
| 133 | return true; |
| 134 | } |
| 135 | |
Florin Malita | 17282da | 2018-09-25 13:10:12 -0400 | [diff] [blame] | 136 | bool onHandleKey(SkUnichar uni) override { |
| 137 | static constexpr SkColor kColors[] = { |
| 138 | SK_ColorBLACK, |
| 139 | SK_ColorRED, |
| 140 | SK_ColorGREEN, |
| 141 | SK_ColorYELLOW, |
| 142 | SK_ColorCYAN, |
| 143 | }; |
| 144 | |
| 145 | if (uni == 'c') { |
| 146 | fColorIndex = (fColorIndex + 1) % SK_ARRAY_COUNT(kColors); |
Florin Malita | 8ac81b7 | 2018-11-28 11:39:39 -0500 | [diff] [blame] | 147 | for (const auto& prop : fColors) { |
| 148 | fPropManager->setColor(prop, kColors[fColorIndex]); |
| 149 | } |
Florin Malita | 17282da | 2018-09-25 13:10:12 -0400 | [diff] [blame] | 150 | return true; |
| 151 | } |
| 152 | |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | private: |
Florin Malita | 17282da | 2018-09-25 13:10:12 -0400 | [diff] [blame] | 157 | static constexpr SkScalar kSize = 800; |
| 158 | |
Florin Malita | 8ac81b7 | 2018-11-28 11:39:39 -0500 | [diff] [blame] | 159 | sk_sp<Animation> fAnimation; |
| 160 | std::unique_ptr<CustomPropertyManager> fPropManager; |
| 161 | std::vector<CustomPropertyManager::PropKey> fColors; |
| 162 | size_t fColorIndex = 0; |
Florin Malita | 17282da | 2018-09-25 13:10:12 -0400 | [diff] [blame] | 163 | |
| 164 | using INHERITED = skiagm::GM; |
| 165 | }; |
| 166 | |
| 167 | DEF_GM(return new SkottieColorizeGM;) |
Florin Malita | 62c6bd9 | 2018-10-03 14:39:56 -0400 | [diff] [blame] | 168 | |
| 169 | class SkottieMultiFrameGM : public skiagm::GM { |
| 170 | public: |
| 171 | protected: |
| 172 | SkString onShortName() override { |
| 173 | return SkString("skottie_multiframe"); |
| 174 | } |
| 175 | |
| 176 | SkISize onISize() override { |
| 177 | return SkISize::Make(kSize, kSize); |
| 178 | } |
| 179 | |
| 180 | void onOnceBeforeDraw() override { |
| 181 | if (auto stream = GetResourceAsStream("skottie/skottie_sample_multiframe.json")) { |
| 182 | fAnimation = Animation::Builder() |
| 183 | .setResourceProvider(sk_make_sp<MultiFrameResourceProvider>()) |
| 184 | .make(stream.get()); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void onDraw(SkCanvas* canvas) override { |
| 189 | if (!fAnimation) { |
Chris Dalton | d7a5a1d | 2019-02-04 01:20:52 -0700 | [diff] [blame^] | 190 | DrawFailureMessage(canvas, "No animation"); |
Florin Malita | 62c6bd9 | 2018-10-03 14:39:56 -0400 | [diff] [blame] | 191 | return; |
| 192 | } |
| 193 | |
| 194 | auto dest = SkRect::MakeWH(kSize, kSize); |
| 195 | fAnimation->render(canvas, &dest); |
| 196 | } |
| 197 | |
| 198 | bool onAnimate(const SkAnimTimer& timer) override { |
| 199 | if (!fAnimation) { |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | const auto duration = fAnimation->duration(); |
| 204 | fAnimation->seek(std::fmod(timer.secs(), duration) / duration); |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | private: |
Florin Malita | 62c6bd9 | 2018-10-03 14:39:56 -0400 | [diff] [blame] | 209 | class MultiFrameResourceProvider final : public skottie::ResourceProvider { |
| 210 | public: |
| 211 | sk_sp<ImageAsset> loadImageAsset(const char[], const char[]) const override { |
Florin Malita | a831655 | 2018-11-09 16:19:44 -0500 | [diff] [blame] | 212 | return skottie_utils::MultiFrameImageAsset::Make( |
| 213 | GetResourceAsData("images/flightAnim.gif")); |
Florin Malita | 62c6bd9 | 2018-10-03 14:39:56 -0400 | [diff] [blame] | 214 | } |
| 215 | }; |
| 216 | |
| 217 | static constexpr SkScalar kSize = 800; |
| 218 | |
| 219 | sk_sp<Animation> fAnimation; |
| 220 | |
| 221 | using INHERITED = skiagm::GM; |
| 222 | }; |
| 223 | |
| 224 | DEF_GM(return new SkottieMultiFrameGM;) |