Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: tests/CodecAnimTest.cpp

Issue 2045293002: Add support for multiple frames in SkCodec (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Disable DNG if Raw is disabled Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
36 { "dng_with_preview.dng", 1, {}, {} },
37 #endif
38 { "mandrill.wbmp", 1, {}, {} },
39 { "randPixels.bmp", 1, {}, {} },
40 { "yellow_rose.webp", 1, {}, {} },
41 };
42
43 for (auto rec : gRecs) {
44 std::unique_ptr<SkStream> stream(GetResourceAsStream(rec.fName));
45 if (!stream) {
46 // Useful error statement, but sometimes people run tests without
47 // resources, and they do not want to see these messages.
48 //ERRORF(r, "Missing resources? Could not find '%s'", rec.fName);
49 continue;
50 }
51
52 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release())) ;
53 if (!codec) {
54 ERRORF(r, "Failed to create an SkCodec from '%s'", rec.fName);
55 continue;
56 }
57
58 const size_t expected = rec.fFrameCount;
59 const auto frameInfos = codec->getFrameInfo();
60 // getFrameInfo returns empty set for non-animated.
61 const size_t frameCount = frameInfos.size() == 0 ? 1 : frameInfos.size() ;
62 if (frameCount != expected) {
63 ERRORF(r, "'%s' expected frame count: %i\tactual: %i", rec.fName, ex pected, frameCount);
64 continue;
65 }
66
67 if (rec.fRequiredFrames.size() + 1 != expected) {
68 ERRORF(r, "'%s' has wrong number entries in fRequiredFrames; expecte d: %i\tactual: %i",
69 rec.fName, expected, rec.fRequiredFrames.size());
70 continue;
71 }
72
73 if (1 == frameCount) {
74 continue;
75 }
76
77 // From here on, we are only concerned with animated images.
78 REPORTER_ASSERT(r, frameInfos[0].fRequiredFrame == SkCodec::kNone);
79 for (size_t i = 1; i < frameCount; i++) {
80 REPORTER_ASSERT(r, rec.fRequiredFrames[i-1] == frameInfos[i].fRequir edFrame);
81 }
82
83 // Compare decoding in two ways:
84 // 1. Provide the frame that a frame depends on, so the codec just has t o blend.
85 // (in the array cachedFrames)
86 // 2. Do not provide the frame that a frame depends on, so the codec has to decode all the
87 // way back to a key-frame. (in a local variable uncachedFrame)
88 // The two should look the same.
89 std::vector<SkBitmap> cachedFrames(frameCount);
90 const auto& info = codec->getInfo().makeColorType(kN32_SkColorType);
91
92 auto decode = [&](SkBitmap* bm, bool cached, size_t index) {
93 bm->allocPixels(info);
94 if (cached) {
95 // First copy the pixels from the cached frame
96 const size_t requiredFrame = frameInfos[index].fRequiredFrame;
97 if (requiredFrame != SkCodec::kNone) {
98 const bool success = cachedFrames[requiredFrame].copyTo(bm);
99 REPORTER_ASSERT(r, success);
100 }
101 }
102 SkCodec::Options opts;
103 SkCodec::MultiFrameOptions multiOpts;
104 multiOpts.fIndex = index;
105 multiOpts.fHasPriorFrame = cached;
106 opts.fFrameOptions = &multiOpts;
107 const SkCodec::Result result = codec->getPixels(info, bm->getPixels( ), bm->rowBytes(),
108 &opts, nullptr, null ptr);
109 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
110 };
111
112 for (size_t i = 0; i < frameCount; i++) {
113 SkBitmap& cachedFrame = cachedFrames[i];
114 decode(&cachedFrame, true, i);
115 SkBitmap uncachedFrame;
116 decode(&uncachedFrame, false, i);
117
118 // Now verify they're equal.
119 const size_t rowLen = info.bytesPerPixel() * info.width();
120 for (int y = 0; y < info.height(); y++) {
121 const void* cachedAddr = cachedFrame.getAddr(0, y);
122 SkASSERT(cachedAddr != nullptr);
123 const void* uncachedAddr = uncachedFrame.getAddr(0, y);
124 SkASSERT(uncachedAddr != nullptr);
125 const bool lineMatches = memcmp(cachedAddr, uncachedAddr, rowLen ) == 0;
126 if (!lineMatches) {
127 ERRORF(r, "%s's frame %i is different depending on caching!" , rec.fName, i);
128 break;
129 }
130 }
131 }
132
133 if (rec.fDurations.size() != expected) {
134 ERRORF(r, "'%s' has wrong number entries in fDurations; expected: %i \tactual: %i",
135 rec.fName, expected, rec.fDurations.size());
136 continue;
137 }
138
139 for (size_t i = 0; i < frameCount; i++) {
140 REPORTER_ASSERT(r, rec.fDurations[i] == frameInfos[i].fDuration);
141 }
142 }
143 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698