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, |
| 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. |
| 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 |
| 259 // Verify that associated bitmap cache entries are purged on SkImage destruction
. |
| 260 DEF_TEST(BitmapCache_discarded_image, reporter) { |
| 261 // Cache entries associated with SkImages fall into two categories: |
| 262 // |
| 263 // 1) generated image bitmaps (managed by the image cacherator) |
| 264 // 2) scaled/resampled bitmaps (cached when HQ filters are used) |
| 265 // |
| 266 // To exercise the first cache type, we use generated/picture-backed SkImage
s. |
| 267 // To exercise the latter, we draw scaled bitmap images using HQ filters. |
| 268 |
| 269 const SkMatrix xforms[] = { |
| 270 SkMatrix::MakeScale(1, 1), |
| 271 SkMatrix::MakeScale(1.7f, 0.5f), |
| 272 }; |
| 273 |
| 274 for (size_t i = 0; i < SK_ARRAY_COUNT(xforms); ++i) { |
| 275 test_discarded_image(reporter, xforms[i], []() { |
| 276 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(10, 10
)); |
| 277 surface->getCanvas()->clear(SK_ColorCYAN); |
| 278 return surface->newImageSnapshot(); |
| 279 }); |
| 280 |
| 281 test_discarded_image(reporter, xforms[i], []() { |
| 282 SkPictureRecorder recorder; |
| 283 SkCanvas* canvas = recorder.beginRecording(10, 10); |
| 284 canvas->clear(SK_ColorCYAN); |
| 285 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 286 return SkImage::NewFromPicture(picture, SkISize::Make(10, 10), nullp
tr, nullptr); |
| 287 }); |
| 288 } |
| 289 } |
OLD | NEW |