| 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" |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 } else { | 101 } else { |
| 102 const size_t byteLimit = 100 * 1024; | 102 const size_t byteLimit = 100 * 1024; |
| 103 cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit))); | 103 cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit))); |
| 104 } | 104 } |
| 105 SkBitmap cachedBitmap; | 105 SkBitmap cachedBitmap; |
| 106 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); | 106 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); |
| 107 cachedBitmap.setImmutable(); | 107 cachedBitmap.setImmutable(); |
| 108 | 108 |
| 109 SkBitmap bm; | 109 SkBitmap bm; |
| 110 SkIRect rect = SkIRect::MakeWH(5, 5); | 110 SkIRect rect = SkIRect::MakeWH(5, 5); |
| 111 uint32_t cachedID = cachedBitmap.getGenerationID(); |
| 112 SkPixelRef* cachedPR = cachedBitmap.pixelRef(); |
| 111 | 113 |
| 112 // Wrong subset size | 114 // Wrong subset size |
| 113 REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedBitmap.getGenerationID()
, SkIRect::MakeWH(4, 6), cachedBitmap, cache)); | 115 REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedPR, SkIRect::MakeWH(4, 6
), cachedBitmap, cache)); |
| 114 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(
), rect, &bm, cache)); | 116 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedID, rect, &bm, cache)); |
| 115 // Wrong offset value | 117 // Wrong offset value |
| 116 REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedBitmap.getGenerationID()
, SkIRect::MakeXYWH(-1, 0, 5, 5), cachedBitmap, cache)); | 118 REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedPR, SkIRect::MakeXYWH(-1
, 0, 5, 5), cachedBitmap, cache)); |
| 117 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(
), rect, &bm, cache)); | 119 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedID, rect, &bm, cache)); |
| 118 | 120 |
| 119 // Should not be in the cache | 121 // Should not be in the cache |
| 120 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(
), rect, &bm, cache)); | 122 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedID, rect, &bm, cache)); |
| 121 | 123 |
| 122 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(),
rect, cachedBitmap, cache)); | 124 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedPR, rect, cachedBitmap, c
ache)); |
| 123 // Should be in the cache, we just added it | 125 // Should be in the cache, we just added it |
| 124 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); | 126 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedID, rect, &bm, cache)); |
| 125 } | 127 } |
| 126 | 128 |
| 127 #include "SkMipMap.h" | 129 #include "SkMipMap.h" |
| 128 | 130 |
| 129 enum LockedState { | 131 enum LockedState { |
| 130 kNotLocked, | 132 kNotLocked, |
| 131 kLocked, | 133 kLocked, |
| 132 }; | 134 }; |
| 133 | 135 |
| 134 enum CachedState { | 136 enum CachedState { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 | 210 |
| 209 static void test_bitmap_notify(skiatest::Reporter* reporter, SkResourceCache* ca
che) { | 211 static void test_bitmap_notify(skiatest::Reporter* reporter, SkResourceCache* ca
che) { |
| 210 const SkIRect subset = SkIRect::MakeWH(5, 5); | 212 const SkIRect subset = SkIRect::MakeWH(5, 5); |
| 211 const int N = 3; | 213 const int N = 3; |
| 212 SkBitmap src[N], dst[N]; | 214 SkBitmap src[N], dst[N]; |
| 213 for (int i = 0; i < N; ++i) { | 215 for (int i = 0; i < N; ++i) { |
| 214 src[i].allocN32Pixels(5, 5); | 216 src[i].allocN32Pixels(5, 5); |
| 215 src[i].setImmutable(); | 217 src[i].setImmutable(); |
| 216 dst[i].allocN32Pixels(5, 5); | 218 dst[i].allocN32Pixels(5, 5); |
| 217 dst[i].setImmutable(); | 219 dst[i].setImmutable(); |
| 218 SkBitmapCache::Add(src[i].getGenerationID(), subset, dst[i], cache); | 220 SkBitmapCache::Add(src[i].pixelRef(), subset, dst[i], cache); |
| 219 } | 221 } |
| 220 | 222 |
| 221 for (int i = 0; i < N; ++i) { | 223 for (int i = 0; i < N; ++i) { |
| 222 const uint32_t genID = src[i].getGenerationID(); | 224 const uint32_t genID = src[i].getGenerationID(); |
| 223 SkBitmap result; | 225 SkBitmap result; |
| 224 bool found = SkBitmapCache::Find(genID, subset, &result, cache); | 226 bool found = SkBitmapCache::Find(genID, subset, &result, cache); |
| 225 if (cache) { | 227 if (cache) { |
| 226 // if cache is null, we're working on the global cache, and other th
reads might purge | 228 // if cache is null, we're working on the global cache, and other th
reads might purge |
| 227 // it, making this check fragile. | 229 // it, making this check fragile. |
| 228 REPORTER_ASSERT(reporter, found); | 230 REPORTER_ASSERT(reporter, found); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 248 } | 250 } |
| 249 SkBitmap cachedBitmap; | 251 SkBitmap cachedBitmap; |
| 250 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); | 252 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); |
| 251 cachedBitmap.setImmutable(); | 253 cachedBitmap.setImmutable(); |
| 252 cachedBitmap.unlockPixels(); | 254 cachedBitmap.unlockPixels(); |
| 253 | 255 |
| 254 SkBitmap bm; | 256 SkBitmap bm; |
| 255 SkIRect rect = SkIRect::MakeWH(5, 5); | 257 SkIRect rect = SkIRect::MakeWH(5, 5); |
| 256 | 258 |
| 257 // Add a bitmap to the cache. | 259 // Add a bitmap to the cache. |
| 258 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(),
rect, cachedBitmap, cache)); | 260 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), rect,
cachedBitmap, cache)); |
| 259 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); | 261 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); |
| 260 | 262 |
| 261 // Finding more than once works fine. | 263 // Finding more than once works fine. |
| 262 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); | 264 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); |
| 263 bm.unlockPixels(); | 265 bm.unlockPixels(); |
| 264 | 266 |
| 265 // Drop the pixels in the bitmap. | 267 // Drop the pixels in the bitmap. |
| 266 if (factory) { | 268 if (factory) { |
| 267 REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed
() > 0); | 269 REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed
() > 0); |
| 268 SkGetGlobalDiscardableMemoryPool()->dumpPool(); | 270 SkGetGlobalDiscardableMemoryPool()->dumpPool(); |
| 269 REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed
() == 0); | 271 REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed
() == 0); |
| 270 | 272 |
| 271 // The bitmap is not in the cache since it has been dropped. | 273 // The bitmap is not in the cache since it has been dropped. |
| 272 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGeneratio
nID(), rect, &bm, cache)); | 274 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGeneratio
nID(), rect, &bm, cache)); |
| 273 } | 275 } |
| 274 | 276 |
| 275 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); | 277 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); |
| 276 cachedBitmap.setImmutable(); | 278 cachedBitmap.setImmutable(); |
| 277 cachedBitmap.unlockPixels(); | 279 cachedBitmap.unlockPixels(); |
| 278 | 280 |
| 279 // 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. |
| 280 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(),
rect, cachedBitmap, cache)); | 282 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), rect,
cachedBitmap, cache)); |
| 281 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); | 283 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); |
| 282 | 284 |
| 283 test_mipmapcache(reporter, cache); | 285 test_mipmapcache(reporter, cache); |
| 284 test_bitmap_notify(reporter, cache); | 286 test_bitmap_notify(reporter, cache); |
| 285 test_mipmap_notify(reporter, cache); | 287 test_mipmap_notify(reporter, cache); |
| 286 } | 288 } |
| OLD | NEW |