blob: dd396055aae2dcc7614905a85273267f3e21690d [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"
Florin Malita15ee9702019-12-10 14:23:32 -050014#include "include/core/SkTime.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "modules/skottie/include/Skottie.h"
Brian Osman849f4d62019-11-26 08:58:26 -050016#include "modules/skresources/include/SkResources.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/utils/SkOSPath.h"
Hal Canary41248072019-07-11 16:32:53 -040018#include "tools/timer/TimeUtils.h"
Florin Malita094ccde2017-12-30 12:27:00 -050019
Florin Malitaa33447d2018-05-29 13:46:54 -040020#include <cmath>
21
Florin Malitac4f6e022019-12-10 13:38:45 -050022#include "imgui.h"
23
Florin Malita40c37422018-08-22 20:37:04 -040024static void draw_stats_box(SkCanvas* canvas, const skottie::Animation::Builder::Stats& stats) {
Florin Malita6eb85a12018-04-30 10:32:18 -040025 static constexpr SkRect kR = { 10, 10, 280, 120 };
26 static constexpr SkScalar kTextSize = 20;
27
28 SkPaint paint;
29 paint.setAntiAlias(true);
30 paint.setColor(0xffeeeeee);
Hal Canarydf2d27e2019-01-08 09:38:02 -050031
32 SkFont font(nullptr, kTextSize);
Florin Malita6eb85a12018-04-30 10:32:18 -040033
34 canvas->drawRect(kR, paint);
35
36 paint.setColor(SK_ColorBLACK);
37
38 const auto json_size = SkStringPrintf("Json size: %lu bytes",
39 stats.fJsonSize);
Hal Canarydf2d27e2019-01-08 09:38:02 -050040 canvas->drawString(json_size, kR.x() + 10, kR.y() + kTextSize * 1, font, paint);
Florin Malita6eb85a12018-04-30 10:32:18 -040041 const auto animator_count = SkStringPrintf("Animator count: %lu",
42 stats.fAnimatorCount);
Hal Canarydf2d27e2019-01-08 09:38:02 -050043 canvas->drawString(animator_count, kR.x() + 10, kR.y() + kTextSize * 2, font, paint);
Florin Malita6eb85a12018-04-30 10:32:18 -040044 const auto json_parse_time = SkStringPrintf("Json parse time: %.3f ms",
45 stats.fJsonParseTimeMS);
Hal Canarydf2d27e2019-01-08 09:38:02 -050046 canvas->drawString(json_parse_time, kR.x() + 10, kR.y() + kTextSize * 3, font, paint);
Florin Malita6eb85a12018-04-30 10:32:18 -040047 const auto scene_parse_time = SkStringPrintf("Scene build time: %.3f ms",
48 stats.fSceneParseTimeMS);
Hal Canarydf2d27e2019-01-08 09:38:02 -050049 canvas->drawString(scene_parse_time, kR.x() + 10, kR.y() + kTextSize * 4, font, paint);
Florin Malita6eb85a12018-04-30 10:32:18 -040050 const auto total_load_time = SkStringPrintf("Total load time: %.3f ms",
51 stats.fTotalLoadTimeMS);
Hal Canarydf2d27e2019-01-08 09:38:02 -050052 canvas->drawString(total_load_time, kR.x() + 10, kR.y() + kTextSize * 5, font, paint);
Florin Malita6eb85a12018-04-30 10:32:18 -040053
54 paint.setStyle(SkPaint::kStroke_Style);
55 canvas->drawRect(kR, paint);
56}
57
Florin Malita54f65c42018-01-16 17:04:30 -050058SkottieSlide::SkottieSlide(const SkString& name, const SkString& path)
Florin Malita094ccde2017-12-30 12:27:00 -050059 : fPath(path) {
60 fName = name;
61}
62
Florin Malitac378fdc2018-02-09 11:15:32 -050063void SkottieSlide::load(SkScalar w, SkScalar h) {
Florin Malita57b9d402018-10-02 12:48:00 -040064 class Logger final : public skottie::Logger {
65 public:
66 struct LogEntry {
67 SkString fMessage,
68 fJSON;
69 };
70
71 void log(skottie::Logger::Level lvl, const char message[], const char json[]) override {
72 auto& log = lvl == skottie::Logger::Level::kError ? fErrors : fWarnings;
73 log.push_back({ SkString(message), json ? SkString(json) : SkString() });
74 }
75
76 void report() const {
77 SkDebugf("Animation loaded with %lu error%s, %lu warning%s.\n",
78 fErrors.size(), fErrors.size() == 1 ? "" : "s",
79 fWarnings.size(), fWarnings.size() == 1 ? "" : "s");
80
81 const auto& show = [](const LogEntry& log, const char prefix[]) {
82 SkDebugf("%s%s", prefix, log.fMessage.c_str());
83 if (!log.fJSON.isEmpty())
84 SkDebugf(" : %s", log.fJSON.c_str());
85 SkDebugf("\n");
86 };
87
88 for (const auto& err : fErrors) show(err, " !! ");
89 for (const auto& wrn : fWarnings) show(wrn, " ?? ");
90 }
91
92 private:
93 std::vector<LogEntry> fErrors,
94 fWarnings;
95 };
96
97 auto logger = sk_make_sp<Logger>();
Florin Malita40c37422018-08-22 20:37:04 -040098 skottie::Animation::Builder builder;
Florin Malita57b9d402018-10-02 12:48:00 -040099
Florin Malitaa8316552018-11-09 16:19:44 -0500100 fAnimation = builder
101 .setLogger(logger)
102 .setResourceProvider(
Brian Osman849f4d62019-11-26 08:58:26 -0500103 skresources::DataURIResourceProviderProxy::Make(
104 skresources::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str()),
Florin Malitacfbc4d42019-10-18 13:13:40 -0400105 /*predecode=*/true),
106 /*predecode=*/true))
Florin Malitaa8316552018-11-09 16:19:44 -0500107 .makeFromFile(fPath.c_str());
Florin Malita40c37422018-08-22 20:37:04 -0400108 fAnimationStats = builder.getStats();
109 fWinSize = SkSize::Make(w, h);
110 fTimeBase = 0; // force a time reset
Florin Malita094ccde2017-12-30 12:27:00 -0500111
112 if (fAnimation) {
Florin Malita87037482019-12-09 11:09:38 -0500113 fAnimation->seek(0);
Florin Malita15ee9702019-12-10 14:23:32 -0500114 fFrameTimes.resize(SkScalarCeilToInt(fAnimation->duration() * fAnimation->fps()));
Florin Malita57b9d402018-10-02 12:48:00 -0400115 SkDebugf("Loaded Bodymovin animation v: %s, size: [%f %f]\n",
Florin Malita094ccde2017-12-30 12:27:00 -0500116 fAnimation->version().c_str(),
117 fAnimation->size().width(),
Florin Malita911ae402018-05-31 16:45:29 -0400118 fAnimation->size().height());
Florin Malita57b9d402018-10-02 12:48:00 -0400119 logger->report();
Florin Malita094ccde2017-12-30 12:27:00 -0500120 } else {
121 SkDebugf("failed to load Bodymovin animation: %s\n", fPath.c_str());
122 }
123}
124
Florin Malita54f65c42018-01-16 17:04:30 -0500125void SkottieSlide::unload() {
Florin Malita094ccde2017-12-30 12:27:00 -0500126 fAnimation.reset();
127}
128
Florin Malitac4f6e022019-12-10 13:38:45 -0500129void SkottieSlide::resize(SkScalar w, SkScalar h) {
130 fWinSize = { w, h };
131}
132
Florin Malita54f65c42018-01-16 17:04:30 -0500133SkISize SkottieSlide::getDimensions() const {
Florin Malitac378fdc2018-02-09 11:15:32 -0500134 // We always scale to fill the window.
135 return fWinSize.toCeil();
Florin Malita094ccde2017-12-30 12:27:00 -0500136}
137
Florin Malita54f65c42018-01-16 17:04:30 -0500138void SkottieSlide::draw(SkCanvas* canvas) {
Florin Malita094ccde2017-12-30 12:27:00 -0500139 if (fAnimation) {
Florin Malitaaa4dc622018-01-02 14:37:37 -0500140 SkAutoCanvasRestore acr(canvas, true);
Florin Malitac378fdc2018-02-09 11:15:32 -0500141 const auto dstR = SkRect::MakeSize(fWinSize);
Florin Malita15ee9702019-12-10 14:23:32 -0500142
143 {
144 const auto t0 = SkTime::GetNSecs();
145 fAnimation->render(canvas, &dstR);
146
147 // TODO: this does not capture GPU flush time!
148 const auto frame_index = SkToSizeT(SkScalarRoundToInt(fCurrentFrame));
149 fFrameTimes[frame_index] = static_cast<float>((SkTime::GetNSecs() - t0) * 1e-6);
150 }
Florin Malita6eb85a12018-04-30 10:32:18 -0400151
152 if (fShowAnimationStats) {
153 draw_stats_box(canvas, fAnimationStats);
154 }
Florin Malita00d4f532019-07-22 12:05:41 -0400155 if (fShowAnimationInval) {
156 const auto t = SkMatrix::MakeRectToRect(SkRect::MakeSize(fAnimation->size()),
157 dstR,
158 SkMatrix::kCenter_ScaleToFit);
159 SkPaint fill, stroke;
160 fill.setAntiAlias(true);
161 fill.setColor(0x40ff0000);
162 stroke.setAntiAlias(true);
163 stroke.setColor(0xffff0000);
164 stroke.setStyle(SkPaint::kStroke_Style);
165
166 for (const auto& r : fInvalController) {
167 SkRect bounds;
168 t.mapRect(&bounds, r);
169 canvas->drawRect(bounds, fill);
170 canvas->drawRect(bounds, stroke);
171 }
172 }
Florin Malitac4f6e022019-12-10 13:38:45 -0500173 if (fShowUI) {
174 this->renderUI();
175 }
176
Florin Malita094ccde2017-12-30 12:27:00 -0500177 }
178}
179
Hal Canary41248072019-07-11 16:32:53 -0400180bool SkottieSlide::animate(double nanos) {
181 SkMSec msec = TimeUtils::NanosToMSec(nanos);
Florin Malita094ccde2017-12-30 12:27:00 -0500182 if (fTimeBase == 0) {
183 // Reset the animation time.
Hal Canary41248072019-07-11 16:32:53 -0400184 fTimeBase = msec;
Florin Malita094ccde2017-12-30 12:27:00 -0500185 }
186
187 if (fAnimation) {
Florin Malita00d4f532019-07-22 12:05:41 -0400188 fInvalController.reset();
Florin Malitac4f6e022019-12-10 13:38:45 -0500189
190 if (!fDraggingProgress) {
191 // Clock-driven progress: update current frame.
192 const double t_sec = (msec - fTimeBase) / 1000.0;
193 fCurrentFrame = std::fmod(t_sec, fAnimation->duration()) * fAnimation->fps();
194 } else {
195 // Slider-driven progress: update the time origin.
196 fTimeBase = TimeUtils::NanosToMSec(nanos)
197 - static_cast<SkMSec>(fCurrentFrame / fAnimation->fps() * 1000);
198 }
199
200 fAnimation->seekFrame(fCurrentFrame);
Florin Malita094ccde2017-12-30 12:27:00 -0500201 }
202 return true;
203}
204
Florin Malita54f65c42018-01-16 17:04:30 -0500205bool SkottieSlide::onChar(SkUnichar c) {
Florin Malita094ccde2017-12-30 12:27:00 -0500206 switch (c) {
207 case 'I':
Florin Malita6eb85a12018-04-30 10:32:18 -0400208 fShowAnimationStats = !fShowAnimationStats;
Florin Malita094ccde2017-12-30 12:27:00 -0500209 break;
210 default:
211 break;
212 }
213
214 return INHERITED::onChar(c);
215}
Florin Malita60d3bfc2018-02-20 16:49:20 -0500216
Hal Canaryb1f411a2019-08-29 10:39:22 -0400217bool SkottieSlide::onMouse(SkScalar x, SkScalar y, skui::InputState state, skui::ModifierKey) {
Florin Malita60d3bfc2018-02-20 16:49:20 -0500218 switch (state) {
Hal Canaryb1f411a2019-08-29 10:39:22 -0400219 case skui::InputState::kUp:
Florin Malita60d3bfc2018-02-20 16:49:20 -0500220 fShowAnimationInval = !fShowAnimationInval;
Florin Malita6eb85a12018-04-30 10:32:18 -0400221 fShowAnimationStats = !fShowAnimationStats;
Florin Malita60d3bfc2018-02-20 16:49:20 -0500222 break;
223 default:
224 break;
225 }
226
Florin Malitac4f6e022019-12-10 13:38:45 -0500227 fShowUI = this->UIArea().contains(x, y);
228
Florin Malita83286a02018-02-21 13:03:41 -0500229 return false;
Florin Malita60d3bfc2018-02-20 16:49:20 -0500230}
Florin Malita3d856bd2018-05-26 09:49:28 -0400231
Florin Malitac4f6e022019-12-10 13:38:45 -0500232SkRect SkottieSlide::UIArea() const {
233 static constexpr float kUIHeight = 150.0f;
234
235 return SkRect::MakeXYWH(0, fWinSize.height() - kUIHeight, fWinSize.width(), kUIHeight);
236}
237
238void SkottieSlide::renderUI() {
Florin Malita15ee9702019-12-10 14:23:32 -0500239 static constexpr auto kUI_opacity = 0.35f,
240 kUI_hist_height = 50.0f;
Florin Malitac4f6e022019-12-10 13:38:45 -0500241
242 ImGui::SetNextWindowBgAlpha(kUI_opacity);
243 if (ImGui::Begin("Skottie Controls", nullptr, ImGuiWindowFlags_NoDecoration |
244 ImGuiWindowFlags_NoResize |
245 ImGuiWindowFlags_NoMove |
246 ImGuiWindowFlags_NoSavedSettings |
247 ImGuiWindowFlags_NoFocusOnAppearing |
248 ImGuiWindowFlags_NoNav)) {
249 const auto ui_area = this->UIArea();
250 ImGui::SetWindowPos(ImVec2(ui_area.x(), ui_area.y()));
251 ImGui::SetWindowSize(ImVec2(ui_area.width(), ui_area.height()));
252
253 ImGui::PushItemWidth(-1);
Florin Malita15ee9702019-12-10 14:23:32 -0500254
255 ImGui::PlotHistogram("", fFrameTimes.data(), fFrameTimes.size(),
256 0, nullptr, FLT_MAX, FLT_MAX, ImVec2(0, kUI_hist_height));
257 ImGui::SliderFloat("", &fCurrentFrame, 0, fAnimation->duration() * fAnimation->fps() - 1);
Florin Malitac4f6e022019-12-10 13:38:45 -0500258 fDraggingProgress = ImGui::IsItemActive();
259
260 ImGui::PopItemWidth();
261 }
262 ImGui::End();
263}
264
Florin Malita3d856bd2018-05-26 09:49:28 -0400265#endif // SK_ENABLE_SKOTTIE