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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkGraphics.h" |
| 10 | #include "include/core/SkPicture.h" |
| 11 | #include "include/core/SkPictureRecorder.h" |
| 12 | #include "include/core/SkSurface.h" |
| 13 | #include "src/core/SkBitmapCache.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/core/SkMipMap.h" |
| 15 | #include "src/core/SkResourceCache.h" |
Mike Reed | 64acf4f | 2019-08-01 15:35:20 -0400 | [diff] [blame] | 16 | #include "src/image/SkImage_Base.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/lazy/SkDiscardableMemoryPool.h" |
| 18 | #include "tests/Test.h" |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 19 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 20 | //////////////////////////////////////////////////////////////////////////////////////// |
mtklein | 26abcf1 | 2014-09-04 10:50:53 -0700 | [diff] [blame] | 21 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 22 | enum LockedState { |
| 23 | kNotLocked, |
| 24 | kLocked, |
| 25 | }; |
| 26 | |
| 27 | enum CachedState { |
| 28 | kNotInCache, |
| 29 | kInCache, |
| 30 | }; |
| 31 | |
| 32 | static void check_data(skiatest::Reporter* reporter, const SkCachedData* data, |
| 33 | int refcnt, CachedState cacheState, LockedState lockedState) { |
| 34 | REPORTER_ASSERT(reporter, data->testing_only_getRefCnt() == refcnt); |
| 35 | REPORTER_ASSERT(reporter, data->testing_only_isInCache() == (kInCache == cacheState)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 36 | bool isLocked = (data->data() != nullptr); |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 37 | REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked)); |
| 38 | } |
| 39 | |
| 40 | static void test_mipmapcache(skiatest::Reporter* reporter, SkResourceCache* cache) { |
| 41 | cache->purgeAll(); |
| 42 | |
| 43 | SkBitmap src; |
| 44 | src.allocN32Pixels(5, 5); |
| 45 | src.setImmutable(); |
Brian Osman | 3a16073 | 2018-10-15 15:32:06 -0400 | [diff] [blame] | 46 | sk_sp<SkImage> img = SkImage::MakeFromBitmap(src); |
Mike Reed | 64acf4f | 2019-08-01 15:35:20 -0400 | [diff] [blame] | 47 | const auto desc = SkBitmapCacheDesc::Make(img.get()); |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 48 | |
Brian Osman | 3a16073 | 2018-10-15 15:32:06 -0400 | [diff] [blame] | 49 | const SkMipMap* mipmap = SkMipMapCache::FindAndRef(desc, cache); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 50 | REPORTER_ASSERT(reporter, nullptr == mipmap); |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 51 | |
Mike Reed | 64acf4f | 2019-08-01 15:35:20 -0400 | [diff] [blame] | 52 | mipmap = SkMipMapCache::AddAndRef(as_IB(img.get()), cache); |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 53 | REPORTER_ASSERT(reporter, mipmap); |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 54 | |
| 55 | { |
Brian Osman | 3a16073 | 2018-10-15 15:32:06 -0400 | [diff] [blame] | 56 | const SkMipMap* mm = SkMipMapCache::FindAndRef(desc, cache); |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 57 | REPORTER_ASSERT(reporter, mm); |
| 58 | REPORTER_ASSERT(reporter, mm == mipmap); |
| 59 | mm->unref(); |
| 60 | } |
| 61 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 62 | check_data(reporter, mipmap, 2, kInCache, kLocked); |
| 63 | |
| 64 | mipmap->unref(); |
| 65 | // tricky, since technically after this I'm no longer an owner, but since the cache is |
| 66 | // local, I know it won't get purged behind my back |
| 67 | check_data(reporter, mipmap, 1, kInCache, kNotLocked); |
| 68 | |
| 69 | // find us again |
Brian Osman | 3a16073 | 2018-10-15 15:32:06 -0400 | [diff] [blame] | 70 | mipmap = SkMipMapCache::FindAndRef(desc, cache); |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 71 | check_data(reporter, mipmap, 2, kInCache, kLocked); |
| 72 | |
| 73 | cache->purgeAll(); |
| 74 | check_data(reporter, mipmap, 1, kNotInCache, kLocked); |
| 75 | |
| 76 | mipmap->unref(); |
| 77 | } |
| 78 | |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 79 | static void test_mipmap_notify(skiatest::Reporter* reporter, SkResourceCache* cache) { |
| 80 | const int N = 3; |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 81 | |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 82 | SkBitmap src[N]; |
Brian Osman | 3a16073 | 2018-10-15 15:32:06 -0400 | [diff] [blame] | 83 | sk_sp<SkImage> img[N]; |
| 84 | SkBitmapCacheDesc desc[N]; |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 85 | for (int i = 0; i < N; ++i) { |
| 86 | src[i].allocN32Pixels(5, 5); |
| 87 | src[i].setImmutable(); |
Brian Osman | 3a16073 | 2018-10-15 15:32:06 -0400 | [diff] [blame] | 88 | img[i] = SkImage::MakeFromBitmap(src[i]); |
Mike Reed | 64acf4f | 2019-08-01 15:35:20 -0400 | [diff] [blame] | 89 | SkMipMapCache::AddAndRef(as_IB(img[i].get()), cache)->unref(); |
| 90 | desc[i] = SkBitmapCacheDesc::Make(img[i].get()); |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | for (int i = 0; i < N; ++i) { |
Brian Osman | 3a16073 | 2018-10-15 15:32:06 -0400 | [diff] [blame] | 94 | const SkMipMap* mipmap = SkMipMapCache::FindAndRef(desc[i], cache); |
| 95 | // We're always using a local cache, so we know we won't be purged by other threads |
| 96 | REPORTER_ASSERT(reporter, mipmap); |
| 97 | SkSafeUnref(mipmap); |
| 98 | |
| 99 | img[i].reset(); // delete the image, which *should not* remove us from the cache |
| 100 | mipmap = SkMipMapCache::FindAndRef(desc[i], cache); |
| 101 | REPORTER_ASSERT(reporter, mipmap); |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 102 | SkSafeUnref(mipmap); |
| 103 | |
Brian Osman | 087c917 | 2018-10-15 10:33:54 -0400 | [diff] [blame] | 104 | src[i].reset(); // delete the underlying pixelref, which *should* remove us from the cache |
Brian Osman | 3a16073 | 2018-10-15 15:32:06 -0400 | [diff] [blame] | 105 | mipmap = SkMipMapCache::FindAndRef(desc[i], cache); |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 106 | REPORTER_ASSERT(reporter, !mipmap); |
| 107 | } |
| 108 | } |
| 109 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 110 | #include "src/lazy/SkDiscardableMemoryPool.h" |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 111 | |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 112 | static SkDiscardableMemoryPool* gPool = nullptr; |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 113 | static SkDiscardableMemory* pool_factory(size_t bytes) { |
| 114 | SkASSERT(gPool); |
| 115 | return gPool->create(bytes); |
| 116 | } |
| 117 | |
| 118 | static void testBitmapCache_discarded_bitmap(skiatest::Reporter* reporter, SkResourceCache* cache, |
| 119 | SkResourceCache::DiscardableFactory factory) { |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 120 | test_mipmapcache(reporter, cache); |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 121 | test_mipmap_notify(reporter, cache); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 122 | } |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 123 | |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 124 | DEF_TEST(BitmapCache_discarded_bitmap, reporter) { |
| 125 | const size_t byteLimit = 100 * 1024; |
| 126 | { |
| 127 | SkResourceCache cache(byteLimit); |
| 128 | testBitmapCache_discarded_bitmap(reporter, &cache, nullptr); |
| 129 | } |
| 130 | { |
Hal Canary | 788c3c4 | 2017-04-25 08:58:57 -0400 | [diff] [blame] | 131 | sk_sp<SkDiscardableMemoryPool> pool(SkDiscardableMemoryPool::Make(byteLimit)); |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 132 | gPool = pool.get(); |
| 133 | SkResourceCache::DiscardableFactory factory = pool_factory; |
| 134 | SkResourceCache cache(factory); |
| 135 | testBitmapCache_discarded_bitmap(reporter, &cache, factory); |
| 136 | } |
| 137 | } |
| 138 | |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 139 | static void test_discarded_image(skiatest::Reporter* reporter, const SkMatrix& transform, |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 140 | sk_sp<SkImage> (*buildImage)()) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 141 | auto surface(SkSurface::MakeRasterN32Premul(10, 10)); |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 142 | SkCanvas* canvas = surface->getCanvas(); |
| 143 | |
| 144 | // SkBitmapCache is global, so other threads could be evicting our bitmaps. Loop a few times |
| 145 | // to mitigate this risk. |
| 146 | const unsigned kRepeatCount = 42; |
| 147 | for (unsigned i = 0; i < kRepeatCount; ++i) { |
| 148 | SkAutoCanvasRestore acr(canvas, true); |
| 149 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 150 | sk_sp<SkImage> image(buildImage()); |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 151 | |
| 152 | // always use high quality to ensure caching when scaled |
| 153 | SkPaint paint; |
| 154 | paint.setFilterQuality(kHigh_SkFilterQuality); |
| 155 | |
| 156 | // draw the image (with a transform, to tickle different code paths) to ensure |
| 157 | // any associated resources get cached |
| 158 | canvas->concat(transform); |
| 159 | canvas->drawImage(image, 0, 0, &paint); |
| 160 | |
Mike Reed | 5fa3d6d | 2017-03-25 09:51:00 -0400 | [diff] [blame] | 161 | const auto desc = SkBitmapCacheDesc::Make(image.get()); |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 162 | |
| 163 | // delete the image |
| 164 | image.reset(nullptr); |
| 165 | |
| 166 | // all resources should have been purged |
| 167 | SkBitmap result; |
Mike Reed | 5fa3d6d | 2017-03-25 09:51:00 -0400 | [diff] [blame] | 168 | REPORTER_ASSERT(reporter, !SkBitmapCache::Find(desc, &result)); |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | |
| 173 | // Verify that associated bitmap cache entries are purged on SkImage destruction. |
| 174 | DEF_TEST(BitmapCache_discarded_image, reporter) { |
| 175 | // Cache entries associated with SkImages fall into two categories: |
| 176 | // |
| 177 | // 1) generated image bitmaps (managed by the image cacherator) |
| 178 | // 2) scaled/resampled bitmaps (cached when HQ filters are used) |
| 179 | // |
| 180 | // To exercise the first cache type, we use generated/picture-backed SkImages. |
| 181 | // To exercise the latter, we draw scaled bitmap images using HQ filters. |
| 182 | |
| 183 | const SkMatrix xforms[] = { |
| 184 | SkMatrix::MakeScale(1, 1), |
| 185 | SkMatrix::MakeScale(1.7f, 0.5f), |
| 186 | }; |
| 187 | |
| 188 | for (size_t i = 0; i < SK_ARRAY_COUNT(xforms); ++i) { |
| 189 | test_discarded_image(reporter, xforms[i], []() { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 190 | auto surface(SkSurface::MakeRasterN32Premul(10, 10)); |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 191 | surface->getCanvas()->clear(SK_ColorCYAN); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 192 | return surface->makeImageSnapshot(); |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 193 | }); |
| 194 | |
| 195 | test_discarded_image(reporter, xforms[i], []() { |
| 196 | SkPictureRecorder recorder; |
| 197 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| 198 | canvas->clear(SK_ColorCYAN); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 199 | return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), |
Brian Osman | 138ea97 | 2016-12-16 11:55:18 -0500 | [diff] [blame] | 200 | SkISize::Make(10, 10), nullptr, nullptr, |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 201 | SkImage::BitDepth::kU8, |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 202 | SkColorSpace::MakeSRGB()); |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 203 | }); |
| 204 | } |
| 205 | } |
Mike Reed | 7a542c5 | 2017-04-11 12:03:44 -0400 | [diff] [blame] | 206 | |
| 207 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 208 | |
| 209 | static void* gTestNamespace; |
| 210 | |
| 211 | struct TestKey : SkResourceCache::Key { |
| 212 | int32_t fData; |
| 213 | |
| 214 | TestKey(int sharedID, int32_t data) : fData(data) { |
| 215 | this->init(&gTestNamespace, sharedID, sizeof(fData)); |
| 216 | } |
| 217 | }; |
| 218 | |
| 219 | struct TestRec : SkResourceCache::Rec { |
| 220 | enum { |
| 221 | kDidInstall = 1 << 0, |
| 222 | }; |
| 223 | |
| 224 | TestKey fKey; |
| 225 | int* fFlags; |
| 226 | bool fCanBePurged; |
| 227 | |
| 228 | TestRec(int sharedID, int32_t data, int* flagPtr) : fKey(sharedID, data), fFlags(flagPtr) { |
| 229 | fCanBePurged = false; |
| 230 | } |
| 231 | |
| 232 | const Key& getKey() const override { return fKey; } |
| 233 | size_t bytesUsed() const override { return 1024; /* just need a value */ } |
| 234 | bool canBePurged() override { return fCanBePurged; } |
| 235 | void postAddInstall(void*) override { |
| 236 | *fFlags |= kDidInstall; |
| 237 | } |
| 238 | const char* getCategory() const override { return "test-category"; } |
| 239 | }; |
| 240 | |
| 241 | static void test_duplicate_add(SkResourceCache* cache, skiatest::Reporter* reporter, |
| 242 | bool purgable) { |
| 243 | int sharedID = 1; |
| 244 | int data = 0; |
| 245 | |
| 246 | int flags0 = 0, flags1 = 0; |
| 247 | |
Mike Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 248 | auto rec0 = std::make_unique<TestRec>(sharedID, data, &flags0); |
| 249 | auto rec1 = std::make_unique<TestRec>(sharedID, data, &flags1); |
Mike Reed | 7a542c5 | 2017-04-11 12:03:44 -0400 | [diff] [blame] | 250 | SkASSERT(rec0->getKey() == rec1->getKey()); |
| 251 | |
| 252 | TestRec* r0 = rec0.get(); // save the bare-pointer since we will release rec0 |
| 253 | r0->fCanBePurged = purgable; |
| 254 | |
| 255 | REPORTER_ASSERT(reporter, !(flags0 & TestRec::kDidInstall)); |
| 256 | REPORTER_ASSERT(reporter, !(flags1 & TestRec::kDidInstall)); |
| 257 | |
| 258 | cache->add(rec0.release(), nullptr); |
| 259 | REPORTER_ASSERT(reporter, flags0 & TestRec::kDidInstall); |
| 260 | REPORTER_ASSERT(reporter, !(flags1 & TestRec::kDidInstall)); |
| 261 | flags0 = 0; // reset the flag |
| 262 | |
| 263 | cache->add(rec1.release(), nullptr); |
| 264 | if (purgable) { |
| 265 | // we purged rec0, and did install rec1 |
| 266 | REPORTER_ASSERT(reporter, !(flags0 & TestRec::kDidInstall)); |
| 267 | REPORTER_ASSERT(reporter, flags1 & TestRec::kDidInstall); |
| 268 | } else { |
| 269 | // we re-used rec0 and did not install rec1 |
| 270 | REPORTER_ASSERT(reporter, flags0 & TestRec::kDidInstall); |
| 271 | REPORTER_ASSERT(reporter, !(flags1 & TestRec::kDidInstall)); |
| 272 | r0->fCanBePurged = true; // so we can cleanup the cache |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Test behavior when the same key is added more than once. |
| 278 | */ |
| 279 | DEF_TEST(ResourceCache_purge, reporter) { |
| 280 | for (bool purgable : { false, true }) { |
| 281 | { |
| 282 | SkResourceCache cache(1024 * 1024); |
| 283 | test_duplicate_add(&cache, reporter, purgable); |
| 284 | } |
| 285 | { |
| 286 | SkResourceCache cache(SkDiscardableMemory::Create); |
| 287 | test_duplicate_add(&cache, reporter, purgable); |
| 288 | } |
| 289 | } |
| 290 | } |