blob: 09908b8548692ebee1ea9bf110d42a082ae9026a [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"
Florin Malita62c6bd92018-10-03 14:39:56 -040010#include "SkAnimCodecPlayer.h"
Florin Malita26870722018-09-20 14:35:30 -040011#include "SkAnimTimer.h"
Florin Malita17282da2018-09-25 13:10:12 -040012#include "SkColor.h"
Florin Malita62c6bd92018-10-03 14:39:56 -040013#include "SkMakeUnique.h"
Florin Malita26870722018-09-20 14:35:30 -040014#include "Skottie.h"
Florin Malita17282da2018-09-25 13:10:12 -040015#include "SkottieProperty.h"
Florin Malitaa8316552018-11-09 16:19:44 -050016#include "SkottieUtils.h"
Florin Malita26870722018-09-20 14:35:30 -040017
18#include <cmath>
Florin Malita17282da2018-09-25 13:10:12 -040019#include <vector>
20
21using namespace skottie;
Florin Malita26870722018-09-20 14:35:30 -040022
23namespace {
24
25static constexpr char kWebFontResource[] = "fonts/Roboto-Regular.ttf";
26static 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 Malita17282da2018-09-25 13:10:12 -040029class FakeWebFontProvider final : public ResourceProvider {
Florin Malita26870722018-09-20 14:35:30 -040030public:
31 FakeWebFontProvider() : fFontData(GetResourceAsData(kWebFontResource)) {}
32
Florin Malita97b150e2018-09-27 10:59:52 -040033 sk_sp<SkData> loadFont(const char[], const char[]) const override {
Florin Malita26870722018-09-20 14:35:30 -040034 return fFontData;
35 }
36
37private:
38 sk_sp<SkData> fFontData;
39
Florin Malita17282da2018-09-25 13:10:12 -040040 using INHERITED = ResourceProvider;
Florin Malita26870722018-09-20 14:35:30 -040041};
42
43} // namespace
44
45class SkottieWebFontGM : public skiagm::GM {
46public:
47protected:
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 Malita17282da2018-09-25 13:10:12 -040058 fAnimation = Animation::Builder()
Florin Malita26870722018-09-20 14:35:30 -040059 .setResourceProvider(sk_make_sp<FakeWebFontProvider>())
60 .make(stream.get());
61 }
62 }
63
64 void onDraw(SkCanvas* canvas) override {
65 if (!fAnimation) {
Chris Daltond7a5a1d2019-02-04 01:20:52 -070066 DrawFailureMessage(canvas, "No animation");
Florin Malita26870722018-09-20 14:35:30 -040067 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
84private:
85 static constexpr SkScalar kSize = 800;
86
Florin Malita17282da2018-09-25 13:10:12 -040087 sk_sp<Animation> fAnimation;
Florin Malita26870722018-09-20 14:35:30 -040088
89 using INHERITED = skiagm::GM;
90};
91
92DEF_GM(return new SkottieWebFontGM;)
Florin Malita17282da2018-09-25 13:10:12 -040093
Florin Malita8ac81b72018-11-28 11:39:39 -050094using namespace skottie_utils;
95
Florin Malita17282da2018-09-25 13:10:12 -040096class SkottieColorizeGM : public skiagm::GM {
Florin Malita17282da2018-09-25 13:10:12 -040097protected:
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 Malita91af8d82018-11-30 16:46:45 -0500108 fPropManager = skstd::make_unique<CustomPropertyManager>();
Florin Malita8ac81b72018-11-28 11:39:39 -0500109 fAnimation = Animation::Builder()
Florin Malita91af8d82018-11-30 16:46:45 -0500110 .setPropertyObserver(fPropManager->getPropertyObserver())
Florin Malita8ac81b72018-11-28 11:39:39 -0500111 .make(stream.get());
Florin Malita8ac81b72018-11-28 11:39:39 -0500112 fColors = fPropManager->getColorProps();
Florin Malita17282da2018-09-25 13:10:12 -0400113 }
114 }
115
116 void onDraw(SkCanvas* canvas) override {
117 if (!fAnimation) {
Chris Daltond7a5a1d2019-02-04 01:20:52 -0700118 DrawFailureMessage(canvas, "No animation");
Florin Malita17282da2018-09-25 13:10:12 -0400119 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 Malita17282da2018-09-25 13:10:12 -0400136 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 Malita8ac81b72018-11-28 11:39:39 -0500147 for (const auto& prop : fColors) {
148 fPropManager->setColor(prop, kColors[fColorIndex]);
149 }
Florin Malita17282da2018-09-25 13:10:12 -0400150 return true;
151 }
152
153 return false;
154 }
155
156private:
Florin Malita17282da2018-09-25 13:10:12 -0400157 static constexpr SkScalar kSize = 800;
158
Florin Malita8ac81b72018-11-28 11:39:39 -0500159 sk_sp<Animation> fAnimation;
160 std::unique_ptr<CustomPropertyManager> fPropManager;
161 std::vector<CustomPropertyManager::PropKey> fColors;
162 size_t fColorIndex = 0;
Florin Malita17282da2018-09-25 13:10:12 -0400163
164 using INHERITED = skiagm::GM;
165};
166
167DEF_GM(return new SkottieColorizeGM;)
Florin Malita62c6bd92018-10-03 14:39:56 -0400168
169class SkottieMultiFrameGM : public skiagm::GM {
170public:
171protected:
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 Daltond7a5a1d2019-02-04 01:20:52 -0700190 DrawFailureMessage(canvas, "No animation");
Florin Malita62c6bd92018-10-03 14:39:56 -0400191 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
208private:
Florin Malita62c6bd92018-10-03 14:39:56 -0400209 class MultiFrameResourceProvider final : public skottie::ResourceProvider {
210 public:
211 sk_sp<ImageAsset> loadImageAsset(const char[], const char[]) const override {
Florin Malitaa8316552018-11-09 16:19:44 -0500212 return skottie_utils::MultiFrameImageAsset::Make(
213 GetResourceAsData("images/flightAnim.gif"));
Florin Malita62c6bd92018-10-03 14:39:56 -0400214 }
215 };
216
217 static constexpr SkScalar kSize = 800;
218
219 sk_sp<Animation> fAnimation;
220
221 using INHERITED = skiagm::GM;
222};
223
224DEF_GM(return new SkottieMultiFrameGM;)