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" |
| 17 | #include "tools/timer/AnimTimer.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 { |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 26 | public: |
| 27 | SampleAnimatedImage() |
| 28 | : INHERITED() |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 29 | , fYOffset(0) |
| 30 | {} |
| 31 | |
| 32 | protected: |
| 33 | void onDrawBackground(SkCanvas* canvas) override { |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 34 | SkFont font; |
| 35 | font.setSize(20); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 36 | |
| 37 | SkString str = SkStringPrintf("Press '%c' to start/pause; '%c' to reset.", |
| 38 | kPauseKey, kResetKey); |
| 39 | const char* text = str.c_str(); |
| 40 | SkRect bounds; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 41 | font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 42 | fYOffset = bounds.height(); |
| 43 | |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 44 | canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 5, fYOffset, font, SkPaint()); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 45 | fYOffset *= 2; |
| 46 | } |
| 47 | |
| 48 | void onDrawContent(SkCanvas* canvas) override { |
| 49 | if (!fImage) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | canvas->translate(0, fYOffset); |
| 54 | |
| 55 | canvas->drawDrawable(fImage.get()); |
| 56 | canvas->drawDrawable(fDrawable.get(), fImage->getBounds().width(), 0); |
| 57 | } |
| 58 | |
Mike Klein | cd5104e | 2019-03-20 11:55:08 -0500 | [diff] [blame] | 59 | bool onAnimate(const AnimTimer& animTimer) override { |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 60 | if (!fImage) { |
| 61 | return false; |
| 62 | } |
| 63 | |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 64 | const double lastWallTime = fLastWallTime; |
| 65 | fLastWallTime = animTimer.msec(); |
| 66 | |
| 67 | if (fRunning) { |
| 68 | fCurrentTime += fLastWallTime - lastWallTime; |
| 69 | if (fCurrentTime > fTimeToShowNextFrame) { |
| 70 | fTimeToShowNextFrame += fImage->decodeNextFrame(); |
| 71 | if (fImage->isFinished()) { |
| 72 | fRunning = false; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 77 | return true; |
| 78 | } |
| 79 | |
| 80 | void onOnceBeforeDraw() override { |
| 81 | sk_sp<SkData> file(GetResourceAsData("images/alphabetAnim.gif")); |
| 82 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(file)); |
| 83 | if (!codec) { |
| 84 | return; |
| 85 | } |
| 86 | |
Leon Scroggins III | 42ee284 | 2018-01-14 14:46:51 -0500 | [diff] [blame] | 87 | fImage = SkAnimatedImage::Make(SkAndroidCodec::MakeFromCodec(std::move(codec))); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 88 | if (!fImage) { |
| 89 | return; |
| 90 | } |
| 91 | |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 92 | fTimeToShowNextFrame = fImage->currentFrameDuration(); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 93 | SkPictureRecorder recorder; |
| 94 | auto canvas = recorder.beginRecording(fImage->getBounds()); |
| 95 | canvas->drawDrawable(fImage.get()); |
| 96 | fDrawable = recorder.finishRecordingAsDrawable(); |
| 97 | } |
| 98 | |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 99 | SkString name() override { return SkString("AnimatedImage"); } |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 100 | |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 101 | bool onQuery(Sample::Event* evt) override { |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 102 | SkUnichar uni; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 103 | if (fImage && Sample::CharQ(*evt, &uni)) { |
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; |
| 107 | if (fImage->isFinished()) { |
| 108 | // fall through |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 109 | } else { |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 110 | return true; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 111 | } |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 112 | case kResetKey: |
| 113 | fImage->reset(); |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 114 | fCurrentTime = fLastWallTime; |
| 115 | fTimeToShowNextFrame = fCurrentTime + fImage->currentFrameDuration(); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 116 | return true; |
| 117 | default: |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | return this->INHERITED::onQuery(evt); |
| 122 | } |
| 123 | |
| 124 | private: |
| 125 | sk_sp<SkAnimatedImage> fImage; |
| 126 | sk_sp<SkDrawable> fDrawable; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 127 | SkScalar fYOffset; |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 128 | bool fRunning = false; |
| 129 | double fCurrentTime = 0.0; |
| 130 | double fLastWallTime = 0.0; |
| 131 | double fTimeToShowNextFrame = 0.0; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 132 | typedef Sample INHERITED; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | /////////////////////////////////////////////////////////////////////////////// |
| 136 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 137 | DEF_SAMPLE( return new SampleAnimatedImage(); ) |