2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkBitmapCache.h"
11 #include "SkDiscardableMemoryPool.h"
12 #include "SkGraphics.h"
13 #include "SkResourceCache.h"
15 static const int kCanvasSize = 1;
16 static const int kBitmapSize = 16;
17 static const int kScale = 8;
19 static bool is_in_scaled_image_cache(const SkBitmap& orig,
23 float roundedImageWidth = SkScalarRoundToScalar(orig.width() * xScale);
24 float roundedImageHeight = SkScalarRoundToScalar(orig.height() * xScale);
25 return SkBitmapCache::Find(orig, roundedImageWidth, roundedImageHeight, &scaled);
28 // Draw a scaled bitmap, then return true iff it has been cached.
29 static bool test_scaled_image_cache_useage() {
30 SkAutoTUnref<SkCanvas> canvas(
31 SkCanvas::NewRasterN32(kCanvasSize, kCanvasSize));
33 bitmap.allocN32Pixels(kBitmapSize, kBitmapSize);
34 bitmap.eraseColor(0xFFFFFFFF);
35 SkScalar scale = SkIntToScalar(kScale);
36 SkScalar scaledSize = SkIntToScalar(kBitmapSize) * scale;
37 canvas->clipRect(SkRect::MakeLTRB(0, 0, scaledSize, scaledSize));
39 paint.setFilterLevel(SkPaint::kHigh_FilterLevel);
41 canvas->drawBitmapRect(bitmap,
42 SkRect::MakeLTRB(0, 0, scaledSize, scaledSize),
45 return is_in_scaled_image_cache(bitmap, scale, scale);
48 // http://crbug.com/389439
49 DEF_TEST(ResourceCache_SingleAllocationByteLimit, reporter) {
50 size_t originalByteLimit = SkGraphics::GetResourceCacheTotalByteLimit();
51 size_t originalAllocationLimit =
52 SkGraphics::GetResourceCacheSingleAllocationByteLimit();
54 size_t size = kBitmapSize * kScale * kBitmapSize * kScale
55 * SkColorTypeBytesPerPixel(kN32_SkColorType);
57 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache
58 SkGraphics::SetResourceCacheTotalByteLimit(2 * size);
59 SkGraphics::SetResourceCacheSingleAllocationByteLimit(0); // No limit
61 REPORTER_ASSERT(reporter, test_scaled_image_cache_useage());
63 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache
64 SkGraphics::SetResourceCacheTotalByteLimit(2 * size);
65 SkGraphics::SetResourceCacheSingleAllocationByteLimit(size * 2); // big enough
67 REPORTER_ASSERT(reporter, test_scaled_image_cache_useage());
69 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache
70 SkGraphics::SetResourceCacheTotalByteLimit(2 * size);
71 SkGraphics::SetResourceCacheSingleAllocationByteLimit(size / 2); // too small
73 REPORTER_ASSERT(reporter, !test_scaled_image_cache_useage());
75 SkGraphics::SetResourceCacheSingleAllocationByteLimit(originalAllocationLimit);
76 SkGraphics::SetResourceCacheTotalByteLimit(originalByteLimit);
79 ////////////////////////////////////////////////////////////////////////////////////////
81 static void make_bitmap(SkBitmap* bitmap, const SkImageInfo& info, SkBitmap::Allocator* allocator) {
83 bitmap->setInfo(info);
84 allocator->allocPixelRef(bitmap, 0);
86 bitmap->allocPixels(info);
90 // http://skbug.com/2894
91 DEF_TEST(BitmapCache_add_rect, reporter) {
92 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardableFactory();
93 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator();
95 SkAutoTDelete<SkResourceCache> cache;
97 cache.reset(SkNEW_ARGS(SkResourceCache, (factory)));
99 const size_t byteLimit = 100 * 1024;
100 cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit)));
102 SkBitmap cachedBitmap;
103 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator);
104 cachedBitmap.setImmutable();
107 SkIRect rect = SkIRect::MakeWH(5, 5);
110 REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedBitmap.getGenerationID(), SkIRect::MakeWH(4, 6), cachedBitmap, cache));
111 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
112 // Wrong offset value
113 REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedBitmap.getGenerationID(), SkIRect::MakeXYWH(-1, 0, 5, 5), cachedBitmap, cache));
114 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
116 // Should not be in the cache
117 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
119 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache));
120 // Should be in the cache, we just added it
121 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
124 #include "SkMipMap.h"
136 static void check_data(skiatest::Reporter* reporter, const SkCachedData* data,
137 int refcnt, CachedState cacheState, LockedState lockedState) {
138 REPORTER_ASSERT(reporter, data->testing_only_getRefCnt() == refcnt);
139 REPORTER_ASSERT(reporter, data->testing_only_isInCache() == (kInCache == cacheState));
140 bool isLocked = (data->data() != NULL);
141 REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked));
144 static void test_mipmapcache(skiatest::Reporter* reporter, SkResourceCache* cache) {
148 src.allocN32Pixels(5, 5);
151 const SkMipMap* mipmap = SkMipMapCache::FindAndRef(src, cache);
152 REPORTER_ASSERT(reporter, NULL == mipmap);
154 mipmap = SkMipMapCache::AddAndRef(src, cache);
155 REPORTER_ASSERT(reporter, mipmap);
156 check_data(reporter, mipmap, 2, kInCache, kLocked);
159 // tricky, since technically after this I'm no longer an owner, but since the cache is
160 // local, I know it won't get purged behind my back
161 check_data(reporter, mipmap, 1, kInCache, kNotLocked);
164 mipmap = SkMipMapCache::FindAndRef(src, cache);
165 check_data(reporter, mipmap, 2, kInCache, kLocked);
168 check_data(reporter, mipmap, 1, kNotInCache, kLocked);
173 DEF_TEST(BitmapCache_discarded_bitmap, reporter) {
174 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardableFactory();
175 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator();
177 SkAutoTDelete<SkResourceCache> cache;
179 cache.reset(SkNEW_ARGS(SkResourceCache, (factory)));
181 const size_t byteLimit = 100 * 1024;
182 cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit)));
184 SkBitmap cachedBitmap;
185 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator);
186 cachedBitmap.setImmutable();
187 cachedBitmap.unlockPixels();
190 SkIRect rect = SkIRect::MakeWH(5, 5);
192 // Add a bitmap to the cache.
193 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache));
194 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
196 // Finding more than once works fine.
197 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
200 // Drop the pixels in the bitmap.
202 REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed() > 0);
203 SkGetGlobalDiscardableMemoryPool()->dumpPool();
204 REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed() == 0);
206 // The bitmap is not in the cache since it has been dropped.
207 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
210 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator);
211 cachedBitmap.setImmutable();
212 cachedBitmap.unlockPixels();
214 // We can add the bitmap back to the cache and find it again.
215 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache));
216 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
218 test_mipmapcache(reporter, cache);