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