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 "tools/viewer/SkottieSlide.h"
9
10 #if defined(SK_ENABLE_SKOTTIE)
11
12 #include "include/core/SkCanvas.h"
13 #include "include/core/SkFont.h"
14 #include "modules/skottie/include/Skottie.h"
15 #include "modules/skottie/utils/SkottieUtils.h"
16 #include "src/utils/SkOSPath.h"
17 #include "tools/timer/TimeUtils.h"
18
19 #include <cmath>
20
draw_stats_box(SkCanvas * canvas,const skottie::Animation::Builder::Stats & stats)21 static void draw_stats_box(SkCanvas* canvas, const skottie::Animation::Builder::Stats& stats) {
22 static constexpr SkRect kR = { 10, 10, 280, 120 };
23 static constexpr SkScalar kTextSize = 20;
24
25 SkPaint paint;
26 paint.setAntiAlias(true);
27 paint.setColor(0xffeeeeee);
28
29 SkFont font(nullptr, kTextSize);
30
31 canvas->drawRect(kR, paint);
32
33 paint.setColor(SK_ColorBLACK);
34
35 const auto json_size = SkStringPrintf("Json size: %lu bytes",
36 stats.fJsonSize);
37 canvas->drawString(json_size, kR.x() + 10, kR.y() + kTextSize * 1, font, paint);
38 const auto animator_count = SkStringPrintf("Animator count: %lu",
39 stats.fAnimatorCount);
40 canvas->drawString(animator_count, kR.x() + 10, kR.y() + kTextSize * 2, font, paint);
41 const auto json_parse_time = SkStringPrintf("Json parse time: %.3f ms",
42 stats.fJsonParseTimeMS);
43 canvas->drawString(json_parse_time, kR.x() + 10, kR.y() + kTextSize * 3, font, paint);
44 const auto scene_parse_time = SkStringPrintf("Scene build time: %.3f ms",
45 stats.fSceneParseTimeMS);
46 canvas->drawString(scene_parse_time, kR.x() + 10, kR.y() + kTextSize * 4, font, paint);
47 const auto total_load_time = SkStringPrintf("Total load time: %.3f ms",
48 stats.fTotalLoadTimeMS);
49 canvas->drawString(total_load_time, kR.x() + 10, kR.y() + kTextSize * 5, font, paint);
50
51 paint.setStyle(SkPaint::kStroke_Style);
52 canvas->drawRect(kR, paint);
53 }
54
SkottieSlide(const SkString & name,const SkString & path)55 SkottieSlide::SkottieSlide(const SkString& name, const SkString& path)
56 : fPath(path) {
57 fName = name;
58 }
59
load(SkScalar w,SkScalar h)60 void SkottieSlide::load(SkScalar w, SkScalar h) {
61 class Logger final : public skottie::Logger {
62 public:
63 struct LogEntry {
64 SkString fMessage,
65 fJSON;
66 };
67
68 void log(skottie::Logger::Level lvl, const char message[], const char json[]) override {
69 auto& log = lvl == skottie::Logger::Level::kError ? fErrors : fWarnings;
70 log.push_back({ SkString(message), json ? SkString(json) : SkString() });
71 }
72
73 void report() const {
74 SkDebugf("Animation loaded with %lu error%s, %lu warning%s.\n",
75 fErrors.size(), fErrors.size() == 1 ? "" : "s",
76 fWarnings.size(), fWarnings.size() == 1 ? "" : "s");
77
78 const auto& show = [](const LogEntry& log, const char prefix[]) {
79 SkDebugf("%s%s", prefix, log.fMessage.c_str());
80 if (!log.fJSON.isEmpty())
81 SkDebugf(" : %s", log.fJSON.c_str());
82 SkDebugf("\n");
83 };
84
85 for (const auto& err : fErrors) show(err, " !! ");
86 for (const auto& wrn : fWarnings) show(wrn, " ?? ");
87 }
88
89 private:
90 std::vector<LogEntry> fErrors,
91 fWarnings;
92 };
93
94 auto logger = sk_make_sp<Logger>();
95 skottie::Animation::Builder builder;
96
97 fAnimation = builder
98 .setLogger(logger)
99 .setResourceProvider(
100 skottie_utils::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str())))
101 .makeFromFile(fPath.c_str());
102 fAnimationStats = builder.getStats();
103 fWinSize = SkSize::Make(w, h);
104 fTimeBase = 0; // force a time reset
105
106 if (fAnimation) {
107 SkDebugf("Loaded Bodymovin animation v: %s, size: [%f %f]\n",
108 fAnimation->version().c_str(),
109 fAnimation->size().width(),
110 fAnimation->size().height());
111 logger->report();
112 } else {
113 SkDebugf("failed to load Bodymovin animation: %s\n", fPath.c_str());
114 }
115 }
116
unload()117 void SkottieSlide::unload() {
118 fAnimation.reset();
119 }
120
getDimensions() const121 SkISize SkottieSlide::getDimensions() const {
122 // We always scale to fill the window.
123 return fWinSize.toCeil();
124 }
125
draw(SkCanvas * canvas)126 void SkottieSlide::draw(SkCanvas* canvas) {
127 if (fAnimation) {
128 SkAutoCanvasRestore acr(canvas, true);
129 const auto dstR = SkRect::MakeSize(fWinSize);
130 fAnimation->render(canvas, &dstR);
131
132 if (fShowAnimationStats) {
133 draw_stats_box(canvas, fAnimationStats);
134 }
135 if (fShowAnimationInval) {
136 const auto t = SkMatrix::MakeRectToRect(SkRect::MakeSize(fAnimation->size()),
137 dstR,
138 SkMatrix::kCenter_ScaleToFit);
139 SkPaint fill, stroke;
140 fill.setAntiAlias(true);
141 fill.setColor(0x40ff0000);
142 stroke.setAntiAlias(true);
143 stroke.setColor(0xffff0000);
144 stroke.setStyle(SkPaint::kStroke_Style);
145
146 for (const auto& r : fInvalController) {
147 SkRect bounds;
148 t.mapRect(&bounds, r);
149 canvas->drawRect(bounds, fill);
150 canvas->drawRect(bounds, stroke);
151 }
152 }
153 }
154 }
155
animate(double nanos)156 bool SkottieSlide::animate(double nanos) {
157 SkMSec msec = TimeUtils::NanosToMSec(nanos);
158 if (fTimeBase == 0) {
159 // Reset the animation time.
160 fTimeBase = msec;
161 }
162
163 if (fAnimation) {
164 fInvalController.reset();
165 const auto t = msec - fTimeBase;
166 const auto d = fAnimation->duration() * 1000;
167 fAnimation->seek(std::fmod(t, d) / d, &fInvalController);
168 }
169 return true;
170 }
171
onChar(SkUnichar c)172 bool SkottieSlide::onChar(SkUnichar c) {
173 switch (c) {
174 case 'I':
175 fShowAnimationStats = !fShowAnimationStats;
176 break;
177 default:
178 break;
179 }
180
181 return INHERITED::onChar(c);
182 }
183
onMouse(SkScalar x,SkScalar y,InputState state,ModifierKey)184 bool SkottieSlide::onMouse(SkScalar x, SkScalar y, InputState state, ModifierKey) {
185 switch (state) {
186 case InputState::kUp:
187 fShowAnimationInval = !fShowAnimationInval;
188 fShowAnimationStats = !fShowAnimationStats;
189 break;
190 default:
191 break;
192 }
193
194 return false;
195 }
196
197 #endif // SK_ENABLE_SKOTTIE
198