blob: 341eb073a971fcf46ca7c97c4d1ae5f3c6fc4aa7 [file] [log] [blame]
halcanary805ef152014-07-17 06:58:01 -07001/*
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 */
reed30ad5302014-09-16 10:39:55 -07007
halcanary805ef152014-07-17 06:58:01 -07008#include "Test.h"
reed04617132014-08-21 09:46:49 -07009#include "SkBitmapCache.h"
reed30ad5302014-09-16 10:39:55 -070010#include "SkCanvas.h"
danakj790ffe32014-09-11 10:49:52 -070011#include "SkDiscardableMemoryPool.h"
reed30ad5302014-09-16 10:39:55 -070012#include "SkGraphics.h"
Mike Reed7a542c52017-04-11 12:03:44 -040013#include "SkMakeUnique.h"
Mike Reed4dc64742017-03-23 15:51:43 -040014#include "SkMipMap.h"
fmalita3b0d5322015-09-18 08:07:31 -070015#include "SkPicture.h"
16#include "SkPictureRecorder.h"
reed30ad5302014-09-16 10:39:55 -070017#include "SkResourceCache.h"
reed3054be12014-12-10 07:24:28 -080018#include "SkSurface.h"
halcanary805ef152014-07-17 06:58:01 -070019
reed30ad5302014-09-16 10:39:55 -070020////////////////////////////////////////////////////////////////////////////////////////
mtklein26abcf12014-09-04 10:50:53 -070021
reed9d93c2e2014-10-08 05:17:12 -070022enum LockedState {
23 kNotLocked,
24 kLocked,
25};
26
27enum CachedState {
28 kNotInCache,
29 kInCache,
30};
31
32static 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));
halcanary96fcdcc2015-08-27 07:41:13 -070036 bool isLocked = (data->data() != nullptr);
reed9d93c2e2014-10-08 05:17:12 -070037 REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked));
38}
39
40static 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 Osman2b23c4b2018-06-01 12:25:08 -040047 const SkMipMap* mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src), cache);
halcanary96fcdcc2015-08-27 07:41:13 -070048 REPORTER_ASSERT(reporter, nullptr == mipmap);
reed9d93c2e2014-10-08 05:17:12 -070049
Brian Osman2b23c4b2018-06-01 12:25:08 -040050 mipmap = SkMipMapCache::AddAndRef(src, cache);
reed9d93c2e2014-10-08 05:17:12 -070051 REPORTER_ASSERT(reporter, mipmap);
reed7eeba252015-02-24 13:54:23 -080052
53 {
Brian Osman2b23c4b2018-06-01 12:25:08 -040054 const SkMipMap* mm = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src), cache);
reed7eeba252015-02-24 13:54:23 -080055 REPORTER_ASSERT(reporter, mm);
56 REPORTER_ASSERT(reporter, mm == mipmap);
57 mm->unref();
58 }
59
reed9d93c2e2014-10-08 05:17:12 -070060 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 Osman2b23c4b2018-06-01 12:25:08 -040068 mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src), cache);
reed9d93c2e2014-10-08 05:17:12 -070069 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
reed7eeba252015-02-24 13:54:23 -080077static void test_mipmap_notify(skiatest::Reporter* reporter, SkResourceCache* cache) {
78 const int N = 3;
reed6644d932016-06-10 11:41:47 -070079
reed7eeba252015-02-24 13:54:23 -080080 SkBitmap src[N];
81 for (int i = 0; i < N; ++i) {
82 src[i].allocN32Pixels(5, 5);
83 src[i].setImmutable();
Brian Osman2b23c4b2018-06-01 12:25:08 -040084 SkMipMapCache::AddAndRef(src[i], cache)->unref();
reed7eeba252015-02-24 13:54:23 -080085 }
86
87 for (int i = 0; i < N; ++i) {
Mike Reed5fa3d6d2017-03-25 09:51:00 -040088 const auto desc = SkBitmapCacheDesc::Make(src[i]);
Brian Osman2b23c4b2018-06-01 12:25:08 -040089 const SkMipMap* mipmap = SkMipMapCache::FindAndRef(desc, cache);
reed7eeba252015-02-24 13:54:23 -080090 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 Osman2b23c4b2018-06-01 12:25:08 -040099 mipmap = SkMipMapCache::FindAndRef(desc, cache);
reed7eeba252015-02-24 13:54:23 -0800100 REPORTER_ASSERT(reporter, !mipmap);
101 }
102}
103
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800104#include "SkDiscardableMemoryPool.h"
105
Ben Wagnera93a14a2017-08-28 10:34:05 -0400106static SkDiscardableMemoryPool* gPool = nullptr;
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800107static SkDiscardableMemory* pool_factory(size_t bytes) {
108 SkASSERT(gPool);
109 return gPool->create(bytes);
110}
111
112static void testBitmapCache_discarded_bitmap(skiatest::Reporter* reporter, SkResourceCache* cache,
113 SkResourceCache::DiscardableFactory factory) {
reed9d93c2e2014-10-08 05:17:12 -0700114 test_mipmapcache(reporter, cache);
reed7eeba252015-02-24 13:54:23 -0800115 test_mipmap_notify(reporter, cache);
danakj790ffe32014-09-11 10:49:52 -0700116}
fmalita3b0d5322015-09-18 08:07:31 -0700117
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800118DEF_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 Canary788c3c42017-04-25 08:58:57 -0400125 sk_sp<SkDiscardableMemoryPool> pool(SkDiscardableMemoryPool::Make(byteLimit));
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800126 gPool = pool.get();
127 SkResourceCache::DiscardableFactory factory = pool_factory;
128 SkResourceCache cache(factory);
129 testBitmapCache_discarded_bitmap(reporter, &cache, factory);
130 }
131}
132
fmalita3b0d5322015-09-18 08:07:31 -0700133static void test_discarded_image(skiatest::Reporter* reporter, const SkMatrix& transform,
reed9ce9d672016-03-17 10:51:11 -0700134 sk_sp<SkImage> (*buildImage)()) {
reede8f30622016-03-23 18:59:25 -0700135 auto surface(SkSurface::MakeRasterN32Premul(10, 10));
fmalita3b0d5322015-09-18 08:07:31 -0700136 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
reed9ce9d672016-03-17 10:51:11 -0700144 sk_sp<SkImage> image(buildImage());
fmalita3b0d5322015-09-18 08:07:31 -0700145
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 Reed5fa3d6d2017-03-25 09:51:00 -0400155 const auto desc = SkBitmapCacheDesc::Make(image.get());
fmalita3b0d5322015-09-18 08:07:31 -0700156
157 // delete the image
158 image.reset(nullptr);
159
160 // all resources should have been purged
161 SkBitmap result;
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400162 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(desc, &result));
fmalita3b0d5322015-09-18 08:07:31 -0700163 }
164}
165
166
167// Verify that associated bitmap cache entries are purged on SkImage destruction.
168DEF_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], []() {
reede8f30622016-03-23 18:59:25 -0700184 auto surface(SkSurface::MakeRasterN32Premul(10, 10));
fmalita3b0d5322015-09-18 08:07:31 -0700185 surface->getCanvas()->clear(SK_ColorCYAN);
reed9ce9d672016-03-17 10:51:11 -0700186 return surface->makeImageSnapshot();
fmalita3b0d5322015-09-18 08:07:31 -0700187 });
188
189 test_discarded_image(reporter, xforms[i], []() {
190 SkPictureRecorder recorder;
191 SkCanvas* canvas = recorder.beginRecording(10, 10);
192 canvas->clear(SK_ColorCYAN);
reedca2622b2016-03-18 07:25:55 -0700193 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
Brian Osman138ea972016-12-16 11:55:18 -0500194 SkISize::Make(10, 10), nullptr, nullptr,
Matt Sarette94255d2017-01-09 12:38:59 -0500195 SkImage::BitDepth::kU8,
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500196 SkColorSpace::MakeSRGB());
fmalita3b0d5322015-09-18 08:07:31 -0700197 });
198 }
199}
Mike Reed7a542c52017-04-11 12:03:44 -0400200
201///////////////////////////////////////////////////////////////////////////////////////////////////
202
203static void* gTestNamespace;
204
205struct 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
213struct 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
235static 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 */
273DEF_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}