blob: d3f385ebdbc794f9d85006ca0aa50ebc9c78db74 [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
Ben Wagnerb607a8f2018-03-12 13:46:21 -04008#include "CodecPriv.h"
9#include "Resources.h"
Leon Scroggins III42ee2842018-01-14 14:46:51 -050010#include "SkAndroidCodec.h"
Florin Malitaf8776c22018-11-06 19:16:28 -050011#include "SkAnimCodecPlayer.h"
scroggo38a2cf52016-10-24 09:56:40 -070012#include "SkBitmap.h"
scroggo19b91532016-10-24 09:03:26 -070013#include "SkCodec.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040014#include "SkCodecAnimation.h"
15#include "SkData.h"
16#include "SkImageInfo.h"
Florin Malitaf8776c22018-11-06 19:16:28 -050017#include "SkMakeUnique.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040018#include "SkRefCnt.h"
19#include "SkSize.h"
20#include "SkString.h"
21#include "SkTypes.h"
scroggo19b91532016-10-24 09:03:26 -070022#include "Test.h"
Matt Sarett68b8e3d2017-04-28 11:15:22 -040023#include "sk_tool_utils.h"
scroggo19b91532016-10-24 09:03:26 -070024
Ben Wagnerb607a8f2018-03-12 13:46:21 -040025#include <cstring>
26#include <memory>
27#include <utility>
scroggo19b91532016-10-24 09:03:26 -070028#include <vector>
29
Leon Scroggins III7d22a332017-04-12 17:01:26 -040030DEF_TEST(Codec_trunc, r) {
Hal Canaryc465d132017-12-08 10:21:31 -050031 sk_sp<SkData> data(GetResourceAsData("images/box.gif"));
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040032 if (!data) {
33 return;
34 }
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040035 // See also Codec_GifTruncated2 in GifTest.cpp for this magic 23.
36 //
37 // TODO: just move this getFrameInfo call to Codec_GifTruncated2?
Mike Reedede7bac2017-07-23 15:30:02 -040038 SkCodec::MakeFromData(SkData::MakeSubset(data.get(), 0, 23))->getFrameInfo();
Leon Scroggins III7d22a332017-04-12 17:01:26 -040039}
40
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040041// 565 does not support alpha, but there is no reason for it not to support an
42// animated image with a frame that has alpha but then blends onto an opaque
43// frame making the result opaque. Test that we can decode such a frame.
44DEF_TEST(Codec_565, r) {
Hal Canaryc465d132017-12-08 10:21:31 -050045 sk_sp<SkData> data(GetResourceAsData("images/blendBG.webp"));
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040046 if (!data) {
47 return;
48 }
Mike Reedede7bac2017-07-23 15:30:02 -040049 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(std::move(data)));
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040050 auto info = codec->getInfo().makeColorType(kRGB_565_SkColorType);
51 SkBitmap bm;
52 bm.allocPixels(info);
53
54 SkCodec::Options options;
55 options.fFrameIndex = 1;
Nigel Tao66bc5242018-08-22 10:56:03 +100056 options.fPriorFrame = SkCodec::kNoFrame;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040057
58 const auto result = codec->getPixels(info, bm.getPixels(), bm.rowBytes(),
Leon Scroggins571b30f2017-07-11 17:35:31 +000059 &options);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040060 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
61}
62
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040063static bool restore_previous(const SkCodec::FrameInfo& info) {
64 return info.fDisposalMethod == SkCodecAnimation::DisposalMethod::kRestorePrevious;
65}
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040066
scroggo19b91532016-10-24 09:03:26 -070067DEF_TEST(Codec_frames, r) {
Nigel Tao66bc5242018-08-22 10:56:03 +100068 constexpr int kNoFrame = SkCodec::kNoFrame;
69 constexpr SkAlphaType kOpaque = kOpaque_SkAlphaType;
70 constexpr SkAlphaType kUnpremul = kUnpremul_SkAlphaType;
71 constexpr SkCodecAnimation::DisposalMethod kKeep =
72 SkCodecAnimation::DisposalMethod::kKeep;
73 constexpr SkCodecAnimation::DisposalMethod kRestoreBG =
74 SkCodecAnimation::DisposalMethod::kRestoreBGColor;
75 constexpr SkCodecAnimation::DisposalMethod kRestorePrev =
76 SkCodecAnimation::DisposalMethod::kRestorePrevious;
77
scroggo19b91532016-10-24 09:03:26 -070078 static const struct {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040079 const char* fName;
80 int fFrameCount;
scroggo19b91532016-10-24 09:03:26 -070081 // One less than fFramecount, since the first frame is always
82 // independent.
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040083 std::vector<int> fRequiredFrames;
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -050084 // Same, since the first frame should match getInfo
85 std::vector<SkAlphaType> fAlphas;
scroggo19b91532016-10-24 09:03:26 -070086 // The size of this one should match fFrameCount for animated, empty
87 // otherwise.
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040088 std::vector<int> fDurations;
89 int fRepetitionCount;
90 std::vector<SkCodecAnimation::DisposalMethod> fDisposalMethods;
scroggo19b91532016-10-24 09:03:26 -070091 } gRecs[] = {
Hal Canaryc465d132017-12-08 10:21:31 -050092 { "images/required.gif", 7,
Chris Blume0b5e7d12017-09-27 13:23:41 -070093 { 0, 1, 2, 3, 4, 5 },
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -050094 { kOpaque, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040095 { 100, 100, 100, 100, 100, 100, 100 },
96 0,
97 { kKeep, kRestoreBG, kKeep, kKeep, kKeep, kRestoreBG, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -050098 { "images/alphabetAnim.gif", 13,
Nigel Tao66bc5242018-08-22 10:56:03 +100099 { kNoFrame, 0, 0, 0, 0, 5, 6, kNoFrame, kNoFrame, 9, 10, 11 },
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500100 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
101 kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400102 { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400103 0,
104 { kKeep, kRestorePrev, kRestorePrev, kRestorePrev, kRestorePrev,
105 kRestoreBG, kKeep, kRestoreBG, kRestoreBG, kKeep, kKeep,
106 kRestoreBG, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500107 { "images/randPixelsAnim2.gif", 4,
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400108 // required frames
109 { 0, 0, 1 },
110 // alphas
111 { kOpaque, kOpaque, kOpaque },
112 // durations
113 { 0, 1000, 170, 40 },
114 // repetition count
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400115 0,
116 { kKeep, kKeep, kRestorePrev, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500117 { "images/randPixelsAnim.gif", 13,
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500118 // required frames
Chris Blume0b5e7d12017-09-27 13:23:41 -0700119 { 0, 1, 2, 3, 4, 3, 6, 7, 7, 7, 9, 9 },
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500120 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
121 kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500122 // durations
123 { 0, 1000, 170, 40, 220, 7770, 90, 90, 90, 90, 90, 90, 90 },
124 // repetition count
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400125 0,
126 { kKeep, kKeep, kKeep, kKeep, kRestoreBG, kRestoreBG, kRestoreBG,
127 kRestoreBG, kRestorePrev, kRestoreBG, kRestorePrev, kRestorePrev,
128 kRestorePrev, } },
Hal Canaryc465d132017-12-08 10:21:31 -0500129 { "images/box.gif", 1, {}, {}, {}, 0, { kKeep } },
130 { "images/color_wheel.gif", 1, {}, {}, {}, 0, { kKeep } },
131 { "images/test640x479.gif", 4, { 0, 1, 2 },
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400132 { kOpaque, kOpaque, kOpaque },
133 { 200, 200, 200, 200 },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400134 SkCodec::kRepetitionCountInfinite,
135 { kKeep, kKeep, kKeep, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500136 { "images/colorTables.gif", 2, { 0 }, { kOpaque }, { 1000, 1000 }, 5,
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400137 { kKeep, kKeep } },
scroggo19b91532016-10-24 09:03:26 -0700138
Hal Canaryc465d132017-12-08 10:21:31 -0500139 { "images/arrow.png", 1, {}, {}, {}, 0, {} },
140 { "images/google_chrome.ico", 1, {}, {}, {}, 0, {} },
141 { "images/brickwork-texture.jpg", 1, {}, {}, {}, 0, {} },
scroggo19b91532016-10-24 09:03:26 -0700142#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
Hal Canaryc465d132017-12-08 10:21:31 -0500143 { "images/dng_with_preview.dng", 1, {}, {}, {}, 0, {} },
scroggo19b91532016-10-24 09:03:26 -0700144#endif
Hal Canaryc465d132017-12-08 10:21:31 -0500145 { "images/mandrill.wbmp", 1, {}, {}, {}, 0, {} },
146 { "images/randPixels.bmp", 1, {}, {}, {}, 0, {} },
147 { "images/yellow_rose.webp", 1, {}, {}, {}, 0, {} },
148 { "images/webp-animated.webp", 3, { 0, 1 }, { kOpaque, kOpaque },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400149 { 1000, 500, 1000 }, SkCodec::kRepetitionCountInfinite,
150 { kKeep, kKeep, kKeep } },
Nigel Tao66bc5242018-08-22 10:56:03 +1000151 { "images/blendBG.webp", 7,
152 { 0, kNoFrame, kNoFrame, kNoFrame, 4, 4 },
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400153 { kOpaque, kOpaque, kUnpremul, kOpaque, kUnpremul, kUnpremul },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400154 { 525, 500, 525, 437, 609, 729, 444 }, 7,
155 { kKeep, kKeep, kKeep, kKeep, kKeep, kKeep, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500156 { "images/required.webp", 7,
Nigel Tao66bc5242018-08-22 10:56:03 +1000157 { 0, 1, 1, kNoFrame, 4, 4 },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400158 { kOpaque, kUnpremul, kUnpremul, kOpaque, kOpaque, kOpaque },
159 { 100, 100, 100, 100, 100, 100, 100 },
160 1,
161 { kKeep, kRestoreBG, kKeep, kKeep, kKeep, kRestoreBG, kKeep } },
scroggo19b91532016-10-24 09:03:26 -0700162 };
163
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400164 for (const auto& rec : gRecs) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400165 sk_sp<SkData> data(GetResourceAsData(rec.fName));
166 if (!data) {
scroggo19b91532016-10-24 09:03:26 -0700167 // Useful error statement, but sometimes people run tests without
168 // resources, and they do not want to see these messages.
169 //ERRORF(r, "Missing resources? Could not find '%s'", rec.fName);
170 continue;
171 }
172
Mike Reedede7bac2017-07-23 15:30:02 -0400173 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data));
scroggo19b91532016-10-24 09:03:26 -0700174 if (!codec) {
175 ERRORF(r, "Failed to create an SkCodec from '%s'", rec.fName);
176 continue;
177 }
178
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400179 {
180 SkCodec::FrameInfo frameInfo;
181 REPORTER_ASSERT(r, !codec->getFrameInfo(0, &frameInfo));
182 }
183
scroggoe71b1a12016-11-01 08:28:28 -0700184 const int repetitionCount = codec->getRepetitionCount();
185 if (repetitionCount != rec.fRepetitionCount) {
186 ERRORF(r, "%s repetition count does not match! expected: %i\tactual: %i",
187 rec.fName, rec.fRepetitionCount, repetitionCount);
188 }
189
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400190 const int expected = rec.fFrameCount;
191 if (rec.fRequiredFrames.size() + 1 != static_cast<size_t>(expected)) {
scroggo19b91532016-10-24 09:03:26 -0700192 ERRORF(r, "'%s' has wrong number entries in fRequiredFrames; expected: %i\tactual: %i",
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400193 rec.fName, expected - 1, rec.fRequiredFrames.size());
scroggo19b91532016-10-24 09:03:26 -0700194 continue;
195 }
196
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400197 if (expected > 1) {
198 if (rec.fDurations.size() != static_cast<size_t>(expected)) {
199 ERRORF(r, "'%s' has wrong number entries in fDurations; expected: %i\tactual: %i",
200 rec.fName, expected, rec.fDurations.size());
201 continue;
202 }
203
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400204 if (rec.fAlphas.size() + 1 != static_cast<size_t>(expected)) {
205 ERRORF(r, "'%s' has wrong number entries in fAlphas; expected: %i\tactual: %i",
206 rec.fName, expected - 1, rec.fAlphas.size());
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400207 continue;
208 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400209
210 if (rec.fDisposalMethods.size() != static_cast<size_t>(expected)) {
211 ERRORF(r, "'%s' has wrong number entries in fDisposalMethods; "
212 "expected %i\tactual: %i",
213 rec.fName, expected, rec.fDisposalMethods.size());
214 continue;
215 }
scroggo19b91532016-10-24 09:03:26 -0700216 }
217
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400218 enum class TestMode {
219 kVector,
220 kIndividual,
221 };
222
223 for (auto mode : { TestMode::kVector, TestMode::kIndividual }) {
224 // Re-create the codec to reset state and test parsing.
Mike Reedede7bac2017-07-23 15:30:02 -0400225 codec = SkCodec::MakeFromData(data);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400226
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400227 int frameCount;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400228 std::vector<SkCodec::FrameInfo> frameInfos;
229 switch (mode) {
230 case TestMode::kVector:
231 frameInfos = codec->getFrameInfo();
232 // getFrameInfo returns empty set for non-animated.
233 frameCount = frameInfos.empty() ? 1 : frameInfos.size();
234 break;
235 case TestMode::kIndividual:
236 frameCount = codec->getFrameCount();
237 break;
238 }
239
240 if (frameCount != expected) {
241 ERRORF(r, "'%s' expected frame count: %i\tactual: %i",
242 rec.fName, expected, frameCount);
243 continue;
244 }
245
246 // From here on, we are only concerned with animated images.
247 if (1 == frameCount) {
248 continue;
249 }
250
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400251 for (int i = 0; i < frameCount; i++) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400252 SkCodec::FrameInfo frameInfo;
253 switch (mode) {
254 case TestMode::kVector:
255 frameInfo = frameInfos[i];
256 break;
257 case TestMode::kIndividual:
258 REPORTER_ASSERT(r, codec->getFrameInfo(i, nullptr));
259 REPORTER_ASSERT(r, codec->getFrameInfo(i, &frameInfo));
260 break;
261 }
262
263 if (rec.fDurations[i] != frameInfo.fDuration) {
264 ERRORF(r, "%s frame %i's durations do not match! expected: %i\tactual: %i",
265 rec.fName, i, rec.fDurations[i], frameInfo.fDuration);
266 }
267
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500268 auto to_string = [](SkAlphaType alpha) {
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400269 switch (alpha) {
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500270 case kUnpremul_SkAlphaType:
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400271 return "unpremul";
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500272 case kOpaque_SkAlphaType:
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400273 return "opaque";
274 default:
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400275 SkASSERT(false);
276 return "unknown";
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400277 }
278 };
279
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500280 auto expectedAlpha = 0 == i ? codec->getInfo().alphaType() : rec.fAlphas[i-1];
281 auto alpha = frameInfo.fAlphaType;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400282 if (expectedAlpha != alpha) {
283 ERRORF(r, "%s's frame %i has wrong alpha type! expected: %s\tactual: %s",
284 rec.fName, i, to_string(expectedAlpha), to_string(alpha));
285 }
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400286
287 if (0 == i) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000288 REPORTER_ASSERT(r, frameInfo.fRequiredFrame == SkCodec::kNoFrame);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400289 } else if (rec.fRequiredFrames[i-1] != frameInfo.fRequiredFrame) {
290 ERRORF(r, "%s's frame %i has wrong dependency! expected: %i\tactual: %i",
291 rec.fName, i, rec.fRequiredFrames[i-1], frameInfo.fRequiredFrame);
292 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400293
294 REPORTER_ASSERT(r, frameInfo.fDisposalMethod == rec.fDisposalMethods[i]);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400295 }
296
297 if (TestMode::kIndividual == mode) {
298 // No need to test decoding twice.
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400299 continue;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400300 }
301
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400302 // Compare decoding in multiple ways:
303 // - Start from scratch for each frame. |codec| will have to decode the required frame
304 // (and any it depends on) to decode. This is stored in |cachedFrames|.
305 // - Provide the frame that a frame depends on, so |codec| just has to blend.
306 // - Provide a frame after the required frame, which will be covered up by the newest
307 // frame.
308 // All should look the same.
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400309 std::vector<SkBitmap> cachedFrames(frameCount);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400310 const auto info = codec->getInfo().makeColorType(kN32_SkColorType);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400311
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400312 auto decode = [&](SkBitmap* bm, int index, int cachedIndex) {
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400313 auto decodeInfo = info;
314 if (index > 0) {
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500315 decodeInfo = info.makeAlphaType(frameInfos[index].fAlphaType);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400316 }
317 bm->allocPixels(decodeInfo);
Nigel Tao66bc5242018-08-22 10:56:03 +1000318 if (cachedIndex != SkCodec::kNoFrame) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400319 // First copy the pixels from the cached frame
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400320 const bool success = sk_tool_utils::copy_to(bm, kN32_SkColorType,
321 cachedFrames[cachedIndex]);
322 REPORTER_ASSERT(r, success);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400323 }
324 SkCodec::Options opts;
325 opts.fFrameIndex = index;
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400326 opts.fPriorFrame = cachedIndex;
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400327 const auto result = codec->getPixels(decodeInfo, bm->getPixels(), bm->rowBytes(),
Leon Scroggins571b30f2017-07-11 17:35:31 +0000328 &opts);
Nigel Tao66bc5242018-08-22 10:56:03 +1000329 if (cachedIndex != SkCodec::kNoFrame &&
330 restore_previous(frameInfos[cachedIndex])) {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400331 if (result == SkCodec::kInvalidParameters) {
332 return true;
333 }
334 ERRORF(r, "Using a kRestorePrevious frame as fPriorFrame should fail");
335 return false;
336 }
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400337 if (result != SkCodec::kSuccess) {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400338 ERRORF(r, "Failed to decode frame %i from %s when providing prior frame %i, "
339 "error %i", index, rec.fName, cachedIndex, result);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400340 }
341 return result == SkCodec::kSuccess;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400342 };
343
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400344 for (int i = 0; i < frameCount; i++) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400345 SkBitmap& cachedFrame = cachedFrames[i];
Nigel Tao66bc5242018-08-22 10:56:03 +1000346 if (!decode(&cachedFrame, i, SkCodec::kNoFrame)) {
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400347 continue;
348 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400349 const auto reqFrame = frameInfos[i].fRequiredFrame;
Nigel Tao66bc5242018-08-22 10:56:03 +1000350 if (reqFrame == SkCodec::kNoFrame) {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400351 // Nothing to compare against.
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400352 continue;
353 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400354 for (int j = reqFrame; j < i; j++) {
355 SkBitmap frame;
356 if (restore_previous(frameInfos[j])) {
357 (void) decode(&frame, i, j);
358 continue;
359 }
360 if (!decode(&frame, i, j)) {
361 continue;
362 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400363
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400364 // Now verify they're equal.
365 const size_t rowLen = info.bytesPerPixel() * info.width();
366 for (int y = 0; y < info.height(); y++) {
367 const void* cachedAddr = cachedFrame.getAddr(0, y);
368 SkASSERT(cachedAddr != nullptr);
369 const void* addr = frame.getAddr(0, y);
370 SkASSERT(addr != nullptr);
371 const bool lineMatches = memcmp(cachedAddr, addr, rowLen) == 0;
372 if (!lineMatches) {
373 SkString name = SkStringPrintf("cached_%i", i);
374 write_bm(name.c_str(), cachedFrame);
375 name = SkStringPrintf("frame_%i", i);
376 write_bm(name.c_str(), frame);
377 ERRORF(r, "%s's frame %i is different (starting from line %i) when "
378 "providing prior frame %i!", rec.fName, i, y, j);
379 break;
380 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400381 }
382 }
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500383 }
scroggo19b91532016-10-24 09:03:26 -0700384 }
385 }
386}
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500387
388// Verify that a webp image can be animated scaled down. This image has a
389// kRestoreBG frame, so it is an interesting image to test. After decoding that
390// frame, we have to erase its rectangle. The rectangle has to be adjusted
391// based on the scaled size.
392DEF_TEST(AndroidCodec_animated, r) {
393 if (GetResourcePath().isEmpty()) {
394 return;
395 }
396
397 const char* file = "images/required.webp";
398 sk_sp<SkData> data(GetResourceAsData(file));
399 if (!data) {
400 ERRORF(r, "Missing %s", file);
401 return;
402 }
403
404 auto codec = SkAndroidCodec::MakeFromCodec(SkCodec::MakeFromData(std::move(data)));
405 if (!codec) {
406 ERRORF(r, "Failed to decode %s", file);
407 return;
408 }
409
410 auto info = codec->getInfo().makeAlphaType(kPremul_SkAlphaType);
411
412 for (int sampleSize : { 8, 32, 100 }) {
413 auto dimensions = codec->codec()->getScaledDimensions(1.0f / sampleSize);
414 info = info.makeWH(dimensions.width(), dimensions.height());
415 SkBitmap bm;
416 bm.allocPixels(info);
417
418 SkCodec::Options options;
419 for (int i = 0; i < codec->codec()->getFrameCount(); ++i) {
420 SkCodec::FrameInfo frameInfo;
421 REPORTER_ASSERT(r, codec->codec()->getFrameInfo(i, &frameInfo));
422 if (5 == i) {
423 REPORTER_ASSERT(r, frameInfo.fDisposalMethod
424 == SkCodecAnimation::DisposalMethod::kRestoreBGColor);
425 }
426 options.fFrameIndex = i;
427 options.fPriorFrame = i - 1;
428 info = info.makeAlphaType(frameInfo.fAlphaType);
429
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400430 auto result = codec->codec()->getPixels(info, bm.getPixels(), bm.rowBytes(),
431 &options);
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500432 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400433
434 // Now compare to not using prior frame.
435 SkBitmap bm2;
436 bm2.allocPixels(info);
437
Nigel Tao66bc5242018-08-22 10:56:03 +1000438 options.fPriorFrame = SkCodec::kNoFrame;
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400439 result = codec->codec()->getPixels(info, bm2.getPixels(), bm2.rowBytes(),
440 &options);
441 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
442
443 for (int y = 0; y < info.height(); ++y) {
444 if (memcmp(bm.getAddr32(0, y), bm2.getAddr32(0, y), info.minRowBytes())) {
445 ERRORF(r, "pixel mismatch for sample size %i, frame %i resulting in "
446 "dimensions %i x %i line %i\n",
447 sampleSize, i, info.width(), info.height(), y);
448 break;
449 }
450 }
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500451 }
452 }
453}
Florin Malitaf8776c22018-11-06 19:16:28 -0500454
455DEF_TEST(AnimCodecPlayer, r) {
456 static constexpr struct {
457 const char* fFile;
458 uint32_t fDuration;
459 SkISize fSize;
460 } gTests[] = {
461 { "images/alphabetAnim.gif", 1300, {100, 100} },
462 { "images/randPixels.gif" , 0, { 8, 8} },
463 { "images/randPixels.jpg" , 0, { 8, 8} },
464 { "images/randPixels.png" , 0, { 8, 8} },
465 };
466
467 for (const auto& test : gTests) {
468 auto codec = SkCodec::MakeFromData(GetResourceAsData(test.fFile));
469 REPORTER_ASSERT(r, codec);
470
471 auto player = skstd::make_unique<SkAnimCodecPlayer>(std::move(codec));
472 if (player->duration() != test.fDuration) {
473 printf("*** %d vs %d\n", player->duration(), test.fDuration);
474 }
475 REPORTER_ASSERT(r, player->duration() == test.fDuration);
476
477 auto f0 = player->getFrame();
478 REPORTER_ASSERT(r, f0);
479 REPORTER_ASSERT(r, f0->bounds().size() == test.fSize);
480
481 player->seek(500);
482 auto f1 = player->getFrame();
483 REPORTER_ASSERT(r, f1);
484 REPORTER_ASSERT(r, f1->bounds().size() == test.fSize);
485 }
486}