| 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 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 | 149 |
| 150 SkBitmap src; | 150 SkBitmap src; |
| 151 src.allocN32Pixels(5, 5); | 151 src.allocN32Pixels(5, 5); |
| 152 src.setImmutable(); | 152 src.setImmutable(); |
| 153 | 153 |
| 154 const SkMipMap* mipmap = SkMipMapCache::FindAndRef(src, cache); | 154 const SkMipMap* mipmap = SkMipMapCache::FindAndRef(src, cache); |
| 155 REPORTER_ASSERT(reporter, NULL == mipmap); | 155 REPORTER_ASSERT(reporter, NULL == mipmap); |
| 156 | 156 |
| 157 mipmap = SkMipMapCache::AddAndRef(src, cache); | 157 mipmap = SkMipMapCache::AddAndRef(src, cache); |
| 158 REPORTER_ASSERT(reporter, mipmap); | 158 REPORTER_ASSERT(reporter, mipmap); |
| 159 | |
| 160 { | |
| 161 const SkMipMap* mm = SkMipMapCache::FindAndRef(src, cache); | |
| 162 REPORTER_ASSERT(reporter, mm); | |
| 163 REPORTER_ASSERT(reporter, mm == mipmap); | |
| 164 mm->unref(); | |
| 165 } | |
| 166 | |
| 167 check_data(reporter, mipmap, 2, kInCache, kLocked); | 159 check_data(reporter, mipmap, 2, kInCache, kLocked); |
| 168 | 160 |
| 169 mipmap->unref(); | 161 mipmap->unref(); |
| 170 // tricky, since technically after this I'm no longer an owner, but since th
e cache is | 162 // tricky, since technically after this I'm no longer an owner, but since th
e cache is |
| 171 // local, I know it won't get purged behind my back | 163 // local, I know it won't get purged behind my back |
| 172 check_data(reporter, mipmap, 1, kInCache, kNotLocked); | 164 check_data(reporter, mipmap, 1, kInCache, kNotLocked); |
| 173 | 165 |
| 174 // find us again | 166 // find us again |
| 175 mipmap = SkMipMapCache::FindAndRef(src, cache); | 167 mipmap = SkMipMapCache::FindAndRef(src, cache); |
| 176 check_data(reporter, mipmap, 2, kInCache, kLocked); | 168 check_data(reporter, mipmap, 2, kInCache, kLocked); |
| 177 | 169 |
| 178 cache->purgeAll(); | 170 cache->purgeAll(); |
| 179 check_data(reporter, mipmap, 1, kNotInCache, kLocked); | 171 check_data(reporter, mipmap, 1, kNotInCache, kLocked); |
| 180 | 172 |
| 181 mipmap->unref(); | 173 mipmap->unref(); |
| 182 } | 174 } |
| 183 | 175 |
| 184 // In a multi-threaded run, we can't reliably assert that something is in the gl
obal cache | |
| 185 // even if we just added it, since another thread might have caused a purge, hen
ce we guard | |
| 186 // those checks with this flag (to be defined only when run locally in 1 thread)
. | |
| 187 #define ONLY_WORKS_RELIABLY_SINGLE_THREADED_SINCE_CACHE_MAY_HAVE_BEEN_PURGED | |
| 188 | |
| 189 static void test_mipmap_notify(skiatest::Reporter* reporter, SkResourceCache* ca
che) { | |
| 190 // TODO make our pixelref notification work for all caches (right now it is
only global caches) | |
| 191 cache = NULL; | |
| 192 | |
| 193 const int N = 3; | |
| 194 SkBitmap src[N]; | |
| 195 for (int i = 0; i < N; ++i) { | |
| 196 src[i].allocN32Pixels(5, 5); | |
| 197 src[i].setImmutable(); | |
| 198 SkMipMapCache::AddAndRef(src[i], cache)->unref(); | |
| 199 } | |
| 200 | |
| 201 for (int i = 0; i < N; ++i) { | |
| 202 const SkMipMap* mipmap = SkMipMapCache::FindAndRef(src[i], cache); | |
| 203 #ifdef ONLY_WORKS_RELIABLY_SINGLE_THREADED_SINCE_CACHE_MAY_HAVE_BEEN_PURGED | |
| 204 REPORTER_ASSERT(reporter, mipmap); | |
| 205 #endif | |
| 206 SkSafeUnref(mipmap); | |
| 207 | |
| 208 src[i].reset(); // delete the underlying pixelref, which *should* remove
us from the cache | |
| 209 | |
| 210 mipmap = SkMipMapCache::FindAndRef(src[i], cache); | |
| 211 REPORTER_ASSERT(reporter, !mipmap); | |
| 212 } | |
| 213 } | |
| 214 | |
| 215 static void test_bitmap_notify(skiatest::Reporter* reporter, SkResourceCache* ca
che) { | |
| 216 // TODO make our pixelref notification work for all caches (right now it is
only global caches) | |
| 217 cache = NULL; | |
| 218 | |
| 219 const SkIRect subset = SkIRect::MakeWH(5, 5); | |
| 220 const int N = 3; | |
| 221 SkBitmap src[N], dst[N]; | |
| 222 for (int i = 0; i < N; ++i) { | |
| 223 src[i].allocN32Pixels(5, 5); | |
| 224 src[i].setImmutable(); | |
| 225 dst[i].allocN32Pixels(5, 5); | |
| 226 dst[i].setImmutable(); | |
| 227 SkBitmapCache::Add(src[i].getGenerationID(), subset, dst[i], cache); | |
| 228 } | |
| 229 | |
| 230 for (int i = 0; i < N; ++i) { | |
| 231 const uint32_t genID = src[i].getGenerationID(); | |
| 232 SkBitmap result; | |
| 233 bool found = SkBitmapCache::Find(genID, subset, &result, cache); | |
| 234 #ifdef ONLY_WORKS_RELIABLY_SINGLE_THREADED_SINCE_CACHE_MAY_HAVE_BEEN_PURGED | |
| 235 REPORTER_ASSERT(reporter, found); | |
| 236 #endif | |
| 237 | |
| 238 src[i].reset(); // delete the underlying pixelref, which *should* remove
us from the cache | |
| 239 | |
| 240 found = SkBitmapCache::Find(genID, subset, &result, cache); | |
| 241 REPORTER_ASSERT(reporter, !found); | |
| 242 } | |
| 243 } | |
| 244 | |
| 245 DEF_TEST(BitmapCache_discarded_bitmap, reporter) { | 176 DEF_TEST(BitmapCache_discarded_bitmap, reporter) { |
| 246 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardabl
eFactory(); | 177 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardabl
eFactory(); |
| 247 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); | 178 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); |
| 248 | 179 |
| 249 SkAutoTDelete<SkResourceCache> cache; | 180 SkAutoTDelete<SkResourceCache> cache; |
| 250 if (factory) { | 181 if (factory) { |
| 251 cache.reset(SkNEW_ARGS(SkResourceCache, (factory))); | 182 cache.reset(SkNEW_ARGS(SkResourceCache, (factory))); |
| 252 } else { | 183 } else { |
| 253 const size_t byteLimit = 100 * 1024; | 184 const size_t byteLimit = 100 * 1024; |
| 254 cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit))); | 185 cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit))); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 281 | 212 |
| 282 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); | 213 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); |
| 283 cachedBitmap.setImmutable(); | 214 cachedBitmap.setImmutable(); |
| 284 cachedBitmap.unlockPixels(); | 215 cachedBitmap.unlockPixels(); |
| 285 | 216 |
| 286 // We can add the bitmap back to the cache and find it again. | 217 // We can add the bitmap back to the cache and find it again. |
| 287 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(),
rect, cachedBitmap, cache)); | 218 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(),
rect, cachedBitmap, cache)); |
| 288 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); | 219 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); |
| 289 | 220 |
| 290 test_mipmapcache(reporter, cache); | 221 test_mipmapcache(reporter, cache); |
| 291 test_bitmap_notify(reporter, cache); | |
| 292 test_mipmap_notify(reporter, cache); | |
| 293 } | 222 } |
| OLD | NEW |