scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 8 | #include "gm.h" |
Matt Sarett | 68b8e3d | 2017-04-28 11:15:22 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 10 | #include "SkAnimTimer.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkCodec.h" |
| 13 | #include "SkColor.h" |
| 14 | #include "SkCommandLineFlags.h" |
Mike Reed | 17c574a | 2018-12-12 18:10:38 -0500 | [diff] [blame] | 15 | #include "SkFont.h" |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 16 | #include "SkPaint.h" |
| 17 | #include "SkString.h" |
| 18 | #include "Resources.h" |
| 19 | |
| 20 | #include <vector> |
| 21 | |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 22 | DEFINE_string(animatedGif, "images/test640x479.gif", "Animated gif in resources folder"); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 23 | |
| 24 | namespace { |
| 25 | void error(SkCanvas* canvas, const SkString& errorText) { |
| 26 | constexpr SkScalar kOffset = 5.0f; |
| 27 | canvas->drawColor(SK_ColorRED); |
| 28 | SkPaint paint; |
Mike Reed | 17c574a | 2018-12-12 18:10:38 -0500 | [diff] [blame] | 29 | SkFont font; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 30 | SkRect bounds; |
Mike Reed | 17c574a | 2018-12-12 18:10:38 -0500 | [diff] [blame] | 31 | font.measureText(errorText.c_str(), errorText.size(), kUTF8_SkTextEncoding, &bounds); |
Hal Canary | 89a644b | 2019-01-07 09:36:09 -0500 | [diff] [blame] | 32 | canvas->drawString(errorText, kOffset, bounds.height() + kOffset, font, paint); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | |
| 36 | class AnimatedGifGM : public skiagm::GM { |
| 37 | private: |
| 38 | std::unique_ptr<SkCodec> fCodec; |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 39 | int fFrame; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 40 | double fNextUpdate; |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 41 | int fTotalFrames; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 42 | std::vector<SkCodec::FrameInfo> fFrameInfos; |
| 43 | std::vector<SkBitmap> fFrames; |
| 44 | |
| 45 | void drawFrame(SkCanvas* canvas, int frameIndex) { |
| 46 | // FIXME: Create from an Image/ImageGenerator? |
| 47 | if (frameIndex >= (int) fFrames.size()) { |
| 48 | fFrames.resize(frameIndex + 1); |
| 49 | } |
| 50 | SkBitmap& bm = fFrames[frameIndex]; |
| 51 | if (!bm.getPixels()) { |
| 52 | const SkImageInfo info = fCodec->getInfo().makeColorType(kN32_SkColorType); |
| 53 | bm.allocPixels(info); |
| 54 | |
| 55 | SkCodec::Options opts; |
| 56 | opts.fFrameIndex = frameIndex; |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 57 | const int requiredFrame = fFrameInfos[frameIndex].fRequiredFrame; |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 58 | if (requiredFrame != SkCodec::kNoFrame) { |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 59 | SkASSERT(requiredFrame >= 0 |
| 60 | && static_cast<size_t>(requiredFrame) < fFrames.size()); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 61 | SkBitmap& requiredBitmap = fFrames[requiredFrame]; |
| 62 | // For simplicity, do not try to cache old frames |
Matt Sarett | 68b8e3d | 2017-04-28 11:15:22 -0400 | [diff] [blame] | 63 | if (requiredBitmap.getPixels() && |
| 64 | sk_tool_utils::copy_to(&bm, requiredBitmap.colorType(), requiredBitmap)) { |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 65 | opts.fPriorFrame = requiredFrame; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | if (SkCodec::kSuccess != fCodec->getPixels(info, bm.getPixels(), |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 70 | bm.rowBytes(), &opts)) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 71 | SkDebugf("Could not getPixels for frame %i: %s", frameIndex, FLAGS_animatedGif[0]); |
| 72 | return; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | canvas->drawBitmap(bm, 0, 0); |
| 77 | } |
| 78 | |
| 79 | public: |
| 80 | AnimatedGifGM() |
| 81 | : fFrame(0) |
| 82 | , fNextUpdate (-1) |
| 83 | , fTotalFrames (-1) {} |
| 84 | |
| 85 | private: |
| 86 | SkString onShortName() override { |
| 87 | return SkString("animatedGif"); |
| 88 | } |
| 89 | |
| 90 | SkISize onISize() override { |
| 91 | if (this->initCodec()) { |
| 92 | SkISize dim = fCodec->getInfo().dimensions(); |
| 93 | // Wide enough to display all the frames. |
| 94 | dim.fWidth *= fTotalFrames; |
| 95 | // Tall enough to show the row of frames plus an animating version. |
| 96 | dim.fHeight *= 2; |
| 97 | return dim; |
| 98 | } |
| 99 | return SkISize::Make(640, 480); |
| 100 | } |
| 101 | |
| 102 | void onDrawBackground(SkCanvas* canvas) override { |
scroggo | cf280a4 | 2016-10-24 09:59:53 -0700 | [diff] [blame] | 103 | canvas->clear(SK_ColorWHITE); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 104 | if (this->initCodec()) { |
| 105 | SkAutoCanvasRestore acr(canvas, true); |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 106 | for (int frameIndex = 0; frameIndex < fTotalFrames; frameIndex++) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 107 | this->drawFrame(canvas, frameIndex); |
Jim Van Verth | 3cfdf6c | 2016-10-26 09:45:23 -0400 | [diff] [blame] | 108 | canvas->translate(SkIntToScalar(fCodec->getInfo().width()), 0); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | bool initCodec() { |
| 114 | if (fCodec) { |
| 115 | return true; |
| 116 | } |
| 117 | if (FLAGS_animatedGif.isEmpty()) { |
| 118 | SkDebugf("Nothing specified for --animatedGif!"); |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | std::unique_ptr<SkStream> stream(GetResourceAsStream(FLAGS_animatedGif[0])); |
| 123 | if (!stream) { |
| 124 | return false; |
| 125 | } |
| 126 | |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 127 | fCodec = SkCodec::MakeFromStream(std::move(stream)); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 128 | if (!fCodec) { |
| 129 | SkDebugf("Could create codec from %s", FLAGS_animatedGif[0]); |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | fFrame = 0; |
| 134 | fFrameInfos = fCodec->getFrameInfo(); |
| 135 | fTotalFrames = fFrameInfos.size(); |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | void onDraw(SkCanvas* canvas) override { |
| 140 | if (!fCodec) { |
| 141 | SkString errorText = SkStringPrintf("Nothing to draw; %s", FLAGS_animatedGif[0]); |
| 142 | error(canvas, errorText); |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | SkAutoCanvasRestore acr(canvas, true); |
Jim Van Verth | 3cfdf6c | 2016-10-26 09:45:23 -0400 | [diff] [blame] | 147 | canvas->translate(0, SkIntToScalar(fCodec->getInfo().height())); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 148 | this->drawFrame(canvas, fFrame); |
| 149 | } |
| 150 | |
| 151 | bool onAnimate(const SkAnimTimer& timer) override { |
| 152 | if (!fCodec || fTotalFrames == 1) { |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | double secs = timer.msec() * .1; |
| 157 | if (fNextUpdate < double(0)) { |
| 158 | // This is a sentinel that we have not done any updates yet. |
| 159 | // I'm assuming this gets called *after* onOnceBeforeDraw, so our first frame should |
| 160 | // already have been retrieved. |
| 161 | SkASSERT(fFrame == 0); |
| 162 | fNextUpdate = secs + fFrameInfos[fFrame].fDuration; |
| 163 | |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | if (secs < fNextUpdate) { |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | while (secs >= fNextUpdate) { |
| 172 | // Retrieve the next frame. |
| 173 | fFrame++; |
| 174 | if (fFrame == fTotalFrames) { |
| 175 | fFrame = 0; |
| 176 | } |
| 177 | |
| 178 | // Note that we loop here. This is not safe if we need to draw the intermediate frame |
| 179 | // in order to draw correctly. |
| 180 | fNextUpdate += fFrameInfos[fFrame].fDuration; |
| 181 | } |
| 182 | |
| 183 | return true; |
| 184 | } |
| 185 | }; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 186 | DEF_GM(return new AnimatedGifGM); |
| 187 | |
Mike Reed | 964aedb | 2018-10-03 10:16:55 -0400 | [diff] [blame] | 188 | |
| 189 | #include "SkAnimCodecPlayer.h" |
| 190 | #include "SkOSFile.h" |
| 191 | #include "SkMakeUnique.h" |
| 192 | |
| 193 | static std::unique_ptr<SkCodec> load_codec(const char filename[]) { |
| 194 | return SkCodec::MakeFromData(SkData::MakeFromFileName(filename)); |
| 195 | } |
| 196 | |
| 197 | class AnimCodecPlayerGM : public skiagm::GM { |
| 198 | private: |
| 199 | std::vector<std::unique_ptr<SkAnimCodecPlayer> > fPlayers; |
| 200 | uint32_t fBaseMSec = 0; |
| 201 | |
| 202 | public: |
| 203 | AnimCodecPlayerGM() { |
| 204 | const char* root = "/skia/anim/"; |
| 205 | SkOSFile::Iter iter(root); |
| 206 | SkString path; |
| 207 | while (iter.next(&path)) { |
| 208 | SkString completepath; |
| 209 | completepath.printf("%s%s", root, path.c_str()); |
| 210 | auto codec = load_codec(completepath.c_str()); |
| 211 | if (codec) { |
| 212 | fPlayers.push_back(skstd::make_unique<SkAnimCodecPlayer>(std::move(codec))); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | private: |
| 218 | SkString onShortName() override { |
| 219 | return SkString("AnimCodecPlayer"); |
| 220 | } |
| 221 | |
| 222 | SkISize onISize() override { |
| 223 | return { 1024, 768 }; |
| 224 | } |
| 225 | |
| 226 | void onDraw(SkCanvas* canvas) override { |
| 227 | canvas->scale(0.25f, 0.25f); |
| 228 | for (auto& p : fPlayers) { |
| 229 | canvas->drawImage(p->getFrame(), 0, 0, nullptr); |
| 230 | canvas->translate(p->dimensions().width(), 0); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | bool onAnimate(const SkAnimTimer& timer) override { |
| 235 | if (fBaseMSec == 0) { |
| 236 | fBaseMSec = timer.msec(); |
| 237 | } |
| 238 | for (auto& p : fPlayers) { |
| 239 | (void)p->seek(timer.msec() - fBaseMSec); |
| 240 | } |
| 241 | return true; |
| 242 | } |
| 243 | }; |
| 244 | DEF_GM(return new AnimCodecPlayerGM); |