Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: tests/SkResourceCacheTest.cpp

Issue 1426753006: SkResourceCache::GetAllocator() index8 and other color types handling (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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);
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
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, SkResourceCache::DiscardableFactory factory) {
185 cache.reset(new SkResourceCache(byteLimit)); 192 SkBitmap::Allocator* allocator = cache->allocator();
193 SkColorType testTypes[] = {
reed1 2015/11/09 14:14:19 nit: const
194 kAlpha_8_SkColorType,
195 kRGB_565_SkColorType,
196 kARGB_4444_SkColorType,
197 kRGBA_8888_SkColorType,
198 kBGRA_8888_SkColorType,
199 kIndex_8_SkColorType,
200 kGray_8_SkColorType
201 };
202 for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) {
203 SkBitmap cachedBitmap;
204 make_bitmap(&cachedBitmap, SkImageInfo::Make(5, 5, testTypes[i], kPremul _SkAlphaType), allocator);
reed1 2015/11/09 14:14:19 nits: 100 cols
205 cachedBitmap.setImmutable();
206 cachedBitmap.unlockPixels();
207
208 SkBitmap bm;
209 SkIRect rect = SkIRect::MakeWH(5, 5);
210
211 // Add a bitmap to the cache.
212 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), re ct, cachedBitmap, cache));
213 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGeneration ID(), rect, &bm, cache));
214
215 // Finding more than once works fine.
216 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGeneration ID(), rect, &bm, cache));
217 bm.unlockPixels();
218
219 // Drop the pixels in the bitmap.
220 if (factory) {
221 REPORTER_ASSERT(reporter, gPool->getRAMUsed() > 0);
222 gPool->dumpPool();
223
224 // The bitmap is not in the cache since it has been dropped.
225 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGener ationID(), rect, &bm, cache));
226 }
227
228 make_bitmap(&cachedBitmap, SkImageInfo::Make(5, 5, testTypes[i], kPremul _SkAlphaType), allocator);
229 cachedBitmap.setImmutable();
230 cachedBitmap.unlockPixels();
231
232 // We can add the bitmap back to the cache and find it again.
233 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), re ct, cachedBitmap, cache));
234 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGeneration ID(), rect, &bm, cache));
186 } 235 }
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); 236 test_mipmapcache(reporter, cache);
221 test_bitmap_notify(reporter, cache); 237 test_bitmap_notify(reporter, cache);
222 test_mipmap_notify(reporter, cache); 238 test_mipmap_notify(reporter, cache);
223 } 239 }
224 240
241 DEF_TEST(BitmapCache_discarded_bitmap, reporter) {
242 const size_t byteLimit = 100 * 1024;
243 {
244 SkAutoTDelete<SkResourceCache> cache;
245 cache.reset(new SkResourceCache(byteLimit));
246 testBitmapCache_discarded_bitmap(reporter, cache, nullptr);
247 }
248 {
249 SkAutoTDelete<SkResourceCache> cache;
250 SkAutoTUnref<SkDiscardableMemoryPool> pool(SkDiscardableMemoryPool::Crea te(byteLimit, nullptr));
251 gPool = pool.get();
252 SkResourceCache::DiscardableFactory factory = pool_factory;
253 cache.reset(new SkResourceCache(factory));
254 testBitmapCache_discarded_bitmap(reporter, cache, factory);
255 }
256 }
257
225 static void test_discarded_image(skiatest::Reporter* reporter, const SkMatrix& t ransform, 258 static void test_discarded_image(skiatest::Reporter* reporter, const SkMatrix& t ransform,
226 SkImage* (*buildImage)()) { 259 SkImage* (*buildImage)()) {
227 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(10, 10)); 260 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(10, 10));
228 SkCanvas* canvas = surface->getCanvas(); 261 SkCanvas* canvas = surface->getCanvas();
229 262
230 // SkBitmapCache is global, so other threads could be evicting our bitmaps. Loop a few times 263 // SkBitmapCache is global, so other threads could be evicting our bitmaps. Loop a few times
231 // to mitigate this risk. 264 // to mitigate this risk.
232 const unsigned kRepeatCount = 42; 265 const unsigned kRepeatCount = 42;
233 for (unsigned i = 0; i < kRepeatCount; ++i) { 266 for (unsigned i = 0; i < kRepeatCount; ++i) {
234 SkAutoCanvasRestore acr(canvas, true); 267 SkAutoCanvasRestore acr(canvas, true);
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 313
281 test_discarded_image(reporter, xforms[i], []() { 314 test_discarded_image(reporter, xforms[i], []() {
282 SkPictureRecorder recorder; 315 SkPictureRecorder recorder;
283 SkCanvas* canvas = recorder.beginRecording(10, 10); 316 SkCanvas* canvas = recorder.beginRecording(10, 10);
284 canvas->clear(SK_ColorCYAN); 317 canvas->clear(SK_ColorCYAN);
285 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 318 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
286 return SkImage::NewFromPicture(picture, SkISize::Make(10, 10), nullp tr, nullptr); 319 return SkImage::NewFromPicture(picture, SkISize::Make(10, 10), nullp tr, nullptr);
287 }); 320 });
288 } 321 }
289 } 322 }
OLDNEW
« tests/CachedDecodingPixelRefTest.cpp ('K') | « tests/CachedDecodingPixelRefTest.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698