| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "Test.h" | 8 #include "Test.h" |
| 9 #include "SkBitmapCache.h" | 9 #include "SkBitmapCache.h" |
| 10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
| 11 #include "SkDiscardableMemoryPool.h" | 11 #include "SkDiscardableMemoryPool.h" |
| 12 #include "SkGraphics.h" | 12 #include "SkGraphics.h" |
| 13 #include "SkResourceCache.h" | 13 #include "SkResourceCache.h" |
| 14 #include "SkSurface.h" | 14 #include "SkSurface.h" |
| 15 | 15 |
| 16 static const int kCanvasSize = 1; | 16 static const int kCanvasSize = 1; |
| 17 static const int kBitmapSize = 16; | 17 static const int kBitmapSize = 16; |
| 18 static const int kScale = 8; | 18 static const int kScale = 8; |
| 19 | 19 |
| 20 static bool is_in_scaled_image_cache(const SkBitmap& orig, | 20 static bool is_in_scaled_image_cache(const SkBitmap& orig, |
| 21 SkScalar xScale, | 21 SkScalar xScale, |
| 22 SkScalar yScale) { | 22 SkScalar yScale) { |
| 23 SkBitmap scaled; | 23 SkBitmap scaled; |
| 24 int width = SkScalarRoundToInt(orig.width() * xScale); | 24 int width = SkScalarRoundToInt(orig.width() * xScale); |
| 25 int height = SkScalarRoundToInt(orig.height() * yScale); | 25 int height = SkScalarRoundToInt(orig.height() * yScale); |
| 26 return SkBitmapCache::FindWH(orig, width, height, &scaled); | 26 return SkBitmapCache::FindWH(SkBitmapCacheDesc::Make(orig, width, height), &
scaled); |
| 27 } | 27 } |
| 28 | 28 |
| 29 // Draw a scaled bitmap, then return true if it has been cached. | 29 // Draw a scaled bitmap, then return true if it has been cached. |
| 30 static bool test_scaled_image_cache_usage() { | 30 static bool test_scaled_image_cache_usage() { |
| 31 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(kCanvasSize, k
CanvasSize)); | 31 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(kCanvasSize, k
CanvasSize)); |
| 32 SkCanvas* canvas = surface->getCanvas(); | 32 SkCanvas* canvas = surface->getCanvas(); |
| 33 SkBitmap bitmap; | 33 SkBitmap bitmap; |
| 34 bitmap.allocN32Pixels(kBitmapSize, kBitmapSize); | 34 bitmap.allocN32Pixels(kBitmapSize, kBitmapSize); |
| 35 bitmap.eraseColor(0xFFFFFFFF); | 35 bitmap.eraseColor(0xFFFFFFFF); |
| 36 SkScalar xScale = SkIntToScalar(kScale); | 36 SkScalar xScale = SkIntToScalar(kScale); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked)); | 146 REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked)); |
| 147 } | 147 } |
| 148 | 148 |
| 149 static void test_mipmapcache(skiatest::Reporter* reporter, SkResourceCache* cach
e) { | 149 static void test_mipmapcache(skiatest::Reporter* reporter, SkResourceCache* cach
e) { |
| 150 cache->purgeAll(); | 150 cache->purgeAll(); |
| 151 | 151 |
| 152 SkBitmap src; | 152 SkBitmap src; |
| 153 src.allocN32Pixels(5, 5); | 153 src.allocN32Pixels(5, 5); |
| 154 src.setImmutable(); | 154 src.setImmutable(); |
| 155 | 155 |
| 156 const SkMipMap* mipmap = SkMipMapCache::FindAndRef(src, cache); | 156 const SkMipMap* mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(s
rc), cache); |
| 157 REPORTER_ASSERT(reporter, nullptr == mipmap); | 157 REPORTER_ASSERT(reporter, nullptr == mipmap); |
| 158 | 158 |
| 159 mipmap = SkMipMapCache::AddAndRef(src, cache); | 159 mipmap = SkMipMapCache::AddAndRef(src, cache); |
| 160 REPORTER_ASSERT(reporter, mipmap); | 160 REPORTER_ASSERT(reporter, mipmap); |
| 161 | 161 |
| 162 { | 162 { |
| 163 const SkMipMap* mm = SkMipMapCache::FindAndRef(src, cache); | 163 const SkMipMap* mm = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(s
rc), cache); |
| 164 REPORTER_ASSERT(reporter, mm); | 164 REPORTER_ASSERT(reporter, mm); |
| 165 REPORTER_ASSERT(reporter, mm == mipmap); | 165 REPORTER_ASSERT(reporter, mm == mipmap); |
| 166 mm->unref(); | 166 mm->unref(); |
| 167 } | 167 } |
| 168 | 168 |
| 169 check_data(reporter, mipmap, 2, kInCache, kLocked); | 169 check_data(reporter, mipmap, 2, kInCache, kLocked); |
| 170 | 170 |
| 171 mipmap->unref(); | 171 mipmap->unref(); |
| 172 // tricky, since technically after this I'm no longer an owner, but since th
e cache is | 172 // tricky, since technically after this I'm no longer an owner, but since th
e cache is |
| 173 // local, I know it won't get purged behind my back | 173 // local, I know it won't get purged behind my back |
| 174 check_data(reporter, mipmap, 1, kInCache, kNotLocked); | 174 check_data(reporter, mipmap, 1, kInCache, kNotLocked); |
| 175 | 175 |
| 176 // find us again | 176 // find us again |
| 177 mipmap = SkMipMapCache::FindAndRef(src, cache); | 177 mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src), cache); |
| 178 check_data(reporter, mipmap, 2, kInCache, kLocked); | 178 check_data(reporter, mipmap, 2, kInCache, kLocked); |
| 179 | 179 |
| 180 cache->purgeAll(); | 180 cache->purgeAll(); |
| 181 check_data(reporter, mipmap, 1, kNotInCache, kLocked); | 181 check_data(reporter, mipmap, 1, kNotInCache, kLocked); |
| 182 | 182 |
| 183 mipmap->unref(); | 183 mipmap->unref(); |
| 184 } | 184 } |
| 185 | 185 |
| 186 static void test_mipmap_notify(skiatest::Reporter* reporter, SkResourceCache* ca
che) { | 186 static void test_mipmap_notify(skiatest::Reporter* reporter, SkResourceCache* ca
che) { |
| 187 const int N = 3; | 187 const int N = 3; |
| 188 SkBitmap src[N]; | 188 SkBitmap src[N]; |
| 189 for (int i = 0; i < N; ++i) { | 189 for (int i = 0; i < N; ++i) { |
| 190 src[i].allocN32Pixels(5, 5); | 190 src[i].allocN32Pixels(5, 5); |
| 191 src[i].setImmutable(); | 191 src[i].setImmutable(); |
| 192 SkMipMapCache::AddAndRef(src[i], cache)->unref(); | 192 SkMipMapCache::AddAndRef(src[i], cache)->unref(); |
| 193 } | 193 } |
| 194 | 194 |
| 195 for (int i = 0; i < N; ++i) { | 195 for (int i = 0; i < N; ++i) { |
| 196 const SkMipMap* mipmap = SkMipMapCache::FindAndRef(src[i], cache); | 196 const SkMipMap* mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Ma
ke(src[i]), cache); |
| 197 if (cache) { | 197 if (cache) { |
| 198 // if cache is null, we're working on the global cache, and other th
reads might purge | 198 // if cache is null, we're working on the global cache, and other th
reads might purge |
| 199 // it, making this check fragile. | 199 // it, making this check fragile. |
| 200 REPORTER_ASSERT(reporter, mipmap); | 200 REPORTER_ASSERT(reporter, mipmap); |
| 201 } | 201 } |
| 202 SkSafeUnref(mipmap); | 202 SkSafeUnref(mipmap); |
| 203 | 203 |
| 204 src[i].reset(); // delete the underlying pixelref, which *should* remove
us from the cache | 204 src[i].reset(); // delete the underlying pixelref, which *should* remove
us from the cache |
| 205 | 205 |
| 206 mipmap = SkMipMapCache::FindAndRef(src[i], cache); | 206 mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src[i]), cach
e); |
| 207 REPORTER_ASSERT(reporter, !mipmap); | 207 REPORTER_ASSERT(reporter, !mipmap); |
| 208 } | 208 } |
| 209 } | 209 } |
| 210 | 210 |
| 211 static void test_bitmap_notify(skiatest::Reporter* reporter, SkResourceCache* ca
che) { | 211 static void test_bitmap_notify(skiatest::Reporter* reporter, SkResourceCache* ca
che) { |
| 212 const SkIRect subset = SkIRect::MakeWH(5, 5); | 212 const SkIRect subset = SkIRect::MakeWH(5, 5); |
| 213 const int N = 3; | 213 const int N = 3; |
| 214 SkBitmap src[N], dst[N]; | 214 SkBitmap src[N], dst[N]; |
| 215 for (int i = 0; i < N; ++i) { | 215 for (int i = 0; i < N; ++i) { |
| 216 src[i].allocN32Pixels(5, 5); | 216 src[i].allocN32Pixels(5, 5); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 cachedBitmap.unlockPixels(); | 279 cachedBitmap.unlockPixels(); |
| 280 | 280 |
| 281 // We can add the bitmap back to the cache and find it again. | 281 // We can add the bitmap back to the cache and find it again. |
| 282 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), rect,
cachedBitmap, cache)); | 282 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), rect,
cachedBitmap, cache)); |
| 283 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); | 283 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); |
| 284 | 284 |
| 285 test_mipmapcache(reporter, cache); | 285 test_mipmapcache(reporter, cache); |
| 286 test_bitmap_notify(reporter, cache); | 286 test_bitmap_notify(reporter, cache); |
| 287 test_mipmap_notify(reporter, cache); | 287 test_mipmap_notify(reporter, cache); |
| 288 } | 288 } |
| OLD | NEW |