blob: 9f7e1152236e777509730a90d87d187809067e55 [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 },
Leon Scroggins IIIae834f572019-12-10 14:24:18 -0500158 6,
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400159 { kKeep, kKeep, kKeep, kKeep, kKeep, kKeep, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500160 { "images/required.webp", 7,
Nigel Tao66bc5242018-08-22 10:56:03 +1000161 { 0, 1, 1, kNoFrame, 4, 4 },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400162 { kOpaque, kUnpremul, kUnpremul, kOpaque, kOpaque, kOpaque },
163 { 100, 100, 100, 100, 100, 100, 100 },
Leon Scroggins IIIae834f572019-12-10 14:24:18 -0500164 0,
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400165 { kKeep, kRestoreBG, kKeep, kKeep, kKeep, kRestoreBG, kKeep } },
scroggo19b91532016-10-24 09:03:26 -0700166 };
167
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)) {
Adlai Holler684838f2020-05-12 10:41:04 -0400196 ERRORF(r, "'%s' has wrong number entries in fRequiredFrames; expected: %i\tactual: %zu",
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)) {
Adlai Holler684838f2020-05-12 10:41:04 -0400203 ERRORF(r, "'%s' has wrong number entries in fDurations; expected: %i\tactual: %zu",
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400204 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)) {
Adlai Holler684838f2020-05-12 10:41:04 -0400209 ERRORF(r, "'%s' has wrong number entries in fAlphas; expected: %i\tactual: %zu",
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400210 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; "
Adlai Holler684838f2020-05-12 10:41:04 -0400216 "expected %i\tactual: %zu",
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400217 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) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000292 REPORTER_ASSERT(r, frameInfo.fRequiredFrame == SkCodec::kNoFrame);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400293 } 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);
Nigel Tao66bc5242018-08-22 10:56:03 +1000322 if (cachedIndex != SkCodec::kNoFrame) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400323 // First copy the pixels from the cached frame
Mike Kleinea3f0142019-03-20 11:12:10 -0500324 const bool success =
325 ToolUtils::copy_to(bm, kN32_SkColorType, cachedFrames[cachedIndex]);
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400326 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);
Nigel Tao66bc5242018-08-22 10:56:03 +1000333 if (cachedIndex != SkCodec::kNoFrame &&
334 restore_previous(frameInfos[cachedIndex])) {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400335 if (result == SkCodec::kInvalidParameters) {
336 return true;
337 }
338 ERRORF(r, "Using a kRestorePrevious frame as fPriorFrame should fail");
339 return false;
340 }
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400341 if (result != SkCodec::kSuccess) {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400342 ERRORF(r, "Failed to decode frame %i from %s when providing prior frame %i, "
343 "error %i", index, rec.fName, cachedIndex, result);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400344 }
345 return result == SkCodec::kSuccess;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400346 };
347
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400348 for (int i = 0; i < frameCount; i++) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400349 SkBitmap& cachedFrame = cachedFrames[i];
Nigel Tao66bc5242018-08-22 10:56:03 +1000350 if (!decode(&cachedFrame, i, SkCodec::kNoFrame)) {
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400351 continue;
352 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400353 const auto reqFrame = frameInfos[i].fRequiredFrame;
Nigel Tao66bc5242018-08-22 10:56:03 +1000354 if (reqFrame == SkCodec::kNoFrame) {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400355 // Nothing to compare against.
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400356 continue;
357 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400358 for (int j = reqFrame; j < i; j++) {
359 SkBitmap frame;
360 if (restore_previous(frameInfos[j])) {
361 (void) decode(&frame, i, j);
362 continue;
363 }
364 if (!decode(&frame, i, j)) {
365 continue;
366 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400367
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400368 // Now verify they're equal.
369 const size_t rowLen = info.bytesPerPixel() * info.width();
370 for (int y = 0; y < info.height(); y++) {
371 const void* cachedAddr = cachedFrame.getAddr(0, y);
372 SkASSERT(cachedAddr != nullptr);
373 const void* addr = frame.getAddr(0, y);
374 SkASSERT(addr != nullptr);
375 const bool lineMatches = memcmp(cachedAddr, addr, rowLen) == 0;
376 if (!lineMatches) {
377 SkString name = SkStringPrintf("cached_%i", i);
378 write_bm(name.c_str(), cachedFrame);
379 name = SkStringPrintf("frame_%i", i);
380 write_bm(name.c_str(), frame);
381 ERRORF(r, "%s's frame %i is different (starting from line %i) when "
382 "providing prior frame %i!", rec.fName, i, y, j);
383 break;
384 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400385 }
386 }
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500387 }
scroggo19b91532016-10-24 09:03:26 -0700388 }
389 }
390}
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500391
392// Verify that a webp image can be animated scaled down. This image has a
393// kRestoreBG frame, so it is an interesting image to test. After decoding that
394// frame, we have to erase its rectangle. The rectangle has to be adjusted
395// based on the scaled size.
396DEF_TEST(AndroidCodec_animated, r) {
397 if (GetResourcePath().isEmpty()) {
398 return;
399 }
400
401 const char* file = "images/required.webp";
402 sk_sp<SkData> data(GetResourceAsData(file));
403 if (!data) {
404 ERRORF(r, "Missing %s", file);
405 return;
406 }
407
408 auto codec = SkAndroidCodec::MakeFromCodec(SkCodec::MakeFromData(std::move(data)));
409 if (!codec) {
410 ERRORF(r, "Failed to decode %s", file);
411 return;
412 }
413
414 auto info = codec->getInfo().makeAlphaType(kPremul_SkAlphaType);
415
416 for (int sampleSize : { 8, 32, 100 }) {
417 auto dimensions = codec->codec()->getScaledDimensions(1.0f / sampleSize);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400418 info = info.makeDimensions(dimensions);
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500419 SkBitmap bm;
420 bm.allocPixels(info);
421
422 SkCodec::Options options;
423 for (int i = 0; i < codec->codec()->getFrameCount(); ++i) {
424 SkCodec::FrameInfo frameInfo;
425 REPORTER_ASSERT(r, codec->codec()->getFrameInfo(i, &frameInfo));
426 if (5 == i) {
427 REPORTER_ASSERT(r, frameInfo.fDisposalMethod
428 == SkCodecAnimation::DisposalMethod::kRestoreBGColor);
429 }
430 options.fFrameIndex = i;
431 options.fPriorFrame = i - 1;
432 info = info.makeAlphaType(frameInfo.fAlphaType);
433
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400434 auto result = codec->codec()->getPixels(info, bm.getPixels(), bm.rowBytes(),
435 &options);
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500436 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400437
438 // Now compare to not using prior frame.
439 SkBitmap bm2;
440 bm2.allocPixels(info);
441
Nigel Tao66bc5242018-08-22 10:56:03 +1000442 options.fPriorFrame = SkCodec::kNoFrame;
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400443 result = codec->codec()->getPixels(info, bm2.getPixels(), bm2.rowBytes(),
444 &options);
445 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
446
447 for (int y = 0; y < info.height(); ++y) {
448 if (memcmp(bm.getAddr32(0, y), bm2.getAddr32(0, y), info.minRowBytes())) {
449 ERRORF(r, "pixel mismatch for sample size %i, frame %i resulting in "
450 "dimensions %i x %i line %i\n",
451 sampleSize, i, info.width(), info.height(), y);
452 break;
453 }
454 }
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500455 }
456 }
457}
Florin Malitaf8776c22018-11-06 19:16:28 -0500458
459DEF_TEST(AnimCodecPlayer, r) {
460 static constexpr struct {
461 const char* fFile;
462 uint32_t fDuration;
463 SkISize fSize;
464 } gTests[] = {
465 { "images/alphabetAnim.gif", 1300, {100, 100} },
466 { "images/randPixels.gif" , 0, { 8, 8} },
467 { "images/randPixels.jpg" , 0, { 8, 8} },
468 { "images/randPixels.png" , 0, { 8, 8} },
469 };
470
471 for (const auto& test : gTests) {
472 auto codec = SkCodec::MakeFromData(GetResourceAsData(test.fFile));
473 REPORTER_ASSERT(r, codec);
474
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500475 auto player = std::make_unique<SkAnimCodecPlayer>(std::move(codec));
Florin Malitaf8776c22018-11-06 19:16:28 -0500476 if (player->duration() != test.fDuration) {
477 printf("*** %d vs %d\n", player->duration(), test.fDuration);
478 }
479 REPORTER_ASSERT(r, player->duration() == test.fDuration);
480
481 auto f0 = player->getFrame();
482 REPORTER_ASSERT(r, f0);
483 REPORTER_ASSERT(r, f0->bounds().size() == test.fSize);
484
485 player->seek(500);
486 auto f1 = player->getFrame();
487 REPORTER_ASSERT(r, f1);
488 REPORTER_ASSERT(r, f1->bounds().size() == test.fSize);
489 }
490}