blob: 02253ef64f582751d25bd82ca670104b5c5ab029 [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
scroggo38a2cf52016-10-24 09:56:40 -07008#include "SkBitmap.h"
scroggo19b91532016-10-24 09:03:26 -07009#include "SkCodec.h"
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -050010#include "SkCommonFlags.h"
11#include "SkImageEncoder.h"
12#include "SkOSPath.h"
scroggo19b91532016-10-24 09:03:26 -070013#include "SkStream.h"
14
15#include "Resources.h"
16#include "Test.h"
17
18#include <initializer_list>
19#include <vector>
20
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -050021static void write_bm(const char* name, const SkBitmap& bm) {
22 if (FLAGS_writePath.isEmpty()) {
23 return;
24 }
25
26 SkString filename = SkOSPath::Join(FLAGS_writePath[0], name);
27 filename.appendf(".png");
28 SkFILEWStream file(filename.c_str());
29 if (!SkEncodeImage(&file, bm, SkEncodedImageFormat::kPNG, 100)) {
30 SkDebugf("failed to write '%s'\n", filename.c_str());
31 }
32}
33
scroggo19b91532016-10-24 09:03:26 -070034DEF_TEST(Codec_frames, r) {
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040035 #define kOpaque kOpaque_SkAlphaType
36 #define kUnpremul kUnpremul_SkAlphaType
scroggo19b91532016-10-24 09:03:26 -070037 static const struct {
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040038 const char* fName;
39 size_t fFrameCount;
scroggo19b91532016-10-24 09:03:26 -070040 // One less than fFramecount, since the first frame is always
41 // independent.
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040042 std::vector<size_t> fRequiredFrames;
43 // Same, since the first frame should match getInfo.
44 std::vector<SkAlphaType> fAlphaTypes;
scroggo19b91532016-10-24 09:03:26 -070045 // The size of this one should match fFrameCount for animated, empty
46 // otherwise.
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040047 std::vector<size_t> fDurations;
48 int fRepetitionCount;
scroggo19b91532016-10-24 09:03:26 -070049 } gRecs[] = {
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040050 { "alphabetAnim.gif", 13,
51 { SkCodec::kNone, 0, 0, 0, 0, 5, 6, SkCodec::kNone,
52 SkCodec::kNone, SkCodec::kNone, 10, 11 },
53 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
54 kUnpremul, kUnpremul, kUnpremul, kOpaque, kOpaque, kUnpremul },
55 { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 },
56 0 },
57 { "randPixelsAnim2.gif", 4,
58 // required frames
59 { 0, 0, 1 },
60 // alphas
61 { kOpaque, kOpaque, kOpaque },
62 // durations
63 { 0, 1000, 170, 40 },
64 // repetition count
65 0 },
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -050066 { "randPixelsAnim.gif", 13,
67 // required frames
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040068 { SkCodec::kNone, 1, 2, 3, 4, 3, 6, 7, 7, 7, 9, 9 },
69 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
70 kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -050071 // durations
72 { 0, 1000, 170, 40, 220, 7770, 90, 90, 90, 90, 90, 90, 90 },
73 // repetition count
74 0 },
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040075 { "box.gif", 1, {}, {}, {}, 0 },
76 { "color_wheel.gif", 1, {}, {}, {}, 0 },
77 { "test640x479.gif", 4, { 0, 1, 2 },
78 { kOpaque, kOpaque, kOpaque },
79 { 200, 200, 200, 200 },
scroggoe71b1a12016-11-01 08:28:28 -070080 SkCodec::kRepetitionCountInfinite },
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040081 { "colorTables.gif", 2, { 0 }, { kOpaque }, { 1000, 1000 }, 5 },
scroggo19b91532016-10-24 09:03:26 -070082
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040083 { "arrow.png", 1, {}, {}, {}, 0 },
84 { "google_chrome.ico", 1, {}, {}, {}, 0 },
85 { "brickwork-texture.jpg", 1, {}, {}, {}, 0 },
scroggo19b91532016-10-24 09:03:26 -070086#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040087 { "dng_with_preview.dng", 1, {}, {}, {}, 0 },
scroggo19b91532016-10-24 09:03:26 -070088#endif
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040089 { "mandrill.wbmp", 1, {}, {}, {}, 0 },
90 { "randPixels.bmp", 1, {}, {}, {}, 0 },
91 { "yellow_rose.webp", 1, {}, {}, {}, 0 },
scroggo19b91532016-10-24 09:03:26 -070092 };
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040093 #undef kOpaque
94 #undef kUnpremul
scroggo19b91532016-10-24 09:03:26 -070095
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040096 for (const auto& rec : gRecs) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -040097 sk_sp<SkData> data(GetResourceAsData(rec.fName));
98 if (!data) {
scroggo19b91532016-10-24 09:03:26 -070099 // Useful error statement, but sometimes people run tests without
100 // resources, and they do not want to see these messages.
101 //ERRORF(r, "Missing resources? Could not find '%s'", rec.fName);
102 continue;
103 }
104
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400105 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
scroggo19b91532016-10-24 09:03:26 -0700106 if (!codec) {
107 ERRORF(r, "Failed to create an SkCodec from '%s'", rec.fName);
108 continue;
109 }
110
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400111 {
112 SkCodec::FrameInfo frameInfo;
113 REPORTER_ASSERT(r, !codec->getFrameInfo(0, &frameInfo));
114 }
115
scroggoe71b1a12016-11-01 08:28:28 -0700116 const int repetitionCount = codec->getRepetitionCount();
117 if (repetitionCount != rec.fRepetitionCount) {
118 ERRORF(r, "%s repetition count does not match! expected: %i\tactual: %i",
119 rec.fName, rec.fRepetitionCount, repetitionCount);
120 }
121
scroggo19b91532016-10-24 09:03:26 -0700122 const size_t expected = rec.fFrameCount;
scroggo19b91532016-10-24 09:03:26 -0700123 if (rec.fRequiredFrames.size() + 1 != expected) {
124 ERRORF(r, "'%s' has wrong number entries in fRequiredFrames; expected: %i\tactual: %i",
Leon Scroggins IIIfc49b402016-10-31 14:08:56 -0400125 rec.fName, expected, rec.fRequiredFrames.size() + 1);
scroggo19b91532016-10-24 09:03:26 -0700126 continue;
127 }
128
scroggo19b91532016-10-24 09:03:26 -0700129 if (rec.fDurations.size() != expected) {
130 ERRORF(r, "'%s' has wrong number entries in fDurations; expected: %i\tactual: %i",
131 rec.fName, expected, rec.fDurations.size());
132 continue;
133 }
134
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400135 enum class TestMode {
136 kVector,
137 kIndividual,
138 };
139
140 for (auto mode : { TestMode::kVector, TestMode::kIndividual }) {
141 // Re-create the codec to reset state and test parsing.
142 codec.reset(SkCodec::NewFromData(data));
143
144 size_t frameCount;
145 std::vector<SkCodec::FrameInfo> frameInfos;
146 switch (mode) {
147 case TestMode::kVector:
148 frameInfos = codec->getFrameInfo();
149 // getFrameInfo returns empty set for non-animated.
150 frameCount = frameInfos.empty() ? 1 : frameInfos.size();
151 break;
152 case TestMode::kIndividual:
153 frameCount = codec->getFrameCount();
154 break;
155 }
156
157 if (frameCount != expected) {
158 ERRORF(r, "'%s' expected frame count: %i\tactual: %i",
159 rec.fName, expected, frameCount);
160 continue;
161 }
162
163 // From here on, we are only concerned with animated images.
164 if (1 == frameCount) {
165 continue;
166 }
167
168 for (size_t i = 0; i < frameCount; i++) {
169 SkCodec::FrameInfo frameInfo;
170 switch (mode) {
171 case TestMode::kVector:
172 frameInfo = frameInfos[i];
173 break;
174 case TestMode::kIndividual:
175 REPORTER_ASSERT(r, codec->getFrameInfo(i, nullptr));
176 REPORTER_ASSERT(r, codec->getFrameInfo(i, &frameInfo));
177 break;
178 }
179
180 if (rec.fDurations[i] != frameInfo.fDuration) {
181 ERRORF(r, "%s frame %i's durations do not match! expected: %i\tactual: %i",
182 rec.fName, i, rec.fDurations[i], frameInfo.fDuration);
183 }
184
185 if (0 == i) {
186 REPORTER_ASSERT(r, frameInfo.fRequiredFrame == SkCodec::kNone);
187 REPORTER_ASSERT(r, frameInfo.fAlphaType == codec->getInfo().alphaType());
188 continue;
189 }
190
191 if (rec.fRequiredFrames[i-1] != frameInfo.fRequiredFrame) {
192 ERRORF(r, "%s's frame %i has wrong dependency! expected: %i\tactual: %i",
193 rec.fName, i, rec.fRequiredFrames[i-1], frameInfo.fRequiredFrame);
194 }
195
196 auto to_string = [](SkAlphaType type) {
197 switch (type) {
198 case kUnpremul_SkAlphaType:
199 return "unpremul";
200 case kOpaque_SkAlphaType:
201 return "opaque";
202 default:
203 return "other";
204 }
205 };
206
207 auto expectedAlpha = rec.fAlphaTypes[i-1];
208 auto alpha = frameInfo.fAlphaType;
209 if (expectedAlpha != alpha) {
210 ERRORF(r, "%s's frame %i has wrong alpha type! expected: %s\tactual: %s",
211 rec.fName, i, to_string(expectedAlpha), to_string(alpha));
212 }
213 }
214
215 if (TestMode::kIndividual == mode) {
216 // No need to test decoding twice.
217 return;
218 }
219
220 // Compare decoding in two ways:
221 // 1. Provide the frame that a frame depends on, so the codec just has to blend.
222 // (in the array cachedFrames)
223 // 2. Do not provide the frame that a frame depends on, so the codec has to decode
224 // all the way back to a key-frame. (in a local variable uncachedFrame)
225 // The two should look the same.
226 std::vector<SkBitmap> cachedFrames(frameCount);
227 const auto& info = codec->getInfo().makeColorType(kN32_SkColorType);
228
229 auto decode = [&](SkBitmap* bm, bool cached, size_t index) {
230 bm->allocPixels(info);
231 if (cached) {
232 // First copy the pixels from the cached frame
233 const size_t requiredFrame = frameInfos[index].fRequiredFrame;
234 if (requiredFrame != SkCodec::kNone) {
235 const bool success = cachedFrames[requiredFrame].copyTo(bm);
236 REPORTER_ASSERT(r, success);
237 }
238 }
239 SkCodec::Options opts;
240 opts.fFrameIndex = index;
241 opts.fHasPriorFrame = cached;
242 auto result = codec->getPixels(info, bm->getPixels(), bm->rowBytes(),
243 &opts, nullptr, nullptr);
244 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
245 };
246
247 for (size_t i = 0; i < frameCount; i++) {
248 SkBitmap& cachedFrame = cachedFrames[i];
249 decode(&cachedFrame, true, i);
250 SkBitmap uncachedFrame;
251 decode(&uncachedFrame, false, i);
252
253 // Now verify they're equal.
254 const size_t rowLen = info.bytesPerPixel() * info.width();
255 for (int y = 0; y < info.height(); y++) {
256 const void* cachedAddr = cachedFrame.getAddr(0, y);
257 SkASSERT(cachedAddr != nullptr);
258 const void* uncachedAddr = uncachedFrame.getAddr(0, y);
259 SkASSERT(uncachedAddr != nullptr);
260 const bool lineMatches = memcmp(cachedAddr, uncachedAddr, rowLen) == 0;
261 if (!lineMatches) {
262 SkString name = SkStringPrintf("cached_%i", i);
263 write_bm(name.c_str(), cachedFrame);
264 name = SkStringPrintf("uncached_%i", i);
265 write_bm(name.c_str(), uncachedFrame);
266 ERRORF(r, "%s's frame %i is different depending on caching!", rec.fName, i);
267 break;
268 }
269 }
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500270 }
scroggo19b91532016-10-24 09:03:26 -0700271 }
272 }
273}