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" |
reed | 3054be1 | 2014-12-10 07:24:28 -0800 | [diff] [blame] | 14 | #include "SkSurface.h" |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 15 | |
| 16 | static const int kCanvasSize = 1; |
| 17 | static const int kBitmapSize = 16; |
| 18 | static const int kScale = 8; |
| 19 | |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 20 | static bool is_in_scaled_image_cache(const SkBitmap& orig, |
| 21 | SkScalar xScale, |
| 22 | SkScalar yScale) { |
| 23 | SkBitmap scaled; |
humper | d73c169 | 2014-08-28 14:27:42 -0700 | [diff] [blame] | 24 | float roundedImageWidth = SkScalarRoundToScalar(orig.width() * xScale); |
| 25 | float roundedImageHeight = SkScalarRoundToScalar(orig.height() * xScale); |
| 26 | return SkBitmapCache::Find(orig, roundedImageWidth, roundedImageHeight, &scaled); |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | // Draw a scaled bitmap, then return true iff it has been cached. |
| 30 | static bool test_scaled_image_cache_useage() { |
reed | 3054be1 | 2014-12-10 07:24:28 -0800 | [diff] [blame] | 31 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(kCanvasSize, kCanvasSize)); |
| 32 | SkCanvas* canvas = surface->getCanvas(); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 33 | SkBitmap bitmap; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 34 | bitmap.allocN32Pixels(kBitmapSize, kBitmapSize); |
halcanary | 0db38cc | 2014-07-17 10:17:28 -0700 | [diff] [blame] | 35 | bitmap.eraseColor(0xFFFFFFFF); |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 36 | SkScalar scale = SkIntToScalar(kScale); |
| 37 | SkScalar scaledSize = SkIntToScalar(kBitmapSize) * scale; |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 38 | canvas->clipRect(SkRect::MakeLTRB(0, 0, scaledSize, scaledSize)); |
| 39 | SkPaint paint; |
| 40 | paint.setFilterLevel(SkPaint::kHigh_FilterLevel); |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 41 | |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 42 | canvas->drawBitmapRect(bitmap, |
| 43 | SkRect::MakeLTRB(0, 0, scaledSize, scaledSize), |
| 44 | &paint); |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 45 | |
| 46 | return is_in_scaled_image_cache(bitmap, scale, scale); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // http://crbug.com/389439 |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 50 | DEF_TEST(ResourceCache_SingleAllocationByteLimit, reporter) { |
| 51 | size_t originalByteLimit = SkGraphics::GetResourceCacheTotalByteLimit(); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 52 | size_t originalAllocationLimit = |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 53 | SkGraphics::GetResourceCacheSingleAllocationByteLimit(); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 54 | |
| 55 | size_t size = kBitmapSize * kScale * kBitmapSize * kScale |
| 56 | * SkColorTypeBytesPerPixel(kN32_SkColorType); |
| 57 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 58 | SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache |
| 59 | SkGraphics::SetResourceCacheTotalByteLimit(2 * size); |
| 60 | SkGraphics::SetResourceCacheSingleAllocationByteLimit(0); // No limit |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 61 | |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 62 | REPORTER_ASSERT(reporter, test_scaled_image_cache_useage()); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 63 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 64 | SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache |
| 65 | SkGraphics::SetResourceCacheTotalByteLimit(2 * size); |
| 66 | SkGraphics::SetResourceCacheSingleAllocationByteLimit(size * 2); // big enough |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 67 | |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 68 | REPORTER_ASSERT(reporter, test_scaled_image_cache_useage()); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 69 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 70 | SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache |
| 71 | SkGraphics::SetResourceCacheTotalByteLimit(2 * size); |
| 72 | SkGraphics::SetResourceCacheSingleAllocationByteLimit(size / 2); // too small |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 73 | |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 74 | REPORTER_ASSERT(reporter, !test_scaled_image_cache_useage()); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 75 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 76 | SkGraphics::SetResourceCacheSingleAllocationByteLimit(originalAllocationLimit); |
| 77 | SkGraphics::SetResourceCacheTotalByteLimit(originalByteLimit); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 78 | } |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 79 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 80 | //////////////////////////////////////////////////////////////////////////////////////// |
mtklein | 26abcf1 | 2014-09-04 10:50:53 -0700 | [diff] [blame] | 81 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 82 | static void make_bitmap(SkBitmap* bitmap, const SkImageInfo& info, SkBitmap::Allocator* allocator) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 83 | if (allocator) { |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 84 | bitmap->setInfo(info); |
| 85 | allocator->allocPixelRef(bitmap, 0); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 86 | } else { |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 87 | bitmap->allocPixels(info); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 88 | } |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // http://skbug.com/2894 |
| 92 | DEF_TEST(BitmapCache_add_rect, reporter) { |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 93 | 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); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 105 | cachedBitmap.setImmutable(); |
| 106 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 107 | SkBitmap bm; |
| 108 | SkIRect rect = SkIRect::MakeWH(5, 5); |
| 109 | |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 110 | // Wrong subset size |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 111 | REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedBitmap.getGenerationID(), SkIRect::MakeWH(4, 6), cachedBitmap, cache)); |
| 112 | REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache)); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 113 | // Wrong offset value |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 114 | 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)); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 116 | |
| 117 | // Should not be in the cache |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 118 | REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache)); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 119 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 120 | REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache)); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 121 | // Should be in the cache, we just added it |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 122 | REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache)); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 123 | } |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 124 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 125 | #include "SkMipMap.h" |
| 126 | |
| 127 | enum LockedState { |
| 128 | kNotLocked, |
| 129 | kLocked, |
| 130 | }; |
| 131 | |
| 132 | enum CachedState { |
| 133 | kNotInCache, |
| 134 | kInCache, |
| 135 | }; |
| 136 | |
| 137 | static 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 | |
| 145 | static 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 | |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 174 | DEF_TEST(BitmapCache_discarded_bitmap, reporter) { |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 175 | 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); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 187 | cachedBitmap.setImmutable(); |
| 188 | cachedBitmap.unlockPixels(); |
| 189 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 190 | SkBitmap bm; |
| 191 | SkIRect rect = SkIRect::MakeWH(5, 5); |
| 192 | |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 193 | // Add a bitmap to the cache. |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 194 | REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache)); |
| 195 | REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache)); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 196 | |
| 197 | // Finding more than once works fine. |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 198 | REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache)); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 199 | bm.unlockPixels(); |
| 200 | |
| 201 | // Drop the pixels in the bitmap. |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 202 | if (factory) { |
| 203 | REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed() > 0); |
| 204 | SkGetGlobalDiscardableMemoryPool()->dumpPool(); |
| 205 | REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed() == 0); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 206 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 207 | // The bitmap is not in the cache since it has been dropped. |
| 208 | REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache)); |
| 209 | } |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 210 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 211 | make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 212 | cachedBitmap.setImmutable(); |
| 213 | cachedBitmap.unlockPixels(); |
| 214 | |
| 215 | // We can add the bitmap back to the cache and find it again. |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 216 | REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache)); |
| 217 | REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache)); |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 218 | |
| 219 | test_mipmapcache(reporter, cache); |
danakj | 790ffe3 | 2014-09-11 10:49:52 -0700 | [diff] [blame] | 220 | } |