blob: f363fb7a44b72cd894fa07abc897ea37d29ecf21 [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"
fmalita3b0d5322015-09-18 08:07:31 -070013#include "SkPicture.h"
14#include "SkPictureRecorder.h"
reed30ad5302014-09-16 10:39:55 -070015#include "SkResourceCache.h"
reed3054be12014-12-10 07:24:28 -080016#include "SkSurface.h"
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080017#include "SkTypes.h"
halcanary805ef152014-07-17 06:58:01 -070018
reed30ad5302014-09-16 10:39:55 -070019////////////////////////////////////////////////////////////////////////////////////////
mtklein26abcf12014-09-04 10:50:53 -070020
reed30ad5302014-09-16 10:39:55 -070021static void make_bitmap(SkBitmap* bitmap, const SkImageInfo& info, SkBitmap::Allocator* allocator) {
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080022 if (info.colorType() == kIndex_8_SkColorType) {
23 bitmap->setInfo(info);
24 SkPMColor ctStorage[256];
25 memset(ctStorage, 0xFF, sizeof(ctStorage)); // init with opaque-white for the moment
26 SkAutoTUnref<SkColorTable> ctable(new SkColorTable(ctStorage, 256));
27 bitmap->allocPixels(allocator, ctable);
28 } else if (allocator) {
reed30ad5302014-09-16 10:39:55 -070029 bitmap->setInfo(info);
30 allocator->allocPixelRef(bitmap, 0);
piotaixr42b0dfe2014-09-03 11:33:13 -070031 } else {
reed30ad5302014-09-16 10:39:55 -070032 bitmap->allocPixels(info);
piotaixr42b0dfe2014-09-03 11:33:13 -070033 }
piotaixr42b0dfe2014-09-03 11:33:13 -070034}
35
halcanary6950de62015-11-07 05:29:00 -080036// https://bug.skia.org/2894
piotaixr42b0dfe2014-09-03 11:33:13 -070037DEF_TEST(BitmapCache_add_rect, reporter) {
reed30ad5302014-09-16 10:39:55 -070038 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardableFactory();
39 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator();
40
41 SkAutoTDelete<SkResourceCache> cache;
42 if (factory) {
halcanary385fe4d2015-08-26 13:07:48 -070043 cache.reset(new SkResourceCache(factory));
reed30ad5302014-09-16 10:39:55 -070044 } else {
45 const size_t byteLimit = 100 * 1024;
halcanary385fe4d2015-08-26 13:07:48 -070046 cache.reset(new SkResourceCache(byteLimit));
reed30ad5302014-09-16 10:39:55 -070047 }
48 SkBitmap cachedBitmap;
49 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator);
piotaixr42b0dfe2014-09-03 11:33:13 -070050 cachedBitmap.setImmutable();
51
reed30ad5302014-09-16 10:39:55 -070052 SkBitmap bm;
53 SkIRect rect = SkIRect::MakeWH(5, 5);
reed83787d02015-02-25 07:17:11 -080054 uint32_t cachedID = cachedBitmap.getGenerationID();
55 SkPixelRef* cachedPR = cachedBitmap.pixelRef();
reed30ad5302014-09-16 10:39:55 -070056
piotaixr42b0dfe2014-09-03 11:33:13 -070057 // Wrong subset size
reed83787d02015-02-25 07:17:11 -080058 REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedPR, SkIRect::MakeWH(4, 6), cachedBitmap, cache));
59 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedID, rect, &bm, cache));
piotaixr42b0dfe2014-09-03 11:33:13 -070060 // Wrong offset value
reed83787d02015-02-25 07:17:11 -080061 REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedPR, SkIRect::MakeXYWH(-1, 0, 5, 5), cachedBitmap, cache));
62 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedID, rect, &bm, cache));
piotaixr42b0dfe2014-09-03 11:33:13 -070063
64 // Should not be in the cache
reed83787d02015-02-25 07:17:11 -080065 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedID, rect, &bm, cache));
piotaixr42b0dfe2014-09-03 11:33:13 -070066
reed83787d02015-02-25 07:17:11 -080067 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedPR, rect, cachedBitmap, cache));
piotaixr42b0dfe2014-09-03 11:33:13 -070068 // Should be in the cache, we just added it
reed83787d02015-02-25 07:17:11 -080069 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedID, rect, &bm, cache));
piotaixr42b0dfe2014-09-03 11:33:13 -070070}
danakj790ffe32014-09-11 10:49:52 -070071
reed9d93c2e2014-10-08 05:17:12 -070072#include "SkMipMap.h"
73
74enum LockedState {
75 kNotLocked,
76 kLocked,
77};
78
79enum CachedState {
80 kNotInCache,
81 kInCache,
82};
83
84static void check_data(skiatest::Reporter* reporter, const SkCachedData* data,
85 int refcnt, CachedState cacheState, LockedState lockedState) {
86 REPORTER_ASSERT(reporter, data->testing_only_getRefCnt() == refcnt);
87 REPORTER_ASSERT(reporter, data->testing_only_isInCache() == (kInCache == cacheState));
halcanary96fcdcc2015-08-27 07:41:13 -070088 bool isLocked = (data->data() != nullptr);
reed9d93c2e2014-10-08 05:17:12 -070089 REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked));
90}
91
92static void test_mipmapcache(skiatest::Reporter* reporter, SkResourceCache* cache) {
93 cache->purgeAll();
94
95 SkBitmap src;
96 src.allocN32Pixels(5, 5);
97 src.setImmutable();
98
reed6644d932016-06-10 11:41:47 -070099 const SkSourceGammaTreatment treatment = SkSourceGammaTreatment::kIgnore;
100
101 const SkMipMap* mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src), treatment,
102 cache);
halcanary96fcdcc2015-08-27 07:41:13 -0700103 REPORTER_ASSERT(reporter, nullptr == mipmap);
reed9d93c2e2014-10-08 05:17:12 -0700104
reed6644d932016-06-10 11:41:47 -0700105 mipmap = SkMipMapCache::AddAndRef(src, treatment, cache);
reed9d93c2e2014-10-08 05:17:12 -0700106 REPORTER_ASSERT(reporter, mipmap);
reed7eeba252015-02-24 13:54:23 -0800107
108 {
reed6644d932016-06-10 11:41:47 -0700109 const SkMipMap* mm = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src), treatment,
110 cache);
reed7eeba252015-02-24 13:54:23 -0800111 REPORTER_ASSERT(reporter, mm);
112 REPORTER_ASSERT(reporter, mm == mipmap);
113 mm->unref();
114 }
115
reed9d93c2e2014-10-08 05:17:12 -0700116 check_data(reporter, mipmap, 2, kInCache, kLocked);
117
118 mipmap->unref();
119 // tricky, since technically after this I'm no longer an owner, but since the cache is
120 // local, I know it won't get purged behind my back
121 check_data(reporter, mipmap, 1, kInCache, kNotLocked);
122
123 // find us again
reed6644d932016-06-10 11:41:47 -0700124 mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src), treatment, cache);
reed9d93c2e2014-10-08 05:17:12 -0700125 check_data(reporter, mipmap, 2, kInCache, kLocked);
126
127 cache->purgeAll();
128 check_data(reporter, mipmap, 1, kNotInCache, kLocked);
129
130 mipmap->unref();
131}
132
reed7eeba252015-02-24 13:54:23 -0800133static void test_mipmap_notify(skiatest::Reporter* reporter, SkResourceCache* cache) {
reed6644d932016-06-10 11:41:47 -0700134 const SkSourceGammaTreatment treatment = SkSourceGammaTreatment::kIgnore;
reed7eeba252015-02-24 13:54:23 -0800135 const int N = 3;
reed6644d932016-06-10 11:41:47 -0700136
reed7eeba252015-02-24 13:54:23 -0800137 SkBitmap src[N];
138 for (int i = 0; i < N; ++i) {
139 src[i].allocN32Pixels(5, 5);
140 src[i].setImmutable();
reed6644d932016-06-10 11:41:47 -0700141 SkMipMapCache::AddAndRef(src[i], treatment, cache)->unref();
reed7eeba252015-02-24 13:54:23 -0800142 }
143
144 for (int i = 0; i < N; ++i) {
reed6644d932016-06-10 11:41:47 -0700145 const SkMipMap* mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src[i]),
146 treatment, cache);
reed7eeba252015-02-24 13:54:23 -0800147 if (cache) {
148 // if cache is null, we're working on the global cache, and other threads might purge
149 // it, making this check fragile.
150 REPORTER_ASSERT(reporter, mipmap);
151 }
152 SkSafeUnref(mipmap);
153
154 src[i].reset(); // delete the underlying pixelref, which *should* remove us from the cache
155
reed6644d932016-06-10 11:41:47 -0700156 mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src[i]), treatment, cache);
reed7eeba252015-02-24 13:54:23 -0800157 REPORTER_ASSERT(reporter, !mipmap);
158 }
159}
160
161static void test_bitmap_notify(skiatest::Reporter* reporter, SkResourceCache* cache) {
162 const SkIRect subset = SkIRect::MakeWH(5, 5);
163 const int N = 3;
164 SkBitmap src[N], dst[N];
165 for (int i = 0; i < N; ++i) {
166 src[i].allocN32Pixels(5, 5);
167 src[i].setImmutable();
168 dst[i].allocN32Pixels(5, 5);
169 dst[i].setImmutable();
reed83787d02015-02-25 07:17:11 -0800170 SkBitmapCache::Add(src[i].pixelRef(), subset, dst[i], cache);
reed7eeba252015-02-24 13:54:23 -0800171 }
172
173 for (int i = 0; i < N; ++i) {
174 const uint32_t genID = src[i].getGenerationID();
175 SkBitmap result;
176 bool found = SkBitmapCache::Find(genID, subset, &result, cache);
177 if (cache) {
178 // if cache is null, we're working on the global cache, and other threads might purge
179 // it, making this check fragile.
180 REPORTER_ASSERT(reporter, found);
181 }
182
183 src[i].reset(); // delete the underlying pixelref, which *should* remove us from the cache
184
185 found = SkBitmapCache::Find(genID, subset, &result, cache);
186 REPORTER_ASSERT(reporter, !found);
187 }
188}
189
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800190#include "SkDiscardableMemoryPool.h"
191
192static SkDiscardableMemoryPool* gPool = 0;
193static SkDiscardableMemory* pool_factory(size_t bytes) {
194 SkASSERT(gPool);
195 return gPool->create(bytes);
196}
197
198static void testBitmapCache_discarded_bitmap(skiatest::Reporter* reporter, SkResourceCache* cache,
199 SkResourceCache::DiscardableFactory factory) {
200 SkBitmap::Allocator* allocator = cache->allocator();
201 const SkColorType testTypes[] = {
202 kAlpha_8_SkColorType,
203 kRGB_565_SkColorType,
204 kRGBA_8888_SkColorType,
205 kBGRA_8888_SkColorType,
206 kIndex_8_SkColorType,
207 kGray_8_SkColorType
208 };
209 for (const SkColorType testType : testTypes) {
210 SkBitmap cachedBitmap;
211 make_bitmap(&cachedBitmap, SkImageInfo::Make(5, 5, testType, kPremul_SkAlphaType),
212 allocator);
213 cachedBitmap.setImmutable();
214 cachedBitmap.unlockPixels();
215
216 SkBitmap bm;
217 SkIRect rect = SkIRect::MakeWH(5, 5);
218
219 // Add a bitmap to the cache.
220 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), rect, cachedBitmap,
221 cache));
222 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm,
223 cache));
224
225 // Finding more than once works fine.
226 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm,
227 cache));
228 bm.unlockPixels();
229
230 // Drop the pixels in the bitmap.
231 if (factory) {
232 REPORTER_ASSERT(reporter, gPool->getRAMUsed() > 0);
233 gPool->dumpPool();
234
235 // The bitmap is not in the cache since it has been dropped.
236 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect,
237 &bm, cache));
238 }
239
240 make_bitmap(&cachedBitmap, SkImageInfo::Make(5, 5, testType, kPremul_SkAlphaType),
241 allocator);
242 cachedBitmap.setImmutable();
243 cachedBitmap.unlockPixels();
244
245 // We can add the bitmap back to the cache and find it again.
246 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), rect, cachedBitmap,
247 cache));
248 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm,
249 cache));
reed30ad5302014-09-16 10:39:55 -0700250 }
reed9d93c2e2014-10-08 05:17:12 -0700251 test_mipmapcache(reporter, cache);
reed7eeba252015-02-24 13:54:23 -0800252 test_bitmap_notify(reporter, cache);
253 test_mipmap_notify(reporter, cache);
danakj790ffe32014-09-11 10:49:52 -0700254}
fmalita3b0d5322015-09-18 08:07:31 -0700255
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800256DEF_TEST(BitmapCache_discarded_bitmap, reporter) {
257 const size_t byteLimit = 100 * 1024;
258 {
259 SkResourceCache cache(byteLimit);
260 testBitmapCache_discarded_bitmap(reporter, &cache, nullptr);
261 }
262 {
263 SkAutoTUnref<SkDiscardableMemoryPool> pool(
264 SkDiscardableMemoryPool::Create(byteLimit, nullptr));
265 gPool = pool.get();
266 SkResourceCache::DiscardableFactory factory = pool_factory;
267 SkResourceCache cache(factory);
268 testBitmapCache_discarded_bitmap(reporter, &cache, factory);
269 }
270}
271
fmalita3b0d5322015-09-18 08:07:31 -0700272static void test_discarded_image(skiatest::Reporter* reporter, const SkMatrix& transform,
reed9ce9d672016-03-17 10:51:11 -0700273 sk_sp<SkImage> (*buildImage)()) {
reede8f30622016-03-23 18:59:25 -0700274 auto surface(SkSurface::MakeRasterN32Premul(10, 10));
fmalita3b0d5322015-09-18 08:07:31 -0700275 SkCanvas* canvas = surface->getCanvas();
276
277 // SkBitmapCache is global, so other threads could be evicting our bitmaps. Loop a few times
278 // to mitigate this risk.
279 const unsigned kRepeatCount = 42;
280 for (unsigned i = 0; i < kRepeatCount; ++i) {
281 SkAutoCanvasRestore acr(canvas, true);
282
reed9ce9d672016-03-17 10:51:11 -0700283 sk_sp<SkImage> image(buildImage());
fmalita3b0d5322015-09-18 08:07:31 -0700284
285 // always use high quality to ensure caching when scaled
286 SkPaint paint;
287 paint.setFilterQuality(kHigh_SkFilterQuality);
288
289 // draw the image (with a transform, to tickle different code paths) to ensure
290 // any associated resources get cached
291 canvas->concat(transform);
292 canvas->drawImage(image, 0, 0, &paint);
293
294 auto imageId = image->uniqueID();
295
296 // delete the image
297 image.reset(nullptr);
298
299 // all resources should have been purged
300 SkBitmap result;
301 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(imageId, &result));
302 }
303}
304
305
306// Verify that associated bitmap cache entries are purged on SkImage destruction.
307DEF_TEST(BitmapCache_discarded_image, reporter) {
308 // Cache entries associated with SkImages fall into two categories:
309 //
310 // 1) generated image bitmaps (managed by the image cacherator)
311 // 2) scaled/resampled bitmaps (cached when HQ filters are used)
312 //
313 // To exercise the first cache type, we use generated/picture-backed SkImages.
314 // To exercise the latter, we draw scaled bitmap images using HQ filters.
315
316 const SkMatrix xforms[] = {
317 SkMatrix::MakeScale(1, 1),
318 SkMatrix::MakeScale(1.7f, 0.5f),
319 };
320
321 for (size_t i = 0; i < SK_ARRAY_COUNT(xforms); ++i) {
322 test_discarded_image(reporter, xforms[i], []() {
reede8f30622016-03-23 18:59:25 -0700323 auto surface(SkSurface::MakeRasterN32Premul(10, 10));
fmalita3b0d5322015-09-18 08:07:31 -0700324 surface->getCanvas()->clear(SK_ColorCYAN);
reed9ce9d672016-03-17 10:51:11 -0700325 return surface->makeImageSnapshot();
fmalita3b0d5322015-09-18 08:07:31 -0700326 });
327
328 test_discarded_image(reporter, xforms[i], []() {
329 SkPictureRecorder recorder;
330 SkCanvas* canvas = recorder.beginRecording(10, 10);
331 canvas->clear(SK_ColorCYAN);
reedca2622b2016-03-18 07:25:55 -0700332 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
333 SkISize::Make(10, 10), nullptr, nullptr);
fmalita3b0d5322015-09-18 08:07:31 -0700334 });
335 }
336}