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 DEFINE_string(animatedGif, "test640x479.gif", "Animated gif in resources folder"
); |
| 19 |
| 20 namespace { |
| 21 void error(SkCanvas* canvas, const SkString& errorText) { |
| 22 constexpr SkScalar kOffset = 5.0f; |
| 23 canvas->drawColor(SK_ColorRED); |
| 24 SkPaint paint; |
| 25 SkRect bounds; |
| 26 paint.measureText(errorText.c_str(), errorText.size(), &bounds); |
| 27 canvas->drawText(errorText.c_str(), errorText.size(), kOffset, bounds.he
ight() + kOffset, |
| 28 paint); |
| 29 } |
| 30 } |
| 31 |
| 32 class AnimatedGifGM : public skiagm::GM { |
| 33 private: |
| 34 std::unique_ptr<SkCodec> fCodec; |
| 35 size_t fFrame; |
| 36 double fNextUpdate; |
| 37 size_t fTotalFrames; |
| 38 |
| 39 void drawBounds(SkCanvas* canvas) { |
| 40 if (!fCodec) { |
| 41 return; |
| 42 } |
| 43 const SkIRect bounds = fCodec->getInfo().bounds().makeOutset(1, 1); |
| 44 SkPaint boundsPaint; |
| 45 boundsPaint.setStyle(SkPaint::kStroke_Style); |
| 46 canvas->drawIRect(bounds, boundsPaint); |
| 47 } |
| 48 |
| 49 void drawFrame(SkCanvas* canvas, int frameIndex) { |
| 50 // FIXME: Create from an Image/ImageGenerator? |
| 51 SkBitmap bm; |
| 52 const SkImageInfo info = fCodec->getInfo().makeColorType(kN32_SkColorTyp
e); |
| 53 bm.allocPixels(info); |
| 54 |
| 55 SkCodec::Options opts; |
| 56 opts.fFrameIndex = frameIndex; |
| 57 opts.fHasPriorFrame = false; |
| 58 if (SkCodec::kSuccess != fCodec->getPixels(info, bm.getPixels(), |
| 59 bm.rowBytes(), &opts, |
| 60 nullptr, nullptr)) { |
| 61 SkDebugf("Could not getPixels for frame %i: %s", frameIndex, FLAGS_a
nimatedGif[0]); |
| 62 return; |
| 63 } |
| 64 |
| 65 canvas->drawBitmap(bm, 0, 0); |
| 66 } |
| 67 |
| 68 public: |
| 69 AnimatedGifGM() |
| 70 : fFrame(0) |
| 71 , fNextUpdate (-1) |
| 72 , fTotalFrames (-1) {} |
| 73 |
| 74 private: |
| 75 SkString onShortName() override { |
| 76 return SkString("animatedGif"); |
| 77 } |
| 78 |
| 79 SkISize onISize() override { |
| 80 if (this->initCodec()) { |
| 81 SkISize dim = fCodec->getInfo().dimensions(); |
| 82 // Wide enough to display all the frames. |
| 83 dim.fWidth *= fCodec->getFrameCount(); |
| 84 // Tall enough to show the row of frames plus an animating version. |
| 85 dim.fHeight *= 2; |
| 86 return dim; |
| 87 } |
| 88 return SkISize::Make(640, 480); |
| 89 } |
| 90 |
| 91 void onDrawBackground(SkCanvas* canvas) override { |
| 92 if (this->initCodec()) { |
| 93 SkAutoCanvasRestore acr(canvas, true); |
| 94 // FIXME: Draw a checkerboard here |
| 95 for (size_t frameIndex = 0; frameIndex < fTotalFrames; frameIndex++)
{ |
| 96 this->drawBounds(canvas); |
| 97 this->drawFrame(canvas, frameIndex); |
| 98 canvas->translate(fCodec->getInfo().width(), 0); |
| 99 } |
| 100 } |
| 101 } |
| 102 |
| 103 bool initCodec() { |
| 104 if (fCodec) { |
| 105 return true; |
| 106 } |
| 107 if (FLAGS_animatedGif.isEmpty()) { |
| 108 SkDebugf("Nothing specified for --animatedGif!"); |
| 109 return false; |
| 110 } |
| 111 |
| 112 std::unique_ptr<SkStream> stream(GetResourceAsStream(FLAGS_animatedGif[0
])); |
| 113 if (!stream) { |
| 114 SkDebugf("Could not find %s", FLAGS_animatedGif[0]); |
| 115 return false; |
| 116 } |
| 117 |
| 118 fCodec.reset(SkCodec::NewFromStream(stream.release())); |
| 119 if (!fCodec) { |
| 120 SkDebugf("Could create codec from %s", FLAGS_animatedGif[0]); |
| 121 return false; |
| 122 } |
| 123 |
| 124 fFrame = 0; |
| 125 fTotalFrames = fCodec->getFrameCount(); |
| 126 return true; |
| 127 } |
| 128 |
| 129 void onDraw(SkCanvas* canvas) { |
| 130 if (!fCodec) { |
| 131 SkString errorText = SkStringPrintf("Nothing to draw; %s", FLAGS_ani
matedGif[0]); |
| 132 error(canvas, errorText); |
| 133 return; |
| 134 } |
| 135 |
| 136 // Now draw it animated. |
| 137 SkAutoCanvasRestore acr(canvas, true); |
| 138 canvas->translate(0, fCodec->getInfo().height()); |
| 139 this->drawBounds(canvas); |
| 140 this->drawFrame(canvas, fFrame); |
| 141 } |
| 142 |
| 143 bool onAnimate(const SkAnimTimer& timer) override { |
| 144 if (!fCodec || fTotalFrames == 1) { |
| 145 return false; |
| 146 } |
| 147 |
| 148 double secs = timer.msec() * .1; |
| 149 if (fNextUpdate < double(0)) { |
| 150 // This is a sentinel that we have not done any updates yet. |
| 151 // I'm assuming this gets called *after* onOnceBeforeDraw, so our fi
rst frame should |
| 152 // already have been retrieved. |
| 153 SkASSERT(fFrame == 0); |
| 154 fNextUpdate = secs + fCodec->getFrameDuration(fFrame); |
| 155 |
| 156 return true; |
| 157 } |
| 158 |
| 159 if (secs < fNextUpdate) { |
| 160 return true; |
| 161 } |
| 162 |
| 163 while (secs >= fNextUpdate) { |
| 164 // Retrieve the next frame. |
| 165 fFrame++; |
| 166 if (fFrame == fTotalFrames) { |
| 167 fFrame = 0; |
| 168 } |
| 169 |
| 170 // Note that we loop here. This is not safe if we need to draw the i
ntermediate frame |
| 171 // in order to draw correctly. |
| 172 fNextUpdate += fCodec->getFrameDuration(fFrame); |
| 173 } |
| 174 |
| 175 return true; |
| 176 } |
| 177 }; |
| 178 |
| 179 DEF_GM(return new AnimatedGifGM); |
| 180 |
OLD | NEW |