blob: ca164fb4ed1b9eeee4f2db5ed76e53fdeb71b396 [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 Malitaa8316552018-11-09 16:19:44 -050014#include "SkOSPath.h"
Florin Malita54f65c42018-01-16 17:04:30 -050015#include "Skottie.h"
Florin Malitaa8316552018-11-09 16:19:44 -050016#include "SkottieUtils.h"
Florin Malita094ccde2017-12-30 12:27:00 -050017
Florin Malitaa33447d2018-05-29 13:46:54 -040018#include <cmath>
19
Florin Malita40c37422018-08-22 20:37:04 -040020static void draw_stats_box(SkCanvas* canvas, const skottie::Animation::Builder::Stats& stats) {
Florin Malita6eb85a12018-04-30 10:32:18 -040021 static constexpr SkRect kR = { 10, 10, 280, 120 };
22 static constexpr SkScalar kTextSize = 20;
23
24 SkPaint paint;
25 paint.setAntiAlias(true);
26 paint.setColor(0xffeeeeee);
27 paint.setTextSize(kTextSize);
28
29 canvas->drawRect(kR, paint);
30
31 paint.setColor(SK_ColorBLACK);
32
33 const auto json_size = SkStringPrintf("Json size: %lu bytes",
34 stats.fJsonSize);
35 canvas->drawText(json_size.c_str(),
36 json_size.size(), kR.x() + 10, kR.y() + kTextSize * 1, paint);
37 const auto animator_count = SkStringPrintf("Animator count: %lu",
38 stats.fAnimatorCount);
39 canvas->drawText(animator_count.c_str(),
40 animator_count.size(), kR.x() + 10, kR.y() + kTextSize * 2, paint);
41 const auto json_parse_time = SkStringPrintf("Json parse time: %.3f ms",
42 stats.fJsonParseTimeMS);
43 canvas->drawText(json_parse_time.c_str(),
44 json_parse_time.size(), kR.x() + 10, kR.y() + kTextSize * 3, paint);
45 const auto scene_parse_time = SkStringPrintf("Scene build time: %.3f ms",
46 stats.fSceneParseTimeMS);
47 canvas->drawText(scene_parse_time.c_str(),
48 scene_parse_time.size(), kR.x() + 10, kR.y() + kTextSize * 4, paint);
49 const auto total_load_time = SkStringPrintf("Total load time: %.3f ms",
50 stats.fTotalLoadTimeMS);
51 canvas->drawText(total_load_time.c_str(),
52 total_load_time.size(), kR.x() + 10, kR.y() + kTextSize * 5, paint);
53
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(
103 skottie_utils::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str())))
104 .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 Malitadf2713c2018-01-09 15:51:21 -0500110 fAnimation->setShowInval(fShowAnimationInval);
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 Malita094ccde2017-12-30 12:27:00 -0500139 }
140}
141
Florin Malita54f65c42018-01-16 17:04:30 -0500142bool SkottieSlide::animate(const SkAnimTimer& timer) {
Florin Malita094ccde2017-12-30 12:27:00 -0500143 if (fTimeBase == 0) {
144 // Reset the animation time.
145 fTimeBase = timer.msec();
146 }
147
148 if (fAnimation) {
Florin Malitaa33447d2018-05-29 13:46:54 -0400149 const auto t = timer.msec() - fTimeBase;
150 const auto d = fAnimation->duration() * 1000;
151 fAnimation->seek(std::fmod(t, d) / d);
Florin Malita094ccde2017-12-30 12:27:00 -0500152 }
153 return true;
154}
155
Florin Malita54f65c42018-01-16 17:04:30 -0500156bool SkottieSlide::onChar(SkUnichar c) {
Florin Malita094ccde2017-12-30 12:27:00 -0500157 switch (c) {
158 case 'I':
Florin Malita6eb85a12018-04-30 10:32:18 -0400159 fShowAnimationStats = !fShowAnimationStats;
Florin Malita094ccde2017-12-30 12:27:00 -0500160 break;
161 default:
162 break;
163 }
164
165 return INHERITED::onChar(c);
166}
Florin Malita60d3bfc2018-02-20 16:49:20 -0500167
168bool SkottieSlide::onMouse(SkScalar x, SkScalar y, sk_app::Window::InputState state, uint32_t) {
169 switch (state) {
170 case sk_app::Window::kUp_InputState:
171 fShowAnimationInval = !fShowAnimationInval;
Florin Malita6eb85a12018-04-30 10:32:18 -0400172 fShowAnimationStats = !fShowAnimationStats;
Florin Malita60d3bfc2018-02-20 16:49:20 -0500173 fAnimation->setShowInval(fShowAnimationInval);
174 break;
175 default:
176 break;
177 }
178
Florin Malita83286a02018-02-21 13:03:41 -0500179 return false;
Florin Malita60d3bfc2018-02-20 16:49:20 -0500180}
Florin Malita3d856bd2018-05-26 09:49:28 -0400181
182#endif // SK_ENABLE_SKOTTIE