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 "include/codec/SkAndroidCodec.h"
9 #include "include/codec/SkCodec.h"
10 #include "include/codec/SkCodecAnimation.h"
11 #include "include/codec/SkEncodedOrigin.h"
12 #include "include/core/SkAlphaType.h"
13 #include "include/core/SkBitmap.h"
14 #include "include/core/SkColorType.h"
15 #include "include/core/SkData.h"
16 #include "include/core/SkImage.h"
17 #include "include/core/SkImageInfo.h"
18 #include "include/core/SkMatrix.h"
19 #include "include/core/SkRect.h"
20 #include "include/core/SkRefCnt.h"
21 #include "include/core/SkSize.h"
22 #include "include/core/SkString.h"
23 #include "include/core/SkTypes.h"
24 #include "include/utils/SkAnimCodecPlayer.h"
25 #include "tests/CodecPriv.h"
26 #include "tests/Test.h"
27 #include "tools/Resources.h"
28 #include "tools/ToolUtils.h"
29
30 #include <cstdint>
31 #include <cstring>
32 #include <initializer_list>
33 #include <memory>
34 #include <utility>
35 #include <vector>
36
DEF_TEST(Codec_trunc,r)37 DEF_TEST(Codec_trunc, r) {
38 sk_sp<SkData> data(GetResourceAsData("images/box.gif"));
39 if (!data) {
40 return;
41 }
42 // See also Codec_GifTruncated2 in GifTest.cpp for this magic 23.
43 //
44 // TODO: just move this getFrameInfo call to Codec_GifTruncated2?
45 SkCodec::MakeFromData(SkData::MakeSubset(data.get(), 0, 23))->getFrameInfo();
46 }
47
48 // 565 does not support alpha, but there is no reason for it not to support an
49 // animated image with a frame that has alpha but then blends onto an opaque
50 // frame making the result opaque. Test that we can decode such a frame.
DEF_TEST(Codec_565,r)51 DEF_TEST(Codec_565, r) {
52 sk_sp<SkData> data(GetResourceAsData("images/blendBG.webp"));
53 if (!data) {
54 return;
55 }
56 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(std::move(data)));
57 auto info = codec->getInfo().makeColorType(kRGB_565_SkColorType);
58 SkBitmap bm;
59 bm.allocPixels(info);
60
61 SkCodec::Options options;
62 options.fFrameIndex = 1;
63 options.fPriorFrame = SkCodec::kNoFrame;
64
65 const auto result = codec->getPixels(info, bm.getPixels(), bm.rowBytes(),
66 &options);
67 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
68 }
69
restore_previous(const SkCodec::FrameInfo & info)70 static bool restore_previous(const SkCodec::FrameInfo& info) {
71 return info.fDisposalMethod == SkCodecAnimation::DisposalMethod::kRestorePrevious;
72 }
73
74 namespace {
to_string(bool boolean)75 SkString to_string(bool boolean) { return boolean ? SkString("true") : SkString("false"); }
to_string(SkCodecAnimation::Blend blend)76 SkString to_string(SkCodecAnimation::Blend blend) {
77 switch (blend) {
78 case SkCodecAnimation::Blend::kSrcOver:
79 return SkString("kSrcOver");
80 case SkCodecAnimation::Blend::kSrc:
81 return SkString("kSrc");
82 default:
83 return SkString();
84 }
85 }
to_string(SkIRect rect)86 SkString to_string(SkIRect rect) {
87 return SkStringPrintf("{ %i, %i, %i, %i }", rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
88 }
89
90 template <typename T>
reporter_assert_equals(skiatest::Reporter * r,const char * name,int i,const char * prop,T expected,T actual)91 void reporter_assert_equals(skiatest::Reporter* r, const char* name, int i, const char* prop,
92 T expected, T actual) {
93 REPORTER_ASSERT(r, expected == actual, "%s's frame %i has wrong %s! expected:"
94 " %s\tactual: %s", name, i, prop, to_string(expected).c_str(),
95 to_string(actual).c_str());
96 }
97 } // namespace
98
DEF_TEST(Codec_frames,r)99 DEF_TEST(Codec_frames, r) {
100 constexpr int kNoFrame = SkCodec::kNoFrame;
101 constexpr SkAlphaType kOpaque = kOpaque_SkAlphaType;
102 constexpr SkAlphaType kUnpremul = kUnpremul_SkAlphaType;
103 constexpr SkCodecAnimation::DisposalMethod kKeep =
104 SkCodecAnimation::DisposalMethod::kKeep;
105 constexpr SkCodecAnimation::DisposalMethod kRestoreBG =
106 SkCodecAnimation::DisposalMethod::kRestoreBGColor;
107 constexpr SkCodecAnimation::DisposalMethod kRestorePrev =
108 SkCodecAnimation::DisposalMethod::kRestorePrevious;
109 constexpr auto kSrcOver = SkCodecAnimation::Blend::kSrcOver;
110 constexpr auto kSrc = SkCodecAnimation::Blend::kSrc;
111
112 static const struct {
113 const char* fName;
114 int fFrameCount;
115 // One less than fFramecount, since the first frame is always
116 // independent.
117 std::vector<int> fRequiredFrames;
118 // Same, since the first frame should match getInfo
119 std::vector<SkAlphaType> fAlphas;
120 // The size of this one should match fFrameCount for animated, empty
121 // otherwise.
122 std::vector<int> fDurations;
123 int fRepetitionCount;
124 std::vector<SkCodecAnimation::DisposalMethod> fDisposalMethods;
125 std::vector<bool> fAlphaWithinBounds;
126 std::vector<SkCodecAnimation::Blend> fBlends;
127 std::vector<SkIRect> fFrameRects;
128 } gRecs[] = {
129 { "images/required.gif", 7,
130 { 0, 1, 2, 3, 4, 5 },
131 { kOpaque, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
132 { 100, 100, 100, 100, 100, 100, 100 },
133 0,
134 { kKeep, kRestoreBG, kKeep, kKeep, kKeep, kRestoreBG, kKeep },
135 { false, true, true, true, true, true, true },
136 { kSrcOver, kSrcOver, kSrcOver, kSrcOver, kSrcOver, kSrcOver,
137 kSrcOver },
138 { {0, 0, 100, 100}, {0, 0, 75, 75}, {0, 0, 50, 50}, {0, 0, 60, 60},
139 {0, 0, 100, 100}, {0, 0, 50, 50}, {0, 0, 75, 75}},
140 },
141 { "images/alphabetAnim.gif", 13,
142 { kNoFrame, 0, 0, 0, 0, 5, 6, kNoFrame, kNoFrame, 9, 10, 11 },
143 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
144 kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
145 { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 },
146 0,
147 { kKeep, kRestorePrev, kRestorePrev, kRestorePrev, kRestorePrev,
148 kRestoreBG, kKeep, kRestoreBG, kRestoreBG, kKeep, kKeep,
149 kRestoreBG, kKeep },
150 { true, false, true, false, true, true, true, true, true, true, true, true, true },
151 { kSrcOver, kSrcOver, kSrcOver, kSrcOver, kSrcOver, kSrcOver,
152 kSrcOver, kSrcOver, kSrcOver, kSrcOver, kSrcOver, kSrcOver,
153 kSrcOver },
154 { {25, 25, 75, 75}, {25, 25, 75, 75}, {25, 25, 75, 75}, {37, 37, 62, 62},
155 {37, 37, 62, 62}, {25, 25, 75, 75}, {0, 0, 50, 50}, {0, 0, 100, 100},
156 {25, 25, 75, 75}, {25, 25, 75, 75}, {0, 0, 100, 100}, {25, 25, 75, 75},
157 {37, 37, 62, 62}},
158 },
159 { "images/randPixelsAnim2.gif", 4,
160 // required frames
161 { 0, 0, 1 },
162 // alphas
163 { kOpaque, kOpaque, kOpaque },
164 // durations
165 { 0, 1000, 170, 40 },
166 // repetition count
167 0,
168 { kKeep, kKeep, kRestorePrev, kKeep },
169 { false, true, false, false },
170 { kSrcOver, kSrcOver, kSrcOver, kSrcOver },
171 { {0, 0, 8, 8}, {6, 6, 8, 8}, {4, 4, 8, 8}, {7, 0, 8, 8} },
172 },
173 { "images/randPixelsAnim.gif", 13,
174 // required frames
175 { 0, 1, 2, 3, 4, 3, 6, 7, 7, 7, 9, 9 },
176 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
177 kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
178 // durations
179 { 0, 1000, 170, 40, 220, 7770, 90, 90, 90, 90, 90, 90, 90 },
180 // repetition count
181 0,
182 { kKeep, kKeep, kKeep, kKeep, kRestoreBG, kRestoreBG, kRestoreBG,
183 kRestoreBG, kRestorePrev, kRestoreBG, kRestorePrev, kRestorePrev,
184 kRestorePrev, },
185 { false, true, true, false, true, true, false, false, true, true, false, false,
186 true },
187 { kSrcOver, kSrcOver, kSrcOver, kSrcOver, kSrcOver, kSrcOver,
188 kSrcOver, kSrcOver, kSrcOver, kSrcOver, kSrcOver, kSrcOver,
189 kSrcOver },
190 { {4, 4, 12, 12}, {4, 4, 12, 12}, {4, 4, 12, 12}, {0, 0, 8, 8}, {8, 8, 16, 16},
191 {8, 8, 16, 16}, {8, 8, 16, 16}, {2, 2, 10, 10}, {7, 7, 15, 15}, {7, 7, 15, 15},
192 {7, 7, 15, 15}, {0, 0, 8, 8}, {14, 14, 16, 16} },
193 },
194 { "images/box.gif", 1, {}, {}, {}, SkCodec::kRepetitionCountInfinite, { kKeep }, {}, {}, {} },
195 { "images/color_wheel.gif", 1, {}, {}, {}, SkCodec::kRepetitionCountInfinite, { kKeep }, {}, {}, {} },
196 { "images/test640x479.gif", 4, { 0, 1, 2 },
197 { kOpaque, kOpaque, kOpaque },
198 { 200, 200, 200, 200 },
199 SkCodec::kRepetitionCountInfinite,
200 { kKeep, kKeep, kKeep, kKeep },
201 { false, true, true, true },
202 { kSrcOver, kSrcOver, kSrcOver, kSrcOver },
203 { {0, 0, 640, 479}, {0, 0, 640, 479}, {0, 0, 640, 479}, {0, 0, 640, 479} },
204 },
205 { "images/colorTables.gif", 2, { 0 }, { kOpaque }, { 1000, 1000 }, 5,
206 { kKeep, kKeep }, {false, true}, { kSrcOver, kSrcOver },
207 { {0, 0, 640, 400}, {0, 0, 640, 200}},
208 },
209
210 { "images/arrow.png", 1, {}, {}, {}, 0, {}, {}, {}, {} },
211 { "images/google_chrome.ico", 1, {}, {}, {}, 0, {}, {}, {}, {} },
212 { "images/brickwork-texture.jpg", 1, {}, {}, {}, 0, {}, {}, {}, {} },
213 #if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
214 { "images/dng_with_preview.dng", 1, {}, {}, {}, 0, {}, {}, {}, {} },
215 #endif
216 { "images/mandrill.wbmp", 1, {}, {}, {}, 0, {}, {}, {}, {} },
217 { "images/randPixels.bmp", 1, {}, {}, {}, 0, {}, {}, {}, {} },
218 { "images/yellow_rose.webp", 1, {}, {}, {}, 0, {}, {}, {}, {} },
219 { "images/stoplight.webp", 3, { 0, 1 }, { kOpaque, kOpaque },
220 { 1000, 500, 1000 }, SkCodec::kRepetitionCountInfinite,
221 { kKeep, kKeep, kKeep }, {false, false, false},
222 {kSrcOver, kSrcOver, kSrcOver},
223 { {0, 0, 11, 29}, {2, 10, 9, 27}, {2, 2, 9, 18}},
224 },
225 { "images/blendBG.webp", 7,
226 { 0, kNoFrame, kNoFrame, kNoFrame, 4, 4 },
227 { kOpaque, kOpaque, kUnpremul, kOpaque, kUnpremul, kUnpremul },
228 { 525, 500, 525, 437, 609, 729, 444 },
229 6,
230 { kKeep, kKeep, kKeep, kKeep, kKeep, kKeep, kKeep },
231 { false, true, false, true, false, true, true },
232 { kSrc, kSrcOver, kSrc, kSrc, kSrc, kSrc, kSrc },
233 { {0, 0, 200, 200}, {0, 0, 200, 200}, {0, 0, 200, 200}, {0, 0, 200, 200},
234 {0, 0, 200, 200}, {100, 100, 200, 200}, {100, 100, 200, 200} },
235 },
236 { "images/required.webp", 7,
237 { 0, 1, 1, kNoFrame, 4, 4 },
238 { kOpaque, kUnpremul, kUnpremul, kOpaque, kOpaque, kOpaque },
239 { 100, 100, 100, 100, 100, 100, 100 },
240 0,
241 { kKeep, kRestoreBG, kKeep, kKeep, kKeep, kRestoreBG, kKeep },
242 { false, false, false, false, false, false, false },
243 { kSrc, kSrcOver, kSrcOver, kSrcOver, kSrc, kSrcOver,
244 kSrcOver },
245 { {0, 0, 100, 100}, {0, 0, 75, 75}, {0, 0, 50, 50}, {0, 0, 60, 60},
246 {0, 0, 100, 100}, {0, 0, 50, 50}, {0, 0, 75, 75}},
247 },
248 };
249
250 for (const auto& rec : gRecs) {
251 sk_sp<SkData> data(GetResourceAsData(rec.fName));
252 if (!data) {
253 // Useful error statement, but sometimes people run tests without
254 // resources, and they do not want to see these messages.
255 //ERRORF(r, "Missing resources? Could not find '%s'", rec.fName);
256 continue;
257 }
258
259 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data));
260 if (!codec) {
261 ERRORF(r, "Failed to create an SkCodec from '%s'", rec.fName);
262 continue;
263 }
264
265 {
266 SkCodec::FrameInfo frameInfo;
267 REPORTER_ASSERT(r, !codec->getFrameInfo(0, &frameInfo));
268 }
269
270 const int expected = rec.fFrameCount;
271 if (rec.fRequiredFrames.size() + 1 != static_cast<size_t>(expected)) {
272 ERRORF(r, "'%s' has wrong number entries in fRequiredFrames; expected: %i\tactual: %zu",
273 rec.fName, expected - 1, rec.fRequiredFrames.size());
274 continue;
275 }
276
277 if (expected > 1) {
278 if (rec.fDurations.size() != static_cast<size_t>(expected)) {
279 ERRORF(r, "'%s' has wrong number entries in fDurations; expected: %i\tactual: %zu",
280 rec.fName, expected, rec.fDurations.size());
281 continue;
282 }
283
284 if (rec.fAlphas.size() + 1 != static_cast<size_t>(expected)) {
285 ERRORF(r, "'%s' has wrong number entries in fAlphas; expected: %i\tactual: %zu",
286 rec.fName, expected - 1, rec.fAlphas.size());
287 continue;
288 }
289
290 if (rec.fDisposalMethods.size() != static_cast<size_t>(expected)) {
291 ERRORF(r, "'%s' has wrong number entries in fDisposalMethods; "
292 "expected %i\tactual: %zu",
293 rec.fName, expected, rec.fDisposalMethods.size());
294 continue;
295 }
296 }
297
298 enum class TestMode {
299 kVector,
300 kIndividual,
301 };
302
303 for (auto mode : { TestMode::kVector, TestMode::kIndividual }) {
304 // Re-create the codec to reset state and test parsing.
305 codec = SkCodec::MakeFromData(data);
306
307 int frameCount;
308 std::vector<SkCodec::FrameInfo> frameInfos;
309 switch (mode) {
310 case TestMode::kVector:
311 frameInfos = codec->getFrameInfo();
312 // getFrameInfo returns empty set for non-animated.
313 frameCount = frameInfos.empty() ? 1 : frameInfos.size();
314 break;
315 case TestMode::kIndividual:
316 frameCount = codec->getFrameCount();
317 break;
318 }
319
320 if (frameCount != expected) {
321 ERRORF(r, "'%s' expected frame count: %i\tactual: %i",
322 rec.fName, expected, frameCount);
323 continue;
324 }
325
326 // Get the repetition count after the codec->getFrameInfo() or
327 // codec->getFrameCount() call above has walked to the end of the
328 // encoded image.
329 //
330 // At the file format level, GIF images can declare their
331 // repetition count multiple times and our codec goes with "last
332 // one wins". Furthermore, for single-frame (still) GIF images, a
333 // zero, positive or infinite repetition count are all equivalent
334 // in practice (in all cases, the pixels do not change over time),
335 // so the codec has some leeway in what to return for single-frame
336 // GIF images, but it cannot distinguish single-frame from
337 // multiple-frame GIFs until we count the number of frames (e.g.
338 // call getFrameInfo or getFrameCount).
339 const int repetitionCount = codec->getRepetitionCount();
340 if (repetitionCount != rec.fRepetitionCount) {
341 ERRORF(r, "%s repetition count does not match! expected: %i\tactual: %i",
342 rec.fName, rec.fRepetitionCount, repetitionCount);
343 }
344
345 // From here on, we are only concerned with animated images.
346 if (1 == frameCount) {
347 continue;
348 }
349
350 for (int i = 0; i < frameCount; i++) {
351 SkCodec::FrameInfo frameInfo;
352 switch (mode) {
353 case TestMode::kVector:
354 frameInfo = frameInfos[i];
355 break;
356 case TestMode::kIndividual:
357 REPORTER_ASSERT(r, codec->getFrameInfo(i, nullptr));
358 REPORTER_ASSERT(r, codec->getFrameInfo(i, &frameInfo));
359 break;
360 }
361
362 if (rec.fDurations[i] != frameInfo.fDuration) {
363 ERRORF(r, "%s frame %i's durations do not match! expected: %i\tactual: %i",
364 rec.fName, i, rec.fDurations[i], frameInfo.fDuration);
365 }
366
367 auto to_string = [](SkAlphaType alpha) {
368 switch (alpha) {
369 case kUnpremul_SkAlphaType:
370 return "unpremul";
371 case kOpaque_SkAlphaType:
372 return "opaque";
373 default:
374 SkASSERT(false);
375 return "unknown";
376 }
377 };
378
379 auto expectedAlpha = 0 == i ? codec->getInfo().alphaType() : rec.fAlphas[i-1];
380 auto alpha = frameInfo.fAlphaType;
381 if (expectedAlpha != alpha) {
382 ERRORF(r, "%s's frame %i has wrong alpha type! expected: %s\tactual: %s",
383 rec.fName, i, to_string(expectedAlpha), to_string(alpha));
384 }
385
386 if (0 == i) {
387 REPORTER_ASSERT(r, frameInfo.fRequiredFrame == SkCodec::kNoFrame);
388 } else if (rec.fRequiredFrames[i-1] != frameInfo.fRequiredFrame) {
389 ERRORF(r, "%s's frame %i has wrong dependency! expected: %i\tactual: %i",
390 rec.fName, i, rec.fRequiredFrames[i-1], frameInfo.fRequiredFrame);
391 }
392
393 REPORTER_ASSERT(r, frameInfo.fDisposalMethod == rec.fDisposalMethods[i]);
394
395 reporter_assert_equals<bool>(r, rec.fName, i, "alpha within bounds",
396 rec.fAlphaWithinBounds[i],
397 frameInfo.fHasAlphaWithinBounds);
398
399 reporter_assert_equals(r, rec.fName, i, "blend mode", rec.fBlends[i],
400 frameInfo.fBlend);
401
402 reporter_assert_equals(r, rec.fName, i, "frame rect", rec.fFrameRects[i],
403 frameInfo.fFrameRect);
404 }
405
406 if (TestMode::kIndividual == mode) {
407 // No need to test decoding twice.
408 continue;
409 }
410
411 // Compare decoding in multiple ways:
412 // - Start from scratch for each frame. |codec| will have to decode the required frame
413 // (and any it depends on) to decode. This is stored in |cachedFrames|.
414 // - Provide the frame that a frame depends on, so |codec| just has to blend.
415 // - Provide a frame after the required frame, which will be covered up by the newest
416 // frame.
417 // All should look the same.
418 std::vector<SkBitmap> cachedFrames(frameCount);
419 const auto info = codec->getInfo().makeColorType(kN32_SkColorType);
420
421 auto decode = [&](SkBitmap* bm, int index, int cachedIndex) {
422 auto decodeInfo = info;
423 if (index > 0) {
424 decodeInfo = info.makeAlphaType(frameInfos[index].fAlphaType);
425 }
426 bm->allocPixels(decodeInfo);
427 if (cachedIndex != SkCodec::kNoFrame) {
428 // First copy the pixels from the cached frame
429 const bool success =
430 ToolUtils::copy_to(bm, kN32_SkColorType, cachedFrames[cachedIndex]);
431 REPORTER_ASSERT(r, success);
432 }
433 SkCodec::Options opts;
434 opts.fFrameIndex = index;
435 opts.fPriorFrame = cachedIndex;
436 const auto result = codec->getPixels(decodeInfo, bm->getPixels(), bm->rowBytes(),
437 &opts);
438 if (cachedIndex != SkCodec::kNoFrame &&
439 restore_previous(frameInfos[cachedIndex])) {
440 if (result == SkCodec::kInvalidParameters) {
441 return true;
442 }
443 ERRORF(r, "Using a kRestorePrevious frame as fPriorFrame should fail");
444 return false;
445 }
446 if (result != SkCodec::kSuccess) {
447 ERRORF(r, "Failed to decode frame %i from %s when providing prior frame %i, "
448 "error %i", index, rec.fName, cachedIndex, result);
449 }
450 return result == SkCodec::kSuccess;
451 };
452
453 for (int i = 0; i < frameCount; i++) {
454 SkBitmap& cachedFrame = cachedFrames[i];
455 if (!decode(&cachedFrame, i, SkCodec::kNoFrame)) {
456 continue;
457 }
458 const auto reqFrame = frameInfos[i].fRequiredFrame;
459 if (reqFrame == SkCodec::kNoFrame) {
460 // Nothing to compare against.
461 continue;
462 }
463 for (int j = reqFrame; j < i; j++) {
464 SkBitmap frame;
465 if (restore_previous(frameInfos[j])) {
466 (void) decode(&frame, i, j);
467 continue;
468 }
469 if (!decode(&frame, i, j)) {
470 continue;
471 }
472
473 // Now verify they're equal.
474 const size_t rowLen = info.bytesPerPixel() * info.width();
475 for (int y = 0; y < info.height(); y++) {
476 const void* cachedAddr = cachedFrame.getAddr(0, y);
477 SkASSERT(cachedAddr != nullptr);
478 const void* addr = frame.getAddr(0, y);
479 SkASSERT(addr != nullptr);
480 const bool lineMatches = memcmp(cachedAddr, addr, rowLen) == 0;
481 if (!lineMatches) {
482 SkString name = SkStringPrintf("cached_%i", i);
483 write_bm(name.c_str(), cachedFrame);
484 name = SkStringPrintf("frame_%i", i);
485 write_bm(name.c_str(), frame);
486 ERRORF(r, "%s's frame %i is different (starting from line %i) when "
487 "providing prior frame %i!", rec.fName, i, y, j);
488 break;
489 }
490 }
491 }
492 }
493 }
494 }
495 }
496
497 // Verify that an image can be animated scaled down. These images have a
498 // kRestoreBG frame, so they are interesting to test. After decoding that
499 // frame, we have to erase its rectangle. The rectangle has to be adjusted
500 // based on the scaled size.
test_animated_AndroidCodec(skiatest::Reporter * r,const char * file)501 static void test_animated_AndroidCodec(skiatest::Reporter* r, const char* file) {
502 if (GetResourcePath().isEmpty()) {
503 return;
504 }
505
506 sk_sp<SkData> data(GetResourceAsData(file));
507 if (!data) {
508 ERRORF(r, "Missing %s", file);
509 return;
510 }
511
512 auto codec = SkAndroidCodec::MakeFromCodec(SkCodec::MakeFromData(std::move(data)));
513 if (!codec) {
514 ERRORF(r, "Failed to decode %s", file);
515 return;
516 }
517
518 auto info = codec->getInfo().makeAlphaType(kPremul_SkAlphaType);
519
520 for (int sampleSize : { 8, 32, 100 }) {
521 auto dimensions = codec->codec()->getScaledDimensions(1.0f / sampleSize);
522 info = info.makeDimensions(dimensions);
523 SkBitmap bm;
524 bm.allocPixels(info);
525
526 SkCodec::Options options;
527 for (int i = 0; i < codec->codec()->getFrameCount(); ++i) {
528 SkCodec::FrameInfo frameInfo;
529 REPORTER_ASSERT(r, codec->codec()->getFrameInfo(i, &frameInfo));
530 if (5 == i) {
531 REPORTER_ASSERT(r, frameInfo.fDisposalMethod
532 == SkCodecAnimation::DisposalMethod::kRestoreBGColor);
533 }
534 options.fFrameIndex = i;
535 options.fPriorFrame = i - 1;
536 info = info.makeAlphaType(frameInfo.fAlphaType);
537
538 auto result = codec->codec()->getPixels(info, bm.getPixels(), bm.rowBytes(),
539 &options);
540 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
541
542 // Now compare to not using prior frame.
543 SkBitmap bm2;
544 bm2.allocPixels(info);
545
546 options.fPriorFrame = SkCodec::kNoFrame;
547 result = codec->codec()->getPixels(info, bm2.getPixels(), bm2.rowBytes(),
548 &options);
549 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
550
551 for (int y = 0; y < info.height(); ++y) {
552 if (0 != memcmp(bm.getAddr32(0, y), bm2.getAddr32(0, y), info.minRowBytes())) {
553 ERRORF(r, "pixel mismatch for sample size %i, frame %i resulting in "
554 "dimensions %i x %i line %i\n",
555 sampleSize, i, info.width(), info.height(), y);
556 break;
557 }
558 }
559 }
560 }
561 }
562
DEF_TEST(AndroidCodec_animated,r)563 DEF_TEST(AndroidCodec_animated, r) {
564 test_animated_AndroidCodec(r, "images/required.webp");
565 }
566
DEF_TEST(AndroidCodec_animated_gif,r)567 DEF_TEST(AndroidCodec_animated_gif, r) {
568 test_animated_AndroidCodec(r, "images/required.gif");
569 }
570
DEF_TEST(EncodedOriginToMatrixTest,r)571 DEF_TEST(EncodedOriginToMatrixTest, r) {
572 // SkAnimCodecPlayer relies on the fact that these matrices are invertible.
573 for (auto origin : { kTopLeft_SkEncodedOrigin ,
574 kTopRight_SkEncodedOrigin ,
575 kBottomRight_SkEncodedOrigin ,
576 kBottomLeft_SkEncodedOrigin ,
577 kLeftTop_SkEncodedOrigin ,
578 kRightTop_SkEncodedOrigin ,
579 kRightBottom_SkEncodedOrigin ,
580 kLeftBottom_SkEncodedOrigin }) {
581 // Arbitrary output dimensions.
582 auto matrix = SkEncodedOriginToMatrix(origin, 100, 80);
583 REPORTER_ASSERT(r, matrix.invert(nullptr));
584 }
585 }
586
DEF_TEST(AnimCodecPlayer,r)587 DEF_TEST(AnimCodecPlayer, r) {
588 static constexpr struct {
589 const char* fFile;
590 uint32_t fDuration;
591 SkISize fSize;
592 } gTests[] = {
593 { "images/alphabetAnim.gif" , 1300, {100, 100} },
594 { "images/randPixels.gif" , 0, { 8, 8} },
595 { "images/randPixels.jpg" , 0, { 8, 8} },
596 { "images/randPixels.png" , 0, { 8, 8} },
597 { "images/stoplight.webp" , 2500, { 11, 29} },
598 { "images/stoplight_h.webp" , 2500, { 29, 11} },
599 { "images/orientation/1.webp", 0, {100, 80} },
600 { "images/orientation/2.webp", 0, {100, 80} },
601 { "images/orientation/3.webp", 0, {100, 80} },
602 { "images/orientation/4.webp", 0, {100, 80} },
603 { "images/orientation/5.webp", 0, {100, 80} },
604 { "images/orientation/6.webp", 0, {100, 80} },
605 { "images/orientation/7.webp", 0, {100, 80} },
606 { "images/orientation/8.webp", 0, {100, 80} },
607 };
608
609 for (const auto& test : gTests) {
610 auto codec = SkCodec::MakeFromData(GetResourceAsData(test.fFile));
611 REPORTER_ASSERT(r, codec);
612
613 auto player = std::make_unique<SkAnimCodecPlayer>(std::move(codec));
614 REPORTER_ASSERT(r, player->duration() == test.fDuration);
615 REPORTER_ASSERT(r, player->dimensions() == test.fSize);
616
617 auto f0 = player->getFrame();
618 REPORTER_ASSERT(r, f0);
619 REPORTER_ASSERT(r, f0->bounds().size() == test.fSize,
620 "Mismatched size for initial frame of %s", test.fFile);
621
622 player->seek(500);
623 auto f1 = player->getFrame();
624 REPORTER_ASSERT(r, f1);
625 REPORTER_ASSERT(r, f1->bounds().size() == test.fSize,
626 "Mismatched size for frame at 500 ms of %s", test.fFile);
627 }
628 }
629