blob: 80bcdd9ee2334c02e7b9894810b3512d17238b08 [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"
Matt Sarett68b8e3d2017-04-28 11:15:22 -04009#include "sk_tool_utils.h"
scroggo19b91532016-10-24 09:03:26 -070010#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
21DEFINE_string(animatedGif, "test640x479.gif", "Animated gif in resources folder");
22
23namespace {
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 Clark2a475ea2017-04-28 15:35:12 -040030 canvas->drawString(errorText, kOffset, bounds.height() + kOffset,
scroggo19b91532016-10-24 09:03:26 -070031 paint);
32 }
33}
34
35class AnimatedGifGM : public skiagm::GM {
36private:
37 std::unique_ptr<SkCodec> fCodec;
Leon Scroggins III249b8e32017-04-17 12:46:33 -040038 int fFrame;
scroggo19b91532016-10-24 09:03:26 -070039 double fNextUpdate;
Leon Scroggins III249b8e32017-04-17 12:46:33 -040040 int fTotalFrames;
scroggo19b91532016-10-24 09:03:26 -070041 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 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
Matt Sarett68b8e3d2017-04-28 11:15:22 -040062 if (requiredBitmap.getPixels() &&
63 sk_tool_utils::copy_to(&bm, requiredBitmap.colorType(), requiredBitmap)) {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040064 opts.fPriorFrame = requiredFrame;
scroggo19b91532016-10-24 09:03:26 -070065 }
66 }
67
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, FLAGS_animatedGif[0]);
72 return;
73 }
74 }
75
76 canvas->drawBitmap(bm, 0, 0);
77 }
78
79public:
80 AnimatedGifGM()
81 : fFrame(0)
82 , fNextUpdate (-1)
83 , fTotalFrames (-1) {}
84
85private:
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 {
scroggocf280a42016-10-24 09:59:53 -0700103 canvas->clear(SK_ColorWHITE);
scroggo19b91532016-10-24 09:03:26 -0700104 if (this->initCodec()) {
105 SkAutoCanvasRestore acr(canvas, true);
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400106 for (int frameIndex = 0; frameIndex < fTotalFrames; frameIndex++) {
scroggo19b91532016-10-24 09:03:26 -0700107 this->drawFrame(canvas, frameIndex);
Jim Van Verth3cfdf6c2016-10-26 09:45:23 -0400108 canvas->translate(SkIntToScalar(fCodec->getInfo().width()), 0);
scroggo19b91532016-10-24 09:03:26 -0700109 }
110 }
111 }
112
113 bool initCodec() {
114 if (fCodec) {
115 return true;
116 }
117 if (FLAGS_animatedGif.isEmpty()) {
118 SkDebugf("Nothing specified for --animatedGif!");
119 return false;
120 }
121
122 std::unique_ptr<SkStream> stream(GetResourceAsStream(FLAGS_animatedGif[0]));
123 if (!stream) {
124 return false;
125 }
126
127 fCodec.reset(SkCodec::NewFromStream(stream.release()));
128 if (!fCodec) {
129 SkDebugf("Could create codec from %s", FLAGS_animatedGif[0]);
130 return false;
131 }
132
133 fFrame = 0;
134 fFrameInfos = fCodec->getFrameInfo();
135 fTotalFrames = fFrameInfos.size();
136 return true;
137 }
138
139 void onDraw(SkCanvas* canvas) override {
140 if (!fCodec) {
141 SkString errorText = SkStringPrintf("Nothing to draw; %s", FLAGS_animatedGif[0]);
142 error(canvas, errorText);
143 return;
144 }
145
146 SkAutoCanvasRestore acr(canvas, true);
Jim Van Verth3cfdf6c2016-10-26 09:45:23 -0400147 canvas->translate(0, SkIntToScalar(fCodec->getInfo().height()));
scroggo19b91532016-10-24 09:03:26 -0700148 this->drawFrame(canvas, fFrame);
149 }
150
151 bool onAnimate(const SkAnimTimer& timer) override {
152 if (!fCodec || fTotalFrames == 1) {
153 return false;
154 }
155
156 double secs = timer.msec() * .1;
157 if (fNextUpdate < double(0)) {
158 // This is a sentinel that we have not done any updates yet.
159 // I'm assuming this gets called *after* onOnceBeforeDraw, so our first frame should
160 // already have been retrieved.
161 SkASSERT(fFrame == 0);
162 fNextUpdate = secs + fFrameInfos[fFrame].fDuration;
163
164 return true;
165 }
166
167 if (secs < fNextUpdate) {
168 return true;
169 }
170
171 while (secs >= fNextUpdate) {
172 // Retrieve the next frame.
173 fFrame++;
174 if (fFrame == fTotalFrames) {
175 fFrame = 0;
176 }
177
178 // Note that we loop here. This is not safe if we need to draw the intermediate frame
179 // in order to draw correctly.
180 fNextUpdate += fFrameInfos[fFrame].fDuration;
181 }
182
183 return true;
184 }
185};
186
187DEF_GM(return new AnimatedGifGM);
188