OLD | NEW |
(Empty) | |
| 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" |
| 9 #include "SkAnimTimer.h" |
| 10 #include "SkCanvas.h" |
| 11 #include "SkCodec.h" |
| 12 #include "SkColor.h" |
| 13 #include "SkCommandLineFlags.h" |
| 14 #include "SkPaint.h" |
| 15 #include "SkString.h" |
| 16 #include "Resources.h" |
| 17 |
| 18 #include <vector> |
| 19 |
| 20 DEFINE_string(animatedGif, "test640x479.gif", "Animated gif in resources folder"
); |
| 21 |
| 22 namespace { |
| 23 void error(SkCanvas* canvas, const SkString& errorText) { |
| 24 constexpr SkScalar kOffset = 5.0f; |
| 25 canvas->drawColor(SK_ColorRED); |
| 26 SkPaint paint; |
| 27 SkRect bounds; |
| 28 paint.measureText(errorText.c_str(), errorText.size(), &bounds); |
| 29 canvas->drawText(errorText.c_str(), errorText.size(), kOffset, bounds.he
ight() + kOffset, |
| 30 paint); |
| 31 } |
| 32 } |
| 33 |
| 34 class AnimatedGifGM : public skiagm::GM { |
| 35 private: |
| 36 std::unique_ptr<SkCodec> fCodec; |
| 37 size_t fFrame; |
| 38 double fNextUpdate; |
| 39 size_t fTotalFrames; |
| 40 std::vector<SkCodec::FrameInfo> fFrameInfos; |
| 41 std::vector<SkBitmap> fFrames; |
| 42 |
| 43 void drawFrame(SkCanvas* canvas, int frameIndex) { |
| 44 // FIXME: Create from an Image/ImageGenerator? |
| 45 if (frameIndex >= (int) fFrames.size()) { |
| 46 fFrames.resize(frameIndex + 1); |
| 47 } |
| 48 SkBitmap& bm = fFrames[frameIndex]; |
| 49 if (!bm.getPixels()) { |
| 50 const SkImageInfo info = fCodec->getInfo().makeColorType(kN32_SkColo
rType); |
| 51 bm.allocPixels(info); |
| 52 |
| 53 SkCodec::MultiFrameOptions multiOpts; |
| 54 multiOpts.fIndex = frameIndex; |
| 55 multiOpts.fHasPriorFrame = false; |
| 56 const size_t requiredFrame = fFrameInfos[frameIndex].fRequiredFrame; |
| 57 if (requiredFrame != SkCodec::kNone) { |
| 58 SkASSERT(requiredFrame < fFrames.size()); |
| 59 SkBitmap& requiredBitmap = fFrames[requiredFrame]; |
| 60 // For simplicity, do not try to cache old frames |
| 61 if (requiredBitmap.getPixels() && requiredBitmap.copyTo(&bm)) { |
| 62 multiOpts.fHasPriorFrame = true; |
| 63 } |
| 64 } |
| 65 |
| 66 SkCodec::Options opts; |
| 67 opts.fFrameOptions = &multiOpts; |
| 68 if (SkCodec::kSuccess != fCodec->getPixels(info, bm.getPixels(), |
| 69 bm.rowBytes(), &opts, |
| 70 nullptr, nullptr)) { |
| 71 SkDebugf("Could not getPixels for frame %i: %s", frameIndex, FLA
GS_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 { |
| 103 if (this->initCodec()) { |
| 104 SkAutoCanvasRestore acr(canvas, true); |
| 105 for (size_t frameIndex = 0; frameIndex < fTotalFrames; frameIndex++)
{ |
| 106 this->drawFrame(canvas, frameIndex); |
| 107 canvas->translate(fCodec->getInfo().width(), 0); |
| 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 |
| 126 fCodec.reset(SkCodec::NewFromStream(stream.release())); |
| 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) { |
| 139 if (!fCodec) { |
| 140 SkString errorText = SkStringPrintf("Nothing to draw; %s", FLAGS_ani
matedGif[0]); |
| 141 error(canvas, errorText); |
| 142 return; |
| 143 } |
| 144 |
| 145 SkAutoCanvasRestore acr(canvas, true); |
| 146 canvas->translate(0, fCodec->getInfo().height()); |
| 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 fi
rst 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 i
ntermediate frame |
| 178 // in order to draw correctly. |
| 179 fNextUpdate += fFrameInfos[fFrame].fDuration; |
| 180 } |
| 181 |
| 182 return true; |
| 183 } |
| 184 }; |
| 185 |
| 186 DEF_GM(return new AnimatedGifGM); |
| 187 |
OLD | NEW |