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