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 // From here on, we are only concerned with animated images.
349 if (1 == frameCount) {
350 continue;
351 }
352
353 for (int i = 0; i < frameCount; i++) {
354 SkCodec::FrameInfo frameInfo;
355 switch (mode) {
356 case TestMode::kVector:
357 frameInfo = frameInfos[i];
358 break;
359 case TestMode::kIndividual:
360 REPORTER_ASSERT(r, codec->getFrameInfo(i, nullptr));
361 REPORTER_ASSERT(r, codec->getFrameInfo(i, &frameInfo));
362 break;
363 }
364
365 if (rec.fDurations[i] != frameInfo.fDuration) {
366 ERRORF(r, "%s frame %i's durations do not match! expected: %i\tactual: %i",
367 rec.fName, i, rec.fDurations[i], frameInfo.fDuration);
368 }
369
370 auto to_string = [](SkAlphaType alpha) {
371 switch (alpha) {
372 case kUnpremul_SkAlphaType:
373 return "unpremul";
374 case kOpaque_SkAlphaType:
375 return "opaque";
376 default:
377 SkASSERT(false);
378 return "unknown";
379 }
380 };
381
382 auto expectedAlpha = 0 == i ? codec->getInfo().alphaType() : rec.fAlphas[i-1];
383 auto alpha = frameInfo.fAlphaType;
384 if (expectedAlpha != alpha) {
385 ERRORF(r, "%s's frame %i has wrong alpha type! expected: %s\tactual: %s",
386 rec.fName, i, to_string(expectedAlpha), to_string(alpha));
387 }
388
389 if (0 == i) {
390 REPORTER_ASSERT(r, frameInfo.fRequiredFrame == SkCodec::kNoFrame);
391 } else if (rec.fRequiredFrames[i-1] != frameInfo.fRequiredFrame) {
392 ERRORF(r, "%s's frame %i has wrong dependency! expected: %i\tactual: %i",
393 rec.fName, i, rec.fRequiredFrames[i-1], frameInfo.fRequiredFrame);
394 }
395
396 REPORTER_ASSERT(r, frameInfo.fDisposalMethod == rec.fDisposalMethods[i]);
397
398 reporter_assert_equals<bool>(r, rec.fName, i, "alpha within bounds",
399 rec.fAlphaWithinBounds[i],
400 frameInfo.fHasAlphaWithinBounds);
401
402 reporter_assert_equals(r, rec.fName, i, "blend mode", rec.fBlends[i],
403 frameInfo.fBlend);
404
405 reporter_assert_equals(r, rec.fName, i, "frame rect", rec.fFrameRects[i],
406 frameInfo.fFrameRect);
407 }
408
409 if (TestMode::kIndividual == mode) {
410 // No need to test decoding twice.
411 continue;
412 }
413
414 // Compare decoding in multiple ways:
415 // - Start from scratch for each frame. |codec| will have to decode the required frame
416 // (and any it depends on) to decode. This is stored in |cachedFrames|.
417 // - Provide the frame that a frame depends on, so |codec| just has to blend.
418 // - Provide a frame after the required frame, which will be covered up by the newest
419 // frame.
420 // All should look the same.
421 std::vector<SkBitmap> cachedFrames(frameCount);
422 const auto info = codec->getInfo().makeColorType(kN32_SkColorType);
423
424 auto decode = [&](SkBitmap* bm, int index, int cachedIndex) {
425 auto decodeInfo = info;
426 if (index > 0) {
427 decodeInfo = info.makeAlphaType(frameInfos[index].fAlphaType);
428 }
429 bm->allocPixels(decodeInfo);
430 if (cachedIndex != SkCodec::kNoFrame) {
431 // First copy the pixels from the cached frame
432 const bool success =
433 ToolUtils::copy_to(bm, kN32_SkColorType, cachedFrames[cachedIndex]);
434 REPORTER_ASSERT(r, success);
435 }
436 SkCodec::Options opts;
437 opts.fFrameIndex = index;
438 opts.fPriorFrame = cachedIndex;
439 const auto result = codec->getPixels(decodeInfo, bm->getPixels(), bm->rowBytes(),
440 &opts);
441 if (cachedIndex != SkCodec::kNoFrame &&
442 restore_previous(frameInfos[cachedIndex])) {
443 if (result == SkCodec::kInvalidParameters) {
444 return true;
445 }
446 ERRORF(r, "Using a kRestorePrevious frame as fPriorFrame should fail");
447 return false;
448 }
449 if (result != SkCodec::kSuccess) {
450 ERRORF(r, "Failed to decode frame %i from %s when providing prior frame %i, "
451 "error %i", index, rec.fName, cachedIndex, result);
452 }
453 return result == SkCodec::kSuccess;
454 };
455
456 for (int i = 0; i < frameCount; i++) {
457 SkBitmap& cachedFrame = cachedFrames[i];
458 if (!decode(&cachedFrame, i, SkCodec::kNoFrame)) {
459 continue;
460 }
461 const auto reqFrame = frameInfos[i].fRequiredFrame;
462 if (reqFrame == SkCodec::kNoFrame) {
463 // Nothing to compare against.
464 continue;
465 }
466 for (int j = reqFrame; j < i; j++) {
467 SkBitmap frame;
468 if (restore_previous(frameInfos[j])) {
469 (void) decode(&frame, i, j);
470 continue;
471 }
472 if (!decode(&frame, i, j)) {
473 continue;
474 }
475
476 // Now verify they're equal.
477 const size_t rowLen = info.bytesPerPixel() * info.width();
478 for (int y = 0; y < info.height(); y++) {
479 const void* cachedAddr = cachedFrame.getAddr(0, y);
480 SkASSERT(cachedAddr != nullptr);
481 const void* addr = frame.getAddr(0, y);
482 SkASSERT(addr != nullptr);
483 const bool lineMatches = memcmp(cachedAddr, addr, rowLen) == 0;
484 if (!lineMatches) {
485 SkString name = SkStringPrintf("cached_%i", i);
486 write_bm(name.c_str(), cachedFrame);
487 name = SkStringPrintf("frame_%i", i);
488 write_bm(name.c_str(), frame);
489 ERRORF(r, "%s's frame %i is different (starting from line %i) when "
490 "providing prior frame %i!", rec.fName, i, y, j);
491 break;
492 }
493 }
494 }
495 }
496 }
497 }
498 }
499
500 // Verify that an image can be animated scaled down. These images have a
501 // kRestoreBG frame, so they are interesting to test. After decoding that
502 // frame, we have to erase its rectangle. The rectangle has to be adjusted
503 // based on the scaled size.
test_animated_AndroidCodec(skiatest::Reporter * r,const char * file)504 static void test_animated_AndroidCodec(skiatest::Reporter* r, const char* file) {
505 if (GetResourcePath().isEmpty()) {
506 return;
507 }
508
509 sk_sp<SkData> data(GetResourceAsData(file));
510 if (!data) {
511 ERRORF(r, "Missing %s", file);
512 return;
513 }
514
515 auto codec = SkAndroidCodec::MakeFromCodec(SkCodec::MakeFromData(std::move(data)));
516 if (!codec) {
517 ERRORF(r, "Failed to decode %s", file);
518 return;
519 }
520
521 auto info = codec->getInfo().makeAlphaType(kPremul_SkAlphaType);
522
523 for (int sampleSize : { 8, 32, 100 }) {
524 auto dimensions = codec->codec()->getScaledDimensions(1.0f / sampleSize);
525 info = info.makeDimensions(dimensions);
526 SkBitmap bm;
527 bm.allocPixels(info);
528
529 SkCodec::Options options;
530 for (int i = 0; i < codec->codec()->getFrameCount(); ++i) {
531 SkCodec::FrameInfo frameInfo;
532 REPORTER_ASSERT(r, codec->codec()->getFrameInfo(i, &frameInfo));
533 if (5 == i) {
534 REPORTER_ASSERT(r, frameInfo.fDisposalMethod
535 == SkCodecAnimation::DisposalMethod::kRestoreBGColor);
536 }
537 options.fFrameIndex = i;
538 options.fPriorFrame = i - 1;
539 info = info.makeAlphaType(frameInfo.fAlphaType);
540
541 auto result = codec->codec()->getPixels(info, bm.getPixels(), bm.rowBytes(),
542 &options);
543 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
544
545 // Now compare to not using prior frame.
546 SkBitmap bm2;
547 bm2.allocPixels(info);
548
549 options.fPriorFrame = SkCodec::kNoFrame;
550 result = codec->codec()->getPixels(info, bm2.getPixels(), bm2.rowBytes(),
551 &options);
552 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
553
554 for (int y = 0; y < info.height(); ++y) {
555 if (0 != memcmp(bm.getAddr32(0, y), bm2.getAddr32(0, y), info.minRowBytes())) {
556 ERRORF(r, "pixel mismatch for sample size %i, frame %i resulting in "
557 "dimensions %i x %i line %i\n",
558 sampleSize, i, info.width(), info.height(), y);
559 break;
560 }
561 }
562 }
563 }
564 }
565
DEF_TEST(AndroidCodec_animated,r)566 DEF_TEST(AndroidCodec_animated, r) {
567 test_animated_AndroidCodec(r, "images/required.webp");
568 }
569
DEF_TEST(AndroidCodec_animated_gif,r)570 DEF_TEST(AndroidCodec_animated_gif, r) {
571 test_animated_AndroidCodec(r, "images/required.gif");
572 }
573
DEF_TEST(EncodedOriginToMatrixTest,r)574 DEF_TEST(EncodedOriginToMatrixTest, r) {
575 // SkAnimCodecPlayer relies on the fact that these matrices are invertible.
576 for (auto origin : { kTopLeft_SkEncodedOrigin ,
577 kTopRight_SkEncodedOrigin ,
578 kBottomRight_SkEncodedOrigin ,
579 kBottomLeft_SkEncodedOrigin ,
580 kLeftTop_SkEncodedOrigin ,
581 kRightTop_SkEncodedOrigin ,
582 kRightBottom_SkEncodedOrigin ,
583 kLeftBottom_SkEncodedOrigin }) {
584 // Arbitrary output dimensions.
585 auto matrix = SkEncodedOriginToMatrix(origin, 100, 80);
586 REPORTER_ASSERT(r, matrix.invert(nullptr));
587 }
588 }
589
590 #if defined(SK_ENABLE_SKOTTIE)
591
592 #include "modules/skresources/src/SkAnimCodecPlayer.h"
593
DEF_TEST(AnimCodecPlayer,r)594 DEF_TEST(AnimCodecPlayer, r) {
595 static constexpr struct {
596 const char* fFile;
597 uint32_t fDuration;
598 SkISize fSize;
599 } gTests[] = {
600 { "images/alphabetAnim.gif" , 1300, {100, 100} },
601 { "images/randPixels.gif" , 0, { 8, 8} },
602 { "images/randPixels.jpg" , 0, { 8, 8} },
603 { "images/randPixels.png" , 0, { 8, 8} },
604 { "images/stoplight.webp" , 2500, { 11, 29} },
605 { "images/stoplight_h.webp" , 2500, { 29, 11} },
606 { "images/orientation/1.webp", 0, {100, 80} },
607 { "images/orientation/2.webp", 0, {100, 80} },
608 { "images/orientation/3.webp", 0, {100, 80} },
609 { "images/orientation/4.webp", 0, {100, 80} },
610 { "images/orientation/5.webp", 0, {100, 80} },
611 { "images/orientation/6.webp", 0, {100, 80} },
612 { "images/orientation/7.webp", 0, {100, 80} },
613 { "images/orientation/8.webp", 0, {100, 80} },
614 };
615
616 for (const auto& test : gTests) {
617 auto codec = SkCodec::MakeFromData(GetResourceAsData(test.fFile));
618 REPORTER_ASSERT(r, codec);
619
620 auto player = std::make_unique<SkAnimCodecPlayer>(std::move(codec));
621 REPORTER_ASSERT(r, player->duration() == test.fDuration);
622 REPORTER_ASSERT(r, player->dimensions() == test.fSize);
623
624 auto f0 = player->getFrame();
625 REPORTER_ASSERT(r, f0);
626 REPORTER_ASSERT(r, f0->bounds().size() == test.fSize,
627 "Mismatched size for initial frame of %s", test.fFile);
628
629 player->seek(500);
630 auto f1 = player->getFrame();
631 REPORTER_ASSERT(r, f1);
632 REPORTER_ASSERT(r, f1->bounds().size() == test.fSize,
633 "Mismatched size for frame at 500 ms of %s", test.fFile);
634 }
635 }
636
637 #endif
638