blob: 02b75dd8c9d23d1a496f98113ac34ca94f80f6f1 [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 Osman7b8400d2016-11-08 17:08:54 -050047 const SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy;
reed6644d932016-06-10 11:41:47 -070048
Brian Osman7b8400d2016-11-08 17:08:54 -050049 const SkMipMap* mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src), colorMode,
reed6644d932016-06-10 11:41:47 -070050 cache);
halcanary96fcdcc2015-08-27 07:41:13 -070051 REPORTER_ASSERT(reporter, nullptr == mipmap);
reed9d93c2e2014-10-08 05:17:12 -070052
Brian Osman7b8400d2016-11-08 17:08:54 -050053 mipmap = SkMipMapCache::AddAndRef(src, colorMode, cache);
reed9d93c2e2014-10-08 05:17:12 -070054 REPORTER_ASSERT(reporter, mipmap);
reed7eeba252015-02-24 13:54:23 -080055
56 {
Brian Osman7b8400d2016-11-08 17:08:54 -050057 const SkMipMap* mm = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src), colorMode,
reed6644d932016-06-10 11:41:47 -070058 cache);
reed7eeba252015-02-24 13:54:23 -080059 REPORTER_ASSERT(reporter, mm);
60 REPORTER_ASSERT(reporter, mm == mipmap);
61 mm->unref();
62 }
63
reed9d93c2e2014-10-08 05:17:12 -070064 check_data(reporter, mipmap, 2, kInCache, kLocked);
65
66 mipmap->unref();
67 // tricky, since technically after this I'm no longer an owner, but since the cache is
68 // local, I know it won't get purged behind my back
69 check_data(reporter, mipmap, 1, kInCache, kNotLocked);
70
71 // find us again
Brian Osman7b8400d2016-11-08 17:08:54 -050072 mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src), colorMode, cache);
reed9d93c2e2014-10-08 05:17:12 -070073 check_data(reporter, mipmap, 2, kInCache, kLocked);
74
75 cache->purgeAll();
76 check_data(reporter, mipmap, 1, kNotInCache, kLocked);
77
78 mipmap->unref();
79}
80
reed7eeba252015-02-24 13:54:23 -080081static void test_mipmap_notify(skiatest::Reporter* reporter, SkResourceCache* cache) {
Brian Osman7b8400d2016-11-08 17:08:54 -050082 const SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy;
reed7eeba252015-02-24 13:54:23 -080083 const int N = 3;
reed6644d932016-06-10 11:41:47 -070084
reed7eeba252015-02-24 13:54:23 -080085 SkBitmap src[N];
86 for (int i = 0; i < N; ++i) {
87 src[i].allocN32Pixels(5, 5);
88 src[i].setImmutable();
Brian Osman7b8400d2016-11-08 17:08:54 -050089 SkMipMapCache::AddAndRef(src[i], colorMode, cache)->unref();
reed7eeba252015-02-24 13:54:23 -080090 }
91
92 for (int i = 0; i < N; ++i) {
Mike Reed5fa3d6d2017-03-25 09:51:00 -040093 const auto desc = SkBitmapCacheDesc::Make(src[i]);
94 const SkMipMap* mipmap = SkMipMapCache::FindAndRef(desc, colorMode, cache);
reed7eeba252015-02-24 13:54:23 -080095 if (cache) {
96 // if cache is null, we're working on the global cache, and other threads might purge
97 // it, making this check fragile.
98 REPORTER_ASSERT(reporter, mipmap);
99 }
100 SkSafeUnref(mipmap);
101
102 src[i].reset(); // delete the underlying pixelref, which *should* remove us from the cache
103
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400104 mipmap = SkMipMapCache::FindAndRef(desc, colorMode, cache);
reed7eeba252015-02-24 13:54:23 -0800105 REPORTER_ASSERT(reporter, !mipmap);
106 }
107}
108
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800109#include "SkDiscardableMemoryPool.h"
110
Ben Wagnera93a14a2017-08-28 10:34:05 -0400111static SkDiscardableMemoryPool* gPool = nullptr;
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800112static SkDiscardableMemory* pool_factory(size_t bytes) {
113 SkASSERT(gPool);
114 return gPool->create(bytes);
115}
116
117static void testBitmapCache_discarded_bitmap(skiatest::Reporter* reporter, SkResourceCache* cache,
118 SkResourceCache::DiscardableFactory factory) {
reed9d93c2e2014-10-08 05:17:12 -0700119 test_mipmapcache(reporter, cache);
reed7eeba252015-02-24 13:54:23 -0800120 test_mipmap_notify(reporter, cache);
danakj790ffe32014-09-11 10:49:52 -0700121}
fmalita3b0d5322015-09-18 08:07:31 -0700122
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800123DEF_TEST(BitmapCache_discarded_bitmap, reporter) {
124 const size_t byteLimit = 100 * 1024;
125 {
126 SkResourceCache cache(byteLimit);
127 testBitmapCache_discarded_bitmap(reporter, &cache, nullptr);
128 }
129 {
Hal Canary788c3c42017-04-25 08:58:57 -0400130 sk_sp<SkDiscardableMemoryPool> pool(SkDiscardableMemoryPool::Make(byteLimit));
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800131 gPool = pool.get();
132 SkResourceCache::DiscardableFactory factory = pool_factory;
133 SkResourceCache cache(factory);
134 testBitmapCache_discarded_bitmap(reporter, &cache, factory);
135 }
136}
137
fmalita3b0d5322015-09-18 08:07:31 -0700138static void test_discarded_image(skiatest::Reporter* reporter, const SkMatrix& transform,
reed9ce9d672016-03-17 10:51:11 -0700139 sk_sp<SkImage> (*buildImage)()) {
reede8f30622016-03-23 18:59:25 -0700140 auto surface(SkSurface::MakeRasterN32Premul(10, 10));
fmalita3b0d5322015-09-18 08:07:31 -0700141 SkCanvas* canvas = surface->getCanvas();
142
143 // SkBitmapCache is global, so other threads could be evicting our bitmaps. Loop a few times
144 // to mitigate this risk.
145 const unsigned kRepeatCount = 42;
146 for (unsigned i = 0; i < kRepeatCount; ++i) {
147 SkAutoCanvasRestore acr(canvas, true);
148
reed9ce9d672016-03-17 10:51:11 -0700149 sk_sp<SkImage> image(buildImage());
fmalita3b0d5322015-09-18 08:07:31 -0700150
151 // always use high quality to ensure caching when scaled
152 SkPaint paint;
153 paint.setFilterQuality(kHigh_SkFilterQuality);
154
155 // draw the image (with a transform, to tickle different code paths) to ensure
156 // any associated resources get cached
157 canvas->concat(transform);
158 canvas->drawImage(image, 0, 0, &paint);
159
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400160 const auto desc = SkBitmapCacheDesc::Make(image.get());
fmalita3b0d5322015-09-18 08:07:31 -0700161
162 // delete the image
163 image.reset(nullptr);
164
165 // all resources should have been purged
166 SkBitmap result;
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400167 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(desc, &result));
fmalita3b0d5322015-09-18 08:07:31 -0700168 }
169}
170
171
172// Verify that associated bitmap cache entries are purged on SkImage destruction.
173DEF_TEST(BitmapCache_discarded_image, reporter) {
174 // Cache entries associated with SkImages fall into two categories:
175 //
176 // 1) generated image bitmaps (managed by the image cacherator)
177 // 2) scaled/resampled bitmaps (cached when HQ filters are used)
178 //
179 // To exercise the first cache type, we use generated/picture-backed SkImages.
180 // To exercise the latter, we draw scaled bitmap images using HQ filters.
181
182 const SkMatrix xforms[] = {
183 SkMatrix::MakeScale(1, 1),
184 SkMatrix::MakeScale(1.7f, 0.5f),
185 };
186
187 for (size_t i = 0; i < SK_ARRAY_COUNT(xforms); ++i) {
188 test_discarded_image(reporter, xforms[i], []() {
reede8f30622016-03-23 18:59:25 -0700189 auto surface(SkSurface::MakeRasterN32Premul(10, 10));
fmalita3b0d5322015-09-18 08:07:31 -0700190 surface->getCanvas()->clear(SK_ColorCYAN);
reed9ce9d672016-03-17 10:51:11 -0700191 return surface->makeImageSnapshot();
fmalita3b0d5322015-09-18 08:07:31 -0700192 });
193
194 test_discarded_image(reporter, xforms[i], []() {
195 SkPictureRecorder recorder;
196 SkCanvas* canvas = recorder.beginRecording(10, 10);
197 canvas->clear(SK_ColorCYAN);
reedca2622b2016-03-18 07:25:55 -0700198 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
Brian Osman138ea972016-12-16 11:55:18 -0500199 SkISize::Make(10, 10), nullptr, nullptr,
Matt Sarette94255d2017-01-09 12:38:59 -0500200 SkImage::BitDepth::kU8,
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500201 SkColorSpace::MakeSRGB());
fmalita3b0d5322015-09-18 08:07:31 -0700202 });
203 }
204}
Mike Reed7a542c52017-04-11 12:03:44 -0400205
206///////////////////////////////////////////////////////////////////////////////////////////////////
207
208static void* gTestNamespace;
209
210struct TestKey : SkResourceCache::Key {
211 int32_t fData;
212
213 TestKey(int sharedID, int32_t data) : fData(data) {
214 this->init(&gTestNamespace, sharedID, sizeof(fData));
215 }
216};
217
218struct TestRec : SkResourceCache::Rec {
219 enum {
220 kDidInstall = 1 << 0,
221 };
222
223 TestKey fKey;
224 int* fFlags;
225 bool fCanBePurged;
226
227 TestRec(int sharedID, int32_t data, int* flagPtr) : fKey(sharedID, data), fFlags(flagPtr) {
228 fCanBePurged = false;
229 }
230
231 const Key& getKey() const override { return fKey; }
232 size_t bytesUsed() const override { return 1024; /* just need a value */ }
233 bool canBePurged() override { return fCanBePurged; }
234 void postAddInstall(void*) override {
235 *fFlags |= kDidInstall;
236 }
237 const char* getCategory() const override { return "test-category"; }
238};
239
240static void test_duplicate_add(SkResourceCache* cache, skiatest::Reporter* reporter,
241 bool purgable) {
242 int sharedID = 1;
243 int data = 0;
244
245 int flags0 = 0, flags1 = 0;
246
247 auto rec0 = skstd::make_unique<TestRec>(sharedID, data, &flags0);
248 auto rec1 = skstd::make_unique<TestRec>(sharedID, data, &flags1);
249 SkASSERT(rec0->getKey() == rec1->getKey());
250
251 TestRec* r0 = rec0.get(); // save the bare-pointer since we will release rec0
252 r0->fCanBePurged = purgable;
253
254 REPORTER_ASSERT(reporter, !(flags0 & TestRec::kDidInstall));
255 REPORTER_ASSERT(reporter, !(flags1 & TestRec::kDidInstall));
256
257 cache->add(rec0.release(), nullptr);
258 REPORTER_ASSERT(reporter, flags0 & TestRec::kDidInstall);
259 REPORTER_ASSERT(reporter, !(flags1 & TestRec::kDidInstall));
260 flags0 = 0; // reset the flag
261
262 cache->add(rec1.release(), nullptr);
263 if (purgable) {
264 // we purged rec0, and did install rec1
265 REPORTER_ASSERT(reporter, !(flags0 & TestRec::kDidInstall));
266 REPORTER_ASSERT(reporter, flags1 & TestRec::kDidInstall);
267 } else {
268 // we re-used rec0 and did not install rec1
269 REPORTER_ASSERT(reporter, flags0 & TestRec::kDidInstall);
270 REPORTER_ASSERT(reporter, !(flags1 & TestRec::kDidInstall));
271 r0->fCanBePurged = true; // so we can cleanup the cache
272 }
273}
274
275/*
276 * Test behavior when the same key is added more than once.
277 */
278DEF_TEST(ResourceCache_purge, reporter) {
279 for (bool purgable : { false, true }) {
280 {
281 SkResourceCache cache(1024 * 1024);
282 test_duplicate_add(&cache, reporter, purgable);
283 }
284 {
285 SkResourceCache cache(SkDiscardableMemory::Create);
286 test_duplicate_add(&cache, reporter, purgable);
287 }
288 }
289}