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 "SkResourceCache.h" | 13 #include "SkResourceCache.h" |
14 #include "SkSurface.h" | 14 #include "SkSurface.h" |
15 | 15 |
16 static const int kCanvasSize = 1; | |
17 static const int kBitmapSize = 16; | |
18 static const int kScale = 8; | |
19 | |
20 static bool is_in_scaled_image_cache(const SkBitmap& orig, | |
21 SkScalar xScale, | |
22 SkScalar yScale) { | |
23 SkBitmap scaled; | |
24 int width = SkScalarRoundToInt(orig.width() * xScale); | |
25 int height = SkScalarRoundToInt(orig.height() * yScale); | |
26 return SkBitmapCache::FindWH(SkBitmapCacheDesc::Make(orig, width, height), &
scaled); | |
27 } | |
28 | |
29 // Draw a scaled bitmap, then return true if it has been cached. | |
30 static bool test_scaled_image_cache_usage() { | |
31 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(kCanvasSize, k
CanvasSize)); | |
32 SkCanvas* canvas = surface->getCanvas(); | |
33 SkBitmap bitmap; | |
34 bitmap.allocN32Pixels(kBitmapSize, kBitmapSize); | |
35 bitmap.eraseColor(0xFFFFFFFF); | |
36 SkScalar xScale = SkIntToScalar(kScale); | |
37 SkScalar yScale = xScale / 2; | |
38 SkScalar xScaledSize = SkIntToScalar(kBitmapSize) * xScale; | |
39 SkScalar yScaledSize = SkIntToScalar(kBitmapSize) * yScale; | |
40 canvas->clipRect(SkRect::MakeLTRB(0, 0, xScaledSize, yScaledSize)); | |
41 SkPaint paint; | |
42 paint.setFilterQuality(kHigh_SkFilterQuality); | |
43 | |
44 canvas->drawBitmapRect(bitmap, | |
45 SkRect::MakeLTRB(0, 0, xScaledSize, yScaledSize), | |
46 &paint); | |
47 | |
48 return is_in_scaled_image_cache(bitmap, xScale, yScale); | |
49 } | |
50 | |
51 // http://crbug.com/389439 | |
52 DEF_TEST(ResourceCache_SingleAllocationByteLimit, reporter) { | |
53 size_t originalByteLimit = SkGraphics::GetResourceCacheTotalByteLimit(); | |
54 size_t originalAllocationLimit = | |
55 SkGraphics::GetResourceCacheSingleAllocationByteLimit(); | |
56 | |
57 size_t size = kBitmapSize * kScale * kBitmapSize * kScale | |
58 * SkColorTypeBytesPerPixel(kN32_SkColorType); | |
59 | |
60 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache | |
61 SkGraphics::SetResourceCacheTotalByteLimit(2 * size); | |
62 SkGraphics::SetResourceCacheSingleAllocationByteLimit(0); // No limit | |
63 | |
64 REPORTER_ASSERT(reporter, test_scaled_image_cache_usage()); | |
65 | |
66 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache | |
67 SkGraphics::SetResourceCacheTotalByteLimit(2 * size); | |
68 SkGraphics::SetResourceCacheSingleAllocationByteLimit(size * 2); // big eno
ugh | |
69 | |
70 REPORTER_ASSERT(reporter, test_scaled_image_cache_usage()); | |
71 | |
72 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache | |
73 SkGraphics::SetResourceCacheTotalByteLimit(2 * size); | |
74 SkGraphics::SetResourceCacheSingleAllocationByteLimit(size / 2); // too sma
ll | |
75 | |
76 REPORTER_ASSERT(reporter, !test_scaled_image_cache_usage()); | |
77 | |
78 SkGraphics::SetResourceCacheSingleAllocationByteLimit(originalAllocationLimi
t); | |
79 SkGraphics::SetResourceCacheTotalByteLimit(originalByteLimit); | |
80 } | |
81 | |
82 ////////////////////////////////////////////////////////////////////////////////
//////// | 16 ////////////////////////////////////////////////////////////////////////////////
//////// |
83 | 17 |
84 static void make_bitmap(SkBitmap* bitmap, const SkImageInfo& info, SkBitmap::All
ocator* allocator) { | 18 static void make_bitmap(SkBitmap* bitmap, const SkImageInfo& info, SkBitmap::All
ocator* allocator) { |
85 if (allocator) { | 19 if (allocator) { |
86 bitmap->setInfo(info); | 20 bitmap->setInfo(info); |
87 allocator->allocPixelRef(bitmap, 0); | 21 allocator->allocPixelRef(bitmap, 0); |
88 } else { | 22 } else { |
89 bitmap->allocPixels(info); | 23 bitmap->allocPixels(info); |
90 } | 24 } |
91 } | 25 } |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); | 195 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); |
262 | 196 |
263 // Finding more than once works fine. | 197 // Finding more than once works fine. |
264 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); | 198 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); |
265 bm.unlockPixels(); | 199 bm.unlockPixels(); |
266 | 200 |
267 // Drop the pixels in the bitmap. | 201 // Drop the pixels in the bitmap. |
268 if (factory) { | 202 if (factory) { |
269 REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed
() > 0); | 203 REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed
() > 0); |
270 SkGetGlobalDiscardableMemoryPool()->dumpPool(); | 204 SkGetGlobalDiscardableMemoryPool()->dumpPool(); |
271 REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed
() == 0); | |
272 | 205 |
273 // The bitmap is not in the cache since it has been dropped. | 206 // The bitmap is not in the cache since it has been dropped. |
274 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGeneratio
nID(), rect, &bm, cache)); | 207 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGeneratio
nID(), rect, &bm, cache)); |
275 } | 208 } |
276 | 209 |
277 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); | 210 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); |
278 cachedBitmap.setImmutable(); | 211 cachedBitmap.setImmutable(); |
279 cachedBitmap.unlockPixels(); | 212 cachedBitmap.unlockPixels(); |
280 | 213 |
281 // We can add the bitmap back to the cache and find it again. | 214 // We can add the bitmap back to the cache and find it again. |
282 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), rect,
cachedBitmap, cache)); | 215 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), rect,
cachedBitmap, cache)); |
283 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); | 216 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); |
284 | 217 |
285 test_mipmapcache(reporter, cache); | 218 test_mipmapcache(reporter, cache); |
286 test_bitmap_notify(reporter, cache); | 219 test_bitmap_notify(reporter, cache); |
287 test_mipmap_notify(reporter, cache); | 220 test_mipmap_notify(reporter, cache); |
288 } | 221 } |
OLD | NEW |