Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: gm/animatedGif.cpp

Issue 2045293002: Add support for multiple frames in SkCodec (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Cache all frames in the GM Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | include/codec/SkCodec.h » ('j') | include/codec/SkCodec.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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<SkBitmap> fFrames;
41
42 void drawBounds(SkCanvas* canvas) {
43 if (!fCodec) {
44 return;
45 }
46 const SkIRect bounds = fCodec->getInfo().bounds().makeOutset(1, 1);
47 SkPaint boundsPaint;
48 boundsPaint.setStyle(SkPaint::kStroke_Style);
49 canvas->drawIRect(bounds, boundsPaint);
50 }
51
52 void drawFrame(SkCanvas* canvas, int frameIndex) {
53 // FIXME: Create from an Image/ImageGenerator?
54 if (frameIndex >= (int) fFrames.size()) {
55 fFrames.resize(frameIndex + 1);
56 }
57 SkBitmap& bm = fFrames[frameIndex];
58 if (!bm.getPixels()) {
59 const SkImageInfo info = fCodec->getInfo().makeColorType(kN32_SkColo rType);
60 bm.allocPixels(info);
61
62 SkCodec::MultiFrameOptions multiOpts;
63 multiOpts.fIndex = frameIndex;
64 multiOpts.fHasPriorFrame = false;
65 const size_t requiredFrame = fCodec->getRequiredFrame(frameIndex);
66 if (requiredFrame != SkCodec::kIndependentFrame) {
67 SkASSERT(requiredFrame < fFrames.size());
68 SkBitmap& requiredBitmap = fFrames[requiredFrame];
69 // For simplicity, do not try to cache old frames
70 if (requiredBitmap.getPixels() && requiredBitmap.copyTo(&bm)) {
71 multiOpts.fHasPriorFrame = true;
72 }
73 }
74
75 SkCodec::Options opts;
76 opts.fFrameOptions = &multiOpts;
77 if (SkCodec::kSuccess != fCodec->getPixels(info, bm.getPixels(),
78 bm.rowBytes(), &opts,
79 nullptr, nullptr)) {
80 SkDebugf("Could not getPixels for frame %i: %s", frameIndex, FLA GS_animatedGif[0]);
81 return;
82 }
83 }
84
85 canvas->drawBitmap(bm, 0, 0);
86 }
87
88 public:
89 AnimatedGifGM()
90 : fFrame(0)
91 , fNextUpdate (-1)
92 , fTotalFrames (-1) {}
93
94 private:
95 SkString onShortName() override {
96 return SkString("animatedGif");
97 }
98
99 SkISize onISize() override {
100 if (this->initCodec()) {
101 SkISize dim = fCodec->getInfo().dimensions();
102 // Wide enough to display all the frames.
103 dim.fWidth *= fCodec->getFrameCount();
104 // Tall enough to show the row of frames plus an animating version.
105 dim.fHeight *= 2;
106 return dim;
107 }
108 return SkISize::Make(640, 480);
109 }
110
111 void onDrawBackground(SkCanvas* canvas) override {
112 if (this->initCodec()) {
113 SkAutoCanvasRestore acr(canvas, true);
114 // FIXME: Draw a checkerboard here
115 for (size_t frameIndex = 0; frameIndex < fTotalFrames; frameIndex++) {
116 this->drawBounds(canvas);
117 this->drawFrame(canvas, frameIndex);
118 canvas->translate(fCodec->getInfo().width(), 0);
119 }
120 }
121 }
122
123 bool initCodec() {
124 if (fCodec) {
125 return true;
126 }
127 if (FLAGS_animatedGif.isEmpty()) {
128 SkDebugf("Nothing specified for --animatedGif!");
129 return false;
130 }
131
132 std::unique_ptr<SkStream> stream(GetResourceAsStream(FLAGS_animatedGif[0 ]));
133 if (!stream) {
134 SkDebugf("Could not find %s", FLAGS_animatedGif[0]);
135 return false;
136 }
137
138 fCodec.reset(SkCodec::NewFromStream(stream.release()));
139 if (!fCodec) {
140 SkDebugf("Could create codec from %s", FLAGS_animatedGif[0]);
141 return false;
142 }
143
144 fFrame = 0;
145 fTotalFrames = fCodec->getFrameCount();
146 return true;
147 }
148
149 void onDraw(SkCanvas* canvas) {
150 if (!fCodec) {
151 SkString errorText = SkStringPrintf("Nothing to draw; %s", FLAGS_ani matedGif[0]);
152 error(canvas, errorText);
153 return;
154 }
155
156 // Now draw it animated.
157 SkAutoCanvasRestore acr(canvas, true);
158 canvas->translate(0, fCodec->getInfo().height());
159 this->drawBounds(canvas);
160 this->drawFrame(canvas, fFrame);
161 }
162
163 bool onAnimate(const SkAnimTimer& timer) override {
164 if (!fCodec || fTotalFrames == 1) {
165 return false;
166 }
167
168 double secs = timer.msec() * .1;
169 if (fNextUpdate < double(0)) {
170 // This is a sentinel that we have not done any updates yet.
171 // I'm assuming this gets called *after* onOnceBeforeDraw, so our fi rst frame should
172 // already have been retrieved.
173 SkASSERT(fFrame == 0);
174 fNextUpdate = secs + fCodec->getFrameDuration(fFrame);
175
176 return true;
177 }
178
179 if (secs < fNextUpdate) {
180 return true;
181 }
182
183 while (secs >= fNextUpdate) {
184 // Retrieve the next frame.
185 fFrame++;
186 if (fFrame == fTotalFrames) {
187 fFrame = 0;
188 }
189
190 // Note that we loop here. This is not safe if we need to draw the i ntermediate frame
191 // in order to draw correctly.
192 fNextUpdate += fCodec->getFrameDuration(fFrame);
193 }
194
195 return true;
196 }
197 };
198
199 DEF_GM(return new AnimatedGifGM);
200
OLDNEW
« no previous file with comments | « no previous file | include/codec/SkCodec.h » ('j') | include/codec/SkCodec.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698