| 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" |
| 11 #include "SkDiscardableMemoryPool.h" | 11 #include "SkDiscardableMemoryPool.h" |
| 12 #include "SkGraphics.h" | 12 #include "SkGraphics.h" |
| 13 #include "SkPicture.h" | 13 #include "SkPicture.h" |
| 14 #include "SkPictureRecorder.h" | 14 #include "SkPictureRecorder.h" |
| 15 #include "SkResourceCache.h" | 15 #include "SkResourceCache.h" |
| 16 #include "SkSurface.h" | 16 #include "SkSurface.h" |
| 17 #include "SkTypes.h" |
| 17 | 18 |
| 18 ////////////////////////////////////////////////////////////////////////////////
//////// | 19 ////////////////////////////////////////////////////////////////////////////////
//////// |
| 19 | 20 |
| 20 static void make_bitmap(SkBitmap* bitmap, const SkImageInfo& info, SkBitmap::All
ocator* allocator) { | 21 static void make_bitmap(SkBitmap* bitmap, const SkImageInfo& info, SkBitmap::All
ocator* allocator) { |
| 21 if (allocator) { | 22 if (info.colorType() == kIndex_8_SkColorType) { |
| 23 bitmap->setInfo(info); |
| 24 SkPMColor ctStorage[256]; |
| 25 memset(ctStorage, 0xFF, sizeof(ctStorage)); // init with opaque-white fo
r the moment |
| 26 SkAutoTUnref<SkColorTable> ctable(new SkColorTable(ctStorage, 256)); |
| 27 bitmap->allocPixels(allocator, ctable); |
| 28 } else if (allocator) { |
| 22 bitmap->setInfo(info); | 29 bitmap->setInfo(info); |
| 23 allocator->allocPixelRef(bitmap, 0); | 30 allocator->allocPixelRef(bitmap, 0); |
| 24 } else { | 31 } else { |
| 25 bitmap->allocPixels(info); | 32 bitmap->allocPixels(info); |
| 26 } | 33 } |
| 27 } | 34 } |
| 28 | 35 |
| 29 // http://skbug.com/2894 | 36 // http://skbug.com/2894 |
| 30 DEF_TEST(BitmapCache_add_rect, reporter) { | 37 DEF_TEST(BitmapCache_add_rect, reporter) { |
| 31 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardabl
eFactory(); | 38 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardabl
eFactory(); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 REPORTER_ASSERT(reporter, found); | 173 REPORTER_ASSERT(reporter, found); |
| 167 } | 174 } |
| 168 | 175 |
| 169 src[i].reset(); // delete the underlying pixelref, which *should* remove
us from the cache | 176 src[i].reset(); // delete the underlying pixelref, which *should* remove
us from the cache |
| 170 | 177 |
| 171 found = SkBitmapCache::Find(genID, subset, &result, cache); | 178 found = SkBitmapCache::Find(genID, subset, &result, cache); |
| 172 REPORTER_ASSERT(reporter, !found); | 179 REPORTER_ASSERT(reporter, !found); |
| 173 } | 180 } |
| 174 } | 181 } |
| 175 | 182 |
| 176 DEF_TEST(BitmapCache_discarded_bitmap, reporter) { | 183 #include "SkDiscardableMemoryPool.h" |
| 177 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardabl
eFactory(); | 184 |
| 178 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); | 185 static SkDiscardableMemoryPool* gPool = 0; |
| 179 | 186 static SkDiscardableMemory* pool_factory(size_t bytes) { |
| 180 SkAutoTDelete<SkResourceCache> cache; | 187 SkASSERT(gPool); |
| 181 if (factory) { | 188 return gPool->create(bytes); |
| 182 cache.reset(new SkResourceCache(factory)); | 189 } |
| 183 } else { | 190 |
| 184 const size_t byteLimit = 100 * 1024; | 191 static void testBitmapCache_discarded_bitmap(skiatest::Reporter* reporter, SkRes
ourceCache* cache, |
| 185 cache.reset(new SkResourceCache(byteLimit)); | 192 SkResourceCache::DiscardableFactory
factory) { |
| 193 SkBitmap::Allocator* allocator = cache->allocator(); |
| 194 const SkColorType testTypes[] = { |
| 195 kAlpha_8_SkColorType, |
| 196 kRGB_565_SkColorType, |
| 197 kRGBA_8888_SkColorType, |
| 198 kBGRA_8888_SkColorType, |
| 199 kIndex_8_SkColorType, |
| 200 kGray_8_SkColorType |
| 201 }; |
| 202 for (const SkColorType testType : testTypes) { |
| 203 SkBitmap cachedBitmap; |
| 204 make_bitmap(&cachedBitmap, SkImageInfo::Make(5, 5, testType, kPremul_SkA
lphaType), |
| 205 allocator); |
| 206 cachedBitmap.setImmutable(); |
| 207 cachedBitmap.unlockPixels(); |
| 208 |
| 209 SkBitmap bm; |
| 210 SkIRect rect = SkIRect::MakeWH(5, 5); |
| 211 |
| 212 // Add a bitmap to the cache. |
| 213 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), re
ct, cachedBitmap, |
| 214 cache)); |
| 215 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGeneration
ID(), rect, &bm, |
| 216 cache)); |
| 217 |
| 218 // Finding more than once works fine. |
| 219 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGeneration
ID(), rect, &bm, |
| 220 cache)); |
| 221 bm.unlockPixels(); |
| 222 |
| 223 // Drop the pixels in the bitmap. |
| 224 if (factory) { |
| 225 REPORTER_ASSERT(reporter, gPool->getRAMUsed() > 0); |
| 226 gPool->dumpPool(); |
| 227 |
| 228 // The bitmap is not in the cache since it has been dropped. |
| 229 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGener
ationID(), rect, |
| 230 &bm, cache)); |
| 231 } |
| 232 |
| 233 make_bitmap(&cachedBitmap, SkImageInfo::Make(5, 5, testType, kPremul_SkA
lphaType), |
| 234 allocator); |
| 235 cachedBitmap.setImmutable(); |
| 236 cachedBitmap.unlockPixels(); |
| 237 |
| 238 // We can add the bitmap back to the cache and find it again. |
| 239 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), re
ct, cachedBitmap, |
| 240 cache)); |
| 241 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGeneration
ID(), rect, &bm, |
| 242 cache)); |
| 186 } | 243 } |
| 187 SkBitmap cachedBitmap; | |
| 188 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); | |
| 189 cachedBitmap.setImmutable(); | |
| 190 cachedBitmap.unlockPixels(); | |
| 191 | |
| 192 SkBitmap bm; | |
| 193 SkIRect rect = SkIRect::MakeWH(5, 5); | |
| 194 | |
| 195 // Add a bitmap to the cache. | |
| 196 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), rect,
cachedBitmap, cache)); | |
| 197 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); | |
| 198 | |
| 199 // Finding more than once works fine. | |
| 200 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); | |
| 201 bm.unlockPixels(); | |
| 202 | |
| 203 // Drop the pixels in the bitmap. | |
| 204 if (factory) { | |
| 205 REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed
() > 0); | |
| 206 SkGetGlobalDiscardableMemoryPool()->dumpPool(); | |
| 207 | |
| 208 // The bitmap is not in the cache since it has been dropped. | |
| 209 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGeneratio
nID(), rect, &bm, cache)); | |
| 210 } | |
| 211 | |
| 212 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); | |
| 213 cachedBitmap.setImmutable(); | |
| 214 cachedBitmap.unlockPixels(); | |
| 215 | |
| 216 // We can add the bitmap back to the cache and find it again. | |
| 217 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), rect,
cachedBitmap, cache)); | |
| 218 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); | |
| 219 | |
| 220 test_mipmapcache(reporter, cache); | 244 test_mipmapcache(reporter, cache); |
| 221 test_bitmap_notify(reporter, cache); | 245 test_bitmap_notify(reporter, cache); |
| 222 test_mipmap_notify(reporter, cache); | 246 test_mipmap_notify(reporter, cache); |
| 223 } | 247 } |
| 224 | 248 |
| 249 DEF_TEST(BitmapCache_discarded_bitmap, reporter) { |
| 250 const size_t byteLimit = 100 * 1024; |
| 251 { |
| 252 SkResourceCache cache(byteLimit); |
| 253 testBitmapCache_discarded_bitmap(reporter, &cache, nullptr); |
| 254 } |
| 255 { |
| 256 SkAutoTUnref<SkDiscardableMemoryPool> pool( |
| 257 SkDiscardableMemoryPool::Create(byteLimit, nullptr)); |
| 258 gPool = pool.get(); |
| 259 SkResourceCache::DiscardableFactory factory = pool_factory; |
| 260 SkResourceCache cache(factory); |
| 261 testBitmapCache_discarded_bitmap(reporter, &cache, factory); |
| 262 } |
| 263 } |
| 264 |
| 225 static void test_discarded_image(skiatest::Reporter* reporter, const SkMatrix& t
ransform, | 265 static void test_discarded_image(skiatest::Reporter* reporter, const SkMatrix& t
ransform, |
| 226 SkImage* (*buildImage)()) { | 266 SkImage* (*buildImage)()) { |
| 227 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(10, 10)); | 267 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(10, 10)); |
| 228 SkCanvas* canvas = surface->getCanvas(); | 268 SkCanvas* canvas = surface->getCanvas(); |
| 229 | 269 |
| 230 // SkBitmapCache is global, so other threads could be evicting our bitmaps.
Loop a few times | 270 // SkBitmapCache is global, so other threads could be evicting our bitmaps.
Loop a few times |
| 231 // to mitigate this risk. | 271 // to mitigate this risk. |
| 232 const unsigned kRepeatCount = 42; | 272 const unsigned kRepeatCount = 42; |
| 233 for (unsigned i = 0; i < kRepeatCount; ++i) { | 273 for (unsigned i = 0; i < kRepeatCount; ++i) { |
| 234 SkAutoCanvasRestore acr(canvas, true); | 274 SkAutoCanvasRestore acr(canvas, true); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 | 320 |
| 281 test_discarded_image(reporter, xforms[i], []() { | 321 test_discarded_image(reporter, xforms[i], []() { |
| 282 SkPictureRecorder recorder; | 322 SkPictureRecorder recorder; |
| 283 SkCanvas* canvas = recorder.beginRecording(10, 10); | 323 SkCanvas* canvas = recorder.beginRecording(10, 10); |
| 284 canvas->clear(SK_ColorCYAN); | 324 canvas->clear(SK_ColorCYAN); |
| 285 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); | 325 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 286 return SkImage::NewFromPicture(picture, SkISize::Make(10, 10), nullp
tr, nullptr); | 326 return SkImage::NewFromPicture(picture, SkISize::Make(10, 10), nullp
tr, nullptr); |
| 287 }); | 327 }); |
| 288 } | 328 } |
| 289 } | 329 } |
| OLD | NEW |