Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 "include/android/SkAnimatedImage.h" |
| 9 | #include "include/codec/SkAndroidCodec.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkFont.h" |
| 12 | #include "include/core/SkPaint.h" |
| 13 | #include "include/core/SkPictureRecorder.h" |
| 14 | #include "include/core/SkRect.h" |
| 15 | #include "include/core/SkScalar.h" |
| 16 | #include "include/core/SkString.h" |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 17 | #include "tools/timer/TimeUtils.h" |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 18 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "samplecode/Sample.h" |
| 20 | #include "tools/Resources.h" |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 21 | |
| 22 | static constexpr char kPauseKey = 'p'; |
| 23 | static constexpr char kResetKey = 'r'; |
| 24 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 25 | class SampleAnimatedImage : public Sample { |
Hal Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 26 | sk_sp<SkAnimatedImage> fImage; |
| 27 | sk_sp<SkDrawable> fDrawable; |
| 28 | SkScalar fYOffset = 0; |
| 29 | bool fRunning = false; |
| 30 | double fCurrentTime = 0.0; |
| 31 | double fLastWallTime = 0.0; |
| 32 | double fTimeToShowNextFrame = 0.0; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 33 | |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 34 | void onDrawBackground(SkCanvas* canvas) override { |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 35 | SkFont font; |
| 36 | font.setSize(20); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 37 | |
| 38 | SkString str = SkStringPrintf("Press '%c' to start/pause; '%c' to reset.", |
| 39 | kPauseKey, kResetKey); |
| 40 | const char* text = str.c_str(); |
| 41 | SkRect bounds; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 42 | font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 43 | fYOffset = bounds.height(); |
| 44 | |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 45 | canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 5, fYOffset, font, SkPaint()); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 46 | fYOffset *= 2; |
| 47 | } |
| 48 | |
| 49 | void onDrawContent(SkCanvas* canvas) override { |
| 50 | if (!fImage) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | canvas->translate(0, fYOffset); |
| 55 | |
| 56 | canvas->drawDrawable(fImage.get()); |
| 57 | canvas->drawDrawable(fDrawable.get(), fImage->getBounds().width(), 0); |
| 58 | } |
| 59 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 60 | bool onAnimate(double nanos) override { |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 61 | if (!fImage) { |
| 62 | return false; |
| 63 | } |
| 64 | |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 65 | const double lastWallTime = fLastWallTime; |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 66 | fLastWallTime = TimeUtils::NanosToMSec(nanos); |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 67 | |
| 68 | if (fRunning) { |
| 69 | fCurrentTime += fLastWallTime - lastWallTime; |
| 70 | if (fCurrentTime > fTimeToShowNextFrame) { |
| 71 | fTimeToShowNextFrame += fImage->decodeNextFrame(); |
| 72 | if (fImage->isFinished()) { |
| 73 | fRunning = false; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 78 | return true; |
| 79 | } |
| 80 | |
| 81 | void onOnceBeforeDraw() override { |
| 82 | sk_sp<SkData> file(GetResourceAsData("images/alphabetAnim.gif")); |
| 83 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(file)); |
| 84 | if (!codec) { |
| 85 | return; |
| 86 | } |
| 87 | |
Leon Scroggins III | 42ee284 | 2018-01-14 14:46:51 -0500 | [diff] [blame] | 88 | fImage = SkAnimatedImage::Make(SkAndroidCodec::MakeFromCodec(std::move(codec))); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 89 | if (!fImage) { |
| 90 | return; |
| 91 | } |
| 92 | |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 93 | fTimeToShowNextFrame = fImage->currentFrameDuration(); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 94 | SkPictureRecorder recorder; |
| 95 | auto canvas = recorder.beginRecording(fImage->getBounds()); |
| 96 | canvas->drawDrawable(fImage.get()); |
| 97 | fDrawable = recorder.finishRecordingAsDrawable(); |
| 98 | } |
| 99 | |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 100 | SkString name() override { return SkString("AnimatedImage"); } |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 101 | |
Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 102 | bool onChar(SkUnichar uni) override { |
| 103 | if (fImage) { |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 104 | switch (uni) { |
| 105 | case kPauseKey: |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 106 | fRunning = !fRunning; |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 107 | if (!fImage->isFinished()) { |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 108 | return true; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 109 | } |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 110 | [[fallthrough]]; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 111 | case kResetKey: |
| 112 | fImage->reset(); |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 113 | fCurrentTime = fLastWallTime; |
| 114 | fTimeToShowNextFrame = fCurrentTime + fImage->currentFrameDuration(); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 115 | return true; |
| 116 | default: |
| 117 | break; |
| 118 | } |
| 119 | } |
Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 120 | return false; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 121 | } |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 122 | }; |
| 123 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 124 | DEF_SAMPLE( return new SampleAnimatedImage(); ) |