Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "tools/viewer/SkottieSlide.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 9 | |
Florin Malita | 3d856bd | 2018-05-26 09:49:28 -0400 | [diff] [blame] | 10 | #if defined(SK_ENABLE_SKOTTIE) |
| 11 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkCanvas.h" |
| 13 | #include "include/core/SkFont.h" |
| 14 | #include "modules/skottie/include/Skottie.h" |
| 15 | #include "modules/skottie/utils/SkottieUtils.h" |
| 16 | #include "src/utils/SkOSPath.h" |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame^] | 17 | #include "tools/timer/TimeUtils.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 18 | |
Florin Malita | a33447d | 2018-05-29 13:46:54 -0400 | [diff] [blame] | 19 | #include <cmath> |
| 20 | |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 21 | static void draw_stats_box(SkCanvas* canvas, const skottie::Animation::Builder::Stats& stats) { |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 22 | static constexpr SkRect kR = { 10, 10, 280, 120 }; |
| 23 | static constexpr SkScalar kTextSize = 20; |
| 24 | |
| 25 | SkPaint paint; |
| 26 | paint.setAntiAlias(true); |
| 27 | paint.setColor(0xffeeeeee); |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 28 | |
| 29 | SkFont font(nullptr, kTextSize); |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 30 | |
| 31 | canvas->drawRect(kR, paint); |
| 32 | |
| 33 | paint.setColor(SK_ColorBLACK); |
| 34 | |
| 35 | const auto json_size = SkStringPrintf("Json size: %lu bytes", |
| 36 | stats.fJsonSize); |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 37 | canvas->drawString(json_size, kR.x() + 10, kR.y() + kTextSize * 1, font, paint); |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 38 | const auto animator_count = SkStringPrintf("Animator count: %lu", |
| 39 | stats.fAnimatorCount); |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 40 | canvas->drawString(animator_count, kR.x() + 10, kR.y() + kTextSize * 2, font, paint); |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 41 | const auto json_parse_time = SkStringPrintf("Json parse time: %.3f ms", |
| 42 | stats.fJsonParseTimeMS); |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 43 | canvas->drawString(json_parse_time, kR.x() + 10, kR.y() + kTextSize * 3, font, paint); |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 44 | const auto scene_parse_time = SkStringPrintf("Scene build time: %.3f ms", |
| 45 | stats.fSceneParseTimeMS); |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 46 | canvas->drawString(scene_parse_time, kR.x() + 10, kR.y() + kTextSize * 4, font, paint); |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 47 | const auto total_load_time = SkStringPrintf("Total load time: %.3f ms", |
| 48 | stats.fTotalLoadTimeMS); |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 49 | canvas->drawString(total_load_time, kR.x() + 10, kR.y() + kTextSize * 5, font, paint); |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 50 | |
| 51 | paint.setStyle(SkPaint::kStroke_Style); |
| 52 | canvas->drawRect(kR, paint); |
| 53 | } |
| 54 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 55 | SkottieSlide::SkottieSlide(const SkString& name, const SkString& path) |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 56 | : fPath(path) { |
| 57 | fName = name; |
| 58 | } |
| 59 | |
Florin Malita | c378fdc | 2018-02-09 11:15:32 -0500 | [diff] [blame] | 60 | void SkottieSlide::load(SkScalar w, SkScalar h) { |
Florin Malita | 57b9d40 | 2018-10-02 12:48:00 -0400 | [diff] [blame] | 61 | class Logger final : public skottie::Logger { |
| 62 | public: |
| 63 | struct LogEntry { |
| 64 | SkString fMessage, |
| 65 | fJSON; |
| 66 | }; |
| 67 | |
| 68 | void log(skottie::Logger::Level lvl, const char message[], const char json[]) override { |
| 69 | auto& log = lvl == skottie::Logger::Level::kError ? fErrors : fWarnings; |
| 70 | log.push_back({ SkString(message), json ? SkString(json) : SkString() }); |
| 71 | } |
| 72 | |
| 73 | void report() const { |
| 74 | SkDebugf("Animation loaded with %lu error%s, %lu warning%s.\n", |
| 75 | fErrors.size(), fErrors.size() == 1 ? "" : "s", |
| 76 | fWarnings.size(), fWarnings.size() == 1 ? "" : "s"); |
| 77 | |
| 78 | const auto& show = [](const LogEntry& log, const char prefix[]) { |
| 79 | SkDebugf("%s%s", prefix, log.fMessage.c_str()); |
| 80 | if (!log.fJSON.isEmpty()) |
| 81 | SkDebugf(" : %s", log.fJSON.c_str()); |
| 82 | SkDebugf("\n"); |
| 83 | }; |
| 84 | |
| 85 | for (const auto& err : fErrors) show(err, " !! "); |
| 86 | for (const auto& wrn : fWarnings) show(wrn, " ?? "); |
| 87 | } |
| 88 | |
| 89 | private: |
| 90 | std::vector<LogEntry> fErrors, |
| 91 | fWarnings; |
| 92 | }; |
| 93 | |
| 94 | auto logger = sk_make_sp<Logger>(); |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 95 | skottie::Animation::Builder builder; |
Florin Malita | 57b9d40 | 2018-10-02 12:48:00 -0400 | [diff] [blame] | 96 | |
Florin Malita | a831655 | 2018-11-09 16:19:44 -0500 | [diff] [blame] | 97 | fAnimation = builder |
| 98 | .setLogger(logger) |
| 99 | .setResourceProvider( |
| 100 | skottie_utils::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str()))) |
| 101 | .makeFromFile(fPath.c_str()); |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 102 | fAnimationStats = builder.getStats(); |
| 103 | fWinSize = SkSize::Make(w, h); |
| 104 | fTimeBase = 0; // force a time reset |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 105 | |
| 106 | if (fAnimation) { |
Florin Malita | df2713c | 2018-01-09 15:51:21 -0500 | [diff] [blame] | 107 | fAnimation->setShowInval(fShowAnimationInval); |
Florin Malita | 57b9d40 | 2018-10-02 12:48:00 -0400 | [diff] [blame] | 108 | SkDebugf("Loaded Bodymovin animation v: %s, size: [%f %f]\n", |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 109 | fAnimation->version().c_str(), |
| 110 | fAnimation->size().width(), |
Florin Malita | 911ae40 | 2018-05-31 16:45:29 -0400 | [diff] [blame] | 111 | fAnimation->size().height()); |
Florin Malita | 57b9d40 | 2018-10-02 12:48:00 -0400 | [diff] [blame] | 112 | logger->report(); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 113 | } else { |
| 114 | SkDebugf("failed to load Bodymovin animation: %s\n", fPath.c_str()); |
| 115 | } |
| 116 | } |
| 117 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 118 | void SkottieSlide::unload() { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 119 | fAnimation.reset(); |
| 120 | } |
| 121 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 122 | SkISize SkottieSlide::getDimensions() const { |
Florin Malita | c378fdc | 2018-02-09 11:15:32 -0500 | [diff] [blame] | 123 | // We always scale to fill the window. |
| 124 | return fWinSize.toCeil(); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 125 | } |
| 126 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 127 | void SkottieSlide::draw(SkCanvas* canvas) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 128 | if (fAnimation) { |
Florin Malita | aa4dc62 | 2018-01-02 14:37:37 -0500 | [diff] [blame] | 129 | SkAutoCanvasRestore acr(canvas, true); |
Florin Malita | c378fdc | 2018-02-09 11:15:32 -0500 | [diff] [blame] | 130 | const auto dstR = SkRect::MakeSize(fWinSize); |
Mike Reed | 2985987 | 2018-01-08 08:25:27 -0500 | [diff] [blame] | 131 | fAnimation->render(canvas, &dstR); |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 132 | |
| 133 | if (fShowAnimationStats) { |
| 134 | draw_stats_box(canvas, fAnimationStats); |
| 135 | } |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame^] | 139 | bool SkottieSlide::animate(double nanos) { |
| 140 | SkMSec msec = TimeUtils::NanosToMSec(nanos); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 141 | if (fTimeBase == 0) { |
| 142 | // Reset the animation time. |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame^] | 143 | fTimeBase = msec; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | if (fAnimation) { |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame^] | 147 | const auto t = msec - fTimeBase; |
Florin Malita | a33447d | 2018-05-29 13:46:54 -0400 | [diff] [blame] | 148 | const auto d = fAnimation->duration() * 1000; |
| 149 | fAnimation->seek(std::fmod(t, d) / d); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 150 | } |
| 151 | return true; |
| 152 | } |
| 153 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 154 | bool SkottieSlide::onChar(SkUnichar c) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 155 | switch (c) { |
| 156 | case 'I': |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 157 | fShowAnimationStats = !fShowAnimationStats; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 158 | break; |
| 159 | default: |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | return INHERITED::onChar(c); |
| 164 | } |
Florin Malita | 60d3bfc | 2018-02-20 16:49:20 -0500 | [diff] [blame] | 165 | |
Hal Canary | 3a85ed1 | 2019-07-08 16:07:57 -0400 | [diff] [blame] | 166 | bool SkottieSlide::onMouse(SkScalar x, SkScalar y, sk_app::Window::InputState state, ModifierKey) { |
Florin Malita | 60d3bfc | 2018-02-20 16:49:20 -0500 | [diff] [blame] | 167 | switch (state) { |
| 168 | case sk_app::Window::kUp_InputState: |
| 169 | fShowAnimationInval = !fShowAnimationInval; |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 170 | fShowAnimationStats = !fShowAnimationStats; |
Florin Malita | 60d3bfc | 2018-02-20 16:49:20 -0500 | [diff] [blame] | 171 | fAnimation->setShowInval(fShowAnimationInval); |
| 172 | break; |
| 173 | default: |
| 174 | break; |
| 175 | } |
| 176 | |
Florin Malita | 83286a0 | 2018-02-21 13:03:41 -0500 | [diff] [blame] | 177 | return false; |
Florin Malita | 60d3bfc | 2018-02-20 16:49:20 -0500 | [diff] [blame] | 178 | } |
Florin Malita | 3d856bd | 2018-05-26 09:49:28 -0400 | [diff] [blame] | 179 | |
| 180 | #endif // SK_ENABLE_SKOTTIE |