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