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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 | 90 |
91 TestResource(GrGpu* gpu) | 91 TestResource(GrGpu* gpu) |
92 : INHERITED(gpu, kCached_LifeCycle) | 92 : INHERITED(gpu, kCached_LifeCycle) |
93 , fToDelete(NULL) | 93 , fToDelete(NULL) |
94 , fSize(kDefaultSize) | 94 , fSize(kDefaultSize) |
95 , fProperty(kProperty1_SimulatedProperty) { | 95 , fProperty(kProperty1_SimulatedProperty) { |
96 ++fNumAlive; | 96 ++fNumAlive; |
97 this->registerWithCache(); | 97 this->registerWithCache(); |
98 } | 98 } |
99 | 99 |
100 static TestResource* CreateScratchTestResource(GrGpu* gpu, SimulatedProperty
property) { | 100 static TestResource* CreateScratchTestResource(GrGpu* gpu, |
101 return SkNEW_ARGS(TestResource, (gpu, property, kScratchConstructor)); | 101 SimulatedProperty property, |
| 102 bool cached = true) { |
| 103 return SkNEW_ARGS(TestResource, (gpu, property, cached, kScratchConstruc
tor)); |
102 } | 104 } |
103 | 105 |
104 ~TestResource() { | 106 ~TestResource() { |
105 --fNumAlive; | 107 --fNumAlive; |
106 SkSafeUnref(fToDelete); | 108 SkSafeUnref(fToDelete); |
107 } | 109 } |
108 | 110 |
109 void setSize(size_t size) { | 111 void setSize(size_t size) { |
110 fSize = size; | 112 fSize = size; |
111 this->didChangeGpuMemorySize(); | 113 this->didChangeGpuMemorySize(); |
(...skipping 13 matching lines...) Expand all Loading... |
125 } | 127 } |
126 } | 128 } |
127 | 129 |
128 static size_t ExpectedScratchKeySize() { | 130 static size_t ExpectedScratchKeySize() { |
129 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaData
Cnt); | 131 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaData
Cnt); |
130 } | 132 } |
131 | 133 |
132 private: | 134 private: |
133 static const int kScratchKeyFieldCnt = 6; | 135 static const int kScratchKeyFieldCnt = 6; |
134 | 136 |
135 TestResource(GrGpu* gpu, SimulatedProperty property, ScratchConstructor) | 137 TestResource(GrGpu* gpu, SimulatedProperty property, bool cached, ScratchCon
structor) |
136 : INHERITED(gpu, kCached_LifeCycle) | 138 : INHERITED(gpu, cached ? kCached_LifeCycle : kUncached_LifeCycle) |
137 , fToDelete(NULL) | 139 , fToDelete(NULL) |
138 , fSize(kDefaultSize) | 140 , fSize(kDefaultSize) |
139 , fProperty(property) { | 141 , fProperty(property) { |
140 GrScratchKey scratchKey; | 142 GrScratchKey scratchKey; |
141 ComputeScratchKey(fProperty, &scratchKey); | 143 ComputeScratchKey(fProperty, &scratchKey); |
142 this->setScratchKey(scratchKey); | 144 this->setScratchKey(scratchKey); |
143 ++fNumAlive; | 145 ++fNumAlive; |
144 this->registerWithCache(); | 146 this->registerWithCache(); |
145 } | 147 } |
146 | 148 |
147 size_t onGpuMemorySize() const SK_OVERRIDE { return fSize; } | 149 size_t onGpuMemorySize() const SK_OVERRIDE { return fSize; } |
148 | 150 |
149 TestResource* fToDelete; | 151 TestResource* fToDelete; |
150 size_t fSize; | 152 size_t fSize; |
151 static int fNumAlive; | 153 static int fNumAlive; |
152 SimulatedProperty fProperty; | 154 SimulatedProperty fProperty; |
153 typedef GrGpuResource INHERITED; | 155 typedef GrGpuResource INHERITED; |
154 }; | 156 }; |
155 int TestResource::fNumAlive = 0; | 157 int TestResource::fNumAlive = 0; |
156 | 158 |
| 159 class Mock { |
| 160 public: |
| 161 Mock(int maxCnt, size_t maxBytes) { |
| 162 fContext.reset(GrContext::CreateMockContext()); |
| 163 SkASSERT(fContext); |
| 164 fContext->setResourceCacheLimits(maxCnt, maxBytes); |
| 165 GrResourceCache2* cache2 = fContext->getResourceCache2(); |
| 166 cache2->purgeAllUnlocked(); |
| 167 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceByte
s()); |
| 168 } |
| 169 |
| 170 GrResourceCache2* cache() { return fContext->getResourceCache2(); } |
| 171 |
| 172 GrContext* context() { return fContext; } |
| 173 |
| 174 private: |
| 175 SkAutoTUnref<GrContext> fContext; |
| 176 }; |
| 177 |
157 static void test_no_key(skiatest::Reporter* reporter) { | 178 static void test_no_key(skiatest::Reporter* reporter) { |
158 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 179 Mock mock(10, 30000); |
159 REPORTER_ASSERT(reporter, SkToBool(context)); | 180 GrContext* context = mock.context(); |
160 if (NULL == context) { | 181 GrResourceCache2* cache2 = mock.cache(); |
161 return; | |
162 } | |
163 context->setResourceCacheLimits(10, 30000); | |
164 GrResourceCache2* cache2 = context->getResourceCache2(); | |
165 cache2->purgeAllUnlocked(); | |
166 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; | |
167 | 182 |
168 // Create a bunch of resources with no keys | 183 // Create a bunch of resources with no keys |
169 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 184 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
170 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 185 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
171 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu())); | 186 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu())); |
172 TestResource* d = SkNEW_ARGS(TestResource, (context->getGpu())); | 187 TestResource* d = SkNEW_ARGS(TestResource, (context->getGpu())); |
173 a->setSize(11); | 188 a->setSize(11); |
174 b->setSize(12); | 189 b->setSize(12); |
175 c->setSize(13); | 190 c->setSize(13); |
176 d->setSize(14); | 191 d->setSize(14); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 } | 225 } |
211 | 226 |
212 // Each integer passed as a template param creates a new domain. | 227 // Each integer passed as a template param creates a new domain. |
213 template <int> static void make_content_key(GrContentKey* key, int data) { | 228 template <int> static void make_content_key(GrContentKey* key, int data) { |
214 static GrContentKey::Domain d = GrContentKey::GenerateDomain(); | 229 static GrContentKey::Domain d = GrContentKey::GenerateDomain(); |
215 GrContentKey::Builder builder(key, d, 1); | 230 GrContentKey::Builder builder(key, d, 1); |
216 builder[0] = data; | 231 builder[0] = data; |
217 } | 232 } |
218 | 233 |
219 static void test_budgeting(skiatest::Reporter* reporter) { | 234 static void test_budgeting(skiatest::Reporter* reporter) { |
220 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 235 Mock mock(10, 300); |
221 REPORTER_ASSERT(reporter, SkToBool(context)); | 236 GrContext* context = mock.context(); |
222 if (NULL == context) { | 237 GrResourceCache2* cache2 = mock.cache(); |
223 return; | |
224 } | |
225 context->setResourceCacheLimits(10, 300); | |
226 GrResourceCache2* cache2 = context->getResourceCache2(); | |
227 cache2->purgeAllUnlocked(); | |
228 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; | |
229 SkASSERT(0 == cache2->getBudgetedResourceCount() && 0 == cache2->getBudgeted
ResourceBytes()); | |
230 | 238 |
231 GrContentKey contentKey; | 239 GrContentKey contentKey; |
232 make_content_key<0>(&contentKey, 0); | 240 make_content_key<0>(&contentKey, 0); |
233 | 241 |
234 // Create a scratch, a content, and a wrapped resource | 242 // Create a scratch, a content, and a wrapped resource |
235 TestResource* scratch = | 243 TestResource* scratch = |
236 TestResource::CreateScratchTestResource(context->getGpu(), | 244 TestResource::CreateScratchTestResource(context->getGpu(), |
237 TestResource::kProperty2_Sim
ulatedProperty); | 245 TestResource::kProperty2_Sim
ulatedProperty); |
238 scratch->setSize(10); | 246 scratch->setSize(10); |
239 TestResource* content = SkNEW_ARGS(TestResource, (context->getGpu())); | 247 TestResource* content = SkNEW_ARGS(TestResource, (context->getGpu())); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); | 311 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
304 | 312 |
305 unbudgeted->unref(); | 313 unbudgeted->unref(); |
306 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 314 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
307 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); | 315 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
308 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); | 316 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); |
309 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); | 317 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
310 } | 318 } |
311 | 319 |
312 static void test_unbudgeted(skiatest::Reporter* reporter) { | 320 static void test_unbudgeted(skiatest::Reporter* reporter) { |
313 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 321 Mock mock(10, 30000); |
314 REPORTER_ASSERT(reporter, SkToBool(context)); | 322 GrContext* context = mock.context(); |
315 if (NULL == context) { | 323 GrResourceCache2* cache2 = mock.cache(); |
316 return; | |
317 } | |
318 context->setResourceCacheLimits(10, 300); | |
319 GrResourceCache2* cache2 = context->getResourceCache2(); | |
320 cache2->purgeAllUnlocked(); | |
321 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; | |
322 SkASSERT(0 == cache2->getBudgetedResourceCount() && 0 == cache2->getBudgeted
ResourceBytes()); | |
323 | 324 |
324 GrContentKey contentKey; | 325 GrContentKey contentKey; |
325 make_content_key<0>(&contentKey, 0); | 326 make_content_key<0>(&contentKey, 0); |
326 | 327 |
327 TestResource* scratch; | 328 TestResource* scratch; |
328 TestResource* content; | 329 TestResource* content; |
329 TestResource* wrapped; | 330 TestResource* wrapped; |
330 TestResource* unbudgeted; | 331 TestResource* unbudgeted; |
331 | 332 |
332 // A large uncached or wrapped resource shouldn't evict anything. | 333 // A large uncached or wrapped resource shouldn't evict anything. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); | 376 REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); |
376 REPORTER_ASSERT(reporter, 21 == cache2->getBudgetedResourceBytes()); | 377 REPORTER_ASSERT(reporter, 21 == cache2->getBudgetedResourceBytes()); |
377 | 378 |
378 cache2->purgeAllUnlocked(); | 379 cache2->purgeAllUnlocked(); |
379 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 380 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
380 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); | 381 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
381 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); | 382 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); |
382 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); | 383 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
383 } | 384 } |
384 | 385 |
| 386 static void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) { |
| 387 Mock mock(10, 300); |
| 388 GrContext* context = mock.context(); |
| 389 GrResourceCache2* cache2 = mock.cache(); |
| 390 |
| 391 TestResource* resource = |
| 392 TestResource::CreateScratchTestResource(context->getGpu(), |
| 393 TestResource::kProperty1_Simulat
edProperty, false); |
| 394 GrScratchKey key; |
| 395 TestResource::ComputeScratchKey(TestResource::kProperty1_SimulatedProperty,
&key); |
| 396 |
| 397 size_t size = resource->gpuMemorySize(); |
| 398 for (int i = 0; i < 2; ++i) { |
| 399 // Since this resource is unbudgeted, it should not be reachable as scra
tch. |
| 400 REPORTER_ASSERT(reporter, resource->cacheAccess().getScratchKey() == key
); |
| 401 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch()); |
| 402 REPORTER_ASSERT(reporter, !resource->cacheAccess().isBudgeted()); |
| 403 REPORTER_ASSERT(reporter, NULL == cache2->findAndRefScratchResource(key)
); |
| 404 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 405 REPORTER_ASSERT(reporter, size == cache2->getResourceBytes()); |
| 406 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); |
| 407 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
| 408 |
| 409 // Once it is unrefed, it should become available as scratch. |
| 410 resource->unref(); |
| 411 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 412 REPORTER_ASSERT(reporter, size == cache2->getResourceBytes()); |
| 413 REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount()); |
| 414 REPORTER_ASSERT(reporter, size == cache2->getBudgetedResourceBytes()); |
| 415 resource = static_cast<TestResource*>(cache2->findAndRefScratchResource(
key)); |
| 416 REPORTER_ASSERT(reporter, resource); |
| 417 REPORTER_ASSERT(reporter, resource->cacheAccess().getScratchKey() == key
); |
| 418 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch()); |
| 419 REPORTER_ASSERT(reporter, resource->cacheAccess().isBudgeted()); |
| 420 |
| 421 if (0 == i) { |
| 422 // If made unbudgeted, it should return to original state: ref'ed an
d unbudgeted. Try |
| 423 // the above tests again. |
| 424 resource->cacheAccess().makeUnbudgeted(); |
| 425 } else { |
| 426 // After the second time around, try removing the scratch key |
| 427 resource->cacheAccess().removeScratchKey(); |
| 428 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 429 REPORTER_ASSERT(reporter, size == cache2->getResourceBytes()); |
| 430 REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount()); |
| 431 REPORTER_ASSERT(reporter, size == cache2->getBudgetedResourceBytes()
); |
| 432 REPORTER_ASSERT(reporter, !resource->cacheAccess().getScratchKey().i
sValid()); |
| 433 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch()); |
| 434 REPORTER_ASSERT(reporter, resource->cacheAccess().isBudgeted()); |
| 435 |
| 436 // now when it is unrefed it should die since it has no key. |
| 437 resource->unref(); |
| 438 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
| 439 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
| 440 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); |
| 441 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
| 442 } |
| 443 } |
| 444 } |
| 445 |
385 static void test_duplicate_scratch_key(skiatest::Reporter* reporter) { | 446 static void test_duplicate_scratch_key(skiatest::Reporter* reporter) { |
386 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 447 Mock mock(5, 30000); |
387 REPORTER_ASSERT(reporter, SkToBool(context)); | 448 GrContext* context = mock.context(); |
388 if (NULL == context) { | 449 GrResourceCache2* cache2 = mock.cache(); |
389 return; | |
390 } | |
391 context->setResourceCacheLimits(5, 30000); | |
392 GrResourceCache2* cache2 = context->getResourceCache2(); | |
393 cache2->purgeAllUnlocked(); | |
394 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; | |
395 | 450 |
396 // Create two resources that have the same scratch key. | 451 // Create two resources that have the same scratch key. |
397 TestResource* a = | 452 TestResource* a = |
398 TestResource::CreateScratchTestResource(context->getGpu(), | 453 TestResource::CreateScratchTestResource(context->getGpu(), |
399 TestResource::kProperty2_Sim
ulatedProperty); | 454 TestResource::kProperty2_Sim
ulatedProperty); |
400 TestResource* b = | 455 TestResource* b = |
401 TestResource::CreateScratchTestResource(context->getGpu(), | 456 TestResource::CreateScratchTestResource(context->getGpu(), |
402 TestResource::kProperty2_Sim
ulatedProperty); | 457 TestResource::kProperty2_Sim
ulatedProperty); |
403 a->setSize(11); | 458 a->setSize(11); |
404 b->setSize(12); | 459 b->setSize(12); |
(...skipping 24 matching lines...) Expand all Loading... |
429 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey
(scratchKey));) | 484 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey
(scratchKey));) |
430 | 485 |
431 // Purge again. This time resources should be purgable. | 486 // Purge again. This time resources should be purgable. |
432 cache2->purgeAllUnlocked(); | 487 cache2->purgeAllUnlocked(); |
433 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 488 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
434 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 489 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
435 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey
(scratchKey));) | 490 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey
(scratchKey));) |
436 } | 491 } |
437 | 492 |
438 static void test_remove_scratch_key(skiatest::Reporter* reporter) { | 493 static void test_remove_scratch_key(skiatest::Reporter* reporter) { |
439 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 494 Mock mock(5, 30000); |
440 REPORTER_ASSERT(reporter, SkToBool(context)); | 495 GrContext* context = mock.context(); |
441 if (NULL == context) { | 496 GrResourceCache2* cache2 = mock.cache(); |
442 return; | |
443 } | |
444 context->setResourceCacheLimits(5, 30000); | |
445 GrResourceCache2* cache2 = context->getResourceCache2(); | |
446 cache2->purgeAllUnlocked(); | |
447 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; | |
448 | 497 |
449 // Create two resources that have the same scratch key. | 498 // Create two resources that have the same scratch key. |
450 TestResource* a = | 499 TestResource* a = |
451 TestResource::CreateScratchTestResource(context->getGpu(), | 500 TestResource::CreateScratchTestResource(context->getGpu(), |
452 TestResource::kProperty2_Sim
ulatedProperty); | 501 TestResource::kProperty2_Sim
ulatedProperty); |
453 TestResource* b = | 502 TestResource* b = |
454 TestResource::CreateScratchTestResource(context->getGpu(), | 503 TestResource::CreateScratchTestResource(context->getGpu(), |
455 TestResource::kProperty2_Sim
ulatedProperty); | 504 TestResource::kProperty2_Sim
ulatedProperty); |
456 a->unref(); | 505 a->unref(); |
457 b->unref(); | 506 b->unref(); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
497 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey
(scratchKey));) | 546 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey
(scratchKey));) |
498 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 547 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
499 | 548 |
500 find->unref(); | 549 find->unref(); |
501 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 550 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
502 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey
(scratchKey));) | 551 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey
(scratchKey));) |
503 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 552 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
504 } | 553 } |
505 | 554 |
506 static void test_scratch_key_consistency(skiatest::Reporter* reporter) { | 555 static void test_scratch_key_consistency(skiatest::Reporter* reporter) { |
507 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 556 Mock mock(5, 30000); |
508 REPORTER_ASSERT(reporter, SkToBool(context)); | 557 GrContext* context = mock.context(); |
509 if (NULL == context) { | 558 GrResourceCache2* cache2 = mock.cache(); |
510 return; | |
511 } | |
512 context->setResourceCacheLimits(5, 30000); | |
513 GrResourceCache2* cache2 = context->getResourceCache2(); | |
514 cache2->purgeAllUnlocked(); | |
515 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; | |
516 | 559 |
517 // Create two resources that have the same scratch key. | 560 // Create two resources that have the same scratch key. |
518 TestResource* a = | 561 TestResource* a = |
519 TestResource::CreateScratchTestResource(context->getGpu(), | 562 TestResource::CreateScratchTestResource(context->getGpu(), |
520 TestResource::kProperty2_Sim
ulatedProperty); | 563 TestResource::kProperty2_Sim
ulatedProperty); |
521 TestResource* b = | 564 TestResource* b = |
522 TestResource::CreateScratchTestResource(context->getGpu(), | 565 TestResource::CreateScratchTestResource(context->getGpu(), |
523 TestResource::kProperty2_Sim
ulatedProperty); | 566 TestResource::kProperty2_Sim
ulatedProperty); |
524 a->unref(); | 567 a->unref(); |
525 b->unref(); | 568 b->unref(); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
564 | 607 |
565 GrGpuResource* find2 = cache2->findAndRefScratchResource(scratchKey2); | 608 GrGpuResource* find2 = cache2->findAndRefScratchResource(scratchKey2); |
566 REPORTER_ASSERT(reporter, find2 != NULL); | 609 REPORTER_ASSERT(reporter, find2 != NULL); |
567 REPORTER_ASSERT(reporter, find2 == a || find2 == b); | 610 REPORTER_ASSERT(reporter, find2 == a || find2 == b); |
568 REPORTER_ASSERT(reporter, find2 != find); | 611 REPORTER_ASSERT(reporter, find2 != find); |
569 find2->unref(); | 612 find2->unref(); |
570 find->unref(); | 613 find->unref(); |
571 } | 614 } |
572 | 615 |
573 static void test_duplicate_content_key(skiatest::Reporter* reporter) { | 616 static void test_duplicate_content_key(skiatest::Reporter* reporter) { |
574 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 617 Mock mock(5, 30000); |
575 REPORTER_ASSERT(reporter, SkToBool(context)); | 618 GrContext* context = mock.context(); |
576 if (NULL == context) { | 619 GrResourceCache2* cache2 = mock.cache(); |
577 return; | |
578 } | |
579 context->setResourceCacheLimits(5, 30000); | |
580 GrResourceCache2* cache2 = context->getResourceCache2(); | |
581 cache2->purgeAllUnlocked(); | |
582 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; | |
583 | 620 |
584 GrContentKey key; | 621 GrContentKey key; |
585 make_content_key<0>(&key, 0); | 622 make_content_key<0>(&key, 0); |
586 | 623 |
587 // Create two resources that we will attempt to register with the same conte
nt key. | 624 // Create two resources that we will attempt to register with the same conte
nt key. |
588 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 625 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
589 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 626 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
590 a->setSize(11); | 627 a->setSize(11); |
591 b->setSize(12); | 628 b->setSize(12); |
592 | 629 |
(...skipping 24 matching lines...) Expand all Loading... |
617 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache2->getResourceBytes()); | 654 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache2->getResourceBytes()); |
618 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | 655 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
619 | 656 |
620 cache2->purgeAllUnlocked(); | 657 cache2->purgeAllUnlocked(); |
621 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 658 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
622 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); | 659 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
623 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 660 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
624 } | 661 } |
625 | 662 |
626 static void test_purge_invalidated(skiatest::Reporter* reporter) { | 663 static void test_purge_invalidated(skiatest::Reporter* reporter) { |
627 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 664 Mock mock(5, 30000); |
628 REPORTER_ASSERT(reporter, SkToBool(context)); | 665 GrContext* context = mock.context(); |
629 if (NULL == context) { | 666 GrResourceCache2* cache2 = mock.cache(); |
630 return; | |
631 } | |
632 | 667 |
633 GrContentKey key1, key2, key3; | 668 GrContentKey key1, key2, key3; |
634 make_content_key<0>(&key1, 1); | 669 make_content_key<0>(&key1, 1); |
635 make_content_key<0>(&key2, 2); | 670 make_content_key<0>(&key2, 2); |
636 make_content_key<0>(&key3, 3); | 671 make_content_key<0>(&key3, 3); |
637 | 672 |
638 context->setResourceCacheLimits(5, 30000); | |
639 GrResourceCache2* cache2 = context->getResourceCache2(); | |
640 cache2->purgeAllUnlocked(); | |
641 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; | |
642 | |
643 // Add three resources to the cache. | 673 // Add three resources to the cache. |
644 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 674 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
645 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 675 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
646 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu())); | 676 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu())); |
647 a->cacheAccess().setContentKey(key1); | 677 a->cacheAccess().setContentKey(key1); |
648 b->cacheAccess().setContentKey(key2); | 678 b->cacheAccess().setContentKey(key2); |
649 c->cacheAccess().setContentKey(key3); | 679 c->cacheAccess().setContentKey(key3); |
650 a->unref(); | 680 a->unref(); |
651 b->unref(); | 681 b->unref(); |
652 c->unref(); | 682 c->unref(); |
(...skipping 25 matching lines...) Expand all Loading... |
678 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key3)); | 708 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key3)); |
679 #endif | 709 #endif |
680 | 710 |
681 cache2->purgeAllUnlocked(); | 711 cache2->purgeAllUnlocked(); |
682 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 712 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
683 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 713 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
684 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); | 714 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
685 } | 715 } |
686 | 716 |
687 static void test_cache_chained_purge(skiatest::Reporter* reporter) { | 717 static void test_cache_chained_purge(skiatest::Reporter* reporter) { |
688 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 718 Mock mock(3, 30000); |
689 REPORTER_ASSERT(reporter, SkToBool(context)); | 719 GrContext* context = mock.context(); |
690 if (NULL == context) { | 720 GrResourceCache2* cache2 = mock.cache(); |
691 return; | |
692 } | |
693 | 721 |
694 GrContentKey key1, key2; | 722 GrContentKey key1, key2; |
695 make_content_key<0>(&key1, 1); | 723 make_content_key<0>(&key1, 1); |
696 make_content_key<0>(&key2, 2); | 724 make_content_key<0>(&key2, 2); |
697 | 725 |
698 { | |
699 context->setResourceCacheLimits(3, 30000); | |
700 GrResourceCache2* cache2 = context->getResourceCache2(); | |
701 cache2->purgeAllUnlocked(); | |
702 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceByte
s()); | |
703 | 726 |
704 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 727 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
705 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 728 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
706 a->cacheAccess().setContentKey(key1); | 729 a->cacheAccess().setContentKey(key1); |
707 b->cacheAccess().setContentKey(key2); | 730 b->cacheAccess().setContentKey(key2); |
708 | 731 |
709 // Make a cycle | 732 // Make a cycle |
710 a->setUnrefWhenDestroyed(b); | 733 a->setUnrefWhenDestroyed(b); |
711 b->setUnrefWhenDestroyed(a); | 734 b->setUnrefWhenDestroyed(a); |
712 | 735 |
713 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 736 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
714 | 737 |
715 a->unref(); | 738 a->unref(); |
716 b->unref(); | 739 b->unref(); |
717 | 740 |
718 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 741 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
719 | 742 |
720 cache2->purgeAllUnlocked(); | 743 cache2->purgeAllUnlocked(); |
721 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 744 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
722 | 745 |
723 // Break the cycle | 746 // Break the cycle |
724 a->setUnrefWhenDestroyed(NULL); | 747 a->setUnrefWhenDestroyed(NULL); |
725 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 748 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
726 | 749 |
727 cache2->purgeAllUnlocked(); | 750 cache2->purgeAllUnlocked(); |
728 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 751 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
729 } | |
730 } | 752 } |
731 | 753 |
732 static void test_resource_size_changed(skiatest::Reporter* reporter) { | 754 static void test_resource_size_changed(skiatest::Reporter* reporter) { |
733 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | |
734 REPORTER_ASSERT(reporter, SkToBool(context)); | |
735 if (NULL == context) { | |
736 return; | |
737 } | |
738 | |
739 GrContentKey key1, key2; | 755 GrContentKey key1, key2; |
740 make_content_key<0>(&key1, 1); | 756 make_content_key<0>(&key1, 1); |
741 make_content_key<0>(&key2, 2); | 757 make_content_key<0>(&key2, 2); |
742 | 758 |
743 // Test changing resources sizes (both increase & decrease). | 759 // Test changing resources sizes (both increase & decrease). |
744 { | 760 { |
745 context->setResourceCacheLimits(3, 30000); | 761 Mock mock(3, 30000); |
746 GrResourceCache2* cache2 = context->getResourceCache2(); | 762 GrContext* context = mock.context(); |
747 cache2->purgeAllUnlocked(); | 763 GrResourceCache2* cache2 = mock.cache(); |
748 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceByte
s()); | |
749 | 764 |
750 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 765 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
751 a->cacheAccess().setContentKey(key1); | 766 a->cacheAccess().setContentKey(key1); |
752 a->unref(); | 767 a->unref(); |
753 | 768 |
754 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 769 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
755 b->cacheAccess().setContentKey(key2); | 770 b->cacheAccess().setContentKey(key2); |
756 b->unref(); | 771 b->unref(); |
757 | 772 |
758 REPORTER_ASSERT(reporter, 200 == cache2->getResourceBytes()); | 773 REPORTER_ASSERT(reporter, 200 == cache2->getResourceBytes()); |
759 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 774 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
760 { | 775 { |
761 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2->
findAndRefContentResource(key2))); | 776 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2->
findAndRefContentResource(key2))); |
762 find2->setSize(200); | 777 find2->setSize(200); |
763 SkAutoTUnref<TestResource> find1(static_cast<TestResource*>(cache2->
findAndRefContentResource(key1))); | 778 SkAutoTUnref<TestResource> find1(static_cast<TestResource*>(cache2->
findAndRefContentResource(key1))); |
764 find1->setSize(50); | 779 find1->setSize(50); |
765 } | 780 } |
766 | 781 |
767 REPORTER_ASSERT(reporter, 250 == cache2->getResourceBytes()); | 782 REPORTER_ASSERT(reporter, 250 == cache2->getResourceBytes()); |
768 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 783 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
769 } | 784 } |
770 | 785 |
771 // Test increasing a resources size beyond the cache budget. | 786 // Test increasing a resources size beyond the cache budget. |
772 { | 787 { |
773 context->setResourceCacheLimits(2, 300); | 788 Mock mock(2, 300); |
774 GrResourceCache2* cache2 = context->getResourceCache2(); | 789 GrContext* context = mock.context(); |
775 cache2->purgeAllUnlocked(); | 790 GrResourceCache2* cache2 = mock.cache(); |
776 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceByte
s()); | |
777 | 791 |
778 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 792 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
779 a->setSize(100); | 793 a->setSize(100); |
780 a->cacheAccess().setContentKey(key1); | 794 a->cacheAccess().setContentKey(key1); |
781 a->unref(); | 795 a->unref(); |
782 | 796 |
783 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 797 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
784 b->setSize(100); | 798 b->setSize(100); |
785 b->cacheAccess().setContentKey(key2); | 799 b->cacheAccess().setContentKey(key2); |
786 b->unref(); | 800 b->unref(); |
787 | 801 |
788 REPORTER_ASSERT(reporter, 200 == cache2->getResourceBytes()); | 802 REPORTER_ASSERT(reporter, 200 == cache2->getResourceBytes()); |
789 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 803 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
790 | 804 |
791 { | 805 { |
792 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2->
findAndRefContentResource(key2))); | 806 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2->
findAndRefContentResource(key2))); |
793 find2->setSize(201); | 807 find2->setSize(201); |
794 } | 808 } |
795 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1)); | 809 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1)); |
796 | 810 |
797 REPORTER_ASSERT(reporter, 201 == cache2->getResourceBytes()); | 811 REPORTER_ASSERT(reporter, 201 == cache2->getResourceBytes()); |
798 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 812 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
799 } | 813 } |
800 } | 814 } |
801 | 815 |
802 static void test_large_resource_count(skiatest::Reporter* reporter) { | 816 static void test_large_resource_count(skiatest::Reporter* reporter) { |
803 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | |
804 REPORTER_ASSERT(reporter, SkToBool(context)); | |
805 if (NULL == context) { | |
806 return; | |
807 } | |
808 | |
809 static const int kResourceCnt = 2000; | |
810 // Set the cache size to double the resource count because we're going to cr
eate 2x that number | 817 // Set the cache size to double the resource count because we're going to cr
eate 2x that number |
811 // resources, using two different key domains. Add a little slop to the byte
s because we resize | 818 // resources, using two different key domains. Add a little slop to the byte
s because we resize |
812 // down to 1 byte after creating the resource. | 819 // down to 1 byte after creating the resource. |
813 context->setResourceCacheLimits(2 * kResourceCnt, 2 * kResourceCnt + 1000); | 820 static const int kResourceCnt = 2000; |
814 GrResourceCache2* cache2 = context->getResourceCache2(); | |
815 cache2->purgeAllUnlocked(); | |
816 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; | |
817 | 821 |
| 822 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000); |
| 823 GrContext* context = mock.context(); |
| 824 GrResourceCache2* cache2 = mock.cache(); |
818 | 825 |
819 for (int i = 0; i < kResourceCnt; ++i) { | 826 for (int i = 0; i < kResourceCnt; ++i) { |
820 GrContentKey key1, key2; | 827 GrContentKey key1, key2; |
821 make_content_key<1>(&key1, i); | 828 make_content_key<1>(&key1, i); |
822 make_content_key<2>(&key2, i); | 829 make_content_key<2>(&key2, i); |
823 | 830 |
824 TestResource* resource; | 831 TestResource* resource; |
825 | 832 |
826 resource = SkNEW_ARGS(TestResource, (context->getGpu())); | 833 resource = SkNEW_ARGS(TestResource, (context->getGpu())); |
827 resource->cacheAccess().setContentKey(key1); | 834 resource->cacheAccess().setContentKey(key1); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
885 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight); | 892 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight); |
886 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, | 893 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, |
887 SkSurface::kN
o_Budgeted, info)); | 894 SkSurface::kN
o_Budgeted, info)); |
888 test_cache(reporter, context, surface->getCanvas()); | 895 test_cache(reporter, context, surface->getCanvas()); |
889 } | 896 } |
890 | 897 |
891 // The below tests create their own mock contexts. | 898 // The below tests create their own mock contexts. |
892 test_no_key(reporter); | 899 test_no_key(reporter); |
893 test_budgeting(reporter); | 900 test_budgeting(reporter); |
894 test_unbudgeted(reporter); | 901 test_unbudgeted(reporter); |
| 902 test_unbudgeted_to_scratch(reporter); |
895 test_duplicate_content_key(reporter); | 903 test_duplicate_content_key(reporter); |
896 test_duplicate_scratch_key(reporter); | 904 test_duplicate_scratch_key(reporter); |
897 test_remove_scratch_key(reporter); | 905 test_remove_scratch_key(reporter); |
898 test_scratch_key_consistency(reporter); | 906 test_scratch_key_consistency(reporter); |
899 test_purge_invalidated(reporter); | 907 test_purge_invalidated(reporter); |
900 test_cache_chained_purge(reporter); | 908 test_cache_chained_purge(reporter); |
901 test_resource_size_changed(reporter); | 909 test_resource_size_changed(reporter); |
902 test_large_resource_count(reporter); | 910 test_large_resource_count(reporter); |
903 } | 911 } |
904 | 912 |
905 #endif | 913 #endif |
OLD | NEW |