blob: 4e069c17fb39c66011c2173e27d220c3d8c8a72b [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 }
Mike Reedede7bac2017-07-23 15:30:02 -040033 SkCodec::MakeFromData(SkData::MakeSubset(data.get(), 0, 23))->getFrameInfo();
Leon Scroggins III7d22a332017-04-12 17:01:26 -040034}
35
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040036// 565 does not support alpha, but there is no reason for it not to support an
37// animated image with a frame that has alpha but then blends onto an opaque
38// frame making the result opaque. Test that we can decode such a frame.
39DEF_TEST(Codec_565, r) {
Hal Canaryc465d132017-12-08 10:21:31 -050040 sk_sp<SkData> data(GetResourceAsData("images/blendBG.webp"));
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040041 if (!data) {
42 return;
43 }
Mike Reedede7bac2017-07-23 15:30:02 -040044 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(std::move(data)));
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040045 auto info = codec->getInfo().makeColorType(kRGB_565_SkColorType);
46 SkBitmap bm;
47 bm.allocPixels(info);
48
49 SkCodec::Options options;
50 options.fFrameIndex = 1;
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040051 options.fPriorFrame = SkCodec::kNone;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040052
53 const auto result = codec->getPixels(info, bm.getPixels(), bm.rowBytes(),
Leon Scroggins571b30f2017-07-11 17:35:31 +000054 &options);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040055 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
56}
57
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040058static bool restore_previous(const SkCodec::FrameInfo& info) {
59 return info.fDisposalMethod == SkCodecAnimation::DisposalMethod::kRestorePrevious;
60}
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040061
scroggo19b91532016-10-24 09:03:26 -070062DEF_TEST(Codec_frames, r) {
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -050063 #define kOpaque kOpaque_SkAlphaType
64 #define kUnpremul kUnpremul_SkAlphaType
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040065 #define kKeep SkCodecAnimation::DisposalMethod::kKeep
66 #define kRestoreBG SkCodecAnimation::DisposalMethod::kRestoreBGColor
67 #define kRestorePrev SkCodecAnimation::DisposalMethod::kRestorePrevious
scroggo19b91532016-10-24 09:03:26 -070068 static const struct {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040069 const char* fName;
70 int fFrameCount;
scroggo19b91532016-10-24 09:03:26 -070071 // One less than fFramecount, since the first frame is always
72 // independent.
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040073 std::vector<int> fRequiredFrames;
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -050074 // Same, since the first frame should match getInfo
75 std::vector<SkAlphaType> fAlphas;
scroggo19b91532016-10-24 09:03:26 -070076 // The size of this one should match fFrameCount for animated, empty
77 // otherwise.
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040078 std::vector<int> fDurations;
79 int fRepetitionCount;
80 std::vector<SkCodecAnimation::DisposalMethod> fDisposalMethods;
scroggo19b91532016-10-24 09:03:26 -070081 } gRecs[] = {
Hal Canaryc465d132017-12-08 10:21:31 -050082 { "images/required.gif", 7,
Chris Blume0b5e7d12017-09-27 13:23:41 -070083 { 0, 1, 2, 3, 4, 5 },
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -050084 { kOpaque, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040085 { 100, 100, 100, 100, 100, 100, 100 },
86 0,
87 { kKeep, kRestoreBG, kKeep, kKeep, kKeep, kRestoreBG, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -050088 { "images/alphabetAnim.gif", 13,
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040089 { SkCodec::kNone, 0, 0, 0, 0, 5, 6, SkCodec::kNone,
Chris Blume0b5e7d12017-09-27 13:23:41 -070090 SkCodec::kNone, 9, 10, 11 },
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -050091 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
92 kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040093 { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040094 0,
95 { kKeep, kRestorePrev, kRestorePrev, kRestorePrev, kRestorePrev,
96 kRestoreBG, kKeep, kRestoreBG, kRestoreBG, kKeep, kKeep,
97 kRestoreBG, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -050098 { "images/randPixelsAnim2.gif", 4,
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040099 // required frames
100 { 0, 0, 1 },
101 // alphas
102 { kOpaque, kOpaque, kOpaque },
103 // durations
104 { 0, 1000, 170, 40 },
105 // repetition count
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400106 0,
107 { kKeep, kKeep, kRestorePrev, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500108 { "images/randPixelsAnim.gif", 13,
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500109 // required frames
Chris Blume0b5e7d12017-09-27 13:23:41 -0700110 { 0, 1, 2, 3, 4, 3, 6, 7, 7, 7, 9, 9 },
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500111 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
112 kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500113 // durations
114 { 0, 1000, 170, 40, 220, 7770, 90, 90, 90, 90, 90, 90, 90 },
115 // repetition count
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400116 0,
117 { kKeep, kKeep, kKeep, kKeep, kRestoreBG, kRestoreBG, kRestoreBG,
118 kRestoreBG, kRestorePrev, kRestoreBG, kRestorePrev, kRestorePrev,
119 kRestorePrev, } },
Hal Canaryc465d132017-12-08 10:21:31 -0500120 { "images/box.gif", 1, {}, {}, {}, 0, { kKeep } },
121 { "images/color_wheel.gif", 1, {}, {}, {}, 0, { kKeep } },
122 { "images/test640x479.gif", 4, { 0, 1, 2 },
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400123 { kOpaque, kOpaque, kOpaque },
124 { 200, 200, 200, 200 },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400125 SkCodec::kRepetitionCountInfinite,
126 { kKeep, kKeep, kKeep, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500127 { "images/colorTables.gif", 2, { 0 }, { kOpaque }, { 1000, 1000 }, 5,
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400128 { kKeep, kKeep } },
scroggo19b91532016-10-24 09:03:26 -0700129
Hal Canaryc465d132017-12-08 10:21:31 -0500130 { "images/arrow.png", 1, {}, {}, {}, 0, {} },
131 { "images/google_chrome.ico", 1, {}, {}, {}, 0, {} },
132 { "images/brickwork-texture.jpg", 1, {}, {}, {}, 0, {} },
scroggo19b91532016-10-24 09:03:26 -0700133#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
Hal Canaryc465d132017-12-08 10:21:31 -0500134 { "images/dng_with_preview.dng", 1, {}, {}, {}, 0, {} },
scroggo19b91532016-10-24 09:03:26 -0700135#endif
Hal Canaryc465d132017-12-08 10:21:31 -0500136 { "images/mandrill.wbmp", 1, {}, {}, {}, 0, {} },
137 { "images/randPixels.bmp", 1, {}, {}, {}, 0, {} },
138 { "images/yellow_rose.webp", 1, {}, {}, {}, 0, {} },
139 { "images/webp-animated.webp", 3, { 0, 1 }, { kOpaque, kOpaque },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400140 { 1000, 500, 1000 }, SkCodec::kRepetitionCountInfinite,
141 { kKeep, kKeep, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500142 { "images/blendBG.webp", 7, { 0, SkCodec::kNone, SkCodec::kNone, SkCodec::kNone,
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400143 4, 4 },
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400144 { kOpaque, kOpaque, kUnpremul, kOpaque, kUnpremul, kUnpremul },
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400145 { 525, 500, 525, 437, 609, 729, 444 }, 7,
146 { kKeep, kKeep, kKeep, kKeep, kKeep, kKeep, kKeep } },
Hal Canaryc465d132017-12-08 10:21:31 -0500147 { "images/required.webp", 7,
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400148 { 0, 1, 1, SkCodec::kNone, 4, 4 },
149 { kOpaque, kUnpremul, kUnpremul, kOpaque, kOpaque, kOpaque },
150 { 100, 100, 100, 100, 100, 100, 100 },
151 1,
152 { kKeep, kRestoreBG, kKeep, kKeep, kKeep, kRestoreBG, kKeep } },
scroggo19b91532016-10-24 09:03:26 -0700153 };
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400154 #undef kOpaque
155 #undef kUnpremul
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400156 #undef kKeep
157 #undef kRestorePrev
158 #undef kRestoreBG
scroggo19b91532016-10-24 09:03:26 -0700159
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400160 for (const auto& rec : gRecs) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400161 sk_sp<SkData> data(GetResourceAsData(rec.fName));
162 if (!data) {
scroggo19b91532016-10-24 09:03:26 -0700163 // Useful error statement, but sometimes people run tests without
164 // resources, and they do not want to see these messages.
165 //ERRORF(r, "Missing resources? Could not find '%s'", rec.fName);
166 continue;
167 }
168
Mike Reedede7bac2017-07-23 15:30:02 -0400169 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data));
scroggo19b91532016-10-24 09:03:26 -0700170 if (!codec) {
171 ERRORF(r, "Failed to create an SkCodec from '%s'", rec.fName);
172 continue;
173 }
174
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400175 {
176 SkCodec::FrameInfo frameInfo;
177 REPORTER_ASSERT(r, !codec->getFrameInfo(0, &frameInfo));
178 }
179
scroggoe71b1a12016-11-01 08:28:28 -0700180 const int repetitionCount = codec->getRepetitionCount();
181 if (repetitionCount != rec.fRepetitionCount) {
182 ERRORF(r, "%s repetition count does not match! expected: %i\tactual: %i",
183 rec.fName, rec.fRepetitionCount, repetitionCount);
184 }
185
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400186 const int expected = rec.fFrameCount;
187 if (rec.fRequiredFrames.size() + 1 != static_cast<size_t>(expected)) {
scroggo19b91532016-10-24 09:03:26 -0700188 ERRORF(r, "'%s' has wrong number entries in fRequiredFrames; expected: %i\tactual: %i",
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400189 rec.fName, expected - 1, rec.fRequiredFrames.size());
scroggo19b91532016-10-24 09:03:26 -0700190 continue;
191 }
192
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400193 if (expected > 1) {
194 if (rec.fDurations.size() != static_cast<size_t>(expected)) {
195 ERRORF(r, "'%s' has wrong number entries in fDurations; expected: %i\tactual: %i",
196 rec.fName, expected, rec.fDurations.size());
197 continue;
198 }
199
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400200 if (rec.fAlphas.size() + 1 != static_cast<size_t>(expected)) {
201 ERRORF(r, "'%s' has wrong number entries in fAlphas; expected: %i\tactual: %i",
202 rec.fName, expected - 1, rec.fAlphas.size());
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400203 continue;
204 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400205
206 if (rec.fDisposalMethods.size() != static_cast<size_t>(expected)) {
207 ERRORF(r, "'%s' has wrong number entries in fDisposalMethods; "
208 "expected %i\tactual: %i",
209 rec.fName, expected, rec.fDisposalMethods.size());
210 continue;
211 }
scroggo19b91532016-10-24 09:03:26 -0700212 }
213
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400214 enum class TestMode {
215 kVector,
216 kIndividual,
217 };
218
219 for (auto mode : { TestMode::kVector, TestMode::kIndividual }) {
220 // Re-create the codec to reset state and test parsing.
Mike Reedede7bac2017-07-23 15:30:02 -0400221 codec = SkCodec::MakeFromData(data);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400222
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400223 int frameCount;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400224 std::vector<SkCodec::FrameInfo> frameInfos;
225 switch (mode) {
226 case TestMode::kVector:
227 frameInfos = codec->getFrameInfo();
228 // getFrameInfo returns empty set for non-animated.
229 frameCount = frameInfos.empty() ? 1 : frameInfos.size();
230 break;
231 case TestMode::kIndividual:
232 frameCount = codec->getFrameCount();
233 break;
234 }
235
236 if (frameCount != expected) {
237 ERRORF(r, "'%s' expected frame count: %i\tactual: %i",
238 rec.fName, expected, frameCount);
239 continue;
240 }
241
242 // From here on, we are only concerned with animated images.
243 if (1 == frameCount) {
244 continue;
245 }
246
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400247 for (int i = 0; i < frameCount; i++) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400248 SkCodec::FrameInfo frameInfo;
249 switch (mode) {
250 case TestMode::kVector:
251 frameInfo = frameInfos[i];
252 break;
253 case TestMode::kIndividual:
254 REPORTER_ASSERT(r, codec->getFrameInfo(i, nullptr));
255 REPORTER_ASSERT(r, codec->getFrameInfo(i, &frameInfo));
256 break;
257 }
258
259 if (rec.fDurations[i] != frameInfo.fDuration) {
260 ERRORF(r, "%s frame %i's durations do not match! expected: %i\tactual: %i",
261 rec.fName, i, rec.fDurations[i], frameInfo.fDuration);
262 }
263
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500264 auto to_string = [](SkAlphaType alpha) {
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400265 switch (alpha) {
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500266 case kUnpremul_SkAlphaType:
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400267 return "unpremul";
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500268 case kOpaque_SkAlphaType:
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400269 return "opaque";
270 default:
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400271 SkASSERT(false);
272 return "unknown";
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400273 }
274 };
275
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500276 auto expectedAlpha = 0 == i ? codec->getInfo().alphaType() : rec.fAlphas[i-1];
277 auto alpha = frameInfo.fAlphaType;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400278 if (expectedAlpha != alpha) {
279 ERRORF(r, "%s's frame %i has wrong alpha type! expected: %s\tactual: %s",
280 rec.fName, i, to_string(expectedAlpha), to_string(alpha));
281 }
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400282
283 if (0 == i) {
284 REPORTER_ASSERT(r, frameInfo.fRequiredFrame == SkCodec::kNone);
285 } else if (rec.fRequiredFrames[i-1] != frameInfo.fRequiredFrame) {
286 ERRORF(r, "%s's frame %i has wrong dependency! expected: %i\tactual: %i",
287 rec.fName, i, rec.fRequiredFrames[i-1], frameInfo.fRequiredFrame);
288 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400289
290 REPORTER_ASSERT(r, frameInfo.fDisposalMethod == rec.fDisposalMethods[i]);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400291 }
292
293 if (TestMode::kIndividual == mode) {
294 // No need to test decoding twice.
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400295 continue;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400296 }
297
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400298 // Compare decoding in multiple ways:
299 // - Start from scratch for each frame. |codec| will have to decode the required frame
300 // (and any it depends on) to decode. This is stored in |cachedFrames|.
301 // - Provide the frame that a frame depends on, so |codec| just has to blend.
302 // - Provide a frame after the required frame, which will be covered up by the newest
303 // frame.
304 // All should look the same.
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400305 std::vector<SkBitmap> cachedFrames(frameCount);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400306 const auto info = codec->getInfo().makeColorType(kN32_SkColorType);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400307
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400308 auto decode = [&](SkBitmap* bm, int index, int cachedIndex) {
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400309 auto decodeInfo = info;
310 if (index > 0) {
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500311 decodeInfo = info.makeAlphaType(frameInfos[index].fAlphaType);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400312 }
313 bm->allocPixels(decodeInfo);
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400314 if (cachedIndex != SkCodec::kNone) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400315 // First copy the pixels from the cached frame
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400316 const bool success = sk_tool_utils::copy_to(bm, kN32_SkColorType,
317 cachedFrames[cachedIndex]);
318 REPORTER_ASSERT(r, success);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400319 }
320 SkCodec::Options opts;
321 opts.fFrameIndex = index;
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400322 opts.fPriorFrame = cachedIndex;
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400323 const auto result = codec->getPixels(decodeInfo, bm->getPixels(), bm->rowBytes(),
Leon Scroggins571b30f2017-07-11 17:35:31 +0000324 &opts);
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400325 if (cachedIndex != SkCodec::kNone && restore_previous(frameInfos[cachedIndex])) {
326 if (result == SkCodec::kInvalidParameters) {
327 return true;
328 }
329 ERRORF(r, "Using a kRestorePrevious frame as fPriorFrame should fail");
330 return false;
331 }
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400332 if (result != SkCodec::kSuccess) {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400333 ERRORF(r, "Failed to decode frame %i from %s when providing prior frame %i, "
334 "error %i", index, rec.fName, cachedIndex, result);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400335 }
336 return result == SkCodec::kSuccess;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400337 };
338
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400339 for (int i = 0; i < frameCount; i++) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400340 SkBitmap& cachedFrame = cachedFrames[i];
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400341 if (!decode(&cachedFrame, i, SkCodec::kNone)) {
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400342 continue;
343 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400344 const auto reqFrame = frameInfos[i].fRequiredFrame;
345 if (reqFrame == SkCodec::kNone) {
346 // Nothing to compare against.
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400347 continue;
348 }
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400349 for (int j = reqFrame; j < i; j++) {
350 SkBitmap frame;
351 if (restore_previous(frameInfos[j])) {
352 (void) decode(&frame, i, j);
353 continue;
354 }
355 if (!decode(&frame, i, j)) {
356 continue;
357 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400358
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400359 // Now verify they're equal.
360 const size_t rowLen = info.bytesPerPixel() * info.width();
361 for (int y = 0; y < info.height(); y++) {
362 const void* cachedAddr = cachedFrame.getAddr(0, y);
363 SkASSERT(cachedAddr != nullptr);
364 const void* addr = frame.getAddr(0, y);
365 SkASSERT(addr != nullptr);
366 const bool lineMatches = memcmp(cachedAddr, addr, rowLen) == 0;
367 if (!lineMatches) {
368 SkString name = SkStringPrintf("cached_%i", i);
369 write_bm(name.c_str(), cachedFrame);
370 name = SkStringPrintf("frame_%i", i);
371 write_bm(name.c_str(), frame);
372 ERRORF(r, "%s's frame %i is different (starting from line %i) when "
373 "providing prior frame %i!", rec.fName, i, y, j);
374 break;
375 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400376 }
377 }
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500378 }
scroggo19b91532016-10-24 09:03:26 -0700379 }
380 }
381}
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500382
383// Verify that a webp image can be animated scaled down. This image has a
384// kRestoreBG frame, so it is an interesting image to test. After decoding that
385// frame, we have to erase its rectangle. The rectangle has to be adjusted
386// based on the scaled size.
387DEF_TEST(AndroidCodec_animated, r) {
388 if (GetResourcePath().isEmpty()) {
389 return;
390 }
391
392 const char* file = "images/required.webp";
393 sk_sp<SkData> data(GetResourceAsData(file));
394 if (!data) {
395 ERRORF(r, "Missing %s", file);
396 return;
397 }
398
399 auto codec = SkAndroidCodec::MakeFromCodec(SkCodec::MakeFromData(std::move(data)));
400 if (!codec) {
401 ERRORF(r, "Failed to decode %s", file);
402 return;
403 }
404
405 auto info = codec->getInfo().makeAlphaType(kPremul_SkAlphaType);
406
407 for (int sampleSize : { 8, 32, 100 }) {
408 auto dimensions = codec->codec()->getScaledDimensions(1.0f / sampleSize);
409 info = info.makeWH(dimensions.width(), dimensions.height());
410 SkBitmap bm;
411 bm.allocPixels(info);
412
413 SkCodec::Options options;
414 for (int i = 0; i < codec->codec()->getFrameCount(); ++i) {
415 SkCodec::FrameInfo frameInfo;
416 REPORTER_ASSERT(r, codec->codec()->getFrameInfo(i, &frameInfo));
417 if (5 == i) {
418 REPORTER_ASSERT(r, frameInfo.fDisposalMethod
419 == SkCodecAnimation::DisposalMethod::kRestoreBGColor);
420 }
421 options.fFrameIndex = i;
422 options.fPriorFrame = i - 1;
423 info = info.makeAlphaType(frameInfo.fAlphaType);
424
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400425 auto result = codec->codec()->getPixels(info, bm.getPixels(), bm.rowBytes(),
426 &options);
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500427 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400428
429 // Now compare to not using prior frame.
430 SkBitmap bm2;
431 bm2.allocPixels(info);
432
433 options.fPriorFrame = SkCodec::kNone;
434 result = codec->codec()->getPixels(info, bm2.getPixels(), bm2.rowBytes(),
435 &options);
436 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
437
438 for (int y = 0; y < info.height(); ++y) {
439 if (memcmp(bm.getAddr32(0, y), bm2.getAddr32(0, y), info.minRowBytes())) {
440 ERRORF(r, "pixel mismatch for sample size %i, frame %i resulting in "
441 "dimensions %i x %i line %i\n",
442 sampleSize, i, info.width(), info.height(), y);
443 break;
444 }
445 }
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500446 }
447 }
448}