OLD | NEW |
(Empty) | |
| 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 |
| 8 #include "SkCodec.h" |
| 9 #include "SkStream.h" |
| 10 |
| 11 #include "Resources.h" |
| 12 #include "Test.h" |
| 13 |
| 14 #include <initializer_list> |
| 15 #include <vector> |
| 16 |
| 17 DEF_TEST(Codec_frames, r) { |
| 18 static const struct { |
| 19 const char* fName; |
| 20 size_t fFrameCount; |
| 21 // One less than fFramecount, since the first frame is always |
| 22 // independent. |
| 23 std::vector<size_t> fRequiredFrames; |
| 24 // The size of this one should match fFrameCount for animated, empty |
| 25 // otherwise. |
| 26 std::vector<size_t> fDurations; |
| 27 } gRecs[] = { |
| 28 { "box.gif", 1, {}, {} }, |
| 29 { "color_wheel.gif", 1, {}, {} }, |
| 30 { "test640x479.gif", 4, { 0, 1, 2 }, { 200, 200, 200, 200 } }, |
| 31 |
| 32 { "arrow.png", 1, {}, {} }, |
| 33 { "google_chrome.ico", 1, {}, {} }, |
| 34 { "brickwork-texture.jpg", 1, {}, {} }, |
| 35 { "dng_with_preview.dng", 1, {}, {} }, |
| 36 { "mandrill.wbmp", 1, {}, {} }, |
| 37 { "randPixels.bmp", 1, {}, {} }, |
| 38 { "yellow_rose.webp", 1, {}, {} }, |
| 39 }; |
| 40 |
| 41 for (auto rec : gRecs) { |
| 42 std::unique_ptr<SkStream> stream(GetResourceAsStream(rec.fName)); |
| 43 if (!stream) { |
| 44 // Useful error statement, but sometimes people run tests without |
| 45 // resources, and they do not want to see these messages. |
| 46 //ERRORF(r, "Missing resources? Could not find '%s'", rec.fName); |
| 47 continue; |
| 48 } |
| 49 |
| 50 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()))
; |
| 51 if (!codec) { |
| 52 ERRORF(r, "Failed to create an SkCodec from '%s'", rec.fName); |
| 53 continue; |
| 54 } |
| 55 |
| 56 const size_t expected = rec.fFrameCount; |
| 57 const auto frameInfos = codec->getFrameInfo(); |
| 58 // getFrameInfo returns empty set for non-animated. |
| 59 const size_t frameCount = frameInfos.size() == 0 ? 1 : frameInfos.size()
; |
| 60 if (frameCount != expected) { |
| 61 ERRORF(r, "'%s' expected frame count: %i\tactual: %i", rec.fName, ex
pected, frameCount); |
| 62 continue; |
| 63 } |
| 64 |
| 65 if (rec.fRequiredFrames.size() + 1 != expected) { |
| 66 ERRORF(r, "'%s' has wrong number entries in fRequiredFrames; expecte
d: %i\tactual: %i", |
| 67 rec.fName, expected, rec.fRequiredFrames.size()); |
| 68 continue; |
| 69 } |
| 70 |
| 71 if (1 == frameCount) { |
| 72 continue; |
| 73 } |
| 74 |
| 75 // From here on, we are only concerned with animated images. |
| 76 REPORTER_ASSERT(r, frameInfos[0].fRequiredFrame == SkCodec::kIndependent
Frame); |
| 77 for (size_t i = 1; i < frameCount; i++) { |
| 78 REPORTER_ASSERT(r, rec.fRequiredFrames[i-1] == frameInfos[i].fRequir
edFrame); |
| 79 } |
| 80 |
| 81 // Compare decoding in two ways: |
| 82 // 1. Provide the frame that a frame depends on, so the codec just has t
o blend. |
| 83 // (in the array cachedFrames) |
| 84 // 2. Do not provide the frame that a frame depends on, so the codec has
to decode all the |
| 85 // way back to a key-frame. (in a local variable uncachedFrame) |
| 86 // The two should look the same. |
| 87 std::vector<SkBitmap> cachedFrames(frameCount); |
| 88 const auto& info = codec->getInfo().makeColorType(kN32_SkColorType); |
| 89 |
| 90 auto decode = [&](SkBitmap* bm, bool cached, size_t index) { |
| 91 bm->allocPixels(info); |
| 92 if (cached) { |
| 93 // First copy the pixels from the cached frame |
| 94 const size_t requiredFrame = frameInfos[index].fRequiredFrame; |
| 95 if (requiredFrame != SkCodec::kIndependentFrame) { |
| 96 const bool success = cachedFrames[requiredFrame].copyTo(bm); |
| 97 REPORTER_ASSERT(r, success); |
| 98 } |
| 99 } |
| 100 SkCodec::Options opts; |
| 101 SkCodec::MultiFrameOptions multiOpts; |
| 102 multiOpts.fIndex = index; |
| 103 multiOpts.fHasPriorFrame = cached; |
| 104 opts.fFrameOptions = &multiOpts; |
| 105 const SkCodec::Result result = codec->getPixels(info, bm->getPixels(
), bm->rowBytes(), |
| 106 &opts, nullptr, null
ptr); |
| 107 REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 108 }; |
| 109 |
| 110 for (size_t i = 0; i < frameCount; i++) { |
| 111 SkBitmap& cachedFrame = cachedFrames[i]; |
| 112 decode(&cachedFrame, true, i); |
| 113 SkBitmap uncachedFrame; |
| 114 decode(&uncachedFrame, false, i); |
| 115 |
| 116 // Now verify they're equal. |
| 117 const size_t rowLen = info.bytesPerPixel() * info.width(); |
| 118 for (int y = 0; y < info.height(); y++) { |
| 119 const void* cachedAddr = cachedFrame.getAddr(0, y); |
| 120 SkASSERT(cachedAddr != nullptr); |
| 121 const void* uncachedAddr = uncachedFrame.getAddr(0, y); |
| 122 SkASSERT(uncachedAddr != nullptr); |
| 123 const bool lineMatches = memcmp(cachedAddr, uncachedAddr, rowLen
) == 0; |
| 124 if (!lineMatches) { |
| 125 ERRORF(r, "%s's frame %i is different depending on caching!"
, rec.fName, i); |
| 126 break; |
| 127 } |
| 128 } |
| 129 } |
| 130 |
| 131 if (rec.fDurations.size() != expected) { |
| 132 ERRORF(r, "'%s' has wrong number entries in fDurations; expected: %i
\tactual: %i", |
| 133 rec.fName, expected, rec.fDurations.size()); |
| 134 continue; |
| 135 } |
| 136 |
| 137 for (size_t i = 0; i < frameCount; i++) { |
| 138 REPORTER_ASSERT(r, rec.fDurations[i] == frameInfos[i].fDuration); |
| 139 } |
| 140 } |
| 141 } |
OLD | NEW |