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 | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 16 | static void draw_stats_box(SkCanvas* canvas, const skottie::Animation::Stats& stats) { |
| 17 | static constexpr SkRect kR = { 10, 10, 280, 120 }; |
| 18 | static constexpr SkScalar kTextSize = 20; |
| 19 | |
| 20 | SkPaint paint; |
| 21 | paint.setAntiAlias(true); |
| 22 | paint.setColor(0xffeeeeee); |
| 23 | paint.setTextSize(kTextSize); |
| 24 | |
| 25 | canvas->drawRect(kR, paint); |
| 26 | |
| 27 | paint.setColor(SK_ColorBLACK); |
| 28 | |
| 29 | const auto json_size = SkStringPrintf("Json size: %lu bytes", |
| 30 | stats.fJsonSize); |
| 31 | canvas->drawText(json_size.c_str(), |
| 32 | json_size.size(), kR.x() + 10, kR.y() + kTextSize * 1, paint); |
| 33 | const auto animator_count = SkStringPrintf("Animator count: %lu", |
| 34 | stats.fAnimatorCount); |
| 35 | canvas->drawText(animator_count.c_str(), |
| 36 | animator_count.size(), kR.x() + 10, kR.y() + kTextSize * 2, paint); |
| 37 | const auto json_parse_time = SkStringPrintf("Json parse time: %.3f ms", |
| 38 | stats.fJsonParseTimeMS); |
| 39 | canvas->drawText(json_parse_time.c_str(), |
| 40 | json_parse_time.size(), kR.x() + 10, kR.y() + kTextSize * 3, paint); |
| 41 | const auto scene_parse_time = SkStringPrintf("Scene build time: %.3f ms", |
| 42 | stats.fSceneParseTimeMS); |
| 43 | canvas->drawText(scene_parse_time.c_str(), |
| 44 | scene_parse_time.size(), kR.x() + 10, kR.y() + kTextSize * 4, paint); |
| 45 | const auto total_load_time = SkStringPrintf("Total load time: %.3f ms", |
| 46 | stats.fTotalLoadTimeMS); |
| 47 | canvas->drawText(total_load_time.c_str(), |
| 48 | total_load_time.size(), kR.x() + 10, kR.y() + kTextSize * 5, paint); |
| 49 | |
| 50 | paint.setStyle(SkPaint::kStroke_Style); |
| 51 | canvas->drawRect(kR, paint); |
| 52 | } |
| 53 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 54 | SkottieSlide::SkottieSlide(const SkString& name, const SkString& path) |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 55 | : fPath(path) { |
| 56 | fName = name; |
| 57 | } |
| 58 | |
Florin Malita | c378fdc | 2018-02-09 11:15:32 -0500 | [diff] [blame] | 59 | void SkottieSlide::load(SkScalar w, SkScalar h) { |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 60 | fAnimation = skottie::Animation::MakeFromFile(fPath.c_str(), nullptr, &fAnimationStats); |
Florin Malita | 1022f74 | 2018-02-23 11:10:22 -0500 | [diff] [blame] | 61 | fWinSize = SkSize::Make(w, h); |
| 62 | fTimeBase = 0; // force a time reset |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 63 | |
| 64 | if (fAnimation) { |
Florin Malita | df2713c | 2018-01-09 15:51:21 -0500 | [diff] [blame] | 65 | fAnimation->setShowInval(fShowAnimationInval); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 66 | SkDebugf("loaded Bodymovin animation v: %s, size: [%f %f], fr: %f\n", |
| 67 | fAnimation->version().c_str(), |
| 68 | fAnimation->size().width(), |
| 69 | fAnimation->size().height(), |
| 70 | fAnimation->frameRate()); |
| 71 | } else { |
| 72 | SkDebugf("failed to load Bodymovin animation: %s\n", fPath.c_str()); |
| 73 | } |
| 74 | } |
| 75 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 76 | void SkottieSlide::unload() { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 77 | fAnimation.reset(); |
| 78 | } |
| 79 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 80 | SkISize SkottieSlide::getDimensions() const { |
Florin Malita | c378fdc | 2018-02-09 11:15:32 -0500 | [diff] [blame] | 81 | // We always scale to fill the window. |
| 82 | return fWinSize.toCeil(); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 83 | } |
| 84 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 85 | void SkottieSlide::draw(SkCanvas* canvas) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 86 | if (fAnimation) { |
Florin Malita | aa4dc62 | 2018-01-02 14:37:37 -0500 | [diff] [blame] | 87 | SkAutoCanvasRestore acr(canvas, true); |
Florin Malita | c378fdc | 2018-02-09 11:15:32 -0500 | [diff] [blame] | 88 | const auto dstR = SkRect::MakeSize(fWinSize); |
Mike Reed | 2985987 | 2018-01-08 08:25:27 -0500 | [diff] [blame] | 89 | fAnimation->render(canvas, &dstR); |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 90 | |
| 91 | if (fShowAnimationStats) { |
| 92 | draw_stats_box(canvas, fAnimationStats); |
| 93 | } |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 97 | bool SkottieSlide::animate(const SkAnimTimer& timer) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 98 | if (fTimeBase == 0) { |
| 99 | // Reset the animation time. |
| 100 | fTimeBase = timer.msec(); |
| 101 | } |
| 102 | |
| 103 | if (fAnimation) { |
| 104 | auto t = timer.msec() - fTimeBase; |
| 105 | fAnimation->animationTick(t); |
| 106 | } |
| 107 | return true; |
| 108 | } |
| 109 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 110 | bool SkottieSlide::onChar(SkUnichar c) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 111 | switch (c) { |
| 112 | case 'I': |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 113 | fShowAnimationStats = !fShowAnimationStats; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 114 | break; |
| 115 | default: |
| 116 | break; |
| 117 | } |
| 118 | |
| 119 | return INHERITED::onChar(c); |
| 120 | } |
Florin Malita | 60d3bfc | 2018-02-20 16:49:20 -0500 | [diff] [blame] | 121 | |
| 122 | bool SkottieSlide::onMouse(SkScalar x, SkScalar y, sk_app::Window::InputState state, uint32_t) { |
| 123 | switch (state) { |
| 124 | case sk_app::Window::kUp_InputState: |
| 125 | fShowAnimationInval = !fShowAnimationInval; |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 126 | fShowAnimationStats = !fShowAnimationStats; |
Florin Malita | 60d3bfc | 2018-02-20 16:49:20 -0500 | [diff] [blame] | 127 | fAnimation->setShowInval(fShowAnimationInval); |
| 128 | break; |
| 129 | default: |
| 130 | break; |
| 131 | } |
| 132 | |
Florin Malita | 83286a0 | 2018-02-21 13:03:41 -0500 | [diff] [blame] | 133 | return false; |
Florin Malita | 60d3bfc | 2018-02-20 16:49:20 -0500 | [diff] [blame] | 134 | } |
Florin Malita | 3d856bd | 2018-05-26 09:49:28 -0400 | [diff] [blame] | 135 | |
| 136 | #endif // SK_ENABLE_SKOTTIE |