blob: 355544102bbf0d494e65b3962b6cf1eddc724bba [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
scroggo19b91532016-10-24 09:03:26 -070070DEF_TEST(Codec_frames, r) {
Nigel Tao66bc5242018-08-22 10:56:03 +100071 constexpr int kNoFrame = SkCodec::kNoFrame;
72 constexpr SkAlphaType kOpaque = kOpaque_SkAlphaType;
73 constexpr SkAlphaType kUnpremul = kUnpremul_SkAlphaType;
74 constexpr SkCodecAnimation::DisposalMethod kKeep =
75 SkCodecAnimation::DisposalMethod::kKeep;
76 constexpr SkCodecAnimation::DisposalMethod kRestoreBG =
77 SkCodecAnimation::DisposalMethod::kRestoreBGColor;
78 constexpr SkCodecAnimation::DisposalMethod kRestorePrev =
79 SkCodecAnimation::DisposalMethod::kRestorePrevious;
80
scroggo19b91532016-10-24 09:03:26 -070081 static const struct {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040082 const char* fName;
83 int fFrameCount;
scroggo19b91532016-10-24 09:03:26 -070084 // One less than fFramecount, since the first frame is always
85 // independent.
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040086 std::vector<int> fRequiredFrames;
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -050087 // Same, since the first frame should match getInfo
88 std::vector<SkAlphaType> fAlphas;
scroggo19b91532016-10-24 09:03:26 -070089 // The size of this one should match fFrameCount for animated, empty
90 // otherwise.
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040091 std::vector<int> fDurations;
92 int fRepetitionCount;
93 std::vector<SkCodecAnimation::DisposalMethod> fDisposalMethods;
scroggo19b91532016-10-24 09:03:26 -070094 } gRecs[] = {
Hal Canaryc465d132017-12-08 10:21:31 -050095 { "images/required.gif", 7,
Chris Blume0b5e7d12017-09-27 13:23:41 -070096 { 0, 1, 2, 3, 4, 5 },
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -050097 { kOpaque, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040098 { 100, 100, 100, 100, 100, 100, 100 },
99 0,
100 { kKeep, kRestoreBG, kKeep, kKeep, kKeep, kRestoreBG, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500101 { "images/alphabetAnim.gif", 13,
Nigel Tao66bc5242018-08-22 10:56:03 +1000102 { kNoFrame, 0, 0, 0, 0, 5, 6, kNoFrame, kNoFrame, 9, 10, 11 },
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500103 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
104 kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400105 { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400106 0,
107 { kKeep, kRestorePrev, kRestorePrev, kRestorePrev, kRestorePrev,
108 kRestoreBG, kKeep, kRestoreBG, kRestoreBG, kKeep, kKeep,
109 kRestoreBG, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500110 { "images/randPixelsAnim2.gif", 4,
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400111 // required frames
112 { 0, 0, 1 },
113 // alphas
114 { kOpaque, kOpaque, kOpaque },
115 // durations
116 { 0, 1000, 170, 40 },
117 // repetition count
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400118 0,
119 { kKeep, kKeep, kRestorePrev, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500120 { "images/randPixelsAnim.gif", 13,
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500121 // required frames
Chris Blume0b5e7d12017-09-27 13:23:41 -0700122 { 0, 1, 2, 3, 4, 3, 6, 7, 7, 7, 9, 9 },
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500123 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
124 kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500125 // durations
126 { 0, 1000, 170, 40, 220, 7770, 90, 90, 90, 90, 90, 90, 90 },
127 // repetition count
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400128 0,
129 { kKeep, kKeep, kKeep, kKeep, kRestoreBG, kRestoreBG, kRestoreBG,
130 kRestoreBG, kRestorePrev, kRestoreBG, kRestorePrev, kRestorePrev,
131 kRestorePrev, } },
Hal Canaryc465d132017-12-08 10:21:31 -0500132 { "images/box.gif", 1, {}, {}, {}, 0, { kKeep } },
133 { "images/color_wheel.gif", 1, {}, {}, {}, 0, { kKeep } },
134 { "images/test640x479.gif", 4, { 0, 1, 2 },
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400135 { kOpaque, kOpaque, kOpaque },
136 { 200, 200, 200, 200 },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400137 SkCodec::kRepetitionCountInfinite,
138 { kKeep, kKeep, kKeep, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500139 { "images/colorTables.gif", 2, { 0 }, { kOpaque }, { 1000, 1000 }, 5,
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400140 { kKeep, kKeep } },
scroggo19b91532016-10-24 09:03:26 -0700141
Hal Canaryc465d132017-12-08 10:21:31 -0500142 { "images/arrow.png", 1, {}, {}, {}, 0, {} },
143 { "images/google_chrome.ico", 1, {}, {}, {}, 0, {} },
144 { "images/brickwork-texture.jpg", 1, {}, {}, {}, 0, {} },
scroggo19b91532016-10-24 09:03:26 -0700145#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
Hal Canaryc465d132017-12-08 10:21:31 -0500146 { "images/dng_with_preview.dng", 1, {}, {}, {}, 0, {} },
scroggo19b91532016-10-24 09:03:26 -0700147#endif
Hal Canaryc465d132017-12-08 10:21:31 -0500148 { "images/mandrill.wbmp", 1, {}, {}, {}, 0, {} },
149 { "images/randPixels.bmp", 1, {}, {}, {}, 0, {} },
150 { "images/yellow_rose.webp", 1, {}, {}, {}, 0, {} },
151 { "images/webp-animated.webp", 3, { 0, 1 }, { kOpaque, kOpaque },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400152 { 1000, 500, 1000 }, SkCodec::kRepetitionCountInfinite,
153 { kKeep, kKeep, kKeep } },
Nigel Tao66bc5242018-08-22 10:56:03 +1000154 { "images/blendBG.webp", 7,
155 { 0, kNoFrame, kNoFrame, kNoFrame, 4, 4 },
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400156 { kOpaque, kOpaque, kUnpremul, kOpaque, kUnpremul, kUnpremul },
Leon Scroggins IIIae834f572019-12-10 14:24:18 -0500157 { 525, 500, 525, 437, 609, 729, 444 },
158#ifdef SK_LEGACY_WEBP_LOOP_COUNT
159 7,
160#else
161 6,
162#endif
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400163 { kKeep, kKeep, kKeep, kKeep, kKeep, kKeep, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500164 { "images/required.webp", 7,
Nigel Tao66bc5242018-08-22 10:56:03 +1000165 { 0, 1, 1, kNoFrame, 4, 4 },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400166 { kOpaque, kUnpremul, kUnpremul, kOpaque, kOpaque, kOpaque },
167 { 100, 100, 100, 100, 100, 100, 100 },
Leon Scroggins IIIae834f572019-12-10 14:24:18 -0500168#ifdef SK_LEGACY_WEBP_LOOP_COUNT
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400169 1,
Leon Scroggins IIIae834f572019-12-10 14:24:18 -0500170#else
171 0,
172#endif
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400173 { kKeep, kRestoreBG, kKeep, kKeep, kKeep, kRestoreBG, kKeep } },
scroggo19b91532016-10-24 09:03:26 -0700174 };
175
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400176 for (const auto& rec : gRecs) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400177 sk_sp<SkData> data(GetResourceAsData(rec.fName));
178 if (!data) {
scroggo19b91532016-10-24 09:03:26 -0700179 // Useful error statement, but sometimes people run tests without
180 // resources, and they do not want to see these messages.
181 //ERRORF(r, "Missing resources? Could not find '%s'", rec.fName);
182 continue;
183 }
184
Mike Reedede7bac2017-07-23 15:30:02 -0400185 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data));
scroggo19b91532016-10-24 09:03:26 -0700186 if (!codec) {
187 ERRORF(r, "Failed to create an SkCodec from '%s'", rec.fName);
188 continue;
189 }
190
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400191 {
192 SkCodec::FrameInfo frameInfo;
193 REPORTER_ASSERT(r, !codec->getFrameInfo(0, &frameInfo));
194 }
195
scroggoe71b1a12016-11-01 08:28:28 -0700196 const int repetitionCount = codec->getRepetitionCount();
197 if (repetitionCount != rec.fRepetitionCount) {
198 ERRORF(r, "%s repetition count does not match! expected: %i\tactual: %i",
199 rec.fName, rec.fRepetitionCount, repetitionCount);
200 }
201
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400202 const int expected = rec.fFrameCount;
203 if (rec.fRequiredFrames.size() + 1 != static_cast<size_t>(expected)) {
scroggo19b91532016-10-24 09:03:26 -0700204 ERRORF(r, "'%s' has wrong number entries in fRequiredFrames; expected: %i\tactual: %i",
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400205 rec.fName, expected - 1, rec.fRequiredFrames.size());
scroggo19b91532016-10-24 09:03:26 -0700206 continue;
207 }
208
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400209 if (expected > 1) {
210 if (rec.fDurations.size() != static_cast<size_t>(expected)) {
211 ERRORF(r, "'%s' has wrong number entries in fDurations; expected: %i\tactual: %i",
212 rec.fName, expected, rec.fDurations.size());
213 continue;
214 }
215
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400216 if (rec.fAlphas.size() + 1 != static_cast<size_t>(expected)) {
217 ERRORF(r, "'%s' has wrong number entries in fAlphas; expected: %i\tactual: %i",
218 rec.fName, expected - 1, rec.fAlphas.size());
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400219 continue;
220 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400221
222 if (rec.fDisposalMethods.size() != static_cast<size_t>(expected)) {
223 ERRORF(r, "'%s' has wrong number entries in fDisposalMethods; "
224 "expected %i\tactual: %i",
225 rec.fName, expected, rec.fDisposalMethods.size());
226 continue;
227 }
scroggo19b91532016-10-24 09:03:26 -0700228 }
229
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400230 enum class TestMode {
231 kVector,
232 kIndividual,
233 };
234
235 for (auto mode : { TestMode::kVector, TestMode::kIndividual }) {
236 // Re-create the codec to reset state and test parsing.
Mike Reedede7bac2017-07-23 15:30:02 -0400237 codec = SkCodec::MakeFromData(data);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400238
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400239 int frameCount;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400240 std::vector<SkCodec::FrameInfo> frameInfos;
241 switch (mode) {
242 case TestMode::kVector:
243 frameInfos = codec->getFrameInfo();
244 // getFrameInfo returns empty set for non-animated.
245 frameCount = frameInfos.empty() ? 1 : frameInfos.size();
246 break;
247 case TestMode::kIndividual:
248 frameCount = codec->getFrameCount();
249 break;
250 }
251
252 if (frameCount != expected) {
253 ERRORF(r, "'%s' expected frame count: %i\tactual: %i",
254 rec.fName, expected, frameCount);
255 continue;
256 }
257
258 // From here on, we are only concerned with animated images.
259 if (1 == frameCount) {
260 continue;
261 }
262
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400263 for (int i = 0; i < frameCount; i++) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400264 SkCodec::FrameInfo frameInfo;
265 switch (mode) {
266 case TestMode::kVector:
267 frameInfo = frameInfos[i];
268 break;
269 case TestMode::kIndividual:
270 REPORTER_ASSERT(r, codec->getFrameInfo(i, nullptr));
271 REPORTER_ASSERT(r, codec->getFrameInfo(i, &frameInfo));
272 break;
273 }
274
275 if (rec.fDurations[i] != frameInfo.fDuration) {
276 ERRORF(r, "%s frame %i's durations do not match! expected: %i\tactual: %i",
277 rec.fName, i, rec.fDurations[i], frameInfo.fDuration);
278 }
279
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500280 auto to_string = [](SkAlphaType alpha) {
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400281 switch (alpha) {
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500282 case kUnpremul_SkAlphaType:
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400283 return "unpremul";
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500284 case kOpaque_SkAlphaType:
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400285 return "opaque";
286 default:
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400287 SkASSERT(false);
288 return "unknown";
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400289 }
290 };
291
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500292 auto expectedAlpha = 0 == i ? codec->getInfo().alphaType() : rec.fAlphas[i-1];
293 auto alpha = frameInfo.fAlphaType;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400294 if (expectedAlpha != alpha) {
295 ERRORF(r, "%s's frame %i has wrong alpha type! expected: %s\tactual: %s",
296 rec.fName, i, to_string(expectedAlpha), to_string(alpha));
297 }
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400298
299 if (0 == i) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000300 REPORTER_ASSERT(r, frameInfo.fRequiredFrame == SkCodec::kNoFrame);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400301 } else if (rec.fRequiredFrames[i-1] != frameInfo.fRequiredFrame) {
302 ERRORF(r, "%s's frame %i has wrong dependency! expected: %i\tactual: %i",
303 rec.fName, i, rec.fRequiredFrames[i-1], frameInfo.fRequiredFrame);
304 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400305
306 REPORTER_ASSERT(r, frameInfo.fDisposalMethod == rec.fDisposalMethods[i]);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400307 }
308
309 if (TestMode::kIndividual == mode) {
310 // No need to test decoding twice.
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400311 continue;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400312 }
313
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400314 // Compare decoding in multiple ways:
315 // - Start from scratch for each frame. |codec| will have to decode the required frame
316 // (and any it depends on) to decode. This is stored in |cachedFrames|.
317 // - Provide the frame that a frame depends on, so |codec| just has to blend.
318 // - Provide a frame after the required frame, which will be covered up by the newest
319 // frame.
320 // All should look the same.
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400321 std::vector<SkBitmap> cachedFrames(frameCount);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400322 const auto info = codec->getInfo().makeColorType(kN32_SkColorType);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400323
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400324 auto decode = [&](SkBitmap* bm, int index, int cachedIndex) {
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400325 auto decodeInfo = info;
326 if (index > 0) {
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500327 decodeInfo = info.makeAlphaType(frameInfos[index].fAlphaType);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400328 }
329 bm->allocPixels(decodeInfo);
Nigel Tao66bc5242018-08-22 10:56:03 +1000330 if (cachedIndex != SkCodec::kNoFrame) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400331 // First copy the pixels from the cached frame
Mike Kleinea3f0142019-03-20 11:12:10 -0500332 const bool success =
333 ToolUtils::copy_to(bm, kN32_SkColorType, cachedFrames[cachedIndex]);
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400334 REPORTER_ASSERT(r, success);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400335 }
336 SkCodec::Options opts;
337 opts.fFrameIndex = index;
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400338 opts.fPriorFrame = cachedIndex;
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400339 const auto result = codec->getPixels(decodeInfo, bm->getPixels(), bm->rowBytes(),
Leon Scroggins571b30f2017-07-11 17:35:31 +0000340 &opts);
Nigel Tao66bc5242018-08-22 10:56:03 +1000341 if (cachedIndex != SkCodec::kNoFrame &&
342 restore_previous(frameInfos[cachedIndex])) {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400343 if (result == SkCodec::kInvalidParameters) {
344 return true;
345 }
346 ERRORF(r, "Using a kRestorePrevious frame as fPriorFrame should fail");
347 return false;
348 }
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400349 if (result != SkCodec::kSuccess) {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400350 ERRORF(r, "Failed to decode frame %i from %s when providing prior frame %i, "
351 "error %i", index, rec.fName, cachedIndex, result);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400352 }
353 return result == SkCodec::kSuccess;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400354 };
355
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400356 for (int i = 0; i < frameCount; i++) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400357 SkBitmap& cachedFrame = cachedFrames[i];
Nigel Tao66bc5242018-08-22 10:56:03 +1000358 if (!decode(&cachedFrame, i, SkCodec::kNoFrame)) {
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400359 continue;
360 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400361 const auto reqFrame = frameInfos[i].fRequiredFrame;
Nigel Tao66bc5242018-08-22 10:56:03 +1000362 if (reqFrame == SkCodec::kNoFrame) {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400363 // Nothing to compare against.
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400364 continue;
365 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400366 for (int j = reqFrame; j < i; j++) {
367 SkBitmap frame;
368 if (restore_previous(frameInfos[j])) {
369 (void) decode(&frame, i, j);
370 continue;
371 }
372 if (!decode(&frame, i, j)) {
373 continue;
374 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400375
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400376 // Now verify they're equal.
377 const size_t rowLen = info.bytesPerPixel() * info.width();
378 for (int y = 0; y < info.height(); y++) {
379 const void* cachedAddr = cachedFrame.getAddr(0, y);
380 SkASSERT(cachedAddr != nullptr);
381 const void* addr = frame.getAddr(0, y);
382 SkASSERT(addr != nullptr);
383 const bool lineMatches = memcmp(cachedAddr, addr, rowLen) == 0;
384 if (!lineMatches) {
385 SkString name = SkStringPrintf("cached_%i", i);
386 write_bm(name.c_str(), cachedFrame);
387 name = SkStringPrintf("frame_%i", i);
388 write_bm(name.c_str(), frame);
389 ERRORF(r, "%s's frame %i is different (starting from line %i) when "
390 "providing prior frame %i!", rec.fName, i, y, j);
391 break;
392 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400393 }
394 }
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500395 }
scroggo19b91532016-10-24 09:03:26 -0700396 }
397 }
398}
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500399
400// Verify that a webp image can be animated scaled down. This image has a
401// kRestoreBG frame, so it is an interesting image to test. After decoding that
402// frame, we have to erase its rectangle. The rectangle has to be adjusted
403// based on the scaled size.
404DEF_TEST(AndroidCodec_animated, r) {
405 if (GetResourcePath().isEmpty()) {
406 return;
407 }
408
409 const char* file = "images/required.webp";
410 sk_sp<SkData> data(GetResourceAsData(file));
411 if (!data) {
412 ERRORF(r, "Missing %s", file);
413 return;
414 }
415
416 auto codec = SkAndroidCodec::MakeFromCodec(SkCodec::MakeFromData(std::move(data)));
417 if (!codec) {
418 ERRORF(r, "Failed to decode %s", file);
419 return;
420 }
421
422 auto info = codec->getInfo().makeAlphaType(kPremul_SkAlphaType);
423
424 for (int sampleSize : { 8, 32, 100 }) {
425 auto dimensions = codec->codec()->getScaledDimensions(1.0f / sampleSize);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400426 info = info.makeDimensions(dimensions);
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500427 SkBitmap bm;
428 bm.allocPixels(info);
429
430 SkCodec::Options options;
431 for (int i = 0; i < codec->codec()->getFrameCount(); ++i) {
432 SkCodec::FrameInfo frameInfo;
433 REPORTER_ASSERT(r, codec->codec()->getFrameInfo(i, &frameInfo));
434 if (5 == i) {
435 REPORTER_ASSERT(r, frameInfo.fDisposalMethod
436 == SkCodecAnimation::DisposalMethod::kRestoreBGColor);
437 }
438 options.fFrameIndex = i;
439 options.fPriorFrame = i - 1;
440 info = info.makeAlphaType(frameInfo.fAlphaType);
441
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400442 auto result = codec->codec()->getPixels(info, bm.getPixels(), bm.rowBytes(),
443 &options);
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500444 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400445
446 // Now compare to not using prior frame.
447 SkBitmap bm2;
448 bm2.allocPixels(info);
449
Nigel Tao66bc5242018-08-22 10:56:03 +1000450 options.fPriorFrame = SkCodec::kNoFrame;
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400451 result = codec->codec()->getPixels(info, bm2.getPixels(), bm2.rowBytes(),
452 &options);
453 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
454
455 for (int y = 0; y < info.height(); ++y) {
456 if (memcmp(bm.getAddr32(0, y), bm2.getAddr32(0, y), info.minRowBytes())) {
457 ERRORF(r, "pixel mismatch for sample size %i, frame %i resulting in "
458 "dimensions %i x %i line %i\n",
459 sampleSize, i, info.width(), info.height(), y);
460 break;
461 }
462 }
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500463 }
464 }
465}
Florin Malitaf8776c22018-11-06 19:16:28 -0500466
467DEF_TEST(AnimCodecPlayer, r) {
468 static constexpr struct {
469 const char* fFile;
470 uint32_t fDuration;
471 SkISize fSize;
472 } gTests[] = {
473 { "images/alphabetAnim.gif", 1300, {100, 100} },
474 { "images/randPixels.gif" , 0, { 8, 8} },
475 { "images/randPixels.jpg" , 0, { 8, 8} },
476 { "images/randPixels.png" , 0, { 8, 8} },
477 };
478
479 for (const auto& test : gTests) {
480 auto codec = SkCodec::MakeFromData(GetResourceAsData(test.fFile));
481 REPORTER_ASSERT(r, codec);
482
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500483 auto player = std::make_unique<SkAnimCodecPlayer>(std::move(codec));
Florin Malitaf8776c22018-11-06 19:16:28 -0500484 if (player->duration() != test.fDuration) {
485 printf("*** %d vs %d\n", player->duration(), test.fDuration);
486 }
487 REPORTER_ASSERT(r, player->duration() == test.fDuration);
488
489 auto f0 = player->getFrame();
490 REPORTER_ASSERT(r, f0);
491 REPORTER_ASSERT(r, f0->bounds().size() == test.fSize);
492
493 player->seek(500);
494 auto f1 = player->getFrame();
495 REPORTER_ASSERT(r, f1);
496 REPORTER_ASSERT(r, f1->bounds().size() == test.fSize);
497 }
498}