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 #include "Test.h" | 8 #include "Test.h" |
9 #include "SkBitmapCache.h" | |
8 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkDiscardableMemoryPool.h" | |
9 #include "SkGraphics.h" | 12 #include "SkGraphics.h" |
10 #include "SkBitmapCache.h" | 13 #include "SkResourceCache.h" |
11 #include "SkDiscardableMemoryPool.h" | |
12 | 14 |
13 static const int kCanvasSize = 1; | 15 static const int kCanvasSize = 1; |
14 static const int kBitmapSize = 16; | 16 static const int kBitmapSize = 16; |
15 static const int kScale = 8; | 17 static const int kScale = 8; |
16 | 18 |
17 static bool is_in_scaled_image_cache(const SkBitmap& orig, | 19 static bool is_in_scaled_image_cache(const SkBitmap& orig, |
18 SkScalar xScale, | 20 SkScalar xScale, |
19 SkScalar yScale) { | 21 SkScalar yScale) { |
20 SkBitmap scaled; | 22 SkBitmap scaled; |
21 float roundedImageWidth = SkScalarRoundToScalar(orig.width() * xScale); | 23 float roundedImageWidth = SkScalarRoundToScalar(orig.width() * xScale); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache | 69 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache |
68 SkGraphics::SetResourceCacheTotalByteLimit(2 * size); | 70 SkGraphics::SetResourceCacheTotalByteLimit(2 * size); |
69 SkGraphics::SetResourceCacheSingleAllocationByteLimit(size / 2); // too sma ll | 71 SkGraphics::SetResourceCacheSingleAllocationByteLimit(size / 2); // too sma ll |
70 | 72 |
71 REPORTER_ASSERT(reporter, !test_scaled_image_cache_useage()); | 73 REPORTER_ASSERT(reporter, !test_scaled_image_cache_useage()); |
72 | 74 |
73 SkGraphics::SetResourceCacheSingleAllocationByteLimit(originalAllocationLimi t); | 75 SkGraphics::SetResourceCacheSingleAllocationByteLimit(originalAllocationLimi t); |
74 SkGraphics::SetResourceCacheTotalByteLimit(originalByteLimit); | 76 SkGraphics::SetResourceCacheTotalByteLimit(originalByteLimit); |
75 } | 77 } |
76 | 78 |
77 #if 0 // skia:2912 | 79 //////////////////////////////////////////////////////////////////////////////// //////// |
78 | 80 |
79 static SkBitmap createAllocatedBitmap(const SkImageInfo& info) { | 81 static void make_bitmap(SkBitmap* bitmap, const SkImageInfo& info, SkBitmap::All ocator* allocator) { |
80 SkBitmap cachedBitmap; | |
81 cachedBitmap.setInfo(info); | |
82 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); | |
83 if (allocator) { | 82 if (allocator) { |
84 allocator->allocPixelRef(&cachedBitmap, 0); | 83 bitmap->setInfo(info); |
84 allocator->allocPixelRef(bitmap, 0); | |
85 } else { | 85 } else { |
86 cachedBitmap.allocPixels(); | 86 bitmap->allocPixels(info); |
87 } | 87 } |
88 | |
89 return cachedBitmap; | |
90 } | 88 } |
91 | 89 |
92 // http://skbug.com/2894 | 90 // http://skbug.com/2894 |
93 DEF_TEST(BitmapCache_add_rect, reporter) { | 91 DEF_TEST(BitmapCache_add_rect, reporter) { |
92 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardabl eFactory(); | |
93 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); | |
94 | |
95 SkAutoTDelete<SkResourceCache> cache; | |
96 if (factory) { | |
97 cache.reset(SkNEW_ARGS(SkResourceCache, (factory))); | |
98 } else { | |
99 const size_t byteLimit = 100 * 1024; | |
100 cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit))); | |
101 } | |
102 SkBitmap cachedBitmap; | |
103 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); | |
104 cachedBitmap.setImmutable(); | |
105 | |
94 SkBitmap bm; | 106 SkBitmap bm; |
95 SkIRect rect = SkIRect::MakeWH(5, 5); | 107 SkIRect rect = SkIRect::MakeWH(5, 5); |
96 SkBitmap cachedBitmap = createAllocatedBitmap(SkImageInfo::MakeN32Premul(5, 5)); | |
97 cachedBitmap.setImmutable(); | |
98 | 108 |
99 // Wrong subset size | 109 // Wrong subset size |
100 REPORTER_ASSERT(reporter, ! SkBitmapCache::Add(cachedBitmap.getGenerationID( ), SkIRect::MakeWH(4, 6), cachedBitmap)); | 110 REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedBitmap.getGenerationID() , SkIRect::MakeWH(4, 6), cachedBitmap, cache)); |
101 REPORTER_ASSERT(reporter, ! SkBitmapCache::Find(cachedBitmap.getGenerationID (), rect, &bm)); | 111 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID( ), rect, &bm, cache)); |
102 // Wrong offset value | 112 // Wrong offset value |
103 REPORTER_ASSERT(reporter, ! SkBitmapCache::Add(cachedBitmap.getGenerationID( ), SkIRect::MakeXYWH(-1, 0, 5, 5), cachedBitmap)); | 113 REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedBitmap.getGenerationID() , SkIRect::MakeXYWH(-1, 0, 5, 5), cachedBitmap, cache)); |
104 REPORTER_ASSERT(reporter, ! SkBitmapCache::Find(cachedBitmap.getGenerationID (), rect, &bm)); | 114 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID( ), rect, &bm, cache)); |
105 | 115 |
106 // Should not be in the cache | 116 // Should not be in the cache |
107 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID( ), rect, &bm)); | 117 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID( ), rect, &bm, cache)); |
108 | 118 |
109 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap)); | 119 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache)); |
110 // Should be in the cache, we just added it | 120 // Should be in the cache, we just added it |
111 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID() , rect, &bm)); | 121 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID() , rect, &bm, cache)); |
112 } | 122 } |
113 | 123 |
114 DEF_TEST(BitmapCache_discarded_bitmap, reporter) { | 124 DEF_TEST(BitmapCache_discarded_bitmap, reporter) { |
115 SkBitmap bm; | 125 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardabl eFactory(); |
116 SkIRect rect = SkIRect::MakeWH(5, 5); | 126 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); |
117 SkBitmap cachedBitmap = createAllocatedBitmap(SkImageInfo::MakeN32Premul(5, 5)); | 127 |
128 SkAutoTDelete<SkResourceCache> cache; | |
129 if (factory) { | |
130 cache.reset(SkNEW_ARGS(SkResourceCache, (factory))); | |
131 } else { | |
132 const size_t byteLimit = 100 * 1024; | |
133 cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit))); | |
134 } | |
135 SkBitmap cachedBitmap; | |
136 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); | |
118 cachedBitmap.setImmutable(); | 137 cachedBitmap.setImmutable(); |
119 cachedBitmap.unlockPixels(); | 138 cachedBitmap.unlockPixels(); |
120 | 139 |
140 SkBitmap bm; | |
141 SkIRect rect = SkIRect::MakeWH(5, 5); | |
142 | |
121 // Add a bitmap to the cache. | 143 // Add a bitmap to the cache. |
122 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap)); | 144 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache)); |
123 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID() , rect, &bm)); | 145 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID() , rect, &bm, cache)); |
124 | 146 |
125 // Finding more than once works fine. | 147 // Finding more than once works fine. |
126 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID() , rect, &bm)); | 148 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID() , rect, &bm, cache)); |
127 bm.unlockPixels(); | 149 bm.unlockPixels(); |
128 | 150 |
129 // Drop the pixels in the bitmap. | 151 // Drop the pixels in the bitmap. |
130 REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed() > 0); | 152 if (factory) { |
131 SkGetGlobalDiscardableMemoryPool()->dumpPool(); | 153 REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed () > 0); |
132 REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed() = = 0); | 154 SkGetGlobalDiscardableMemoryPool()->dumpPool(); |
danakj
2014/09/16 15:26:27
Thanks, it's great that we can enable this test. I
reed1
2014/09/16 17:31:12
Agreed, that could be a separate issue/CL to allow
| |
155 REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed () == 0); | |
133 | 156 |
134 // The bitmap is not in the cache since it has been dropped. | 157 // The bitmap is not in the cache since it has been dropped. |
135 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID( ), rect, &bm)); | 158 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGeneratio nID(), rect, &bm, cache)); |
159 } | |
136 | 160 |
137 cachedBitmap = createAllocatedBitmap(SkImageInfo::MakeN32Premul(5, 5)); | 161 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); |
138 cachedBitmap.setImmutable(); | 162 cachedBitmap.setImmutable(); |
139 cachedBitmap.unlockPixels(); | 163 cachedBitmap.unlockPixels(); |
140 | 164 |
141 // We can add the bitmap back to the cache and find it again. | 165 // We can add the bitmap back to the cache and find it again. |
142 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap)); | 166 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache)); |
143 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID() , rect, &bm)); | 167 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID() , rect, &bm, cache)); |
144 } | 168 } |
145 #endif | |
OLD | NEW |