• 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/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 "tests/CodecPriv.h"
25 #include "tests/Test.h"
26 #include "tools/Resources.h"
27 #include "tools/ToolUtils.h"
28 
29 #include <cstdint>
30 #include <cstring>
31 #include <initializer_list>
32 #include <memory>
33 #include <utility>
34 #include <vector>
35 
DEF_TEST(Codec_trunc,r)36 DEF_TEST(Codec_trunc, r) {
37     sk_sp<SkData> data(GetResourceAsData("images/box.gif"));
38     if (!data) {
39         return;
40     }
41     // See also Codec_GifTruncated2 in GifTest.cpp for this magic 23.
42     //
43     // TODO: just move this getFrameInfo call to Codec_GifTruncated2?
44     SkCodec::MakeFromData(SkData::MakeSubset(data.get(), 0, 23))->getFrameInfo();
45 }
46 
47 // 565 does not support alpha, but there is no reason for it not to support an
48 // animated image with a frame that has alpha but then blends onto an opaque
49 // frame making the result opaque. Test that we can decode such a frame.
DEF_TEST(Codec_565,r)50 DEF_TEST(Codec_565, r) {
51     sk_sp<SkData> data(GetResourceAsData("images/blendBG.webp"));
52     if (!data) {
53         return;
54     }
55     std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(std::move(data)));
56     REPORTER_ASSERT(r, codec);
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 #if defined(SK_CODEC_DECODES_ICO)
212         { "images/google_chrome.ico", 1, {}, {}, {}, 0, {}, {}, {}, {} },
213 #endif
214         { "images/brickwork-texture.jpg", 1, {}, {}, {}, 0, {}, {}, {}, {} },
215 #if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
216         { "images/dng_with_preview.dng", 1, {}, {}, {}, 0, {}, {}, {}, {} },
217 #endif
218         { "images/mandrill.wbmp", 1, {}, {}, {}, 0, {}, {}, {}, {} },
219         { "images/randPixels.bmp", 1, {}, {}, {}, 0, {}, {}, {}, {} },
220         { "images/yellow_rose.webp", 1, {}, {}, {}, 0, {}, {}, {}, {} },
221         { "images/stoplight.webp", 3, { 0, 1 }, { kOpaque, kOpaque },
222             { 1000, 500, 1000 }, SkCodec::kRepetitionCountInfinite,
223             { kKeep, kKeep, kKeep }, {false, false, false},
224             {kSrcOver, kSrcOver, kSrcOver},
225             { {0, 0, 11, 29}, {2, 10, 9, 27}, {2, 2, 9, 18}},
226         },
227         { "images/blendBG.webp", 7,
228             { 0, kNoFrame, kNoFrame, kNoFrame, 4, 4 },
229             { kOpaque, kOpaque, kUnpremul, kOpaque, kUnpremul, kUnpremul },
230             { 525, 500, 525, 437, 609, 729, 444 },
231             6,
232             { kKeep, kKeep, kKeep, kKeep, kKeep, kKeep, kKeep },
233             { false, true, false, true, false, true, true },
234             { kSrc, kSrcOver, kSrc, kSrc, kSrc, kSrc, kSrc },
235             { {0, 0, 200, 200}, {0, 0, 200, 200}, {0, 0, 200, 200}, {0, 0, 200, 200},
236               {0, 0, 200, 200}, {100, 100, 200, 200}, {100, 100, 200, 200} },
237         },
238         { "images/required.webp", 7,
239             { 0, 1, 1, kNoFrame, 4, 4 },
240             { kOpaque, kUnpremul, kUnpremul, kOpaque, kOpaque, kOpaque },
241             { 100, 100, 100, 100, 100, 100, 100 },
242             0,
243             { kKeep, kRestoreBG, kKeep, kKeep, kKeep, kRestoreBG, kKeep },
244             { false, false, false, false, false, false, false },
245             { kSrc, kSrcOver, kSrcOver, kSrcOver, kSrc, kSrcOver,
246               kSrcOver },
247             { {0, 0, 100, 100}, {0, 0, 75, 75}, {0, 0, 50, 50}, {0, 0, 60, 60},
248               {0, 0, 100, 100}, {0, 0, 50, 50}, {0, 0, 75, 75}},
249           },
250     };
251 
252     for (const auto& rec : gRecs) {
253         skiatest::ReporterContext context(r, rec.fName);
254         sk_sp<SkData> data(GetResourceAsData(rec.fName));
255         if (!data) {
256             // Useful error statement, but sometimes people run tests without
257             // resources, and they do not want to see these messages.
258             //ERRORF(r, "Missing resources? Could not find '%s'", rec.fName);
259             continue;
260         }
261 
262         std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data));
263         if (!codec) {
264             ERRORF(r, "Failed to create an SkCodec from '%s'", rec.fName);
265             continue;
266         }
267 
268         {
269             SkCodec::FrameInfo frameInfo;
270             REPORTER_ASSERT(r, !codec->getFrameInfo(0, &frameInfo));
271         }
272 
273         const int expected = rec.fFrameCount;
274         if (rec.fRequiredFrames.size() + 1 != static_cast<size_t>(expected)) {
275             ERRORF(r, "'%s' has wrong number entries in fRequiredFrames; expected: %i\tactual: %zu",
276                    rec.fName, expected - 1, rec.fRequiredFrames.size());
277             continue;
278         }
279 
280         if (expected > 1) {
281             if (rec.fDurations.size() != static_cast<size_t>(expected)) {
282                 ERRORF(r, "'%s' has wrong number entries in fDurations; expected: %i\tactual: %zu",
283                        rec.fName, expected, rec.fDurations.size());
284                 continue;
285             }
286 
287             if (rec.fAlphas.size() + 1 != static_cast<size_t>(expected)) {
288                 ERRORF(r, "'%s' has wrong number entries in fAlphas; expected: %i\tactual: %zu",
289                        rec.fName, expected - 1, rec.fAlphas.size());
290                 continue;
291             }
292 
293             if (rec.fDisposalMethods.size() != static_cast<size_t>(expected)) {
294                 ERRORF(r, "'%s' has wrong number entries in fDisposalMethods; "
295                        "expected %i\tactual: %zu",
296                        rec.fName, expected, rec.fDisposalMethods.size());
297                 continue;
298             }
299         }
300 
301         enum class TestMode {
302             kVector,
303             kIndividual,
304         };
305 
306         for (auto mode : { TestMode::kVector, TestMode::kIndividual }) {
307             // Re-create the codec to reset state and test parsing.
308             codec = SkCodec::MakeFromData(data);
309 
310             int frameCount;
311             std::vector<SkCodec::FrameInfo> frameInfos;
312             switch (mode) {
313                 case TestMode::kVector:
314                     frameInfos = codec->getFrameInfo();
315                     // getFrameInfo returns empty set for non-animated.
316                     frameCount = frameInfos.empty() ? 1 : frameInfos.size();
317                     break;
318                 case TestMode::kIndividual:
319                     frameCount = codec->getFrameCount();
320                     break;
321             }
322 
323             if (frameCount != expected) {
324                 ERRORF(r, "'%s' expected frame count: %i\tactual: %i",
325                        rec.fName, expected, frameCount);
326                 continue;
327             }
328 
329             // Get the repetition count after the codec->getFrameInfo() or
330             // codec->getFrameCount() call above has walked to the end of the
331             // encoded image.
332             //
333             // At the file format level, GIF images can declare their
334             // repetition count multiple times and our codec goes with "last
335             // one wins". Furthermore, for single-frame (still) GIF images, a
336             // zero, positive or infinite repetition count are all equivalent
337             // in practice (in all cases, the pixels do not change over time),
338             // so the codec has some leeway in what to return for single-frame
339             // GIF images, but it cannot distinguish single-frame from
340             // multiple-frame GIFs until we count the number of frames (e.g.
341             // call getFrameInfo or getFrameCount).
342             const int repetitionCount = codec->getRepetitionCount();
343             if (repetitionCount != rec.fRepetitionCount) {
344                 ERRORF(r, "%s repetition count does not match! expected: %i\tactual: %i",
345                           rec.fName, rec.fRepetitionCount, repetitionCount);
346             }
347 
348             // When decoding the full, non-partial input, `isAnimated()` will
349             // just be a proxy for "is there just a single frame?".
350             const SkCodec::IsAnimated expectedIsAnimated =
351                     rec.fFrameCount == 1 ? SkCodec::IsAnimated::kNo : SkCodec::IsAnimated::kYes;
352             const SkCodec::IsAnimated actualIsAnimated = codec->isAnimated();
353             if (expectedIsAnimated != actualIsAnimated) {
354                 ERRORF(r, "%s isAnimated does not match! expected: %i\tactual: %i", rec.fName,
355                        static_cast<int>(expectedIsAnimated), static_cast<int>(actualIsAnimated));
356             }
357 
358             // From here on, we are only concerned with animated images.
359             if (1 == frameCount) {
360                 continue;
361             }
362 
363             for (int i = 0; i < frameCount; i++) {
364                 SkCodec::FrameInfo frameInfo;
365                 switch (mode) {
366                     case TestMode::kVector:
367                         frameInfo = frameInfos[i];
368                         break;
369                     case TestMode::kIndividual:
370                         REPORTER_ASSERT(r, codec->getFrameInfo(i, nullptr));
371                         REPORTER_ASSERT(r, codec->getFrameInfo(i, &frameInfo));
372                         break;
373                 }
374 
375                 if (rec.fDurations[i] != frameInfo.fDuration) {
376                     ERRORF(r, "%s frame %i's durations do not match! expected: %i\tactual: %i",
377                            rec.fName, i, rec.fDurations[i], frameInfo.fDuration);
378                 }
379 
380                 auto to_string = [](SkAlphaType alpha) {
381                     switch (alpha) {
382                         case kUnpremul_SkAlphaType:
383                             return "unpremul";
384                         case kOpaque_SkAlphaType:
385                             return "opaque";
386                         default:
387                             SkASSERT(false);
388                             return "unknown";
389                     }
390                 };
391 
392                 auto expectedAlpha = 0 == i ? codec->getInfo().alphaType() : rec.fAlphas[i-1];
393                 auto alpha = frameInfo.fAlphaType;
394                 if (expectedAlpha != alpha) {
395                     ERRORF(r, "%s's frame %i has wrong alpha type! expected: %s\tactual: %s",
396                            rec.fName, i, to_string(expectedAlpha), to_string(alpha));
397                 }
398 
399                 if (0 == i) {
400                     REPORTER_ASSERT(r, frameInfo.fRequiredFrame == SkCodec::kNoFrame);
401                 } else if (rec.fRequiredFrames[i-1] != frameInfo.fRequiredFrame) {
402                     ERRORF(r, "%s's frame %i has wrong dependency! expected: %i\tactual: %i",
403                            rec.fName, i, rec.fRequiredFrames[i-1], frameInfo.fRequiredFrame);
404                 }
405 
406                 REPORTER_ASSERT(r, frameInfo.fDisposalMethod == rec.fDisposalMethods[i]);
407 
408                 reporter_assert_equals<bool>(r, rec.fName, i, "alpha within bounds",
409                                              rec.fAlphaWithinBounds[i],
410                                              frameInfo.fHasAlphaWithinBounds);
411 
412                 reporter_assert_equals(r, rec.fName, i, "blend mode", rec.fBlends[i],
413                                        frameInfo.fBlend);
414 
415                 reporter_assert_equals(r, rec.fName, i, "frame rect", rec.fFrameRects[i],
416                                        frameInfo.fFrameRect);
417             }
418 
419             if (TestMode::kIndividual == mode) {
420                 // No need to test decoding twice.
421                 continue;
422             }
423 
424             // Compare decoding in multiple ways:
425             // - Start from scratch for each frame. |codec| will have to decode the required frame
426             //   (and any it depends on) to decode. This is stored in |cachedFrames|.
427             // - Provide the frame that a frame depends on, so |codec| just has to blend.
428             // - Provide a frame after the required frame, which will be covered up by the newest
429             //   frame.
430             // All should look the same.
431             std::vector<SkBitmap> cachedFrames(frameCount);
432             const auto info = codec->getInfo().makeColorType(kN32_SkColorType);
433 
434             auto decode = [&](SkBitmap* bm, int index, int cachedIndex) {
435                 auto decodeInfo = info;
436                 if (index > 0) {
437                     decodeInfo = info.makeAlphaType(frameInfos[index].fAlphaType);
438                 }
439                 bm->allocPixels(decodeInfo);
440                 if (cachedIndex != SkCodec::kNoFrame) {
441                     // First copy the pixels from the cached frame
442                     const bool success =
443                             ToolUtils::copy_to(bm, kN32_SkColorType, cachedFrames[cachedIndex]);
444                     REPORTER_ASSERT(r, success);
445                 }
446                 SkCodec::Options opts;
447                 opts.fFrameIndex = index;
448                 opts.fPriorFrame = cachedIndex;
449                 const auto result = codec->getPixels(decodeInfo, bm->getPixels(), bm->rowBytes(),
450                                                      &opts);
451                 if (cachedIndex != SkCodec::kNoFrame &&
452                         restore_previous(frameInfos[cachedIndex])) {
453                     if (result == SkCodec::kInvalidParameters) {
454                         return true;
455                     }
456                     ERRORF(r, "Using a kRestorePrevious frame as fPriorFrame should fail");
457                     return false;
458                 }
459                 if (result != SkCodec::kSuccess) {
460                     ERRORF(r, "Failed to decode frame %i from %s when providing prior frame %i, "
461                               "error %i", index, rec.fName, cachedIndex, result);
462                 }
463                 return result == SkCodec::kSuccess;
464             };
465 
466             for (int i = 0; i < frameCount; i++) {
467                 SkBitmap& cachedFrame = cachedFrames[i];
468                 if (!decode(&cachedFrame, i, SkCodec::kNoFrame)) {
469                     continue;
470                 }
471                 const auto reqFrame = frameInfos[i].fRequiredFrame;
472                 if (reqFrame == SkCodec::kNoFrame) {
473                     // Nothing to compare against.
474                     continue;
475                 }
476                 for (int j = reqFrame; j < i; j++) {
477                     SkBitmap frame;
478                     if (restore_previous(frameInfos[j])) {
479                         (void) decode(&frame, i, j);
480                         continue;
481                     }
482                     if (!decode(&frame, i, j)) {
483                         continue;
484                     }
485 
486                     // Now verify they're equal.
487                     const size_t rowLen = info.bytesPerPixel() * info.width();
488                     for (int y = 0; y < info.height(); y++) {
489                         const void* cachedAddr = cachedFrame.getAddr(0, y);
490                         SkASSERT(cachedAddr != nullptr);
491                         const void* addr = frame.getAddr(0, y);
492                         SkASSERT(addr != nullptr);
493                         const bool lineMatches = memcmp(cachedAddr, addr, rowLen) == 0;
494                         if (!lineMatches) {
495                             SkString name = SkStringPrintf("cached_%i", i);
496                             write_bm(name.c_str(), cachedFrame);
497                             name = SkStringPrintf("frame_%i", i);
498                             write_bm(name.c_str(), frame);
499                             ERRORF(r, "%s's frame %i is different (starting from line %i) when "
500                                       "providing prior frame %i!", rec.fName, i, y, j);
501                             break;
502                         }
503                     }
504                 }
505             }
506         }
507     }
508 }
509 
510 // Verify that an image can be animated scaled down. These images have a
511 // kRestoreBG frame, so they are interesting to test. After decoding that
512 // frame, we have to erase its rectangle. The rectangle has to be adjusted
513 // based on the scaled size.
test_animated_AndroidCodec(skiatest::Reporter * r,const char * file)514 static void test_animated_AndroidCodec(skiatest::Reporter* r, const char* file) {
515     if (GetResourcePath().isEmpty()) {
516         return;
517     }
518 
519     sk_sp<SkData> data(GetResourceAsData(file));
520     if (!data) {
521         ERRORF(r, "Missing %s", file);
522         return;
523     }
524 
525     auto codec = SkAndroidCodec::MakeFromCodec(SkCodec::MakeFromData(std::move(data)));
526     if (!codec) {
527         ERRORF(r, "Failed to decode %s", file);
528         return;
529     }
530 
531     auto info = codec->getInfo().makeAlphaType(kPremul_SkAlphaType);
532 
533     for (int sampleSize : { 8, 32, 100 }) {
534         auto dimensions = codec->codec()->getScaledDimensions(1.0f / sampleSize);
535         info = info.makeDimensions(dimensions);
536         SkBitmap bm;
537         bm.allocPixels(info);
538 
539         SkCodec::Options options;
540         for (int i = 0; i < codec->codec()->getFrameCount(); ++i) {
541             SkCodec::FrameInfo frameInfo;
542             REPORTER_ASSERT(r, codec->codec()->getFrameInfo(i, &frameInfo));
543             if (5 == i) {
544                 REPORTER_ASSERT(r, frameInfo.fDisposalMethod
545                         == SkCodecAnimation::DisposalMethod::kRestoreBGColor);
546             }
547             options.fFrameIndex = i;
548             options.fPriorFrame = i - 1;
549             info = info.makeAlphaType(frameInfo.fAlphaType);
550 
551             auto result = codec->codec()->getPixels(info, bm.getPixels(), bm.rowBytes(),
552                                                     &options);
553             REPORTER_ASSERT(r, result == SkCodec::kSuccess);
554 
555             // Now compare to not using prior frame.
556             SkBitmap bm2;
557             bm2.allocPixels(info);
558 
559             options.fPriorFrame = SkCodec::kNoFrame;
560             result = codec->codec()->getPixels(info, bm2.getPixels(), bm2.rowBytes(),
561                                                &options);
562             REPORTER_ASSERT(r, result == SkCodec::kSuccess);
563 
564             for (int y = 0; y < info.height(); ++y) {
565                 if (0 != memcmp(bm.getAddr32(0, y), bm2.getAddr32(0, y), info.minRowBytes())) {
566                     ERRORF(r, "pixel mismatch for sample size %i, frame %i resulting in "
567                               "dimensions %i x %i line %i\n",
568                               sampleSize, i, info.width(), info.height(), y);
569                     break;
570                 }
571             }
572         }
573     }
574 }
575 
DEF_TEST(AndroidCodec_animated,r)576 DEF_TEST(AndroidCodec_animated, r) {
577     test_animated_AndroidCodec(r, "images/required.webp");
578 }
579 
DEF_TEST(AndroidCodec_animated_gif,r)580 DEF_TEST(AndroidCodec_animated_gif, r) {
581     test_animated_AndroidCodec(r, "images/required.gif");
582 }
583 
DEF_TEST(EncodedOriginToMatrixTest,r)584 DEF_TEST(EncodedOriginToMatrixTest, r) {
585     // SkAnimCodecPlayer relies on the fact that these matrices are invertible.
586     for (auto origin : { kTopLeft_SkEncodedOrigin     ,
587                          kTopRight_SkEncodedOrigin    ,
588                          kBottomRight_SkEncodedOrigin ,
589                          kBottomLeft_SkEncodedOrigin  ,
590                          kLeftTop_SkEncodedOrigin     ,
591                          kRightTop_SkEncodedOrigin    ,
592                          kRightBottom_SkEncodedOrigin ,
593                          kLeftBottom_SkEncodedOrigin  }) {
594         // Arbitrary output dimensions.
595         auto matrix = SkEncodedOriginToMatrix(origin, 100, 80);
596         REPORTER_ASSERT(r, matrix.invert(nullptr));
597     }
598 }
599 
600 #if defined(SK_ENABLE_SKOTTIE)
601 
602 #include "modules/skresources/src/SkAnimCodecPlayer.h"
603 
DEF_TEST(AnimCodecPlayer,r)604 DEF_TEST(AnimCodecPlayer, r) {
605     static constexpr struct {
606         const char* fFile;
607         uint32_t    fDuration;
608         SkISize     fSize;
609     } gTests[] = {
610         { "images/alphabetAnim.gif"  , 1300, {100, 100} },
611         { "images/randPixels.gif"    ,    0, {  8,   8} },
612         { "images/randPixels.jpg"    ,    0, {  8,   8} },
613         { "images/randPixels.png"    ,    0, {  8,   8} },
614         { "images/stoplight.webp"    , 2500, { 11,  29} },
615         { "images/stoplight_h.webp"  , 2500, { 29,  11} },
616         { "images/orientation/1.webp",    0, {100,  80} },
617         { "images/orientation/2.webp",    0, {100,  80} },
618         { "images/orientation/3.webp",    0, {100,  80} },
619         { "images/orientation/4.webp",    0, {100,  80} },
620         { "images/orientation/5.webp",    0, {100,  80} },
621         { "images/orientation/6.webp",    0, {100,  80} },
622         { "images/orientation/7.webp",    0, {100,  80} },
623         { "images/orientation/8.webp",    0, {100,  80} },
624     };
625 
626     for (const auto& test : gTests) {
627         auto codec = SkCodec::MakeFromData(GetResourceAsData(test.fFile));
628         REPORTER_ASSERT(r, codec);
629 
630         auto player = std::make_unique<SkAnimCodecPlayer>(std::move(codec));
631         REPORTER_ASSERT(r, player->duration() == test.fDuration);
632         REPORTER_ASSERT(r, player->dimensions() == test.fSize);
633 
634         auto f0 = player->getFrame();
635         REPORTER_ASSERT(r, f0);
636         REPORTER_ASSERT(r, f0->bounds().size() == test.fSize,
637                         "Mismatched size for initial frame of %s", test.fFile);
638 
639         player->seek(500);
640         auto f1 = player->getFrame();
641         REPORTER_ASSERT(r, f1);
642         REPORTER_ASSERT(r, f1->bounds().size() == test.fSize,
643                         "Mismatched size for frame at 500 ms of %s", test.fFile);
644     }
645 }
646 
647 #endif
648