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 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 | 238 |
239 GrUniqueKey uniqueKey; | 239 GrUniqueKey uniqueKey; |
240 make_unique_key<0>(&uniqueKey, 0); | 240 make_unique_key<0>(&uniqueKey, 0); |
241 | 241 |
242 // Create a scratch, a unique, and a wrapped resource | 242 // Create a scratch, a unique, and a wrapped resource |
243 TestResource* scratch = | 243 TestResource* scratch = |
244 TestResource::CreateScratch(context->getGpu(), TestResource::kB_Simu
latedProperty); | 244 TestResource::CreateScratch(context->getGpu(), TestResource::kB_Simu
latedProperty); |
245 scratch->setSize(10); | 245 scratch->setSize(10); |
246 TestResource* unique = SkNEW_ARGS(TestResource, (context->getGpu())); | 246 TestResource* unique = SkNEW_ARGS(TestResource, (context->getGpu())); |
247 unique->setSize(11); | 247 unique->setSize(11); |
248 REPORTER_ASSERT(reporter, unique->resourcePriv().setUniqueKey(uniqueKey)); | 248 unique->resourcePriv().setUniqueKey(uniqueKey); |
249 TestResource* wrapped = SkNEW_ARGS(TestResource, | 249 TestResource* wrapped = SkNEW_ARGS(TestResource, |
250 (context->getGpu(), GrGpuResource::kWrapp
ed_LifeCycle)); | 250 (context->getGpu(), GrGpuResource::kWrapp
ed_LifeCycle)); |
251 wrapped->setSize(12); | 251 wrapped->setSize(12); |
252 TestResource* unbudgeted = SkNEW_ARGS(TestResource, | 252 TestResource* unbudgeted = SkNEW_ARGS(TestResource, |
253 (context->getGpu(), GrGpuResource::kUn
cached_LifeCycle)); | 253 (context->getGpu(), GrGpuResource::kUn
cached_LifeCycle)); |
254 unbudgeted->setSize(13); | 254 unbudgeted->setSize(13); |
255 | 255 |
256 // Make sure we can't add a unique key to the wrapped resource | 256 // Make sure we can't add a unique key to the wrapped resource |
257 GrUniqueKey uniqueKey2; | 257 GrUniqueKey uniqueKey2; |
258 make_unique_key<0>(&uniqueKey2, 1); | 258 make_unique_key<0>(&uniqueKey2, 1); |
259 REPORTER_ASSERT(reporter, !wrapped->resourcePriv().setUniqueKey(uniqueKey2))
; | 259 wrapped->resourcePriv().setUniqueKey(uniqueKey2); |
260 REPORTER_ASSERT(reporter, NULL == cache->findAndRefUniqueResource(uniqueKey2
)); | 260 REPORTER_ASSERT(reporter, NULL == cache->findAndRefUniqueResource(uniqueKey2
)); |
261 | 261 |
262 // Make sure sizes are as we expect | 262 // Make sure sizes are as we expect |
263 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); | 263 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); |
264 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize()
+ | 264 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize()
+ |
265 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySi
ze() == | 265 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySi
ze() == |
266 cache->getResourceBytes()); | 266 cache->getResourceBytes()); |
267 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); | 267 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
268 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize()
== | 268 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize()
== |
269 cache->getBudgetedResourceBytes()); | 269 cache->getBudgetedResourceBytes()); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_Si
mulatedProperty); | 333 scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_Si
mulatedProperty); |
334 scratch->setSize(10); | 334 scratch->setSize(10); |
335 scratch->unref(); | 335 scratch->unref(); |
336 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); | 336 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
337 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes()); | 337 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes()); |
338 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); | 338 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); |
339 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes()); | 339 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes()); |
340 | 340 |
341 unique = SkNEW_ARGS(TestResource, (context->getGpu())); | 341 unique = SkNEW_ARGS(TestResource, (context->getGpu())); |
342 unique->setSize(11); | 342 unique->setSize(11); |
343 REPORTER_ASSERT(reporter, unique->resourcePriv().setUniqueKey(uniqueKey)); | 343 unique->resourcePriv().setUniqueKey(uniqueKey); |
344 unique->unref(); | 344 unique->unref(); |
345 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); | 345 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
346 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); | 346 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); |
347 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); | 347 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
348 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); | 348 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
349 | 349 |
350 size_t large = 2 * cache->getResourceBytes(); | 350 size_t large = 2 * cache->getResourceBytes(); |
351 unbudgeted = SkNEW_ARGS(TestResource, | 351 unbudgeted = SkNEW_ARGS(TestResource, |
352 (context->getGpu(), large, GrGpuResource::kUncached_
LifeCycle)); | 352 (context->getGpu(), large, GrGpuResource::kUncached_
LifeCycle)); |
353 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); | 353 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 GrResourceCache* cache = mock.cache(); | 493 GrResourceCache* cache = mock.cache(); |
494 | 494 |
495 // Create two resources that have the same scratch key. | 495 // Create two resources that have the same scratch key. |
496 TestResource* a = TestResource::CreateScratch(context->getGpu(), | 496 TestResource* a = TestResource::CreateScratch(context->getGpu(), |
497 TestResource::kB_SimulatedProp
erty); | 497 TestResource::kB_SimulatedProp
erty); |
498 TestResource* b = TestResource::CreateScratch(context->getGpu(), | 498 TestResource* b = TestResource::CreateScratch(context->getGpu(), |
499 TestResource::kB_SimulatedProp
erty); | 499 TestResource::kB_SimulatedProp
erty); |
500 a->unref(); | 500 a->unref(); |
501 b->unref(); | 501 b->unref(); |
502 | 502 |
503 | |
504 GrScratchKey scratchKey; | 503 GrScratchKey scratchKey; |
505 // Ensure that scratch key lookup is correct for negative case. | 504 // Ensure that scratch key lookup is correct for negative case. |
506 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratch
Key); | 505 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratch
Key); |
507 // (following leaks upon test failure). | 506 // (following leaks upon test failure). |
508 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NU
LL); | 507 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NU
LL); |
509 | 508 |
510 // Scratch resources are registered with GrResourceCache just by existing. T
here are 2. | 509 // Scratch resources are registered with GrResourceCache just by existing. T
here are 2. |
511 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratch
Key); | 510 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratch
Key); |
512 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 511 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
513 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(
scratchKey));) | 512 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(
scratchKey));) |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 static void test_duplicate_unique_key(skiatest::Reporter* reporter) { | 608 static void test_duplicate_unique_key(skiatest::Reporter* reporter) { |
610 Mock mock(5, 30000); | 609 Mock mock(5, 30000); |
611 GrContext* context = mock.context(); | 610 GrContext* context = mock.context(); |
612 GrResourceCache* cache = mock.cache(); | 611 GrResourceCache* cache = mock.cache(); |
613 | 612 |
614 GrUniqueKey key; | 613 GrUniqueKey key; |
615 make_unique_key<0>(&key, 0); | 614 make_unique_key<0>(&key, 0); |
616 | 615 |
617 // Create two resources that we will attempt to register with the same uniqu
e key. | 616 // Create two resources that we will attempt to register with the same uniqu
e key. |
618 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 617 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
619 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | |
620 a->setSize(11); | 618 a->setSize(11); |
621 b->setSize(12); | |
622 | 619 |
623 // Can't set the same unique key on two resources. | 620 // Set key on resource a. |
624 REPORTER_ASSERT(reporter, a->resourcePriv().setUniqueKey(key)); | 621 a->resourcePriv().setUniqueKey(key); |
625 REPORTER_ASSERT(reporter, !b->resourcePriv().setUniqueKey(key)); | 622 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key)); |
| 623 a->unref(); |
626 | 624 |
627 // Still have two resources because b is still reffed. | 625 // Make sure that redundantly setting a's key works. |
628 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); | 626 a->resourcePriv().setUniqueKey(key); |
629 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == | 627 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key)); |
630 cache->getResourceBytes()); | |
631 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | |
632 | |
633 b->unref(); | |
634 // Now b should be gone. | |
635 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); | |
636 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes()); | |
637 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | |
638 | |
639 cache->purgeAllUnlocked(); | |
640 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); | |
641 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes()); | |
642 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | |
643 | |
644 // Drop the ref on a but it isn't immediately purged as it still has a valid
scratch key. | |
645 a->unref(); | 628 a->unref(); |
646 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); | 629 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
647 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes()); | 630 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes()); |
648 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | 631 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
649 | 632 |
| 633 // Create resource b and set the same key. It should replace a's unique key
cache entry. |
| 634 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 635 b->setSize(12); |
| 636 b->resourcePriv().setUniqueKey(key); |
| 637 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key)); |
| 638 b->unref(); |
| 639 |
| 640 // Still have two resources because a is still reffed. |
| 641 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 642 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->
getResourceBytes()); |
| 643 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 644 |
| 645 a->unref(); |
| 646 // Now a should be gone. |
| 647 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 648 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes()); |
| 649 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 650 |
| 651 // Now replace b with c, but make sure c can start with one unique key and c
hange it to b's key. |
| 652 // Also make b be unreffed when replacement occurs. |
| 653 b->unref(); |
| 654 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 655 GrUniqueKey differentKey; |
| 656 make_unique_key<0>(&differentKey, 1); |
| 657 c->setSize(13); |
| 658 c->resourcePriv().setUniqueKey(differentKey); |
| 659 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 660 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->
getResourceBytes()); |
| 661 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 662 // c replaces b and b should be immediately purged. |
| 663 c->resourcePriv().setUniqueKey(key); |
| 664 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 665 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes()); |
| 666 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 667 |
| 668 // c shouldn't be purged because it is ref'ed. |
650 cache->purgeAllUnlocked(); | 669 cache->purgeAllUnlocked(); |
| 670 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 671 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes()); |
| 672 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 673 |
| 674 // Drop the ref on c, it should be kept alive because it has a unique key. |
| 675 c->unref(); |
| 676 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 677 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes()); |
| 678 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 679 |
| 680 // Verify that we can find c, then remove its unique key. It should get purg
ed immediately. |
| 681 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key)); |
| 682 c->resourcePriv().removeUniqueKey(); |
| 683 c->unref(); |
651 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); | 684 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
652 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); | 685 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
653 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 686 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
654 } | 687 } |
655 | 688 |
656 static void test_purge_invalidated(skiatest::Reporter* reporter) { | 689 static void test_purge_invalidated(skiatest::Reporter* reporter) { |
657 Mock mock(5, 30000); | 690 Mock mock(5, 30000); |
658 GrContext* context = mock.context(); | 691 GrContext* context = mock.context(); |
659 GrResourceCache* cache = mock.cache(); | 692 GrResourceCache* cache = mock.cache(); |
660 | 693 |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
914 test_duplicate_scratch_key(reporter); | 947 test_duplicate_scratch_key(reporter); |
915 test_remove_scratch_key(reporter); | 948 test_remove_scratch_key(reporter); |
916 test_scratch_key_consistency(reporter); | 949 test_scratch_key_consistency(reporter); |
917 test_purge_invalidated(reporter); | 950 test_purge_invalidated(reporter); |
918 test_cache_chained_purge(reporter); | 951 test_cache_chained_purge(reporter); |
919 test_resource_size_changed(reporter); | 952 test_resource_size_changed(reporter); |
920 test_large_resource_count(reporter); | 953 test_large_resource_count(reporter); |
921 } | 954 } |
922 | 955 |
923 #endif | 956 #endif |
OLD | NEW |