blob: 179e771ed826ba3c00362544d70213a59567fb5b [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"
13#include "SkResourceCache.h"
reed3054be12014-12-10 07:24:28 -080014#include "SkSurface.h"
halcanary805ef152014-07-17 06:58:01 -070015
16static const int kCanvasSize = 1;
17static const int kBitmapSize = 16;
18static const int kScale = 8;
19
halcanary1d1795b2014-07-18 09:18:40 -070020static bool is_in_scaled_image_cache(const SkBitmap& orig,
21 SkScalar xScale,
22 SkScalar yScale) {
23 SkBitmap scaled;
humperd73c1692014-08-28 14:27:42 -070024 float roundedImageWidth = SkScalarRoundToScalar(orig.width() * xScale);
yunchao.he90acb8e2015-01-23 17:06:20 -080025 float roundedImageHeight = SkScalarRoundToScalar(orig.height() * yScale);
humperd73c1692014-08-28 14:27:42 -070026 return SkBitmapCache::Find(orig, roundedImageWidth, roundedImageHeight, &scaled);
halcanary1d1795b2014-07-18 09:18:40 -070027}
28
yunchao.he90acb8e2015-01-23 17:06:20 -080029// Draw a scaled bitmap, then return true if it has been cached.
30static bool test_scaled_image_cache_usage() {
reed3054be12014-12-10 07:24:28 -080031 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(kCanvasSize, kCanvasSize));
32 SkCanvas* canvas = surface->getCanvas();
halcanary805ef152014-07-17 06:58:01 -070033 SkBitmap bitmap;
reed84825042014-09-02 12:50:45 -070034 bitmap.allocN32Pixels(kBitmapSize, kBitmapSize);
halcanary0db38cc2014-07-17 10:17:28 -070035 bitmap.eraseColor(0xFFFFFFFF);
yunchao.he90acb8e2015-01-23 17:06:20 -080036 SkScalar xScale = SkIntToScalar(kScale);
37 SkScalar yScale = xScale / 2;
38 SkScalar xScaledSize = SkIntToScalar(kBitmapSize) * xScale;
39 SkScalar yScaledSize = SkIntToScalar(kBitmapSize) * yScale;
40 canvas->clipRect(SkRect::MakeLTRB(0, 0, xScaledSize, yScaledSize));
halcanary805ef152014-07-17 06:58:01 -070041 SkPaint paint;
42 paint.setFilterLevel(SkPaint::kHigh_FilterLevel);
halcanary1d1795b2014-07-18 09:18:40 -070043
halcanary805ef152014-07-17 06:58:01 -070044 canvas->drawBitmapRect(bitmap,
yunchao.he90acb8e2015-01-23 17:06:20 -080045 SkRect::MakeLTRB(0, 0, xScaledSize, yScaledSize),
halcanary805ef152014-07-17 06:58:01 -070046 &paint);
halcanary1d1795b2014-07-18 09:18:40 -070047
yunchao.he90acb8e2015-01-23 17:06:20 -080048 return is_in_scaled_image_cache(bitmap, xScale, yScale);
halcanary805ef152014-07-17 06:58:01 -070049}
50
51// http://crbug.com/389439
reed011f39a2014-08-28 13:35:23 -070052DEF_TEST(ResourceCache_SingleAllocationByteLimit, reporter) {
53 size_t originalByteLimit = SkGraphics::GetResourceCacheTotalByteLimit();
halcanary805ef152014-07-17 06:58:01 -070054 size_t originalAllocationLimit =
reed011f39a2014-08-28 13:35:23 -070055 SkGraphics::GetResourceCacheSingleAllocationByteLimit();
halcanary805ef152014-07-17 06:58:01 -070056
57 size_t size = kBitmapSize * kScale * kBitmapSize * kScale
58 * SkColorTypeBytesPerPixel(kN32_SkColorType);
59
reed011f39a2014-08-28 13:35:23 -070060 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache
61 SkGraphics::SetResourceCacheTotalByteLimit(2 * size);
62 SkGraphics::SetResourceCacheSingleAllocationByteLimit(0); // No limit
halcanary805ef152014-07-17 06:58:01 -070063
yunchao.he90acb8e2015-01-23 17:06:20 -080064 REPORTER_ASSERT(reporter, test_scaled_image_cache_usage());
halcanary805ef152014-07-17 06:58:01 -070065
reed011f39a2014-08-28 13:35:23 -070066 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache
67 SkGraphics::SetResourceCacheTotalByteLimit(2 * size);
68 SkGraphics::SetResourceCacheSingleAllocationByteLimit(size * 2); // big enough
halcanary805ef152014-07-17 06:58:01 -070069
yunchao.he90acb8e2015-01-23 17:06:20 -080070 REPORTER_ASSERT(reporter, test_scaled_image_cache_usage());
halcanary805ef152014-07-17 06:58:01 -070071
reed011f39a2014-08-28 13:35:23 -070072 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache
73 SkGraphics::SetResourceCacheTotalByteLimit(2 * size);
74 SkGraphics::SetResourceCacheSingleAllocationByteLimit(size / 2); // too small
halcanary805ef152014-07-17 06:58:01 -070075
yunchao.he90acb8e2015-01-23 17:06:20 -080076 REPORTER_ASSERT(reporter, !test_scaled_image_cache_usage());
halcanary805ef152014-07-17 06:58:01 -070077
reed011f39a2014-08-28 13:35:23 -070078 SkGraphics::SetResourceCacheSingleAllocationByteLimit(originalAllocationLimit);
79 SkGraphics::SetResourceCacheTotalByteLimit(originalByteLimit);
halcanary805ef152014-07-17 06:58:01 -070080}
piotaixr42b0dfe2014-09-03 11:33:13 -070081
reed30ad5302014-09-16 10:39:55 -070082////////////////////////////////////////////////////////////////////////////////////////
mtklein26abcf12014-09-04 10:50:53 -070083
reed30ad5302014-09-16 10:39:55 -070084static void make_bitmap(SkBitmap* bitmap, const SkImageInfo& info, SkBitmap::Allocator* allocator) {
bsalomon49f085d2014-09-05 13:34:00 -070085 if (allocator) {
reed30ad5302014-09-16 10:39:55 -070086 bitmap->setInfo(info);
87 allocator->allocPixelRef(bitmap, 0);
piotaixr42b0dfe2014-09-03 11:33:13 -070088 } else {
reed30ad5302014-09-16 10:39:55 -070089 bitmap->allocPixels(info);
piotaixr42b0dfe2014-09-03 11:33:13 -070090 }
piotaixr42b0dfe2014-09-03 11:33:13 -070091}
92
93// http://skbug.com/2894
94DEF_TEST(BitmapCache_add_rect, reporter) {
reed30ad5302014-09-16 10:39:55 -070095 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardableFactory();
96 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator();
97
98 SkAutoTDelete<SkResourceCache> cache;
99 if (factory) {
100 cache.reset(SkNEW_ARGS(SkResourceCache, (factory)));
101 } else {
102 const size_t byteLimit = 100 * 1024;
103 cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit)));
104 }
105 SkBitmap cachedBitmap;
106 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator);
piotaixr42b0dfe2014-09-03 11:33:13 -0700107 cachedBitmap.setImmutable();
108
reed30ad5302014-09-16 10:39:55 -0700109 SkBitmap bm;
110 SkIRect rect = SkIRect::MakeWH(5, 5);
111
piotaixr42b0dfe2014-09-03 11:33:13 -0700112 // Wrong subset size
reed30ad5302014-09-16 10:39:55 -0700113 REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedBitmap.getGenerationID(), SkIRect::MakeWH(4, 6), cachedBitmap, cache));
114 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
piotaixr42b0dfe2014-09-03 11:33:13 -0700115 // Wrong offset value
reed30ad5302014-09-16 10:39:55 -0700116 REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedBitmap.getGenerationID(), SkIRect::MakeXYWH(-1, 0, 5, 5), cachedBitmap, cache));
117 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
piotaixr42b0dfe2014-09-03 11:33:13 -0700118
119 // Should not be in the cache
reed30ad5302014-09-16 10:39:55 -0700120 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
piotaixr42b0dfe2014-09-03 11:33:13 -0700121
reed30ad5302014-09-16 10:39:55 -0700122 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache));
piotaixr42b0dfe2014-09-03 11:33:13 -0700123 // Should be in the cache, we just added it
reed30ad5302014-09-16 10:39:55 -0700124 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
piotaixr42b0dfe2014-09-03 11:33:13 -0700125}
danakj790ffe32014-09-11 10:49:52 -0700126
reed9d93c2e2014-10-08 05:17:12 -0700127#include "SkMipMap.h"
128
129enum LockedState {
130 kNotLocked,
131 kLocked,
132};
133
134enum CachedState {
135 kNotInCache,
136 kInCache,
137};
138
139static void check_data(skiatest::Reporter* reporter, const SkCachedData* data,
140 int refcnt, CachedState cacheState, LockedState lockedState) {
141 REPORTER_ASSERT(reporter, data->testing_only_getRefCnt() == refcnt);
142 REPORTER_ASSERT(reporter, data->testing_only_isInCache() == (kInCache == cacheState));
143 bool isLocked = (data->data() != NULL);
144 REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked));
145}
146
147static void test_mipmapcache(skiatest::Reporter* reporter, SkResourceCache* cache) {
148 cache->purgeAll();
149
150 SkBitmap src;
151 src.allocN32Pixels(5, 5);
152 src.setImmutable();
153
154 const SkMipMap* mipmap = SkMipMapCache::FindAndRef(src, cache);
155 REPORTER_ASSERT(reporter, NULL == mipmap);
156
157 mipmap = SkMipMapCache::AddAndRef(src, cache);
158 REPORTER_ASSERT(reporter, mipmap);
159 check_data(reporter, mipmap, 2, kInCache, kLocked);
160
161 mipmap->unref();
162 // tricky, since technically after this I'm no longer an owner, but since the cache is
163 // local, I know it won't get purged behind my back
164 check_data(reporter, mipmap, 1, kInCache, kNotLocked);
165
166 // find us again
167 mipmap = SkMipMapCache::FindAndRef(src, cache);
168 check_data(reporter, mipmap, 2, kInCache, kLocked);
169
170 cache->purgeAll();
171 check_data(reporter, mipmap, 1, kNotInCache, kLocked);
172
173 mipmap->unref();
174}
175
danakj790ffe32014-09-11 10:49:52 -0700176DEF_TEST(BitmapCache_discarded_bitmap, reporter) {
reed30ad5302014-09-16 10:39:55 -0700177 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardableFactory();
178 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator();
179
180 SkAutoTDelete<SkResourceCache> cache;
181 if (factory) {
182 cache.reset(SkNEW_ARGS(SkResourceCache, (factory)));
183 } else {
184 const size_t byteLimit = 100 * 1024;
185 cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit)));
186 }
187 SkBitmap cachedBitmap;
188 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator);
danakj790ffe32014-09-11 10:49:52 -0700189 cachedBitmap.setImmutable();
190 cachedBitmap.unlockPixels();
191
reed30ad5302014-09-16 10:39:55 -0700192 SkBitmap bm;
193 SkIRect rect = SkIRect::MakeWH(5, 5);
194
danakj790ffe32014-09-11 10:49:52 -0700195 // Add a bitmap to the cache.
reed30ad5302014-09-16 10:39:55 -0700196 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache));
197 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
danakj790ffe32014-09-11 10:49:52 -0700198
199 // Finding more than once works fine.
reed30ad5302014-09-16 10:39:55 -0700200 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
danakj790ffe32014-09-11 10:49:52 -0700201 bm.unlockPixels();
202
203 // Drop the pixels in the bitmap.
reed30ad5302014-09-16 10:39:55 -0700204 if (factory) {
205 REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed() > 0);
206 SkGetGlobalDiscardableMemoryPool()->dumpPool();
207 REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed() == 0);
danakj790ffe32014-09-11 10:49:52 -0700208
reed30ad5302014-09-16 10:39:55 -0700209 // The bitmap is not in the cache since it has been dropped.
210 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
211 }
danakj790ffe32014-09-11 10:49:52 -0700212
reed30ad5302014-09-16 10:39:55 -0700213 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator);
danakj790ffe32014-09-11 10:49:52 -0700214 cachedBitmap.setImmutable();
215 cachedBitmap.unlockPixels();
216
217 // We can add the bitmap back to the cache and find it again.
reed30ad5302014-09-16 10:39:55 -0700218 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache));
219 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
reed9d93c2e2014-10-08 05:17:12 -0700220
221 test_mipmapcache(reporter, cache);
danakj790ffe32014-09-11 10:49:52 -0700222}