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