blob: 0c427b1eaf5f11204d9a7a692503fc0220ce5293 [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 Malitac4f6e022019-12-10 13:38:45 -050021#include "imgui.h"
22
Florin Malita40c37422018-08-22 20:37:04 -040023static void draw_stats_box(SkCanvas* canvas, const skottie::Animation::Builder::Stats& stats) {
Florin Malita6eb85a12018-04-30 10:32:18 -040024 static constexpr SkRect kR = { 10, 10, 280, 120 };
25 static constexpr SkScalar kTextSize = 20;
26
27 SkPaint paint;
28 paint.setAntiAlias(true);
29 paint.setColor(0xffeeeeee);
Hal Canarydf2d27e2019-01-08 09:38:02 -050030
31 SkFont font(nullptr, kTextSize);
Florin Malita6eb85a12018-04-30 10:32:18 -040032
33 canvas->drawRect(kR, paint);
34
35 paint.setColor(SK_ColorBLACK);
36
37 const auto json_size = SkStringPrintf("Json size: %lu bytes",
38 stats.fJsonSize);
Hal Canarydf2d27e2019-01-08 09:38:02 -050039 canvas->drawString(json_size, kR.x() + 10, kR.y() + kTextSize * 1, font, paint);
Florin Malita6eb85a12018-04-30 10:32:18 -040040 const auto animator_count = SkStringPrintf("Animator count: %lu",
41 stats.fAnimatorCount);
Hal Canarydf2d27e2019-01-08 09:38:02 -050042 canvas->drawString(animator_count, kR.x() + 10, kR.y() + kTextSize * 2, font, paint);
Florin Malita6eb85a12018-04-30 10:32:18 -040043 const auto json_parse_time = SkStringPrintf("Json parse time: %.3f ms",
44 stats.fJsonParseTimeMS);
Hal Canarydf2d27e2019-01-08 09:38:02 -050045 canvas->drawString(json_parse_time, kR.x() + 10, kR.y() + kTextSize * 3, font, paint);
Florin Malita6eb85a12018-04-30 10:32:18 -040046 const auto scene_parse_time = SkStringPrintf("Scene build time: %.3f ms",
47 stats.fSceneParseTimeMS);
Hal Canarydf2d27e2019-01-08 09:38:02 -050048 canvas->drawString(scene_parse_time, kR.x() + 10, kR.y() + kTextSize * 4, font, paint);
Florin Malita6eb85a12018-04-30 10:32:18 -040049 const auto total_load_time = SkStringPrintf("Total load time: %.3f ms",
50 stats.fTotalLoadTimeMS);
Hal Canarydf2d27e2019-01-08 09:38:02 -050051 canvas->drawString(total_load_time, kR.x() + 10, kR.y() + kTextSize * 5, font, paint);
Florin Malita6eb85a12018-04-30 10:32:18 -040052
53 paint.setStyle(SkPaint::kStroke_Style);
54 canvas->drawRect(kR, paint);
55}
56
Florin Malita54f65c42018-01-16 17:04:30 -050057SkottieSlide::SkottieSlide(const SkString& name, const SkString& path)
Florin Malita094ccde2017-12-30 12:27:00 -050058 : fPath(path) {
59 fName = name;
60}
61
Florin Malitac378fdc2018-02-09 11:15:32 -050062void SkottieSlide::load(SkScalar w, SkScalar h) {
Florin Malita57b9d402018-10-02 12:48:00 -040063 class Logger final : public skottie::Logger {
64 public:
65 struct LogEntry {
66 SkString fMessage,
67 fJSON;
68 };
69
70 void log(skottie::Logger::Level lvl, const char message[], const char json[]) override {
71 auto& log = lvl == skottie::Logger::Level::kError ? fErrors : fWarnings;
72 log.push_back({ SkString(message), json ? SkString(json) : SkString() });
73 }
74
75 void report() const {
76 SkDebugf("Animation loaded with %lu error%s, %lu warning%s.\n",
77 fErrors.size(), fErrors.size() == 1 ? "" : "s",
78 fWarnings.size(), fWarnings.size() == 1 ? "" : "s");
79
80 const auto& show = [](const LogEntry& log, const char prefix[]) {
81 SkDebugf("%s%s", prefix, log.fMessage.c_str());
82 if (!log.fJSON.isEmpty())
83 SkDebugf(" : %s", log.fJSON.c_str());
84 SkDebugf("\n");
85 };
86
87 for (const auto& err : fErrors) show(err, " !! ");
88 for (const auto& wrn : fWarnings) show(wrn, " ?? ");
89 }
90
91 private:
92 std::vector<LogEntry> fErrors,
93 fWarnings;
94 };
95
96 auto logger = sk_make_sp<Logger>();
Florin Malita40c37422018-08-22 20:37:04 -040097 skottie::Animation::Builder builder;
Florin Malita57b9d402018-10-02 12:48:00 -040098
Florin Malitaa8316552018-11-09 16:19:44 -050099 fAnimation = builder
100 .setLogger(logger)
101 .setResourceProvider(
Brian Osman849f4d62019-11-26 08:58:26 -0500102 skresources::DataURIResourceProviderProxy::Make(
103 skresources::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str()),
Florin Malitacfbc4d42019-10-18 13:13:40 -0400104 /*predecode=*/true),
105 /*predecode=*/true))
Florin Malitaa8316552018-11-09 16:19:44 -0500106 .makeFromFile(fPath.c_str());
Florin Malita40c37422018-08-22 20:37:04 -0400107 fAnimationStats = builder.getStats();
108 fWinSize = SkSize::Make(w, h);
109 fTimeBase = 0; // force a time reset
Florin Malita094ccde2017-12-30 12:27:00 -0500110
111 if (fAnimation) {
Florin Malita87037482019-12-09 11:09:38 -0500112 fAnimation->seek(0);
Florin Malita57b9d402018-10-02 12:48:00 -0400113 SkDebugf("Loaded Bodymovin animation v: %s, size: [%f %f]\n",
Florin Malita094ccde2017-12-30 12:27:00 -0500114 fAnimation->version().c_str(),
115 fAnimation->size().width(),
Florin Malita911ae402018-05-31 16:45:29 -0400116 fAnimation->size().height());
Florin Malita57b9d402018-10-02 12:48:00 -0400117 logger->report();
Florin Malita094ccde2017-12-30 12:27:00 -0500118 } else {
119 SkDebugf("failed to load Bodymovin animation: %s\n", fPath.c_str());
120 }
121}
122
Florin Malita54f65c42018-01-16 17:04:30 -0500123void SkottieSlide::unload() {
Florin Malita094ccde2017-12-30 12:27:00 -0500124 fAnimation.reset();
125}
126
Florin Malitac4f6e022019-12-10 13:38:45 -0500127void SkottieSlide::resize(SkScalar w, SkScalar h) {
128 fWinSize = { w, h };
129}
130
Florin Malita54f65c42018-01-16 17:04:30 -0500131SkISize SkottieSlide::getDimensions() const {
Florin Malitac378fdc2018-02-09 11:15:32 -0500132 // We always scale to fill the window.
133 return fWinSize.toCeil();
Florin Malita094ccde2017-12-30 12:27:00 -0500134}
135
Florin Malita54f65c42018-01-16 17:04:30 -0500136void SkottieSlide::draw(SkCanvas* canvas) {
Florin Malita094ccde2017-12-30 12:27:00 -0500137 if (fAnimation) {
Florin Malitaaa4dc622018-01-02 14:37:37 -0500138 SkAutoCanvasRestore acr(canvas, true);
Florin Malitac378fdc2018-02-09 11:15:32 -0500139 const auto dstR = SkRect::MakeSize(fWinSize);
Mike Reed29859872018-01-08 08:25:27 -0500140 fAnimation->render(canvas, &dstR);
Florin Malita6eb85a12018-04-30 10:32:18 -0400141
142 if (fShowAnimationStats) {
143 draw_stats_box(canvas, fAnimationStats);
144 }
Florin Malita00d4f532019-07-22 12:05:41 -0400145 if (fShowAnimationInval) {
146 const auto t = SkMatrix::MakeRectToRect(SkRect::MakeSize(fAnimation->size()),
147 dstR,
148 SkMatrix::kCenter_ScaleToFit);
149 SkPaint fill, stroke;
150 fill.setAntiAlias(true);
151 fill.setColor(0x40ff0000);
152 stroke.setAntiAlias(true);
153 stroke.setColor(0xffff0000);
154 stroke.setStyle(SkPaint::kStroke_Style);
155
156 for (const auto& r : fInvalController) {
157 SkRect bounds;
158 t.mapRect(&bounds, r);
159 canvas->drawRect(bounds, fill);
160 canvas->drawRect(bounds, stroke);
161 }
162 }
Florin Malitac4f6e022019-12-10 13:38:45 -0500163 if (fShowUI) {
164 this->renderUI();
165 }
166
Florin Malita094ccde2017-12-30 12:27:00 -0500167 }
168}
169
Hal Canary41248072019-07-11 16:32:53 -0400170bool SkottieSlide::animate(double nanos) {
171 SkMSec msec = TimeUtils::NanosToMSec(nanos);
Florin Malita094ccde2017-12-30 12:27:00 -0500172 if (fTimeBase == 0) {
173 // Reset the animation time.
Hal Canary41248072019-07-11 16:32:53 -0400174 fTimeBase = msec;
Florin Malita094ccde2017-12-30 12:27:00 -0500175 }
176
177 if (fAnimation) {
Florin Malita00d4f532019-07-22 12:05:41 -0400178 fInvalController.reset();
Florin Malitac4f6e022019-12-10 13:38:45 -0500179
180 if (!fDraggingProgress) {
181 // Clock-driven progress: update current frame.
182 const double t_sec = (msec - fTimeBase) / 1000.0;
183 fCurrentFrame = std::fmod(t_sec, fAnimation->duration()) * fAnimation->fps();
184 } else {
185 // Slider-driven progress: update the time origin.
186 fTimeBase = TimeUtils::NanosToMSec(nanos)
187 - static_cast<SkMSec>(fCurrentFrame / fAnimation->fps() * 1000);
188 }
189
190 fAnimation->seekFrame(fCurrentFrame);
Florin Malita094ccde2017-12-30 12:27:00 -0500191 }
192 return true;
193}
194
Florin Malita54f65c42018-01-16 17:04:30 -0500195bool SkottieSlide::onChar(SkUnichar c) {
Florin Malita094ccde2017-12-30 12:27:00 -0500196 switch (c) {
197 case 'I':
Florin Malita6eb85a12018-04-30 10:32:18 -0400198 fShowAnimationStats = !fShowAnimationStats;
Florin Malita094ccde2017-12-30 12:27:00 -0500199 break;
200 default:
201 break;
202 }
203
204 return INHERITED::onChar(c);
205}
Florin Malita60d3bfc2018-02-20 16:49:20 -0500206
Hal Canaryb1f411a2019-08-29 10:39:22 -0400207bool SkottieSlide::onMouse(SkScalar x, SkScalar y, skui::InputState state, skui::ModifierKey) {
Florin Malita60d3bfc2018-02-20 16:49:20 -0500208 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -0400209 case skui::InputState::kUp:
Florin Malita60d3bfc2018-02-20 16:49:20 -0500210 fShowAnimationInval = !fShowAnimationInval;
Florin Malita6eb85a12018-04-30 10:32:18 -0400211 fShowAnimationStats = !fShowAnimationStats;
Florin Malita60d3bfc2018-02-20 16:49:20 -0500212 break;
213 default:
214 break;
215 }
216
Florin Malitac4f6e022019-12-10 13:38:45 -0500217 fShowUI = this->UIArea().contains(x, y);
218
Florin Malita83286a02018-02-21 13:03:41 -0500219 return false;
Florin Malita60d3bfc2018-02-20 16:49:20 -0500220}
Florin Malita3d856bd2018-05-26 09:49:28 -0400221
Florin Malitac4f6e022019-12-10 13:38:45 -0500222SkRect SkottieSlide::UIArea() const {
223 static constexpr float kUIHeight = 150.0f;
224
225 return SkRect::MakeXYWH(0, fWinSize.height() - kUIHeight, fWinSize.width(), kUIHeight);
226}
227
228void SkottieSlide::renderUI() {
229 static constexpr auto kUI_opacity = 0.35f;
230
231 ImGui::SetNextWindowBgAlpha(kUI_opacity);
232 if (ImGui::Begin("Skottie Controls", nullptr, ImGuiWindowFlags_NoDecoration |
233 ImGuiWindowFlags_NoResize |
234 ImGuiWindowFlags_NoMove |
235 ImGuiWindowFlags_NoSavedSettings |
236 ImGuiWindowFlags_NoFocusOnAppearing |
237 ImGuiWindowFlags_NoNav)) {
238 const auto ui_area = this->UIArea();
239 ImGui::SetWindowPos(ImVec2(ui_area.x(), ui_area.y()));
240 ImGui::SetWindowSize(ImVec2(ui_area.width(), ui_area.height()));
241
242 ImGui::PushItemWidth(-1);
243 ImGui::SliderFloat("", &fCurrentFrame, 0, fAnimation->duration() * fAnimation->fps());
244 fDraggingProgress = ImGui::IsItemActive();
245
246 ImGui::PopItemWidth();
247 }
248 ImGui::End();
249}
250
Florin Malita3d856bd2018-05-26 09:49:28 -0400251#endif // SK_ENABLE_SKOTTIE