OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/base/rtree.h" | 5 #include "cc/base/rtree.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 #include <cmath> | 11 #include <cmath> |
12 | 12 |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/numerics/saturated_arithmetic.h" | 14 #include "base/numerics/saturated_arithmetic.h" |
15 | 15 |
16 namespace cc { | 16 namespace cc { |
17 | 17 |
18 RTree::RTree() : num_data_elements_(0u), nodes_(sizeof(Node)) {} | 18 RTree::RTree() : num_data_elements_(0u) {} |
19 | 19 |
20 RTree::~RTree() {} | 20 RTree::~RTree() {} |
21 | 21 |
22 RTree::Node* RTree::AllocateNodeAtLevel(int level) { | 22 RTree::Node* RTree::AllocateNodeAtLevel(int level) { |
23 Node& node = nodes_.AllocateAndConstruct<Node>(); | 23 // We don't allow reallocations, since that would invalidate references to |
| 24 // existing nodes, so verify that capacity > size. |
| 25 DCHECK_GT(nodes_.capacity(), nodes_.size()); |
| 26 nodes_.emplace_back(); |
| 27 Node& node = nodes_.back(); |
24 node.num_children = 0; | 28 node.num_children = 0; |
25 node.level = level; | 29 node.level = level; |
26 return &node; | 30 return &node; |
27 } | 31 } |
28 | 32 |
29 RTree::Branch RTree::BuildRecursive(std::vector<Branch>* branches, int level) { | 33 RTree::Branch RTree::BuildRecursive(std::vector<Branch>* branches, int level) { |
30 // Only one branch. It will be the root. | 34 // Only one branch. It will be the root. |
31 if (branches->size() == 1) | 35 if (branches->size() == 1) |
32 return (*branches)[0]; | 36 return (*branches)[0]; |
33 | 37 |
34 // TODO(vmpstr): Investigate if branches should be sorted in y. | 38 // TODO(vmpstr): Investigate if branches should be sorted in y. |
35 // The comment from Skia reads: | 39 // The comment from Skia reads: |
36 // We might sort our branches here, but we expect Blink gives us a reasonable | 40 // We might sort our branches here, but we expect Blink gives us a reasonable |
37 // x,y order. Skipping a call to sort (in Y) here resulted in a 17% win for | 41 // x,y order. Skipping a call to sort (in Y) here resulted in a 17% win for |
38 // recording with negligible difference in playback speed. | 42 // recording with negligible difference in playback speed. |
39 int num_branches = static_cast<int>(branches->size() / MAX_CHILDREN); | 43 int num_branches = static_cast<int>(branches->size() / kMaxChildren); |
40 int remainder = static_cast<int>(branches->size() % MAX_CHILDREN); | 44 int remainder = static_cast<int>(branches->size() % kMaxChildren); |
41 | 45 |
42 if (remainder > 0) { | 46 if (remainder > 0) { |
43 ++num_branches; | 47 ++num_branches; |
44 // If the remainder isn't enough to fill a node, we'll add fewer nodes to | 48 // If the remainder isn't enough to fill a node, we'll add fewer nodes to |
45 // other branches. | 49 // other branches. |
46 if (remainder >= MIN_CHILDREN) | 50 if (remainder >= kMinChildren) |
47 remainder = 0; | 51 remainder = 0; |
48 else | 52 else |
49 remainder = MIN_CHILDREN - remainder; | 53 remainder = kMinChildren - remainder; |
50 } | 54 } |
51 | 55 |
52 int num_strips = static_cast<int>(std::ceil(std::sqrt(num_branches))); | 56 int num_strips = static_cast<int>(std::ceil(std::sqrt(num_branches))); |
53 int num_tiles = static_cast<int>( | 57 int num_tiles = static_cast<int>( |
54 std::ceil(num_branches / static_cast<float>(num_strips))); | 58 std::ceil(num_branches / static_cast<float>(num_strips))); |
55 size_t current_branch = 0; | 59 size_t current_branch = 0; |
56 | 60 |
57 size_t new_branch_index = 0; | 61 size_t new_branch_index = 0; |
58 for (int i = 0; i < num_strips; ++i) { | 62 for (int i = 0; i < num_strips; ++i) { |
59 // Might be worth sorting by X here too. | 63 // Might be worth sorting by X here too. |
60 for (int j = 0; j < num_tiles && current_branch < branches->size(); ++j) { | 64 for (int j = 0; j < num_tiles && current_branch < branches->size(); ++j) { |
61 int increment_by = MAX_CHILDREN; | 65 int increment_by = kMaxChildren; |
62 if (remainder != 0) { | 66 if (remainder != 0) { |
63 // if need be, omit some nodes to make up for remainder | 67 // if need be, omit some nodes to make up for remainder |
64 if (remainder <= MAX_CHILDREN - MIN_CHILDREN) { | 68 if (remainder <= kMaxChildren - kMinChildren) { |
65 increment_by -= remainder; | 69 increment_by -= remainder; |
66 remainder = 0; | 70 remainder = 0; |
67 } else { | 71 } else { |
68 increment_by = MIN_CHILDREN; | 72 increment_by = kMinChildren; |
69 remainder -= MAX_CHILDREN - MIN_CHILDREN; | 73 remainder -= kMaxChildren - kMinChildren; |
70 } | 74 } |
71 } | 75 } |
72 Node* node = AllocateNodeAtLevel(level); | 76 Node* node = AllocateNodeAtLevel(level); |
73 node->num_children = 1; | 77 node->num_children = 1; |
74 node->children[0] = (*branches)[current_branch]; | 78 node->children[0] = (*branches)[current_branch]; |
75 | 79 |
76 Branch branch; | 80 Branch branch; |
77 branch.bounds = (*branches)[current_branch].bounds; | 81 branch.bounds = (*branches)[current_branch].bounds; |
78 branch.subtree = node; | 82 branch.subtree = node; |
79 ++current_branch; | 83 ++current_branch; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 SearchRecursive(node->children[i].subtree, query, results); | 128 SearchRecursive(node->children[i].subtree, query, results); |
125 } | 129 } |
126 } | 130 } |
127 } | 131 } |
128 | 132 |
129 gfx::Rect RTree::GetBounds() const { | 133 gfx::Rect RTree::GetBounds() const { |
130 return root_.bounds; | 134 return root_.bounds; |
131 } | 135 } |
132 | 136 |
133 } // namespace cc | 137 } // namespace cc |
OLD | NEW |