| 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 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | 166 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 167 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 167 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 168 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache2->getResourceBytes()); | 168 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache2->getResourceBytes()); |
| 169 | 169 |
| 170 b->unref(); | 170 b->unref(); |
| 171 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 171 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
| 172 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 172 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
| 173 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); | 173 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
| 174 } | 174 } |
| 175 | 175 |
| 176 static void test_wrapped(skiatest::Reporter* reporter) { | 176 static void test_budgeting(skiatest::Reporter* reporter) { |
| 177 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 177 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 178 REPORTER_ASSERT(reporter, SkToBool(context)); | 178 REPORTER_ASSERT(reporter, SkToBool(context)); |
| 179 if (NULL == context) { | 179 if (NULL == context) { |
| 180 return; | 180 return; |
| 181 } | 181 } |
| 182 context->setResourceCacheLimits(10, 300); | 182 context->setResourceCacheLimits(10, 300); |
| 183 GrResourceCache2* cache2 = context->getResourceCache2(); | 183 GrResourceCache2* cache2 = context->getResourceCache2(); |
| 184 cache2->purgeAllUnlocked(); | 184 cache2->purgeAllUnlocked(); |
| 185 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; | 185 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; |
| 186 SkASSERT(0 == cache2->getBudgetedResourceCount() && 0 == cache2->getBudgeted
ResourceBytes()); | 186 SkASSERT(0 == cache2->getBudgetedResourceCount() && 0 == cache2->getBudgeted
ResourceBytes()); |
| 187 | 187 |
| 188 GrCacheID::Key keyData; | 188 GrCacheID::Key keyData; |
| 189 memset(&keyData, 0, sizeof(keyData)); | 189 memset(&keyData, 0, sizeof(keyData)); |
| 190 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); | 190 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 191 GrResourceKey scratchKey(GrCacheID(GrResourceKey::ScratchDomain(), keyData),
t, 0); | 191 GrResourceKey scratchKey(GrCacheID(GrResourceKey::ScratchDomain(), keyData),
t, 0); |
| 192 GrResourceKey contentKey(GrCacheID(GrCacheID::GenerateDomain(), keyData), t,
0); | 192 GrResourceKey contentKey(GrCacheID(GrCacheID::GenerateDomain(), keyData), t,
0); |
| 193 | 193 |
| 194 // Create a scratch, a content, and a wrapped resource | 194 // Create a scratch, a content, and a wrapped resource |
| 195 TestResource* scratch = new TestResource(context->getGpu(), scratchKey); | 195 TestResource* scratch = new TestResource(context->getGpu(), scratchKey); |
| 196 scratch->setSize(10); | 196 scratch->setSize(10); |
| 197 TestResource* content = new TestResource(context->getGpu()); | 197 TestResource* content = new TestResource(context->getGpu()); |
| 198 scratch->setSize(11); | 198 scratch->setSize(11); |
| 199 REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey)); | 199 REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey)); |
| 200 TestResource* wrapped = new TestResource(context->getGpu(), true); | 200 TestResource* wrapped = new TestResource(context->getGpu(), true); |
| 201 scratch->setSize(12); | 201 scratch->setSize(12); |
| 202 TestResource* unbudgeted = new TestResource(context->getGpu()); |
| 203 unbudgeted->setSize(13); |
| 204 unbudgeted->cacheAccess().setBudgeted(false); |
| 202 | 205 |
| 203 // Make sure we can't add a content key to the wrapped resource | 206 // Make sure we can't add a content key to the wrapped resource |
| 204 keyData.fData8[0] = 1; | 207 keyData.fData8[0] = 1; |
| 205 GrResourceKey contentKey2(GrCacheID(GrCacheID::GenerateDomain(), keyData), t
, 0); | 208 GrResourceKey contentKey2(GrCacheID(GrCacheID::GenerateDomain(), keyData), t
, 0); |
| 206 REPORTER_ASSERT(reporter, !wrapped->cacheAccess().setContentKey(contentKey2)
); | 209 REPORTER_ASSERT(reporter, !wrapped->cacheAccess().setContentKey(contentKey2)
); |
| 207 REPORTER_ASSERT(reporter, NULL == cache2->findAndRefContentResource(contentK
ey2)); | 210 REPORTER_ASSERT(reporter, NULL == cache2->findAndRefContentResource(contentK
ey2)); |
| 208 | 211 |
| 209 // Make sure sizes are as we expect | 212 // Make sure sizes are as we expect |
| 210 REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount()); | 213 REPORTER_ASSERT(reporter, 4 == cache2->getResourceCount()); |
| 211 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) + | 214 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) + |
| 212 wrapped->gpuMemorySize() == cache2->getResourceByt
es()); | 215 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySi
ze() == |
| 216 cache2->getResourceBytes()); |
| 213 REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); | 217 REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); |
| 214 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) == | 218 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) == |
| 215 cache2->getBudgetedResourceBytes()); | 219 cache2->getBudgetedResourceBytes()); |
| 216 | 220 |
| 217 // Our refs mean that the resources are non purgable. | 221 // Our refs mean that the resources are non purgable. |
| 218 cache2->purgeAllUnlocked(); | 222 cache2->purgeAllUnlocked(); |
| 219 REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount()); | 223 REPORTER_ASSERT(reporter, 4 == cache2->getResourceCount()); |
| 220 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) + | 224 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) + |
| 221 wrapped->gpuMemorySize() == cache2->getResourceByt
es()); | 225 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySi
ze() == |
| 226 cache2->getResourceBytes()); |
| 222 REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); | 227 REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); |
| 223 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) == | 228 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) == |
| 224 cache2->getBudgetedResourceBytes()); | 229 cache2->getBudgetedResourceBytes()); |
| 225 | 230 |
| 226 // Unreffing the wrapped resource should free it right away. | 231 // Unreffing the wrapped resource should free it right away. |
| 227 wrapped->unref(); | 232 wrapped->unref(); |
| 228 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 233 REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount()); |
| 229 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) == | 234 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) + |
| 230 cache2->getResourceBytes()); | 235 unbudgeted->gpuMemorySize() == cache2->getResource
Bytes()); |
| 231 | 236 |
| 232 // Now try freeing the other two resources first | 237 // Now try freeing the budgeted resources first |
| 233 wrapped = new TestResource(context->getGpu(), true); | 238 wrapped = new TestResource(context->getGpu(), true); |
| 234 scratch->setSize(12); | 239 scratch->setSize(12); |
| 235 content->unref(); | 240 content->unref(); |
| 236 cache2->purgeAllUnlocked(); | 241 cache2->purgeAllUnlocked(); |
| 237 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 242 REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount()); |
| 238 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize(
) == | 243 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize(
) + |
| 239 cache2->getResourceBytes()); | 244 unbudgeted->gpuMemorySize() == cache2->getResource
Bytes()); |
| 240 REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount()); | 245 REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount()); |
| 241 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache2->getBudgetedRes
ourceBytes()); | 246 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache2->getBudgetedRes
ourceBytes()); |
| 242 | 247 |
| 243 scratch->unref(); | 248 scratch->unref(); |
| 244 cache2->purgeAllUnlocked(); | 249 cache2->purgeAllUnlocked(); |
| 245 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 250 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
| 246 REPORTER_ASSERT(reporter, wrapped->gpuMemorySize() == cache2->getResourceByt
es()); | 251 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySi
ze() == |
| 252 cache2->getResourceBytes()); |
| 247 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); | 253 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); |
| 248 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); | 254 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
| 249 | 255 |
| 250 wrapped->unref(); | 256 wrapped->unref(); |
| 257 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 258 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache2->getResource
Bytes()); |
| 259 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); |
| 260 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
| 261 |
| 262 unbudgeted->unref(); |
| 251 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 263 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
| 252 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); | 264 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
| 265 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); |
| 266 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
| 253 } | 267 } |
| 254 | 268 |
| 255 static void test_duplicate_scratch_key(skiatest::Reporter* reporter) { | 269 static void test_duplicate_scratch_key(skiatest::Reporter* reporter) { |
| 256 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 270 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 257 REPORTER_ASSERT(reporter, SkToBool(context)); | 271 REPORTER_ASSERT(reporter, SkToBool(context)); |
| 258 if (NULL == context) { | 272 if (NULL == context) { |
| 259 return; | 273 return; |
| 260 } | 274 } |
| 261 context->setResourceCacheLimits(5, 30000); | 275 context->setResourceCacheLimits(5, 30000); |
| 262 GrResourceCache2* cache2 = context->getResourceCache2(); | 276 GrResourceCache2* cache2 = context->getResourceCache2(); |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 571 desc.fFlags = kRenderTarget_GrSurfaceFlag; | 585 desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 572 desc.fWidth = gWidth; | 586 desc.fWidth = gWidth; |
| 573 desc.fHeight = gHeight; | 587 desc.fHeight = gHeight; |
| 574 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight); | 588 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight); |
| 575 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, info
)); | 589 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, info
)); |
| 576 test_cache(reporter, context, surface->getCanvas()); | 590 test_cache(reporter, context, surface->getCanvas()); |
| 577 } | 591 } |
| 578 | 592 |
| 579 // The below tests create their own mock contexts. | 593 // The below tests create their own mock contexts. |
| 580 test_no_key(reporter); | 594 test_no_key(reporter); |
| 581 test_wrapped(reporter); | 595 test_budgeting(reporter); |
| 582 test_duplicate_content_key(reporter); | 596 test_duplicate_content_key(reporter); |
| 583 test_duplicate_scratch_key(reporter); | 597 test_duplicate_scratch_key(reporter); |
| 584 test_purge_invalidated(reporter); | 598 test_purge_invalidated(reporter); |
| 585 test_cache_chained_purge(reporter); | 599 test_cache_chained_purge(reporter); |
| 586 test_resource_size_changed(reporter); | 600 test_resource_size_changed(reporter); |
| 587 } | 601 } |
| 588 | 602 |
| 589 #endif | 603 #endif |
| OLD | NEW |