blob: e1f8a65bbca42b7d989e3dd56f3e88d357417dc5 [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);
25 float roundedImageHeight = SkScalarRoundToScalar(orig.height() * xScale);
26 return SkBitmapCache::Find(orig, roundedImageWidth, roundedImageHeight, &scaled);
halcanary1d1795b2014-07-18 09:18:40 -070027}
28
29// Draw a scaled bitmap, then return true iff it has been cached.
30static bool test_scaled_image_cache_useage() {
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);
halcanary1d1795b2014-07-18 09:18:40 -070036 SkScalar scale = SkIntToScalar(kScale);
37 SkScalar scaledSize = SkIntToScalar(kBitmapSize) * scale;
halcanary805ef152014-07-17 06:58:01 -070038 canvas->clipRect(SkRect::MakeLTRB(0, 0, scaledSize, scaledSize));
39 SkPaint paint;
40 paint.setFilterLevel(SkPaint::kHigh_FilterLevel);
halcanary1d1795b2014-07-18 09:18:40 -070041
halcanary805ef152014-07-17 06:58:01 -070042 canvas->drawBitmapRect(bitmap,
43 SkRect::MakeLTRB(0, 0, scaledSize, scaledSize),
44 &paint);
halcanary1d1795b2014-07-18 09:18:40 -070045
46 return is_in_scaled_image_cache(bitmap, scale, scale);
halcanary805ef152014-07-17 06:58:01 -070047}
48
49// http://crbug.com/389439
reed011f39a2014-08-28 13:35:23 -070050DEF_TEST(ResourceCache_SingleAllocationByteLimit, reporter) {
51 size_t originalByteLimit = SkGraphics::GetResourceCacheTotalByteLimit();
halcanary805ef152014-07-17 06:58:01 -070052 size_t originalAllocationLimit =
reed011f39a2014-08-28 13:35:23 -070053 SkGraphics::GetResourceCacheSingleAllocationByteLimit();
halcanary805ef152014-07-17 06:58:01 -070054
55 size_t size = kBitmapSize * kScale * kBitmapSize * kScale
56 * SkColorTypeBytesPerPixel(kN32_SkColorType);
57
reed011f39a2014-08-28 13:35:23 -070058 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache
59 SkGraphics::SetResourceCacheTotalByteLimit(2 * size);
60 SkGraphics::SetResourceCacheSingleAllocationByteLimit(0); // No limit
halcanary805ef152014-07-17 06:58:01 -070061
halcanary1d1795b2014-07-18 09:18:40 -070062 REPORTER_ASSERT(reporter, test_scaled_image_cache_useage());
halcanary805ef152014-07-17 06:58:01 -070063
reed011f39a2014-08-28 13:35:23 -070064 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache
65 SkGraphics::SetResourceCacheTotalByteLimit(2 * size);
66 SkGraphics::SetResourceCacheSingleAllocationByteLimit(size * 2); // big enough
halcanary805ef152014-07-17 06:58:01 -070067
halcanary1d1795b2014-07-18 09:18:40 -070068 REPORTER_ASSERT(reporter, test_scaled_image_cache_useage());
halcanary805ef152014-07-17 06:58:01 -070069
reed011f39a2014-08-28 13:35:23 -070070 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache
71 SkGraphics::SetResourceCacheTotalByteLimit(2 * size);
72 SkGraphics::SetResourceCacheSingleAllocationByteLimit(size / 2); // too small
halcanary805ef152014-07-17 06:58:01 -070073
halcanary1d1795b2014-07-18 09:18:40 -070074 REPORTER_ASSERT(reporter, !test_scaled_image_cache_useage());
halcanary805ef152014-07-17 06:58:01 -070075
reed011f39a2014-08-28 13:35:23 -070076 SkGraphics::SetResourceCacheSingleAllocationByteLimit(originalAllocationLimit);
77 SkGraphics::SetResourceCacheTotalByteLimit(originalByteLimit);
halcanary805ef152014-07-17 06:58:01 -070078}
piotaixr42b0dfe2014-09-03 11:33:13 -070079
reed30ad5302014-09-16 10:39:55 -070080////////////////////////////////////////////////////////////////////////////////////////
mtklein26abcf12014-09-04 10:50:53 -070081
reed30ad5302014-09-16 10:39:55 -070082static void make_bitmap(SkBitmap* bitmap, const SkImageInfo& info, SkBitmap::Allocator* allocator) {
bsalomon49f085d2014-09-05 13:34:00 -070083 if (allocator) {
reed30ad5302014-09-16 10:39:55 -070084 bitmap->setInfo(info);
85 allocator->allocPixelRef(bitmap, 0);
piotaixr42b0dfe2014-09-03 11:33:13 -070086 } else {
reed30ad5302014-09-16 10:39:55 -070087 bitmap->allocPixels(info);
piotaixr42b0dfe2014-09-03 11:33:13 -070088 }
piotaixr42b0dfe2014-09-03 11:33:13 -070089}
90
91// http://skbug.com/2894
92DEF_TEST(BitmapCache_add_rect, reporter) {
reed30ad5302014-09-16 10:39:55 -070093 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardableFactory();
94 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator();
95
96 SkAutoTDelete<SkResourceCache> cache;
97 if (factory) {
98 cache.reset(SkNEW_ARGS(SkResourceCache, (factory)));
99 } else {
100 const size_t byteLimit = 100 * 1024;
101 cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit)));
102 }
103 SkBitmap cachedBitmap;
104 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator);
piotaixr42b0dfe2014-09-03 11:33:13 -0700105 cachedBitmap.setImmutable();
106
reed30ad5302014-09-16 10:39:55 -0700107 SkBitmap bm;
108 SkIRect rect = SkIRect::MakeWH(5, 5);
109
piotaixr42b0dfe2014-09-03 11:33:13 -0700110 // Wrong subset size
reed30ad5302014-09-16 10:39:55 -0700111 REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedBitmap.getGenerationID(), SkIRect::MakeWH(4, 6), cachedBitmap, cache));
112 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
piotaixr42b0dfe2014-09-03 11:33:13 -0700113 // Wrong offset value
reed30ad5302014-09-16 10:39:55 -0700114 REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedBitmap.getGenerationID(), SkIRect::MakeXYWH(-1, 0, 5, 5), cachedBitmap, cache));
115 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
piotaixr42b0dfe2014-09-03 11:33:13 -0700116
117 // Should not be in the cache
reed30ad5302014-09-16 10:39:55 -0700118 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
piotaixr42b0dfe2014-09-03 11:33:13 -0700119
reed30ad5302014-09-16 10:39:55 -0700120 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache));
piotaixr42b0dfe2014-09-03 11:33:13 -0700121 // Should be in the cache, we just added it
reed30ad5302014-09-16 10:39:55 -0700122 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
piotaixr42b0dfe2014-09-03 11:33:13 -0700123}
danakj790ffe32014-09-11 10:49:52 -0700124
reed9d93c2e2014-10-08 05:17:12 -0700125#include "SkMipMap.h"
126
127enum LockedState {
128 kNotLocked,
129 kLocked,
130};
131
132enum CachedState {
133 kNotInCache,
134 kInCache,
135};
136
137static void check_data(skiatest::Reporter* reporter, const SkCachedData* data,
138 int refcnt, CachedState cacheState, LockedState lockedState) {
139 REPORTER_ASSERT(reporter, data->testing_only_getRefCnt() == refcnt);
140 REPORTER_ASSERT(reporter, data->testing_only_isInCache() == (kInCache == cacheState));
141 bool isLocked = (data->data() != NULL);
142 REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked));
143}
144
145static void test_mipmapcache(skiatest::Reporter* reporter, SkResourceCache* cache) {
146 cache->purgeAll();
147
148 SkBitmap src;
149 src.allocN32Pixels(5, 5);
150 src.setImmutable();
151
152 const SkMipMap* mipmap = SkMipMapCache::FindAndRef(src, cache);
153 REPORTER_ASSERT(reporter, NULL == mipmap);
154
155 mipmap = SkMipMapCache::AddAndRef(src, cache);
156 REPORTER_ASSERT(reporter, mipmap);
157 check_data(reporter, mipmap, 2, kInCache, kLocked);
158
159 mipmap->unref();
160 // tricky, since technically after this I'm no longer an owner, but since the cache is
161 // local, I know it won't get purged behind my back
162 check_data(reporter, mipmap, 1, kInCache, kNotLocked);
163
164 // find us again
165 mipmap = SkMipMapCache::FindAndRef(src, cache);
166 check_data(reporter, mipmap, 2, kInCache, kLocked);
167
168 cache->purgeAll();
169 check_data(reporter, mipmap, 1, kNotInCache, kLocked);
170
171 mipmap->unref();
172}
173
danakj790ffe32014-09-11 10:49:52 -0700174DEF_TEST(BitmapCache_discarded_bitmap, reporter) {
reed30ad5302014-09-16 10:39:55 -0700175 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardableFactory();
176 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator();
177
178 SkAutoTDelete<SkResourceCache> cache;
179 if (factory) {
180 cache.reset(SkNEW_ARGS(SkResourceCache, (factory)));
181 } else {
182 const size_t byteLimit = 100 * 1024;
183 cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit)));
184 }
185 SkBitmap cachedBitmap;
186 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator);
danakj790ffe32014-09-11 10:49:52 -0700187 cachedBitmap.setImmutable();
188 cachedBitmap.unlockPixels();
189
reed30ad5302014-09-16 10:39:55 -0700190 SkBitmap bm;
191 SkIRect rect = SkIRect::MakeWH(5, 5);
192
danakj790ffe32014-09-11 10:49:52 -0700193 // Add a bitmap to the cache.
reed30ad5302014-09-16 10:39:55 -0700194 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache));
195 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
danakj790ffe32014-09-11 10:49:52 -0700196
197 // Finding more than once works fine.
reed30ad5302014-09-16 10:39:55 -0700198 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
danakj790ffe32014-09-11 10:49:52 -0700199 bm.unlockPixels();
200
201 // Drop the pixels in the bitmap.
reed30ad5302014-09-16 10:39:55 -0700202 if (factory) {
203 REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed() > 0);
204 SkGetGlobalDiscardableMemoryPool()->dumpPool();
205 REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed() == 0);
danakj790ffe32014-09-11 10:49:52 -0700206
reed30ad5302014-09-16 10:39:55 -0700207 // The bitmap is not in the cache since it has been dropped.
208 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
209 }
danakj790ffe32014-09-11 10:49:52 -0700210
reed30ad5302014-09-16 10:39:55 -0700211 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator);
danakj790ffe32014-09-11 10:49:52 -0700212 cachedBitmap.setImmutable();
213 cachedBitmap.unlockPixels();
214
215 // We can add the bitmap back to the cache and find it again.
reed30ad5302014-09-16 10:39:55 -0700216 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache));
217 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
reed9d93c2e2014-10-08 05:17:12 -0700218
219 test_mipmapcache(reporter, cache);
danakj790ffe32014-09-11 10:49:52 -0700220}