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

Side by Side Diff: tests/ResourceCacheTest.cpp

Issue 747043004: Use scratch keys for stencil buffers. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Address comment Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/gpu/gl/GrGpuGL.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 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 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); 306 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
307 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey (scratchKey));) 307 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey (scratchKey));)
308 308
309 // Purge again. This time resources should be purgable. 309 // Purge again. This time resources should be purgable.
310 cache2->purgeAllUnlocked(); 310 cache2->purgeAllUnlocked();
311 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); 311 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
312 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); 312 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount());
313 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey (scratchKey));) 313 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey (scratchKey));)
314 } 314 }
315 315
316 static void test_remove_scratch_key(skiatest::Reporter* reporter) {
317 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
318 REPORTER_ASSERT(reporter, SkToBool(context));
319 if (NULL == context) {
320 return;
321 }
322 context->setResourceCacheLimits(5, 30000);
323 GrResourceCache2* cache2 = context->getResourceCache2();
324 cache2->purgeAllUnlocked();
325 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()) ;
326
327 GrCacheID::Key keyData;
328 memset(&keyData, 0, sizeof(keyData));
329 GrCacheID::Domain domain = GrResourceKey::ScratchDomain();
330 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
331 GrResourceKey scratchKey(GrCacheID(domain, keyData), t, 0);
332
333 // Create two resources that have the same scratch key.
334 TestResource* a = new TestResource(context->getGpu(), scratchKey);
335 TestResource* b = new TestResource(context->getGpu(), scratchKey);
336 a->unref();
337 b->unref();
338
339 // Scratch resources are registered with GrResourceCache2 just by existing. There are 2.
340 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
341 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey (scratchKey));)
342 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount());
343
344 // Find the first resource and remove its scratch key
345 GrGpuResource* find;
346 find = cache2->findAndRefScratchResource(scratchKey);
347 find->cacheAccess().removeScratchKey();
348 // It's still alive, but not cached by scratch key anymore
349 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
350 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache2->countScratchEntriesForKey (scratchKey));)
351 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount());
352
353 // The cache should immediately delete it when it's unrefed since it isn't a ccessible.
354 find->unref();
355 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
356 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache2->countScratchEntriesForKey (scratchKey));)
357 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount());
358
359 // Repeat for the second resource.
360 find = cache2->findAndRefScratchResource(scratchKey);
361 find->cacheAccess().removeScratchKey();
362 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
363 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey (scratchKey));)
364 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount());
365
366 // Should be able to call this multiple times with no problem.
367 find->cacheAccess().removeScratchKey();
368 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
369 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey (scratchKey));)
370 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount());
371
372 find->unref();
373 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
374 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey (scratchKey));)
375 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount());
376 }
377
316 static void test_duplicate_content_key(skiatest::Reporter* reporter) { 378 static void test_duplicate_content_key(skiatest::Reporter* reporter) {
317 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); 379 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
318 REPORTER_ASSERT(reporter, SkToBool(context)); 380 REPORTER_ASSERT(reporter, SkToBool(context));
319 if (NULL == context) { 381 if (NULL == context) {
320 return; 382 return;
321 } 383 }
322 context->setResourceCacheLimits(5, 30000); 384 context->setResourceCacheLimits(5, 30000);
323 GrResourceCache2* cache2 = context->getResourceCache2(); 385 GrResourceCache2* cache2 = context->getResourceCache2();
324 cache2->purgeAllUnlocked(); 386 cache2->purgeAllUnlocked();
325 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()) ; 387 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()) ;
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
562 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2-> findAndRefContentResource(key2))); 624 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2-> findAndRefContentResource(key2)));
563 find2->setSize(201); 625 find2->setSize(201);
564 } 626 }
565 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1)); 627 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1));
566 628
567 REPORTER_ASSERT(reporter, 201 == cache2->getResourceBytes()); 629 REPORTER_ASSERT(reporter, 201 == cache2->getResourceBytes());
568 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); 630 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount());
569 } 631 }
570 } 632 }
571 633
634 static void test_large_resource_count(skiatest::Reporter* reporter) {
635 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
636 REPORTER_ASSERT(reporter, SkToBool(context));
637 if (NULL == context) {
638 return;
639 }
640
641 static const int kResourceCnt = 2000;
642 // Set the cache size to double the resource count because we're going to cr eate 2x that number
643 // resources, using two different key domains. Add a little slop to the byte s because we resize
644 // down to 1 byte after creating the resource.
645 context->setResourceCacheLimits(2 * kResourceCnt, 2 * kResourceCnt + 1000);
646 GrResourceCache2* cache2 = context->getResourceCache2();
647 cache2->purgeAllUnlocked();
648 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()) ;
649
650 GrCacheID::Domain domain0 = GrCacheID::GenerateDomain();
651 GrCacheID::Domain domain1 = GrCacheID::GenerateDomain();
652 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
653
654 GrCacheID::Key keyData;
655 memset(&keyData, 0, sizeof(keyData));
656
657 for (int i = 0; i < kResourceCnt; ++i) {
658 TestResource* resource;
659 keyData.fData32[0] = i;
660
661 GrResourceKey key0(GrCacheID(domain0, keyData), t, 0);
662 resource = SkNEW_ARGS(TestResource, (context->getGpu()));
663 resource->cacheAccess().setContentKey(key0);
664 resource->setSize(1);
665 resource->unref();
666
667 GrResourceKey key1(GrCacheID(domain1, keyData), t, 0);
668 resource = SkNEW_ARGS(TestResource, (context->getGpu()));
669 resource->cacheAccess().setContentKey(key1);
670 resource->setSize(1);
671 resource->unref();
672 }
673
674 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
675 REPORTER_ASSERT(reporter, cache2->getBudgetedResourceBytes() == 2 * kResourc eCnt);
676 REPORTER_ASSERT(reporter, cache2->getBudgetedResourceCount() == 2 * kResourc eCnt);
677 REPORTER_ASSERT(reporter, cache2->getResourceBytes() == 2 * kResourceCnt);
678 REPORTER_ASSERT(reporter, cache2->getResourceCount() == 2 * kResourceCnt);
679 for (int i = 0; i < kResourceCnt; ++i) {
680 keyData.fData32[0] = i;
681 GrResourceKey key0(GrCacheID(domain0, keyData), t, 0);
682 REPORTER_ASSERT(reporter, cache2->hasContentKey(key0));
683 GrResourceKey key1(GrCacheID(domain0, keyData), t, 0);
684 REPORTER_ASSERT(reporter, cache2->hasContentKey(key1));
685 }
686
687 cache2->purgeAllUnlocked();
688 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
689 REPORTER_ASSERT(reporter, cache2->getBudgetedResourceBytes() == 0);
690 REPORTER_ASSERT(reporter, cache2->getBudgetedResourceCount() == 0);
691 REPORTER_ASSERT(reporter, cache2->getResourceBytes() == 0);
692 REPORTER_ASSERT(reporter, cache2->getResourceCount() == 0);
693
694 for (int i = 0; i < kResourceCnt; ++i) {
695 keyData.fData32[0] = i;
696 GrResourceKey key0(GrCacheID(domain0, keyData), t, 0);
697 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key0));
698 GrResourceKey key1(GrCacheID(domain0, keyData), t, 0);
699 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1));
700 }
701 }
702
703
572 //////////////////////////////////////////////////////////////////////////////// 704 ////////////////////////////////////////////////////////////////////////////////
573 DEF_GPUTEST(ResourceCache, reporter, factory) { 705 DEF_GPUTEST(ResourceCache, reporter, factory) {
574 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { 706 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
575 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G LContextType>(type); 707 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G LContextType>(type);
576 if (!GrContextFactory::IsRenderingGLContext(glType)) { 708 if (!GrContextFactory::IsRenderingGLContext(glType)) {
577 continue; 709 continue;
578 } 710 }
579 GrContext* context = factory->get(glType); 711 GrContext* context = factory->get(glType);
580 if (NULL == context) { 712 if (NULL == context) {
581 continue; 713 continue;
582 } 714 }
583 GrSurfaceDesc desc; 715 GrSurfaceDesc desc;
584 desc.fConfig = kSkia8888_GrPixelConfig; 716 desc.fConfig = kSkia8888_GrPixelConfig;
585 desc.fFlags = kRenderTarget_GrSurfaceFlag; 717 desc.fFlags = kRenderTarget_GrSurfaceFlag;
586 desc.fWidth = gWidth; 718 desc.fWidth = gWidth;
587 desc.fHeight = gHeight; 719 desc.fHeight = gHeight;
588 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight); 720 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
589 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, info )); 721 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, info ));
590 test_cache(reporter, context, surface->getCanvas()); 722 test_cache(reporter, context, surface->getCanvas());
591 } 723 }
592 724
593 // The below tests create their own mock contexts. 725 // The below tests create their own mock contexts.
594 test_no_key(reporter); 726 test_no_key(reporter);
595 test_budgeting(reporter); 727 test_budgeting(reporter);
596 test_duplicate_content_key(reporter); 728 test_duplicate_content_key(reporter);
597 test_duplicate_scratch_key(reporter); 729 test_duplicate_scratch_key(reporter);
730 test_remove_scratch_key(reporter);
598 test_purge_invalidated(reporter); 731 test_purge_invalidated(reporter);
599 test_cache_chained_purge(reporter); 732 test_cache_chained_purge(reporter);
600 test_resource_size_changed(reporter); 733 test_resource_size_changed(reporter);
734 test_large_resource_count(reporter);
601 } 735 }
602 736
603 #endif 737 #endif
OLDNEW
« no previous file with comments | « src/gpu/gl/GrGpuGL.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698