| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 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 "SkRTree.h" | 9 #include "SkRTree.h" |
| 10 #include "SkTSort.h" | 10 #include "SkTSort.h" |
| 11 | 11 |
| 12 static inline uint32_t get_area(const SkIRect& rect); | 12 static inline uint32_t get_area(const SkIRect& rect); |
| 13 static inline uint32_t get_overlap(const SkIRect& rect1, const SkIRect& rect2); | 13 static inline uint32_t get_overlap(const SkIRect& rect1, const SkIRect& rect2); |
| 14 static inline uint32_t get_margin(const SkIRect& rect); | 14 static inline uint32_t get_margin(const SkIRect& rect); |
| 15 static inline uint32_t get_overlap_increase(const SkIRect& rect1, const SkIRect&
rect2, | 15 static inline uint32_t get_overlap_increase(const SkIRect& rect1, const SkIRect&
rect2, |
| 16 SkIRect expandBy); | 16 SkIRect expandBy); |
| 17 static inline uint32_t get_area_increase(const SkIRect& rect1, SkIRect rect2); | 17 static inline uint32_t get_area_increase(const SkIRect& rect1, SkIRect rect2); |
| 18 static inline void join_no_empty_check(const SkIRect& joinWith, SkIRect* out); | 18 static inline void join_no_empty_check(const SkIRect& joinWith, SkIRect* out); |
| 19 | 19 |
| 20 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 20 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 21 | 21 |
| 22 SK_DEFINE_INST_COUNT(SkRTree) | 22 SK_DEFINE_INST_COUNT(SkRTree) |
| 23 | 23 |
| 24 SkRTree* SkRTree::Create(int minChildren, int maxChildren, SkScalar aspectRatio)
{ | 24 SkRTree* SkRTree::Create(int minChildren, int maxChildren, SkScalar aspectRatio, |
| 25 bool sortWhenBulkLoading) { |
| 25 if (minChildren < maxChildren && (maxChildren + 1) / 2 >= minChildren && | 26 if (minChildren < maxChildren && (maxChildren + 1) / 2 >= minChildren && |
| 26 minChildren > 0 && maxChildren < static_cast<int>(SK_MaxU16)) { | 27 minChildren > 0 && maxChildren < static_cast<int>(SK_MaxU16)) { |
| 27 return new SkRTree(minChildren, maxChildren, aspectRatio); | 28 return new SkRTree(minChildren, maxChildren, aspectRatio, sortWhenBulkLo
ading); |
| 28 } | 29 } |
| 29 return NULL; | 30 return NULL; |
| 30 } | 31 } |
| 31 | 32 |
| 32 SkRTree::SkRTree(int minChildren, int maxChildren, SkScalar aspectRatio) | 33 SkRTree::SkRTree(int minChildren, int maxChildren, SkScalar aspectRatio, |
| 34 bool sortWhenBulkLoading) |
| 33 : fMinChildren(minChildren) | 35 : fMinChildren(minChildren) |
| 34 , fMaxChildren(maxChildren) | 36 , fMaxChildren(maxChildren) |
| 35 , fNodeSize(sizeof(Node) + sizeof(Branch) * maxChildren) | 37 , fNodeSize(sizeof(Node) + sizeof(Branch) * maxChildren) |
| 36 , fCount(0) | 38 , fCount(0) |
| 37 , fNodes(fNodeSize * 256) | 39 , fNodes(fNodeSize * 256) |
| 38 , fAspectRatio(aspectRatio) { | 40 , fAspectRatio(aspectRatio) |
| 41 , fSortWhenBulkLoading(sortWhenBulkLoading) { |
| 39 SkASSERT(minChildren < maxChildren && minChildren > 0 && maxChildren < | 42 SkASSERT(minChildren < maxChildren && minChildren > 0 && maxChildren < |
| 40 static_cast<int>(SK_MaxU16)); | 43 static_cast<int>(SK_MaxU16)); |
| 41 SkASSERT((maxChildren + 1) / 2 >= minChildren); | 44 SkASSERT((maxChildren + 1) / 2 >= minChildren); |
| 42 this->validate(); | 45 this->validate(); |
| 43 } | 46 } |
| 44 | 47 |
| 45 SkRTree::~SkRTree() { | 48 SkRTree::~SkRTree() { |
| 46 this->clear(); | 49 this->clear(); |
| 47 } | 50 } |
| 48 | 51 |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 } | 319 } |
| 317 } | 320 } |
| 318 | 321 |
| 319 SkRTree::Branch SkRTree::bulkLoad(SkTDArray<Branch>* branches, int level) { | 322 SkRTree::Branch SkRTree::bulkLoad(SkTDArray<Branch>* branches, int level) { |
| 320 if (branches->count() == 1) { | 323 if (branches->count() == 1) { |
| 321 // Only one branch: it will be the root | 324 // Only one branch: it will be the root |
| 322 Branch out = (*branches)[0]; | 325 Branch out = (*branches)[0]; |
| 323 branches->rewind(); | 326 branches->rewind(); |
| 324 return out; | 327 return out; |
| 325 } else { | 328 } else { |
| 326 // First we sort the whole list by y coordinates | 329 // We sort the whole list by y coordinates, if we are told to do so. |
| 327 SkTQSort(branches->begin(), branches->end() - 1, RectLessY()); | 330 // |
| 331 // We expect Webkit / Blink to give us a reasonable x,y order. |
| 332 // Avoiding this call resulted in a 17% win for recording with |
| 333 // negligible difference in playback speed. |
| 334 if (fSortWhenBulkLoading) { |
| 335 SkTQSort(branches->begin(), branches->end() - 1, RectLessY()); |
| 336 } |
| 328 | 337 |
| 329 int numBranches = branches->count() / fMaxChildren; | 338 int numBranches = branches->count() / fMaxChildren; |
| 330 int remainder = branches->count() % fMaxChildren; | 339 int remainder = branches->count() % fMaxChildren; |
| 331 int newBranches = 0; | 340 int newBranches = 0; |
| 332 | 341 |
| 333 if (0 != remainder) { | 342 if (0 != remainder) { |
| 334 ++numBranches; | 343 ++numBranches; |
| 335 // If the remainder isn't enough to fill a node, we'll need to add f
ewer nodes to | 344 // If the remainder isn't enough to fill a node, we'll need to add f
ewer nodes to |
| 336 // some other branches to make up for it | 345 // some other branches to make up for it |
| 337 if (remainder >= fMinChildren) { | 346 if (remainder >= fMinChildren) { |
| 338 remainder = 0; | 347 remainder = 0; |
| 339 } else { | 348 } else { |
| 340 remainder = fMinChildren - remainder; | 349 remainder = fMinChildren - remainder; |
| 341 } | 350 } |
| 342 } | 351 } |
| 343 | 352 |
| 344 int numStrips = SkScalarCeil(SkScalarSqrt(SkIntToScalar(numBranches) * | 353 int numStrips = SkScalarCeil(SkScalarSqrt(SkIntToScalar(numBranches) * |
| 345 SkScalarInvert(fAspectRatio))); | 354 SkScalarInvert(fAspectRatio))); |
| 346 int numTiles = SkScalarCeil(SkIntToScalar(numBranches) / | 355 int numTiles = SkScalarCeil(SkIntToScalar(numBranches) / |
| 347 SkIntToScalar(numStrips)); | 356 SkIntToScalar(numStrips)); |
| 348 int currentBranch = 0; | 357 int currentBranch = 0; |
| 349 | 358 |
| 350 for (int i = 0; i < numStrips; ++i) { | 359 for (int i = 0; i < numStrips; ++i) { |
| 351 int begin = currentBranch; | 360 // Once again, if we are told to do so, we sort by x. |
| 352 int end = currentBranch + numTiles * fMaxChildren - SkMin32(remainde
r, | 361 if (fSortWhenBulkLoading) { |
| 353 (fMaxChildren - fMinChildren) * numTiles); | 362 int begin = currentBranch; |
| 354 if (end > branches->count()) { | 363 int end = currentBranch + numTiles * fMaxChildren - SkMin32(rema
inder, |
| 355 end = branches->count(); | 364 (fMaxChildren - fMinChildren) * numTiles); |
| 365 if (end > branches->count()) { |
| 366 end = branches->count(); |
| 367 } |
| 368 |
| 369 // Now we sort horizontal strips of rectangles by their x coords |
| 370 SkTQSort(branches->begin() + begin, branches->begin() + end - 1,
RectLessX()); |
| 356 } | 371 } |
| 357 | 372 |
| 358 // Now we sort horizontal strips of rectangles by their x coords | |
| 359 SkTQSort(branches->begin() + begin, branches->begin() + end - 1, Rec
tLessX()); | |
| 360 | |
| 361 for (int j = 0; j < numTiles && currentBranch < branches->count(); +
+j) { | 373 for (int j = 0; j < numTiles && currentBranch < branches->count(); +
+j) { |
| 362 int incrementBy = fMaxChildren; | 374 int incrementBy = fMaxChildren; |
| 363 if (remainder != 0) { | 375 if (remainder != 0) { |
| 364 // if need be, omit some nodes to make up for remainder | 376 // if need be, omit some nodes to make up for remainder |
| 365 if (remainder <= fMaxChildren - fMinChildren) { | 377 if (remainder <= fMaxChildren - fMinChildren) { |
| 366 incrementBy -= remainder; | 378 incrementBy -= remainder; |
| 367 remainder = 0; | 379 remainder = 0; |
| 368 } else { | 380 } else { |
| 369 incrementBy = fMinChildren; | 381 incrementBy = fMinChildren; |
| 370 remainder -= fMaxChildren - fMinChildren; | 382 remainder -= fMaxChildren - fMinChildren; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 | 484 |
| 473 // Expand 'out' to include 'joinWith' | 485 // Expand 'out' to include 'joinWith' |
| 474 static inline void join_no_empty_check(const SkIRect& joinWith, SkIRect* out) { | 486 static inline void join_no_empty_check(const SkIRect& joinWith, SkIRect* out) { |
| 475 // since we check for empty bounds on insert, we know we'll never have empty
rects | 487 // since we check for empty bounds on insert, we know we'll never have empty
rects |
| 476 // and we can save the empty check that SkIRect::join requires | 488 // and we can save the empty check that SkIRect::join requires |
| 477 if (joinWith.fLeft < out->fLeft) { out->fLeft = joinWith.fLeft; } | 489 if (joinWith.fLeft < out->fLeft) { out->fLeft = joinWith.fLeft; } |
| 478 if (joinWith.fTop < out->fTop) { out->fTop = joinWith.fTop; } | 490 if (joinWith.fTop < out->fTop) { out->fTop = joinWith.fTop; } |
| 479 if (joinWith.fRight > out->fRight) { out->fRight = joinWith.fRight; } | 491 if (joinWith.fRight > out->fRight) { out->fRight = joinWith.fRight; } |
| 480 if (joinWith.fBottom > out->fBottom) { out->fBottom = joinWith.fBottom; } | 492 if (joinWith.fBottom > out->fBottom) { out->fBottom = joinWith.fBottom; } |
| 481 } | 493 } |
| OLD | NEW |