blob: f707f1e04c7ddc109366801045ebe05cfa2f2f12 [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"
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -050011#include "SkCommonFlags.h"
12#include "SkImageEncoder.h"
13#include "SkOSPath.h"
scroggo19b91532016-10-24 09:03:26 -070014#include "SkStream.h"
15
16#include "Resources.h"
17#include "Test.h"
Matt Sarett68b8e3d2017-04-28 11:15:22 -040018#include "sk_tool_utils.h"
scroggo19b91532016-10-24 09:03:26 -070019
20#include <initializer_list>
21#include <vector>
22
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -050023static void write_bm(const char* name, const SkBitmap& bm) {
24 if (FLAGS_writePath.isEmpty()) {
25 return;
26 }
27
28 SkString filename = SkOSPath::Join(FLAGS_writePath[0], name);
29 filename.appendf(".png");
30 SkFILEWStream file(filename.c_str());
31 if (!SkEncodeImage(&file, bm, SkEncodedImageFormat::kPNG, 100)) {
32 SkDebugf("failed to write '%s'\n", filename.c_str());
33 }
34}
35
Leon Scroggins III7d22a332017-04-12 17:01:26 -040036DEF_TEST(Codec_trunc, r) {
Hal Canaryc465d132017-12-08 10:21:31 -050037 sk_sp<SkData> data(GetResourceAsData("images/box.gif"));
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040038 if (!data) {
39 return;
40 }
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;
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040059 options.fPriorFrame = SkCodec::kNone;
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
scroggo19b91532016-10-24 09:03:26 -070070DEF_TEST(Codec_frames, r) {
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -050071 #define kOpaque kOpaque_SkAlphaType
72 #define kUnpremul kUnpremul_SkAlphaType
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040073 #define kKeep SkCodecAnimation::DisposalMethod::kKeep
74 #define kRestoreBG SkCodecAnimation::DisposalMethod::kRestoreBGColor
75 #define kRestorePrev SkCodecAnimation::DisposalMethod::kRestorePrevious
scroggo19b91532016-10-24 09:03:26 -070076 static const struct {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040077 const char* fName;
78 int fFrameCount;
scroggo19b91532016-10-24 09:03:26 -070079 // One less than fFramecount, since the first frame is always
80 // independent.
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040081 std::vector<int> fRequiredFrames;
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -050082 // Same, since the first frame should match getInfo
83 std::vector<SkAlphaType> fAlphas;
scroggo19b91532016-10-24 09:03:26 -070084 // The size of this one should match fFrameCount for animated, empty
85 // otherwise.
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040086 std::vector<int> fDurations;
87 int fRepetitionCount;
88 std::vector<SkCodecAnimation::DisposalMethod> fDisposalMethods;
scroggo19b91532016-10-24 09:03:26 -070089 } gRecs[] = {
Hal Canaryc465d132017-12-08 10:21:31 -050090 { "images/required.gif", 7,
Chris Blume0b5e7d12017-09-27 13:23:41 -070091 { 0, 1, 2, 3, 4, 5 },
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -050092 { kOpaque, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040093 { 100, 100, 100, 100, 100, 100, 100 },
94 0,
95 { kKeep, kRestoreBG, kKeep, kKeep, kKeep, kRestoreBG, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -050096 { "images/alphabetAnim.gif", 13,
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040097 { SkCodec::kNone, 0, 0, 0, 0, 5, 6, SkCodec::kNone,
Chris Blume0b5e7d12017-09-27 13:23:41 -070098 SkCodec::kNone, 9, 10, 11 },
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -050099 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
100 kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400101 { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400102 0,
103 { kKeep, kRestorePrev, kRestorePrev, kRestorePrev, kRestorePrev,
104 kRestoreBG, kKeep, kRestoreBG, kRestoreBG, kKeep, kKeep,
105 kRestoreBG, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500106 { "images/randPixelsAnim2.gif", 4,
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400107 // required frames
108 { 0, 0, 1 },
109 // alphas
110 { kOpaque, kOpaque, kOpaque },
111 // durations
112 { 0, 1000, 170, 40 },
113 // repetition count
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400114 0,
115 { kKeep, kKeep, kRestorePrev, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500116 { "images/randPixelsAnim.gif", 13,
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500117 // required frames
Chris Blume0b5e7d12017-09-27 13:23:41 -0700118 { 0, 1, 2, 3, 4, 3, 6, 7, 7, 7, 9, 9 },
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500119 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
120 kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500121 // durations
122 { 0, 1000, 170, 40, 220, 7770, 90, 90, 90, 90, 90, 90, 90 },
123 // repetition count
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400124 0,
125 { kKeep, kKeep, kKeep, kKeep, kRestoreBG, kRestoreBG, kRestoreBG,
126 kRestoreBG, kRestorePrev, kRestoreBG, kRestorePrev, kRestorePrev,
127 kRestorePrev, } },
Hal Canaryc465d132017-12-08 10:21:31 -0500128 { "images/box.gif", 1, {}, {}, {}, 0, { kKeep } },
129 { "images/color_wheel.gif", 1, {}, {}, {}, 0, { kKeep } },
130 { "images/test640x479.gif", 4, { 0, 1, 2 },
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400131 { kOpaque, kOpaque, kOpaque },
132 { 200, 200, 200, 200 },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400133 SkCodec::kRepetitionCountInfinite,
134 { kKeep, kKeep, kKeep, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500135 { "images/colorTables.gif", 2, { 0 }, { kOpaque }, { 1000, 1000 }, 5,
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400136 { kKeep, kKeep } },
scroggo19b91532016-10-24 09:03:26 -0700137
Hal Canaryc465d132017-12-08 10:21:31 -0500138 { "images/arrow.png", 1, {}, {}, {}, 0, {} },
139 { "images/google_chrome.ico", 1, {}, {}, {}, 0, {} },
140 { "images/brickwork-texture.jpg", 1, {}, {}, {}, 0, {} },
scroggo19b91532016-10-24 09:03:26 -0700141#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
Hal Canaryc465d132017-12-08 10:21:31 -0500142 { "images/dng_with_preview.dng", 1, {}, {}, {}, 0, {} },
scroggo19b91532016-10-24 09:03:26 -0700143#endif
Hal Canaryc465d132017-12-08 10:21:31 -0500144 { "images/mandrill.wbmp", 1, {}, {}, {}, 0, {} },
145 { "images/randPixels.bmp", 1, {}, {}, {}, 0, {} },
146 { "images/yellow_rose.webp", 1, {}, {}, {}, 0, {} },
147 { "images/webp-animated.webp", 3, { 0, 1 }, { kOpaque, kOpaque },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400148 { 1000, 500, 1000 }, SkCodec::kRepetitionCountInfinite,
149 { kKeep, kKeep, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500150 { "images/blendBG.webp", 7, { 0, SkCodec::kNone, SkCodec::kNone, SkCodec::kNone,
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400151 4, 4 },
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400152 { kOpaque, kOpaque, kUnpremul, kOpaque, kUnpremul, kUnpremul },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400153 { 525, 500, 525, 437, 609, 729, 444 }, 7,
154 { kKeep, kKeep, kKeep, kKeep, kKeep, kKeep, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500155 { "images/required.webp", 7,
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400156 { 0, 1, 1, SkCodec::kNone, 4, 4 },
157 { kOpaque, kUnpremul, kUnpremul, kOpaque, kOpaque, kOpaque },
158 { 100, 100, 100, 100, 100, 100, 100 },
159 1,
160 { kKeep, kRestoreBG, kKeep, kKeep, kKeep, kRestoreBG, kKeep } },
scroggo19b91532016-10-24 09:03:26 -0700161 };
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400162 #undef kOpaque
163 #undef kUnpremul
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400164 #undef kKeep
165 #undef kRestorePrev
166 #undef kRestoreBG
scroggo19b91532016-10-24 09:03:26 -0700167
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400168 for (const auto& rec : gRecs) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400169 sk_sp<SkData> data(GetResourceAsData(rec.fName));
170 if (!data) {
scroggo19b91532016-10-24 09:03:26 -0700171 // Useful error statement, but sometimes people run tests without
172 // resources, and they do not want to see these messages.
173 //ERRORF(r, "Missing resources? Could not find '%s'", rec.fName);
174 continue;
175 }
176
Mike Reedede7bac2017-07-23 15:30:02 -0400177 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data));
scroggo19b91532016-10-24 09:03:26 -0700178 if (!codec) {
179 ERRORF(r, "Failed to create an SkCodec from '%s'", rec.fName);
180 continue;
181 }
182
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400183 {
184 SkCodec::FrameInfo frameInfo;
185 REPORTER_ASSERT(r, !codec->getFrameInfo(0, &frameInfo));
186 }
187
scroggoe71b1a12016-11-01 08:28:28 -0700188 const int repetitionCount = codec->getRepetitionCount();
189 if (repetitionCount != rec.fRepetitionCount) {
190 ERRORF(r, "%s repetition count does not match! expected: %i\tactual: %i",
191 rec.fName, rec.fRepetitionCount, repetitionCount);
192 }
193
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400194 const int expected = rec.fFrameCount;
195 if (rec.fRequiredFrames.size() + 1 != static_cast<size_t>(expected)) {
scroggo19b91532016-10-24 09:03:26 -0700196 ERRORF(r, "'%s' has wrong number entries in fRequiredFrames; expected: %i\tactual: %i",
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400197 rec.fName, expected - 1, rec.fRequiredFrames.size());
scroggo19b91532016-10-24 09:03:26 -0700198 continue;
199 }
200
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400201 if (expected > 1) {
202 if (rec.fDurations.size() != static_cast<size_t>(expected)) {
203 ERRORF(r, "'%s' has wrong number entries in fDurations; expected: %i\tactual: %i",
204 rec.fName, expected, rec.fDurations.size());
205 continue;
206 }
207
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400208 if (rec.fAlphas.size() + 1 != static_cast<size_t>(expected)) {
209 ERRORF(r, "'%s' has wrong number entries in fAlphas; expected: %i\tactual: %i",
210 rec.fName, expected - 1, rec.fAlphas.size());
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400211 continue;
212 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400213
214 if (rec.fDisposalMethods.size() != static_cast<size_t>(expected)) {
215 ERRORF(r, "'%s' has wrong number entries in fDisposalMethods; "
216 "expected %i\tactual: %i",
217 rec.fName, expected, rec.fDisposalMethods.size());
218 continue;
219 }
scroggo19b91532016-10-24 09:03:26 -0700220 }
221
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400222 enum class TestMode {
223 kVector,
224 kIndividual,
225 };
226
227 for (auto mode : { TestMode::kVector, TestMode::kIndividual }) {
228 // Re-create the codec to reset state and test parsing.
Mike Reedede7bac2017-07-23 15:30:02 -0400229 codec = SkCodec::MakeFromData(data);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400230
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400231 int frameCount;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400232 std::vector<SkCodec::FrameInfo> frameInfos;
233 switch (mode) {
234 case TestMode::kVector:
235 frameInfos = codec->getFrameInfo();
236 // getFrameInfo returns empty set for non-animated.
237 frameCount = frameInfos.empty() ? 1 : frameInfos.size();
238 break;
239 case TestMode::kIndividual:
240 frameCount = codec->getFrameCount();
241 break;
242 }
243
244 if (frameCount != expected) {
245 ERRORF(r, "'%s' expected frame count: %i\tactual: %i",
246 rec.fName, expected, frameCount);
247 continue;
248 }
249
250 // From here on, we are only concerned with animated images.
251 if (1 == frameCount) {
252 continue;
253 }
254
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400255 for (int i = 0; i < frameCount; i++) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400256 SkCodec::FrameInfo frameInfo;
257 switch (mode) {
258 case TestMode::kVector:
259 frameInfo = frameInfos[i];
260 break;
261 case TestMode::kIndividual:
262 REPORTER_ASSERT(r, codec->getFrameInfo(i, nullptr));
263 REPORTER_ASSERT(r, codec->getFrameInfo(i, &frameInfo));
264 break;
265 }
266
267 if (rec.fDurations[i] != frameInfo.fDuration) {
268 ERRORF(r, "%s frame %i's durations do not match! expected: %i\tactual: %i",
269 rec.fName, i, rec.fDurations[i], frameInfo.fDuration);
270 }
271
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500272 auto to_string = [](SkAlphaType alpha) {
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400273 switch (alpha) {
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500274 case kUnpremul_SkAlphaType:
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400275 return "unpremul";
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500276 case kOpaque_SkAlphaType:
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400277 return "opaque";
278 default:
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400279 SkASSERT(false);
280 return "unknown";
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400281 }
282 };
283
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500284 auto expectedAlpha = 0 == i ? codec->getInfo().alphaType() : rec.fAlphas[i-1];
285 auto alpha = frameInfo.fAlphaType;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400286 if (expectedAlpha != alpha) {
287 ERRORF(r, "%s's frame %i has wrong alpha type! expected: %s\tactual: %s",
288 rec.fName, i, to_string(expectedAlpha), to_string(alpha));
289 }
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400290
291 if (0 == i) {
292 REPORTER_ASSERT(r, frameInfo.fRequiredFrame == SkCodec::kNone);
293 } else if (rec.fRequiredFrames[i-1] != frameInfo.fRequiredFrame) {
294 ERRORF(r, "%s's frame %i has wrong dependency! expected: %i\tactual: %i",
295 rec.fName, i, rec.fRequiredFrames[i-1], frameInfo.fRequiredFrame);
296 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400297
298 REPORTER_ASSERT(r, frameInfo.fDisposalMethod == rec.fDisposalMethods[i]);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400299 }
300
301 if (TestMode::kIndividual == mode) {
302 // No need to test decoding twice.
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400303 continue;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400304 }
305
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400306 // Compare decoding in multiple ways:
307 // - Start from scratch for each frame. |codec| will have to decode the required frame
308 // (and any it depends on) to decode. This is stored in |cachedFrames|.
309 // - Provide the frame that a frame depends on, so |codec| just has to blend.
310 // - Provide a frame after the required frame, which will be covered up by the newest
311 // frame.
312 // All should look the same.
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400313 std::vector<SkBitmap> cachedFrames(frameCount);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400314 const auto info = codec->getInfo().makeColorType(kN32_SkColorType);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400315
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400316 auto decode = [&](SkBitmap* bm, int index, int cachedIndex) {
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400317 auto decodeInfo = info;
318 if (index > 0) {
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500319 decodeInfo = info.makeAlphaType(frameInfos[index].fAlphaType);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400320 }
321 bm->allocPixels(decodeInfo);
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400322 if (cachedIndex != SkCodec::kNone) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400323 // First copy the pixels from the cached frame
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400324 const bool success = sk_tool_utils::copy_to(bm, kN32_SkColorType,
325 cachedFrames[cachedIndex]);
326 REPORTER_ASSERT(r, success);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400327 }
328 SkCodec::Options opts;
329 opts.fFrameIndex = index;
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400330 opts.fPriorFrame = cachedIndex;
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400331 const auto result = codec->getPixels(decodeInfo, bm->getPixels(), bm->rowBytes(),
Leon Scroggins571b30f2017-07-11 17:35:31 +0000332 &opts);
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400333 if (cachedIndex != SkCodec::kNone && restore_previous(frameInfos[cachedIndex])) {
334 if (result == SkCodec::kInvalidParameters) {
335 return true;
336 }
337 ERRORF(r, "Using a kRestorePrevious frame as fPriorFrame should fail");
338 return false;
339 }
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400340 if (result != SkCodec::kSuccess) {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400341 ERRORF(r, "Failed to decode frame %i from %s when providing prior frame %i, "
342 "error %i", index, rec.fName, cachedIndex, result);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400343 }
344 return result == SkCodec::kSuccess;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400345 };
346
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400347 for (int i = 0; i < frameCount; i++) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400348 SkBitmap& cachedFrame = cachedFrames[i];
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400349 if (!decode(&cachedFrame, i, SkCodec::kNone)) {
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400350 continue;
351 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400352 const auto reqFrame = frameInfos[i].fRequiredFrame;
353 if (reqFrame == SkCodec::kNone) {
354 // Nothing to compare against.
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400355 continue;
356 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400357 for (int j = reqFrame; j < i; j++) {
358 SkBitmap frame;
359 if (restore_previous(frameInfos[j])) {
360 (void) decode(&frame, i, j);
361 continue;
362 }
363 if (!decode(&frame, i, j)) {
364 continue;
365 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400366
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400367 // Now verify they're equal.
368 const size_t rowLen = info.bytesPerPixel() * info.width();
369 for (int y = 0; y < info.height(); y++) {
370 const void* cachedAddr = cachedFrame.getAddr(0, y);
371 SkASSERT(cachedAddr != nullptr);
372 const void* addr = frame.getAddr(0, y);
373 SkASSERT(addr != nullptr);
374 const bool lineMatches = memcmp(cachedAddr, addr, rowLen) == 0;
375 if (!lineMatches) {
376 SkString name = SkStringPrintf("cached_%i", i);
377 write_bm(name.c_str(), cachedFrame);
378 name = SkStringPrintf("frame_%i", i);
379 write_bm(name.c_str(), frame);
380 ERRORF(r, "%s's frame %i is different (starting from line %i) when "
381 "providing prior frame %i!", rec.fName, i, y, j);
382 break;
383 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400384 }
385 }
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500386 }
scroggo19b91532016-10-24 09:03:26 -0700387 }
388 }
389}
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500390
391// Verify that a webp image can be animated scaled down. This image has a
392// kRestoreBG frame, so it is an interesting image to test. After decoding that
393// frame, we have to erase its rectangle. The rectangle has to be adjusted
394// based on the scaled size.
395DEF_TEST(AndroidCodec_animated, r) {
396 if (GetResourcePath().isEmpty()) {
397 return;
398 }
399
400 const char* file = "images/required.webp";
401 sk_sp<SkData> data(GetResourceAsData(file));
402 if (!data) {
403 ERRORF(r, "Missing %s", file);
404 return;
405 }
406
407 auto codec = SkAndroidCodec::MakeFromCodec(SkCodec::MakeFromData(std::move(data)));
408 if (!codec) {
409 ERRORF(r, "Failed to decode %s", file);
410 return;
411 }
412
413 auto info = codec->getInfo().makeAlphaType(kPremul_SkAlphaType);
414
415 for (int sampleSize : { 8, 32, 100 }) {
416 auto dimensions = codec->codec()->getScaledDimensions(1.0f / sampleSize);
417 info = info.makeWH(dimensions.width(), dimensions.height());
418 SkBitmap bm;
419 bm.allocPixels(info);
420
421 SkCodec::Options options;
422 for (int i = 0; i < codec->codec()->getFrameCount(); ++i) {
423 SkCodec::FrameInfo frameInfo;
424 REPORTER_ASSERT(r, codec->codec()->getFrameInfo(i, &frameInfo));
425 if (5 == i) {
426 REPORTER_ASSERT(r, frameInfo.fDisposalMethod
427 == SkCodecAnimation::DisposalMethod::kRestoreBGColor);
428 }
429 options.fFrameIndex = i;
430 options.fPriorFrame = i - 1;
431 info = info.makeAlphaType(frameInfo.fAlphaType);
432
433 const auto result = codec->codec()->getPixels(info, bm.getPixels(), bm.rowBytes(),
434 &options);
435 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
436 }
437 }
438}