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->tryAllocPixels(allocator, ctable); | |
scroggo
2015/11/09 20:18:09
I think this should be allocPixels, to match the o
| |
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 kARGB_4444_SkColorType, | |
scroggo
2015/11/09 20:18:09
Are we supporting 4444?
| |
198 kRGBA_8888_SkColorType, | |
199 kBGRA_8888_SkColorType, | |
200 kIndex_8_SkColorType, | |
201 kGray_8_SkColorType | |
202 }; | |
203 for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) { | |
204 SkBitmap cachedBitmap; | |
205 make_bitmap(&cachedBitmap, SkImageInfo::Make(5, 5, testTypes[i], kPremul _SkAlphaType), | |
206 allocator); | |
207 cachedBitmap.setImmutable(); | |
208 cachedBitmap.unlockPixels(); | |
209 | |
210 SkBitmap bm; | |
211 SkIRect rect = SkIRect::MakeWH(5, 5); | |
212 | |
213 // Add a bitmap to the cache. | |
214 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), re ct, cachedBitmap, | |
215 cache)); | |
216 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGeneration ID(), rect, &bm, | |
217 cache)); | |
218 | |
219 // Finding more than once works fine. | |
220 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGeneration ID(), rect, &bm, | |
221 cache)); | |
222 bm.unlockPixels(); | |
223 | |
224 // Drop the pixels in the bitmap. | |
225 if (factory) { | |
226 REPORTER_ASSERT(reporter, gPool->getRAMUsed() > 0); | |
227 gPool->dumpPool(); | |
228 | |
229 // The bitmap is not in the cache since it has been dropped. | |
230 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGener ationID(), rect, | |
231 &bm, cache)); | |
232 } | |
233 | |
234 make_bitmap(&cachedBitmap, SkImageInfo::Make(5, 5, testTypes[i], kPremul _SkAlphaType), | |
235 allocator); | |
236 cachedBitmap.setImmutable(); | |
237 cachedBitmap.unlockPixels(); | |
238 | |
239 // We can add the bitmap back to the cache and find it again. | |
240 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), re ct, cachedBitmap, | |
241 cache)); | |
242 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGeneration ID(), rect, &bm, | |
243 cache)); | |
186 } | 244 } |
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); | 245 test_mipmapcache(reporter, cache); |
221 test_bitmap_notify(reporter, cache); | 246 test_bitmap_notify(reporter, cache); |
222 test_mipmap_notify(reporter, cache); | 247 test_mipmap_notify(reporter, cache); |
223 } | 248 } |
224 | 249 |
250 DEF_TEST(BitmapCache_discarded_bitmap, reporter) { | |
251 const size_t byteLimit = 100 * 1024; | |
252 { | |
253 SkAutoTDelete<SkResourceCache> cache; | |
254 cache.reset(new SkResourceCache(byteLimit)); | |
scroggo
2015/11/09 20:18:09
nit: Why not just put this on the stack? (same dow
aleksandar.stojiljkovic
2015/11/09 22:19:48
Good point - refactored the purpose out. fixed.
| |
255 testBitmapCache_discarded_bitmap(reporter, cache, nullptr); | |
256 } | |
257 { | |
258 SkAutoTDelete<SkResourceCache> cache; | |
259 SkAutoTUnref<SkDiscardableMemoryPool> pool( | |
260 SkDiscardableMemoryPool::Create(byteLimit, nullptr)); | |
261 gPool = pool.get(); | |
262 SkResourceCache::DiscardableFactory factory = pool_factory; | |
263 cache.reset(new SkResourceCache(factory)); | |
264 testBitmapCache_discarded_bitmap(reporter, cache, factory); | |
265 } | |
266 } | |
267 | |
225 static void test_discarded_image(skiatest::Reporter* reporter, const SkMatrix& t ransform, | 268 static void test_discarded_image(skiatest::Reporter* reporter, const SkMatrix& t ransform, |
226 SkImage* (*buildImage)()) { | 269 SkImage* (*buildImage)()) { |
227 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(10, 10)); | 270 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(10, 10)); |
228 SkCanvas* canvas = surface->getCanvas(); | 271 SkCanvas* canvas = surface->getCanvas(); |
229 | 272 |
230 // SkBitmapCache is global, so other threads could be evicting our bitmaps. Loop a few times | 273 // SkBitmapCache is global, so other threads could be evicting our bitmaps. Loop a few times |
231 // to mitigate this risk. | 274 // to mitigate this risk. |
232 const unsigned kRepeatCount = 42; | 275 const unsigned kRepeatCount = 42; |
233 for (unsigned i = 0; i < kRepeatCount; ++i) { | 276 for (unsigned i = 0; i < kRepeatCount; ++i) { |
234 SkAutoCanvasRestore acr(canvas, true); | 277 SkAutoCanvasRestore acr(canvas, true); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
280 | 323 |
281 test_discarded_image(reporter, xforms[i], []() { | 324 test_discarded_image(reporter, xforms[i], []() { |
282 SkPictureRecorder recorder; | 325 SkPictureRecorder recorder; |
283 SkCanvas* canvas = recorder.beginRecording(10, 10); | 326 SkCanvas* canvas = recorder.beginRecording(10, 10); |
284 canvas->clear(SK_ColorCYAN); | 327 canvas->clear(SK_ColorCYAN); |
285 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); | 328 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
286 return SkImage::NewFromPicture(picture, SkISize::Make(10, 10), nullp tr, nullptr); | 329 return SkImage::NewFromPicture(picture, SkISize::Make(10, 10), nullp tr, nullptr); |
287 }); | 330 }); |
288 } | 331 } |
289 } | 332 } |
OLD | NEW |