blob: 51b031107fd7b8fc8c8aa810ab793ae0c58cf74a [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 Malita40c37422018-08-22 20:37:04 -040062 skottie::Animation::Builder builder;
63 fAnimation = builder.makeFromFile(fPath.c_str());
64 fAnimationStats = builder.getStats();
65 fWinSize = SkSize::Make(w, h);
66 fTimeBase = 0; // force a time reset
Florin Malita094ccde2017-12-30 12:27:00 -050067
68 if (fAnimation) {
Florin Malitadf2713c2018-01-09 15:51:21 -050069 fAnimation->setShowInval(fShowAnimationInval);
Florin Malita911ae402018-05-31 16:45:29 -040070 SkDebugf("loaded Bodymovin animation v: %s, size: [%f %f]\n",
Florin Malita094ccde2017-12-30 12:27:00 -050071 fAnimation->version().c_str(),
72 fAnimation->size().width(),
Florin Malita911ae402018-05-31 16:45:29 -040073 fAnimation->size().height());
Florin Malita094ccde2017-12-30 12:27:00 -050074 } else {
75 SkDebugf("failed to load Bodymovin animation: %s\n", fPath.c_str());
76 }
77}
78
Florin Malita54f65c42018-01-16 17:04:30 -050079void SkottieSlide::unload() {
Florin Malita094ccde2017-12-30 12:27:00 -050080 fAnimation.reset();
81}
82
Florin Malita54f65c42018-01-16 17:04:30 -050083SkISize SkottieSlide::getDimensions() const {
Florin Malitac378fdc2018-02-09 11:15:32 -050084 // We always scale to fill the window.
85 return fWinSize.toCeil();
Florin Malita094ccde2017-12-30 12:27:00 -050086}
87
Florin Malita54f65c42018-01-16 17:04:30 -050088void SkottieSlide::draw(SkCanvas* canvas) {
Florin Malita094ccde2017-12-30 12:27:00 -050089 if (fAnimation) {
Florin Malitaaa4dc622018-01-02 14:37:37 -050090 SkAutoCanvasRestore acr(canvas, true);
Florin Malitac378fdc2018-02-09 11:15:32 -050091 const auto dstR = SkRect::MakeSize(fWinSize);
Mike Reed29859872018-01-08 08:25:27 -050092 fAnimation->render(canvas, &dstR);
Florin Malita6eb85a12018-04-30 10:32:18 -040093
94 if (fShowAnimationStats) {
95 draw_stats_box(canvas, fAnimationStats);
96 }
Florin Malita094ccde2017-12-30 12:27:00 -050097 }
98}
99
Florin Malita54f65c42018-01-16 17:04:30 -0500100bool SkottieSlide::animate(const SkAnimTimer& timer) {
Florin Malita094ccde2017-12-30 12:27:00 -0500101 if (fTimeBase == 0) {
102 // Reset the animation time.
103 fTimeBase = timer.msec();
104 }
105
106 if (fAnimation) {
Florin Malitaa33447d2018-05-29 13:46:54 -0400107 const auto t = timer.msec() - fTimeBase;
108 const auto d = fAnimation->duration() * 1000;
109 fAnimation->seek(std::fmod(t, d) / d);
Florin Malita094ccde2017-12-30 12:27:00 -0500110 }
111 return true;
112}
113
Florin Malita54f65c42018-01-16 17:04:30 -0500114bool SkottieSlide::onChar(SkUnichar c) {
Florin Malita094ccde2017-12-30 12:27:00 -0500115 switch (c) {
116 case 'I':
Florin Malita6eb85a12018-04-30 10:32:18 -0400117 fShowAnimationStats = !fShowAnimationStats;
Florin Malita094ccde2017-12-30 12:27:00 -0500118 break;
119 default:
120 break;
121 }
122
123 return INHERITED::onChar(c);
124}
Florin Malita60d3bfc2018-02-20 16:49:20 -0500125
126bool SkottieSlide::onMouse(SkScalar x, SkScalar y, sk_app::Window::InputState state, uint32_t) {
127 switch (state) {
128 case sk_app::Window::kUp_InputState:
129 fShowAnimationInval = !fShowAnimationInval;
Florin Malita6eb85a12018-04-30 10:32:18 -0400130 fShowAnimationStats = !fShowAnimationStats;
Florin Malita60d3bfc2018-02-20 16:49:20 -0500131 fAnimation->setShowInval(fShowAnimationInval);
132 break;
133 default:
134 break;
135 }
136
Florin Malita83286a02018-02-21 13:03:41 -0500137 return false;
Florin Malita60d3bfc2018-02-20 16:49:20 -0500138}
Florin Malita3d856bd2018-05-26 09:49:28 -0400139
140#endif // SK_ENABLE_SKOTTIE