Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2014 Google Inc. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license that can be | |
| 5 * found in the LICENSE file. | |
| 6 */ | |
| 7 | |
| 8 #if SK_SUPPORT_GPU | |
| 9 | |
| 10 #include "gl/GrGLNameAllocator.h" | |
| 11 #include "Test.h" | |
| 12 | |
| 13 //////////////////////////////////////////////////////////////////////////////// | |
| 14 | |
| 15 class NameLeakTest { | |
|
bsalomon
2014/06/09 13:52:21
style nit: Lots of missing this-> for method calls
Chris Dalton
2014/06/09 17:27:28
Done.
| |
| 16 static const GrGLuint kFirstName = 101; | |
| 17 static const GrGLuint kRange = 1013; | |
| 18 | |
| 19 public: | |
| 20 NameLeakTest(skiatest::Reporter* reporter) | |
| 21 : fReporter(reporter), | |
| 22 fAllocator(kFirstName, kFirstName + kRange), | |
| 23 fAllocatedCount(0), | |
| 24 fRandomName(kFirstName + 4 * kRange / 7) { | |
| 25 memset(fAllocatedNames, 0, sizeof(fAllocatedNames)); | |
| 26 } | |
| 27 | |
| 28 bool run() { | |
| 29 if (!allocateAllRemaining()) { | |
| 30 return false; | |
| 31 } | |
| 32 | |
| 33 for (GrGLuint freeCount = 1; freeCount <= kRange; ++freeCount) { | |
| 34 if (!freeRandomNames(freeCount)) { | |
| 35 return false; | |
| 36 } | |
| 37 if (!allocateAllRemaining()) { | |
| 38 return false; | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 return true; | |
| 43 } | |
| 44 | |
| 45 private: | |
| 46 bool isAllocated(GrGLuint name) const { | |
| 47 return fAllocatedNames[name - kFirstName]; | |
| 48 } | |
| 49 | |
| 50 void setAllocated(GrGLuint name, bool allocated) { | |
| 51 fAllocatedNames[name - kFirstName] = allocated; | |
| 52 } | |
| 53 | |
| 54 bool allocateAllRemaining() { | |
| 55 for (; fAllocatedCount < kRange; ++fAllocatedCount) { | |
| 56 GrGLuint name = fAllocator.allocateName(); | |
| 57 if (0 == name) { | |
| 58 ERRORF(fReporter, | |
| 59 "Name allocate failed, but there should still be %u free names", | |
| 60 kRange - fAllocatedCount); | |
| 61 return false; | |
| 62 } | |
| 63 if (name < kFirstName || name >= kFirstName + kRange) { | |
| 64 ERRORF(fReporter, | |
| 65 "Name allocate returned name %u outside its bounds [%u, % u)", | |
| 66 name, kFirstName, kFirstName + kRange); | |
| 67 return false; | |
| 68 } | |
| 69 if (isAllocated(name)) { | |
| 70 ERRORF(fReporter, "Name allocate returned name that is already a llocated"); | |
| 71 return false; | |
| 72 } | |
| 73 | |
| 74 setAllocated(name, true); | |
| 75 } | |
| 76 | |
| 77 // Ensure it returns 0 once all the names are allocated. | |
| 78 GrGLuint name = fAllocator.allocateName(); | |
| 79 if (0 != name) { | |
| 80 ERRORF(fReporter, | |
| 81 "Name allocate did not fail when all names were already in us e"); | |
| 82 return false; | |
| 83 } | |
| 84 | |
| 85 // Ensure every unique name is allocated. | |
| 86 for (GrGLuint i = 0; i < kRange; ++i) { | |
| 87 if (!isAllocated(kFirstName + i)) { | |
| 88 ERRORF(fReporter, "Not all unique names are allocated after allo cateAllRemaining()"); | |
| 89 return false; | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 return true; | |
| 94 } | |
| 95 | |
| 96 bool freeRandomNames(GrGLuint count) { | |
| 97 // The values a and c make up an LCG (pseudo-random generator). These | |
| 98 // values must satisfy the Hull-Dobell Theorem (with m=kRange): | |
| 99 // http://en.wikipedia.org/wiki/Linear_congruential_generator | |
| 100 const GrGLuint seed = (count + fRandomName) / 2; | |
|
bsalomon
2014/06/09 13:52:21
Is there a reason for adding a new random number g
Chris Dalton
2014/06/09 16:59:39
.. Because I wasn't aware of SkRandom?
But also,
bsalomon
2014/06/09 17:09:42
Ah, ok, that makes sense.
| |
| 101 const GrGLuint a = seed * kRange + 1; | |
| 102 const GrGLuint c = (seed * 743) % kRange; | |
| 103 | |
| 104 for (GrGLuint i = 0; i < count; ++i) { | |
| 105 fRandomName = (a * fRandomName + c) % kRange; | |
| 106 const GrGLuint name = kFirstName + fRandomName; | |
| 107 if (!isAllocated(name)) { | |
| 108 ERRORF(fReporter, "Test bug: Should not free a not-allocated nam e at this point (%u)", i); | |
| 109 return false; | |
| 110 } | |
| 111 | |
| 112 fAllocator.free(name); | |
| 113 setAllocated(name, false); | |
| 114 --fAllocatedCount; | |
| 115 } | |
| 116 | |
| 117 return true; | |
| 118 } | |
| 119 | |
| 120 skiatest::Reporter* fReporter; | |
| 121 GrGLNameAllocator fAllocator; | |
| 122 bool fAllocatedNames[kRange]; | |
| 123 GrGLuint fAllocatedCount; | |
| 124 GrGLuint fRandomName; | |
| 125 }; | |
| 126 | |
| 127 DEF_GPUTEST(NameAllocator, reporter, factory) { | |
| 128 // Ensure no names are leaked or double-allocated during heavy usage. | |
| 129 { | |
| 130 NameLeakTest nameLeakTest(reporter); | |
| 131 nameLeakTest.run(); | |
| 132 } | |
| 133 | |
| 134 static const GrGLuint range = 32; | |
| 135 GrGLNameAllocator allocator(1, 1 + range); | |
| 136 for (GrGLuint i = 1; i <= range; ++i) { | |
| 137 allocator.allocateName(); | |
| 138 } | |
| 139 REPORTER_ASSERT(reporter, 0 == allocator.allocateName()); | |
| 140 | |
| 141 // Test freeing names out of range. | |
| 142 allocator.free(allocator.firstName() - 1); | |
| 143 allocator.free(allocator.endName()); | |
| 144 REPORTER_ASSERT(reporter, 0 == allocator.allocateName()); | |
| 145 | |
| 146 // Test freeing not-allocated names. | |
| 147 for (GrGLuint i = 1; i <= range/2; i += 2) { | |
| 148 allocator.free(i); | |
| 149 } | |
| 150 for (GrGLuint i = 1; i <= range/2; i += 2) { | |
| 151 // None of these names will be allocated. | |
| 152 allocator.free(i); | |
| 153 } | |
| 154 for (GrGLuint i = 1; i <= range/2; ++i) { | |
| 155 // Every other name will not be be allocated. | |
| 156 allocator.free(i); | |
| 157 } | |
| 158 for (GrGLuint i = 1; i <= range/2; ++i) { | |
| 159 if (0 == allocator.allocateName()) { | |
| 160 ERRORF(reporter, "Name allocate failed when there should be free nam es"); | |
| 161 break; | |
| 162 } | |
| 163 } | |
| 164 REPORTER_ASSERT(reporter, 0 == allocator.allocateName()); | |
| 165 } | |
| 166 | |
| 167 #endif | |
| OLD | NEW |