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 "sk_tool_utils.h"
10 #include "SkAnimTimer.h"
11 #include "SkCanvas.h"
12 #include "SkCodec.h"
13 #include "SkColor.h"
14 #include "SkCommandLineFlags.h"
15 #include "SkFont.h"
16 #include "SkPaint.h"
17 #include "SkString.h"
18 #include "Resources.h"
19
20 #include <vector>
21
22 DEFINE_string(animatedGif, "images/test640x479.gif", "Animated gif in resources folder");
23
24 namespace {
error(SkCanvas * canvas,const SkString & errorText)25 void error(SkCanvas* canvas, const SkString& errorText) {
26 constexpr SkScalar kOffset = 5.0f;
27 canvas->drawColor(SK_ColorRED);
28 SkPaint paint;
29 SkFont font;
30 SkRect bounds;
31 font.measureText(errorText.c_str(), errorText.size(), kUTF8_SkTextEncoding, &bounds);
32 canvas->drawString(errorText, kOffset, bounds.height() + kOffset, font, paint);
33 }
34 }
35
36 class AnimatedGifGM : public skiagm::GM {
37 private:
38 std::unique_ptr<SkCodec> fCodec;
39 int fFrame;
40 double fNextUpdate;
41 int fTotalFrames;
42 std::vector<SkCodec::FrameInfo> fFrameInfos;
43 std::vector<SkBitmap> fFrames;
44
drawFrame(SkCanvas * canvas,int frameIndex)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;
57 const int requiredFrame = fFrameInfos[frameIndex].fRequiredFrame;
58 if (requiredFrame != SkCodec::kNoFrame) {
59 SkASSERT(requiredFrame >= 0
60 && static_cast<size_t>(requiredFrame) < fFrames.size());
61 SkBitmap& requiredBitmap = fFrames[requiredFrame];
62 // For simplicity, do not try to cache old frames
63 if (requiredBitmap.getPixels() &&
64 sk_tool_utils::copy_to(&bm, requiredBitmap.colorType(), requiredBitmap)) {
65 opts.fPriorFrame = requiredFrame;
66 }
67 }
68
69 if (SkCodec::kSuccess != fCodec->getPixels(info, bm.getPixels(),
70 bm.rowBytes(), &opts)) {
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:
AnimatedGifGM()80 AnimatedGifGM()
81 : fFrame(0)
82 , fNextUpdate (-1)
83 , fTotalFrames (-1) {}
84
85 private:
onShortName()86 SkString onShortName() override {
87 return SkString("animatedGif");
88 }
89
onISize()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
initCodec()102 bool initCodec() {
103 if (fCodec) {
104 return true;
105 }
106 if (FLAGS_animatedGif.isEmpty()) {
107 SkDebugf("Nothing specified for --animatedGif!");
108 return false;
109 }
110
111 std::unique_ptr<SkStream> stream(GetResourceAsStream(FLAGS_animatedGif[0]));
112 if (!stream) {
113 return false;
114 }
115
116 fCodec = SkCodec::MakeFromStream(std::move(stream));
117 if (!fCodec) {
118 SkDebugf("Could create codec from %s", FLAGS_animatedGif[0]);
119 return false;
120 }
121
122 fFrame = 0;
123 fFrameInfos = fCodec->getFrameInfo();
124 fTotalFrames = fFrameInfos.size();
125 return true;
126 }
127
onDraw(SkCanvas * canvas)128 void onDraw(SkCanvas* canvas) override {
129 if (!this->initCodec()) {
130 SkString errorText = SkStringPrintf("Nothing to draw; %s", FLAGS_animatedGif[0]);
131 error(canvas, errorText);
132 return;
133 }
134
135 canvas->save();
136 for (int frameIndex = 0; frameIndex < fTotalFrames; frameIndex++) {
137 this->drawFrame(canvas, frameIndex);
138 canvas->translate(SkIntToScalar(fCodec->getInfo().width()), 0);
139 }
140 canvas->restore();
141
142 SkAutoCanvasRestore acr(canvas, true);
143 canvas->translate(0, SkIntToScalar(fCodec->getInfo().height()));
144 this->drawFrame(canvas, fFrame);
145 }
146
onAnimate(const SkAnimTimer & timer)147 bool onAnimate(const SkAnimTimer& timer) override {
148 if (!fCodec || fTotalFrames == 1) {
149 return false;
150 }
151
152 double secs = timer.msec() * .1;
153 if (fNextUpdate < double(0)) {
154 // This is a sentinel that we have not done any updates yet.
155 // I'm assuming this gets called *after* onOnceBeforeDraw, so our first frame should
156 // already have been retrieved.
157 SkASSERT(fFrame == 0);
158 fNextUpdate = secs + fFrameInfos[fFrame].fDuration;
159
160 return true;
161 }
162
163 if (secs < fNextUpdate) {
164 return true;
165 }
166
167 while (secs >= fNextUpdate) {
168 // Retrieve the next frame.
169 fFrame++;
170 if (fFrame == fTotalFrames) {
171 fFrame = 0;
172 }
173
174 // Note that we loop here. This is not safe if we need to draw the intermediate frame
175 // in order to draw correctly.
176 fNextUpdate += fFrameInfos[fFrame].fDuration;
177 }
178
179 return true;
180 }
181 };
182 DEF_GM(return new AnimatedGifGM);
183
184
185 #include "SkAnimCodecPlayer.h"
186 #include "SkOSFile.h"
187 #include "SkMakeUnique.h"
188
load_codec(const char filename[])189 static std::unique_ptr<SkCodec> load_codec(const char filename[]) {
190 return SkCodec::MakeFromData(SkData::MakeFromFileName(filename));
191 }
192
193 class AnimCodecPlayerGM : public skiagm::GM {
194 private:
195 std::vector<std::unique_ptr<SkAnimCodecPlayer> > fPlayers;
196 uint32_t fBaseMSec = 0;
197
198 public:
AnimCodecPlayerGM()199 AnimCodecPlayerGM() {
200 const char* root = "/skia/anim/";
201 SkOSFile::Iter iter(root);
202 SkString path;
203 while (iter.next(&path)) {
204 SkString completepath;
205 completepath.printf("%s%s", root, path.c_str());
206 auto codec = load_codec(completepath.c_str());
207 if (codec) {
208 fPlayers.push_back(skstd::make_unique<SkAnimCodecPlayer>(std::move(codec)));
209 }
210 }
211 }
212
213 private:
onShortName()214 SkString onShortName() override {
215 return SkString("AnimCodecPlayer");
216 }
217
onISize()218 SkISize onISize() override {
219 return { 1024, 768 };
220 }
221
onDraw(SkCanvas * canvas)222 void onDraw(SkCanvas* canvas) override {
223 canvas->scale(0.25f, 0.25f);
224 for (auto& p : fPlayers) {
225 canvas->drawImage(p->getFrame(), 0, 0, nullptr);
226 canvas->translate(p->dimensions().width(), 0);
227 }
228 }
229
onAnimate(const SkAnimTimer & timer)230 bool onAnimate(const SkAnimTimer& timer) override {
231 if (fBaseMSec == 0) {
232 fBaseMSec = timer.msec();
233 }
234 for (auto& p : fPlayers) {
235 (void)p->seek(timer.msec() - fBaseMSec);
236 }
237 return true;
238 }
239 };
240 DEF_GM(return new AnimCodecPlayerGM);
241