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 static void test_mipmap_notify(skiatest::Reporter* reporter, SkResourceCache* ca
che) { |
| 185 const int N = 3; |
| 186 SkBitmap src[N]; |
| 187 for (int i = 0; i < N; ++i) { |
| 188 src[i].allocN32Pixels(5, 5); |
| 189 src[i].setImmutable(); |
| 190 SkMipMapCache::AddAndRef(src[i], cache)->unref(); |
| 191 } |
| 192 |
| 193 for (int i = 0; i < N; ++i) { |
| 194 const SkMipMap* mipmap = SkMipMapCache::FindAndRef(src[i], cache); |
| 195 if (cache) { |
| 196 // if cache is null, we're working on the global cache, and other th
reads might purge |
| 197 // it, making this check fragile. |
| 198 REPORTER_ASSERT(reporter, mipmap); |
| 199 } |
| 200 SkSafeUnref(mipmap); |
| 201 |
| 202 src[i].reset(); // delete the underlying pixelref, which *should* remove
us from the cache |
| 203 |
| 204 mipmap = SkMipMapCache::FindAndRef(src[i], cache); |
| 205 REPORTER_ASSERT(reporter, !mipmap); |
| 206 } |
| 207 } |
| 208 |
| 209 static void test_bitmap_notify(skiatest::Reporter* reporter, SkResourceCache* ca
che) { |
| 210 const SkIRect subset = SkIRect::MakeWH(5, 5); |
| 211 const int N = 3; |
| 212 SkBitmap src[N], dst[N]; |
| 213 for (int i = 0; i < N; ++i) { |
| 214 src[i].allocN32Pixels(5, 5); |
| 215 src[i].setImmutable(); |
| 216 dst[i].allocN32Pixels(5, 5); |
| 217 dst[i].setImmutable(); |
| 218 SkBitmapCache::Add(src[i].getGenerationID(), subset, dst[i], cache); |
| 219 } |
| 220 |
| 221 for (int i = 0; i < N; ++i) { |
| 222 const uint32_t genID = src[i].getGenerationID(); |
| 223 SkBitmap result; |
| 224 bool found = SkBitmapCache::Find(genID, subset, &result, cache); |
| 225 if (cache) { |
| 226 // if cache is null, we're working on the global cache, and other th
reads might purge |
| 227 // it, making this check fragile. |
| 228 REPORTER_ASSERT(reporter, found); |
| 229 } |
| 230 |
| 231 src[i].reset(); // delete the underlying pixelref, which *should* remove
us from the cache |
| 232 |
| 233 found = SkBitmapCache::Find(genID, subset, &result, cache); |
| 234 REPORTER_ASSERT(reporter, !found); |
| 235 } |
| 236 } |
| 237 |
176 DEF_TEST(BitmapCache_discarded_bitmap, reporter) { | 238 DEF_TEST(BitmapCache_discarded_bitmap, reporter) { |
177 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardabl
eFactory(); | 239 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardabl
eFactory(); |
178 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); | 240 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); |
179 | 241 |
180 SkAutoTDelete<SkResourceCache> cache; | 242 SkAutoTDelete<SkResourceCache> cache; |
181 if (factory) { | 243 if (factory) { |
182 cache.reset(SkNEW_ARGS(SkResourceCache, (factory))); | 244 cache.reset(SkNEW_ARGS(SkResourceCache, (factory))); |
183 } else { | 245 } else { |
184 const size_t byteLimit = 100 * 1024; | 246 const size_t byteLimit = 100 * 1024; |
185 cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit))); | 247 cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit))); |
(...skipping 26 matching lines...) Expand all Loading... |
212 | 274 |
213 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); | 275 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); |
214 cachedBitmap.setImmutable(); | 276 cachedBitmap.setImmutable(); |
215 cachedBitmap.unlockPixels(); | 277 cachedBitmap.unlockPixels(); |
216 | 278 |
217 // We can add the bitmap back to the cache and find it again. | 279 // We can add the bitmap back to the cache and find it again. |
218 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(),
rect, cachedBitmap, cache)); | 280 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(),
rect, cachedBitmap, cache)); |
219 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); | 281 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); |
220 | 282 |
221 test_mipmapcache(reporter, cache); | 283 test_mipmapcache(reporter, cache); |
| 284 test_bitmap_notify(reporter, cache); |
| 285 test_mipmap_notify(reporter, cache); |
222 } | 286 } |
OLD | NEW |