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