Chromium Code Reviews| Index: src/core/SkRTree.cpp |
| diff --git a/src/core/SkRTree.cpp b/src/core/SkRTree.cpp |
| index 4a081db26d957add05c3affabea927be9c95deab..a6bbf672d183ddf912391dceed06d6fcae882be1 100644 |
| --- a/src/core/SkRTree.cpp |
| +++ b/src/core/SkRTree.cpp |
| @@ -44,68 +44,59 @@ SkRTree::~SkRTree() { |
| this->clear(); |
| } |
| -void SkRTree::insert(unsigned opIndex, const SkRect& fbounds, bool defer) { |
| - SkIRect bounds; |
| - if (fbounds.isLargest()) { |
| - bounds.setLargest(); |
| - } else { |
| - fbounds.roundOut(&bounds); |
| - } |
| - |
| +void SkRTree::insert(SkAutoTMalloc<SkRect>* boundsArray, int N) { |
| this->validate(); |
| - if (bounds.isEmpty()) { |
| - SkASSERT(false); |
| - return; |
| - } |
| - Branch newBranch; |
| - newBranch.fBounds = bounds; |
| - newBranch.fChild.opIndex = opIndex; |
| - if (this->isEmpty()) { |
| - // since a bulk-load into an existing tree is as of yet unimplemented (and arguably not |
| - // of vital importance right now), we only batch up inserts if the tree is empty. |
| - if (defer) { |
| - fDeferredInserts.push(newBranch); |
| - return; |
| - } else { |
| - fRoot.fChild.subtree = allocateNode(0); |
| - fRoot.fChild.subtree->fNumChildren = 0; |
| + |
| + // Since a bulk-load into an existing tree is as of yet unimplemented (and arguably not |
| + // of vital importance right now), we only batch up inserts if the tree is empty. |
| + const bool bulkLoad = this->isEmpty(); |
| + SkTDArray<Branch> deferred; |
| + deferred.setReserve(bulkLoad ? N : 0); |
| + |
| + for (int i = 0; i < N; i++) { |
| + SkIRect bounds; |
| + (*boundsArray)[i].roundOut(&bounds); |
| + if (bounds.isEmpty()) { |
| + continue; |
| } |
| - } |
| + fCount++; |
| + |
| + Branch newBranch; |
| + newBranch.fBounds = bounds; |
| + newBranch.fChild.opIndex = i; |
| - Branch* newSibling = insert(fRoot.fChild.subtree, &newBranch); |
| - fRoot.fBounds = this->computeBounds(fRoot.fChild.subtree); |
| + if (bulkLoad) { |
| + deferred.push(newBranch); |
| + continue; |
| + } |
|
robertphillips
2014/10/24 18:52:45
In this case the tree isn't empty. So I think this
mtklein
2014/10/24 19:05:48
I've just asserted this->isEmpty() and deleted the
|
| - if (newSibling) { |
| - Node* oldRoot = fRoot.fChild.subtree; |
| - Node* newRoot = this->allocateNode(oldRoot->fLevel + 1); |
| - newRoot->fNumChildren = 2; |
| - *newRoot->child(0) = fRoot; |
| - *newRoot->child(1) = *newSibling; |
| - fRoot.fChild.subtree = newRoot; |
| + fRoot.fChild.subtree = allocateNode(0); |
| + fRoot.fChild.subtree->fNumChildren = 0; |
| + |
|
robertphillips
2014/10/24 18:52:45
this-> ?
|
| + Branch* newSibling = insert(fRoot.fChild.subtree, &newBranch); |
| fRoot.fBounds = this->computeBounds(fRoot.fChild.subtree); |
| + if (newSibling) { |
| + Node* oldRoot = fRoot.fChild.subtree; |
| + Node* newRoot = this->allocateNode(oldRoot->fLevel + 1); |
| + newRoot->fNumChildren = 2; |
| + *newRoot->child(0) = fRoot; |
| + *newRoot->child(1) = *newSibling; |
| + fRoot.fChild.subtree = newRoot; |
| + fRoot.fBounds = this->computeBounds(fRoot.fChild.subtree); |
| + } |
| } |
| - ++fCount; |
| - this->validate(); |
| -} |
| - |
| -void SkRTree::flushDeferredInserts() { |
| - this->validate(); |
| - if (this->isEmpty() && fDeferredInserts.count() > 0) { |
| - fCount = fDeferredInserts.count(); |
| + if (bulkLoad && fCount > 0) { |
| + SkASSERT(fCount == deferred.count()); |
| if (1 == fCount) { |
|
robertphillips
2014/10/24 18:52:45
this-> ?
mtklein
2014/10/24 19:05:48
Done.
|
| fRoot.fChild.subtree = allocateNode(0); |
| fRoot.fChild.subtree->fNumChildren = 0; |
| - this->insert(fRoot.fChild.subtree, &fDeferredInserts[0]); |
| - fRoot.fBounds = fDeferredInserts[0].fBounds; |
| + this->insert(fRoot.fChild.subtree, &deferred[0]); |
| + fRoot.fBounds = deferred[0].fBounds; |
| } else { |
| - fRoot = this->bulkLoad(&fDeferredInserts); |
| + fRoot = this->bulkLoad(&deferred); |
| } |
| - } else { |
| - // TODO: some algorithm for bulk loading into an already populated tree |
| - SkASSERT(0 == fDeferredInserts.count()); |
| } |
| - fDeferredInserts.rewind(); |
| this->validate(); |
| } |
| @@ -113,7 +104,6 @@ void SkRTree::search(const SkRect& fquery, SkTDArray<unsigned>* results) const { |
| SkIRect query; |
| fquery.roundOut(&query); |
| this->validate(); |
| - SkASSERT(0 == fDeferredInserts.count()); // If this fails, you should have flushed. |
| if (!this->isEmpty() && SkIRect::IntersectsNoEmptyCheck(fRoot.fBounds, query)) { |
| this->search(fRoot.fChild.subtree, query, results); |
| } |
| @@ -123,7 +113,6 @@ void SkRTree::search(const SkRect& fquery, SkTDArray<unsigned>* results) const { |
| void SkRTree::clear() { |
| this->validate(); |
| fNodes.reset(); |
| - fDeferredInserts.rewind(); |
| fCount = 0; |
| this->validate(); |
| } |