blob: c5c65f84683ad104484ad4f741db38da4c56161d [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
Leon Scroggins III42ee2842018-01-14 14:46:51 -05008#include "SkAndroidCodec.h"
scroggo38a2cf52016-10-24 09:56:40 -07009#include "SkBitmap.h"
scroggo19b91532016-10-24 09:03:26 -070010#include "SkCodec.h"
Ben Wagner501c17c2018-03-12 20:04:31 +000011#include "SkStream.h"
12
13#include "CodecPriv.h"
14#include "Resources.h"
scroggo19b91532016-10-24 09:03:26 -070015#include "Test.h"
Matt Sarett68b8e3d2017-04-28 11:15:22 -040016#include "sk_tool_utils.h"
scroggo19b91532016-10-24 09:03:26 -070017
Ben Wagner501c17c2018-03-12 20:04:31 +000018#include <initializer_list>
scroggo19b91532016-10-24 09:03:26 -070019#include <vector>
20
Leon Scroggins III7d22a332017-04-12 17:01:26 -040021DEF_TEST(Codec_trunc, r) {
Hal Canaryc465d132017-12-08 10:21:31 -050022 sk_sp<SkData> data(GetResourceAsData("images/box.gif"));
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040023 if (!data) {
24 return;
25 }
Mike Reedede7bac2017-07-23 15:30:02 -040026 SkCodec::MakeFromData(SkData::MakeSubset(data.get(), 0, 23))->getFrameInfo();
Leon Scroggins III7d22a332017-04-12 17:01:26 -040027}
28
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040029// 565 does not support alpha, but there is no reason for it not to support an
30// animated image with a frame that has alpha but then blends onto an opaque
31// frame making the result opaque. Test that we can decode such a frame.
32DEF_TEST(Codec_565, r) {
Hal Canaryc465d132017-12-08 10:21:31 -050033 sk_sp<SkData> data(GetResourceAsData("images/blendBG.webp"));
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040034 if (!data) {
35 return;
36 }
Mike Reedede7bac2017-07-23 15:30:02 -040037 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(std::move(data)));
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040038 auto info = codec->getInfo().makeColorType(kRGB_565_SkColorType);
39 SkBitmap bm;
40 bm.allocPixels(info);
41
42 SkCodec::Options options;
43 options.fFrameIndex = 1;
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040044 options.fPriorFrame = SkCodec::kNone;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040045
46 const auto result = codec->getPixels(info, bm.getPixels(), bm.rowBytes(),
Leon Scroggins571b30f2017-07-11 17:35:31 +000047 &options);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040048 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
49}
50
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040051static bool restore_previous(const SkCodec::FrameInfo& info) {
52 return info.fDisposalMethod == SkCodecAnimation::DisposalMethod::kRestorePrevious;
53}
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040054
scroggo19b91532016-10-24 09:03:26 -070055DEF_TEST(Codec_frames, r) {
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -050056 #define kOpaque kOpaque_SkAlphaType
57 #define kUnpremul kUnpremul_SkAlphaType
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040058 #define kKeep SkCodecAnimation::DisposalMethod::kKeep
59 #define kRestoreBG SkCodecAnimation::DisposalMethod::kRestoreBGColor
60 #define kRestorePrev SkCodecAnimation::DisposalMethod::kRestorePrevious
scroggo19b91532016-10-24 09:03:26 -070061 static const struct {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040062 const char* fName;
63 int fFrameCount;
scroggo19b91532016-10-24 09:03:26 -070064 // One less than fFramecount, since the first frame is always
65 // independent.
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040066 std::vector<int> fRequiredFrames;
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -050067 // Same, since the first frame should match getInfo
68 std::vector<SkAlphaType> fAlphas;
scroggo19b91532016-10-24 09:03:26 -070069 // The size of this one should match fFrameCount for animated, empty
70 // otherwise.
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040071 std::vector<int> fDurations;
72 int fRepetitionCount;
73 std::vector<SkCodecAnimation::DisposalMethod> fDisposalMethods;
scroggo19b91532016-10-24 09:03:26 -070074 } gRecs[] = {
Hal Canaryc465d132017-12-08 10:21:31 -050075 { "images/required.gif", 7,
Chris Blume0b5e7d12017-09-27 13:23:41 -070076 { 0, 1, 2, 3, 4, 5 },
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -050077 { kOpaque, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040078 { 100, 100, 100, 100, 100, 100, 100 },
79 0,
80 { kKeep, kRestoreBG, kKeep, kKeep, kKeep, kRestoreBG, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -050081 { "images/alphabetAnim.gif", 13,
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040082 { SkCodec::kNone, 0, 0, 0, 0, 5, 6, SkCodec::kNone,
Chris Blume0b5e7d12017-09-27 13:23:41 -070083 SkCodec::kNone, 9, 10, 11 },
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -050084 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
85 kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040086 { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040087 0,
88 { kKeep, kRestorePrev, kRestorePrev, kRestorePrev, kRestorePrev,
89 kRestoreBG, kKeep, kRestoreBG, kRestoreBG, kKeep, kKeep,
90 kRestoreBG, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -050091 { "images/randPixelsAnim2.gif", 4,
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040092 // required frames
93 { 0, 0, 1 },
94 // alphas
95 { kOpaque, kOpaque, kOpaque },
96 // durations
97 { 0, 1000, 170, 40 },
98 // repetition count
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040099 0,
100 { kKeep, kKeep, kRestorePrev, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500101 { "images/randPixelsAnim.gif", 13,
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500102 // required frames
Chris Blume0b5e7d12017-09-27 13:23:41 -0700103 { 0, 1, 2, 3, 4, 3, 6, 7, 7, 7, 9, 9 },
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500104 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
105 kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500106 // durations
107 { 0, 1000, 170, 40, 220, 7770, 90, 90, 90, 90, 90, 90, 90 },
108 // repetition count
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400109 0,
110 { kKeep, kKeep, kKeep, kKeep, kRestoreBG, kRestoreBG, kRestoreBG,
111 kRestoreBG, kRestorePrev, kRestoreBG, kRestorePrev, kRestorePrev,
112 kRestorePrev, } },
Hal Canaryc465d132017-12-08 10:21:31 -0500113 { "images/box.gif", 1, {}, {}, {}, 0, { kKeep } },
114 { "images/color_wheel.gif", 1, {}, {}, {}, 0, { kKeep } },
115 { "images/test640x479.gif", 4, { 0, 1, 2 },
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400116 { kOpaque, kOpaque, kOpaque },
117 { 200, 200, 200, 200 },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400118 SkCodec::kRepetitionCountInfinite,
119 { kKeep, kKeep, kKeep, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500120 { "images/colorTables.gif", 2, { 0 }, { kOpaque }, { 1000, 1000 }, 5,
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400121 { kKeep, kKeep } },
scroggo19b91532016-10-24 09:03:26 -0700122
Hal Canaryc465d132017-12-08 10:21:31 -0500123 { "images/arrow.png", 1, {}, {}, {}, 0, {} },
124 { "images/google_chrome.ico", 1, {}, {}, {}, 0, {} },
125 { "images/brickwork-texture.jpg", 1, {}, {}, {}, 0, {} },
scroggo19b91532016-10-24 09:03:26 -0700126#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
Hal Canaryc465d132017-12-08 10:21:31 -0500127 { "images/dng_with_preview.dng", 1, {}, {}, {}, 0, {} },
scroggo19b91532016-10-24 09:03:26 -0700128#endif
Hal Canaryc465d132017-12-08 10:21:31 -0500129 { "images/mandrill.wbmp", 1, {}, {}, {}, 0, {} },
130 { "images/randPixels.bmp", 1, {}, {}, {}, 0, {} },
131 { "images/yellow_rose.webp", 1, {}, {}, {}, 0, {} },
132 { "images/webp-animated.webp", 3, { 0, 1 }, { kOpaque, kOpaque },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400133 { 1000, 500, 1000 }, SkCodec::kRepetitionCountInfinite,
134 { kKeep, kKeep, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500135 { "images/blendBG.webp", 7, { 0, SkCodec::kNone, SkCodec::kNone, SkCodec::kNone,
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400136 4, 4 },
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400137 { kOpaque, kOpaque, kUnpremul, kOpaque, kUnpremul, kUnpremul },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400138 { 525, 500, 525, 437, 609, 729, 444 }, 7,
139 { kKeep, kKeep, kKeep, kKeep, kKeep, kKeep, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500140 { "images/required.webp", 7,
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400141 { 0, 1, 1, SkCodec::kNone, 4, 4 },
142 { kOpaque, kUnpremul, kUnpremul, kOpaque, kOpaque, kOpaque },
143 { 100, 100, 100, 100, 100, 100, 100 },
144 1,
145 { kKeep, kRestoreBG, kKeep, kKeep, kKeep, kRestoreBG, kKeep } },
scroggo19b91532016-10-24 09:03:26 -0700146 };
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400147 #undef kOpaque
148 #undef kUnpremul
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400149 #undef kKeep
150 #undef kRestorePrev
151 #undef kRestoreBG
scroggo19b91532016-10-24 09:03:26 -0700152
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400153 for (const auto& rec : gRecs) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400154 sk_sp<SkData> data(GetResourceAsData(rec.fName));
155 if (!data) {
scroggo19b91532016-10-24 09:03:26 -0700156 // Useful error statement, but sometimes people run tests without
157 // resources, and they do not want to see these messages.
158 //ERRORF(r, "Missing resources? Could not find '%s'", rec.fName);
159 continue;
160 }
161
Mike Reedede7bac2017-07-23 15:30:02 -0400162 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data));
scroggo19b91532016-10-24 09:03:26 -0700163 if (!codec) {
164 ERRORF(r, "Failed to create an SkCodec from '%s'", rec.fName);
165 continue;
166 }
167
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400168 {
169 SkCodec::FrameInfo frameInfo;
170 REPORTER_ASSERT(r, !codec->getFrameInfo(0, &frameInfo));
171 }
172
scroggoe71b1a12016-11-01 08:28:28 -0700173 const int repetitionCount = codec->getRepetitionCount();
174 if (repetitionCount != rec.fRepetitionCount) {
175 ERRORF(r, "%s repetition count does not match! expected: %i\tactual: %i",
176 rec.fName, rec.fRepetitionCount, repetitionCount);
177 }
178
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400179 const int expected = rec.fFrameCount;
180 if (rec.fRequiredFrames.size() + 1 != static_cast<size_t>(expected)) {
scroggo19b91532016-10-24 09:03:26 -0700181 ERRORF(r, "'%s' has wrong number entries in fRequiredFrames; expected: %i\tactual: %i",
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400182 rec.fName, expected - 1, rec.fRequiredFrames.size());
scroggo19b91532016-10-24 09:03:26 -0700183 continue;
184 }
185
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400186 if (expected > 1) {
187 if (rec.fDurations.size() != static_cast<size_t>(expected)) {
188 ERRORF(r, "'%s' has wrong number entries in fDurations; expected: %i\tactual: %i",
189 rec.fName, expected, rec.fDurations.size());
190 continue;
191 }
192
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400193 if (rec.fAlphas.size() + 1 != static_cast<size_t>(expected)) {
194 ERRORF(r, "'%s' has wrong number entries in fAlphas; expected: %i\tactual: %i",
195 rec.fName, expected - 1, rec.fAlphas.size());
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400196 continue;
197 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400198
199 if (rec.fDisposalMethods.size() != static_cast<size_t>(expected)) {
200 ERRORF(r, "'%s' has wrong number entries in fDisposalMethods; "
201 "expected %i\tactual: %i",
202 rec.fName, expected, rec.fDisposalMethods.size());
203 continue;
204 }
scroggo19b91532016-10-24 09:03:26 -0700205 }
206
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400207 enum class TestMode {
208 kVector,
209 kIndividual,
210 };
211
212 for (auto mode : { TestMode::kVector, TestMode::kIndividual }) {
213 // Re-create the codec to reset state and test parsing.
Mike Reedede7bac2017-07-23 15:30:02 -0400214 codec = SkCodec::MakeFromData(data);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400215
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400216 int frameCount;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400217 std::vector<SkCodec::FrameInfo> frameInfos;
218 switch (mode) {
219 case TestMode::kVector:
220 frameInfos = codec->getFrameInfo();
221 // getFrameInfo returns empty set for non-animated.
222 frameCount = frameInfos.empty() ? 1 : frameInfos.size();
223 break;
224 case TestMode::kIndividual:
225 frameCount = codec->getFrameCount();
226 break;
227 }
228
229 if (frameCount != expected) {
230 ERRORF(r, "'%s' expected frame count: %i\tactual: %i",
231 rec.fName, expected, frameCount);
232 continue;
233 }
234
235 // From here on, we are only concerned with animated images.
236 if (1 == frameCount) {
237 continue;
238 }
239
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400240 for (int i = 0; i < frameCount; i++) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400241 SkCodec::FrameInfo frameInfo;
242 switch (mode) {
243 case TestMode::kVector:
244 frameInfo = frameInfos[i];
245 break;
246 case TestMode::kIndividual:
247 REPORTER_ASSERT(r, codec->getFrameInfo(i, nullptr));
248 REPORTER_ASSERT(r, codec->getFrameInfo(i, &frameInfo));
249 break;
250 }
251
252 if (rec.fDurations[i] != frameInfo.fDuration) {
253 ERRORF(r, "%s frame %i's durations do not match! expected: %i\tactual: %i",
254 rec.fName, i, rec.fDurations[i], frameInfo.fDuration);
255 }
256
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500257 auto to_string = [](SkAlphaType alpha) {
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400258 switch (alpha) {
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500259 case kUnpremul_SkAlphaType:
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400260 return "unpremul";
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500261 case kOpaque_SkAlphaType:
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400262 return "opaque";
263 default:
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400264 SkASSERT(false);
265 return "unknown";
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400266 }
267 };
268
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500269 auto expectedAlpha = 0 == i ? codec->getInfo().alphaType() : rec.fAlphas[i-1];
270 auto alpha = frameInfo.fAlphaType;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400271 if (expectedAlpha != alpha) {
272 ERRORF(r, "%s's frame %i has wrong alpha type! expected: %s\tactual: %s",
273 rec.fName, i, to_string(expectedAlpha), to_string(alpha));
274 }
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400275
276 if (0 == i) {
277 REPORTER_ASSERT(r, frameInfo.fRequiredFrame == SkCodec::kNone);
278 } else if (rec.fRequiredFrames[i-1] != frameInfo.fRequiredFrame) {
279 ERRORF(r, "%s's frame %i has wrong dependency! expected: %i\tactual: %i",
280 rec.fName, i, rec.fRequiredFrames[i-1], frameInfo.fRequiredFrame);
281 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400282
283 REPORTER_ASSERT(r, frameInfo.fDisposalMethod == rec.fDisposalMethods[i]);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400284 }
285
286 if (TestMode::kIndividual == mode) {
287 // No need to test decoding twice.
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400288 continue;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400289 }
290
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400291 // Compare decoding in multiple ways:
292 // - Start from scratch for each frame. |codec| will have to decode the required frame
293 // (and any it depends on) to decode. This is stored in |cachedFrames|.
294 // - Provide the frame that a frame depends on, so |codec| just has to blend.
295 // - Provide a frame after the required frame, which will be covered up by the newest
296 // frame.
297 // All should look the same.
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400298 std::vector<SkBitmap> cachedFrames(frameCount);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400299 const auto info = codec->getInfo().makeColorType(kN32_SkColorType);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400300
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400301 auto decode = [&](SkBitmap* bm, int index, int cachedIndex) {
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400302 auto decodeInfo = info;
303 if (index > 0) {
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500304 decodeInfo = info.makeAlphaType(frameInfos[index].fAlphaType);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400305 }
306 bm->allocPixels(decodeInfo);
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400307 if (cachedIndex != SkCodec::kNone) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400308 // First copy the pixels from the cached frame
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400309 const bool success = sk_tool_utils::copy_to(bm, kN32_SkColorType,
310 cachedFrames[cachedIndex]);
311 REPORTER_ASSERT(r, success);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400312 }
313 SkCodec::Options opts;
314 opts.fFrameIndex = index;
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400315 opts.fPriorFrame = cachedIndex;
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400316 const auto result = codec->getPixels(decodeInfo, bm->getPixels(), bm->rowBytes(),
Leon Scroggins571b30f2017-07-11 17:35:31 +0000317 &opts);
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400318 if (cachedIndex != SkCodec::kNone && restore_previous(frameInfos[cachedIndex])) {
319 if (result == SkCodec::kInvalidParameters) {
320 return true;
321 }
322 ERRORF(r, "Using a kRestorePrevious frame as fPriorFrame should fail");
323 return false;
324 }
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400325 if (result != SkCodec::kSuccess) {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400326 ERRORF(r, "Failed to decode frame %i from %s when providing prior frame %i, "
327 "error %i", index, rec.fName, cachedIndex, result);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400328 }
329 return result == SkCodec::kSuccess;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400330 };
331
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400332 for (int i = 0; i < frameCount; i++) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400333 SkBitmap& cachedFrame = cachedFrames[i];
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400334 if (!decode(&cachedFrame, i, SkCodec::kNone)) {
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400335 continue;
336 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400337 const auto reqFrame = frameInfos[i].fRequiredFrame;
338 if (reqFrame == SkCodec::kNone) {
339 // Nothing to compare against.
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400340 continue;
341 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400342 for (int j = reqFrame; j < i; j++) {
343 SkBitmap frame;
344 if (restore_previous(frameInfos[j])) {
345 (void) decode(&frame, i, j);
346 continue;
347 }
348 if (!decode(&frame, i, j)) {
349 continue;
350 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400351
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400352 // Now verify they're equal.
353 const size_t rowLen = info.bytesPerPixel() * info.width();
354 for (int y = 0; y < info.height(); y++) {
355 const void* cachedAddr = cachedFrame.getAddr(0, y);
356 SkASSERT(cachedAddr != nullptr);
357 const void* addr = frame.getAddr(0, y);
358 SkASSERT(addr != nullptr);
359 const bool lineMatches = memcmp(cachedAddr, addr, rowLen) == 0;
360 if (!lineMatches) {
361 SkString name = SkStringPrintf("cached_%i", i);
362 write_bm(name.c_str(), cachedFrame);
363 name = SkStringPrintf("frame_%i", i);
364 write_bm(name.c_str(), frame);
365 ERRORF(r, "%s's frame %i is different (starting from line %i) when "
366 "providing prior frame %i!", rec.fName, i, y, j);
367 break;
368 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400369 }
370 }
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500371 }
scroggo19b91532016-10-24 09:03:26 -0700372 }
373 }
374}
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500375
376// Verify that a webp image can be animated scaled down. This image has a
377// kRestoreBG frame, so it is an interesting image to test. After decoding that
378// frame, we have to erase its rectangle. The rectangle has to be adjusted
379// based on the scaled size.
380DEF_TEST(AndroidCodec_animated, r) {
381 if (GetResourcePath().isEmpty()) {
382 return;
383 }
384
385 const char* file = "images/required.webp";
386 sk_sp<SkData> data(GetResourceAsData(file));
387 if (!data) {
388 ERRORF(r, "Missing %s", file);
389 return;
390 }
391
392 auto codec = SkAndroidCodec::MakeFromCodec(SkCodec::MakeFromData(std::move(data)));
393 if (!codec) {
394 ERRORF(r, "Failed to decode %s", file);
395 return;
396 }
397
398 auto info = codec->getInfo().makeAlphaType(kPremul_SkAlphaType);
399
400 for (int sampleSize : { 8, 32, 100 }) {
401 auto dimensions = codec->codec()->getScaledDimensions(1.0f / sampleSize);
402 info = info.makeWH(dimensions.width(), dimensions.height());
403 SkBitmap bm;
404 bm.allocPixels(info);
405
406 SkCodec::Options options;
407 for (int i = 0; i < codec->codec()->getFrameCount(); ++i) {
408 SkCodec::FrameInfo frameInfo;
409 REPORTER_ASSERT(r, codec->codec()->getFrameInfo(i, &frameInfo));
410 if (5 == i) {
411 REPORTER_ASSERT(r, frameInfo.fDisposalMethod
412 == SkCodecAnimation::DisposalMethod::kRestoreBGColor);
413 }
414 options.fFrameIndex = i;
415 options.fPriorFrame = i - 1;
416 info = info.makeAlphaType(frameInfo.fAlphaType);
417
418 const auto result = codec->codec()->getPixels(info, bm.getPixels(), bm.rowBytes(),
419 &options);
420 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
421 }
422 }
423}