blob: 251faaaefe8d9cefedc2b74c54cdd0c64e7b4048 [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
Florin Malita54f65c42018-01-16 17:04:30 -05008#include "SkottieSlide.h"
Florin Malita094ccde2017-12-30 12:27:00 -05009
Florin Malita3d856bd2018-05-26 09:49:28 -040010#if defined(SK_ENABLE_SKOTTIE)
11
Florin Malita094ccde2017-12-30 12:27:00 -050012#include "SkAnimTimer.h"
Florin Malitaaa4dc622018-01-02 14:37:37 -050013#include "SkCanvas.h"
Florin Malita54f65c42018-01-16 17:04:30 -050014#include "Skottie.h"
Florin Malita094ccde2017-12-30 12:27:00 -050015
Florin Malitaa33447d2018-05-29 13:46:54 -040016#include <cmath>
17
Florin Malita40c37422018-08-22 20:37:04 -040018static void draw_stats_box(SkCanvas* canvas, const skottie::Animation::Builder::Stats& stats) {
Florin Malita6eb85a12018-04-30 10:32:18 -040019 static constexpr SkRect kR = { 10, 10, 280, 120 };
20 static constexpr SkScalar kTextSize = 20;
21
22 SkPaint paint;
23 paint.setAntiAlias(true);
24 paint.setColor(0xffeeeeee);
25 paint.setTextSize(kTextSize);
26
27 canvas->drawRect(kR, paint);
28
29 paint.setColor(SK_ColorBLACK);
30
31 const auto json_size = SkStringPrintf("Json size: %lu bytes",
32 stats.fJsonSize);
33 canvas->drawText(json_size.c_str(),
34 json_size.size(), kR.x() + 10, kR.y() + kTextSize * 1, paint);
35 const auto animator_count = SkStringPrintf("Animator count: %lu",
36 stats.fAnimatorCount);
37 canvas->drawText(animator_count.c_str(),
38 animator_count.size(), kR.x() + 10, kR.y() + kTextSize * 2, paint);
39 const auto json_parse_time = SkStringPrintf("Json parse time: %.3f ms",
40 stats.fJsonParseTimeMS);
41 canvas->drawText(json_parse_time.c_str(),
42 json_parse_time.size(), kR.x() + 10, kR.y() + kTextSize * 3, paint);
43 const auto scene_parse_time = SkStringPrintf("Scene build time: %.3f ms",
44 stats.fSceneParseTimeMS);
45 canvas->drawText(scene_parse_time.c_str(),
46 scene_parse_time.size(), kR.x() + 10, kR.y() + kTextSize * 4, paint);
47 const auto total_load_time = SkStringPrintf("Total load time: %.3f ms",
48 stats.fTotalLoadTimeMS);
49 canvas->drawText(total_load_time.c_str(),
50 total_load_time.size(), kR.x() + 10, kR.y() + kTextSize * 5, paint);
51
52 paint.setStyle(SkPaint::kStroke_Style);
53 canvas->drawRect(kR, paint);
54}
55
Florin Malita54f65c42018-01-16 17:04:30 -050056SkottieSlide::SkottieSlide(const SkString& name, const SkString& path)
Florin Malita094ccde2017-12-30 12:27:00 -050057 : fPath(path) {
58 fName = name;
59}
60
Florin Malitac378fdc2018-02-09 11:15:32 -050061void SkottieSlide::load(SkScalar w, SkScalar h) {
Florin Malita57b9d402018-10-02 12:48:00 -040062 class Logger final : public skottie::Logger {
63 public:
64 struct LogEntry {
65 SkString fMessage,
66 fJSON;
67 };
68
69 void log(skottie::Logger::Level lvl, const char message[], const char json[]) override {
70 auto& log = lvl == skottie::Logger::Level::kError ? fErrors : fWarnings;
71 log.push_back({ SkString(message), json ? SkString(json) : SkString() });
72 }
73
74 void report() const {
75 SkDebugf("Animation loaded with %lu error%s, %lu warning%s.\n",
76 fErrors.size(), fErrors.size() == 1 ? "" : "s",
77 fWarnings.size(), fWarnings.size() == 1 ? "" : "s");
78
79 const auto& show = [](const LogEntry& log, const char prefix[]) {
80 SkDebugf("%s%s", prefix, log.fMessage.c_str());
81 if (!log.fJSON.isEmpty())
82 SkDebugf(" : %s", log.fJSON.c_str());
83 SkDebugf("\n");
84 };
85
86 for (const auto& err : fErrors) show(err, " !! ");
87 for (const auto& wrn : fWarnings) show(wrn, " ?? ");
88 }
89
90 private:
91 std::vector<LogEntry> fErrors,
92 fWarnings;
93 };
94
95 auto logger = sk_make_sp<Logger>();
Florin Malita40c37422018-08-22 20:37:04 -040096 skottie::Animation::Builder builder;
Florin Malita57b9d402018-10-02 12:48:00 -040097
98 fAnimation = builder.setLogger(logger).makeFromFile(fPath.c_str());
Florin Malita40c37422018-08-22 20:37:04 -040099 fAnimationStats = builder.getStats();
100 fWinSize = SkSize::Make(w, h);
101 fTimeBase = 0; // force a time reset
Florin Malita094ccde2017-12-30 12:27:00 -0500102
103 if (fAnimation) {
Florin Malitadf2713c2018-01-09 15:51:21 -0500104 fAnimation->setShowInval(fShowAnimationInval);
Florin Malita57b9d402018-10-02 12:48:00 -0400105 SkDebugf("Loaded Bodymovin animation v: %s, size: [%f %f]\n",
Florin Malita094ccde2017-12-30 12:27:00 -0500106 fAnimation->version().c_str(),
107 fAnimation->size().width(),
Florin Malita911ae402018-05-31 16:45:29 -0400108 fAnimation->size().height());
Florin Malita57b9d402018-10-02 12:48:00 -0400109 logger->report();
Florin Malita094ccde2017-12-30 12:27:00 -0500110 } else {
111 SkDebugf("failed to load Bodymovin animation: %s\n", fPath.c_str());
112 }
113}
114
Florin Malita54f65c42018-01-16 17:04:30 -0500115void SkottieSlide::unload() {
Florin Malita094ccde2017-12-30 12:27:00 -0500116 fAnimation.reset();
117}
118
Florin Malita54f65c42018-01-16 17:04:30 -0500119SkISize SkottieSlide::getDimensions() const {
Florin Malitac378fdc2018-02-09 11:15:32 -0500120 // We always scale to fill the window.
121 return fWinSize.toCeil();
Florin Malita094ccde2017-12-30 12:27:00 -0500122}
123
Florin Malita54f65c42018-01-16 17:04:30 -0500124void SkottieSlide::draw(SkCanvas* canvas) {
Florin Malita094ccde2017-12-30 12:27:00 -0500125 if (fAnimation) {
Florin Malitaaa4dc622018-01-02 14:37:37 -0500126 SkAutoCanvasRestore acr(canvas, true);
Florin Malitac378fdc2018-02-09 11:15:32 -0500127 const auto dstR = SkRect::MakeSize(fWinSize);
Mike Reed29859872018-01-08 08:25:27 -0500128 fAnimation->render(canvas, &dstR);
Florin Malita6eb85a12018-04-30 10:32:18 -0400129
130 if (fShowAnimationStats) {
131 draw_stats_box(canvas, fAnimationStats);
132 }
Florin Malita094ccde2017-12-30 12:27:00 -0500133 }
134}
135
Florin Malita54f65c42018-01-16 17:04:30 -0500136bool SkottieSlide::animate(const SkAnimTimer& timer) {
Florin Malita094ccde2017-12-30 12:27:00 -0500137 if (fTimeBase == 0) {
138 // Reset the animation time.
139 fTimeBase = timer.msec();
140 }
141
142 if (fAnimation) {
Florin Malitaa33447d2018-05-29 13:46:54 -0400143 const auto t = timer.msec() - fTimeBase;
144 const auto d = fAnimation->duration() * 1000;
145 fAnimation->seek(std::fmod(t, d) / d);
Florin Malita094ccde2017-12-30 12:27:00 -0500146 }
147 return true;
148}
149
Florin Malita54f65c42018-01-16 17:04:30 -0500150bool SkottieSlide::onChar(SkUnichar c) {
Florin Malita094ccde2017-12-30 12:27:00 -0500151 switch (c) {
152 case 'I':
Florin Malita6eb85a12018-04-30 10:32:18 -0400153 fShowAnimationStats = !fShowAnimationStats;
Florin Malita094ccde2017-12-30 12:27:00 -0500154 break;
155 default:
156 break;
157 }
158
159 return INHERITED::onChar(c);
160}
Florin Malita60d3bfc2018-02-20 16:49:20 -0500161
162bool SkottieSlide::onMouse(SkScalar x, SkScalar y, sk_app::Window::InputState state, uint32_t) {
163 switch (state) {
164 case sk_app::Window::kUp_InputState:
165 fShowAnimationInval = !fShowAnimationInval;
Florin Malita6eb85a12018-04-30 10:32:18 -0400166 fShowAnimationStats = !fShowAnimationStats;
Florin Malita60d3bfc2018-02-20 16:49:20 -0500167 fAnimation->setShowInval(fShowAnimationInval);
168 break;
169 default:
170 break;
171 }
172
Florin Malita83286a02018-02-21 13:03:41 -0500173 return false;
Florin Malita60d3bfc2018-02-20 16:49:20 -0500174}
Florin Malita3d856bd2018-05-26 09:49:28 -0400175
176#endif // SK_ENABLE_SKOTTIE