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 | } |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 40 | SkCodec::MakeFromData(SkData::MakeSubset(data.get(), 0, 23))->getFrameInfo(); |
Leon Scroggins III | 7d22a33 | 2017-04-12 17:01:26 -0400 | [diff] [blame] | 41 | } |
| 42 | |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 43 | // 565 does not support alpha, but there is no reason for it not to support an |
| 44 | // animated image with a frame that has alpha but then blends onto an opaque |
| 45 | // frame making the result opaque. Test that we can decode such a frame. |
| 46 | DEF_TEST(Codec_565, r) { |
| 47 | sk_sp<SkData> data(GetResourceAsData("blendBG.webp")); |
| 48 | if (!data) { |
| 49 | return; |
| 50 | } |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 51 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(std::move(data))); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 52 | auto info = codec->getInfo().makeColorType(kRGB_565_SkColorType); |
| 53 | SkBitmap bm; |
| 54 | bm.allocPixels(info); |
| 55 | |
| 56 | SkCodec::Options options; |
| 57 | options.fFrameIndex = 1; |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 58 | options.fPriorFrame = SkCodec::kNone; |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 59 | |
| 60 | const auto result = codec->getPixels(info, bm.getPixels(), bm.rowBytes(), |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 61 | &options); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 62 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 63 | } |
| 64 | |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 65 | static bool restore_previous(const SkCodec::FrameInfo& info) { |
| 66 | return info.fDisposalMethod == SkCodecAnimation::DisposalMethod::kRestorePrevious; |
| 67 | } |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 68 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 69 | DEF_TEST(Codec_frames, r) { |
Leon Scroggins III | ae79f32 | 2017-08-18 10:53:24 -0400 | [diff] [blame] | 70 | #define kOpaque SkEncodedInfo::kOpaque_Alpha |
| 71 | #define kUnpremul SkEncodedInfo::kUnpremul_Alpha |
| 72 | #define kBinary SkEncodedInfo::kBinary_Alpha |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 73 | #define kKeep SkCodecAnimation::DisposalMethod::kKeep |
| 74 | #define kRestoreBG SkCodecAnimation::DisposalMethod::kRestoreBGColor |
| 75 | #define kRestorePrev SkCodecAnimation::DisposalMethod::kRestorePrevious |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 76 | static const struct { |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 77 | const char* fName; |
| 78 | int fFrameCount; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 79 | // One less than fFramecount, since the first frame is always |
| 80 | // independent. |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 81 | std::vector<int> fRequiredFrames; |
Leon Scroggins III | ae79f32 | 2017-08-18 10:53:24 -0400 | [diff] [blame] | 82 | // Same, since the first frame should match getEncodedInfo |
| 83 | std::vector<SkEncodedInfo::Alpha> fAlphas; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 84 | // The size of this one should match fFrameCount for animated, empty |
| 85 | // otherwise. |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 86 | std::vector<int> fDurations; |
| 87 | int fRepetitionCount; |
| 88 | std::vector<SkCodecAnimation::DisposalMethod> fDisposalMethods; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 89 | } gRecs[] = { |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 90 | { "required.gif", 7, |
Chris Blume | 0b5e7d1 | 2017-09-27 13:23:41 -0700 | [diff] [blame^] | 91 | { 0, 1, 2, 3, 4, 5 }, |
| 92 | { kOpaque, kBinary, kBinary, kBinary, kBinary, kBinary }, |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 93 | { 100, 100, 100, 100, 100, 100, 100 }, |
| 94 | 0, |
| 95 | { kKeep, kRestoreBG, kKeep, kKeep, kKeep, kRestoreBG, kKeep } }, |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 96 | { "alphabetAnim.gif", 13, |
| 97 | { SkCodec::kNone, 0, 0, 0, 0, 5, 6, SkCodec::kNone, |
Chris Blume | 0b5e7d1 | 2017-09-27 13:23:41 -0700 | [diff] [blame^] | 98 | SkCodec::kNone, 9, 10, 11 }, |
Leon Scroggins III | ae79f32 | 2017-08-18 10:53:24 -0400 | [diff] [blame] | 99 | { kBinary, kBinary, kBinary, kBinary, kBinary, kBinary, |
Chris Blume | 0b5e7d1 | 2017-09-27 13:23:41 -0700 | [diff] [blame^] | 100 | kBinary, kBinary, kBinary, kBinary, kBinary, kBinary }, |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 101 | { 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] | 102 | 0, |
| 103 | { kKeep, kRestorePrev, kRestorePrev, kRestorePrev, kRestorePrev, |
| 104 | kRestoreBG, kKeep, kRestoreBG, kRestoreBG, kKeep, kKeep, |
| 105 | kRestoreBG, kKeep } }, |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 106 | { "randPixelsAnim2.gif", 4, |
| 107 | // required frames |
| 108 | { 0, 0, 1 }, |
| 109 | // alphas |
| 110 | { kOpaque, kOpaque, kOpaque }, |
| 111 | // durations |
| 112 | { 0, 1000, 170, 40 }, |
| 113 | // repetition count |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 114 | 0, |
| 115 | { kKeep, kKeep, kRestorePrev, kKeep } }, |
Leon Scroggins III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 116 | { "randPixelsAnim.gif", 13, |
| 117 | // required frames |
Chris Blume | 0b5e7d1 | 2017-09-27 13:23:41 -0700 | [diff] [blame^] | 118 | { 0, 1, 2, 3, 4, 3, 6, 7, 7, 7, 9, 9 }, |
Leon Scroggins III | ae79f32 | 2017-08-18 10:53:24 -0400 | [diff] [blame] | 119 | { kBinary, kBinary, kBinary, kBinary, kBinary, kBinary, |
| 120 | kBinary, kBinary, kBinary, kBinary, kBinary, kBinary }, |
Leon Scroggins III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 121 | // durations |
| 122 | { 0, 1000, 170, 40, 220, 7770, 90, 90, 90, 90, 90, 90, 90 }, |
| 123 | // repetition count |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 124 | 0, |
| 125 | { kKeep, kKeep, kKeep, kKeep, kRestoreBG, kRestoreBG, kRestoreBG, |
| 126 | kRestoreBG, kRestorePrev, kRestoreBG, kRestorePrev, kRestorePrev, |
| 127 | kRestorePrev, } }, |
| 128 | { "box.gif", 1, {}, {}, {}, 0, { kKeep } }, |
| 129 | { "color_wheel.gif", 1, {}, {}, {}, 0, { kKeep } }, |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 130 | { "test640x479.gif", 4, { 0, 1, 2 }, |
| 131 | { kOpaque, kOpaque, kOpaque }, |
| 132 | { 200, 200, 200, 200 }, |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 133 | SkCodec::kRepetitionCountInfinite, |
| 134 | { kKeep, kKeep, kKeep, kKeep } }, |
| 135 | { "colorTables.gif", 2, { 0 }, { kOpaque }, { 1000, 1000 }, 5, |
| 136 | { kKeep, kKeep } }, |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 137 | |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 138 | { "arrow.png", 1, {}, {}, {}, 0, {} }, |
| 139 | { "google_chrome.ico", 1, {}, {}, {}, 0, {} }, |
| 140 | { "brickwork-texture.jpg", 1, {}, {}, {}, 0, {} }, |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 141 | #if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32)) |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 142 | { "dng_with_preview.dng", 1, {}, {}, {}, 0, {} }, |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 143 | #endif |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 144 | { "mandrill.wbmp", 1, {}, {}, {}, 0, {} }, |
| 145 | { "randPixels.bmp", 1, {}, {}, {}, 0, {} }, |
| 146 | { "yellow_rose.webp", 1, {}, {}, {}, 0, {} }, |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 147 | { "webp-animated.webp", 3, { 0, 1 }, { kOpaque, kOpaque }, |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 148 | { 1000, 500, 1000 }, SkCodec::kRepetitionCountInfinite, |
| 149 | { kKeep, kKeep, kKeep } }, |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 150 | { "blendBG.webp", 7, { 0, SkCodec::kNone, SkCodec::kNone, SkCodec::kNone, |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 151 | 4, 4 }, |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 152 | { kOpaque, kOpaque, kUnpremul, kOpaque, kUnpremul, kUnpremul }, |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 153 | { 525, 500, 525, 437, 609, 729, 444 }, 7, |
| 154 | { kKeep, kKeep, kKeep, kKeep, kKeep, kKeep, kKeep } }, |
| 155 | { "required.webp", 7, |
| 156 | { 0, 1, 1, SkCodec::kNone, 4, 4 }, |
| 157 | { kOpaque, kUnpremul, kUnpremul, kOpaque, kOpaque, kOpaque }, |
| 158 | { 100, 100, 100, 100, 100, 100, 100 }, |
| 159 | 1, |
| 160 | { kKeep, kRestoreBG, kKeep, kKeep, kKeep, kRestoreBG, kKeep } }, |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 161 | }; |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 162 | #undef kOpaque |
| 163 | #undef kUnpremul |
Leon Scroggins III | ae79f32 | 2017-08-18 10:53:24 -0400 | [diff] [blame] | 164 | #undef kBinary |
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 | |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 178 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(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 | |
Leon Scroggins III | ae79f32 | 2017-08-18 10:53:24 -0400 | [diff] [blame] | 209 | if (rec.fAlphas.size() + 1 != static_cast<size_t>(expected)) { |
| 210 | ERRORF(r, "'%s' has wrong number entries in fAlphas; expected: %i\tactual: %i", |
| 211 | rec.fName, expected - 1, rec.fAlphas.size()); |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 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. |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 230 | codec = SkCodec::MakeFromData(data); |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 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 | ae79f32 | 2017-08-18 10:53:24 -0400 | [diff] [blame] | 273 | auto to_string = [](SkEncodedInfo::Alpha alpha) { |
| 274 | switch (alpha) { |
| 275 | case SkEncodedInfo::kUnpremul_Alpha: |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 276 | return "unpremul"; |
Leon Scroggins III | ae79f32 | 2017-08-18 10:53:24 -0400 | [diff] [blame] | 277 | case SkEncodedInfo::kOpaque_Alpha: |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 278 | return "opaque"; |
Leon Scroggins III | ae79f32 | 2017-08-18 10:53:24 -0400 | [diff] [blame] | 279 | case SkEncodedInfo::kBinary_Alpha: |
| 280 | return "binary"; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 281 | default: |
Leon Scroggins III | ae79f32 | 2017-08-18 10:53:24 -0400 | [diff] [blame] | 282 | SkASSERT(false); |
| 283 | return "unknown"; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 284 | } |
| 285 | }; |
| 286 | |
Leon Scroggins III | ae79f32 | 2017-08-18 10:53:24 -0400 | [diff] [blame] | 287 | auto expectedAlpha = 0 == i ? codec->getEncodedInfo().alpha() : rec.fAlphas[i-1]; |
| 288 | auto alpha = frameInfo.fAlpha; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 289 | if (expectedAlpha != alpha) { |
| 290 | ERRORF(r, "%s's frame %i has wrong alpha type! expected: %s\tactual: %s", |
| 291 | rec.fName, i, to_string(expectedAlpha), to_string(alpha)); |
| 292 | } |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 293 | |
| 294 | if (0 == i) { |
| 295 | REPORTER_ASSERT(r, frameInfo.fRequiredFrame == SkCodec::kNone); |
| 296 | } else if (rec.fRequiredFrames[i-1] != frameInfo.fRequiredFrame) { |
| 297 | ERRORF(r, "%s's frame %i has wrong dependency! expected: %i\tactual: %i", |
| 298 | rec.fName, i, rec.fRequiredFrames[i-1], frameInfo.fRequiredFrame); |
| 299 | } |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 300 | |
| 301 | REPORTER_ASSERT(r, frameInfo.fDisposalMethod == rec.fDisposalMethods[i]); |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | if (TestMode::kIndividual == mode) { |
| 305 | // No need to test decoding twice. |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 306 | continue; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 307 | } |
| 308 | |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 309 | // Compare decoding in multiple ways: |
| 310 | // - Start from scratch for each frame. |codec| will have to decode the required frame |
| 311 | // (and any it depends on) to decode. This is stored in |cachedFrames|. |
| 312 | // - Provide the frame that a frame depends on, so |codec| just has to blend. |
| 313 | // - Provide a frame after the required frame, which will be covered up by the newest |
| 314 | // frame. |
| 315 | // All should look the same. |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 316 | std::vector<SkBitmap> cachedFrames(frameCount); |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 317 | const auto info = codec->getInfo().makeColorType(kN32_SkColorType); |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 318 | |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 319 | auto decode = [&](SkBitmap* bm, int index, int cachedIndex) { |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 320 | auto decodeInfo = info; |
| 321 | if (index > 0) { |
Leon Scroggins III | ae79f32 | 2017-08-18 10:53:24 -0400 | [diff] [blame] | 322 | auto alphaType = frameInfos[index].fAlpha == SkEncodedInfo::kOpaque_Alpha |
| 323 | ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
| 324 | decodeInfo = info.makeAlphaType(alphaType); |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 325 | } |
| 326 | bm->allocPixels(decodeInfo); |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 327 | if (cachedIndex != SkCodec::kNone) { |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 328 | // First copy the pixels from the cached frame |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 329 | const bool success = sk_tool_utils::copy_to(bm, kN32_SkColorType, |
| 330 | cachedFrames[cachedIndex]); |
| 331 | REPORTER_ASSERT(r, success); |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 332 | } |
| 333 | SkCodec::Options opts; |
| 334 | opts.fFrameIndex = index; |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 335 | opts.fPriorFrame = cachedIndex; |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 336 | const auto result = codec->getPixels(decodeInfo, bm->getPixels(), bm->rowBytes(), |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 337 | &opts); |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 338 | if (cachedIndex != SkCodec::kNone && restore_previous(frameInfos[cachedIndex])) { |
| 339 | if (result == SkCodec::kInvalidParameters) { |
| 340 | return true; |
| 341 | } |
| 342 | ERRORF(r, "Using a kRestorePrevious frame as fPriorFrame should fail"); |
| 343 | return false; |
| 344 | } |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 345 | if (result != SkCodec::kSuccess) { |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 346 | ERRORF(r, "Failed to decode frame %i from %s when providing prior frame %i, " |
| 347 | "error %i", index, rec.fName, cachedIndex, result); |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 348 | } |
| 349 | return result == SkCodec::kSuccess; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 350 | }; |
| 351 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 352 | for (int i = 0; i < frameCount; i++) { |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 353 | SkBitmap& cachedFrame = cachedFrames[i]; |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 354 | if (!decode(&cachedFrame, i, SkCodec::kNone)) { |
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 | const auto reqFrame = frameInfos[i].fRequiredFrame; |
| 358 | if (reqFrame == SkCodec::kNone) { |
| 359 | // Nothing to compare against. |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 360 | continue; |
| 361 | } |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 362 | for (int j = reqFrame; j < i; j++) { |
| 363 | SkBitmap frame; |
| 364 | if (restore_previous(frameInfos[j])) { |
| 365 | (void) decode(&frame, i, j); |
| 366 | continue; |
| 367 | } |
| 368 | if (!decode(&frame, i, j)) { |
| 369 | continue; |
| 370 | } |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 371 | |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 372 | // Now verify they're equal. |
| 373 | const size_t rowLen = info.bytesPerPixel() * info.width(); |
| 374 | for (int y = 0; y < info.height(); y++) { |
| 375 | const void* cachedAddr = cachedFrame.getAddr(0, y); |
| 376 | SkASSERT(cachedAddr != nullptr); |
| 377 | const void* addr = frame.getAddr(0, y); |
| 378 | SkASSERT(addr != nullptr); |
| 379 | const bool lineMatches = memcmp(cachedAddr, addr, rowLen) == 0; |
| 380 | if (!lineMatches) { |
| 381 | SkString name = SkStringPrintf("cached_%i", i); |
| 382 | write_bm(name.c_str(), cachedFrame); |
| 383 | name = SkStringPrintf("frame_%i", i); |
| 384 | write_bm(name.c_str(), frame); |
| 385 | ERRORF(r, "%s's frame %i is different (starting from line %i) when " |
| 386 | "providing prior frame %i!", rec.fName, i, y, j); |
| 387 | break; |
| 388 | } |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 389 | } |
| 390 | } |
Leon Scroggins III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 391 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | } |