blob: 8498d881495090372005bd49f1295233d2db9b40 [file] [log] [blame]
scroggo19b91532016-10-24 09:03:26 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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"
Ben Wagner9707a7e2019-05-06 17:17:19 -040013#include "include/core/SkImage.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkImageInfo.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040015#include "include/core/SkRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "tests/CodecPriv.h"
22#include "tests/Test.h"
23#include "tools/Resources.h"
24#include "tools/ToolUtils.h"
scroggo19b91532016-10-24 09:03:26 -070025
Ben Wagner9707a7e2019-05-06 17:17:19 -040026#include <stdio.h>
Ben Wagnerb607a8f2018-03-12 13:46:21 -040027#include <cstring>
Ben Wagner9707a7e2019-05-06 17:17:19 -040028#include <initializer_list>
Ben Wagnerb607a8f2018-03-12 13:46:21 -040029#include <memory>
30#include <utility>
scroggo19b91532016-10-24 09:03:26 -070031#include <vector>
32
Leon Scroggins III7d22a332017-04-12 17:01:26 -040033DEF_TEST(Codec_trunc, r) {
Hal Canaryc465d132017-12-08 10:21:31 -050034 sk_sp<SkData> data(GetResourceAsData("images/box.gif"));
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040035 if (!data) {
36 return;
37 }
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040038 // See also Codec_GifTruncated2 in GifTest.cpp for this magic 23.
39 //
40 // TODO: just move this getFrameInfo call to Codec_GifTruncated2?
Mike Reedede7bac2017-07-23 15:30:02 -040041 SkCodec::MakeFromData(SkData::MakeSubset(data.get(), 0, 23))->getFrameInfo();
Leon Scroggins III7d22a332017-04-12 17:01:26 -040042}
43
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040044// 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.
47DEF_TEST(Codec_565, r) {
Hal Canaryc465d132017-12-08 10:21:31 -050048 sk_sp<SkData> data(GetResourceAsData("images/blendBG.webp"));
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040049 if (!data) {
50 return;
51 }
Mike Reedede7bac2017-07-23 15:30:02 -040052 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(std::move(data)));
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040053 auto info = codec->getInfo().makeColorType(kRGB_565_SkColorType);
54 SkBitmap bm;
55 bm.allocPixels(info);
56
57 SkCodec::Options options;
58 options.fFrameIndex = 1;
Nigel Tao66bc5242018-08-22 10:56:03 +100059 options.fPriorFrame = SkCodec::kNoFrame;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040060
61 const auto result = codec->getPixels(info, bm.getPixels(), bm.rowBytes(),
Leon Scroggins571b30f2017-07-11 17:35:31 +000062 &options);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040063 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
64}
65
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040066static bool restore_previous(const SkCodec::FrameInfo& info) {
67 return info.fDisposalMethod == SkCodecAnimation::DisposalMethod::kRestorePrevious;
68}
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040069
Leon Scroggins III469d67e2020-11-11 12:45:40 -050070namespace {
71SkString to_string(bool boolean) { return boolean ? SkString("true") : SkString("false"); }
72SkString 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}
82SkString to_string(SkIRect rect) {
83 return SkStringPrintf("{ %i, %i, %i, %i }", rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
84}
85
86template <typename T>
87void 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
scroggo19b91532016-10-24 09:03:26 -070095DEF_TEST(Codec_frames, r) {
Nigel Tao66bc5242018-08-22 10:56:03 +100096 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;
Leon Scroggins III469d67e2020-11-11 12:45:40 -0500105 constexpr auto kSrcOver = SkCodecAnimation::Blend::kSrcOver;
106 constexpr auto kSrc = SkCodecAnimation::Blend::kSrc;
Nigel Tao66bc5242018-08-22 10:56:03 +1000107
scroggo19b91532016-10-24 09:03:26 -0700108 static const struct {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400109 const char* fName;
110 int fFrameCount;
scroggo19b91532016-10-24 09:03:26 -0700111 // One less than fFramecount, since the first frame is always
112 // independent.
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400113 std::vector<int> fRequiredFrames;
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500114 // Same, since the first frame should match getInfo
115 std::vector<SkAlphaType> fAlphas;
scroggo19b91532016-10-24 09:03:26 -0700116 // The size of this one should match fFrameCount for animated, empty
117 // otherwise.
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400118 std::vector<int> fDurations;
119 int fRepetitionCount;
120 std::vector<SkCodecAnimation::DisposalMethod> fDisposalMethods;
Leon Scroggins III469d67e2020-11-11 12:45:40 -0500121 std::vector<bool> fAlphaWithinBounds;
122 std::vector<SkCodecAnimation::Blend> fBlends;
123 std::vector<SkIRect> fFrameRects;
scroggo19b91532016-10-24 09:03:26 -0700124 } gRecs[] = {
Hal Canaryc465d132017-12-08 10:21:31 -0500125 { "images/required.gif", 7,
Chris Blume0b5e7d12017-09-27 13:23:41 -0700126 { 0, 1, 2, 3, 4, 5 },
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500127 { kOpaque, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400128 { 100, 100, 100, 100, 100, 100, 100 },
129 0,
Leon Scroggins III469d67e2020-11-11 12:45:40 -0500130 { 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 },
Hal Canaryc465d132017-12-08 10:21:31 -0500137 { "images/alphabetAnim.gif", 13,
Nigel Tao66bc5242018-08-22 10:56:03 +1000138 { kNoFrame, 0, 0, 0, 0, 5, 6, kNoFrame, kNoFrame, 9, 10, 11 },
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500139 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
140 kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400141 { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400142 0,
143 { kKeep, kRestorePrev, kRestorePrev, kRestorePrev, kRestorePrev,
144 kRestoreBG, kKeep, kRestoreBG, kRestoreBG, kKeep, kKeep,
Leon Scroggins III469d67e2020-11-11 12:45:40 -0500145 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 },
Hal Canaryc465d132017-12-08 10:21:31 -0500155 { "images/randPixelsAnim2.gif", 4,
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400156 // required frames
157 { 0, 0, 1 },
158 // alphas
159 { kOpaque, kOpaque, kOpaque },
160 // durations
161 { 0, 1000, 170, 40 },
162 // repetition count
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400163 0,
Leon Scroggins III469d67e2020-11-11 12:45:40 -0500164 { 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 },
Hal Canaryc465d132017-12-08 10:21:31 -0500169 { "images/randPixelsAnim.gif", 13,
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500170 // required frames
Chris Blume0b5e7d12017-09-27 13:23:41 -0700171 { 0, 1, 2, 3, 4, 3, 6, 7, 7, 7, 9, 9 },
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500172 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
173 kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500174 // durations
175 { 0, 1000, 170, 40, 220, 7770, 90, 90, 90, 90, 90, 90, 90 },
176 // repetition count
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400177 0,
178 { kKeep, kKeep, kKeep, kKeep, kRestoreBG, kRestoreBG, kRestoreBG,
179 kRestoreBG, kRestorePrev, kRestoreBG, kRestorePrev, kRestorePrev,
Leon Scroggins III469d67e2020-11-11 12:45:40 -0500180 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 }, {}, {}, {} },
Hal Canaryc465d132017-12-08 10:21:31 -0500192 { "images/test640x479.gif", 4, { 0, 1, 2 },
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400193 { kOpaque, kOpaque, kOpaque },
194 { 200, 200, 200, 200 },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400195 SkCodec::kRepetitionCountInfinite,
Leon Scroggins III469d67e2020-11-11 12:45:40 -0500196 { 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 },
Hal Canaryc465d132017-12-08 10:21:31 -0500201 { "images/colorTables.gif", 2, { 0 }, { kOpaque }, { 1000, 1000 }, 5,
Leon Scroggins III469d67e2020-11-11 12:45:40 -0500202 { kKeep, kKeep }, {false, true}, { kSrcOver, kSrcOver },
203 { {0, 0, 640, 400}, {0, 0, 640, 200}},
204 },
scroggo19b91532016-10-24 09:03:26 -0700205
Leon Scroggins III469d67e2020-11-11 12:45:40 -0500206 { "images/arrow.png", 1, {}, {}, {}, 0, {}, {}, {}, {} },
207 { "images/google_chrome.ico", 1, {}, {}, {}, 0, {}, {}, {}, {} },
208 { "images/brickwork-texture.jpg", 1, {}, {}, {}, 0, {}, {}, {}, {} },
scroggo19b91532016-10-24 09:03:26 -0700209#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
Leon Scroggins III469d67e2020-11-11 12:45:40 -0500210 { "images/dng_with_preview.dng", 1, {}, {}, {}, 0, {}, {}, {}, {} },
scroggo19b91532016-10-24 09:03:26 -0700211#endif
Leon Scroggins III469d67e2020-11-11 12:45:40 -0500212 { "images/mandrill.wbmp", 1, {}, {}, {}, 0, {}, {}, {}, {} },
213 { "images/randPixels.bmp", 1, {}, {}, {}, 0, {}, {}, {}, {} },
214 { "images/yellow_rose.webp", 1, {}, {}, {}, 0, {}, {}, {}, {} },
Leon Scrogginsbc098ef2020-10-27 15:24:18 -0400215 { "images/stoplight.webp", 3, { 0, 1 }, { kOpaque, kOpaque },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400216 { 1000, 500, 1000 }, SkCodec::kRepetitionCountInfinite,
Leon Scroggins III469d67e2020-11-11 12:45:40 -0500217 { kKeep, kKeep, kKeep }, {false, false, false},
218 {kSrcOver, kSrcOver, kSrcOver},
219 { {0, 0, 11, 29}, {2, 10, 9, 27}, {2, 2, 9, 18}},
220 },
Nigel Tao66bc5242018-08-22 10:56:03 +1000221 { "images/blendBG.webp", 7,
222 { 0, kNoFrame, kNoFrame, kNoFrame, 4, 4 },
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400223 { kOpaque, kOpaque, kUnpremul, kOpaque, kUnpremul, kUnpremul },
Leon Scroggins IIIae834f572019-12-10 14:24:18 -0500224 { 525, 500, 525, 437, 609, 729, 444 },
Leon Scroggins IIIae834f572019-12-10 14:24:18 -0500225 6,
Leon Scroggins III469d67e2020-11-11 12:45:40 -0500226 { 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 },
Hal Canaryc465d132017-12-08 10:21:31 -0500232 { "images/required.webp", 7,
Nigel Tao66bc5242018-08-22 10:56:03 +1000233 { 0, 1, 1, kNoFrame, 4, 4 },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400234 { kOpaque, kUnpremul, kUnpremul, kOpaque, kOpaque, kOpaque },
235 { 100, 100, 100, 100, 100, 100, 100 },
Leon Scroggins IIIae834f572019-12-10 14:24:18 -0500236 0,
Leon Scroggins III469d67e2020-11-11 12:45:40 -0500237 { 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 },
scroggo19b91532016-10-24 09:03:26 -0700244 };
245
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400246 for (const auto& rec : gRecs) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400247 sk_sp<SkData> data(GetResourceAsData(rec.fName));
248 if (!data) {
scroggo19b91532016-10-24 09:03:26 -0700249 // 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
Mike Reedede7bac2017-07-23 15:30:02 -0400255 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data));
scroggo19b91532016-10-24 09:03:26 -0700256 if (!codec) {
257 ERRORF(r, "Failed to create an SkCodec from '%s'", rec.fName);
258 continue;
259 }
260
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400261 {
262 SkCodec::FrameInfo frameInfo;
263 REPORTER_ASSERT(r, !codec->getFrameInfo(0, &frameInfo));
264 }
265
scroggoe71b1a12016-11-01 08:28:28 -0700266 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
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400272 const int expected = rec.fFrameCount;
273 if (rec.fRequiredFrames.size() + 1 != static_cast<size_t>(expected)) {
Adlai Holler684838f2020-05-12 10:41:04 -0400274 ERRORF(r, "'%s' has wrong number entries in fRequiredFrames; expected: %i\tactual: %zu",
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400275 rec.fName, expected - 1, rec.fRequiredFrames.size());
scroggo19b91532016-10-24 09:03:26 -0700276 continue;
277 }
278
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400279 if (expected > 1) {
280 if (rec.fDurations.size() != static_cast<size_t>(expected)) {
Adlai Holler684838f2020-05-12 10:41:04 -0400281 ERRORF(r, "'%s' has wrong number entries in fDurations; expected: %i\tactual: %zu",
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400282 rec.fName, expected, rec.fDurations.size());
283 continue;
284 }
285
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400286 if (rec.fAlphas.size() + 1 != static_cast<size_t>(expected)) {
Adlai Holler684838f2020-05-12 10:41:04 -0400287 ERRORF(r, "'%s' has wrong number entries in fAlphas; expected: %i\tactual: %zu",
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400288 rec.fName, expected - 1, rec.fAlphas.size());
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400289 continue;
290 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400291
292 if (rec.fDisposalMethods.size() != static_cast<size_t>(expected)) {
293 ERRORF(r, "'%s' has wrong number entries in fDisposalMethods; "
Adlai Holler684838f2020-05-12 10:41:04 -0400294 "expected %i\tactual: %zu",
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400295 rec.fName, expected, rec.fDisposalMethods.size());
296 continue;
297 }
scroggo19b91532016-10-24 09:03:26 -0700298 }
299
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400300 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.
Mike Reedede7bac2017-07-23 15:30:02 -0400307 codec = SkCodec::MakeFromData(data);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400308
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400309 int frameCount;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400310 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
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400333 for (int i = 0; i < frameCount; i++) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400334 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
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500350 auto to_string = [](SkAlphaType alpha) {
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400351 switch (alpha) {
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500352 case kUnpremul_SkAlphaType:
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400353 return "unpremul";
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500354 case kOpaque_SkAlphaType:
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400355 return "opaque";
356 default:
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400357 SkASSERT(false);
358 return "unknown";
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400359 }
360 };
361
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500362 auto expectedAlpha = 0 == i ? codec->getInfo().alphaType() : rec.fAlphas[i-1];
363 auto alpha = frameInfo.fAlphaType;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400364 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 }
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400368
369 if (0 == i) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000370 REPORTER_ASSERT(r, frameInfo.fRequiredFrame == SkCodec::kNoFrame);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400371 } 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 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400375
376 REPORTER_ASSERT(r, frameInfo.fDisposalMethod == rec.fDisposalMethods[i]);
Leon Scroggins III469d67e2020-11-11 12:45:40 -0500377
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);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400387 }
388
389 if (TestMode::kIndividual == mode) {
390 // No need to test decoding twice.
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400391 continue;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400392 }
393
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400394 // 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.
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400401 std::vector<SkBitmap> cachedFrames(frameCount);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400402 const auto info = codec->getInfo().makeColorType(kN32_SkColorType);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400403
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400404 auto decode = [&](SkBitmap* bm, int index, int cachedIndex) {
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400405 auto decodeInfo = info;
406 if (index > 0) {
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500407 decodeInfo = info.makeAlphaType(frameInfos[index].fAlphaType);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400408 }
409 bm->allocPixels(decodeInfo);
Nigel Tao66bc5242018-08-22 10:56:03 +1000410 if (cachedIndex != SkCodec::kNoFrame) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400411 // First copy the pixels from the cached frame
Mike Kleinea3f0142019-03-20 11:12:10 -0500412 const bool success =
413 ToolUtils::copy_to(bm, kN32_SkColorType, cachedFrames[cachedIndex]);
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400414 REPORTER_ASSERT(r, success);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400415 }
416 SkCodec::Options opts;
417 opts.fFrameIndex = index;
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400418 opts.fPriorFrame = cachedIndex;
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400419 const auto result = codec->getPixels(decodeInfo, bm->getPixels(), bm->rowBytes(),
Leon Scroggins571b30f2017-07-11 17:35:31 +0000420 &opts);
Nigel Tao66bc5242018-08-22 10:56:03 +1000421 if (cachedIndex != SkCodec::kNoFrame &&
422 restore_previous(frameInfos[cachedIndex])) {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400423 if (result == SkCodec::kInvalidParameters) {
424 return true;
425 }
426 ERRORF(r, "Using a kRestorePrevious frame as fPriorFrame should fail");
427 return false;
428 }
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400429 if (result != SkCodec::kSuccess) {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400430 ERRORF(r, "Failed to decode frame %i from %s when providing prior frame %i, "
431 "error %i", index, rec.fName, cachedIndex, result);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400432 }
433 return result == SkCodec::kSuccess;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400434 };
435
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400436 for (int i = 0; i < frameCount; i++) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400437 SkBitmap& cachedFrame = cachedFrames[i];
Nigel Tao66bc5242018-08-22 10:56:03 +1000438 if (!decode(&cachedFrame, i, SkCodec::kNoFrame)) {
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400439 continue;
440 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400441 const auto reqFrame = frameInfos[i].fRequiredFrame;
Nigel Tao66bc5242018-08-22 10:56:03 +1000442 if (reqFrame == SkCodec::kNoFrame) {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400443 // Nothing to compare against.
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400444 continue;
445 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400446 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 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400455
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400456 // 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 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400473 }
474 }
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500475 }
scroggo19b91532016-10-24 09:03:26 -0700476 }
477 }
478}
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500479
Leon Scroggins1340dbd2020-11-09 14:18:12 -0500480// 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
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500482// frame, we have to erase its rectangle. The rectangle has to be adjusted
483// based on the scaled size.
Leon Scroggins1340dbd2020-11-09 14:18:12 -0500484static void test_animated_AndroidCodec(skiatest::Reporter* r, const char* file) {
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500485 if (GetResourcePath().isEmpty()) {
486 return;
487 }
488
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500489 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);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400505 info = info.makeDimensions(dimensions);
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500506 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
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400521 auto result = codec->codec()->getPixels(info, bm.getPixels(), bm.rowBytes(),
522 &options);
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500523 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400524
525 // Now compare to not using prior frame.
526 SkBitmap bm2;
527 bm2.allocPixels(info);
528
Nigel Tao66bc5242018-08-22 10:56:03 +1000529 options.fPriorFrame = SkCodec::kNoFrame;
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400530 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) {
John Stilesc1c3c6d2020-08-15 23:22:53 -0400535 if (0 != memcmp(bm.getAddr32(0, y), bm2.getAddr32(0, y), info.minRowBytes())) {
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400536 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 }
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500542 }
543 }
544}
Florin Malitaf8776c22018-11-06 19:16:28 -0500545
Leon Scroggins1340dbd2020-11-09 14:18:12 -0500546DEF_TEST(AndroidCodec_animated, r) {
547 test_animated_AndroidCodec(r, "images/required.webp");
548}
549
550DEF_TEST(AndroidCodec_animated_gif, r) {
551 test_animated_AndroidCodec(r, "images/required.gif");
552}
553
Leon Scrogginsbc098ef2020-10-27 15:24:18 -0400554DEF_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
Florin Malitaf8776c22018-11-06 19:16:28 -0500570DEF_TEST(AnimCodecPlayer, r) {
571 static constexpr struct {
572 const char* fFile;
573 uint32_t fDuration;
574 SkISize fSize;
575 } gTests[] = {
Leon Scrogginsbc098ef2020-10-27 15:24:18 -0400576 { "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} },
Florin Malitaf8776c22018-11-06 19:16:28 -0500590 };
591
592 for (const auto& test : gTests) {
593 auto codec = SkCodec::MakeFromData(GetResourceAsData(test.fFile));
594 REPORTER_ASSERT(r, codec);
595
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500596 auto player = std::make_unique<SkAnimCodecPlayer>(std::move(codec));
Florin Malitaf8776c22018-11-06 19:16:28 -0500597 REPORTER_ASSERT(r, player->duration() == test.fDuration);
Leon Scrogginsbc098ef2020-10-27 15:24:18 -0400598 REPORTER_ASSERT(r, player->dimensions() == test.fSize);
Florin Malitaf8776c22018-11-06 19:16:28 -0500599
600 auto f0 = player->getFrame();
601 REPORTER_ASSERT(r, f0);
Leon Scrogginsbc098ef2020-10-27 15:24:18 -0400602 REPORTER_ASSERT(r, f0->bounds().size() == test.fSize,
603 "Mismatched size for initial frame of %s", test.fFile);
Florin Malitaf8776c22018-11-06 19:16:28 -0500604
605 player->seek(500);
606 auto f1 = player->getFrame();
607 REPORTER_ASSERT(r, f1);
Leon Scrogginsbc098ef2020-10-27 15:24:18 -0400608 REPORTER_ASSERT(r, f1->bounds().size() == test.fSize,
609 "Mismatched size for frame at 500 ms of %s", test.fFile);
Florin Malitaf8776c22018-11-06 19:16:28 -0500610 }
611}