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 #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 |
176 DEF_TEST(BitmapCache_discarded_bitmap, reporter) { | 245 DEF_TEST(BitmapCache_discarded_bitmap, reporter) { |
177 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardabl
eFactory(); | 246 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardabl
eFactory(); |
178 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); | 247 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); |
179 | 248 |
180 SkAutoTDelete<SkResourceCache> cache; | 249 SkAutoTDelete<SkResourceCache> cache; |
181 if (factory) { | 250 if (factory) { |
182 cache.reset(SkNEW_ARGS(SkResourceCache, (factory))); | 251 cache.reset(SkNEW_ARGS(SkResourceCache, (factory))); |
183 } else { | 252 } else { |
184 const size_t byteLimit = 100 * 1024; | 253 const size_t byteLimit = 100 * 1024; |
185 cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit))); | 254 cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit))); |
(...skipping 26 matching lines...) Expand all Loading... |
212 | 281 |
213 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); | 282 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); |
214 cachedBitmap.setImmutable(); | 283 cachedBitmap.setImmutable(); |
215 cachedBitmap.unlockPixels(); | 284 cachedBitmap.unlockPixels(); |
216 | 285 |
217 // We can add the bitmap back to the cache and find it again. | 286 // We can add the bitmap back to the cache and find it again. |
218 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(),
rect, cachedBitmap, cache)); | 287 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(),
rect, cachedBitmap, cache)); |
219 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); | 288 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); |
220 | 289 |
221 test_mipmapcache(reporter, cache); | 290 test_mipmapcache(reporter, cache); |
| 291 test_bitmap_notify(reporter, cache); |
| 292 test_mipmap_notify(reporter, cache); |
222 } | 293 } |
OLD | NEW |