halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 7 | |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 8 | #include "Test.h" |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 9 | #include "SkBitmapCache.h" |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 10 | #include "SkCanvas.h" |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 11 | #include "SkDiscardableMemoryPool.h" |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 12 | #include "SkGraphics.h" |
| 13 | #include "SkResourceCache.h" |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 14 | |
| 15 | static const int kCanvasSize = 1; |
| 16 | static const int kBitmapSize = 16; |
| 17 | static const int kScale = 8; |
| 18 | |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 19 | static bool is_in_scaled_image_cache(const SkBitmap& orig, |
| 20 | SkScalar xScale, |
| 21 | SkScalar yScale) { |
| 22 | SkBitmap scaled; |
humper | d73c169 | 2014-08-28 14:27:42 -0700 | [diff] [blame] | 23 | float roundedImageWidth = SkScalarRoundToScalar(orig.width() * xScale); |
| 24 | float roundedImageHeight = SkScalarRoundToScalar(orig.height() * xScale); |
| 25 | return SkBitmapCache::Find(orig, roundedImageWidth, roundedImageHeight, &scaled); |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | // Draw a scaled bitmap, then return true iff it has been cached. |
| 29 | static bool test_scaled_image_cache_useage() { |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 30 | SkAutoTUnref<SkCanvas> canvas( |
| 31 | SkCanvas::NewRasterN32(kCanvasSize, kCanvasSize)); |
| 32 | SkBitmap bitmap; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 33 | bitmap.allocN32Pixels(kBitmapSize, kBitmapSize); |
halcanary | 0db38cc | 2014-07-17 10:17:28 -0700 | [diff] [blame] | 34 | bitmap.eraseColor(0xFFFFFFFF); |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 35 | SkScalar scale = SkIntToScalar(kScale); |
| 36 | SkScalar scaledSize = SkIntToScalar(kBitmapSize) * scale; |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 37 | canvas->clipRect(SkRect::MakeLTRB(0, 0, scaledSize, scaledSize)); |
| 38 | SkPaint paint; |
| 39 | paint.setFilterLevel(SkPaint::kHigh_FilterLevel); |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 40 | |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 41 | canvas->drawBitmapRect(bitmap, |
| 42 | SkRect::MakeLTRB(0, 0, scaledSize, scaledSize), |
| 43 | &paint); |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 44 | |
| 45 | return is_in_scaled_image_cache(bitmap, scale, scale); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // http://crbug.com/389439 |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 49 | DEF_TEST(ResourceCache_SingleAllocationByteLimit, reporter) { |
| 50 | size_t originalByteLimit = SkGraphics::GetResourceCacheTotalByteLimit(); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 51 | size_t originalAllocationLimit = |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 52 | SkGraphics::GetResourceCacheSingleAllocationByteLimit(); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 53 | |
| 54 | size_t size = kBitmapSize * kScale * kBitmapSize * kScale |
| 55 | * SkColorTypeBytesPerPixel(kN32_SkColorType); |
| 56 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 57 | SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache |
| 58 | SkGraphics::SetResourceCacheTotalByteLimit(2 * size); |
| 59 | SkGraphics::SetResourceCacheSingleAllocationByteLimit(0); // No limit |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 60 | |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 61 | REPORTER_ASSERT(reporter, test_scaled_image_cache_useage()); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 62 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 63 | SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache |
| 64 | SkGraphics::SetResourceCacheTotalByteLimit(2 * size); |
| 65 | SkGraphics::SetResourceCacheSingleAllocationByteLimit(size * 2); // big enough |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 66 | |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 67 | REPORTER_ASSERT(reporter, test_scaled_image_cache_useage()); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 68 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 69 | SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache |
| 70 | SkGraphics::SetResourceCacheTotalByteLimit(2 * size); |
| 71 | SkGraphics::SetResourceCacheSingleAllocationByteLimit(size / 2); // too small |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 72 | |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 73 | REPORTER_ASSERT(reporter, !test_scaled_image_cache_useage()); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 74 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 75 | SkGraphics::SetResourceCacheSingleAllocationByteLimit(originalAllocationLimit); |
| 76 | SkGraphics::SetResourceCacheTotalByteLimit(originalByteLimit); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 77 | } |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 78 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 79 | //////////////////////////////////////////////////////////////////////////////////////// |
mtklein | 26abcf1 | 2014-09-04 10:50:53 -0700 | [diff] [blame] | 80 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 81 | static void make_bitmap(SkBitmap* bitmap, const SkImageInfo& info, SkBitmap::Allocator* allocator) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 82 | if (allocator) { |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 83 | bitmap->setInfo(info); |
| 84 | allocator->allocPixelRef(bitmap, 0); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 85 | } else { |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 86 | bitmap->allocPixels(info); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 87 | } |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // http://skbug.com/2894 |
| 91 | DEF_TEST(BitmapCache_add_rect, reporter) { |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 92 | SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardableFactory(); |
| 93 | SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); |
| 94 | |
| 95 | SkAutoTDelete<SkResourceCache> cache; |
| 96 | if (factory) { |
| 97 | cache.reset(SkNEW_ARGS(SkResourceCache, (factory))); |
| 98 | } else { |
| 99 | const size_t byteLimit = 100 * 1024; |
| 100 | cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit))); |
| 101 | } |
| 102 | SkBitmap cachedBitmap; |
| 103 | make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 104 | cachedBitmap.setImmutable(); |
| 105 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 106 | SkBitmap bm; |
| 107 | SkIRect rect = SkIRect::MakeWH(5, 5); |
| 108 | |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 109 | // Wrong subset size |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 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)); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 112 | // Wrong offset value |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 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)); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 115 | |
| 116 | // Should not be in the cache |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 117 | REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache)); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 118 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 119 | REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache)); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 120 | // Should be in the cache, we just added it |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 121 | REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache)); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 122 | } |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 123 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 124 | #include "SkMipMap.h" |
| 125 | |
| 126 | enum LockedState { |
| 127 | kNotLocked, |
| 128 | kLocked, |
| 129 | }; |
| 130 | |
| 131 | enum CachedState { |
| 132 | kNotInCache, |
| 133 | kInCache, |
| 134 | }; |
| 135 | |
| 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)); |
| 142 | } |
| 143 | |
| 144 | static void test_mipmapcache(skiatest::Reporter* reporter, SkResourceCache* cache) { |
| 145 | cache->purgeAll(); |
| 146 | |
| 147 | SkBitmap src; |
| 148 | src.allocN32Pixels(5, 5); |
| 149 | src.setImmutable(); |
| 150 | |
| 151 | const SkMipMap* mipmap = SkMipMapCache::FindAndRef(src, cache); |
| 152 | REPORTER_ASSERT(reporter, NULL == mipmap); |
| 153 | |
| 154 | mipmap = SkMipMapCache::AddAndRef(src, cache); |
| 155 | REPORTER_ASSERT(reporter, mipmap); |
| 156 | check_data(reporter, mipmap, 2, kInCache, kLocked); |
| 157 | |
| 158 | mipmap->unref(); |
| 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); |
| 162 | |
| 163 | // find us again |
| 164 | mipmap = SkMipMapCache::FindAndRef(src, cache); |
| 165 | check_data(reporter, mipmap, 2, kInCache, kLocked); |
| 166 | |
| 167 | cache->purgeAll(); |
| 168 | check_data(reporter, mipmap, 1, kNotInCache, kLocked); |
| 169 | |
| 170 | mipmap->unref(); |
| 171 | } |
| 172 | |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 173 | DEF_TEST(BitmapCache_discarded_bitmap, reporter) { |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 174 | SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardableFactory(); |
| 175 | SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); |
| 176 | |
| 177 | SkAutoTDelete<SkResourceCache> cache; |
| 178 | if (factory) { |
| 179 | cache.reset(SkNEW_ARGS(SkResourceCache, (factory))); |
| 180 | } else { |
| 181 | const size_t byteLimit = 100 * 1024; |
| 182 | cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit))); |
| 183 | } |
| 184 | SkBitmap cachedBitmap; |
| 185 | make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 186 | cachedBitmap.setImmutable(); |
| 187 | cachedBitmap.unlockPixels(); |
| 188 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 189 | SkBitmap bm; |
| 190 | SkIRect rect = SkIRect::MakeWH(5, 5); |
| 191 | |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 192 | // Add a bitmap to the cache. |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 193 | REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache)); |
| 194 | REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache)); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 195 | |
| 196 | // Finding more than once works fine. |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 197 | REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache)); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 198 | bm.unlockPixels(); |
| 199 | |
| 200 | // Drop the pixels in the bitmap. |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 201 | if (factory) { |
| 202 | REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed() > 0); |
| 203 | SkGetGlobalDiscardableMemoryPool()->dumpPool(); |
| 204 | REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed() == 0); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 205 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 206 | // The bitmap is not in the cache since it has been dropped. |
| 207 | REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache)); |
| 208 | } |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 209 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 210 | make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 211 | cachedBitmap.setImmutable(); |
| 212 | cachedBitmap.unlockPixels(); |
| 213 | |
| 214 | // We can add the bitmap back to the cache and find it again. |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 215 | REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache)); |
| 216 | REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache)); |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 217 | |
| 218 | test_mipmapcache(reporter, cache); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 219 | } |