| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 #include "SkRTree.h" | 8 #include "SkRTree.h" |
| 9 #include "SkTSort.h" | 9 #include "SkTSort.h" |
| 10 | 10 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 SkASSERT(minChildren < maxChildren && minChildren > 0 && maxChildren < | 37 SkASSERT(minChildren < maxChildren && minChildren > 0 && maxChildren < |
| 38 static_cast<int>(SK_MaxU16)); | 38 static_cast<int>(SK_MaxU16)); |
| 39 SkASSERT((maxChildren + 1) / 2 >= minChildren); | 39 SkASSERT((maxChildren + 1) / 2 >= minChildren); |
| 40 this->validate(); | 40 this->validate(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 SkRTree::~SkRTree() { | 43 SkRTree::~SkRTree() { |
| 44 this->clear(); | 44 this->clear(); |
| 45 } | 45 } |
| 46 | 46 |
| 47 void SkRTree::insert(void* data, const SkRect& fbounds, bool defer) { | 47 void SkRTree::insert(unsigned opIndex, const SkRect& fbounds, bool defer) { |
| 48 SkIRect bounds; | 48 SkIRect bounds; |
| 49 if (fbounds.isLargest()) { | 49 if (fbounds.isLargest()) { |
| 50 bounds.setLargest(); | 50 bounds.setLargest(); |
| 51 } else { | 51 } else { |
| 52 fbounds.roundOut(&bounds); | 52 fbounds.roundOut(&bounds); |
| 53 } | 53 } |
| 54 | 54 |
| 55 this->validate(); | 55 this->validate(); |
| 56 if (bounds.isEmpty()) { | 56 if (bounds.isEmpty()) { |
| 57 SkASSERT(false); | 57 SkASSERT(false); |
| 58 return; | 58 return; |
| 59 } | 59 } |
| 60 Branch newBranch; | 60 Branch newBranch; |
| 61 newBranch.fBounds = bounds; | 61 newBranch.fBounds = bounds; |
| 62 newBranch.fChild.data = data; | 62 newBranch.fChild.opIndex = opIndex; |
| 63 if (this->isEmpty()) { | 63 if (this->isEmpty()) { |
| 64 // since a bulk-load into an existing tree is as of yet unimplemented (a
nd arguably not | 64 // since a bulk-load into an existing tree is as of yet unimplemented (a
nd arguably not |
| 65 // of vital importance right now), we only batch up inserts if the tree
is empty. | 65 // of vital importance right now), we only batch up inserts if the tree
is empty. |
| 66 if (defer) { | 66 if (defer) { |
| 67 fDeferredInserts.push(newBranch); | 67 fDeferredInserts.push(newBranch); |
| 68 return; | 68 return; |
| 69 } else { | 69 } else { |
| 70 fRoot.fChild.subtree = allocateNode(0); | 70 fRoot.fChild.subtree = allocateNode(0); |
| 71 fRoot.fChild.subtree->fNumChildren = 0; | 71 fRoot.fChild.subtree->fNumChildren = 0; |
| 72 } | 72 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 102 fRoot = this->bulkLoad(&fDeferredInserts); | 102 fRoot = this->bulkLoad(&fDeferredInserts); |
| 103 } | 103 } |
| 104 } else { | 104 } else { |
| 105 // TODO: some algorithm for bulk loading into an already populated tree | 105 // TODO: some algorithm for bulk loading into an already populated tree |
| 106 SkASSERT(0 == fDeferredInserts.count()); | 106 SkASSERT(0 == fDeferredInserts.count()); |
| 107 } | 107 } |
| 108 fDeferredInserts.rewind(); | 108 fDeferredInserts.rewind(); |
| 109 this->validate(); | 109 this->validate(); |
| 110 } | 110 } |
| 111 | 111 |
| 112 void SkRTree::search(const SkRect& fquery, SkTDArray<void*>* results) const { | 112 void SkRTree::search(const SkRect& fquery, SkTDArray<unsigned>* results) const { |
| 113 SkIRect query; | 113 SkIRect query; |
| 114 fquery.roundOut(&query); | 114 fquery.roundOut(&query); |
| 115 this->validate(); | 115 this->validate(); |
| 116 SkASSERT(0 == fDeferredInserts.count()); // If this fails, you should have
flushed. | 116 SkASSERT(0 == fDeferredInserts.count()); // If this fails, you should have
flushed. |
| 117 if (!this->isEmpty() && SkIRect::IntersectsNoEmptyCheck(fRoot.fBounds, query
)) { | 117 if (!this->isEmpty() && SkIRect::IntersectsNoEmptyCheck(fRoot.fBounds, query
)) { |
| 118 this->search(fRoot.fChild.subtree, query, results); | 118 this->search(fRoot.fChild.subtree, query, results); |
| 119 } | 119 } |
| 120 this->validate(); | 120 this->validate(); |
| 121 } | 121 } |
| 122 | 122 |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 | 302 |
| 303 // replicate the sort of the winning distribution, (we can skip this if the
last | 303 // replicate the sort of the winning distribution, (we can skip this if the
last |
| 304 // sort ended up being best) | 304 // sort ended up being best) |
| 305 if (!(axis == 1 && sortSide == 1)) { | 305 if (!(axis == 1 && sortSide == 1)) { |
| 306 SkTQSort(children, children + fMaxChildren, RectLessThan(sorts[axis][sor
tSide])); | 306 SkTQSort(children, children + fMaxChildren, RectLessThan(sorts[axis][sor
tSide])); |
| 307 } | 307 } |
| 308 | 308 |
| 309 return fMinChildren - 1 + k; | 309 return fMinChildren - 1 + k; |
| 310 } | 310 } |
| 311 | 311 |
| 312 void SkRTree::search(Node* root, const SkIRect query, SkTDArray<void*>* results)
const { | 312 void SkRTree::search(Node* root, const SkIRect query, SkTDArray<unsigned>* resul
ts) const { |
| 313 for (int i = 0; i < root->fNumChildren; ++i) { | 313 for (int i = 0; i < root->fNumChildren; ++i) { |
| 314 if (SkIRect::IntersectsNoEmptyCheck(root->child(i)->fBounds, query)) { | 314 if (SkIRect::IntersectsNoEmptyCheck(root->child(i)->fBounds, query)) { |
| 315 if (root->isLeaf()) { | 315 if (root->isLeaf()) { |
| 316 results->push(root->child(i)->fChild.data); | 316 results->push(root->child(i)->fChild.opIndex); |
| 317 } else { | 317 } else { |
| 318 this->search(root->child(i)->fChild.subtree, query, results); | 318 this->search(root->child(i)->fChild.subtree, query, results); |
| 319 } | 319 } |
| 320 } | 320 } |
| 321 } | 321 } |
| 322 } | 322 } |
| 323 | 323 |
| 324 SkRTree::Branch SkRTree::bulkLoad(SkTDArray<Branch>* branches, int level) { | 324 SkRTree::Branch SkRTree::bulkLoad(SkTDArray<Branch>* branches, int level) { |
| 325 if (branches->count() == 1) { | 325 if (branches->count() == 1) { |
| 326 // Only one branch: it will be the root | 326 // Only one branch: it will be the root |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 int childCount = 0; | 441 int childCount = 0; |
| 442 for (int i = 0; i < root->fNumChildren; ++i) { | 442 for (int i = 0; i < root->fNumChildren; ++i) { |
| 443 SkASSERT(root->child(i)->fChild.subtree->fLevel == root->fLevel - 1)
; | 443 SkASSERT(root->child(i)->fChild.subtree->fLevel == root->fLevel - 1)
; |
| 444 childCount += this->validateSubtree(root->child(i)->fChild.subtree, | 444 childCount += this->validateSubtree(root->child(i)->fChild.subtree, |
| 445 root->child(i)->fBounds); | 445 root->child(i)->fBounds); |
| 446 } | 446 } |
| 447 return childCount; | 447 return childCount; |
| 448 } | 448 } |
| 449 } | 449 } |
| 450 | 450 |
| 451 void SkRTree::rewindInserts() { | |
| 452 SkASSERT(this->isEmpty()); // Currently only supports deferred inserts | |
| 453 while (!fDeferredInserts.isEmpty() && | |
| 454 fClient->shouldRewind(fDeferredInserts.top().fChild.data)) { | |
| 455 fDeferredInserts.pop(); | |
| 456 } | |
| 457 } | |
| 458 | |
| 459 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 451 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 460 | 452 |
| 461 static inline uint32_t get_area(const SkIRect& rect) { | 453 static inline uint32_t get_area(const SkIRect& rect) { |
| 462 return rect.width() * rect.height(); | 454 return rect.width() * rect.height(); |
| 463 } | 455 } |
| 464 | 456 |
| 465 static inline uint32_t get_overlap(const SkIRect& rect1, const SkIRect& rect2) { | 457 static inline uint32_t get_overlap(const SkIRect& rect1, const SkIRect& rect2) { |
| 466 // I suspect there's a more efficient way of computing this... | 458 // I suspect there's a more efficient way of computing this... |
| 467 return SkMax32(0, SkMin32(rect1.fRight, rect2.fRight) - SkMax32(rect1.fLeft,
rect2.fLeft)) * | 459 return SkMax32(0, SkMin32(rect1.fRight, rect2.fRight) - SkMax32(rect1.fLeft,
rect2.fLeft)) * |
| 468 SkMax32(0, SkMin32(rect1.fBottom, rect2.fBottom) - SkMax32(rect1.fTop
, rect2.fTop)); | 460 SkMax32(0, SkMin32(rect1.fBottom, rect2.fBottom) - SkMax32(rect1.fTop
, rect2.fTop)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 480 | 472 |
| 481 // Expand 'out' to include 'joinWith' | 473 // Expand 'out' to include 'joinWith' |
| 482 static inline void join_no_empty_check(const SkIRect& joinWith, SkIRect* out) { | 474 static inline void join_no_empty_check(const SkIRect& joinWith, SkIRect* out) { |
| 483 // since we check for empty bounds on insert, we know we'll never have empty
rects | 475 // since we check for empty bounds on insert, we know we'll never have empty
rects |
| 484 // and we can save the empty check that SkIRect::join requires | 476 // and we can save the empty check that SkIRect::join requires |
| 485 if (joinWith.fLeft < out->fLeft) { out->fLeft = joinWith.fLeft; } | 477 if (joinWith.fLeft < out->fLeft) { out->fLeft = joinWith.fLeft; } |
| 486 if (joinWith.fTop < out->fTop) { out->fTop = joinWith.fTop; } | 478 if (joinWith.fTop < out->fTop) { out->fTop = joinWith.fTop; } |
| 487 if (joinWith.fRight > out->fRight) { out->fRight = joinWith.fRight; } | 479 if (joinWith.fRight > out->fRight) { out->fRight = joinWith.fRight; } |
| 488 if (joinWith.fBottom > out->fBottom) { out->fBottom = joinWith.fBottom; } | 480 if (joinWith.fBottom > out->fBottom) { out->fBottom = joinWith.fBottom; } |
| 489 } | 481 } |
| OLD | NEW |