OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 #if SK_SUPPORT_GPU | 8 #if SK_SUPPORT_GPU |
9 | 9 |
10 #include "GrContext.h" | 10 #include "GrContext.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
58 } | 58 } |
59 | 59 |
60 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes); | 60 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes); |
61 } | 61 } |
62 | 62 |
63 class TestResource : public GrGpuResource { | 63 class TestResource : public GrGpuResource { |
64 static const size_t kDefaultSize = 100; | 64 static const size_t kDefaultSize = 100; |
65 | 65 |
66 public: | 66 public: |
67 SK_DECLARE_INST_COUNT(TestResource); | 67 SK_DECLARE_INST_COUNT(TestResource); |
68 TestResource(GrGpu* gpu, size_t size = kDefaultSize) | 68 TestResource(GrGpu* gpu) |
69 : INHERITED(gpu, false) | 69 : INHERITED(gpu, false) |
70 , fCache(NULL) | 70 , fCache(NULL) |
71 , fToDelete(NULL) | 71 , fToDelete(NULL) |
72 , fSize(size) { | 72 , fSize(kDefaultSize) { |
73 ++fNumAlive; | 73 ++fNumAlive; |
74 this->registerWithCache(); | 74 this->registerWithCache(); |
75 } | 75 } |
76 | |
77 TestResource(GrGpu* gpu, const GrResourceKey& scratchKey) | |
78 : INHERITED(gpu, false) | |
79 , fCache(NULL) | |
80 , fToDelete(NULL) | |
81 , fSize(kDefaultSize) { | |
82 this->setScratchKey(scratchKey); | |
83 ++fNumAlive; | |
84 this->registerWithCache(); | |
85 } | |
76 | 86 |
77 ~TestResource() { | 87 ~TestResource() { |
78 --fNumAlive; | 88 --fNumAlive; |
79 if (fToDelete) { | 89 if (fToDelete) { |
80 // Breaks our little 2-element cycle below. | 90 // Breaks our little 2-element cycle below. |
81 fToDelete->setDeleteWhenDestroyed(NULL, NULL); | 91 fToDelete->setDeleteWhenDestroyed(NULL, NULL); |
82 fCache->deleteResource(fToDelete->getCacheEntry()); | 92 fCache->deleteResource(fToDelete->getCacheEntry()); |
83 } | 93 } |
84 this->release(); | 94 this->release(); |
85 } | 95 } |
(...skipping 15 matching lines...) Expand all Loading... | |
101 private: | 111 private: |
102 GrResourceCache* fCache; | 112 GrResourceCache* fCache; |
103 TestResource* fToDelete; | 113 TestResource* fToDelete; |
104 size_t fSize; | 114 size_t fSize; |
105 static int fNumAlive; | 115 static int fNumAlive; |
106 | 116 |
107 typedef GrGpuResource INHERITED; | 117 typedef GrGpuResource INHERITED; |
108 }; | 118 }; |
109 int TestResource::fNumAlive = 0; | 119 int TestResource::fNumAlive = 0; |
110 | 120 |
111 static void test_purge_invalidated(skiatest::Reporter* reporter, GrContext* cont ext) { | 121 static void test_duplicate_scratch_key(skiatest::Reporter* reporter) { |
112 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); | 122 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
123 REPORTER_ASSERT(reporter, SkToBool(context)); | |
124 if (NULL == context) { | |
125 return; | |
126 } | |
127 context->setResourceCacheLimits(5, 30000); | |
128 GrResourceCache* cache = context->getResourceCache(); | |
129 SkDEBUGCODE(GrResourceCache2* cache2 = context->getResourceCache2();) | |
130 cache->purgeAllUnlocked(); | |
131 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResour ceBytes()); | |
132 | |
113 GrCacheID::Key keyData; | 133 GrCacheID::Key keyData; |
114 keyData.fData64[0] = 5; | 134 GrCacheID::Domain domain = GrResourceKey::ScratchDomain(); |
115 keyData.fData64[1] = 18; | |
116 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); | 135 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
117 GrResourceKey key(GrCacheID(domain, keyData), t, 0); | 136 GrResourceKey scratchKey(GrCacheID(domain, keyData), t, 0); |
118 | 137 |
robertphillips
2014/11/10 13:28:26
Here.
| |
138 // Add two resources that delete each other from the cache when destroyed. | |
139 TestResource* a = new TestResource(context->getGpu(), scratchKey); | |
140 TestResource* b = new TestResource(context->getGpu(), scratchKey); | |
141 a->setSize(11); | |
142 b->setSize(12); | |
143 // Scratch resources are registered with GrResourceCache2 just by existing. There are 2. | |
144 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey (scratchKey));) | |
145 | |
146 REPORTER_ASSERT(reporter, cache->addResource(scratchKey, a)); | |
147 | |
148 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey (scratchKey));) | |
149 | |
150 // Can't add the same resource twice. | |
151 REPORTER_ASSERT(reporter, !cache->addResource(scratchKey, a)); | |
152 REPORTER_ASSERT(reporter, 1 == cache->getCachedResourceCount()); | |
153 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getCachedResourceByte s()); | |
154 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey (scratchKey));) | |
155 | |
156 // Add a second with the same key. | |
157 REPORTER_ASSERT(reporter, cache->addResource(scratchKey, b)); | |
158 REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount()); | |
159 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == | |
160 cache->getCachedResourceBytes()); | |
161 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | |
162 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey (scratchKey));) | |
163 | |
164 // Our refs mean that the resources are non purgable. | |
165 cache->purgeAllUnlocked(); | |
166 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | |
167 REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount()); | |
168 | |
169 // Unref but don't purge | |
170 a->unref(); | |
171 b->unref(); | |
172 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | |
173 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey (scratchKey));) | |
174 | |
175 // Purge again. This time resources should be purgable. | |
176 cache->purgeAllUnlocked(); | |
177 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | |
178 REPORTER_ASSERT(reporter, 0 == cache->getCachedResourceCount()); | |
179 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey (scratchKey));) | |
180 } | |
181 | |
182 static void test_duplicate_content_key(skiatest::Reporter* reporter) { | |
183 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | |
184 REPORTER_ASSERT(reporter, SkToBool(context)); | |
185 if (NULL == context) { | |
186 return; | |
187 } | |
119 context->setResourceCacheLimits(5, 30000); | 188 context->setResourceCacheLimits(5, 30000); |
120 GrResourceCache* cache = context->getResourceCache(); | 189 GrResourceCache* cache = context->getResourceCache(); |
121 cache->purgeAllUnlocked(); | 190 cache->purgeAllUnlocked(); |
122 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResour ceBytes()); | 191 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResour ceBytes()); |
123 | 192 |
124 // Add two resources with the same key that delete each other from the cache when destroyed. | 193 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
194 GrCacheID::Key keyData; | |
195 memset(&keyData, 0, sizeof(keyData)); | |
196 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); | |
197 GrResourceKey key(GrCacheID(domain, keyData), t, 0); | |
198 | |
199 | |
robertphillips
2014/11/10 13:28:26
Here.
| |
200 // Add two resources that delete each other from the cache when destroyed. | |
125 TestResource* a = new TestResource(context->getGpu()); | 201 TestResource* a = new TestResource(context->getGpu()); |
126 TestResource* b = new TestResource(context->getGpu()); | 202 TestResource* b = new TestResource(context->getGpu()); |
127 cache->addResource(key, a); | 203 a->setSize(11); |
128 cache->addResource(key, b); | 204 b->setSize(12); |
205 REPORTER_ASSERT(reporter, cache->addResource(key, a)); | |
206 // Can't add the same or another resource with the same key. | |
207 REPORTER_ASSERT(reporter, !cache->addResource(key, a)); | |
208 REPORTER_ASSERT(reporter, !cache->addResource(key, b)); | |
209 REPORTER_ASSERT(reporter, 1 == cache->getCachedResourceCount()); | |
210 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getCachedResourceByte s()); | |
211 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | |
212 | |
213 b->unref(); | |
214 cache->purgeAllUnlocked(); | |
215 a->setSize(10); | |
216 REPORTER_ASSERT(reporter, 1 == cache->getCachedResourceCount()); | |
217 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | |
218 | |
219 a->unref(); | |
220 cache->purgeAllUnlocked(); | |
221 REPORTER_ASSERT(reporter, 0 == cache->getCachedResourceCount()); | |
222 REPORTER_ASSERT(reporter, 0 == cache->getCachedResourceBytes()); | |
223 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | |
224 } | |
225 | |
226 static void test_purge_invalidated(skiatest::Reporter* reporter) { | |
227 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | |
228 REPORTER_ASSERT(reporter, SkToBool(context)); | |
229 if (NULL == context) { | |
230 return; | |
231 } | |
232 | |
233 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); | |
234 GrCacheID::Key keyData; | |
235 memset(&keyData, 0, sizeof(keyData)); | |
236 | |
237 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); | |
238 | |
239 keyData.fData64[0] = 1; | |
240 GrResourceKey key1(GrCacheID(domain, keyData), t, 0); | |
241 keyData.fData64[0] = 2; | |
242 GrResourceKey key2(GrCacheID(domain, keyData), t, 0); | |
243 keyData.fData64[0] = 3; | |
244 GrResourceKey key3(GrCacheID(domain, keyData), t, 0); | |
245 | |
246 context->setResourceCacheLimits(5, 30000); | |
247 GrResourceCache* cache = context->getResourceCache(); | |
248 GrResourceCache2* cache2 = context->getResourceCache2(); | |
249 cache->purgeAllUnlocked(); | |
250 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResour ceBytes()); | |
251 | |
robertphillips
2014/11/07 20:32:13
This comment doesn't seem right.
bsalomon
2014/11/07 21:48:44
Don't they delete each other (setDeleteWhenDestroy
robertphillips
2014/11/10 13:28:26
Ooops - I put that comment on the wrong line - ple
bsalomon
2014/11/10 14:14:57
Gotcha, will update before submitting.
| |
252 // Add two resources that delete each other from the cache when destroyed. | |
253 TestResource* a = new TestResource(context->getGpu()); | |
254 TestResource* b = new TestResource(context->getGpu()); | |
255 cache->addResource(key1, a); | |
256 cache->addResource(key2, b); | |
129 // Circle back. | 257 // Circle back. |
130 a->setDeleteWhenDestroyed(cache, b); | 258 a->setDeleteWhenDestroyed(cache, b); |
131 b->setDeleteWhenDestroyed(cache, a); | 259 b->setDeleteWhenDestroyed(cache, a); |
132 a->unref(); | 260 a->unref(); |
133 b->unref(); | 261 b->unref(); |
134 | 262 |
135 // Add a third independent resource also with the same key. | 263 // Add a third independent resource. |
136 GrGpuResource* r = new TestResource(context->getGpu()); | 264 GrGpuResource* r = new TestResource(context->getGpu()); |
137 cache->addResource(key, r); | 265 cache->addResource(key3, r); |
138 r->unref(); | 266 r->unref(); |
139 | 267 |
268 REPORTER_ASSERT(reporter, cache2->hasContentKey(key1)); | |
269 REPORTER_ASSERT(reporter, cache2->hasContentKey(key2)); | |
270 REPORTER_ASSERT(reporter, cache2->hasContentKey(key3)); | |
271 | |
robertphillips
2014/11/07 20:32:13
// Invalidate 2 of the 3 ?
bsalomon
2014/11/07 21:48:44
Done.
| |
140 // Invalidate all three, all three should be purged and destroyed. | 272 // Invalidate all three, all three should be purged and destroyed. |
141 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); | 273 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); |
142 const GrResourceInvalidatedMessage msg = { key }; | 274 const GrResourceInvalidatedMessage msg1 = { key1 }; |
143 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg); | 275 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg1); |
276 const GrResourceInvalidatedMessage msg2 = { key2 }; | |
277 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg2); | |
278 cache->purgeAsNeeded(); | |
279 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | |
280 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1)); | |
281 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key2)); | |
282 REPORTER_ASSERT(reporter, cache2->hasContentKey(key3)); | |
283 | |
robertphillips
2014/11/07 20:32:13
// Finally invalidate the third. ?
bsalomon
2014/11/07 21:48:44
Done.
| |
284 const GrResourceInvalidatedMessage msg3 = { key3 }; | |
285 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg3); | |
144 cache->purgeAsNeeded(); | 286 cache->purgeAsNeeded(); |
145 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 287 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
288 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key3)); | |
146 } | 289 } |
147 | 290 |
148 static void test_cache_delete_on_destruction(skiatest::Reporter* reporter, | 291 static void test_cache_delete_on_destruction(skiatest::Reporter* reporter) { |
149 GrContext* context) { | 292 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
293 REPORTER_ASSERT(reporter, SkToBool(context)); | |
294 if (NULL == context) { | |
295 return; | |
296 } | |
297 | |
150 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); | 298 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
151 GrCacheID::Key keyData; | 299 GrCacheID::Key keyData; |
152 keyData.fData64[0] = 5; | 300 memset(&keyData, 0, sizeof(keyData)); |
153 keyData.fData64[1] = 0; | |
154 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); | 301 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
155 | 302 |
156 GrResourceKey key(GrCacheID(domain, keyData), t, 0); | 303 keyData.fData64[0] = 1; |
304 GrResourceKey key1(GrCacheID(domain, keyData), t, 0); | |
305 | |
306 keyData.fData64[0] = 2; | |
307 GrResourceKey key2(GrCacheID(domain, keyData), t, 0); | |
157 | 308 |
158 { | 309 { |
159 context->setResourceCacheLimits(3, 30000); | 310 context->setResourceCacheLimits(3, 30000); |
160 GrResourceCache* cache = context->getResourceCache(); | 311 GrResourceCache* cache = context->getResourceCache(); |
161 cache->purgeAllUnlocked(); | 312 cache->purgeAllUnlocked(); |
162 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedRe sourceBytes()); | 313 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedRe sourceBytes()); |
163 | 314 |
164 TestResource* a = new TestResource(context->getGpu()); | 315 TestResource* a = new TestResource(context->getGpu()); |
165 TestResource* b = new TestResource(context->getGpu()); | 316 TestResource* b = new TestResource(context->getGpu()); |
166 cache->addResource(key, a); | 317 cache->addResource(key1, a); |
167 cache->addResource(key, b); | 318 cache->addResource(key2, b); |
168 | 319 |
169 a->setDeleteWhenDestroyed(cache, b); | 320 a->setDeleteWhenDestroyed(cache, b); |
170 b->setDeleteWhenDestroyed(cache, a); | 321 b->setDeleteWhenDestroyed(cache, a); |
171 | 322 |
172 a->unref(); | 323 a->unref(); |
173 b->unref(); | 324 b->unref(); |
325 | |
174 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 326 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
327 | |
175 cache->purgeAllUnlocked(); | 328 cache->purgeAllUnlocked(); |
176 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 329 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
177 } | 330 } |
178 { | 331 { |
179 context->setResourceCacheLimits(3, 30000); | 332 context->setResourceCacheLimits(3, 30000); |
180 GrResourceCache* cache = context->getResourceCache(); | 333 GrResourceCache* cache = context->getResourceCache(); |
181 cache->purgeAllUnlocked(); | 334 cache->purgeAllUnlocked(); |
182 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedRe sourceBytes()); | 335 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedRe sourceBytes()); |
336 | |
183 TestResource* a = new TestResource(context->getGpu()); | 337 TestResource* a = new TestResource(context->getGpu()); |
184 TestResource* b = new TestResource(context->getGpu()); | 338 TestResource* b = new TestResource(context->getGpu()); |
185 cache->addResource(key, a); | 339 cache->addResource(key1, a); |
186 cache->addResource(key, b); | 340 cache->addResource(key2, b); |
187 | 341 |
188 a->setDeleteWhenDestroyed(cache, b); | 342 a->setDeleteWhenDestroyed(cache, b); |
189 b->setDeleteWhenDestroyed(cache, a); | 343 b->setDeleteWhenDestroyed(cache, a); |
190 | 344 |
191 a->unref(); | 345 a->unref(); |
192 b->unref(); | 346 b->unref(); |
193 | 347 |
194 cache->deleteResource(a->getCacheEntry()); | 348 cache->deleteResource(a->getCacheEntry()); |
195 | |
196 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 349 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
197 } | 350 } |
198 } | 351 } |
199 | 352 |
200 static void test_resource_size_changed(skiatest::Reporter* reporter, | 353 static void test_resource_size_changed(skiatest::Reporter* reporter) { |
201 GrContext* context) { | 354 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
355 REPORTER_ASSERT(reporter, SkToBool(context)); | |
356 if (NULL == context) { | |
357 return; | |
358 } | |
359 | |
202 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); | 360 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
203 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); | 361 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
204 | 362 |
205 GrCacheID::Key key1Data; | 363 GrCacheID::Key key1Data; |
206 key1Data.fData64[0] = 0; | 364 key1Data.fData64[0] = 0; |
207 key1Data.fData64[1] = 0; | 365 key1Data.fData64[1] = 0; |
208 GrResourceKey key1(GrCacheID(domain, key1Data), t, 0); | 366 GrResourceKey key1(GrCacheID(domain, key1Data), t, 0); |
209 | 367 |
210 GrCacheID::Key key2Data; | 368 GrCacheID::Key key2Data; |
211 key2Data.fData64[0] = 1; | 369 key2Data.fData64[0] = 1; |
212 key2Data.fData64[1] = 0; | 370 key2Data.fData64[1] = 0; |
213 GrResourceKey key2(GrCacheID(domain, key2Data), t, 0); | 371 GrResourceKey key2(GrCacheID(domain, key2Data), t, 0); |
214 | 372 |
215 // Test changing resources sizes (both increase & decrease). | 373 // Test changing resources sizes (both increase & decrease). |
216 { | 374 { |
217 context->setResourceCacheLimits(3, 30000); | 375 context->setResourceCacheLimits(3, 30000); |
218 GrResourceCache* cache = context->getResourceCache(); | 376 GrResourceCache* cache = context->getResourceCache(); |
377 GrResourceCache2* cache2 = context->getResourceCache2(); | |
219 cache->purgeAllUnlocked(); | 378 cache->purgeAllUnlocked(); |
220 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedRe sourceBytes()); | 379 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedRe sourceBytes()); |
221 | 380 |
222 TestResource* a = new TestResource(context->getGpu()); | 381 TestResource* a = new TestResource(context->getGpu()); |
223 a->setSize(100); // Test didChangeGpuMemorySize() when not in the cache. | 382 a->setSize(100); // Test didChangeGpuMemorySize() when not in the cache. |
224 cache->addResource(key1, a); | 383 cache->addResource(key1, a); |
225 a->unref(); | 384 a->unref(); |
226 | 385 |
227 TestResource* b = new TestResource(context->getGpu()); | 386 TestResource* b = new TestResource(context->getGpu()); |
228 b->setSize(100); | 387 b->setSize(100); |
229 cache->addResource(key2, b); | 388 cache->addResource(key2, b); |
230 b->unref(); | 389 b->unref(); |
231 | 390 |
232 REPORTER_ASSERT(reporter, 200 == cache->getCachedResourceBytes()); | 391 REPORTER_ASSERT(reporter, 200 == cache->getCachedResourceBytes()); |
233 REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount()); | 392 REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount()); |
234 | 393 { |
235 static_cast<TestResource*>(cache->find(key2))->setSize(200); | 394 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2-> findAndRefContentResource(key2))); |
236 static_cast<TestResource*>(cache->find(key1))->setSize(50); | 395 find2->setSize(200); |
396 SkAutoTUnref<TestResource> find1(static_cast<TestResource*>(cache2-> findAndRefContentResource(key1))); | |
397 find1->setSize(50); | |
398 } | |
237 | 399 |
238 REPORTER_ASSERT(reporter, 250 == cache->getCachedResourceBytes()); | 400 REPORTER_ASSERT(reporter, 250 == cache->getCachedResourceBytes()); |
239 REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount()); | 401 REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount()); |
240 } | 402 } |
241 | 403 |
242 // Test increasing a resources size beyond the cache budget. | 404 // Test increasing a resources size beyond the cache budget. |
243 { | 405 { |
244 context->setResourceCacheLimits(2, 300); | 406 context->setResourceCacheLimits(2, 300); |
245 GrResourceCache* cache = context->getResourceCache(); | 407 GrResourceCache* cache = context->getResourceCache(); |
408 GrResourceCache2* cache2 = context->getResourceCache2(); | |
246 cache->purgeAllUnlocked(); | 409 cache->purgeAllUnlocked(); |
247 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedRe sourceBytes()); | 410 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedRe sourceBytes()); |
248 | 411 |
249 TestResource* a = new TestResource(context->getGpu(), 100); | 412 TestResource* a = new TestResource(context->getGpu()); |
413 a->setSize(100); | |
250 cache->addResource(key1, a); | 414 cache->addResource(key1, a); |
251 a->unref(); | 415 a->unref(); |
252 | 416 |
253 TestResource* b = new TestResource(context->getGpu(), 100); | 417 TestResource* b = new TestResource(context->getGpu()); |
418 b->setSize(100); | |
254 cache->addResource(key2, b); | 419 cache->addResource(key2, b); |
255 b->unref(); | 420 b->unref(); |
256 | 421 |
257 REPORTER_ASSERT(reporter, 200 == cache->getCachedResourceBytes()); | 422 REPORTER_ASSERT(reporter, 200 == cache->getCachedResourceBytes()); |
258 REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount()); | 423 REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount()); |
259 | 424 |
260 static_cast<TestResource*>(cache->find(key2))->setSize(201); | 425 { |
261 REPORTER_ASSERT(reporter, !cache->hasKey(key1)); | 426 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2-> findAndRefContentResource(key2))); |
427 find2->setSize(201); | |
428 } | |
429 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1)); | |
262 | 430 |
263 REPORTER_ASSERT(reporter, 201 == cache->getCachedResourceBytes()); | 431 REPORTER_ASSERT(reporter, 201 == cache->getCachedResourceBytes()); |
264 REPORTER_ASSERT(reporter, 1 == cache->getCachedResourceCount()); | 432 REPORTER_ASSERT(reporter, 1 == cache->getCachedResourceCount()); |
265 } | 433 } |
266 } | 434 } |
267 | 435 |
268 //////////////////////////////////////////////////////////////////////////////// | 436 //////////////////////////////////////////////////////////////////////////////// |
269 DEF_GPUTEST(ResourceCache, reporter, factory) { | 437 DEF_GPUTEST(ResourceCache, reporter, factory) { |
270 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { | 438 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
271 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G LContextType>(type); | 439 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G LContextType>(type); |
272 if (!GrContextFactory::IsRenderingGLContext(glType)) { | 440 if (!GrContextFactory::IsRenderingGLContext(glType)) { |
273 continue; | 441 continue; |
274 } | 442 } |
275 GrContext* context = factory->get(glType); | 443 GrContext* context = factory->get(glType); |
276 if (NULL == context) { | 444 if (NULL == context) { |
277 continue; | 445 continue; |
278 } | 446 } |
279 GrSurfaceDesc desc; | 447 GrSurfaceDesc desc; |
280 desc.fConfig = kSkia8888_GrPixelConfig; | 448 desc.fConfig = kSkia8888_GrPixelConfig; |
281 desc.fFlags = kRenderTarget_GrSurfaceFlag; | 449 desc.fFlags = kRenderTarget_GrSurfaceFlag; |
282 desc.fWidth = gWidth; | 450 desc.fWidth = gWidth; |
283 desc.fHeight = gHeight; | 451 desc.fHeight = gHeight; |
284 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight); | 452 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight); |
285 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, info )); | 453 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, info )); |
286 test_cache(reporter, context, surface->getCanvas()); | 454 test_cache(reporter, context, surface->getCanvas()); |
287 } | 455 } |
288 | 456 |
289 // The below tests use a mock context. | 457 // The below tests create their own mock contexts. |
290 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 458 test_duplicate_content_key(reporter); |
291 REPORTER_ASSERT(reporter, SkToBool(context)); | 459 test_duplicate_scratch_key(reporter); |
292 if (NULL == context) { | 460 test_purge_invalidated(reporter); |
293 return; | 461 test_cache_delete_on_destruction(reporter); |
294 } | 462 test_resource_size_changed(reporter); |
295 | |
296 test_purge_invalidated(reporter, context); | |
297 test_cache_delete_on_destruction(reporter, context); | |
298 test_resource_size_changed(reporter, context); | |
299 } | 463 } |
300 | 464 |
301 #endif | 465 #endif |
OLD | NEW |