blob: c7871e0d120f353a39eee44946d00f0b0ad30da6 [file] [log] [blame]
scroggo19b91532016-10-24 09:03:26 -07001/*
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
20DEFINE_string(animatedGif, "test640x479.gif", "Animated gif in resources folder");
21
22namespace {
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.height() + kOffset,
30 paint);
31 }
32}
33
34class AnimatedGifGM : public skiagm::GM {
35private:
36 std::unique_ptr<SkCodec> fCodec;
Leon Scroggins III249b8e32017-04-17 12:46:33 -040037 int fFrame;
scroggo19b91532016-10-24 09:03:26 -070038 double fNextUpdate;
Leon Scroggins III249b8e32017-04-17 12:46:33 -040039 int fTotalFrames;
scroggo19b91532016-10-24 09:03:26 -070040 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_SkColorType);
51 bm.allocPixels(info);
52
53 SkCodec::Options opts;
54 opts.fFrameIndex = frameIndex;
55 opts.fHasPriorFrame = false;
Leon Scroggins III249b8e32017-04-17 12:46:33 -040056 const int requiredFrame = fFrameInfos[frameIndex].fRequiredFrame;
scroggo19b91532016-10-24 09:03:26 -070057 if (requiredFrame != SkCodec::kNone) {
Leon Scroggins III249b8e32017-04-17 12:46:33 -040058 SkASSERT(requiredFrame >= 0
59 && static_cast<size_t>(requiredFrame) < fFrames.size());
scroggo19b91532016-10-24 09:03:26 -070060 SkBitmap& requiredBitmap = fFrames[requiredFrame];
61 // For simplicity, do not try to cache old frames
62 if (requiredBitmap.getPixels() && requiredBitmap.copyTo(&bm)) {
63 opts.fHasPriorFrame = true;
64 }
65 }
66
67 if (SkCodec::kSuccess != fCodec->getPixels(info, bm.getPixels(),
68 bm.rowBytes(), &opts,
69 nullptr, nullptr)) {
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
78public:
79 AnimatedGifGM()
80 : fFrame(0)
81 , fNextUpdate (-1)
82 , fTotalFrames (-1) {}
83
84private:
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 {
scroggocf280a42016-10-24 09:59:53 -0700102 canvas->clear(SK_ColorWHITE);
scroggo19b91532016-10-24 09:03:26 -0700103 if (this->initCodec()) {
104 SkAutoCanvasRestore acr(canvas, true);
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400105 for (int frameIndex = 0; frameIndex < fTotalFrames; frameIndex++) {
scroggo19b91532016-10-24 09:03:26 -0700106 this->drawFrame(canvas, frameIndex);
Jim Van Verth3cfdf6c2016-10-26 09:45:23 -0400107 canvas->translate(SkIntToScalar(fCodec->getInfo().width()), 0);
scroggo19b91532016-10-24 09:03:26 -0700108 }
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) 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 Verth3cfdf6c2016-10-26 09:45:23 -0400146 canvas->translate(0, SkIntToScalar(fCodec->getInfo().height()));
scroggo19b91532016-10-24 09:03:26 -0700147 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};
185
186DEF_GM(return new AnimatedGifGM);
187