OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkData.h" | 10 #include "SkData.h" |
11 #include "SkForceLinking.h" | 11 #include "SkForceLinking.h" |
12 #include "SkImageDecoder.h" | 12 #include "SkImageDecoder.h" |
13 #include "SkImagePriv.h" | 13 #include "SkImagePriv.h" |
14 #include "SkLazyCachingPixelRef.h" | |
14 #include "SkLazyPixelRef.h" | 15 #include "SkLazyPixelRef.h" |
15 #include "SkScaledImageCache.h" | 16 #include "SkScaledImageCache.h" |
16 #include "SkStream.h" | 17 #include "SkStream.h" |
18 | |
17 #include "Test.h" | 19 #include "Test.h" |
20 #include "TestClassDef.h" | |
18 | 21 |
19 __SK_FORCE_IMAGE_DECODER_LINKING; | 22 __SK_FORCE_IMAGE_DECODER_LINKING; |
20 | 23 |
21 /** | 24 /** |
22 * Fill this bitmap with some color. | 25 * Fill this bitmap with some color. |
23 */ | 26 */ |
24 static void make_test_image(SkBitmap* bm) { | 27 static void make_test_image(SkBitmap* bm) { |
25 static const int W = 50, H = 50; | 28 static const int W = 50, H = 50; |
26 static const SkBitmap::Config config = SkBitmap::kARGB_8888_Config; | 29 static const SkBitmap::Config config = SkBitmap::kARGB_8888_Config; |
27 bm->setConfig(config, W, H); | 30 bm->setConfig(config, W, H); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
85 if ((!(b1.getPixels())) || (!(b2.getPixels()))) { | 88 if ((!(b1.getPixels())) || (!(b2.getPixels()))) { |
86 return; | 89 return; |
87 } | 90 } |
88 if ((b1.width() != b2.width()) || | 91 if ((b1.width() != b2.width()) || |
89 (b1.height() != b2.height())) { | 92 (b1.height() != b2.height())) { |
90 return; | 93 return; |
91 } | 94 } |
92 if (!pixelPerfect) { | 95 if (!pixelPerfect) { |
93 return; | 96 return; |
94 } | 97 } |
98 | |
95 int pixelErrors = 0; | 99 int pixelErrors = 0; |
96 for (int y = 0; y < b2.height(); ++y) { | 100 for (int y = 0; y < b2.height(); ++y) { |
97 for (int x = 0; x < b2.width(); ++x) { | 101 for (int x = 0; x < b2.width(); ++x) { |
98 if (b1.getColor(x, y) != b2.getColor(x, y)) { | 102 if (b1.getColor(x, y) != b2.getColor(x, y)) { |
99 ++pixelErrors; | 103 ++pixelErrors; |
100 } | 104 } |
101 } | 105 } |
102 } | 106 } |
103 REPORTER_ASSERT(reporter, 0 == pixelErrors); | 107 REPORTER_ASSERT(reporter, 0 == pixelErrors); |
104 } | 108 } |
105 | 109 |
110 | |
111 typedef void(*CompareEncodedToOriginal)(skiatest::Reporter* reporter, | |
112 SkData* encoded, | |
113 const SkBitmap& original, | |
114 bool pixelPerfect); | |
106 /** | 115 /** |
107 * This checks to see that a SkLazyPixelRef works as advertized. | 116 this function tests three differently encoded images against the |
108 */ | 117 origianl bitmap */ |
109 #include "TestClassDef.h" | 118 void testThreeEncodings(skiatest::Reporter* reporter, |
scroggo
2013/11/01 15:48:00
This function should be static.
Also, static func
hal.canary
2013/11/01 16:31:09
Done.
| |
110 DEF_TEST(CachedDecodingPixelRefTest, reporter) { | 119 CompareEncodedToOriginal comp) { |
111 SkBitmap original; | 120 SkBitmap original; |
112 make_test_image(&original); | 121 make_test_image(&original); |
113 const size_t bitmapSize = original.getSize(); | 122 REPORTER_ASSERT(reporter, !original.empty()); |
114 const size_t oldByteLimit = SkScaledImageCache::GetByteLimit(); | 123 REPORTER_ASSERT(reporter, !original.isNull()); |
115 REPORTER_ASSERT(reporter, (!(original.empty())) && (!(original.isNull()))); | 124 if (original.empty() || original.isNull()) { |
116 | 125 return; |
126 } | |
117 static const SkImageEncoder::Type types[] = { | 127 static const SkImageEncoder::Type types[] = { |
118 SkImageEncoder::kPNG_Type, | 128 SkImageEncoder::kPNG_Type, |
119 SkImageEncoder::kJPEG_Type, | 129 SkImageEncoder::kJPEG_Type, |
120 SkImageEncoder::kWEBP_Type | 130 SkImageEncoder::kWEBP_Type |
121 }; | 131 }; |
122 | |
123 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) { | 132 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) { |
124 SkImageEncoder::Type type = types[i]; | 133 SkImageEncoder::Type type = types[i]; |
125 SkAutoDataUnref encoded(create_data_from_bitmap(original, type)); | 134 SkAutoDataUnref encoded(create_data_from_bitmap(original, type)); |
126 REPORTER_ASSERT(reporter, encoded.get() != NULL); | 135 REPORTER_ASSERT(reporter, encoded.get() != NULL); |
127 if (NULL == encoded.get()) { | 136 if (NULL != encoded.get()) { |
128 continue; | 137 bool comparePixels = (SkImageEncoder::kPNG_Type == type); |
138 comp(reporter, encoded, original, comparePixels); | |
129 } | 139 } |
130 SkBitmap lazy; | |
131 static const SkBitmapFactory::DecodeProc decoder = | |
132 &(SkImageDecoder::DecodeMemoryToTarget); | |
133 bool success = simple_bitmap_factory(decoder, encoded.get(), &lazy); | |
134 | |
135 REPORTER_ASSERT(reporter, success); | |
136 | |
137 size_t bytesUsed = SkScaledImageCache::GetBytesUsed(); | |
138 | |
139 if (oldByteLimit < bitmapSize) { | |
140 SkScaledImageCache::SetByteLimit(bitmapSize + oldByteLimit); | |
141 } | |
142 void* lazyPixels = NULL; | |
143 | |
144 // Since this is lazy, it shouldn't have fPixels yet! | |
145 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | |
146 { | |
147 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | |
148 lazyPixels = lazy.getPixels(); | |
149 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | |
150 // first time we lock pixels, we should get bump in the size | |
151 // of the cache by exactly bitmapSize. | |
152 REPORTER_ASSERT(reporter, bytesUsed + bitmapSize | |
153 == SkScaledImageCache::GetBytesUsed()); | |
154 bytesUsed = SkScaledImageCache::GetBytesUsed(); | |
155 } | |
156 // pixels should be gone! | |
157 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | |
158 { | |
159 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | |
160 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | |
161 | |
162 // verify that the same pixels are used this time. | |
163 REPORTER_ASSERT(reporter, lazy.getPixels() == lazyPixels); | |
164 } | |
165 | |
166 bool comparePixels = (SkImageEncoder::kPNG_Type == type); | |
167 // Only PNG is pixel-perfect. | |
168 compare_bitmaps(reporter, original, lazy, comparePixels); | |
169 | |
170 // force the cache to clear by making it too small. | |
171 SkScaledImageCache::SetByteLimit(bitmapSize / 2); | |
172 compare_bitmaps(reporter, original, lazy, comparePixels); | |
173 | |
174 // I'm pretty sure that the logic of the cache should mean | |
175 // that it will clear to zero, regardless of where it started. | |
176 REPORTER_ASSERT(reporter, SkScaledImageCache::GetBytesUsed() == 0); | |
177 // TODO(someone) - write a custom allocator that can verify | |
178 // that the memory where those pixels were cached really did | |
179 // get freed. | |
180 | |
181 //////////////////////////////////////////////////////////////////////// | |
182 // The following commented-out code happens to work on my | |
183 // machine, and indicates to me that the SkLazyPixelRef is | |
184 // behaving as designed. But I don't know an easy way to | |
185 // guarantee that a second allocation of the same size will | |
186 // give a different address. | |
187 //////////////////////////////////////////////////////////////////////// | |
188 // { | |
189 // // confuse the heap allocation system | |
190 // SkAutoMalloc autoMalloc(bitmapSize); | |
191 // REPORTER_ASSERT(reporter, autoMalloc.get() == lazyPixels); | |
192 // { | |
193 // SkAutoLockPixels autoLockPixels(lazy); | |
194 // // verify that *different* pixels are used this time. | |
195 // REPORTER_ASSERT(reporter, lazy.getPixels() != lazyPixels); | |
196 // compare_bitmaps(reporter, original, lazy, comparePixels); | |
197 // } | |
198 // } | |
199 | |
200 // restore cache size | |
201 SkScaledImageCache::SetByteLimit(oldByteLimit); | |
202 } | 140 } |
203 } | 141 } |
142 | |
143 /** | |
144 * This checks to see that a SkLazyPixelRef works as advertised. | |
145 */ | |
146 void compareWithSkLazyPixelRef(skiatest::Reporter* reporter, | |
scroggo
2013/11/01 15:48:00
ditto
hal.canary
2013/11/01 16:31:09
Done.
| |
147 SkData* encoded, | |
148 const SkBitmap& original, | |
149 bool comparePixels) { | |
150 SkBitmap lazy; | |
151 static const SkBitmapFactory::DecodeProc decoder = | |
152 &(SkImageDecoder::DecodeMemoryToTarget); | |
153 bool success = simple_bitmap_factory(decoder, encoded, &lazy); | |
154 REPORTER_ASSERT(reporter, success); | |
155 | |
156 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | |
157 { | |
158 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | |
159 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | |
160 } | |
161 // pixels should be gone! | |
162 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | |
163 { | |
164 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | |
165 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | |
166 } | |
167 compare_bitmaps(reporter, original, lazy, comparePixels); | |
168 } | |
169 DEF_TEST(CachedDecodingPixelRefTest, reporter) { | |
170 testThreeEncodings(reporter, compareWithSkLazyPixelRef); | |
171 } | |
172 | |
173 | |
174 | |
175 /** | |
176 * This checks to see that a SkLazyCachedPixelRef works as advertised. | |
177 */ | |
178 | |
179 void compareWithLazyCachedPixelRef(skiatest::Reporter* reporter, | |
scroggo
2013/11/01 15:48:00
ditto
hal.canary
2013/11/01 16:31:09
Done.
| |
180 SkData* encoded, | |
181 const SkBitmap& original, | |
182 bool comparePixels) { | |
183 const size_t bitmapSize = original.getSize(); | |
184 // By using a non-global cache, our unit tests won't collide with | |
185 // other unit tests. Unit tests can be run in separate threads. | |
186 // TODO(halcanary): Find a way to robustly test the global | |
187 // instance of SkScaledImageCache. | |
188 SkScaledImageCache cache(2000000, NULL); | |
189 const size_t oldByteLimit = cache.getByteLimit(); | |
190 SkBitmap lazy; | |
191 static const SkBitmapFactory::DecodeProc decoder = | |
192 &(SkImageDecoder::DecodeMemoryToTarget); | |
193 | |
194 bool success = SkLazyCachingPixelRef::Install(decoder, encoded, | |
195 &lazy, &cache); | |
196 REPORTER_ASSERT(reporter, success); | |
197 | |
198 size_t bytesUsed = cache.getBytesUsed(); | |
199 | |
200 if (oldByteLimit < bitmapSize) { | |
201 cache.setByteLimit(bitmapSize + oldByteLimit); | |
202 } | |
203 void* lazyPixels = NULL; | |
204 | |
205 // Since this is lazy, it shouldn't have fPixels yet! | |
206 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | |
207 { | |
208 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | |
209 lazyPixels = lazy.getPixels(); | |
210 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | |
211 // first time we lock pixels, we should get bump in the size | |
212 // of the cache by exactly bitmapSize. | |
213 REPORTER_ASSERT(reporter, bytesUsed + bitmapSize | |
214 == cache.getBytesUsed()); | |
215 bytesUsed = cache.getBytesUsed(); | |
216 } | |
217 // pixels should be gone! | |
218 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | |
219 { | |
220 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | |
221 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | |
222 | |
223 // verify that the same pixels are used this time. | |
224 REPORTER_ASSERT(reporter, lazy.getPixels() == lazyPixels); | |
225 } | |
226 | |
227 // Only PNG is pixel-perfect. | |
228 compare_bitmaps(reporter, original, lazy, comparePixels); | |
229 | |
230 // force the cache to clear by making it too small. | |
231 cache.setByteLimit(bitmapSize / 2); | |
232 | |
233 compare_bitmaps(reporter, original, lazy, comparePixels); | |
234 | |
235 // I'm pretty sure that the logic of the cache should mean | |
236 // that it will clear to zero, regardless of where it started. | |
237 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | |
238 REPORTER_ASSERT(reporter, cache.getBytesUsed() == 0); | |
239 // TODO(someone) - write a custom allocator that can verify | |
240 // that the memory where those pixels were cached really did | |
241 // get freed. | |
242 | |
243 //////////////////////////////////////////////////////////////////////// | |
244 // The following commented-out code happens to work on my | |
245 // machine, and indicates to me that the SkLazyPixelRef is | |
246 // behaving as designed. But I don't know an easy way to | |
247 // guarantee that a second allocation of the same size will | |
248 // give a different address. | |
249 //////////////////////////////////////////////////////////////////////// | |
250 // { | |
251 // // confuse the heap allocation system | |
252 // SkAutoMalloc autoMalloc(bitmapSize); | |
253 // REPORTER_ASSERT(reporter, autoMalloc.get() == lazyPixels); | |
254 // { | |
255 // SkAutoLockPixels autoLockPixels(lazy); | |
256 // // verify that *different* pixels are used this time. | |
257 // REPORTER_ASSERT(reporter, lazy.getPixels() != lazyPixels); | |
258 // compare_bitmaps(reporter, original, lazy, comparePixels); | |
259 // } | |
260 // } | |
261 } | |
262 DEF_TEST(LazyCachedPixelRefTest, reporter) { | |
263 testThreeEncodings(reporter, compareWithLazyCachedPixelRef); | |
264 } | |
265 | |
OLD | NEW |