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 "SkCachingPixelRef.h" | |
10 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
11 #include "SkData.h" | 10 #include "SkData.h" |
12 #include "SkDiscardableMemoryPool.h" | 11 #include "SkDiscardableMemoryPool.h" |
13 #include "SkImageDecoder.h" | 12 #include "SkImageDecoder.h" |
14 #include "SkImageGeneratorPriv.h" | 13 #include "SkImageGeneratorPriv.h" |
15 #include "SkResourceCache.h" | 14 #include "SkResourceCache.h" |
16 #include "SkStream.h" | 15 #include "SkStream.h" |
17 #include "SkUtils.h" | 16 #include "SkUtils.h" |
18 | 17 |
19 #include "Test.h" | 18 #include "Test.h" |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 if (nullptr == lazy.getPixels()) { | 133 if (nullptr == lazy.getPixels()) { |
135 continue; | 134 continue; |
136 } | 135 } |
137 } | 136 } |
138 bool comparePixels = (SkImageEncoder::kPNG_Type == type); | 137 bool comparePixels = (SkImageEncoder::kPNG_Type == type); |
139 compare_bitmaps(reporter, original, lazy, comparePixels); | 138 compare_bitmaps(reporter, original, lazy, comparePixels); |
140 } | 139 } |
141 } | 140 } |
142 | 141 |
143 //////////////////////////////////////////////////////////////////////////////// | 142 //////////////////////////////////////////////////////////////////////////////// |
144 static bool install_skCachingPixelRef(SkData* encoded, SkBitmap* dst) { | |
145 return SkCachingPixelRef::Install(SkImageGenerator::NewFromEncoded(encoded),
dst); | |
146 } | |
147 static bool install_skDiscardablePixelRef(SkData* encoded, SkBitmap* dst) { | 143 static bool install_skDiscardablePixelRef(SkData* encoded, SkBitmap* dst) { |
148 // Use system-default discardable memory. | 144 // Use system-default discardable memory. |
149 return SkInstallDiscardablePixelRef(encoded, dst); | 145 return SkInstallDiscardablePixelRef(encoded, dst); |
150 } | 146 } |
151 | 147 |
152 //////////////////////////////////////////////////////////////////////////////// | 148 //////////////////////////////////////////////////////////////////////////////// |
153 /** | 149 /** |
154 * This checks to see that a SkCachingPixelRef and a | 150 * This checks to see that SkDiscardablePixelRef works as advertised with a |
155 * SkDiscardablePixelRef works as advertised with a | |
156 * SkDecodingImageGenerator. | 151 * SkDecodingImageGenerator. |
157 */ | 152 */ |
158 DEF_TEST(DecodingImageGenerator, reporter) { | 153 DEF_TEST(DecodingImageGenerator, reporter) { |
159 test_three_encodings(reporter, install_skCachingPixelRef); | |
160 test_three_encodings(reporter, install_skDiscardablePixelRef); | 154 test_three_encodings(reporter, install_skDiscardablePixelRef); |
161 } | 155 } |
162 | 156 |
163 class TestImageGenerator : public SkImageGenerator { | 157 class TestImageGenerator : public SkImageGenerator { |
164 public: | 158 public: |
165 enum TestType { | 159 enum TestType { |
166 kFailGetPixels_TestType, | 160 kFailGetPixels_TestType, |
167 kSucceedGetPixels_TestType, | 161 kSucceedGetPixels_TestType, |
168 kLast_TestType = kSucceedGetPixels_TestType | 162 kLast_TestType = kSucceedGetPixels_TestType |
169 }; | 163 }; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 for (int y = 0; y < bm.height(); ++y) { | 215 for (int y = 0; y < bm.height(); ++y) { |
222 for (int x = 0; x < bm.width(); ++x) { | 216 for (int x = 0; x < bm.width(); ++x) { |
223 if (TestImageGenerator::Color() != *bm.getAddr32(x, y)) { | 217 if (TestImageGenerator::Color() != *bm.getAddr32(x, y)) { |
224 ++errors; | 218 ++errors; |
225 } | 219 } |
226 } | 220 } |
227 } | 221 } |
228 REPORTER_ASSERT(reporter, 0 == errors); | 222 REPORTER_ASSERT(reporter, 0 == errors); |
229 } | 223 } |
230 | 224 |
231 enum PixelRefType { | |
232 kSkCaching_PixelRefType, | |
233 kSkDiscardable_PixelRefType, | |
234 kLast_PixelRefType = kSkDiscardable_PixelRefType | |
235 }; | |
236 | |
237 static void check_pixelref(TestImageGenerator::TestType type, | 225 static void check_pixelref(TestImageGenerator::TestType type, |
238 skiatest::Reporter* reporter, | 226 skiatest::Reporter* reporter, |
239 PixelRefType pixelRefType, | |
240 SkDiscardableMemory::Factory* factory) { | 227 SkDiscardableMemory::Factory* factory) { |
241 SkASSERT((pixelRefType >= 0) && (pixelRefType <= kLast_PixelRefType)); | |
242 SkAutoTDelete<SkImageGenerator> gen(new TestImageGenerator(type, reporter)); | 228 SkAutoTDelete<SkImageGenerator> gen(new TestImageGenerator(type, reporter)); |
243 REPORTER_ASSERT(reporter, gen.get() != nullptr); | 229 REPORTER_ASSERT(reporter, gen.get() != nullptr); |
244 SkBitmap lazy; | 230 SkBitmap lazy; |
245 bool success; | 231 bool success = SkInstallDiscardablePixelRef(gen.detach(), nullptr, &lazy, fa
ctory); |
246 if (kSkCaching_PixelRefType == pixelRefType) { | 232 |
247 // Ignore factory; use global cache. | |
248 success = SkCachingPixelRef::Install(gen.detach(), &lazy); | |
249 } else { | |
250 success = SkInstallDiscardablePixelRef(gen.detach(), nullptr, &lazy, fac
tory); | |
251 } | |
252 REPORTER_ASSERT(reporter, success); | 233 REPORTER_ASSERT(reporter, success); |
253 if (TestImageGenerator::kSucceedGetPixels_TestType == type) { | 234 if (TestImageGenerator::kSucceedGetPixels_TestType == type) { |
254 check_test_image_generator_bitmap(reporter, lazy); | 235 check_test_image_generator_bitmap(reporter, lazy); |
255 } else if (TestImageGenerator::kFailGetPixels_TestType == type) { | 236 } else if (TestImageGenerator::kFailGetPixels_TestType == type) { |
256 SkAutoLockPixels autoLockPixels(lazy); | 237 SkAutoLockPixels autoLockPixels(lazy); |
257 REPORTER_ASSERT(reporter, nullptr == lazy.getPixels()); | 238 REPORTER_ASSERT(reporter, nullptr == lazy.getPixels()); |
258 } | 239 } |
259 } | 240 } |
260 | 241 |
261 // new/lock/delete is an odd pattern for a pixelref, but it needs to not assert | 242 // new/lock/delete is an odd pattern for a pixelref, but it needs to not assert |
262 static void test_newlockdelete(skiatest::Reporter* reporter) { | 243 static void test_newlockdelete(skiatest::Reporter* reporter) { |
263 #ifdef SK_SUPPORT_LEGACY_UNBALANCED_PIXELREF_LOCKCOUNT | 244 #ifdef SK_SUPPORT_LEGACY_UNBALANCED_PIXELREF_LOCKCOUNT |
264 SkBitmap bm; | 245 SkBitmap bm; |
265 SkImageGenerator* ig = new TestImageGenerator( | 246 SkImageGenerator* ig = new TestImageGenerator( |
266 TestImageGenerator::kSucceedGetPixels_TestType, reporter); | 247 TestImageGenerator::kSucceedGetPixels_TestType, reporter); |
267 SkInstallDiscardablePixelRef(ig, &bm); | 248 SkInstallDiscardablePixelRef(ig, &bm); |
268 bm.pixelRef()->lockPixels(); | 249 bm.pixelRef()->lockPixels(); |
269 #endif | 250 #endif |
270 } | 251 } |
271 | 252 |
272 /** | 253 /** |
273 * This tests the basic functionality of SkDiscardablePixelRef with a | 254 * This tests the basic functionality of SkDiscardablePixelRef with a |
274 * basic SkImageGenerator implementation and several | 255 * basic SkImageGenerator implementation and several |
275 * SkDiscardableMemory::Factory choices. | 256 * SkDiscardableMemory::Factory choices. |
276 */ | 257 */ |
277 DEF_TEST(DiscardableAndCachingPixelRef, reporter) { | 258 DEF_TEST(DiscardableAndCachingPixelRef, reporter) { |
278 test_newlockdelete(reporter); | 259 test_newlockdelete(reporter); |
279 | 260 |
280 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, | 261 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, reporter, nullpt
r); |
281 reporter, kSkCaching_PixelRefType, nullptr); | 262 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, reporter, nul
lptr); |
282 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, | |
283 reporter, kSkCaching_PixelRefType, nullptr); | |
284 | |
285 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, | |
286 reporter, kSkDiscardable_PixelRefType, nullptr); | |
287 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, | |
288 reporter, kSkDiscardable_PixelRefType, nullptr); | |
289 | 263 |
290 SkAutoTUnref<SkDiscardableMemoryPool> pool( | 264 SkAutoTUnref<SkDiscardableMemoryPool> pool( |
291 SkDiscardableMemoryPool::Create(1, nullptr)); | 265 SkDiscardableMemoryPool::Create(1, nullptr)); |
292 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); | 266 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); |
293 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, | 267 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, reporter, pool); |
294 reporter, kSkDiscardable_PixelRefType, pool); | |
295 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); | 268 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); |
296 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, | 269 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, reporter, poo
l); |
297 reporter, kSkDiscardable_PixelRefType, pool); | |
298 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); | 270 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); |
299 | 271 |
300 SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool(); | 272 SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool(); |
301 // Only acts differently from nullptr on a platform that has a | 273 // Only acts differently from nullptr on a platform that has a |
302 // default discardable memory implementation that differs from the | 274 // default discardable memory implementation that differs from the |
303 // global DM pool. | 275 // global DM pool. |
304 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, | 276 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, reporter, global
Pool); |
305 reporter, kSkDiscardable_PixelRefType, globalPool); | 277 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, reporter, glo
balPool); |
306 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, | |
307 reporter, kSkDiscardable_PixelRefType, globalPool); | |
308 } | 278 } |
309 | 279 |
310 //////////////////////////////////////////////////////////////////////////////// | 280 //////////////////////////////////////////////////////////////////////////////// |
311 | 281 |
312 DEF_TEST(Image_NewFromGenerator, r) { | 282 DEF_TEST(Image_NewFromGenerator, r) { |
313 TestImageGenerator::TestType testTypes[] = { | 283 TestImageGenerator::TestType testTypes[] = { |
314 TestImageGenerator::kFailGetPixels_TestType, | 284 TestImageGenerator::kFailGetPixels_TestType, |
315 TestImageGenerator::kSucceedGetPixels_TestType, | 285 TestImageGenerator::kSucceedGetPixels_TestType, |
316 }; | 286 }; |
317 for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) { | 287 for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) { |
(...skipping 16 matching lines...) Expand all Loading... |
334 canvas.clear(kDefaultColor); | 304 canvas.clear(kDefaultColor); |
335 canvas.drawImage(image, 0, 0, nullptr); | 305 canvas.drawImage(image, 0, 0, nullptr); |
336 if (TestImageGenerator::kSucceedGetPixels_TestType == test) { | 306 if (TestImageGenerator::kSucceedGetPixels_TestType == test) { |
337 REPORTER_ASSERT( | 307 REPORTER_ASSERT( |
338 r, TestImageGenerator::Color() == *bitmap.getAddr32(0, 0)); | 308 r, TestImageGenerator::Color() == *bitmap.getAddr32(0, 0)); |
339 } else { | 309 } else { |
340 REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0,0)); | 310 REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0,0)); |
341 } | 311 } |
342 } | 312 } |
343 } | 313 } |
OLD | NEW |