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