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