halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 7 | |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 8 | #include "Test.h" |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 9 | #include "SkBitmapCache.h" |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 10 | #include "SkCanvas.h" |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 11 | #include "SkDiscardableMemoryPool.h" |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 12 | #include "SkGraphics.h" |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame^] | 13 | #include "SkPicture.h" |
| 14 | #include "SkPictureRecorder.h" |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 15 | #include "SkResourceCache.h" |
reed | 3054be1 | 2014-12-10 07:24:28 -0800 | [diff] [blame] | 16 | #include "SkSurface.h" |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 17 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 18 | //////////////////////////////////////////////////////////////////////////////////////// |
mtklein | 26abcf1 | 2014-09-04 10:50:53 -0700 | [diff] [blame] | 19 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 20 | static void make_bitmap(SkBitmap* bitmap, const SkImageInfo& info, SkBitmap::Allocator* allocator) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 21 | if (allocator) { |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 22 | bitmap->setInfo(info); |
| 23 | allocator->allocPixelRef(bitmap, 0); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 24 | } else { |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 25 | bitmap->allocPixels(info); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 26 | } |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | // http://skbug.com/2894 |
| 30 | DEF_TEST(BitmapCache_add_rect, reporter) { |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 31 | SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardableFactory(); |
| 32 | SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); |
| 33 | |
| 34 | SkAutoTDelete<SkResourceCache> cache; |
| 35 | if (factory) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 36 | cache.reset(new SkResourceCache(factory)); |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 37 | } else { |
| 38 | const size_t byteLimit = 100 * 1024; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 39 | cache.reset(new SkResourceCache(byteLimit)); |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 40 | } |
| 41 | SkBitmap cachedBitmap; |
| 42 | make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 43 | cachedBitmap.setImmutable(); |
| 44 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 45 | SkBitmap bm; |
| 46 | SkIRect rect = SkIRect::MakeWH(5, 5); |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 47 | uint32_t cachedID = cachedBitmap.getGenerationID(); |
| 48 | SkPixelRef* cachedPR = cachedBitmap.pixelRef(); |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 49 | |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 50 | // Wrong subset size |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 51 | REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedPR, SkIRect::MakeWH(4, 6), cachedBitmap, cache)); |
| 52 | REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedID, rect, &bm, cache)); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 53 | // Wrong offset value |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 54 | REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedPR, SkIRect::MakeXYWH(-1, 0, 5, 5), cachedBitmap, cache)); |
| 55 | REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedID, rect, &bm, cache)); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 56 | |
| 57 | // Should not be in the cache |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 58 | REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedID, rect, &bm, cache)); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 59 | |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 60 | REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedPR, rect, cachedBitmap, cache)); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 61 | // Should be in the cache, we just added it |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 62 | REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedID, rect, &bm, cache)); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 63 | } |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 64 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 65 | #include "SkMipMap.h" |
| 66 | |
| 67 | enum LockedState { |
| 68 | kNotLocked, |
| 69 | kLocked, |
| 70 | }; |
| 71 | |
| 72 | enum CachedState { |
| 73 | kNotInCache, |
| 74 | kInCache, |
| 75 | }; |
| 76 | |
| 77 | static void check_data(skiatest::Reporter* reporter, const SkCachedData* data, |
| 78 | int refcnt, CachedState cacheState, LockedState lockedState) { |
| 79 | REPORTER_ASSERT(reporter, data->testing_only_getRefCnt() == refcnt); |
| 80 | REPORTER_ASSERT(reporter, data->testing_only_isInCache() == (kInCache == cacheState)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 81 | bool isLocked = (data->data() != nullptr); |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 82 | REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked)); |
| 83 | } |
| 84 | |
| 85 | static void test_mipmapcache(skiatest::Reporter* reporter, SkResourceCache* cache) { |
| 86 | cache->purgeAll(); |
| 87 | |
| 88 | SkBitmap src; |
| 89 | src.allocN32Pixels(5, 5); |
| 90 | src.setImmutable(); |
| 91 | |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 92 | const SkMipMap* mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src), cache); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 93 | REPORTER_ASSERT(reporter, nullptr == mipmap); |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 94 | |
| 95 | mipmap = SkMipMapCache::AddAndRef(src, cache); |
| 96 | REPORTER_ASSERT(reporter, mipmap); |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 97 | |
| 98 | { |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 99 | const SkMipMap* mm = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src), cache); |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 100 | REPORTER_ASSERT(reporter, mm); |
| 101 | REPORTER_ASSERT(reporter, mm == mipmap); |
| 102 | mm->unref(); |
| 103 | } |
| 104 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 105 | check_data(reporter, mipmap, 2, kInCache, kLocked); |
| 106 | |
| 107 | mipmap->unref(); |
| 108 | // tricky, since technically after this I'm no longer an owner, but since the cache is |
| 109 | // local, I know it won't get purged behind my back |
| 110 | check_data(reporter, mipmap, 1, kInCache, kNotLocked); |
| 111 | |
| 112 | // find us again |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 113 | mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src), cache); |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 114 | check_data(reporter, mipmap, 2, kInCache, kLocked); |
| 115 | |
| 116 | cache->purgeAll(); |
| 117 | check_data(reporter, mipmap, 1, kNotInCache, kLocked); |
| 118 | |
| 119 | mipmap->unref(); |
| 120 | } |
| 121 | |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 122 | static void test_mipmap_notify(skiatest::Reporter* reporter, SkResourceCache* cache) { |
| 123 | const int N = 3; |
| 124 | SkBitmap src[N]; |
| 125 | for (int i = 0; i < N; ++i) { |
| 126 | src[i].allocN32Pixels(5, 5); |
| 127 | src[i].setImmutable(); |
| 128 | SkMipMapCache::AddAndRef(src[i], cache)->unref(); |
| 129 | } |
| 130 | |
| 131 | for (int i = 0; i < N; ++i) { |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 132 | const SkMipMap* mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src[i]), cache); |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 133 | if (cache) { |
| 134 | // if cache is null, we're working on the global cache, and other threads might purge |
| 135 | // it, making this check fragile. |
| 136 | REPORTER_ASSERT(reporter, mipmap); |
| 137 | } |
| 138 | SkSafeUnref(mipmap); |
| 139 | |
| 140 | src[i].reset(); // delete the underlying pixelref, which *should* remove us from the cache |
| 141 | |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 142 | mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src[i]), cache); |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 143 | REPORTER_ASSERT(reporter, !mipmap); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static void test_bitmap_notify(skiatest::Reporter* reporter, SkResourceCache* cache) { |
| 148 | const SkIRect subset = SkIRect::MakeWH(5, 5); |
| 149 | const int N = 3; |
| 150 | SkBitmap src[N], dst[N]; |
| 151 | for (int i = 0; i < N; ++i) { |
| 152 | src[i].allocN32Pixels(5, 5); |
| 153 | src[i].setImmutable(); |
| 154 | dst[i].allocN32Pixels(5, 5); |
| 155 | dst[i].setImmutable(); |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 156 | SkBitmapCache::Add(src[i].pixelRef(), subset, dst[i], cache); |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | for (int i = 0; i < N; ++i) { |
| 160 | const uint32_t genID = src[i].getGenerationID(); |
| 161 | SkBitmap result; |
| 162 | bool found = SkBitmapCache::Find(genID, subset, &result, cache); |
| 163 | if (cache) { |
| 164 | // if cache is null, we're working on the global cache, and other threads might purge |
| 165 | // it, making this check fragile. |
| 166 | REPORTER_ASSERT(reporter, found); |
| 167 | } |
| 168 | |
| 169 | src[i].reset(); // delete the underlying pixelref, which *should* remove us from the cache |
| 170 | |
| 171 | found = SkBitmapCache::Find(genID, subset, &result, cache); |
| 172 | REPORTER_ASSERT(reporter, !found); |
| 173 | } |
| 174 | } |
| 175 | |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 176 | DEF_TEST(BitmapCache_discarded_bitmap, reporter) { |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 177 | SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardableFactory(); |
| 178 | SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); |
| 179 | |
| 180 | SkAutoTDelete<SkResourceCache> cache; |
| 181 | if (factory) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 182 | cache.reset(new SkResourceCache(factory)); |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 183 | } else { |
| 184 | const size_t byteLimit = 100 * 1024; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 185 | cache.reset(new SkResourceCache(byteLimit)); |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 186 | } |
| 187 | SkBitmap cachedBitmap; |
| 188 | make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 189 | cachedBitmap.setImmutable(); |
| 190 | cachedBitmap.unlockPixels(); |
| 191 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 192 | SkBitmap bm; |
| 193 | SkIRect rect = SkIRect::MakeWH(5, 5); |
| 194 | |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 195 | // Add a bitmap to the cache. |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 196 | REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), rect, cachedBitmap, cache)); |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 197 | REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache)); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 198 | |
| 199 | // Finding more than once works fine. |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 200 | REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache)); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 201 | bm.unlockPixels(); |
| 202 | |
| 203 | // Drop the pixels in the bitmap. |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 204 | if (factory) { |
| 205 | REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed() > 0); |
| 206 | SkGetGlobalDiscardableMemoryPool()->dumpPool(); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 207 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 208 | // The bitmap is not in the cache since it has been dropped. |
| 209 | REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache)); |
| 210 | } |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 211 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 212 | make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 213 | cachedBitmap.setImmutable(); |
| 214 | cachedBitmap.unlockPixels(); |
| 215 | |
| 216 | // We can add the bitmap back to the cache and find it again. |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 217 | REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), rect, cachedBitmap, cache)); |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 218 | REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache)); |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 219 | |
| 220 | test_mipmapcache(reporter, cache); |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 221 | test_bitmap_notify(reporter, cache); |
| 222 | test_mipmap_notify(reporter, cache); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 223 | } |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame^] | 224 | |
| 225 | static void test_discarded_image(skiatest::Reporter* reporter, const SkMatrix& transform, |
| 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 SkImages. |
| 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), nullptr, nullptr); |
| 287 | }); |
| 288 | } |
| 289 | } |