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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 | 56 |
57 // we should never go over the size limit | 57 // we should never go over the size limit |
58 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize); | 58 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize); |
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 enum ScratchConstructor { kScratchConstructor }; | 66 |
67 public: | 67 public: |
68 SK_DECLARE_INST_COUNT(TestResource); | 68 SK_DECLARE_INST_COUNT(TestResource); |
69 /** Property that distinctly categorizes the resource. | |
70 * For example, textures have width, height, ... */ | |
71 enum SimulatedProperty { kProperty1_SimulatedProperty, kProperty2_SimulatedP
roperty }; | |
72 | |
73 TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle) | 69 TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle) |
74 : INHERITED(gpu, lifeCycle) | 70 : INHERITED(gpu, lifeCycle) |
75 , fToDelete(NULL) | 71 , fToDelete(NULL) |
76 , fSize(size) | 72 , fSize(size) { |
77 , fProperty(kProperty1_SimulatedProperty) { | |
78 ++fNumAlive; | 73 ++fNumAlive; |
79 this->registerWithCache(); | 74 this->registerWithCache(); |
80 } | 75 } |
81 | 76 |
82 TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle) | 77 TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle) |
83 : INHERITED(gpu, lifeCycle) | 78 : INHERITED(gpu, lifeCycle) |
84 , fToDelete(NULL) | 79 , fToDelete(NULL) |
85 , fSize(kDefaultSize) | 80 , fSize(kDefaultSize) { |
86 , fProperty(kProperty1_SimulatedProperty) { | |
87 ++fNumAlive; | 81 ++fNumAlive; |
88 this->registerWithCache(); | 82 this->registerWithCache(); |
89 } | 83 } |
90 | 84 |
91 TestResource(GrGpu* gpu) | 85 TestResource(GrGpu* gpu) |
92 : INHERITED(gpu, kCached_LifeCycle) | 86 : INHERITED(gpu, kCached_LifeCycle) |
93 , fToDelete(NULL) | 87 , fToDelete(NULL) |
94 , fSize(kDefaultSize) | 88 , fSize(kDefaultSize) { |
95 , fProperty(kProperty1_SimulatedProperty) { | |
96 ++fNumAlive; | 89 ++fNumAlive; |
97 this->registerWithCache(); | 90 this->registerWithCache(); |
98 } | 91 } |
99 | 92 |
100 static TestResource* CreateScratchTestResource(GrGpu* gpu, SimulatedProperty
property) { | 93 TestResource(GrGpu* gpu, const GrScratchKey& scratchKey) |
101 return SkNEW_ARGS(TestResource, (gpu, property, kScratchConstructor)); | 94 : INHERITED(gpu, kCached_LifeCycle) |
| 95 , fToDelete(NULL) |
| 96 , fSize(kDefaultSize) { |
| 97 this->setScratchKey(scratchKey); |
| 98 ++fNumAlive; |
| 99 this->registerWithCache(); |
102 } | 100 } |
103 | 101 |
104 ~TestResource() { | 102 ~TestResource() { |
105 --fNumAlive; | 103 --fNumAlive; |
106 SkSafeUnref(fToDelete); | 104 SkSafeUnref(fToDelete); |
107 } | 105 } |
108 | 106 |
109 void setSize(size_t size) { | 107 void setSize(size_t size) { |
110 fSize = size; | 108 fSize = size; |
111 this->didChangeGpuMemorySize(); | 109 this->didChangeGpuMemorySize(); |
112 } | 110 } |
113 | 111 |
114 static int NumAlive() { return fNumAlive; } | 112 static int NumAlive() { return fNumAlive; } |
115 | 113 |
116 void setUnrefWhenDestroyed(TestResource* resource) { | 114 void setUnrefWhenDestroyed(TestResource* resource) { |
117 SkRefCnt_SafeAssign(fToDelete, resource); | 115 SkRefCnt_SafeAssign(fToDelete, resource); |
118 } | 116 } |
119 | 117 |
120 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key)
{ | |
121 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType
(); | |
122 static const size_t kTestAmount = 6; | |
123 GrScratchKey::Builder builder(key, t, kTestAmount); | |
124 for (size_t i = 0; i < kTestAmount; ++i) { | |
125 builder[i] = i + static_cast<int>(property); | |
126 } | |
127 } | |
128 | |
129 private: | 118 private: |
130 TestResource(GrGpu* gpu, SimulatedProperty property, ScratchConstructor) | |
131 : INHERITED(gpu, kCached_LifeCycle) | |
132 , fToDelete(NULL) | |
133 , fSize(kDefaultSize) | |
134 , fProperty(property) { | |
135 GrScratchKey scratchKey; | |
136 ComputeScratchKey(fProperty, &scratchKey); | |
137 this->setScratchKey(scratchKey); | |
138 ++fNumAlive; | |
139 this->registerWithCache(); | |
140 } | |
141 | |
142 size_t onGpuMemorySize() const SK_OVERRIDE { return fSize; } | 119 size_t onGpuMemorySize() const SK_OVERRIDE { return fSize; } |
143 | 120 |
144 TestResource* fToDelete; | 121 TestResource* fToDelete; |
145 size_t fSize; | 122 size_t fSize; |
146 static int fNumAlive; | 123 static int fNumAlive; |
147 SimulatedProperty fProperty; | 124 |
148 typedef GrGpuResource INHERITED; | 125 typedef GrGpuResource INHERITED; |
149 }; | 126 }; |
150 int TestResource::fNumAlive = 0; | 127 int TestResource::fNumAlive = 0; |
151 | 128 |
152 static void test_no_key(skiatest::Reporter* reporter) { | 129 static void test_no_key(skiatest::Reporter* reporter) { |
153 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 130 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
154 REPORTER_ASSERT(reporter, SkToBool(context)); | 131 REPORTER_ASSERT(reporter, SkToBool(context)); |
155 if (NULL == context) { | 132 if (NULL == context) { |
156 return; | 133 return; |
157 } | 134 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | 174 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
198 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 175 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
199 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache2->getResourceBytes()); | 176 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache2->getResourceBytes()); |
200 | 177 |
201 b->unref(); | 178 b->unref(); |
202 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 179 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
203 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 180 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
204 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); | 181 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
205 } | 182 } |
206 | 183 |
| 184 static void make_scratch_key(GrScratchKey* key) { |
| 185 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType(); |
| 186 GrScratchKey::Builder builder(key, t, 0); |
| 187 } |
| 188 |
207 static void test_budgeting(skiatest::Reporter* reporter) { | 189 static void test_budgeting(skiatest::Reporter* reporter) { |
208 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 190 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
209 REPORTER_ASSERT(reporter, SkToBool(context)); | 191 REPORTER_ASSERT(reporter, SkToBool(context)); |
210 if (NULL == context) { | 192 if (NULL == context) { |
211 return; | 193 return; |
212 } | 194 } |
213 context->setResourceCacheLimits(10, 300); | 195 context->setResourceCacheLimits(10, 300); |
214 GrResourceCache2* cache2 = context->getResourceCache2(); | 196 GrResourceCache2* cache2 = context->getResourceCache2(); |
215 cache2->purgeAllUnlocked(); | 197 cache2->purgeAllUnlocked(); |
216 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; | 198 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; |
217 SkASSERT(0 == cache2->getBudgetedResourceCount() && 0 == cache2->getBudgeted
ResourceBytes()); | 199 SkASSERT(0 == cache2->getBudgetedResourceCount() && 0 == cache2->getBudgeted
ResourceBytes()); |
218 | 200 |
| 201 GrScratchKey scratchKey; |
| 202 make_scratch_key(&scratchKey); |
| 203 |
219 GrCacheID::Key keyData; | 204 GrCacheID::Key keyData; |
220 memset(&keyData, 0, sizeof(keyData)); | 205 memset(&keyData, 0, sizeof(keyData)); |
221 GrResourceKey contentKey(GrCacheID(GrCacheID::GenerateDomain(), keyData), 0)
; | 206 GrResourceKey contentKey(GrCacheID(GrCacheID::GenerateDomain(), keyData), 0)
; |
222 | 207 |
223 // Create a scratch, a content, and a wrapped resource | 208 // Create a scratch, a content, and a wrapped resource |
224 TestResource* scratch = | 209 TestResource* scratch = SkNEW_ARGS(TestResource, (context->getGpu(), scratch
Key)); |
225 TestResource::CreateScratchTestResource(context->getGpu(), | |
226 TestResource::kProperty2_Sim
ulatedProperty); | |
227 scratch->setSize(10); | 210 scratch->setSize(10); |
228 TestResource* content = SkNEW_ARGS(TestResource, (context->getGpu())); | 211 TestResource* content = SkNEW_ARGS(TestResource, (context->getGpu())); |
229 content->setSize(11); | 212 content->setSize(11); |
230 REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey)); | 213 REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey)); |
231 TestResource* wrapped = SkNEW_ARGS(TestResource, | 214 TestResource* wrapped = SkNEW_ARGS(TestResource, |
232 (context->getGpu(), GrGpuResource::kWrapp
ed_LifeCycle)); | 215 (context->getGpu(), GrGpuResource::kWrapp
ed_LifeCycle)); |
233 wrapped->setSize(12); | 216 wrapped->setSize(12); |
234 TestResource* unbudgeted = SkNEW_ARGS(TestResource, | 217 TestResource* unbudgeted = SkNEW_ARGS(TestResource, |
235 (context->getGpu(), GrGpuResource::kUn
cached_LifeCycle)); | 218 (context->getGpu(), GrGpuResource::kUn
cached_LifeCycle)); |
236 unbudgeted->setSize(13); | 219 unbudgeted->setSize(13); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 REPORTER_ASSERT(reporter, SkToBool(context)); | 286 REPORTER_ASSERT(reporter, SkToBool(context)); |
304 if (NULL == context) { | 287 if (NULL == context) { |
305 return; | 288 return; |
306 } | 289 } |
307 context->setResourceCacheLimits(10, 300); | 290 context->setResourceCacheLimits(10, 300); |
308 GrResourceCache2* cache2 = context->getResourceCache2(); | 291 GrResourceCache2* cache2 = context->getResourceCache2(); |
309 cache2->purgeAllUnlocked(); | 292 cache2->purgeAllUnlocked(); |
310 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; | 293 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; |
311 SkASSERT(0 == cache2->getBudgetedResourceCount() && 0 == cache2->getBudgeted
ResourceBytes()); | 294 SkASSERT(0 == cache2->getBudgetedResourceCount() && 0 == cache2->getBudgeted
ResourceBytes()); |
312 | 295 |
| 296 GrScratchKey scratchKey; |
| 297 make_scratch_key(&scratchKey); |
| 298 |
313 GrCacheID::Key keyData; | 299 GrCacheID::Key keyData; |
314 memset(&keyData, 0, sizeof(keyData)); | 300 memset(&keyData, 0, sizeof(keyData)); |
315 GrResourceKey contentKey(GrCacheID(GrCacheID::GenerateDomain(), keyData), 0)
; | 301 GrResourceKey contentKey(GrCacheID(GrCacheID::GenerateDomain(), keyData), 0)
; |
316 | 302 |
317 TestResource* scratch; | 303 TestResource* scratch; |
318 TestResource* content; | 304 TestResource* content; |
319 TestResource* wrapped; | 305 TestResource* wrapped; |
320 TestResource* unbudgeted; | 306 TestResource* unbudgeted; |
321 | 307 |
322 // A large uncached or wrapped resource shouldn't evict anything. | 308 // A large uncached or wrapped resource shouldn't evict anything. |
323 scratch = TestResource::CreateScratchTestResource(context->getGpu(), | 309 scratch = SkNEW_ARGS(TestResource, (context->getGpu(), scratchKey)); |
324 TestResource::kProperty2_S
imulatedProperty); | |
325 scratch->setSize(10); | 310 scratch->setSize(10); |
326 scratch->unref(); | 311 scratch->unref(); |
327 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 312 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
328 REPORTER_ASSERT(reporter, 10 == cache2->getResourceBytes()); | 313 REPORTER_ASSERT(reporter, 10 == cache2->getResourceBytes()); |
329 REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount()); | 314 REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount()); |
330 REPORTER_ASSERT(reporter, 10 == cache2->getBudgetedResourceBytes()); | 315 REPORTER_ASSERT(reporter, 10 == cache2->getBudgetedResourceBytes()); |
331 | 316 |
332 content = SkNEW_ARGS(TestResource, (context->getGpu())); | 317 content = SkNEW_ARGS(TestResource, (context->getGpu())); |
333 content->setSize(11); | 318 content->setSize(11); |
334 REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey)); | 319 REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey)); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 361 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
377 REPORTER_ASSERT(reporter, SkToBool(context)); | 362 REPORTER_ASSERT(reporter, SkToBool(context)); |
378 if (NULL == context) { | 363 if (NULL == context) { |
379 return; | 364 return; |
380 } | 365 } |
381 context->setResourceCacheLimits(5, 30000); | 366 context->setResourceCacheLimits(5, 30000); |
382 GrResourceCache2* cache2 = context->getResourceCache2(); | 367 GrResourceCache2* cache2 = context->getResourceCache2(); |
383 cache2->purgeAllUnlocked(); | 368 cache2->purgeAllUnlocked(); |
384 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; | 369 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; |
385 | 370 |
| 371 GrScratchKey scratchKey; |
| 372 make_scratch_key(&scratchKey); |
| 373 |
386 // Create two resources that have the same scratch key. | 374 // Create two resources that have the same scratch key. |
387 TestResource* a = | 375 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu(), scratchKey)); |
388 TestResource::CreateScratchTestResource(context->getGpu(), | 376 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu(), scratchKey)); |
389 TestResource::kProperty2_Sim
ulatedProperty); | |
390 TestResource* b = | |
391 TestResource::CreateScratchTestResource(context->getGpu(), | |
392 TestResource::kProperty2_Sim
ulatedProperty); | |
393 a->setSize(11); | 377 a->setSize(11); |
394 b->setSize(12); | 378 b->setSize(12); |
395 GrScratchKey scratchKey1; | |
396 TestResource::ComputeScratchKey(TestResource::kProperty1_SimulatedProperty,
&scratchKey1); | |
397 // Check for negative case consistency. (leaks upon test failure.) | |
398 REPORTER_ASSERT(reporter, NULL == cache2->findAndRefScratchResource(scratchK
ey1)); | |
399 | |
400 GrScratchKey scratchKey; | |
401 TestResource::ComputeScratchKey(TestResource::kProperty2_SimulatedProperty,
&scratchKey); | |
402 | |
403 // Scratch resources are registered with GrResourceCache2 just by existing.
There are 2. | 379 // Scratch resources are registered with GrResourceCache2 just by existing.
There are 2. |
404 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 380 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
405 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey
(scratchKey));) | 381 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey
(scratchKey));) |
406 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 382 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
407 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == | 383 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == |
408 cache2->getResourceBytes()); | 384 cache2->getResourceBytes()); |
409 | 385 |
410 // Our refs mean that the resources are non purgable. | 386 // Our refs mean that the resources are non purgable. |
411 cache2->purgeAllUnlocked(); | 387 cache2->purgeAllUnlocked(); |
412 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 388 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
(...skipping 16 matching lines...) Expand all Loading... |
429 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 405 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
430 REPORTER_ASSERT(reporter, SkToBool(context)); | 406 REPORTER_ASSERT(reporter, SkToBool(context)); |
431 if (NULL == context) { | 407 if (NULL == context) { |
432 return; | 408 return; |
433 } | 409 } |
434 context->setResourceCacheLimits(5, 30000); | 410 context->setResourceCacheLimits(5, 30000); |
435 GrResourceCache2* cache2 = context->getResourceCache2(); | 411 GrResourceCache2* cache2 = context->getResourceCache2(); |
436 cache2->purgeAllUnlocked(); | 412 cache2->purgeAllUnlocked(); |
437 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; | 413 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; |
438 | 414 |
| 415 GrScratchKey scratchKey; |
| 416 make_scratch_key(&scratchKey); |
| 417 |
439 // Create two resources that have the same scratch key. | 418 // Create two resources that have the same scratch key. |
440 TestResource* a = | 419 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu(), scratchKey)); |
441 TestResource::CreateScratchTestResource(context->getGpu(), | 420 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu(), scratchKey)); |
442 TestResource::kProperty2_Sim
ulatedProperty); | |
443 TestResource* b = | |
444 TestResource::CreateScratchTestResource(context->getGpu(), | |
445 TestResource::kProperty2_Sim
ulatedProperty); | |
446 a->unref(); | 421 a->unref(); |
447 b->unref(); | 422 b->unref(); |
448 | 423 |
449 | |
450 GrScratchKey scratchKey; | |
451 // Ensure that scratch key lookup is correct for negative case. | |
452 TestResource::ComputeScratchKey(TestResource::kProperty1_SimulatedProperty,
&scratchKey); | |
453 // (following leaks upon test failure). | |
454 REPORTER_ASSERT(reporter, cache2->findAndRefScratchResource(scratchKey) == N
ULL); | |
455 | |
456 // Scratch resources are registered with GrResourceCache2 just by existing.
There are 2. | 424 // Scratch resources are registered with GrResourceCache2 just by existing.
There are 2. |
457 TestResource::ComputeScratchKey(TestResource::kProperty2_SimulatedProperty,
&scratchKey); | |
458 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 425 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
459 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey
(scratchKey));) | 426 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey
(scratchKey));) |
460 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 427 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
461 | 428 |
462 // Find the first resource and remove its scratch key | 429 // Find the first resource and remove its scratch key |
463 GrGpuResource* find; | 430 GrGpuResource* find; |
464 find = cache2->findAndRefScratchResource(scratchKey); | 431 find = cache2->findAndRefScratchResource(scratchKey); |
465 find->cacheAccess().removeScratchKey(); | 432 find->cacheAccess().removeScratchKey(); |
466 // It's still alive, but not cached by scratch key anymore | 433 // It's still alive, but not cached by scratch key anymore |
467 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 434 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
(...skipping 18 matching lines...) Expand all Loading... |
486 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | 453 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
487 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey
(scratchKey));) | 454 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey
(scratchKey));) |
488 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 455 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
489 | 456 |
490 find->unref(); | 457 find->unref(); |
491 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 458 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
492 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey
(scratchKey));) | 459 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey
(scratchKey));) |
493 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 460 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
494 } | 461 } |
495 | 462 |
496 static void test_scratch_key_consistency(skiatest::Reporter* reporter) { | |
497 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | |
498 REPORTER_ASSERT(reporter, SkToBool(context)); | |
499 if (NULL == context) { | |
500 return; | |
501 } | |
502 context->setResourceCacheLimits(5, 30000); | |
503 GrResourceCache2* cache2 = context->getResourceCache2(); | |
504 cache2->purgeAllUnlocked(); | |
505 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; | |
506 | |
507 // Create two resources that have the same scratch key. | |
508 TestResource* a = | |
509 TestResource::CreateScratchTestResource(context->getGpu(), | |
510 TestResource::kProperty2_Sim
ulatedProperty); | |
511 TestResource* b = | |
512 TestResource::CreateScratchTestResource(context->getGpu(), | |
513 TestResource::kProperty2_Sim
ulatedProperty); | |
514 a->unref(); | |
515 b->unref(); | |
516 | |
517 GrScratchKey scratchKey; | |
518 // Ensure that scratch key comparison and assignment is consistent. | |
519 GrScratchKey scratchKey1; | |
520 TestResource::ComputeScratchKey(TestResource::kProperty1_SimulatedProperty,
&scratchKey1); | |
521 GrScratchKey scratchKey2; | |
522 TestResource::ComputeScratchKey(TestResource::kProperty2_SimulatedProperty,
&scratchKey2); | |
523 REPORTER_ASSERT(reporter, scratchKey1.size() <= sizeof(scratchKey1)); | |
524 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2); | |
525 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1); | |
526 scratchKey = scratchKey1; | |
527 REPORTER_ASSERT(reporter, scratchKey.size() <= sizeof(scratchKey)); | |
528 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey); | |
529 REPORTER_ASSERT(reporter, scratchKey == scratchKey1); | |
530 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey); | |
531 REPORTER_ASSERT(reporter, scratchKey != scratchKey2); | |
532 scratchKey = scratchKey2; | |
533 REPORTER_ASSERT(reporter, scratchKey.size() <= sizeof(scratchKey)); | |
534 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey); | |
535 REPORTER_ASSERT(reporter, scratchKey != scratchKey1); | |
536 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey); | |
537 REPORTER_ASSERT(reporter, scratchKey == scratchKey2); | |
538 | |
539 // Ensure that scratch key lookup is correct for negative case. | |
540 TestResource::ComputeScratchKey(TestResource::kProperty1_SimulatedProperty,
&scratchKey); | |
541 // (following leaks upon test failure). | |
542 REPORTER_ASSERT(reporter, cache2->findAndRefScratchResource(scratchKey) == N
ULL); | |
543 | |
544 // Find the first resource with a scratch key and a copy of a scratch key. | |
545 TestResource::ComputeScratchKey(TestResource::kProperty2_SimulatedProperty,
&scratchKey); | |
546 GrGpuResource* find = cache2->findAndRefScratchResource(scratchKey); | |
547 REPORTER_ASSERT(reporter, find != NULL); | |
548 find->unref(); | |
549 | |
550 scratchKey2 = scratchKey; | |
551 find = cache2->findAndRefScratchResource(scratchKey2); | |
552 REPORTER_ASSERT(reporter, find != NULL); | |
553 REPORTER_ASSERT(reporter, find == a || find == b); | |
554 | |
555 GrGpuResource* find2 = cache2->findAndRefScratchResource(scratchKey2); | |
556 REPORTER_ASSERT(reporter, find2 != NULL); | |
557 REPORTER_ASSERT(reporter, find2 == a || find2 == b); | |
558 REPORTER_ASSERT(reporter, find2 != find); | |
559 find2->unref(); | |
560 find->unref(); | |
561 } | |
562 | |
563 static void test_duplicate_content_key(skiatest::Reporter* reporter) { | 463 static void test_duplicate_content_key(skiatest::Reporter* reporter) { |
564 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 464 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
565 REPORTER_ASSERT(reporter, SkToBool(context)); | 465 REPORTER_ASSERT(reporter, SkToBool(context)); |
566 if (NULL == context) { | 466 if (NULL == context) { |
567 return; | 467 return; |
568 } | 468 } |
569 context->setResourceCacheLimits(5, 30000); | 469 context->setResourceCacheLimits(5, 30000); |
570 GrResourceCache2* cache2 = context->getResourceCache2(); | 470 GrResourceCache2* cache2 = context->getResourceCache2(); |
571 cache2->purgeAllUnlocked(); | 471 cache2->purgeAllUnlocked(); |
572 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; | 472 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes())
; |
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
902 test_cache(reporter, context, surface->getCanvas()); | 802 test_cache(reporter, context, surface->getCanvas()); |
903 } | 803 } |
904 | 804 |
905 // The below tests create their own mock contexts. | 805 // The below tests create their own mock contexts. |
906 test_no_key(reporter); | 806 test_no_key(reporter); |
907 test_budgeting(reporter); | 807 test_budgeting(reporter); |
908 test_unbudgeted(reporter); | 808 test_unbudgeted(reporter); |
909 test_duplicate_content_key(reporter); | 809 test_duplicate_content_key(reporter); |
910 test_duplicate_scratch_key(reporter); | 810 test_duplicate_scratch_key(reporter); |
911 test_remove_scratch_key(reporter); | 811 test_remove_scratch_key(reporter); |
912 test_scratch_key_consistency(reporter); | |
913 test_purge_invalidated(reporter); | 812 test_purge_invalidated(reporter); |
914 test_cache_chained_purge(reporter); | 813 test_cache_chained_purge(reporter); |
915 test_resource_size_changed(reporter); | 814 test_resource_size_changed(reporter); |
916 test_large_resource_count(reporter); | 815 test_large_resource_count(reporter); |
917 } | 816 } |
918 | 817 |
919 #endif | 818 #endif |
OLD | NEW |