blob: 99ed876fbd87ebc5ad7017bd6956d3f0d0a5c533 [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"
scroggo38a2cf52016-10-24 09:56:40 -070011#include "SkBitmap.h"
scroggo19b91532016-10-24 09:03:26 -070012#include "SkCodec.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040013#include "SkCodecAnimation.h"
14#include "SkData.h"
15#include "SkImageInfo.h"
16#include "SkRefCnt.h"
17#include "SkSize.h"
18#include "SkString.h"
19#include "SkTypes.h"
scroggo19b91532016-10-24 09:03:26 -070020#include "Test.h"
Matt Sarett68b8e3d2017-04-28 11:15:22 -040021#include "sk_tool_utils.h"
scroggo19b91532016-10-24 09:03:26 -070022
Ben Wagnerb607a8f2018-03-12 13:46:21 -040023#include <cstring>
24#include <memory>
25#include <utility>
scroggo19b91532016-10-24 09:03:26 -070026#include <vector>
27
Leon Scroggins III7d22a332017-04-12 17:01:26 -040028DEF_TEST(Codec_trunc, r) {
Hal Canaryc465d132017-12-08 10:21:31 -050029 sk_sp<SkData> data(GetResourceAsData("images/box.gif"));
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040030 if (!data) {
31 return;
32 }
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040033 // See also Codec_GifTruncated2 in GifTest.cpp for this magic 23.
34 //
35 // TODO: just move this getFrameInfo call to Codec_GifTruncated2?
Mike Reedede7bac2017-07-23 15:30:02 -040036 SkCodec::MakeFromData(SkData::MakeSubset(data.get(), 0, 23))->getFrameInfo();
Leon Scroggins III7d22a332017-04-12 17:01:26 -040037}
38
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040039// 565 does not support alpha, but there is no reason for it not to support an
40// animated image with a frame that has alpha but then blends onto an opaque
41// frame making the result opaque. Test that we can decode such a frame.
42DEF_TEST(Codec_565, r) {
Hal Canaryc465d132017-12-08 10:21:31 -050043 sk_sp<SkData> data(GetResourceAsData("images/blendBG.webp"));
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040044 if (!data) {
45 return;
46 }
Mike Reedede7bac2017-07-23 15:30:02 -040047 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(std::move(data)));
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040048 auto info = codec->getInfo().makeColorType(kRGB_565_SkColorType);
49 SkBitmap bm;
50 bm.allocPixels(info);
51
52 SkCodec::Options options;
53 options.fFrameIndex = 1;
Nigel Tao66bc5242018-08-22 10:56:03 +100054 options.fPriorFrame = SkCodec::kNoFrame;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040055
56 const auto result = codec->getPixels(info, bm.getPixels(), bm.rowBytes(),
Leon Scroggins571b30f2017-07-11 17:35:31 +000057 &options);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040058 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
59}
60
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040061static bool restore_previous(const SkCodec::FrameInfo& info) {
62 return info.fDisposalMethod == SkCodecAnimation::DisposalMethod::kRestorePrevious;
63}
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040064
scroggo19b91532016-10-24 09:03:26 -070065DEF_TEST(Codec_frames, r) {
Nigel Tao66bc5242018-08-22 10:56:03 +100066 constexpr int kNoFrame = SkCodec::kNoFrame;
67 constexpr SkAlphaType kOpaque = kOpaque_SkAlphaType;
68 constexpr SkAlphaType kUnpremul = kUnpremul_SkAlphaType;
69 constexpr SkCodecAnimation::DisposalMethod kKeep =
70 SkCodecAnimation::DisposalMethod::kKeep;
71 constexpr SkCodecAnimation::DisposalMethod kRestoreBG =
72 SkCodecAnimation::DisposalMethod::kRestoreBGColor;
73 constexpr SkCodecAnimation::DisposalMethod kRestorePrev =
74 SkCodecAnimation::DisposalMethod::kRestorePrevious;
75
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,
Nigel Tao66bc5242018-08-22 10:56:03 +100097 { kNoFrame, 0, 0, 0, 0, 5, 6, kNoFrame, kNoFrame, 9, 10, 11 },
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -050098 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
99 kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400100 { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400101 0,
102 { kKeep, kRestorePrev, kRestorePrev, kRestorePrev, kRestorePrev,
103 kRestoreBG, kKeep, kRestoreBG, kRestoreBG, kKeep, kKeep,
104 kRestoreBG, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500105 { "images/randPixelsAnim2.gif", 4,
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400106 // required frames
107 { 0, 0, 1 },
108 // alphas
109 { kOpaque, kOpaque, kOpaque },
110 // durations
111 { 0, 1000, 170, 40 },
112 // repetition count
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400113 0,
114 { kKeep, kKeep, kRestorePrev, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500115 { "images/randPixelsAnim.gif", 13,
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500116 // required frames
Chris Blume0b5e7d12017-09-27 13:23:41 -0700117 { 0, 1, 2, 3, 4, 3, 6, 7, 7, 7, 9, 9 },
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500118 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
119 kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500120 // durations
121 { 0, 1000, 170, 40, 220, 7770, 90, 90, 90, 90, 90, 90, 90 },
122 // repetition count
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400123 0,
124 { kKeep, kKeep, kKeep, kKeep, kRestoreBG, kRestoreBG, kRestoreBG,
125 kRestoreBG, kRestorePrev, kRestoreBG, kRestorePrev, kRestorePrev,
126 kRestorePrev, } },
Hal Canaryc465d132017-12-08 10:21:31 -0500127 { "images/box.gif", 1, {}, {}, {}, 0, { kKeep } },
128 { "images/color_wheel.gif", 1, {}, {}, {}, 0, { kKeep } },
129 { "images/test640x479.gif", 4, { 0, 1, 2 },
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400130 { kOpaque, kOpaque, kOpaque },
131 { 200, 200, 200, 200 },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400132 SkCodec::kRepetitionCountInfinite,
133 { kKeep, kKeep, kKeep, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500134 { "images/colorTables.gif", 2, { 0 }, { kOpaque }, { 1000, 1000 }, 5,
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400135 { kKeep, kKeep } },
scroggo19b91532016-10-24 09:03:26 -0700136
Hal Canaryc465d132017-12-08 10:21:31 -0500137 { "images/arrow.png", 1, {}, {}, {}, 0, {} },
138 { "images/google_chrome.ico", 1, {}, {}, {}, 0, {} },
139 { "images/brickwork-texture.jpg", 1, {}, {}, {}, 0, {} },
scroggo19b91532016-10-24 09:03:26 -0700140#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
Hal Canaryc465d132017-12-08 10:21:31 -0500141 { "images/dng_with_preview.dng", 1, {}, {}, {}, 0, {} },
scroggo19b91532016-10-24 09:03:26 -0700142#endif
Hal Canaryc465d132017-12-08 10:21:31 -0500143 { "images/mandrill.wbmp", 1, {}, {}, {}, 0, {} },
144 { "images/randPixels.bmp", 1, {}, {}, {}, 0, {} },
145 { "images/yellow_rose.webp", 1, {}, {}, {}, 0, {} },
146 { "images/webp-animated.webp", 3, { 0, 1 }, { kOpaque, kOpaque },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400147 { 1000, 500, 1000 }, SkCodec::kRepetitionCountInfinite,
148 { kKeep, kKeep, kKeep } },
Nigel Tao66bc5242018-08-22 10:56:03 +1000149 { "images/blendBG.webp", 7,
150 { 0, kNoFrame, kNoFrame, kNoFrame, 4, 4 },
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400151 { kOpaque, kOpaque, kUnpremul, kOpaque, kUnpremul, kUnpremul },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400152 { 525, 500, 525, 437, 609, 729, 444 }, 7,
153 { kKeep, kKeep, kKeep, kKeep, kKeep, kKeep, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500154 { "images/required.webp", 7,
Nigel Tao66bc5242018-08-22 10:56:03 +1000155 { 0, 1, 1, kNoFrame, 4, 4 },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400156 { kOpaque, kUnpremul, kUnpremul, kOpaque, kOpaque, kOpaque },
157 { 100, 100, 100, 100, 100, 100, 100 },
158 1,
159 { kKeep, kRestoreBG, kKeep, kKeep, kKeep, kRestoreBG, kKeep } },
scroggo19b91532016-10-24 09:03:26 -0700160 };
161
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400162 for (const auto& rec : gRecs) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400163 sk_sp<SkData> data(GetResourceAsData(rec.fName));
164 if (!data) {
scroggo19b91532016-10-24 09:03:26 -0700165 // Useful error statement, but sometimes people run tests without
166 // resources, and they do not want to see these messages.
167 //ERRORF(r, "Missing resources? Could not find '%s'", rec.fName);
168 continue;
169 }
170
Mike Reedede7bac2017-07-23 15:30:02 -0400171 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data));
scroggo19b91532016-10-24 09:03:26 -0700172 if (!codec) {
173 ERRORF(r, "Failed to create an SkCodec from '%s'", rec.fName);
174 continue;
175 }
176
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400177 {
178 SkCodec::FrameInfo frameInfo;
179 REPORTER_ASSERT(r, !codec->getFrameInfo(0, &frameInfo));
180 }
181
scroggoe71b1a12016-11-01 08:28:28 -0700182 const int repetitionCount = codec->getRepetitionCount();
183 if (repetitionCount != rec.fRepetitionCount) {
184 ERRORF(r, "%s repetition count does not match! expected: %i\tactual: %i",
185 rec.fName, rec.fRepetitionCount, repetitionCount);
186 }
187
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400188 const int expected = rec.fFrameCount;
189 if (rec.fRequiredFrames.size() + 1 != static_cast<size_t>(expected)) {
scroggo19b91532016-10-24 09:03:26 -0700190 ERRORF(r, "'%s' has wrong number entries in fRequiredFrames; expected: %i\tactual: %i",
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400191 rec.fName, expected - 1, rec.fRequiredFrames.size());
scroggo19b91532016-10-24 09:03:26 -0700192 continue;
193 }
194
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400195 if (expected > 1) {
196 if (rec.fDurations.size() != static_cast<size_t>(expected)) {
197 ERRORF(r, "'%s' has wrong number entries in fDurations; expected: %i\tactual: %i",
198 rec.fName, expected, rec.fDurations.size());
199 continue;
200 }
201
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400202 if (rec.fAlphas.size() + 1 != static_cast<size_t>(expected)) {
203 ERRORF(r, "'%s' has wrong number entries in fAlphas; expected: %i\tactual: %i",
204 rec.fName, expected - 1, rec.fAlphas.size());
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400205 continue;
206 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400207
208 if (rec.fDisposalMethods.size() != static_cast<size_t>(expected)) {
209 ERRORF(r, "'%s' has wrong number entries in fDisposalMethods; "
210 "expected %i\tactual: %i",
211 rec.fName, expected, rec.fDisposalMethods.size());
212 continue;
213 }
scroggo19b91532016-10-24 09:03:26 -0700214 }
215
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400216 enum class TestMode {
217 kVector,
218 kIndividual,
219 };
220
221 for (auto mode : { TestMode::kVector, TestMode::kIndividual }) {
222 // Re-create the codec to reset state and test parsing.
Mike Reedede7bac2017-07-23 15:30:02 -0400223 codec = SkCodec::MakeFromData(data);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400224
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400225 int frameCount;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400226 std::vector<SkCodec::FrameInfo> frameInfos;
227 switch (mode) {
228 case TestMode::kVector:
229 frameInfos = codec->getFrameInfo();
230 // getFrameInfo returns empty set for non-animated.
231 frameCount = frameInfos.empty() ? 1 : frameInfos.size();
232 break;
233 case TestMode::kIndividual:
234 frameCount = codec->getFrameCount();
235 break;
236 }
237
238 if (frameCount != expected) {
239 ERRORF(r, "'%s' expected frame count: %i\tactual: %i",
240 rec.fName, expected, frameCount);
241 continue;
242 }
243
244 // From here on, we are only concerned with animated images.
245 if (1 == frameCount) {
246 continue;
247 }
248
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400249 for (int i = 0; i < frameCount; i++) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400250 SkCodec::FrameInfo frameInfo;
251 switch (mode) {
252 case TestMode::kVector:
253 frameInfo = frameInfos[i];
254 break;
255 case TestMode::kIndividual:
256 REPORTER_ASSERT(r, codec->getFrameInfo(i, nullptr));
257 REPORTER_ASSERT(r, codec->getFrameInfo(i, &frameInfo));
258 break;
259 }
260
261 if (rec.fDurations[i] != frameInfo.fDuration) {
262 ERRORF(r, "%s frame %i's durations do not match! expected: %i\tactual: %i",
263 rec.fName, i, rec.fDurations[i], frameInfo.fDuration);
264 }
265
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500266 auto to_string = [](SkAlphaType alpha) {
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400267 switch (alpha) {
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500268 case kUnpremul_SkAlphaType:
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400269 return "unpremul";
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500270 case kOpaque_SkAlphaType:
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400271 return "opaque";
272 default:
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400273 SkASSERT(false);
274 return "unknown";
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400275 }
276 };
277
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500278 auto expectedAlpha = 0 == i ? codec->getInfo().alphaType() : rec.fAlphas[i-1];
279 auto alpha = frameInfo.fAlphaType;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400280 if (expectedAlpha != alpha) {
281 ERRORF(r, "%s's frame %i has wrong alpha type! expected: %s\tactual: %s",
282 rec.fName, i, to_string(expectedAlpha), to_string(alpha));
283 }
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400284
285 if (0 == i) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000286 REPORTER_ASSERT(r, frameInfo.fRequiredFrame == SkCodec::kNoFrame);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400287 } else if (rec.fRequiredFrames[i-1] != frameInfo.fRequiredFrame) {
288 ERRORF(r, "%s's frame %i has wrong dependency! expected: %i\tactual: %i",
289 rec.fName, i, rec.fRequiredFrames[i-1], frameInfo.fRequiredFrame);
290 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400291
292 REPORTER_ASSERT(r, frameInfo.fDisposalMethod == rec.fDisposalMethods[i]);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400293 }
294
295 if (TestMode::kIndividual == mode) {
296 // No need to test decoding twice.
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400297 continue;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400298 }
299
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400300 // Compare decoding in multiple ways:
301 // - Start from scratch for each frame. |codec| will have to decode the required frame
302 // (and any it depends on) to decode. This is stored in |cachedFrames|.
303 // - Provide the frame that a frame depends on, so |codec| just has to blend.
304 // - Provide a frame after the required frame, which will be covered up by the newest
305 // frame.
306 // All should look the same.
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400307 std::vector<SkBitmap> cachedFrames(frameCount);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400308 const auto info = codec->getInfo().makeColorType(kN32_SkColorType);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400309
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400310 auto decode = [&](SkBitmap* bm, int index, int cachedIndex) {
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400311 auto decodeInfo = info;
312 if (index > 0) {
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500313 decodeInfo = info.makeAlphaType(frameInfos[index].fAlphaType);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400314 }
315 bm->allocPixels(decodeInfo);
Nigel Tao66bc5242018-08-22 10:56:03 +1000316 if (cachedIndex != SkCodec::kNoFrame) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400317 // First copy the pixels from the cached frame
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400318 const bool success = sk_tool_utils::copy_to(bm, kN32_SkColorType,
319 cachedFrames[cachedIndex]);
320 REPORTER_ASSERT(r, success);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400321 }
322 SkCodec::Options opts;
323 opts.fFrameIndex = index;
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400324 opts.fPriorFrame = cachedIndex;
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400325 const auto result = codec->getPixels(decodeInfo, bm->getPixels(), bm->rowBytes(),
Leon Scroggins571b30f2017-07-11 17:35:31 +0000326 &opts);
Nigel Tao66bc5242018-08-22 10:56:03 +1000327 if (cachedIndex != SkCodec::kNoFrame &&
328 restore_previous(frameInfos[cachedIndex])) {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400329 if (result == SkCodec::kInvalidParameters) {
330 return true;
331 }
332 ERRORF(r, "Using a kRestorePrevious frame as fPriorFrame should fail");
333 return false;
334 }
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400335 if (result != SkCodec::kSuccess) {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400336 ERRORF(r, "Failed to decode frame %i from %s when providing prior frame %i, "
337 "error %i", index, rec.fName, cachedIndex, result);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400338 }
339 return result == SkCodec::kSuccess;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400340 };
341
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400342 for (int i = 0; i < frameCount; i++) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400343 SkBitmap& cachedFrame = cachedFrames[i];
Nigel Tao66bc5242018-08-22 10:56:03 +1000344 if (!decode(&cachedFrame, i, SkCodec::kNoFrame)) {
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400345 continue;
346 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400347 const auto reqFrame = frameInfos[i].fRequiredFrame;
Nigel Tao66bc5242018-08-22 10:56:03 +1000348 if (reqFrame == SkCodec::kNoFrame) {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400349 // Nothing to compare against.
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400350 continue;
351 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400352 for (int j = reqFrame; j < i; j++) {
353 SkBitmap frame;
354 if (restore_previous(frameInfos[j])) {
355 (void) decode(&frame, i, j);
356 continue;
357 }
358 if (!decode(&frame, i, j)) {
359 continue;
360 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400361
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400362 // Now verify they're equal.
363 const size_t rowLen = info.bytesPerPixel() * info.width();
364 for (int y = 0; y < info.height(); y++) {
365 const void* cachedAddr = cachedFrame.getAddr(0, y);
366 SkASSERT(cachedAddr != nullptr);
367 const void* addr = frame.getAddr(0, y);
368 SkASSERT(addr != nullptr);
369 const bool lineMatches = memcmp(cachedAddr, addr, rowLen) == 0;
370 if (!lineMatches) {
371 SkString name = SkStringPrintf("cached_%i", i);
372 write_bm(name.c_str(), cachedFrame);
373 name = SkStringPrintf("frame_%i", i);
374 write_bm(name.c_str(), frame);
375 ERRORF(r, "%s's frame %i is different (starting from line %i) when "
376 "providing prior frame %i!", rec.fName, i, y, j);
377 break;
378 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400379 }
380 }
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500381 }
scroggo19b91532016-10-24 09:03:26 -0700382 }
383 }
384}
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500385
386// Verify that a webp image can be animated scaled down. This image has a
387// kRestoreBG frame, so it is an interesting image to test. After decoding that
388// frame, we have to erase its rectangle. The rectangle has to be adjusted
389// based on the scaled size.
390DEF_TEST(AndroidCodec_animated, r) {
391 if (GetResourcePath().isEmpty()) {
392 return;
393 }
394
395 const char* file = "images/required.webp";
396 sk_sp<SkData> data(GetResourceAsData(file));
397 if (!data) {
398 ERRORF(r, "Missing %s", file);
399 return;
400 }
401
402 auto codec = SkAndroidCodec::MakeFromCodec(SkCodec::MakeFromData(std::move(data)));
403 if (!codec) {
404 ERRORF(r, "Failed to decode %s", file);
405 return;
406 }
407
408 auto info = codec->getInfo().makeAlphaType(kPremul_SkAlphaType);
409
410 for (int sampleSize : { 8, 32, 100 }) {
411 auto dimensions = codec->codec()->getScaledDimensions(1.0f / sampleSize);
412 info = info.makeWH(dimensions.width(), dimensions.height());
413 SkBitmap bm;
414 bm.allocPixels(info);
415
416 SkCodec::Options options;
417 for (int i = 0; i < codec->codec()->getFrameCount(); ++i) {
418 SkCodec::FrameInfo frameInfo;
419 REPORTER_ASSERT(r, codec->codec()->getFrameInfo(i, &frameInfo));
420 if (5 == i) {
421 REPORTER_ASSERT(r, frameInfo.fDisposalMethod
422 == SkCodecAnimation::DisposalMethod::kRestoreBGColor);
423 }
424 options.fFrameIndex = i;
425 options.fPriorFrame = i - 1;
426 info = info.makeAlphaType(frameInfo.fAlphaType);
427
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400428 auto result = codec->codec()->getPixels(info, bm.getPixels(), bm.rowBytes(),
429 &options);
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500430 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400431
432 // Now compare to not using prior frame.
433 SkBitmap bm2;
434 bm2.allocPixels(info);
435
Nigel Tao66bc5242018-08-22 10:56:03 +1000436 options.fPriorFrame = SkCodec::kNoFrame;
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400437 result = codec->codec()->getPixels(info, bm2.getPixels(), bm2.rowBytes(),
438 &options);
439 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
440
441 for (int y = 0; y < info.height(); ++y) {
442 if (memcmp(bm.getAddr32(0, y), bm2.getAddr32(0, y), info.minRowBytes())) {
443 ERRORF(r, "pixel mismatch for sample size %i, frame %i resulting in "
444 "dimensions %i x %i line %i\n",
445 sampleSize, i, info.width(), info.height(), y);
446 break;
447 }
448 }
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500449 }
450 }
451}