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 |
159 check_data(reporter, mipmap, 2, kInCache, kLocked); | 167 check_data(reporter, mipmap, 2, kInCache, kLocked); |
160 | 168 |
161 mipmap->unref(); | 169 mipmap->unref(); |
162 // tricky, since technically after this I'm no longer an owner, but since th
e cache is | 170 // tricky, since technically after this I'm no longer an owner, but since th
e cache is |
163 // local, I know it won't get purged behind my back | 171 // local, I know it won't get purged behind my back |
164 check_data(reporter, mipmap, 1, kInCache, kNotLocked); | 172 check_data(reporter, mipmap, 1, kInCache, kNotLocked); |
165 | 173 |
166 // find us again | 174 // find us again |
167 mipmap = SkMipMapCache::FindAndRef(src, cache); | 175 mipmap = SkMipMapCache::FindAndRef(src, cache); |
168 check_data(reporter, mipmap, 2, kInCache, kLocked); | 176 check_data(reporter, mipmap, 2, kInCache, kLocked); |
169 | 177 |
170 cache->purgeAll(); | 178 cache->purgeAll(); |
171 check_data(reporter, mipmap, 1, kNotInCache, kLocked); | 179 check_data(reporter, mipmap, 1, kNotInCache, kLocked); |
172 | 180 |
173 mipmap->unref(); | 181 mipmap->unref(); |
174 } | 182 } |
175 | 183 |
| 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 // |
| 188 // Basically, only define this locally if you're testing single-threaded. |
| 189 //#define ONLY_WORKS_RELIABLY_SINGLE_THREADED_SINCE_CACHE_MAY_HAVE_BEEN_PURGED |
| 190 |
| 191 static void test_mipmap_notify(skiatest::Reporter* reporter, SkResourceCache* ca
che) { |
| 192 const int N = 3; |
| 193 SkBitmap src[N]; |
| 194 for (int i = 0; i < N; ++i) { |
| 195 src[i].allocN32Pixels(5, 5); |
| 196 src[i].setImmutable(); |
| 197 SkMipMapCache::AddAndRef(src[i], cache)->unref(); |
| 198 } |
| 199 |
| 200 for (int i = 0; i < N; ++i) { |
| 201 const SkMipMap* mipmap = SkMipMapCache::FindAndRef(src[i], cache); |
| 202 #ifdef ONLY_WORKS_RELIABLY_SINGLE_THREADED_SINCE_CACHE_MAY_HAVE_BEEN_PURGED |
| 203 REPORTER_ASSERT(reporter, mipmap); |
| 204 #endif |
| 205 SkSafeUnref(mipmap); |
| 206 |
| 207 src[i].reset(); // delete the underlying pixelref, which *should* remove
us from the cache |
| 208 |
| 209 mipmap = SkMipMapCache::FindAndRef(src[i], cache); |
| 210 REPORTER_ASSERT(reporter, !mipmap); |
| 211 } |
| 212 } |
| 213 |
| 214 static void test_bitmap_notify(skiatest::Reporter* reporter, SkResourceCache* ca
che) { |
| 215 const SkIRect subset = SkIRect::MakeWH(5, 5); |
| 216 const int N = 3; |
| 217 SkBitmap src[N], dst[N]; |
| 218 for (int i = 0; i < N; ++i) { |
| 219 src[i].allocN32Pixels(5, 5); |
| 220 src[i].setImmutable(); |
| 221 dst[i].allocN32Pixels(5, 5); |
| 222 dst[i].setImmutable(); |
| 223 SkBitmapCache::Add(src[i].getGenerationID(), subset, dst[i], cache); |
| 224 } |
| 225 |
| 226 for (int i = 0; i < N; ++i) { |
| 227 const uint32_t genID = src[i].getGenerationID(); |
| 228 SkBitmap result; |
| 229 bool found = SkBitmapCache::Find(genID, subset, &result, cache); |
| 230 #ifdef ONLY_WORKS_RELIABLY_SINGLE_THREADED_SINCE_CACHE_MAY_HAVE_BEEN_PURGED |
| 231 REPORTER_ASSERT(reporter, found); |
| 232 #endif |
| 233 |
| 234 src[i].reset(); // delete the underlying pixelref, which *should* remove
us from the cache |
| 235 |
| 236 found = SkBitmapCache::Find(genID, subset, &result, cache); |
| 237 REPORTER_ASSERT(reporter, !found); |
| 238 } |
| 239 } |
| 240 |
176 DEF_TEST(BitmapCache_discarded_bitmap, reporter) { | 241 DEF_TEST(BitmapCache_discarded_bitmap, reporter) { |
177 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardabl
eFactory(); | 242 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardabl
eFactory(); |
178 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); | 243 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); |
179 | 244 |
180 SkAutoTDelete<SkResourceCache> cache; | 245 SkAutoTDelete<SkResourceCache> cache; |
181 if (factory) { | 246 if (factory) { |
182 cache.reset(SkNEW_ARGS(SkResourceCache, (factory))); | 247 cache.reset(SkNEW_ARGS(SkResourceCache, (factory))); |
183 } else { | 248 } else { |
184 const size_t byteLimit = 100 * 1024; | 249 const size_t byteLimit = 100 * 1024; |
185 cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit))); | 250 cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit))); |
(...skipping 26 matching lines...) Expand all Loading... |
212 | 277 |
213 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); | 278 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); |
214 cachedBitmap.setImmutable(); | 279 cachedBitmap.setImmutable(); |
215 cachedBitmap.unlockPixels(); | 280 cachedBitmap.unlockPixels(); |
216 | 281 |
217 // We can add the bitmap back to the cache and find it again. | 282 // We can add the bitmap back to the cache and find it again. |
218 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(),
rect, cachedBitmap, cache)); | 283 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(),
rect, cachedBitmap, cache)); |
219 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); | 284 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); |
220 | 285 |
221 test_mipmapcache(reporter, cache); | 286 test_mipmapcache(reporter, cache); |
| 287 test_bitmap_notify(reporter, cache); |
| 288 test_mipmap_notify(reporter, cache); |
222 } | 289 } |
OLD | NEW |