CodecAnimTest.cpp 20 KB

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