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 | } |
Leon Scroggins III | e93ec68 | 2018-10-26 09:25:51 -0400 | [diff] [blame] | 33 | // See also Codec_GifTruncated2 in GifTest.cpp for this magic 23. |
| 34 | // |
| 35 | // TODO: just move this getFrameInfo call to Codec_GifTruncated2? |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 36 | SkCodec::MakeFromData(SkData::MakeSubset(data.get(), 0, 23))->getFrameInfo(); |
Leon Scroggins III | 7d22a33 | 2017-04-12 17:01:26 -0400 | [diff] [blame] | 37 | } |
| 38 | |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 39 | // 565 does not support alpha, but there is no reason for it not to support an |
| 40 | // animated image with a frame that has alpha but then blends onto an opaque |
| 41 | // frame making the result opaque. Test that we can decode such a frame. |
| 42 | DEF_TEST(Codec_565, r) { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 43 | sk_sp<SkData> data(GetResourceAsData("images/blendBG.webp")); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 44 | if (!data) { |
| 45 | return; |
| 46 | } |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 47 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(std::move(data))); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 48 | auto info = codec->getInfo().makeColorType(kRGB_565_SkColorType); |
| 49 | SkBitmap bm; |
| 50 | bm.allocPixels(info); |
| 51 | |
| 52 | SkCodec::Options options; |
| 53 | options.fFrameIndex = 1; |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 54 | options.fPriorFrame = SkCodec::kNoFrame; |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 55 | |
| 56 | const auto result = codec->getPixels(info, bm.getPixels(), bm.rowBytes(), |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 57 | &options); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 58 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 59 | } |
| 60 | |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 61 | static bool restore_previous(const SkCodec::FrameInfo& info) { |
| 62 | return info.fDisposalMethod == SkCodecAnimation::DisposalMethod::kRestorePrevious; |
| 63 | } |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 64 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 65 | DEF_TEST(Codec_frames, r) { |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 66 | constexpr int kNoFrame = SkCodec::kNoFrame; |
| 67 | constexpr SkAlphaType kOpaque = kOpaque_SkAlphaType; |
| 68 | constexpr SkAlphaType kUnpremul = kUnpremul_SkAlphaType; |
| 69 | constexpr SkCodecAnimation::DisposalMethod kKeep = |
| 70 | SkCodecAnimation::DisposalMethod::kKeep; |
| 71 | constexpr SkCodecAnimation::DisposalMethod kRestoreBG = |
| 72 | SkCodecAnimation::DisposalMethod::kRestoreBGColor; |
| 73 | constexpr SkCodecAnimation::DisposalMethod kRestorePrev = |
| 74 | SkCodecAnimation::DisposalMethod::kRestorePrevious; |
| 75 | |
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 | c8037dc | 2017-12-05 13:55:24 -0500 | [diff] [blame] | 82 | // Same, since the first frame should match getInfo |
| 83 | std::vector<SkAlphaType> 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[] = { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 90 | { "images/required.gif", 7, |
Chris Blume | 0b5e7d1 | 2017-09-27 13:23:41 -0700 | [diff] [blame] | 91 | { 0, 1, 2, 3, 4, 5 }, |
Leon Scroggins III | c8037dc | 2017-12-05 13:55:24 -0500 | [diff] [blame] | 92 | { kOpaque, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul }, |
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 } }, |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 96 | { "images/alphabetAnim.gif", 13, |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 97 | { kNoFrame, 0, 0, 0, 0, 5, 6, kNoFrame, kNoFrame, 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 } }, |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 149 | { "images/blendBG.webp", 7, |
| 150 | { 0, kNoFrame, kNoFrame, kNoFrame, 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, |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 155 | { 0, 1, 1, kNoFrame, 4, 4 }, |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 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 | }; |
| 161 | |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 162 | for (const auto& rec : gRecs) { |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 163 | sk_sp<SkData> data(GetResourceAsData(rec.fName)); |
| 164 | if (!data) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 165 | // Useful error statement, but sometimes people run tests without |
| 166 | // resources, and they do not want to see these messages. |
| 167 | //ERRORF(r, "Missing resources? Could not find '%s'", rec.fName); |
| 168 | continue; |
| 169 | } |
| 170 | |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 171 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data)); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 172 | if (!codec) { |
| 173 | ERRORF(r, "Failed to create an SkCodec from '%s'", rec.fName); |
| 174 | continue; |
| 175 | } |
| 176 | |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 177 | { |
| 178 | SkCodec::FrameInfo frameInfo; |
| 179 | REPORTER_ASSERT(r, !codec->getFrameInfo(0, &frameInfo)); |
| 180 | } |
| 181 | |
scroggo | e71b1a1 | 2016-11-01 08:28:28 -0700 | [diff] [blame] | 182 | const int repetitionCount = codec->getRepetitionCount(); |
| 183 | if (repetitionCount != rec.fRepetitionCount) { |
| 184 | ERRORF(r, "%s repetition count does not match! expected: %i\tactual: %i", |
| 185 | rec.fName, rec.fRepetitionCount, repetitionCount); |
| 186 | } |
| 187 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 188 | const int expected = rec.fFrameCount; |
| 189 | if (rec.fRequiredFrames.size() + 1 != static_cast<size_t>(expected)) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 190 | 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] | 191 | rec.fName, expected - 1, rec.fRequiredFrames.size()); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 192 | continue; |
| 193 | } |
| 194 | |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 195 | if (expected > 1) { |
| 196 | if (rec.fDurations.size() != static_cast<size_t>(expected)) { |
| 197 | ERRORF(r, "'%s' has wrong number entries in fDurations; expected: %i\tactual: %i", |
| 198 | rec.fName, expected, rec.fDurations.size()); |
| 199 | continue; |
| 200 | } |
| 201 | |
Leon Scroggins III | ae79f32 | 2017-08-18 10:53:24 -0400 | [diff] [blame] | 202 | if (rec.fAlphas.size() + 1 != static_cast<size_t>(expected)) { |
| 203 | ERRORF(r, "'%s' has wrong number entries in fAlphas; expected: %i\tactual: %i", |
| 204 | rec.fName, expected - 1, rec.fAlphas.size()); |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 205 | continue; |
| 206 | } |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 207 | |
| 208 | if (rec.fDisposalMethods.size() != static_cast<size_t>(expected)) { |
| 209 | ERRORF(r, "'%s' has wrong number entries in fDisposalMethods; " |
| 210 | "expected %i\tactual: %i", |
| 211 | rec.fName, expected, rec.fDisposalMethods.size()); |
| 212 | continue; |
| 213 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 216 | enum class TestMode { |
| 217 | kVector, |
| 218 | kIndividual, |
| 219 | }; |
| 220 | |
| 221 | for (auto mode : { TestMode::kVector, TestMode::kIndividual }) { |
| 222 | // Re-create the codec to reset state and test parsing. |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 223 | codec = SkCodec::MakeFromData(data); |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 224 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 225 | int frameCount; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 226 | std::vector<SkCodec::FrameInfo> frameInfos; |
| 227 | switch (mode) { |
| 228 | case TestMode::kVector: |
| 229 | frameInfos = codec->getFrameInfo(); |
| 230 | // getFrameInfo returns empty set for non-animated. |
| 231 | frameCount = frameInfos.empty() ? 1 : frameInfos.size(); |
| 232 | break; |
| 233 | case TestMode::kIndividual: |
| 234 | frameCount = codec->getFrameCount(); |
| 235 | break; |
| 236 | } |
| 237 | |
| 238 | if (frameCount != expected) { |
| 239 | ERRORF(r, "'%s' expected frame count: %i\tactual: %i", |
| 240 | rec.fName, expected, frameCount); |
| 241 | continue; |
| 242 | } |
| 243 | |
| 244 | // From here on, we are only concerned with animated images. |
| 245 | if (1 == frameCount) { |
| 246 | continue; |
| 247 | } |
| 248 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 249 | for (int i = 0; i < frameCount; i++) { |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 250 | SkCodec::FrameInfo frameInfo; |
| 251 | switch (mode) { |
| 252 | case TestMode::kVector: |
| 253 | frameInfo = frameInfos[i]; |
| 254 | break; |
| 255 | case TestMode::kIndividual: |
| 256 | REPORTER_ASSERT(r, codec->getFrameInfo(i, nullptr)); |
| 257 | REPORTER_ASSERT(r, codec->getFrameInfo(i, &frameInfo)); |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | if (rec.fDurations[i] != frameInfo.fDuration) { |
| 262 | ERRORF(r, "%s frame %i's durations do not match! expected: %i\tactual: %i", |
| 263 | rec.fName, i, rec.fDurations[i], frameInfo.fDuration); |
| 264 | } |
| 265 | |
Leon Scroggins III | c8037dc | 2017-12-05 13:55:24 -0500 | [diff] [blame] | 266 | auto to_string = [](SkAlphaType alpha) { |
Leon Scroggins III | ae79f32 | 2017-08-18 10:53:24 -0400 | [diff] [blame] | 267 | switch (alpha) { |
Leon Scroggins III | c8037dc | 2017-12-05 13:55:24 -0500 | [diff] [blame] | 268 | case kUnpremul_SkAlphaType: |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 269 | return "unpremul"; |
Leon Scroggins III | c8037dc | 2017-12-05 13:55:24 -0500 | [diff] [blame] | 270 | case kOpaque_SkAlphaType: |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 271 | return "opaque"; |
| 272 | default: |
Leon Scroggins III | ae79f32 | 2017-08-18 10:53:24 -0400 | [diff] [blame] | 273 | SkASSERT(false); |
| 274 | return "unknown"; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 275 | } |
| 276 | }; |
| 277 | |
Leon Scroggins III | c8037dc | 2017-12-05 13:55:24 -0500 | [diff] [blame] | 278 | auto expectedAlpha = 0 == i ? codec->getInfo().alphaType() : rec.fAlphas[i-1]; |
| 279 | auto alpha = frameInfo.fAlphaType; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 280 | if (expectedAlpha != alpha) { |
| 281 | ERRORF(r, "%s's frame %i has wrong alpha type! expected: %s\tactual: %s", |
| 282 | rec.fName, i, to_string(expectedAlpha), to_string(alpha)); |
| 283 | } |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 284 | |
| 285 | if (0 == i) { |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 286 | REPORTER_ASSERT(r, frameInfo.fRequiredFrame == SkCodec::kNoFrame); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 287 | } else if (rec.fRequiredFrames[i-1] != frameInfo.fRequiredFrame) { |
| 288 | ERRORF(r, "%s's frame %i has wrong dependency! expected: %i\tactual: %i", |
| 289 | rec.fName, i, rec.fRequiredFrames[i-1], frameInfo.fRequiredFrame); |
| 290 | } |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 291 | |
| 292 | REPORTER_ASSERT(r, frameInfo.fDisposalMethod == rec.fDisposalMethods[i]); |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | if (TestMode::kIndividual == mode) { |
| 296 | // No need to test decoding twice. |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 297 | continue; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 298 | } |
| 299 | |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 300 | // Compare decoding in multiple ways: |
| 301 | // - Start from scratch for each frame. |codec| will have to decode the required frame |
| 302 | // (and any it depends on) to decode. This is stored in |cachedFrames|. |
| 303 | // - Provide the frame that a frame depends on, so |codec| just has to blend. |
| 304 | // - Provide a frame after the required frame, which will be covered up by the newest |
| 305 | // frame. |
| 306 | // All should look the same. |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 307 | std::vector<SkBitmap> cachedFrames(frameCount); |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 308 | const auto info = codec->getInfo().makeColorType(kN32_SkColorType); |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 309 | |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 310 | auto decode = [&](SkBitmap* bm, int index, int cachedIndex) { |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 311 | auto decodeInfo = info; |
| 312 | if (index > 0) { |
Leon Scroggins III | c8037dc | 2017-12-05 13:55:24 -0500 | [diff] [blame] | 313 | decodeInfo = info.makeAlphaType(frameInfos[index].fAlphaType); |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 314 | } |
| 315 | bm->allocPixels(decodeInfo); |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 316 | if (cachedIndex != SkCodec::kNoFrame) { |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 317 | // First copy the pixels from the cached frame |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 318 | const bool success = sk_tool_utils::copy_to(bm, kN32_SkColorType, |
| 319 | cachedFrames[cachedIndex]); |
| 320 | REPORTER_ASSERT(r, success); |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 321 | } |
| 322 | SkCodec::Options opts; |
| 323 | opts.fFrameIndex = index; |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 324 | opts.fPriorFrame = cachedIndex; |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 325 | const auto result = codec->getPixels(decodeInfo, bm->getPixels(), bm->rowBytes(), |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 326 | &opts); |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 327 | if (cachedIndex != SkCodec::kNoFrame && |
| 328 | restore_previous(frameInfos[cachedIndex])) { |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 329 | if (result == SkCodec::kInvalidParameters) { |
| 330 | return true; |
| 331 | } |
| 332 | ERRORF(r, "Using a kRestorePrevious frame as fPriorFrame should fail"); |
| 333 | return false; |
| 334 | } |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 335 | if (result != SkCodec::kSuccess) { |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 336 | ERRORF(r, "Failed to decode frame %i from %s when providing prior frame %i, " |
| 337 | "error %i", index, rec.fName, cachedIndex, result); |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 338 | } |
| 339 | return result == SkCodec::kSuccess; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 340 | }; |
| 341 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 342 | for (int i = 0; i < frameCount; i++) { |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 343 | SkBitmap& cachedFrame = cachedFrames[i]; |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 344 | if (!decode(&cachedFrame, i, SkCodec::kNoFrame)) { |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 345 | continue; |
| 346 | } |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 347 | const auto reqFrame = frameInfos[i].fRequiredFrame; |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 348 | if (reqFrame == SkCodec::kNoFrame) { |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 349 | // Nothing to compare against. |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 350 | continue; |
| 351 | } |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 352 | for (int j = reqFrame; j < i; j++) { |
| 353 | SkBitmap frame; |
| 354 | if (restore_previous(frameInfos[j])) { |
| 355 | (void) decode(&frame, i, j); |
| 356 | continue; |
| 357 | } |
| 358 | if (!decode(&frame, i, j)) { |
| 359 | continue; |
| 360 | } |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 361 | |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 362 | // Now verify they're equal. |
| 363 | const size_t rowLen = info.bytesPerPixel() * info.width(); |
| 364 | for (int y = 0; y < info.height(); y++) { |
| 365 | const void* cachedAddr = cachedFrame.getAddr(0, y); |
| 366 | SkASSERT(cachedAddr != nullptr); |
| 367 | const void* addr = frame.getAddr(0, y); |
| 368 | SkASSERT(addr != nullptr); |
| 369 | const bool lineMatches = memcmp(cachedAddr, addr, rowLen) == 0; |
| 370 | if (!lineMatches) { |
| 371 | SkString name = SkStringPrintf("cached_%i", i); |
| 372 | write_bm(name.c_str(), cachedFrame); |
| 373 | name = SkStringPrintf("frame_%i", i); |
| 374 | write_bm(name.c_str(), frame); |
| 375 | ERRORF(r, "%s's frame %i is different (starting from line %i) when " |
| 376 | "providing prior frame %i!", rec.fName, i, y, j); |
| 377 | break; |
| 378 | } |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 379 | } |
| 380 | } |
Leon Scroggins III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 381 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | } |
Leon Scroggins III | 42ee284 | 2018-01-14 14:46:51 -0500 | [diff] [blame] | 385 | |
| 386 | // Verify that a webp image can be animated scaled down. This image has a |
| 387 | // kRestoreBG frame, so it is an interesting image to test. After decoding that |
| 388 | // frame, we have to erase its rectangle. The rectangle has to be adjusted |
| 389 | // based on the scaled size. |
| 390 | DEF_TEST(AndroidCodec_animated, r) { |
| 391 | if (GetResourcePath().isEmpty()) { |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | const char* file = "images/required.webp"; |
| 396 | sk_sp<SkData> data(GetResourceAsData(file)); |
| 397 | if (!data) { |
| 398 | ERRORF(r, "Missing %s", file); |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | auto codec = SkAndroidCodec::MakeFromCodec(SkCodec::MakeFromData(std::move(data))); |
| 403 | if (!codec) { |
| 404 | ERRORF(r, "Failed to decode %s", file); |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | auto info = codec->getInfo().makeAlphaType(kPremul_SkAlphaType); |
| 409 | |
| 410 | for (int sampleSize : { 8, 32, 100 }) { |
| 411 | auto dimensions = codec->codec()->getScaledDimensions(1.0f / sampleSize); |
| 412 | info = info.makeWH(dimensions.width(), dimensions.height()); |
| 413 | SkBitmap bm; |
| 414 | bm.allocPixels(info); |
| 415 | |
| 416 | SkCodec::Options options; |
| 417 | for (int i = 0; i < codec->codec()->getFrameCount(); ++i) { |
| 418 | SkCodec::FrameInfo frameInfo; |
| 419 | REPORTER_ASSERT(r, codec->codec()->getFrameInfo(i, &frameInfo)); |
| 420 | if (5 == i) { |
| 421 | REPORTER_ASSERT(r, frameInfo.fDisposalMethod |
| 422 | == SkCodecAnimation::DisposalMethod::kRestoreBGColor); |
| 423 | } |
| 424 | options.fFrameIndex = i; |
| 425 | options.fPriorFrame = i - 1; |
| 426 | info = info.makeAlphaType(frameInfo.fAlphaType); |
| 427 | |
Leon Scroggins III | 7916c0e | 2018-05-24 13:04:06 -0400 | [diff] [blame] | 428 | auto result = codec->codec()->getPixels(info, bm.getPixels(), bm.rowBytes(), |
| 429 | &options); |
Leon Scroggins III | 42ee284 | 2018-01-14 14:46:51 -0500 | [diff] [blame] | 430 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
Leon Scroggins III | 7916c0e | 2018-05-24 13:04:06 -0400 | [diff] [blame] | 431 | |
| 432 | // Now compare to not using prior frame. |
| 433 | SkBitmap bm2; |
| 434 | bm2.allocPixels(info); |
| 435 | |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 436 | options.fPriorFrame = SkCodec::kNoFrame; |
Leon Scroggins III | 7916c0e | 2018-05-24 13:04:06 -0400 | [diff] [blame] | 437 | result = codec->codec()->getPixels(info, bm2.getPixels(), bm2.rowBytes(), |
| 438 | &options); |
| 439 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 440 | |
| 441 | for (int y = 0; y < info.height(); ++y) { |
| 442 | if (memcmp(bm.getAddr32(0, y), bm2.getAddr32(0, y), info.minRowBytes())) { |
| 443 | ERRORF(r, "pixel mismatch for sample size %i, frame %i resulting in " |
| 444 | "dimensions %i x %i line %i\n", |
| 445 | sampleSize, i, info.width(), info.height(), y); |
| 446 | break; |
| 447 | } |
| 448 | } |
Leon Scroggins III | 42ee284 | 2018-01-14 14:46:51 -0500 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | } |