| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2014 Google Inc. | 3 * Copyright 2014 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #include "GrGLNameAllocator.h" | 9 #include "GrGLNameAllocator.h" |
| 10 | 10 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 | 57 |
| 58 /** | 58 /** |
| 59 * Remove the leftmost leaf node from this range (or the entire thing if it | 59 * Remove the leftmost leaf node from this range (or the entire thing if it |
| 60 * *is* a leaf node). This is an internal helper method that is used after | 60 * *is* a leaf node). This is an internal helper method that is used after |
| 61 * an allocation if one contiguous range became adjacent to another. (The | 61 * an allocation if one contiguous range became adjacent to another. (The |
| 62 * range gets removed so the one immediately before can be extended, | 62 * range gets removed so the one immediately before can be extended, |
| 63 * collapsing the two into one.) | 63 * collapsing the two into one.) |
| 64 * | 64 * |
| 65 * @param removedCount A pointer that receives the size of the contiguous | 65 * @param removedCount A pointer that receives the size of the contiguous |
| 66 range that was removed. | 66 range that was removed. |
| 67 * @return The resulting SparseNameRange after the removal (or NULL if it | 67 * @return The resulting SparseNameRange after the removal (or nullptr if it |
| 68 * became empty). Note that this call is destructive, so the | 68 * became empty). Note that this call is destructive, so the |
| 69 * original SparseNameRange will no longer be valid afterward. The | 69 * original SparseNameRange will no longer be valid afterward. The |
| 70 * caller must always update its pointer with the new | 70 * caller must always update its pointer with the new |
| 71 * SparseNameRange. | 71 * SparseNameRange. |
| 72 */ | 72 */ |
| 73 virtual SparseNameRange* SK_WARN_UNUSED_RESULT removeLeftmostContiguousRange
(GrGLuint* removedCount) = 0; | 73 virtual SparseNameRange* SK_WARN_UNUSED_RESULT removeLeftmostContiguousRange
(GrGLuint* removedCount) = 0; |
| 74 | 74 |
| 75 /** | 75 /** |
| 76 * Append adjacent allocated names to the end of this range. This operation | 76 * Append adjacent allocated names to the end of this range. This operation |
| 77 * does not affect the structure of the tree. The caller is responsible for | 77 * does not affect the structure of the tree. The caller is responsible for |
| (...skipping 15 matching lines...) Expand all Loading... |
| 93 */ | 93 */ |
| 94 virtual GrGLuint prependNames(GrGLuint count) = 0; | 94 virtual GrGLuint prependNames(GrGLuint count) = 0; |
| 95 | 95 |
| 96 /** | 96 /** |
| 97 * Free a name so it is no longer tracked as allocated. If the name is at | 97 * Free a name so it is no longer tracked as allocated. If the name is at |
| 98 * the very beginning or very end of the range, the boundaries [fFirst, fEnd
) | 98 * the very beginning or very end of the range, the boundaries [fFirst, fEnd
) |
| 99 * will be tightened. | 99 * will be tightened. |
| 100 * | 100 * |
| 101 * @param name The name to free. Not-allocated names are silently ignored | 101 * @param name The name to free. Not-allocated names are silently ignored |
| 102 * the same way they are in the OpenGL spec. | 102 * the same way they are in the OpenGL spec. |
| 103 * @return The resulting SparseNameRange after the free (or NULL if it | 103 * @return The resulting SparseNameRange after the free (or nullptr if it |
| 104 * became empty). Note that this call is destructive, so the | 104 * became empty). Note that this call is destructive, so the |
| 105 * original SparseNameRange will no longer be valid afterward. The | 105 * original SparseNameRange will no longer be valid afterward. The |
| 106 * caller must always update its pointer with the new | 106 * caller must always update its pointer with the new |
| 107 * SparseNameRange. | 107 * SparseNameRange. |
| 108 */ | 108 */ |
| 109 virtual SparseNameRange* SK_WARN_UNUSED_RESULT free(GrGLuint name) = 0; | 109 virtual SparseNameRange* SK_WARN_UNUSED_RESULT free(GrGLuint name) = 0; |
| 110 | 110 |
| 111 protected: | 111 protected: |
| 112 SparseNameRange* takeRef() { | 112 SparseNameRange* takeRef() { |
| 113 this->ref(); | 113 this->ref(); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 139 if (0 != *outName) { | 139 if (0 != *outName) { |
| 140 this->updateStats(); | 140 this->updateStats(); |
| 141 return this->rebalance(); | 141 return this->rebalance(); |
| 142 } | 142 } |
| 143 | 143 |
| 144 if (fLeft->end() + 1 == fRight->first()) { | 144 if (fLeft->end() + 1 == fRight->first()) { |
| 145 // It closed the gap between fLeft and fRight; merge. | 145 // It closed the gap between fLeft and fRight; merge. |
| 146 GrGLuint removedCount; | 146 GrGLuint removedCount; |
| 147 fRight.reset(fRight->removeLeftmostContiguousRange(&removedCount)); | 147 fRight.reset(fRight->removeLeftmostContiguousRange(&removedCount)); |
| 148 *outName = fLeft->appendNames(1 + removedCount); | 148 *outName = fLeft->appendNames(1 + removedCount); |
| 149 if (NULL == fRight.get()) { | 149 if (nullptr == fRight.get()) { |
| 150 return fLeft.detach(); | 150 return fLeft.detach(); |
| 151 } | 151 } |
| 152 this->updateStats(); | 152 this->updateStats(); |
| 153 return this->rebalance(); | 153 return this->rebalance(); |
| 154 } | 154 } |
| 155 | 155 |
| 156 // There is guaranteed to be a gap between fLeft and fRight, and the | 156 // There is guaranteed to be a gap between fLeft and fRight, and the |
| 157 // "size 1" case has already been covered. | 157 // "size 1" case has already been covered. |
| 158 SkASSERT(fLeft->end() + 1 < fRight->first()); | 158 SkASSERT(fLeft->end() + 1 < fRight->first()); |
| 159 *outName = fLeft->appendNames(1); | 159 *outName = fLeft->appendNames(1); |
| 160 return this->takeRef(); | 160 return this->takeRef(); |
| 161 } | 161 } |
| 162 | 162 |
| 163 SparseNameRange* SK_WARN_UNUSED_RESULT removeLeftmostContiguousRange(GrGLuin
t* removedCount) override { | 163 SparseNameRange* SK_WARN_UNUSED_RESULT removeLeftmostContiguousRange(GrGLuin
t* removedCount) override { |
| 164 fLeft.reset(fLeft->removeLeftmostContiguousRange(removedCount)); | 164 fLeft.reset(fLeft->removeLeftmostContiguousRange(removedCount)); |
| 165 if (NULL == fLeft) { | 165 if (nullptr == fLeft) { |
| 166 return fRight.detach(); | 166 return fRight.detach(); |
| 167 } | 167 } |
| 168 this->updateStats(); | 168 this->updateStats(); |
| 169 return this->rebalance(); | 169 return this->rebalance(); |
| 170 } | 170 } |
| 171 | 171 |
| 172 GrGLuint appendNames(GrGLuint count) override { | 172 GrGLuint appendNames(GrGLuint count) override { |
| 173 SkASSERT(fEnd + count > fEnd); // Check for integer wrap. | 173 SkASSERT(fEnd + count > fEnd); // Check for integer wrap. |
| 174 GrGLuint name = fRight->appendNames(count); | 174 GrGLuint name = fRight->appendNames(count); |
| 175 SkASSERT(fRight->end() == fEnd + count); | 175 SkASSERT(fRight->end() == fEnd + count); |
| 176 this->updateStats(); | 176 this->updateStats(); |
| 177 return name; | 177 return name; |
| 178 } | 178 } |
| 179 | 179 |
| 180 GrGLuint prependNames(GrGLuint count) override { | 180 GrGLuint prependNames(GrGLuint count) override { |
| 181 SkASSERT(fFirst > count); // We can't allocate at or below 0. | 181 SkASSERT(fFirst > count); // We can't allocate at or below 0. |
| 182 GrGLuint name = fLeft->prependNames(count); | 182 GrGLuint name = fLeft->prependNames(count); |
| 183 SkASSERT(fLeft->first() == fFirst - count); | 183 SkASSERT(fLeft->first() == fFirst - count); |
| 184 this->updateStats(); | 184 this->updateStats(); |
| 185 return name; | 185 return name; |
| 186 } | 186 } |
| 187 | 187 |
| 188 SparseNameRange* SK_WARN_UNUSED_RESULT free(GrGLuint name) override { | 188 SparseNameRange* SK_WARN_UNUSED_RESULT free(GrGLuint name) override { |
| 189 if (name < fLeft->end()) { | 189 if (name < fLeft->end()) { |
| 190 fLeft.reset(fLeft->free(name)); | 190 fLeft.reset(fLeft->free(name)); |
| 191 if (NULL == fLeft) { | 191 if (nullptr == fLeft) { |
| 192 // fLeft became empty after the free. | 192 // fLeft became empty after the free. |
| 193 return fRight.detach(); | 193 return fRight.detach(); |
| 194 } | 194 } |
| 195 this->updateStats(); | 195 this->updateStats(); |
| 196 return this->rebalance(); | 196 return this->rebalance(); |
| 197 } else { | 197 } else { |
| 198 fRight.reset(fRight->free(name)); | 198 fRight.reset(fRight->free(name)); |
| 199 if (NULL == fRight) { | 199 if (nullptr == fRight) { |
| 200 // fRight became empty after the free. | 200 // fRight became empty after the free. |
| 201 return fLeft.detach(); | 201 return fLeft.detach(); |
| 202 } | 202 } |
| 203 this->updateStats(); | 203 this->updateStats(); |
| 204 return this->rebalance(); | 204 return this->rebalance(); |
| 205 } | 205 } |
| 206 } | 206 } |
| 207 | 207 |
| 208 private: | 208 private: |
| 209 typedef SkAutoTUnref<SparseNameRange> SparseNameTree::* ChildRange; | 209 typedef SkAutoTUnref<SparseNameRange> SparseNameTree::* ChildRange; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 fHeight = 0; | 280 fHeight = 0; |
| 281 } | 281 } |
| 282 | 282 |
| 283 SparseNameRange* SK_WARN_UNUSED_RESULT internalAllocate(GrGLuint* outName) o
verride { | 283 SparseNameRange* SK_WARN_UNUSED_RESULT internalAllocate(GrGLuint* outName) o
verride { |
| 284 *outName = 0; // No internal gaps, we are contiguous. | 284 *outName = 0; // No internal gaps, we are contiguous. |
| 285 return this->takeRef(); | 285 return this->takeRef(); |
| 286 } | 286 } |
| 287 | 287 |
| 288 SparseNameRange* SK_WARN_UNUSED_RESULT removeLeftmostContiguousRange(GrGLuin
t* removedCount) override { | 288 SparseNameRange* SK_WARN_UNUSED_RESULT removeLeftmostContiguousRange(GrGLuin
t* removedCount) override { |
| 289 *removedCount = fEnd - fFirst; | 289 *removedCount = fEnd - fFirst; |
| 290 return NULL; | 290 return nullptr; |
| 291 } | 291 } |
| 292 | 292 |
| 293 GrGLuint appendNames(GrGLuint count) override { | 293 GrGLuint appendNames(GrGLuint count) override { |
| 294 SkASSERT(fEnd + count > fEnd); // Check for integer wrap. | 294 SkASSERT(fEnd + count > fEnd); // Check for integer wrap. |
| 295 GrGLuint name = fEnd; | 295 GrGLuint name = fEnd; |
| 296 fEnd += count; | 296 fEnd += count; |
| 297 return name; | 297 return name; |
| 298 } | 298 } |
| 299 | 299 |
| 300 GrGLuint prependNames(GrGLuint count) override { | 300 GrGLuint prependNames(GrGLuint count) override { |
| 301 SkASSERT(fFirst > count); // We can't allocate at or below 0. | 301 SkASSERT(fFirst > count); // We can't allocate at or below 0. |
| 302 fFirst -= count; | 302 fFirst -= count; |
| 303 return fFirst; | 303 return fFirst; |
| 304 } | 304 } |
| 305 | 305 |
| 306 SparseNameRange* SK_WARN_UNUSED_RESULT free(GrGLuint name) override { | 306 SparseNameRange* SK_WARN_UNUSED_RESULT free(GrGLuint name) override { |
| 307 if (name < fFirst || name >= fEnd) { | 307 if (name < fFirst || name >= fEnd) { |
| 308 // Not-allocated names are silently ignored. | 308 // Not-allocated names are silently ignored. |
| 309 return this->takeRef(); | 309 return this->takeRef(); |
| 310 } | 310 } |
| 311 | 311 |
| 312 if (fFirst == name) { | 312 if (fFirst == name) { |
| 313 ++fFirst; | 313 ++fFirst; |
| 314 return (fEnd == fFirst) ? NULL : this->takeRef(); | 314 return (fEnd == fFirst) ? nullptr : this->takeRef(); |
| 315 } | 315 } |
| 316 | 316 |
| 317 if (fEnd == name + 1) { | 317 if (fEnd == name + 1) { |
| 318 --fEnd; | 318 --fEnd; |
| 319 return this->takeRef(); | 319 return this->takeRef(); |
| 320 } | 320 } |
| 321 | 321 |
| 322 SparseNameRange* left = new ContiguousNameRange(fFirst, name); | 322 SparseNameRange* left = new ContiguousNameRange(fFirst, name); |
| 323 SparseNameRange* right = this->takeRef(); | 323 SparseNameRange* right = this->takeRef(); |
| 324 fFirst = name + 1; | 324 fFirst = name + 1; |
| 325 return new SparseNameTree(left, right); | 325 return new SparseNameTree(left, right); |
| 326 } | 326 } |
| 327 }; | 327 }; |
| 328 | 328 |
| 329 GrGLNameAllocator::GrGLNameAllocator(GrGLuint firstName, GrGLuint endName) | 329 GrGLNameAllocator::GrGLNameAllocator(GrGLuint firstName, GrGLuint endName) |
| 330 : fFirstName(firstName), | 330 : fFirstName(firstName), |
| 331 fEndName(endName) { | 331 fEndName(endName) { |
| 332 SkASSERT(firstName > 0); | 332 SkASSERT(firstName > 0); |
| 333 SkASSERT(endName > firstName); | 333 SkASSERT(endName > firstName); |
| 334 } | 334 } |
| 335 | 335 |
| 336 GrGLNameAllocator::~GrGLNameAllocator() { | 336 GrGLNameAllocator::~GrGLNameAllocator() { |
| 337 } | 337 } |
| 338 | 338 |
| 339 GrGLuint GrGLNameAllocator::allocateName() { | 339 GrGLuint GrGLNameAllocator::allocateName() { |
| 340 if (NULL == fAllocatedNames.get()) { | 340 if (nullptr == fAllocatedNames.get()) { |
| 341 fAllocatedNames.reset(new ContiguousNameRange(fFirstName, fFirstName + 1
)); | 341 fAllocatedNames.reset(new ContiguousNameRange(fFirstName, fFirstName + 1
)); |
| 342 return fFirstName; | 342 return fFirstName; |
| 343 } | 343 } |
| 344 | 344 |
| 345 if (fAllocatedNames->first() > fFirstName) { | 345 if (fAllocatedNames->first() > fFirstName) { |
| 346 return fAllocatedNames->prependNames(1); | 346 return fAllocatedNames->prependNames(1); |
| 347 } | 347 } |
| 348 | 348 |
| 349 GrGLuint name; | 349 GrGLuint name; |
| 350 fAllocatedNames.reset(fAllocatedNames->internalAllocate(&name)); | 350 fAllocatedNames.reset(fAllocatedNames->internalAllocate(&name)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 361 } | 361 } |
| 362 | 362 |
| 363 void GrGLNameAllocator::free(GrGLuint name) { | 363 void GrGLNameAllocator::free(GrGLuint name) { |
| 364 if (!fAllocatedNames.get()) { | 364 if (!fAllocatedNames.get()) { |
| 365 // Not-allocated names are silently ignored. | 365 // Not-allocated names are silently ignored. |
| 366 return; | 366 return; |
| 367 } | 367 } |
| 368 | 368 |
| 369 fAllocatedNames.reset(fAllocatedNames->free(name)); | 369 fAllocatedNames.reset(fAllocatedNames->free(name)); |
| 370 } | 370 } |
| OLD | NEW |