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 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 8 | #include "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 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 12 | #include "SkAnimTimer.h" |
Florin Malita | aa4dc62 | 2018-01-02 14:37:37 -0500 | [diff] [blame] | 13 | #include "SkCanvas.h" |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 14 | #include "Skottie.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 15 | |
Florin Malita | a33447d | 2018-05-29 13:46:54 -0400 | [diff] [blame^] | 16 | #include <cmath> |
| 17 | |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 18 | static void draw_stats_box(SkCanvas* canvas, const skottie::Animation::Stats& stats) { |
| 19 | 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 Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 56 | SkottieSlide::SkottieSlide(const SkString& name, const SkString& path) |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 57 | : fPath(path) { |
| 58 | fName = name; |
| 59 | } |
| 60 | |
Florin Malita | c378fdc | 2018-02-09 11:15:32 -0500 | [diff] [blame] | 61 | void SkottieSlide::load(SkScalar w, SkScalar h) { |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 62 | fAnimation = skottie::Animation::MakeFromFile(fPath.c_str(), nullptr, &fAnimationStats); |
Florin Malita | 1022f74 | 2018-02-23 11:10:22 -0500 | [diff] [blame] | 63 | fWinSize = SkSize::Make(w, h); |
| 64 | fTimeBase = 0; // force a time reset |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 65 | |
| 66 | if (fAnimation) { |
Florin Malita | df2713c | 2018-01-09 15:51:21 -0500 | [diff] [blame] | 67 | fAnimation->setShowInval(fShowAnimationInval); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 68 | SkDebugf("loaded Bodymovin animation v: %s, size: [%f %f], fr: %f\n", |
| 69 | fAnimation->version().c_str(), |
| 70 | fAnimation->size().width(), |
| 71 | fAnimation->size().height(), |
| 72 | fAnimation->frameRate()); |
| 73 | } else { |
| 74 | SkDebugf("failed to load Bodymovin animation: %s\n", fPath.c_str()); |
| 75 | } |
| 76 | } |
| 77 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 78 | void SkottieSlide::unload() { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 79 | fAnimation.reset(); |
| 80 | } |
| 81 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 82 | SkISize SkottieSlide::getDimensions() const { |
Florin Malita | c378fdc | 2018-02-09 11:15:32 -0500 | [diff] [blame] | 83 | // We always scale to fill the window. |
| 84 | return fWinSize.toCeil(); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 85 | } |
| 86 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 87 | void SkottieSlide::draw(SkCanvas* canvas) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 88 | if (fAnimation) { |
Florin Malita | aa4dc62 | 2018-01-02 14:37:37 -0500 | [diff] [blame] | 89 | SkAutoCanvasRestore acr(canvas, true); |
Florin Malita | c378fdc | 2018-02-09 11:15:32 -0500 | [diff] [blame] | 90 | const auto dstR = SkRect::MakeSize(fWinSize); |
Mike Reed | 2985987 | 2018-01-08 08:25:27 -0500 | [diff] [blame] | 91 | fAnimation->render(canvas, &dstR); |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 92 | |
| 93 | if (fShowAnimationStats) { |
| 94 | draw_stats_box(canvas, fAnimationStats); |
| 95 | } |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 99 | bool SkottieSlide::animate(const SkAnimTimer& timer) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 100 | if (fTimeBase == 0) { |
| 101 | // Reset the animation time. |
| 102 | fTimeBase = timer.msec(); |
| 103 | } |
| 104 | |
| 105 | if (fAnimation) { |
Florin Malita | a33447d | 2018-05-29 13:46:54 -0400 | [diff] [blame^] | 106 | const auto t = timer.msec() - fTimeBase; |
| 107 | const auto d = fAnimation->duration() * 1000; |
| 108 | fAnimation->seek(std::fmod(t, d) / d); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 109 | } |
| 110 | return true; |
| 111 | } |
| 112 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 113 | bool SkottieSlide::onChar(SkUnichar c) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 114 | switch (c) { |
| 115 | case 'I': |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 116 | fShowAnimationStats = !fShowAnimationStats; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 117 | break; |
| 118 | default: |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | return INHERITED::onChar(c); |
| 123 | } |
Florin Malita | 60d3bfc | 2018-02-20 16:49:20 -0500 | [diff] [blame] | 124 | |
| 125 | bool SkottieSlide::onMouse(SkScalar x, SkScalar y, sk_app::Window::InputState state, uint32_t) { |
| 126 | switch (state) { |
| 127 | case sk_app::Window::kUp_InputState: |
| 128 | fShowAnimationInval = !fShowAnimationInval; |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 129 | fShowAnimationStats = !fShowAnimationStats; |
Florin Malita | 60d3bfc | 2018-02-20 16:49:20 -0500 | [diff] [blame] | 130 | fAnimation->setShowInval(fShowAnimationInval); |
| 131 | break; |
| 132 | default: |
| 133 | break; |
| 134 | } |
| 135 | |
Florin Malita | 83286a0 | 2018-02-21 13:03:41 -0500 | [diff] [blame] | 136 | return false; |
Florin Malita | 60d3bfc | 2018-02-20 16:49:20 -0500 | [diff] [blame] | 137 | } |
Florin Malita | 3d856bd | 2018-05-26 09:49:28 -0400 | [diff] [blame] | 138 | |
| 139 | #endif // SK_ENABLE_SKOTTIE |