OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 "Test.h" | 8 #include "Test.h" |
9 #include "SkBitmapCache.h" | 9 #include "SkBitmapCache.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkDiscardableMemoryPool.h" | 11 #include "SkDiscardableMemoryPool.h" |
12 #include "SkGraphics.h" | 12 #include "SkGraphics.h" |
13 #include "SkPicture.h" | |
14 #include "SkPictureRecorder.h" | |
13 #include "SkResourceCache.h" | 15 #include "SkResourceCache.h" |
14 #include "SkSurface.h" | 16 #include "SkSurface.h" |
15 | 17 |
16 //////////////////////////////////////////////////////////////////////////////// //////// | 18 //////////////////////////////////////////////////////////////////////////////// //////// |
17 | 19 |
18 static void make_bitmap(SkBitmap* bitmap, const SkImageInfo& info, SkBitmap::All ocator* allocator) { | 20 static void make_bitmap(SkBitmap* bitmap, const SkImageInfo& info, SkBitmap::All ocator* allocator) { |
19 if (allocator) { | 21 if (allocator) { |
20 bitmap->setInfo(info); | 22 bitmap->setInfo(info); |
21 allocator->allocPixelRef(bitmap, 0); | 23 allocator->allocPixelRef(bitmap, 0); |
22 } else { | 24 } else { |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 cachedBitmap.unlockPixels(); | 214 cachedBitmap.unlockPixels(); |
213 | 215 |
214 // We can add the bitmap back to the cache and find it again. | 216 // We can add the bitmap back to the cache and find it again. |
215 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), rect, cachedBitmap, cache)); | 217 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), rect, cachedBitmap, cache)); |
216 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID() , rect, &bm, cache)); | 218 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID() , rect, &bm, cache)); |
217 | 219 |
218 test_mipmapcache(reporter, cache); | 220 test_mipmapcache(reporter, cache); |
219 test_bitmap_notify(reporter, cache); | 221 test_bitmap_notify(reporter, cache); |
220 test_mipmap_notify(reporter, cache); | 222 test_mipmap_notify(reporter, cache); |
221 } | 223 } |
224 | |
225 static void test_discarded_image(skiatest::Reporter* reporter, const SkMatrix& t ransform, | |
reed1
2015/09/18 14:32:53
How about a block-comment here or on the main test
f(malita)
2015/09/18 15:00:40
Done.
| |
226 SkImage* (*buildImage)()) { | |
227 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(10, 10)); | |
228 SkCanvas* canvas = surface->getCanvas(); | |
229 | |
230 // SkBitmapCache is global, so other threads could be evicting our bitmaps. Loop a few times | |
231 // to mitigate this risk. | |
tomhudson
2015/09/18 13:30:54
Not sure how this is safe from flake?
f(malita)
2015/09/18 13:44:24
Note that we don't assert image pixels are cached,
mtklein
2015/09/18 13:56:15
DM has a way to group tasks that cannot run concur
| |
232 const unsigned kRepeatCount = 42; | |
233 for (unsigned i = 0; i < kRepeatCount; ++i) { | |
234 SkAutoCanvasRestore acr(canvas, true); | |
235 | |
236 SkAutoTUnref<SkImage> image(buildImage()); | |
237 | |
238 // always use high quality to ensure caching when scaled | |
239 SkPaint paint; | |
240 paint.setFilterQuality(kHigh_SkFilterQuality); | |
241 | |
242 // draw the image (with a transform, to tickle different code paths) to ensure | |
243 // any associated resources get cached | |
244 canvas->concat(transform); | |
245 canvas->drawImage(image, 0, 0, &paint); | |
246 | |
247 auto imageId = image->uniqueID(); | |
248 | |
249 // delete the image | |
250 image.reset(nullptr); | |
251 | |
252 // all resources should have been purged | |
253 SkBitmap result; | |
254 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(imageId, &result)); | |
255 } | |
256 } | |
257 | |
258 DEF_TEST(BitmapCache_discarded_image, reporter) { | |
259 const SkMatrix xforms[] = { | |
260 SkMatrix::MakeScale(1, 1), | |
261 SkMatrix::MakeScale(1.7f, 0.5f), | |
262 }; | |
263 | |
264 for (size_t i = 0; i < SK_ARRAY_COUNT(xforms); ++i) { | |
265 test_discarded_image(reporter, xforms[i], []() { | |
266 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(10, 10 )); | |
267 surface->getCanvas()->clear(SK_ColorCYAN); | |
268 return surface->newImageSnapshot(); | |
269 }); | |
270 | |
271 test_discarded_image(reporter, xforms[i], []() { | |
272 SkPictureRecorder recorder; | |
273 SkCanvas* canvas = recorder.beginRecording(10, 10); | |
274 canvas->clear(SK_ColorCYAN); | |
275 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); | |
276 return SkImage::NewFromPicture(picture, SkISize::Make(10, 10), nullp tr, nullptr); | |
277 }); | |
278 } | |
279 } | |
OLD | NEW |