scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
scroggo | 38a2cf5 | 2016-10-24 09:56:40 -0700 | [diff] [blame] | 8 | #include "SkBitmap.h" |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 9 | #include "SkCodec.h" |
Leon Scroggins III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 10 | #include "SkCommonFlags.h" |
| 11 | #include "SkImageEncoder.h" |
| 12 | #include "SkOSPath.h" |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 13 | #include "SkStream.h" |
| 14 | |
| 15 | #include "Resources.h" |
| 16 | #include "Test.h" |
Matt Sarett | 68b8e3d | 2017-04-28 11:15:22 -0400 | [diff] [blame] | 17 | #include "sk_tool_utils.h" |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 18 | |
| 19 | #include <initializer_list> |
| 20 | #include <vector> |
| 21 | |
Leon Scroggins III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 22 | static void write_bm(const char* name, const SkBitmap& bm) { |
| 23 | if (FLAGS_writePath.isEmpty()) { |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | SkString filename = SkOSPath::Join(FLAGS_writePath[0], name); |
| 28 | filename.appendf(".png"); |
| 29 | SkFILEWStream file(filename.c_str()); |
| 30 | if (!SkEncodeImage(&file, bm, SkEncodedImageFormat::kPNG, 100)) { |
| 31 | SkDebugf("failed to write '%s'\n", filename.c_str()); |
| 32 | } |
| 33 | } |
| 34 | |
Leon Scroggins III | 7d22a33 | 2017-04-12 17:01:26 -0400 | [diff] [blame] | 35 | DEF_TEST(Codec_trunc, r) { |
| 36 | sk_sp<SkData> data(GetResourceAsData("box.gif")); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 37 | if (!data) { |
| 38 | return; |
| 39 | } |
Leon Scroggins III | 7d22a33 | 2017-04-12 17:01:26 -0400 | [diff] [blame] | 40 | data = SkData::MakeSubset(data.get(), 0, 23); |
| 41 | std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data)); |
| 42 | codec->getFrameInfo(); |
| 43 | } |
| 44 | |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 45 | // 565 does not support alpha, but there is no reason for it not to support an |
| 46 | // animated image with a frame that has alpha but then blends onto an opaque |
| 47 | // frame making the result opaque. Test that we can decode such a frame. |
| 48 | DEF_TEST(Codec_565, r) { |
| 49 | sk_sp<SkData> data(GetResourceAsData("blendBG.webp")); |
| 50 | if (!data) { |
| 51 | return; |
| 52 | } |
| 53 | std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(std::move(data))); |
| 54 | auto info = codec->getInfo().makeColorType(kRGB_565_SkColorType); |
| 55 | SkBitmap bm; |
| 56 | bm.allocPixels(info); |
| 57 | |
| 58 | SkCodec::Options options; |
| 59 | options.fFrameIndex = 1; |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 60 | options.fPriorFrame = SkCodec::kNone; |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 61 | |
| 62 | const auto result = codec->getPixels(info, bm.getPixels(), bm.rowBytes(), |
| 63 | &options, nullptr, nullptr); |
| 64 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 65 | } |
| 66 | |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 67 | static bool restore_previous(const SkCodec::FrameInfo& info) { |
| 68 | return info.fDisposalMethod == SkCodecAnimation::DisposalMethod::kRestorePrevious; |
| 69 | } |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 70 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 71 | DEF_TEST(Codec_frames, r) { |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 72 | #define kOpaque kOpaque_SkAlphaType |
| 73 | #define kUnpremul kUnpremul_SkAlphaType |
| 74 | #define kKeep SkCodecAnimation::DisposalMethod::kKeep |
| 75 | #define kRestoreBG SkCodecAnimation::DisposalMethod::kRestoreBGColor |
| 76 | #define kRestorePrev SkCodecAnimation::DisposalMethod::kRestorePrevious |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 77 | static const struct { |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 78 | const char* fName; |
| 79 | int fFrameCount; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 80 | // One less than fFramecount, since the first frame is always |
| 81 | // independent. |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 82 | std::vector<int> fRequiredFrames; |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 83 | // Same, since the first frame should match getInfo. |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 84 | std::vector<SkAlphaType> fAlphaTypes; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 85 | // The size of this one should match fFrameCount for animated, empty |
| 86 | // otherwise. |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 87 | std::vector<int> fDurations; |
| 88 | int fRepetitionCount; |
| 89 | std::vector<SkCodecAnimation::DisposalMethod> fDisposalMethods; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 90 | } gRecs[] = { |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 91 | { "required.gif", 7, |
| 92 | { 0, 1, 1, SkCodec::kNone, 4, 4 }, |
| 93 | { kOpaque, kUnpremul, kUnpremul, kOpaque, kOpaque, kOpaque }, |
| 94 | { 100, 100, 100, 100, 100, 100, 100 }, |
| 95 | 0, |
| 96 | { kKeep, kRestoreBG, kKeep, kKeep, kKeep, kRestoreBG, kKeep } }, |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 97 | { "alphabetAnim.gif", 13, |
| 98 | { SkCodec::kNone, 0, 0, 0, 0, 5, 6, SkCodec::kNone, |
| 99 | SkCodec::kNone, SkCodec::kNone, 10, 11 }, |
| 100 | { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, |
| 101 | kUnpremul, kUnpremul, kUnpremul, kOpaque, kOpaque, kUnpremul }, |
| 102 | { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 }, |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 103 | 0, |
| 104 | { kKeep, kRestorePrev, kRestorePrev, kRestorePrev, kRestorePrev, |
| 105 | kRestoreBG, kKeep, kRestoreBG, kRestoreBG, kKeep, kKeep, |
| 106 | kRestoreBG, kKeep } }, |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 107 | { "randPixelsAnim2.gif", 4, |
| 108 | // required frames |
| 109 | { 0, 0, 1 }, |
| 110 | // alphas |
| 111 | { kOpaque, kOpaque, kOpaque }, |
| 112 | // durations |
| 113 | { 0, 1000, 170, 40 }, |
| 114 | // repetition count |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 115 | 0, |
| 116 | { kKeep, kKeep, kRestorePrev, kKeep } }, |
Leon Scroggins III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 117 | { "randPixelsAnim.gif", 13, |
| 118 | // required frames |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 119 | { SkCodec::kNone, 1, 2, 3, 4, 3, 6, 7, 7, 7, 9, 9 }, |
| 120 | { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, |
| 121 | kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul }, |
Leon Scroggins III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 122 | // durations |
| 123 | { 0, 1000, 170, 40, 220, 7770, 90, 90, 90, 90, 90, 90, 90 }, |
| 124 | // repetition count |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 125 | 0, |
| 126 | { kKeep, kKeep, kKeep, kKeep, kRestoreBG, kRestoreBG, kRestoreBG, |
| 127 | kRestoreBG, kRestorePrev, kRestoreBG, kRestorePrev, kRestorePrev, |
| 128 | kRestorePrev, } }, |
| 129 | { "box.gif", 1, {}, {}, {}, 0, { kKeep } }, |
| 130 | { "color_wheel.gif", 1, {}, {}, {}, 0, { kKeep } }, |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 131 | { "test640x479.gif", 4, { 0, 1, 2 }, |
| 132 | { kOpaque, kOpaque, kOpaque }, |
| 133 | { 200, 200, 200, 200 }, |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 134 | SkCodec::kRepetitionCountInfinite, |
| 135 | { kKeep, kKeep, kKeep, kKeep } }, |
| 136 | { "colorTables.gif", 2, { 0 }, { kOpaque }, { 1000, 1000 }, 5, |
| 137 | { kKeep, kKeep } }, |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 138 | |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 139 | { "arrow.png", 1, {}, {}, {}, 0, {} }, |
| 140 | { "google_chrome.ico", 1, {}, {}, {}, 0, {} }, |
| 141 | { "brickwork-texture.jpg", 1, {}, {}, {}, 0, {} }, |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 142 | #if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32)) |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 143 | { "dng_with_preview.dng", 1, {}, {}, {}, 0, {} }, |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 144 | #endif |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 145 | { "mandrill.wbmp", 1, {}, {}, {}, 0, {} }, |
| 146 | { "randPixels.bmp", 1, {}, {}, {}, 0, {} }, |
| 147 | { "yellow_rose.webp", 1, {}, {}, {}, 0, {} }, |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 148 | { "webp-animated.webp", 3, { 0, 1 }, { kOpaque, kOpaque }, |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 149 | { 1000, 500, 1000 }, SkCodec::kRepetitionCountInfinite, |
| 150 | { kKeep, kKeep, kKeep } }, |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 151 | { "blendBG.webp", 7, { 0, SkCodec::kNone, SkCodec::kNone, SkCodec::kNone, |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 152 | 4, 4 }, |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 153 | { kOpaque, kOpaque, kUnpremul, kOpaque, kUnpremul, kUnpremul }, |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 154 | { 525, 500, 525, 437, 609, 729, 444 }, 7, |
| 155 | { kKeep, kKeep, kKeep, kKeep, kKeep, kKeep, kKeep } }, |
| 156 | { "required.webp", 7, |
| 157 | { 0, 1, 1, SkCodec::kNone, 4, 4 }, |
| 158 | { kOpaque, kUnpremul, kUnpremul, kOpaque, kOpaque, kOpaque }, |
| 159 | { 100, 100, 100, 100, 100, 100, 100 }, |
| 160 | 1, |
| 161 | { kKeep, kRestoreBG, kKeep, kKeep, kKeep, kRestoreBG, kKeep } }, |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 162 | }; |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 163 | #undef kOpaque |
| 164 | #undef kUnpremul |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 165 | #undef kKeep |
| 166 | #undef kRestorePrev |
| 167 | #undef kRestoreBG |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 168 | |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 169 | for (const auto& rec : gRecs) { |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 170 | sk_sp<SkData> data(GetResourceAsData(rec.fName)); |
| 171 | if (!data) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 172 | // Useful error statement, but sometimes people run tests without |
| 173 | // resources, and they do not want to see these messages. |
| 174 | //ERRORF(r, "Missing resources? Could not find '%s'", rec.fName); |
| 175 | continue; |
| 176 | } |
| 177 | |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 178 | std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data)); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 179 | if (!codec) { |
| 180 | ERRORF(r, "Failed to create an SkCodec from '%s'", rec.fName); |
| 181 | continue; |
| 182 | } |
| 183 | |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 184 | { |
| 185 | SkCodec::FrameInfo frameInfo; |
| 186 | REPORTER_ASSERT(r, !codec->getFrameInfo(0, &frameInfo)); |
| 187 | } |
| 188 | |
scroggo | e71b1a1 | 2016-11-01 08:28:28 -0700 | [diff] [blame] | 189 | const int repetitionCount = codec->getRepetitionCount(); |
| 190 | if (repetitionCount != rec.fRepetitionCount) { |
| 191 | ERRORF(r, "%s repetition count does not match! expected: %i\tactual: %i", |
| 192 | rec.fName, rec.fRepetitionCount, repetitionCount); |
| 193 | } |
| 194 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 195 | const int expected = rec.fFrameCount; |
| 196 | if (rec.fRequiredFrames.size() + 1 != static_cast<size_t>(expected)) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 197 | ERRORF(r, "'%s' has wrong number entries in fRequiredFrames; expected: %i\tactual: %i", |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 198 | rec.fName, expected - 1, rec.fRequiredFrames.size()); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 199 | continue; |
| 200 | } |
| 201 | |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 202 | if (expected > 1) { |
| 203 | if (rec.fDurations.size() != static_cast<size_t>(expected)) { |
| 204 | ERRORF(r, "'%s' has wrong number entries in fDurations; expected: %i\tactual: %i", |
| 205 | rec.fName, expected, rec.fDurations.size()); |
| 206 | continue; |
| 207 | } |
| 208 | |
| 209 | if (rec.fAlphaTypes.size() + 1 != static_cast<size_t>(expected)) { |
| 210 | ERRORF(r, "'%s' has wrong number entries in fAlphaTypes; expected: %i\tactual: %i", |
| 211 | rec.fName, expected - 1, rec.fAlphaTypes.size()); |
| 212 | continue; |
| 213 | } |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 214 | |
| 215 | if (rec.fDisposalMethods.size() != static_cast<size_t>(expected)) { |
| 216 | ERRORF(r, "'%s' has wrong number entries in fDisposalMethods; " |
| 217 | "expected %i\tactual: %i", |
| 218 | rec.fName, expected, rec.fDisposalMethods.size()); |
| 219 | continue; |
| 220 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 223 | enum class TestMode { |
| 224 | kVector, |
| 225 | kIndividual, |
| 226 | }; |
| 227 | |
| 228 | for (auto mode : { TestMode::kVector, TestMode::kIndividual }) { |
| 229 | // Re-create the codec to reset state and test parsing. |
| 230 | codec.reset(SkCodec::NewFromData(data)); |
| 231 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 232 | int frameCount; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 233 | std::vector<SkCodec::FrameInfo> frameInfos; |
| 234 | switch (mode) { |
| 235 | case TestMode::kVector: |
| 236 | frameInfos = codec->getFrameInfo(); |
| 237 | // getFrameInfo returns empty set for non-animated. |
| 238 | frameCount = frameInfos.empty() ? 1 : frameInfos.size(); |
| 239 | break; |
| 240 | case TestMode::kIndividual: |
| 241 | frameCount = codec->getFrameCount(); |
| 242 | break; |
| 243 | } |
| 244 | |
| 245 | if (frameCount != expected) { |
| 246 | ERRORF(r, "'%s' expected frame count: %i\tactual: %i", |
| 247 | rec.fName, expected, frameCount); |
| 248 | continue; |
| 249 | } |
| 250 | |
| 251 | // From here on, we are only concerned with animated images. |
| 252 | if (1 == frameCount) { |
| 253 | continue; |
| 254 | } |
| 255 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 256 | for (int i = 0; i < frameCount; i++) { |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 257 | SkCodec::FrameInfo frameInfo; |
| 258 | switch (mode) { |
| 259 | case TestMode::kVector: |
| 260 | frameInfo = frameInfos[i]; |
| 261 | break; |
| 262 | case TestMode::kIndividual: |
| 263 | REPORTER_ASSERT(r, codec->getFrameInfo(i, nullptr)); |
| 264 | REPORTER_ASSERT(r, codec->getFrameInfo(i, &frameInfo)); |
| 265 | break; |
| 266 | } |
| 267 | |
| 268 | if (rec.fDurations[i] != frameInfo.fDuration) { |
| 269 | ERRORF(r, "%s frame %i's durations do not match! expected: %i\tactual: %i", |
| 270 | rec.fName, i, rec.fDurations[i], frameInfo.fDuration); |
| 271 | } |
| 272 | |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 273 | auto to_string = [](SkAlphaType type) { |
| 274 | switch (type) { |
| 275 | case kUnpremul_SkAlphaType: |
| 276 | return "unpremul"; |
| 277 | case kOpaque_SkAlphaType: |
| 278 | return "opaque"; |
| 279 | default: |
| 280 | return "other"; |
| 281 | } |
| 282 | }; |
| 283 | |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 284 | auto expectedAlpha = 0 == i ? codec->getInfo().alphaType() : rec.fAlphaTypes[i-1]; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 285 | auto alpha = frameInfo.fAlphaType; |
| 286 | 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 III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 290 | |
| 291 | if (0 == i) { |
| 292 | REPORTER_ASSERT(r, frameInfo.fRequiredFrame == SkCodec::kNone); |
| 293 | } 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 III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 297 | |
| 298 | REPORTER_ASSERT(r, frameInfo.fDisposalMethod == rec.fDisposalMethods[i]); |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | if (TestMode::kIndividual == mode) { |
| 302 | // No need to test decoding twice. |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 303 | continue; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 304 | } |
| 305 | |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 306 | // 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 III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 313 | std::vector<SkBitmap> cachedFrames(frameCount); |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 314 | const auto info = codec->getInfo().makeColorType(kN32_SkColorType); |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 315 | |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 316 | auto decode = [&](SkBitmap* bm, int index, int cachedIndex) { |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 317 | auto decodeInfo = info; |
| 318 | if (index > 0) { |
| 319 | decodeInfo = info.makeAlphaType(frameInfos[index].fAlphaType); |
| 320 | } |
| 321 | bm->allocPixels(decodeInfo); |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 322 | if (cachedIndex != SkCodec::kNone) { |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 323 | // First copy the pixels from the cached frame |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 324 | const bool success = sk_tool_utils::copy_to(bm, kN32_SkColorType, |
| 325 | cachedFrames[cachedIndex]); |
| 326 | REPORTER_ASSERT(r, success); |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 327 | } |
| 328 | SkCodec::Options opts; |
| 329 | opts.fFrameIndex = index; |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 330 | opts.fPriorFrame = cachedIndex; |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 331 | const auto result = codec->getPixels(decodeInfo, bm->getPixels(), bm->rowBytes(), |
| 332 | &opts, nullptr, nullptr); |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 333 | if (cachedIndex != SkCodec::kNone && restore_previous(frameInfos[cachedIndex])) { |
| 334 | if (result == SkCodec::kInvalidParameters) { |
| 335 | return true; |
| 336 | } |
| 337 | ERRORF(r, "Using a kRestorePrevious frame as fPriorFrame should fail"); |
| 338 | return false; |
| 339 | } |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 340 | if (result != SkCodec::kSuccess) { |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 341 | ERRORF(r, "Failed to decode frame %i from %s when providing prior frame %i, " |
| 342 | "error %i", index, rec.fName, cachedIndex, result); |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 343 | } |
| 344 | return result == SkCodec::kSuccess; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 345 | }; |
| 346 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 347 | for (int i = 0; i < frameCount; i++) { |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 348 | SkBitmap& cachedFrame = cachedFrames[i]; |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 349 | if (!decode(&cachedFrame, i, SkCodec::kNone)) { |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 350 | continue; |
| 351 | } |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 352 | const auto reqFrame = frameInfos[i].fRequiredFrame; |
| 353 | if (reqFrame == SkCodec::kNone) { |
| 354 | // Nothing to compare against. |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 355 | continue; |
| 356 | } |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 357 | for (int j = reqFrame; j < i; j++) { |
| 358 | SkBitmap frame; |
| 359 | if (restore_previous(frameInfos[j])) { |
| 360 | (void) decode(&frame, i, j); |
| 361 | continue; |
| 362 | } |
| 363 | if (!decode(&frame, i, j)) { |
| 364 | continue; |
| 365 | } |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 366 | |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame^] | 367 | // Now verify they're equal. |
| 368 | const size_t rowLen = info.bytesPerPixel() * info.width(); |
| 369 | for (int y = 0; y < info.height(); y++) { |
| 370 | const void* cachedAddr = cachedFrame.getAddr(0, y); |
| 371 | SkASSERT(cachedAddr != nullptr); |
| 372 | const void* addr = frame.getAddr(0, y); |
| 373 | SkASSERT(addr != nullptr); |
| 374 | const bool lineMatches = memcmp(cachedAddr, addr, rowLen) == 0; |
| 375 | if (!lineMatches) { |
| 376 | SkString name = SkStringPrintf("cached_%i", i); |
| 377 | write_bm(name.c_str(), cachedFrame); |
| 378 | name = SkStringPrintf("frame_%i", i); |
| 379 | write_bm(name.c_str(), frame); |
| 380 | ERRORF(r, "%s's frame %i is different (starting from line %i) when " |
| 381 | "providing prior frame %i!", rec.fName, i, y, j); |
| 382 | break; |
| 383 | } |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 384 | } |
| 385 | } |
Leon Scroggins III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 386 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | } |