blob: 1efe712e47bacf9e794673629b18741fda75410a [file] [log] [blame]
Florin Malita094ccde2017-12-30 12:27:00 -05001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tools/viewer/SkottieSlide.h"
Florin Malita094ccde2017-12-30 12:27:00 -05009
Florin Malita3d856bd2018-05-26 09:49:28 -040010#if defined(SK_ENABLE_SKOTTIE)
11
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#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/AnimTimer.h"
Florin Malita094ccde2017-12-30 12:27:00 -050018
Florin Malitaa33447d2018-05-29 13:46:54 -040019#include <cmath>
20
Florin Malita40c37422018-08-22 20:37:04 -040021static void draw_stats_box(SkCanvas* canvas, const skottie::Animation::Builder::Stats& stats) {
Florin Malita6eb85a12018-04-30 10:32:18 -040022 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);
Hal Canarydf2d27e2019-01-08 09:38:02 -050028
29 SkFont font(nullptr, kTextSize);
Florin Malita6eb85a12018-04-30 10:32:18 -040030
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);
Hal Canarydf2d27e2019-01-08 09:38:02 -050037 canvas->drawString(json_size, kR.x() + 10, kR.y() + kTextSize * 1, font, paint);
Florin Malita6eb85a12018-04-30 10:32:18 -040038 const auto animator_count = SkStringPrintf("Animator count: %lu",
39 stats.fAnimatorCount);
Hal Canarydf2d27e2019-01-08 09:38:02 -050040 canvas->drawString(animator_count, kR.x() + 10, kR.y() + kTextSize * 2, font, paint);
Florin Malita6eb85a12018-04-30 10:32:18 -040041 const auto json_parse_time = SkStringPrintf("Json parse time: %.3f ms",
42 stats.fJsonParseTimeMS);
Hal Canarydf2d27e2019-01-08 09:38:02 -050043 canvas->drawString(json_parse_time, kR.x() + 10, kR.y() + kTextSize * 3, font, paint);
Florin Malita6eb85a12018-04-30 10:32:18 -040044 const auto scene_parse_time = SkStringPrintf("Scene build time: %.3f ms",
45 stats.fSceneParseTimeMS);
Hal Canarydf2d27e2019-01-08 09:38:02 -050046 canvas->drawString(scene_parse_time, kR.x() + 10, kR.y() + kTextSize * 4, font, paint);
Florin Malita6eb85a12018-04-30 10:32:18 -040047 const auto total_load_time = SkStringPrintf("Total load time: %.3f ms",
48 stats.fTotalLoadTimeMS);
Hal Canarydf2d27e2019-01-08 09:38:02 -050049 canvas->drawString(total_load_time, kR.x() + 10, kR.y() + kTextSize * 5, font, paint);
Florin Malita6eb85a12018-04-30 10:32:18 -040050
51 paint.setStyle(SkPaint::kStroke_Style);
52 canvas->drawRect(kR, paint);
53}
54
Florin Malita54f65c42018-01-16 17:04:30 -050055SkottieSlide::SkottieSlide(const SkString& name, const SkString& path)
Florin Malita094ccde2017-12-30 12:27:00 -050056 : fPath(path) {
57 fName = name;
58}
59
Florin Malitac378fdc2018-02-09 11:15:32 -050060void SkottieSlide::load(SkScalar w, SkScalar h) {
Florin Malita57b9d402018-10-02 12:48:00 -040061 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>();
Florin Malita40c37422018-08-22 20:37:04 -040095 skottie::Animation::Builder builder;
Florin Malita57b9d402018-10-02 12:48:00 -040096
Florin Malitaa8316552018-11-09 16:19:44 -050097 fAnimation = builder
98 .setLogger(logger)
99 .setResourceProvider(
100 skottie_utils::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str())))
101 .makeFromFile(fPath.c_str());
Florin Malita40c37422018-08-22 20:37:04 -0400102 fAnimationStats = builder.getStats();
103 fWinSize = SkSize::Make(w, h);
104 fTimeBase = 0; // force a time reset
Florin Malita094ccde2017-12-30 12:27:00 -0500105
106 if (fAnimation) {
Florin Malitadf2713c2018-01-09 15:51:21 -0500107 fAnimation->setShowInval(fShowAnimationInval);
Florin Malita57b9d402018-10-02 12:48:00 -0400108 SkDebugf("Loaded Bodymovin animation v: %s, size: [%f %f]\n",
Florin Malita094ccde2017-12-30 12:27:00 -0500109 fAnimation->version().c_str(),
110 fAnimation->size().width(),
Florin Malita911ae402018-05-31 16:45:29 -0400111 fAnimation->size().height());
Florin Malita57b9d402018-10-02 12:48:00 -0400112 logger->report();
Florin Malita094ccde2017-12-30 12:27:00 -0500113 } else {
114 SkDebugf("failed to load Bodymovin animation: %s\n", fPath.c_str());
115 }
116}
117
Florin Malita54f65c42018-01-16 17:04:30 -0500118void SkottieSlide::unload() {
Florin Malita094ccde2017-12-30 12:27:00 -0500119 fAnimation.reset();
120}
121
Florin Malita54f65c42018-01-16 17:04:30 -0500122SkISize SkottieSlide::getDimensions() const {
Florin Malitac378fdc2018-02-09 11:15:32 -0500123 // We always scale to fill the window.
124 return fWinSize.toCeil();
Florin Malita094ccde2017-12-30 12:27:00 -0500125}
126
Florin Malita54f65c42018-01-16 17:04:30 -0500127void SkottieSlide::draw(SkCanvas* canvas) {
Florin Malita094ccde2017-12-30 12:27:00 -0500128 if (fAnimation) {
Florin Malitaaa4dc622018-01-02 14:37:37 -0500129 SkAutoCanvasRestore acr(canvas, true);
Florin Malitac378fdc2018-02-09 11:15:32 -0500130 const auto dstR = SkRect::MakeSize(fWinSize);
Mike Reed29859872018-01-08 08:25:27 -0500131 fAnimation->render(canvas, &dstR);
Florin Malita6eb85a12018-04-30 10:32:18 -0400132
133 if (fShowAnimationStats) {
134 draw_stats_box(canvas, fAnimationStats);
135 }
Florin Malita094ccde2017-12-30 12:27:00 -0500136 }
137}
138
Mike Kleincd5104e2019-03-20 11:55:08 -0500139bool SkottieSlide::animate(const AnimTimer& timer) {
Florin Malita094ccde2017-12-30 12:27:00 -0500140 if (fTimeBase == 0) {
141 // Reset the animation time.
142 fTimeBase = timer.msec();
143 }
144
145 if (fAnimation) {
Florin Malitaa33447d2018-05-29 13:46:54 -0400146 const auto t = timer.msec() - fTimeBase;
147 const auto d = fAnimation->duration() * 1000;
148 fAnimation->seek(std::fmod(t, d) / d);
Florin Malita094ccde2017-12-30 12:27:00 -0500149 }
150 return true;
151}
152
Florin Malita54f65c42018-01-16 17:04:30 -0500153bool SkottieSlide::onChar(SkUnichar c) {
Florin Malita094ccde2017-12-30 12:27:00 -0500154 switch (c) {
155 case 'I':
Florin Malita6eb85a12018-04-30 10:32:18 -0400156 fShowAnimationStats = !fShowAnimationStats;
Florin Malita094ccde2017-12-30 12:27:00 -0500157 break;
158 default:
159 break;
160 }
161
162 return INHERITED::onChar(c);
163}
Florin Malita60d3bfc2018-02-20 16:49:20 -0500164
165bool SkottieSlide::onMouse(SkScalar x, SkScalar y, sk_app::Window::InputState state, uint32_t) {
166 switch (state) {
167 case sk_app::Window::kUp_InputState:
168 fShowAnimationInval = !fShowAnimationInval;
Florin Malita6eb85a12018-04-30 10:32:18 -0400169 fShowAnimationStats = !fShowAnimationStats;
Florin Malita60d3bfc2018-02-20 16:49:20 -0500170 fAnimation->setShowInval(fShowAnimationInval);
171 break;
172 default:
173 break;
174 }
175
Florin Malita83286a02018-02-21 13:03:41 -0500176 return false;
Florin Malita60d3bfc2018-02-20 16:49:20 -0500177}
Florin Malita3d856bd2018-05-26 09:49:28 -0400178
179#endif // SK_ENABLE_SKOTTIE