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

Side by Side Diff: tests/SkResourceCacheTest.cpp

Issue 592843003: Add SkCachedData and use it for SkMipMap (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: rebase Created 6 years, 2 months 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
« no previous file with comments | « tests/MipMapTest.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID( ), rect, &bm, cache)); 114 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID( ), rect, &bm, cache));
115 115
116 // Should not be in the cache 116 // Should not be in the cache
117 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID( ), rect, &bm, cache)); 117 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID( ), rect, &bm, cache));
118 118
119 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache)); 119 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache));
120 // Should be in the cache, we just added it 120 // Should be in the cache, we just added it
121 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID() , rect, &bm, cache)); 121 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID() , rect, &bm, cache));
122 } 122 }
123 123
124 #include "SkMipMap.h"
125
126 enum LockedState {
127 kNotLocked,
128 kLocked,
129 };
130
131 enum CachedState {
132 kNotInCache,
133 kInCache,
134 };
135
136 static void check_data(skiatest::Reporter* reporter, const SkCachedData* data,
137 int refcnt, CachedState cacheState, LockedState lockedSta te) {
138 REPORTER_ASSERT(reporter, data->testing_only_getRefCnt() == refcnt);
139 REPORTER_ASSERT(reporter, data->testing_only_isInCache() == (kInCache == cac heState));
140 bool isLocked = (data->data() != NULL);
141 REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked));
142 }
143
144 static void test_mipmapcache(skiatest::Reporter* reporter, SkResourceCache* cach e) {
145 cache->purgeAll();
146
147 SkBitmap src;
148 src.allocN32Pixels(5, 5);
149 src.setImmutable();
150
151 const SkMipMap* mipmap = SkMipMapCache::FindAndRef(src, cache);
152 REPORTER_ASSERT(reporter, NULL == mipmap);
153
154 mipmap = SkMipMapCache::AddAndRef(src, cache);
155 REPORTER_ASSERT(reporter, mipmap);
156 check_data(reporter, mipmap, 2, kInCache, kLocked);
157
158 mipmap->unref();
159 // tricky, since technically after this I'm no longer an owner, but since th e cache is
160 // local, I know it won't get purged behind my back
161 check_data(reporter, mipmap, 1, kInCache, kNotLocked);
162
163 // find us again
164 mipmap = SkMipMapCache::FindAndRef(src, cache);
165 check_data(reporter, mipmap, 2, kInCache, kLocked);
166
167 cache->purgeAll();
168 check_data(reporter, mipmap, 1, kNotInCache, kLocked);
169
170 mipmap->unref();
171 }
172
124 DEF_TEST(BitmapCache_discarded_bitmap, reporter) { 173 DEF_TEST(BitmapCache_discarded_bitmap, reporter) {
125 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardabl eFactory(); 174 SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardabl eFactory();
126 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); 175 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator();
127 176
128 SkAutoTDelete<SkResourceCache> cache; 177 SkAutoTDelete<SkResourceCache> cache;
129 if (factory) { 178 if (factory) {
130 cache.reset(SkNEW_ARGS(SkResourceCache, (factory))); 179 cache.reset(SkNEW_ARGS(SkResourceCache, (factory)));
131 } else { 180 } else {
132 const size_t byteLimit = 100 * 1024; 181 const size_t byteLimit = 100 * 1024;
133 cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit))); 182 cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit)));
(...skipping 24 matching lines...) Expand all
158 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGeneratio nID(), rect, &bm, cache)); 207 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGeneratio nID(), rect, &bm, cache));
159 } 208 }
160 209
161 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); 210 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator);
162 cachedBitmap.setImmutable(); 211 cachedBitmap.setImmutable();
163 cachedBitmap.unlockPixels(); 212 cachedBitmap.unlockPixels();
164 213
165 // 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.
166 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache)); 215 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache));
167 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID() , rect, &bm, cache)); 216 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID() , rect, &bm, cache));
217
218 test_mipmapcache(reporter, cache);
168 } 219 }
OLDNEW
« no previous file with comments | « tests/MipMapTest.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698