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