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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes); | 61 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes); |
62 } | 62 } |
63 | 63 |
64 class TestResource : public GrGpuResource { | 64 class TestResource : public GrGpuResource { |
65 static const size_t kDefaultSize = 100; | 65 static const size_t kDefaultSize = 100; |
66 enum ScratchConstructor { kScratchConstructor }; | 66 enum ScratchConstructor { kScratchConstructor }; |
67 public: | 67 public: |
68 SK_DECLARE_INST_COUNT(TestResource); | 68 SK_DECLARE_INST_COUNT(TestResource); |
69 /** Property that distinctly categorizes the resource. | 69 /** Property that distinctly categorizes the resource. |
70 * For example, textures have width, height, ... */ | 70 * For example, textures have width, height, ... */ |
71 enum SimulatedProperty { kProperty1_SimulatedProperty, kProperty2_SimulatedP
roperty }; | 71 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty }; |
72 | 72 |
73 TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle) | 73 TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle) |
74 : INHERITED(gpu, lifeCycle) | 74 : INHERITED(gpu, lifeCycle) |
75 , fToDelete(NULL) | 75 , fToDelete(NULL) |
76 , fSize(size) | 76 , fSize(size) |
77 , fProperty(kProperty1_SimulatedProperty) { | 77 , fProperty(kA_SimulatedProperty) { |
78 ++fNumAlive; | 78 ++fNumAlive; |
79 this->registerWithCache(); | 79 this->registerWithCache(); |
80 } | 80 } |
81 | 81 |
82 TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle) | 82 TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle) |
83 : INHERITED(gpu, lifeCycle) | 83 : INHERITED(gpu, lifeCycle) |
84 , fToDelete(NULL) | 84 , fToDelete(NULL) |
85 , fSize(kDefaultSize) | 85 , fSize(kDefaultSize) |
86 , fProperty(kProperty1_SimulatedProperty) { | 86 , fProperty(kA_SimulatedProperty) { |
87 ++fNumAlive; | 87 ++fNumAlive; |
88 this->registerWithCache(); | 88 this->registerWithCache(); |
89 } | 89 } |
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(kA_SimulatedProperty) { |
96 ++fNumAlive; | 96 ++fNumAlive; |
97 this->registerWithCache(); | 97 this->registerWithCache(); |
98 } | 98 } |
99 | 99 |
100 static TestResource* CreateScratchTestResource(GrGpu* gpu, | 100 static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, b
ool cached = true) { |
101 SimulatedProperty property, | |
102 bool cached = true) { | |
103 return SkNEW_ARGS(TestResource, (gpu, property, cached, kScratchConstruc
tor)); | 101 return SkNEW_ARGS(TestResource, (gpu, property, cached, kScratchConstruc
tor)); |
104 } | 102 } |
105 | 103 |
106 ~TestResource() { | 104 ~TestResource() { |
107 --fNumAlive; | 105 --fNumAlive; |
108 SkSafeUnref(fToDelete); | 106 SkSafeUnref(fToDelete); |
109 } | 107 } |
110 | 108 |
111 void setSize(size_t size) { | 109 void setSize(size_t size) { |
112 fSize = size; | 110 fSize = size; |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 static void test_budgeting(skiatest::Reporter* reporter) { | 232 static void test_budgeting(skiatest::Reporter* reporter) { |
235 Mock mock(10, 300); | 233 Mock mock(10, 300); |
236 GrContext* context = mock.context(); | 234 GrContext* context = mock.context(); |
237 GrResourceCache2* cache2 = mock.cache(); | 235 GrResourceCache2* cache2 = mock.cache(); |
238 | 236 |
239 GrContentKey contentKey; | 237 GrContentKey contentKey; |
240 make_content_key<0>(&contentKey, 0); | 238 make_content_key<0>(&contentKey, 0); |
241 | 239 |
242 // Create a scratch, a content, and a wrapped resource | 240 // Create a scratch, a content, and a wrapped resource |
243 TestResource* scratch = | 241 TestResource* scratch = |
244 TestResource::CreateScratchTestResource(context->getGpu(), | 242 TestResource::CreateScratch(context->getGpu(), TestResource::kB_Simu
latedProperty); |
245 TestResource::kProperty2_Sim
ulatedProperty); | |
246 scratch->setSize(10); | 243 scratch->setSize(10); |
247 TestResource* content = SkNEW_ARGS(TestResource, (context->getGpu())); | 244 TestResource* content = SkNEW_ARGS(TestResource, (context->getGpu())); |
248 content->setSize(11); | 245 content->setSize(11); |
249 REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey)); | 246 REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey)); |
250 TestResource* wrapped = SkNEW_ARGS(TestResource, | 247 TestResource* wrapped = SkNEW_ARGS(TestResource, |
251 (context->getGpu(), GrGpuResource::kWrapp
ed_LifeCycle)); | 248 (context->getGpu(), GrGpuResource::kWrapp
ed_LifeCycle)); |
252 wrapped->setSize(12); | 249 wrapped->setSize(12); |
253 TestResource* unbudgeted = SkNEW_ARGS(TestResource, | 250 TestResource* unbudgeted = SkNEW_ARGS(TestResource, |
254 (context->getGpu(), GrGpuResource::kUn
cached_LifeCycle)); | 251 (context->getGpu(), GrGpuResource::kUn
cached_LifeCycle)); |
255 unbudgeted->setSize(13); | 252 unbudgeted->setSize(13); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 | 321 |
325 GrContentKey contentKey; | 322 GrContentKey contentKey; |
326 make_content_key<0>(&contentKey, 0); | 323 make_content_key<0>(&contentKey, 0); |
327 | 324 |
328 TestResource* scratch; | 325 TestResource* scratch; |
329 TestResource* content; | 326 TestResource* content; |
330 TestResource* wrapped; | 327 TestResource* wrapped; |
331 TestResource* unbudgeted; | 328 TestResource* unbudgeted; |
332 | 329 |
333 // A large uncached or wrapped resource shouldn't evict anything. | 330 // A large uncached or wrapped resource shouldn't evict anything. |
334 scratch = TestResource::CreateScratchTestResource(context->getGpu(), | 331 scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_Si
mulatedProperty); |
335 TestResource::kProperty2_S
imulatedProperty); | |
336 scratch->setSize(10); | 332 scratch->setSize(10); |
337 scratch->unref(); | 333 scratch->unref(); |
338 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 334 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
339 REPORTER_ASSERT(reporter, 10 == cache2->getResourceBytes()); | 335 REPORTER_ASSERT(reporter, 10 == cache2->getResourceBytes()); |
340 REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount()); | 336 REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount()); |
341 REPORTER_ASSERT(reporter, 10 == cache2->getBudgetedResourceBytes()); | 337 REPORTER_ASSERT(reporter, 10 == cache2->getBudgetedResourceBytes()); |
342 | 338 |
343 content = SkNEW_ARGS(TestResource, (context->getGpu())); | 339 content = SkNEW_ARGS(TestResource, (context->getGpu())); |
344 content->setSize(11); | 340 content->setSize(11); |
345 REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey)); | 341 REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey)); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); | 378 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); |
383 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); | 379 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
384 } | 380 } |
385 | 381 |
386 static void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) { | 382 static void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) { |
387 Mock mock(10, 300); | 383 Mock mock(10, 300); |
388 GrContext* context = mock.context(); | 384 GrContext* context = mock.context(); |
389 GrResourceCache2* cache2 = mock.cache(); | 385 GrResourceCache2* cache2 = mock.cache(); |
390 | 386 |
391 TestResource* resource = | 387 TestResource* resource = |
392 TestResource::CreateScratchTestResource(context->getGpu(), | 388 TestResource::CreateScratch(context->getGpu(), TestResource::kA_Simulate
dProperty, false); |
393 TestResource::kProperty1_Simulat
edProperty, false); | |
394 GrScratchKey key; | 389 GrScratchKey key; |
395 TestResource::ComputeScratchKey(TestResource::kProperty1_SimulatedProperty,
&key); | 390 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key); |
396 | 391 |
397 size_t size = resource->gpuMemorySize(); | 392 size_t size = resource->gpuMemorySize(); |
398 for (int i = 0; i < 2; ++i) { | 393 for (int i = 0; i < 2; ++i) { |
399 // Since this resource is unbudgeted, it should not be reachable as scra
tch. | 394 // Since this resource is unbudgeted, it should not be reachable as scra
tch. |
400 REPORTER_ASSERT(reporter, resource->cacheAccess().getScratchKey() == key
); | 395 REPORTER_ASSERT(reporter, resource->cacheAccess().getScratchKey() == key
); |
401 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch()); | 396 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch()); |
402 REPORTER_ASSERT(reporter, !resource->cacheAccess().isBudgeted()); | 397 REPORTER_ASSERT(reporter, !resource->cacheAccess().isBudgeted()); |
403 REPORTER_ASSERT(reporter, NULL == cache2->findAndRefScratchResource(key)
); | 398 REPORTER_ASSERT(reporter, NULL == cache2->findAndRefScratchResource(key)
); |
404 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 399 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
405 REPORTER_ASSERT(reporter, size == cache2->getResourceBytes()); | 400 REPORTER_ASSERT(reporter, size == cache2->getResourceBytes()); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 } | 437 } |
443 } | 438 } |
444 } | 439 } |
445 | 440 |
446 static void test_duplicate_scratch_key(skiatest::Reporter* reporter) { | 441 static void test_duplicate_scratch_key(skiatest::Reporter* reporter) { |
447 Mock mock(5, 30000); | 442 Mock mock(5, 30000); |
448 GrContext* context = mock.context(); | 443 GrContext* context = mock.context(); |
449 GrResourceCache2* cache2 = mock.cache(); | 444 GrResourceCache2* cache2 = mock.cache(); |
450 | 445 |
451 // Create two resources that have the same scratch key. | 446 // Create two resources that have the same scratch key. |
452 TestResource* a = | 447 TestResource* a = TestResource::CreateScratch(context->getGpu(), |
453 TestResource::CreateScratchTestResource(context->getGpu(), | 448 TestResource::kB_SimulatedProp
erty); |
454 TestResource::kProperty2_Sim
ulatedProperty); | 449 TestResource* b = TestResource::CreateScratch(context->getGpu(), |
455 TestResource* b = | 450 TestResource::kB_SimulatedProp
erty); |
456 TestResource::CreateScratchTestResource(context->getGpu(), | |
457 TestResource::kProperty2_Sim
ulatedProperty); | |
458 a->setSize(11); | 451 a->setSize(11); |
459 b->setSize(12); | 452 b->setSize(12); |
460 GrScratchKey scratchKey1; | 453 GrScratchKey scratchKey1; |
461 TestResource::ComputeScratchKey(TestResource::kProperty1_SimulatedProperty,
&scratchKey1); | 454 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratch
Key1); |
462 // Check for negative case consistency. (leaks upon test failure.) | 455 // Check for negative case consistency. (leaks upon test failure.) |
463 REPORTER_ASSERT(reporter, NULL == cache2->findAndRefScratchResource(scratchK
ey1)); | 456 REPORTER_ASSERT(reporter, NULL == cache2->findAndRefScratchResource(scratchK
ey1)); |
464 | 457 |
465 GrScratchKey scratchKey; | 458 GrScratchKey scratchKey; |
466 TestResource::ComputeScratchKey(TestResource::kProperty2_SimulatedProperty,
&scratchKey); | 459 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratch
Key); |
467 | 460 |
468 // Scratch resources are registered with GrResourceCache2 just by existing.
There are 2. | 461 // Scratch resources are registered with GrResourceCache2 just by existing.
There are 2. |
469 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 462 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
470 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey
(scratchKey));) | 463 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey
(scratchKey));) |
471 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 464 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
472 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == | 465 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == |
473 cache2->getResourceBytes()); | 466 cache2->getResourceBytes()); |
474 | 467 |
475 // Our refs mean that the resources are non purgeable. | 468 // Our refs mean that the resources are non purgeable. |
476 cache2->purgeAllUnlocked(); | 469 cache2->purgeAllUnlocked(); |
(...skipping 12 matching lines...) Expand all Loading... |
489 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 482 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
490 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey
(scratchKey));) | 483 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey
(scratchKey));) |
491 } | 484 } |
492 | 485 |
493 static void test_remove_scratch_key(skiatest::Reporter* reporter) { | 486 static void test_remove_scratch_key(skiatest::Reporter* reporter) { |
494 Mock mock(5, 30000); | 487 Mock mock(5, 30000); |
495 GrContext* context = mock.context(); | 488 GrContext* context = mock.context(); |
496 GrResourceCache2* cache2 = mock.cache(); | 489 GrResourceCache2* cache2 = mock.cache(); |
497 | 490 |
498 // Create two resources that have the same scratch key. | 491 // Create two resources that have the same scratch key. |
499 TestResource* a = | 492 TestResource* a = TestResource::CreateScratch(context->getGpu(), |
500 TestResource::CreateScratchTestResource(context->getGpu(), | 493 TestResource::kB_SimulatedProp
erty); |
501 TestResource::kProperty2_Sim
ulatedProperty); | 494 TestResource* b = TestResource::CreateScratch(context->getGpu(), |
502 TestResource* b = | 495 TestResource::kB_SimulatedProp
erty); |
503 TestResource::CreateScratchTestResource(context->getGpu(), | |
504 TestResource::kProperty2_Sim
ulatedProperty); | |
505 a->unref(); | 496 a->unref(); |
506 b->unref(); | 497 b->unref(); |
507 | 498 |
508 | 499 |
509 GrScratchKey scratchKey; | 500 GrScratchKey scratchKey; |
510 // Ensure that scratch key lookup is correct for negative case. | 501 // Ensure that scratch key lookup is correct for negative case. |
511 TestResource::ComputeScratchKey(TestResource::kProperty1_SimulatedProperty,
&scratchKey); | 502 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratch
Key); |
512 // (following leaks upon test failure). | 503 // (following leaks upon test failure). |
513 REPORTER_ASSERT(reporter, cache2->findAndRefScratchResource(scratchKey) == N
ULL); | 504 REPORTER_ASSERT(reporter, cache2->findAndRefScratchResource(scratchKey) == N
ULL); |
514 | 505 |
515 // Scratch resources are registered with GrResourceCache2 just by existing.
There are 2. | 506 // Scratch resources are registered with GrResourceCache2 just by existing.
There are 2. |
516 TestResource::ComputeScratchKey(TestResource::kProperty2_SimulatedProperty,
&scratchKey); | 507 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratch
Key); |
517 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 508 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
518 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey
(scratchKey));) | 509 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey
(scratchKey));) |
519 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 510 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
520 | 511 |
521 // Find the first resource and remove its scratch key | 512 // Find the first resource and remove its scratch key |
522 GrGpuResource* find; | 513 GrGpuResource* find; |
523 find = cache2->findAndRefScratchResource(scratchKey); | 514 find = cache2->findAndRefScratchResource(scratchKey); |
524 find->cacheAccess().removeScratchKey(); | 515 find->cacheAccess().removeScratchKey(); |
525 // It's still alive, but not cached by scratch key anymore | 516 // It's still alive, but not cached by scratch key anymore |
526 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 517 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
(...skipping 24 matching lines...) Expand all Loading... |
551 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey
(scratchKey));) | 542 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey
(scratchKey));) |
552 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 543 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
553 } | 544 } |
554 | 545 |
555 static void test_scratch_key_consistency(skiatest::Reporter* reporter) { | 546 static void test_scratch_key_consistency(skiatest::Reporter* reporter) { |
556 Mock mock(5, 30000); | 547 Mock mock(5, 30000); |
557 GrContext* context = mock.context(); | 548 GrContext* context = mock.context(); |
558 GrResourceCache2* cache2 = mock.cache(); | 549 GrResourceCache2* cache2 = mock.cache(); |
559 | 550 |
560 // Create two resources that have the same scratch key. | 551 // Create two resources that have the same scratch key. |
561 TestResource* a = | 552 TestResource* a = TestResource::CreateScratch(context->getGpu(), |
562 TestResource::CreateScratchTestResource(context->getGpu(), | 553 TestResource::kB_SimulatedProp
erty); |
563 TestResource::kProperty2_Sim
ulatedProperty); | 554 TestResource* b = TestResource::CreateScratch(context->getGpu(), |
564 TestResource* b = | 555 TestResource::kB_SimulatedProp
erty); |
565 TestResource::CreateScratchTestResource(context->getGpu(), | |
566 TestResource::kProperty2_Sim
ulatedProperty); | |
567 a->unref(); | 556 a->unref(); |
568 b->unref(); | 557 b->unref(); |
569 | 558 |
570 GrScratchKey scratchKey; | 559 GrScratchKey scratchKey; |
571 // Ensure that scratch key comparison and assignment is consistent. | 560 // Ensure that scratch key comparison and assignment is consistent. |
572 GrScratchKey scratchKey1; | 561 GrScratchKey scratchKey1; |
573 TestResource::ComputeScratchKey(TestResource::kProperty1_SimulatedProperty,
&scratchKey1); | 562 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratch
Key1); |
574 GrScratchKey scratchKey2; | 563 GrScratchKey scratchKey2; |
575 TestResource::ComputeScratchKey(TestResource::kProperty2_SimulatedProperty,
&scratchKey2); | 564 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratch
Key2); |
576 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratc
hKeySize()); | 565 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratc
hKeySize()); |
577 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2); | 566 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2); |
578 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1); | 567 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1); |
579 scratchKey = scratchKey1; | 568 scratchKey = scratchKey1; |
580 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratch
KeySize()); | 569 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratch
KeySize()); |
581 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey); | 570 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey); |
582 REPORTER_ASSERT(reporter, scratchKey == scratchKey1); | 571 REPORTER_ASSERT(reporter, scratchKey == scratchKey1); |
583 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey); | 572 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey); |
584 REPORTER_ASSERT(reporter, scratchKey != scratchKey2); | 573 REPORTER_ASSERT(reporter, scratchKey != scratchKey2); |
585 scratchKey = scratchKey2; | 574 scratchKey = scratchKey2; |
586 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratch
KeySize()); | 575 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratch
KeySize()); |
587 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey); | 576 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey); |
588 REPORTER_ASSERT(reporter, scratchKey != scratchKey1); | 577 REPORTER_ASSERT(reporter, scratchKey != scratchKey1); |
589 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey); | 578 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey); |
590 REPORTER_ASSERT(reporter, scratchKey == scratchKey2); | 579 REPORTER_ASSERT(reporter, scratchKey == scratchKey2); |
591 | 580 |
592 // Ensure that scratch key lookup is correct for negative case. | 581 // Ensure that scratch key lookup is correct for negative case. |
593 TestResource::ComputeScratchKey(TestResource::kProperty1_SimulatedProperty,
&scratchKey); | 582 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratch
Key); |
594 // (following leaks upon test failure). | 583 // (following leaks upon test failure). |
595 REPORTER_ASSERT(reporter, cache2->findAndRefScratchResource(scratchKey) == N
ULL); | 584 REPORTER_ASSERT(reporter, cache2->findAndRefScratchResource(scratchKey) == N
ULL); |
596 | 585 |
597 // Find the first resource with a scratch key and a copy of a scratch key. | 586 // Find the first resource with a scratch key and a copy of a scratch key. |
598 TestResource::ComputeScratchKey(TestResource::kProperty2_SimulatedProperty,
&scratchKey); | 587 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratch
Key); |
599 GrGpuResource* find = cache2->findAndRefScratchResource(scratchKey); | 588 GrGpuResource* find = cache2->findAndRefScratchResource(scratchKey); |
600 REPORTER_ASSERT(reporter, find != NULL); | 589 REPORTER_ASSERT(reporter, find != NULL); |
601 find->unref(); | 590 find->unref(); |
602 | 591 |
603 scratchKey2 = scratchKey; | 592 scratchKey2 = scratchKey; |
604 find = cache2->findAndRefScratchResource(scratchKey2); | 593 find = cache2->findAndRefScratchResource(scratchKey2); |
605 REPORTER_ASSERT(reporter, find != NULL); | 594 REPORTER_ASSERT(reporter, find != NULL); |
606 REPORTER_ASSERT(reporter, find == a || find == b); | 595 REPORTER_ASSERT(reporter, find == a || find == b); |
607 | 596 |
608 GrGpuResource* find2 = cache2->findAndRefScratchResource(scratchKey2); | 597 GrGpuResource* find2 = cache2->findAndRefScratchResource(scratchKey2); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 static void test_purge_invalidated(skiatest::Reporter* reporter) { | 652 static void test_purge_invalidated(skiatest::Reporter* reporter) { |
664 Mock mock(5, 30000); | 653 Mock mock(5, 30000); |
665 GrContext* context = mock.context(); | 654 GrContext* context = mock.context(); |
666 GrResourceCache2* cache2 = mock.cache(); | 655 GrResourceCache2* cache2 = mock.cache(); |
667 | 656 |
668 GrContentKey key1, key2, key3; | 657 GrContentKey key1, key2, key3; |
669 make_content_key<0>(&key1, 1); | 658 make_content_key<0>(&key1, 1); |
670 make_content_key<0>(&key2, 2); | 659 make_content_key<0>(&key2, 2); |
671 make_content_key<0>(&key3, 3); | 660 make_content_key<0>(&key3, 3); |
672 | 661 |
673 // Add three resources to the cache. | 662 // Add three resources to the cache. Only c is usable as scratch. |
674 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 663 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
675 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 664 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
676 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu())); | 665 TestResource* c = TestResource::CreateScratch(context->getGpu(), |
| 666 TestResource::kA_SimulatedProp
erty); |
677 a->cacheAccess().setContentKey(key1); | 667 a->cacheAccess().setContentKey(key1); |
678 b->cacheAccess().setContentKey(key2); | 668 b->cacheAccess().setContentKey(key2); |
679 c->cacheAccess().setContentKey(key3); | 669 c->cacheAccess().setContentKey(key3); |
680 a->unref(); | 670 a->unref(); |
681 b->unref(); | 671 // hold b until *after* the message is sent. |
682 c->unref(); | 672 c->unref(); |
683 | 673 |
684 REPORTER_ASSERT(reporter, cache2->hasContentKey(key1)); | 674 REPORTER_ASSERT(reporter, cache2->hasContentKey(key1)); |
685 REPORTER_ASSERT(reporter, cache2->hasContentKey(key2)); | 675 REPORTER_ASSERT(reporter, cache2->hasContentKey(key2)); |
686 REPORTER_ASSERT(reporter, cache2->hasContentKey(key3)); | 676 REPORTER_ASSERT(reporter, cache2->hasContentKey(key3)); |
| 677 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); |
687 | 678 |
688 // Invalidate two of the three, they should be purged and destroyed. | 679 typedef GrContentKeyInvalidatedMessage Msg; |
689 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); | 680 typedef SkMessageBus<GrContentKeyInvalidatedMessage> Bus; |
690 const GrResourceInvalidatedMessage msg1 = { key1 }; | 681 |
691 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg1); | 682 // Invalidate two of the three, they should be purged and no longer accessib
le via their keys. |
692 const GrResourceInvalidatedMessage msg2 = { key2 }; | 683 Bus::Post(Msg(key1)); |
693 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg2); | 684 Bus::Post(Msg(key2)); |
694 #if 0 // Disabled until reimplemented in GrResourceCache2. | |
695 cache2->purgeAsNeeded(); | 685 cache2->purgeAsNeeded(); |
696 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | 686 // a should be deleted now, but we still have a ref on b. |
697 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1)); | 687 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1)); |
698 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key2)); | 688 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key2)); |
| 689 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
699 REPORTER_ASSERT(reporter, cache2->hasContentKey(key3)); | 690 REPORTER_ASSERT(reporter, cache2->hasContentKey(key3)); |
700 #endif | |
701 | 691 |
702 // Invalidate the third. | 692 // Invalidate the third. |
703 const GrResourceInvalidatedMessage msg3 = { key3 }; | 693 Bus::Post(Msg(key3)); |
704 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg3); | |
705 #if 0 // Disabled until reimplemented in GrResourceCache2. | |
706 cache2->purgeAsNeeded(); | 694 cache2->purgeAsNeeded(); |
707 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 695 // we still have a ref on b, c should be recycled as scratch. |
| 696 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
708 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key3)); | 697 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key3)); |
709 #endif | |
710 | 698 |
| 699 // make b purgeable. It should be immediately deleted since it has no key. |
| 700 b->unref(); |
| 701 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 702 |
| 703 // Make sure we actually get to c via it's scratch key, before we say goodby
e. |
| 704 GrScratchKey scratchKey; |
| 705 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratch
Key); |
| 706 GrGpuResource* scratch = cache2->findAndRefScratchResource(scratchKey); |
| 707 REPORTER_ASSERT(reporter, scratch == c); |
| 708 SkSafeUnref(scratch); |
| 709 |
| 710 // Get rid of c. |
711 cache2->purgeAllUnlocked(); | 711 cache2->purgeAllUnlocked(); |
| 712 scratch = cache2->findAndRefScratchResource(scratchKey); |
712 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 713 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
713 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 714 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
714 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); | 715 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
| 716 REPORTER_ASSERT(reporter, !scratch); |
| 717 SkSafeUnref(scratch); |
715 } | 718 } |
716 | 719 |
717 static void test_cache_chained_purge(skiatest::Reporter* reporter) { | 720 static void test_cache_chained_purge(skiatest::Reporter* reporter) { |
718 Mock mock(3, 30000); | 721 Mock mock(3, 30000); |
719 GrContext* context = mock.context(); | 722 GrContext* context = mock.context(); |
720 GrResourceCache2* cache2 = mock.cache(); | 723 GrResourceCache2* cache2 = mock.cache(); |
721 | 724 |
722 GrContentKey key1, key2; | 725 GrContentKey key1, key2; |
723 make_content_key<0>(&key1, 1); | 726 make_content_key<0>(&key1, 1); |
724 make_content_key<0>(&key2, 2); | 727 make_content_key<0>(&key2, 2); |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
904 test_duplicate_scratch_key(reporter); | 907 test_duplicate_scratch_key(reporter); |
905 test_remove_scratch_key(reporter); | 908 test_remove_scratch_key(reporter); |
906 test_scratch_key_consistency(reporter); | 909 test_scratch_key_consistency(reporter); |
907 test_purge_invalidated(reporter); | 910 test_purge_invalidated(reporter); |
908 test_cache_chained_purge(reporter); | 911 test_cache_chained_purge(reporter); |
909 test_resource_size_changed(reporter); | 912 test_resource_size_changed(reporter); |
910 test_large_resource_count(reporter); | 913 test_large_resource_count(reporter); |
911 } | 914 } |
912 | 915 |
913 #endif | 916 #endif |
OLD | NEW |