| 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 } | 59 } |
| 60 | 60 |
| 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 | 66 |
| 67 public: | 67 public: |
| 68 SK_DECLARE_INST_COUNT(TestResource); | 68 SK_DECLARE_INST_COUNT(TestResource); |
| 69 TestResource(GrGpu* gpu, bool isWrapped) |
| 70 : INHERITED(gpu, isWrapped) |
| 71 , fToDelete(NULL) |
| 72 , fSize(kDefaultSize) { |
| 73 ++fNumAlive; |
| 74 this->registerWithCache(); |
| 75 } |
| 76 |
| 69 TestResource(GrGpu* gpu) | 77 TestResource(GrGpu* gpu) |
| 70 : INHERITED(gpu, false) | 78 : INHERITED(gpu, false) |
| 71 , fToDelete(NULL) | 79 , fToDelete(NULL) |
| 72 , fSize(kDefaultSize) { | 80 , fSize(kDefaultSize) { |
| 73 ++fNumAlive; | 81 ++fNumAlive; |
| 74 this->registerWithCache(); | 82 this->registerWithCache(); |
| 75 } | 83 } |
| 76 | 84 |
| 77 TestResource(GrGpu* gpu, const GrResourceKey& scratchKey) | 85 TestResource(GrGpu* gpu, const GrResourceKey& scratchKey) |
| 78 : INHERITED(gpu, false) | 86 : INHERITED(gpu, false) |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | 166 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 159 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 167 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 160 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache2->getResourceBytes()); | 168 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache2->getResourceBytes()); |
| 161 | 169 |
| 162 b->unref(); | 170 b->unref(); |
| 163 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 171 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
| 164 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 172 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
| 165 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); | 173 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
| 166 } | 174 } |
| 167 | 175 |
| 176 static void test_wrapped(skiatest::Reporter* reporter) { |
| 177 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 178 REPORTER_ASSERT(reporter, SkToBool(context)); |
| 179 if (NULL == context) { |
| 180 return; |
| 181 } |
| 182 context->setResourceCacheLimits(10, 300); |
| 183 GrResourceCache2* cache2 = context->getResourceCache2(); |
| 184 cache2->purgeAllUnlocked(); |
| 185 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; |
| 186 SkASSERT(0 == cache2->getBudgetedResourceCount() && 0 == cache2->getBudgeted
ResourceBytes()); |
| 187 |
| 188 GrCacheID::Key keyData; |
| 189 memset(&keyData, 0, sizeof(keyData)); |
| 190 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 191 GrResourceKey scratchKey(GrCacheID(GrResourceKey::ScratchDomain(), keyData),
t, 0); |
| 192 GrResourceKey contentKey(GrCacheID(GrCacheID::GenerateDomain(), keyData), t,
0); |
| 193 |
| 194 // Create a scratch, a content, and a wrapped resource |
| 195 TestResource* scratch = new TestResource(context->getGpu(), scratchKey); |
| 196 scratch->setSize(10); |
| 197 TestResource* content = new TestResource(context->getGpu()); |
| 198 scratch->setSize(11); |
| 199 REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey)); |
| 200 TestResource* wrapped = new TestResource(context->getGpu(), true); |
| 201 scratch->setSize(12); |
| 202 |
| 203 // Make sure we can't add a content key to the wrapped resource |
| 204 keyData.fData8[0] = 1; |
| 205 GrResourceKey contentKey2(GrCacheID(GrCacheID::GenerateDomain(), keyData), t
, 0); |
| 206 REPORTER_ASSERT(reporter, !wrapped->cacheAccess().setContentKey(contentKey2)
); |
| 207 REPORTER_ASSERT(reporter, NULL == cache2->findAndRefContentResource(contentK
ey2)); |
| 208 |
| 209 // Make sure sizes are as we expect |
| 210 REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount()); |
| 211 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) + |
| 212 wrapped->gpuMemorySize() == cache2->getResourceByt
es()); |
| 213 REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); |
| 214 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) == |
| 215 cache2->getBudgetedResourceBytes()); |
| 216 |
| 217 // Our refs mean that the resources are non purgable. |
| 218 cache2->purgeAllUnlocked(); |
| 219 REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount()); |
| 220 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) + |
| 221 wrapped->gpuMemorySize() == cache2->getResourceByt
es()); |
| 222 REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); |
| 223 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) == |
| 224 cache2->getBudgetedResourceBytes()); |
| 225 |
| 226 // Unreffing the wrapped resource should free it right away. |
| 227 wrapped->unref(); |
| 228 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
| 229 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) == |
| 230 cache2->getResourceBytes()); |
| 231 |
| 232 // Now try freeing the other two resources first |
| 233 wrapped = new TestResource(context->getGpu(), true); |
| 234 scratch->setSize(12); |
| 235 content->unref(); |
| 236 cache2->purgeAllUnlocked(); |
| 237 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
| 238 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize(
) == |
| 239 cache2->getResourceBytes()); |
| 240 REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount()); |
| 241 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache2->getBudgetedRes
ourceBytes()); |
| 242 |
| 243 scratch->unref(); |
| 244 cache2->purgeAllUnlocked(); |
| 245 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 246 REPORTER_ASSERT(reporter, wrapped->gpuMemorySize() == cache2->getResourceByt
es()); |
| 247 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); |
| 248 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
| 249 |
| 250 wrapped->unref(); |
| 251 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
| 252 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
| 253 } |
| 254 |
| 168 static void test_duplicate_scratch_key(skiatest::Reporter* reporter) { | 255 static void test_duplicate_scratch_key(skiatest::Reporter* reporter) { |
| 169 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 256 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 170 REPORTER_ASSERT(reporter, SkToBool(context)); | 257 REPORTER_ASSERT(reporter, SkToBool(context)); |
| 171 if (NULL == context) { | 258 if (NULL == context) { |
| 172 return; | 259 return; |
| 173 } | 260 } |
| 174 context->setResourceCacheLimits(5, 30000); | 261 context->setResourceCacheLimits(5, 30000); |
| 175 GrResourceCache2* cache2 = context->getResourceCache2(); | 262 GrResourceCache2* cache2 = context->getResourceCache2(); |
| 176 cache2->purgeAllUnlocked(); | 263 cache2->purgeAllUnlocked(); |
| 177 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; | 264 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; |
| (...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 desc.fFlags = kRenderTarget_GrSurfaceFlag; | 571 desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 485 desc.fWidth = gWidth; | 572 desc.fWidth = gWidth; |
| 486 desc.fHeight = gHeight; | 573 desc.fHeight = gHeight; |
| 487 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight); | 574 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight); |
| 488 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, info
)); | 575 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, info
)); |
| 489 test_cache(reporter, context, surface->getCanvas()); | 576 test_cache(reporter, context, surface->getCanvas()); |
| 490 } | 577 } |
| 491 | 578 |
| 492 // The below tests create their own mock contexts. | 579 // The below tests create their own mock contexts. |
| 493 test_no_key(reporter); | 580 test_no_key(reporter); |
| 581 test_wrapped(reporter); |
| 494 test_duplicate_content_key(reporter); | 582 test_duplicate_content_key(reporter); |
| 495 test_duplicate_scratch_key(reporter); | 583 test_duplicate_scratch_key(reporter); |
| 496 test_purge_invalidated(reporter); | 584 test_purge_invalidated(reporter); |
| 497 test_cache_chained_purge(reporter); | 585 test_cache_chained_purge(reporter); |
| 498 test_resource_size_changed(reporter); | 586 test_resource_size_changed(reporter); |
| 499 } | 587 } |
| 500 | 588 |
| 501 #endif | 589 #endif |
| OLD | NEW |