blob: a40571cd8e84f3eda77d2257be09ce5376cf77c2 [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"
Brian Osman849f4d62019-11-26 08:58:26 -050015#include "modules/skresources/include/SkResources.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/utils/SkOSPath.h"
Hal Canary41248072019-07-11 16:32:53 -040017#include "tools/timer/TimeUtils.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(
Brian Osman849f4d62019-11-26 08:58:26 -0500100 skresources::DataURIResourceProviderProxy::Make(
101 skresources::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str()),
Florin Malitacfbc4d42019-10-18 13:13:40 -0400102 /*predecode=*/true),
103 /*predecode=*/true))
Florin Malitaa8316552018-11-09 16:19:44 -0500104 .makeFromFile(fPath.c_str());
Florin Malita40c37422018-08-22 20:37:04 -0400105 fAnimationStats = builder.getStats();
106 fWinSize = SkSize::Make(w, h);
107 fTimeBase = 0; // force a time reset
Florin Malita094ccde2017-12-30 12:27:00 -0500108
109 if (fAnimation) {
Florin Malita87037482019-12-09 11:09:38 -0500110 fAnimation->seek(0);
Florin Malita57b9d402018-10-02 12:48:00 -0400111 SkDebugf("Loaded Bodymovin animation v: %s, size: [%f %f]\n",
Florin Malita094ccde2017-12-30 12:27:00 -0500112 fAnimation->version().c_str(),
113 fAnimation->size().width(),
Florin Malita911ae402018-05-31 16:45:29 -0400114 fAnimation->size().height());
Florin Malita57b9d402018-10-02 12:48:00 -0400115 logger->report();
Florin Malita094ccde2017-12-30 12:27:00 -0500116 } else {
117 SkDebugf("failed to load Bodymovin animation: %s\n", fPath.c_str());
118 }
119}
120
Florin Malita54f65c42018-01-16 17:04:30 -0500121void SkottieSlide::unload() {
Florin Malita094ccde2017-12-30 12:27:00 -0500122 fAnimation.reset();
123}
124
Florin Malita54f65c42018-01-16 17:04:30 -0500125SkISize SkottieSlide::getDimensions() const {
Florin Malitac378fdc2018-02-09 11:15:32 -0500126 // We always scale to fill the window.
127 return fWinSize.toCeil();
Florin Malita094ccde2017-12-30 12:27:00 -0500128}
129
Florin Malita54f65c42018-01-16 17:04:30 -0500130void SkottieSlide::draw(SkCanvas* canvas) {
Florin Malita094ccde2017-12-30 12:27:00 -0500131 if (fAnimation) {
Florin Malitaaa4dc622018-01-02 14:37:37 -0500132 SkAutoCanvasRestore acr(canvas, true);
Florin Malitac378fdc2018-02-09 11:15:32 -0500133 const auto dstR = SkRect::MakeSize(fWinSize);
Mike Reed29859872018-01-08 08:25:27 -0500134 fAnimation->render(canvas, &dstR);
Florin Malita6eb85a12018-04-30 10:32:18 -0400135
136 if (fShowAnimationStats) {
137 draw_stats_box(canvas, fAnimationStats);
138 }
Florin Malita00d4f532019-07-22 12:05:41 -0400139 if (fShowAnimationInval) {
140 const auto t = SkMatrix::MakeRectToRect(SkRect::MakeSize(fAnimation->size()),
141 dstR,
142 SkMatrix::kCenter_ScaleToFit);
143 SkPaint fill, stroke;
144 fill.setAntiAlias(true);
145 fill.setColor(0x40ff0000);
146 stroke.setAntiAlias(true);
147 stroke.setColor(0xffff0000);
148 stroke.setStyle(SkPaint::kStroke_Style);
149
150 for (const auto& r : fInvalController) {
151 SkRect bounds;
152 t.mapRect(&bounds, r);
153 canvas->drawRect(bounds, fill);
154 canvas->drawRect(bounds, stroke);
155 }
156 }
Florin Malita094ccde2017-12-30 12:27:00 -0500157 }
158}
159
Hal Canary41248072019-07-11 16:32:53 -0400160bool SkottieSlide::animate(double nanos) {
161 SkMSec msec = TimeUtils::NanosToMSec(nanos);
Florin Malita094ccde2017-12-30 12:27:00 -0500162 if (fTimeBase == 0) {
163 // Reset the animation time.
Hal Canary41248072019-07-11 16:32:53 -0400164 fTimeBase = msec;
Florin Malita094ccde2017-12-30 12:27:00 -0500165 }
166
167 if (fAnimation) {
Florin Malita00d4f532019-07-22 12:05:41 -0400168 fInvalController.reset();
Hal Canary41248072019-07-11 16:32:53 -0400169 const auto t = msec - fTimeBase;
Florin Malitaa33447d2018-05-29 13:46:54 -0400170 const auto d = fAnimation->duration() * 1000;
Florin Malita00d4f532019-07-22 12:05:41 -0400171 fAnimation->seek(std::fmod(t, d) / d, &fInvalController);
Florin Malita094ccde2017-12-30 12:27:00 -0500172 }
173 return true;
174}
175
Florin Malita54f65c42018-01-16 17:04:30 -0500176bool SkottieSlide::onChar(SkUnichar c) {
Florin Malita094ccde2017-12-30 12:27:00 -0500177 switch (c) {
178 case 'I':
Florin Malita6eb85a12018-04-30 10:32:18 -0400179 fShowAnimationStats = !fShowAnimationStats;
Florin Malita094ccde2017-12-30 12:27:00 -0500180 break;
181 default:
182 break;
183 }
184
185 return INHERITED::onChar(c);
186}
Florin Malita60d3bfc2018-02-20 16:49:20 -0500187
Hal Canaryb1f411a2019-08-29 10:39:22 -0400188bool SkottieSlide::onMouse(SkScalar x, SkScalar y, skui::InputState state, skui::ModifierKey) {
Florin Malita60d3bfc2018-02-20 16:49:20 -0500189 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -0400190 case skui::InputState::kUp:
Florin Malita60d3bfc2018-02-20 16:49:20 -0500191 fShowAnimationInval = !fShowAnimationInval;
Florin Malita6eb85a12018-04-30 10:32:18 -0400192 fShowAnimationStats = !fShowAnimationStats;
Florin Malita60d3bfc2018-02-20 16:49:20 -0500193 break;
194 default:
195 break;
196 }
197
Florin Malita83286a02018-02-21 13:03:41 -0500198 return false;
Florin Malita60d3bfc2018-02-20 16:49:20 -0500199}
Florin Malita3d856bd2018-05-26 09:49:28 -0400200
201#endif // SK_ENABLE_SKOTTIE