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

Side by Side Diff: tests/ResourceCacheTest.cpp

Issue 860333002: Make GrScratchKey memory buffer correct size on copy (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 11 months 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 | « include/gpu/GrResourceKey.h ('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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 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
118 private: 129 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
119 size_t onGpuMemorySize() const SK_OVERRIDE { return fSize; } 142 size_t onGpuMemorySize() const SK_OVERRIDE { return fSize; }
120 143
121 TestResource* fToDelete; 144 TestResource* fToDelete;
122 size_t fSize; 145 size_t fSize;
123 static int fNumAlive; 146 static int fNumAlive;
124 147 SimulatedProperty fProperty;
125 typedef GrGpuResource INHERITED; 148 typedef GrGpuResource INHERITED;
126 }; 149 };
127 int TestResource::fNumAlive = 0; 150 int TestResource::fNumAlive = 0;
128 151
129 static void test_no_key(skiatest::Reporter* reporter) { 152 static void test_no_key(skiatest::Reporter* reporter) {
130 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); 153 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
131 REPORTER_ASSERT(reporter, SkToBool(context)); 154 REPORTER_ASSERT(reporter, SkToBool(context));
132 if (NULL == context) { 155 if (NULL == context) {
133 return; 156 return;
134 } 157 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); 197 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
175 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); 198 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount());
176 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache2->getResourceBytes()); 199 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache2->getResourceBytes());
177 200
178 b->unref(); 201 b->unref();
179 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); 202 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
180 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); 203 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount());
181 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); 204 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes());
182 } 205 }
183 206
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) { 207 static void test_budgeting(skiatest::Reporter* reporter) {
190 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); 208 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
191 REPORTER_ASSERT(reporter, SkToBool(context)); 209 REPORTER_ASSERT(reporter, SkToBool(context));
192 if (NULL == context) { 210 if (NULL == context) {
193 return; 211 return;
194 } 212 }
195 context->setResourceCacheLimits(10, 300); 213 context->setResourceCacheLimits(10, 300);
196 GrResourceCache2* cache2 = context->getResourceCache2(); 214 GrResourceCache2* cache2 = context->getResourceCache2();
197 cache2->purgeAllUnlocked(); 215 cache2->purgeAllUnlocked();
198 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()) ; 216 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()) ;
199 SkASSERT(0 == cache2->getBudgetedResourceCount() && 0 == cache2->getBudgeted ResourceBytes()); 217 SkASSERT(0 == cache2->getBudgetedResourceCount() && 0 == cache2->getBudgeted ResourceBytes());
200 218
201 GrScratchKey scratchKey;
202 make_scratch_key(&scratchKey);
203
204 GrCacheID::Key keyData; 219 GrCacheID::Key keyData;
205 memset(&keyData, 0, sizeof(keyData)); 220 memset(&keyData, 0, sizeof(keyData));
206 GrResourceKey contentKey(GrCacheID(GrCacheID::GenerateDomain(), keyData), 0) ; 221 GrResourceKey contentKey(GrCacheID(GrCacheID::GenerateDomain(), keyData), 0) ;
207 222
208 // Create a scratch, a content, and a wrapped resource 223 // Create a scratch, a content, and a wrapped resource
209 TestResource* scratch = SkNEW_ARGS(TestResource, (context->getGpu(), scratch Key)); 224 TestResource* scratch =
225 TestResource::CreateScratchTestResource(context->getGpu(),
226 TestResource::kProperty2_Sim ulatedProperty);
210 scratch->setSize(10); 227 scratch->setSize(10);
211 TestResource* content = SkNEW_ARGS(TestResource, (context->getGpu())); 228 TestResource* content = SkNEW_ARGS(TestResource, (context->getGpu()));
212 content->setSize(11); 229 content->setSize(11);
213 REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey)); 230 REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey));
214 TestResource* wrapped = SkNEW_ARGS(TestResource, 231 TestResource* wrapped = SkNEW_ARGS(TestResource,
215 (context->getGpu(), GrGpuResource::kWrapp ed_LifeCycle)); 232 (context->getGpu(), GrGpuResource::kWrapp ed_LifeCycle));
216 wrapped->setSize(12); 233 wrapped->setSize(12);
217 TestResource* unbudgeted = SkNEW_ARGS(TestResource, 234 TestResource* unbudgeted = SkNEW_ARGS(TestResource,
218 (context->getGpu(), GrGpuResource::kUn cached_LifeCycle)); 235 (context->getGpu(), GrGpuResource::kUn cached_LifeCycle));
219 unbudgeted->setSize(13); 236 unbudgeted->setSize(13);
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 REPORTER_ASSERT(reporter, SkToBool(context)); 303 REPORTER_ASSERT(reporter, SkToBool(context));
287 if (NULL == context) { 304 if (NULL == context) {
288 return; 305 return;
289 } 306 }
290 context->setResourceCacheLimits(10, 300); 307 context->setResourceCacheLimits(10, 300);
291 GrResourceCache2* cache2 = context->getResourceCache2(); 308 GrResourceCache2* cache2 = context->getResourceCache2();
292 cache2->purgeAllUnlocked(); 309 cache2->purgeAllUnlocked();
293 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()) ; 310 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()) ;
294 SkASSERT(0 == cache2->getBudgetedResourceCount() && 0 == cache2->getBudgeted ResourceBytes()); 311 SkASSERT(0 == cache2->getBudgetedResourceCount() && 0 == cache2->getBudgeted ResourceBytes());
295 312
296 GrScratchKey scratchKey;
297 make_scratch_key(&scratchKey);
298
299 GrCacheID::Key keyData; 313 GrCacheID::Key keyData;
300 memset(&keyData, 0, sizeof(keyData)); 314 memset(&keyData, 0, sizeof(keyData));
301 GrResourceKey contentKey(GrCacheID(GrCacheID::GenerateDomain(), keyData), 0) ; 315 GrResourceKey contentKey(GrCacheID(GrCacheID::GenerateDomain(), keyData), 0) ;
302 316
303 TestResource* scratch; 317 TestResource* scratch;
304 TestResource* content; 318 TestResource* content;
305 TestResource* wrapped; 319 TestResource* wrapped;
306 TestResource* unbudgeted; 320 TestResource* unbudgeted;
307 321
308 // A large uncached or wrapped resource shouldn't evict anything. 322 // A large uncached or wrapped resource shouldn't evict anything.
309 scratch = SkNEW_ARGS(TestResource, (context->getGpu(), scratchKey)); 323 scratch = TestResource::CreateScratchTestResource(context->getGpu(),
324 TestResource::kProperty2_S imulatedProperty);
310 scratch->setSize(10); 325 scratch->setSize(10);
311 scratch->unref(); 326 scratch->unref();
312 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); 327 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount());
313 REPORTER_ASSERT(reporter, 10 == cache2->getResourceBytes()); 328 REPORTER_ASSERT(reporter, 10 == cache2->getResourceBytes());
314 REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount()); 329 REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount());
315 REPORTER_ASSERT(reporter, 10 == cache2->getBudgetedResourceBytes()); 330 REPORTER_ASSERT(reporter, 10 == cache2->getBudgetedResourceBytes());
316 331
317 content = SkNEW_ARGS(TestResource, (context->getGpu())); 332 content = SkNEW_ARGS(TestResource, (context->getGpu()));
318 content->setSize(11); 333 content->setSize(11);
319 REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey)); 334 REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey));
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); 376 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
362 REPORTER_ASSERT(reporter, SkToBool(context)); 377 REPORTER_ASSERT(reporter, SkToBool(context));
363 if (NULL == context) { 378 if (NULL == context) {
364 return; 379 return;
365 } 380 }
366 context->setResourceCacheLimits(5, 30000); 381 context->setResourceCacheLimits(5, 30000);
367 GrResourceCache2* cache2 = context->getResourceCache2(); 382 GrResourceCache2* cache2 = context->getResourceCache2();
368 cache2->purgeAllUnlocked(); 383 cache2->purgeAllUnlocked();
369 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()) ; 384 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()) ;
370 385
371 GrScratchKey scratchKey;
372 make_scratch_key(&scratchKey);
373
374 // Create two resources that have the same scratch key. 386 // Create two resources that have the same scratch key.
375 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu(), scratchKey)); 387 TestResource* a =
376 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu(), scratchKey)); 388 TestResource::CreateScratchTestResource(context->getGpu(),
389 TestResource::kProperty2_Sim ulatedProperty);
390 TestResource* b =
391 TestResource::CreateScratchTestResource(context->getGpu(),
392 TestResource::kProperty2_Sim ulatedProperty);
377 a->setSize(11); 393 a->setSize(11);
378 b->setSize(12); 394 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
379 // Scratch resources are registered with GrResourceCache2 just by existing. There are 2. 403 // Scratch resources are registered with GrResourceCache2 just by existing. There are 2.
380 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); 404 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
381 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey (scratchKey));) 405 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey (scratchKey));)
382 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); 406 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount());
383 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == 407 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
384 cache2->getResourceBytes()); 408 cache2->getResourceBytes());
385 409
386 // Our refs mean that the resources are non purgable. 410 // Our refs mean that the resources are non purgable.
387 cache2->purgeAllUnlocked(); 411 cache2->purgeAllUnlocked();
388 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); 412 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
(...skipping 16 matching lines...) Expand all
405 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); 429 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
406 REPORTER_ASSERT(reporter, SkToBool(context)); 430 REPORTER_ASSERT(reporter, SkToBool(context));
407 if (NULL == context) { 431 if (NULL == context) {
408 return; 432 return;
409 } 433 }
410 context->setResourceCacheLimits(5, 30000); 434 context->setResourceCacheLimits(5, 30000);
411 GrResourceCache2* cache2 = context->getResourceCache2(); 435 GrResourceCache2* cache2 = context->getResourceCache2();
412 cache2->purgeAllUnlocked(); 436 cache2->purgeAllUnlocked();
413 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()) ; 437 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()) ;
414 438
415 GrScratchKey scratchKey;
416 make_scratch_key(&scratchKey);
417
418 // Create two resources that have the same scratch key. 439 // Create two resources that have the same scratch key.
419 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu(), scratchKey)); 440 TestResource* a =
420 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu(), scratchKey)); 441 TestResource::CreateScratchTestResource(context->getGpu(),
442 TestResource::kProperty2_Sim ulatedProperty);
443 TestResource* b =
444 TestResource::CreateScratchTestResource(context->getGpu(),
445 TestResource::kProperty2_Sim ulatedProperty);
421 a->unref(); 446 a->unref();
422 b->unref(); 447 b->unref();
423 448
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
424 // Scratch resources are registered with GrResourceCache2 just by existing. There are 2. 456 // Scratch resources are registered with GrResourceCache2 just by existing. There are 2.
457 TestResource::ComputeScratchKey(TestResource::kProperty2_SimulatedProperty, &scratchKey);
425 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); 458 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
426 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey (scratchKey));) 459 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey (scratchKey));)
427 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); 460 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount());
428 461
429 // Find the first resource and remove its scratch key 462 // Find the first resource and remove its scratch key
430 GrGpuResource* find; 463 GrGpuResource* find;
431 find = cache2->findAndRefScratchResource(scratchKey); 464 find = cache2->findAndRefScratchResource(scratchKey);
432 find->cacheAccess().removeScratchKey(); 465 find->cacheAccess().removeScratchKey();
433 // It's still alive, but not cached by scratch key anymore 466 // It's still alive, but not cached by scratch key anymore
434 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); 467 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
(...skipping 18 matching lines...) Expand all
453 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); 486 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
454 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey (scratchKey));) 487 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey (scratchKey));)
455 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); 488 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount());
456 489
457 find->unref(); 490 find->unref();
458 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); 491 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
459 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey (scratchKey));) 492 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey (scratchKey));)
460 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); 493 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount());
461 } 494 }
462 495
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
463 static void test_duplicate_content_key(skiatest::Reporter* reporter) { 563 static void test_duplicate_content_key(skiatest::Reporter* reporter) {
464 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); 564 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
465 REPORTER_ASSERT(reporter, SkToBool(context)); 565 REPORTER_ASSERT(reporter, SkToBool(context));
466 if (NULL == context) { 566 if (NULL == context) {
467 return; 567 return;
468 } 568 }
469 context->setResourceCacheLimits(5, 30000); 569 context->setResourceCacheLimits(5, 30000);
470 GrResourceCache2* cache2 = context->getResourceCache2(); 570 GrResourceCache2* cache2 = context->getResourceCache2();
471 cache2->purgeAllUnlocked(); 571 cache2->purgeAllUnlocked();
472 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()) ; 572 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()) ;
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
802 test_cache(reporter, context, surface->getCanvas()); 902 test_cache(reporter, context, surface->getCanvas());
803 } 903 }
804 904
805 // The below tests create their own mock contexts. 905 // The below tests create their own mock contexts.
806 test_no_key(reporter); 906 test_no_key(reporter);
807 test_budgeting(reporter); 907 test_budgeting(reporter);
808 test_unbudgeted(reporter); 908 test_unbudgeted(reporter);
809 test_duplicate_content_key(reporter); 909 test_duplicate_content_key(reporter);
810 test_duplicate_scratch_key(reporter); 910 test_duplicate_scratch_key(reporter);
811 test_remove_scratch_key(reporter); 911 test_remove_scratch_key(reporter);
912 test_scratch_key_consistency(reporter);
812 test_purge_invalidated(reporter); 913 test_purge_invalidated(reporter);
813 test_cache_chained_purge(reporter); 914 test_cache_chained_purge(reporter);
814 test_resource_size_changed(reporter); 915 test_resource_size_changed(reporter);
815 test_large_resource_count(reporter); 916 test_large_resource_count(reporter);
816 } 917 }
817 918
818 #endif 919 #endif
OLDNEW
« no previous file with comments | « include/gpu/GrResourceKey.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698