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