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

Side by Side Diff: tests/CodecPartialTest.cpp

Issue 1997703003: Make SkPngCodec decode progressively. (Closed) Base URL: https://skia.googlesource.com/skia.git@foil
Patch Set: Rebase Created 4 years, 3 months 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
« no previous file with comments | « src/codec/SkSampler.h ('k') | tests/CodecTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "SkBitmap.h"
9 #include "SkCodec.h"
10 #include "SkData.h"
11 #include "SkImageInfo.h"
12 #include "SkRWBuffer.h"
13 #include "SkString.h"
14
15 #include "Resources.h"
16 #include "Test.h"
17
18 static sk_sp<SkData> make_from_resource(const char* name) {
19 SkString fullPath = GetResourcePath(name);
20 return SkData::MakeFromFileName(fullPath.c_str());
21 }
22
23 static SkImageInfo standardize_info(SkCodec* codec) {
24 SkImageInfo defaultInfo = codec->getInfo();
25 // Note: This drops the SkColorSpace, allowing the equality check between tw o
26 // different codecs created from the same file to have the same SkImageInfo.
27 return SkImageInfo::MakeN32Premul(defaultInfo.width(), defaultInfo.height()) ;
28 }
29
30 static bool create_truth(sk_sp<SkData> data, SkBitmap* dst) {
31 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(std::move(data)));
32 if (!codec) {
33 return false;
34 }
35
36 const SkImageInfo info = standardize_info(codec);
37 dst->allocPixels(info);
38 return SkCodec::kSuccess == codec->getPixels(info, dst->getPixels(), dst->ro wBytes());
39 }
40
41 /*
42 * Represents a stream without all of its data.
43 */
44 class HaltingStream : public SkStream {
45 public:
46 HaltingStream(sk_sp<SkData> data)
47 : fTotalSize(data->size())
48 , fLimit(fTotalSize / 2)
49 , fStream(std::move(data))
50 {}
51
52 void addNewData() {
53 // Arbitrary size, but deliberately different from
54 // the buffer size used by SkPngCodec.
55 fLimit = SkTMin(fTotalSize, fLimit + 1000);
56 }
57
58 size_t read(void* buffer, size_t size) override {
59 if (fStream.getPosition() + size > fLimit) {
60 size = fLimit - fStream.getPosition();
61 }
62
63 return fStream.read(buffer, size);
64 }
65
66 bool isAtEnd() const override {
67 return fStream.isAtEnd();
68 }
69
70 bool hasPosition() const override { return true; }
71 size_t getPosition() const override { return fStream.getPosition(); }
72 bool rewind() override { return fStream.rewind(); }
73 bool move(long offset) override { return fStream.move(offset); }
74
75 private:
76 const size_t fTotalSize;
77 size_t fLimit;
78 SkMemoryStream fStream;
79 };
80
81 static void test_partial(skiatest::Reporter* r, const char* name) {
82 sk_sp<SkData> file = make_from_resource(name);
83 if (!file) {
84 SkDebugf("missing resource %s\n", name);
85 return;
86 }
87
88 SkBitmap truth;
89 if (!create_truth(file, &truth)) {
90 ERRORF(r, "Failed to decode %s\n", name);
91 return;
92 }
93
94 const size_t fileSize = file->size();
95
96 // Now decode part of the file
97 HaltingStream* stream = new HaltingStream(file);
98
99 // Note that we cheat and hold on to a pointer to stream, though it is owned by
100 // partialCodec.
101 SkAutoTDelete<SkCodec> partialCodec(SkCodec::NewFromStream(stream));
102 if (!partialCodec) {
103 // Technically, this could be a small file where half the file is not
104 // enough.
105 ERRORF(r, "Failed to create codec for %s", name);
106 return;
107 }
108
109 const SkImageInfo info = standardize_info(partialCodec);
110 SkASSERT(info == truth.info());
111 SkBitmap incremental;
112 incremental.allocPixels(info);
113
114 const SkCodec::Result startResult = partialCodec->startIncrementalDecode(inf o,
115 incremental.getPixels(), incremental.rowBytes());
116 if (startResult != SkCodec::kSuccess) {
117 ERRORF(r, "Failed to start incremental decode\n");
118 return;
119 }
120
121 while (true) {
122 const SkCodec::Result result = partialCodec->incrementalDecode();
123
124 if (stream->getPosition() == fileSize) {
125 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
126 break;
127 }
128
129 SkASSERT(stream->getPosition() < fileSize);
130
131 REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput);
132
133 // Append an arbitrary amount of data.
134 stream->addNewData();
135 }
136
137 // compare to original
138 for (int i = 0; i < info.height(); i++) {
139 REPORTER_ASSERT(r, !memcmp(truth.getAddr(0, 0), incremental.getAddr(0, 0 ),
140 info.minRowBytes()));
141 }
142 }
143
144 DEF_TEST(Codec_partial, r) {
145 test_partial(r, "plane.png");
146 test_partial(r, "plane_interlaced.png");
147 test_partial(r, "yellow_rose.png");
148 test_partial(r, "index8.png");
149 test_partial(r, "color_wheel.png");
150 test_partial(r, "mandrill_256.png");
151 test_partial(r, "mandrill_32.png");
152 test_partial(r, "arrow.png");
153 test_partial(r, "randPixels.png");
154 test_partial(r, "baby_tux.png");
155 }
156
157 // Test that calling getPixels when an incremental decode has been
158 // started (but not finished) makes the next call to incrementalDecode
159 // require a call to startIncrementalDecode.
160 static void test_interleaved(skiatest::Reporter* r, const char* name) {
161 sk_sp<SkData> file = make_from_resource(name);
162 SkAutoTDelete<SkCodec> partialCodec(SkCodec::NewFromStream(
163 new HaltingStream(std::move(file))));
164 if (!partialCodec) {
165 ERRORF(r, "Failed to create codec for %s", name);
166 return;
167 }
168
169 const SkImageInfo info = standardize_info(partialCodec);
170 SkBitmap incremental;
171 incremental.allocPixels(info);
172
173 const SkCodec::Result startResult = partialCodec->startIncrementalDecode(inf o,
174 incremental.getPixels(), incremental.rowBytes());
175 if (startResult != SkCodec::kSuccess) {
176 ERRORF(r, "Failed to start incremental decode\n");
177 return;
178 }
179
180 SkCodec::Result result = partialCodec->incrementalDecode();
181 REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput);
182
183 SkBitmap full;
184 full.allocPixels(info);
185 result = partialCodec->getPixels(info, full.getPixels(), full.rowBytes());
186 REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput);
187
188 // Now incremental decode will fail
189 result = partialCodec->incrementalDecode();
190 REPORTER_ASSERT(r, result == SkCodec::kInvalidParameters);
191 }
192
193 DEF_TEST(Codec_rewind, r) {
194 test_interleaved(r, "plane.png");
195 test_interleaved(r, "plane_interlaced.png");
196 }
OLDNEW
« no previous file with comments | « src/codec/SkSampler.h ('k') | tests/CodecTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698