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