1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
/*
* Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "config.h"
#include "AreaAllocator.h"
namespace WebKit {
AreaAllocator::AreaAllocator(const WebCore::IntSize& size)
: m_size(size)
, m_minAlloc(1, 1)
, m_margin(0, 0)
{
}
AreaAllocator::~AreaAllocator()
{
}
void AreaAllocator::expand(const WebCore::IntSize& size)
{
m_size = m_size.expandedTo(size);
}
void AreaAllocator::expandBy(const WebCore::IntSize& size)
{
m_size += size;
}
void AreaAllocator::release(const WebCore::IntRect&)
{
}
int AreaAllocator::overhead() const
{
return 0;
}
WebCore::IntSize AreaAllocator::roundAllocation(const WebCore::IntSize& size) const
{
int width = size.width() + m_margin.width();
int height = size.height() + m_margin.height();
int extra = width % m_minAlloc.width();
if (extra)
width += m_minAlloc.width() - extra;
extra = height % m_minAlloc.height();
if (extra)
height += m_minAlloc.height() - extra;
return WebCore::IntSize(width, height);
}
GeneralAreaAllocator::GeneralAreaAllocator(const WebCore::IntSize& size)
: AreaAllocator(WebCore::nextPowerOfTwo(size))
{
m_root = new Node();
m_root->rect = WebCore::IntRect(0, 0, m_size.width(), m_size.height());
m_root->largestFree = m_size;
m_root->parent = 0;
m_root->left = 0;
m_root->right = 0;
m_nodeCount = 1;
setMinimumAllocation(WebCore::IntSize(8, 8));
}
GeneralAreaAllocator::~GeneralAreaAllocator()
{
freeNode(m_root);
}
void GeneralAreaAllocator::freeNode(Node* node)
{
if (node) {
freeNode(node->left);
freeNode(node->right);
}
delete node;
}
void GeneralAreaAllocator::expand(const WebCore::IntSize& size)
{
AreaAllocator::expand(WebCore::nextPowerOfTwo(size));
if (m_root->rect.size() == m_size)
return; // No change.
if (!m_root->left && m_root->largestFree.width() > 0) {
// No allocations have occurred, so just adjust the root size.
m_root->rect = WebCore::IntRect(0, 0, m_size.width(), m_size.height());
m_root->largestFree = m_size;
return;
}
// Add extra nodes above the current root to expand the tree.
Node* oldRoot = m_root;
Split split;
if (m_size.width() >= m_size.height())
split = SplitOnX;
else
split = SplitOnY;
while (m_root->rect.size() != m_size) {
if (m_root->rect.width() == m_size.width())
split = SplitOnY;
else if (m_root->rect.height() == m_size.height())
split = SplitOnX;
Node* parent = new Node();
Node* right = new Node();
m_nodeCount += 2;
m_root->parent = parent;
parent->parent = 0;
parent->left = m_root;
parent->right = right;
parent->largestFree = m_root->rect.size();
right->parent = parent;
right->left = 0;
right->right = 0;
right->largestFree = m_root->rect.size();
if (split == SplitOnX) {
parent->rect = WebCore::IntRect(m_root->rect.x(), m_root->rect.y(),
m_root->rect.width() * 2, m_root->rect.height());
right->rect = WebCore::IntRect(m_root->rect.x() + m_root->rect.width(), m_root->rect.y(),
m_root->rect.width(), m_root->rect.height());
} else {
parent->rect = WebCore::IntRect(m_root->rect.x(), m_root->rect.y(),
m_root->rect.width(), m_root->rect.height() * 2);
right->rect = WebCore::IntRect(m_root->rect.x(), m_root->rect.y() + m_root->rect.width(),
m_root->rect.width(), m_root->rect.height());
}
split = (split == SplitOnX ? SplitOnY : SplitOnX);
m_root = parent;
}
updateLargestFree(oldRoot);
}
static inline bool fitsWithin(const WebCore::IntSize& size1, const WebCore::IntSize& size2)
{
return size1.width() <= size2.width() && size1.height() <= size2.height();
}
WebCore::IntRect GeneralAreaAllocator::allocate(const WebCore::IntSize& size)
{
WebCore::IntSize rounded = roundAllocation(size);
rounded = WebCore::nextPowerOfTwo(rounded);
if (rounded.width() <= 0 || rounded.width() > m_size.width()
|| rounded.height() <= 0 || rounded.height() > m_size.height())
return WebCore::IntRect();
WebCore::IntPoint point = allocateFromNode(rounded, m_root);
if (point.x() >= 0)
return WebCore::IntRect(point, size);
return WebCore::IntRect();
}
WebCore::IntPoint GeneralAreaAllocator::allocateFromNode(const WebCore::IntSize& size, Node* node)
{
// Find the best node to insert into, which should be
// a node with the least amount of unused space that is
// big enough to contain the requested size.
while (node) {
// Go down a level and determine if the left or right
// sub-tree contains the best chance of allocation.
Node* left = node->left;
Node* right = node->right;
if (left && fitsWithin(size, left->largestFree)) {
if (right && fitsWithin(size, right->largestFree)) {
if (left->largestFree.width() < right->largestFree.width()
|| left->largestFree.height() < right->largestFree.height()) {
// The largestFree values may be a little oversized,
// so try the left sub-tree and then the right sub-tree.
WebCore::IntPoint point = allocateFromNode(size, left);
if (point.x() >= 0)
return point;
return allocateFromNode(size, right);
}
node = right;
} else
node = left;
} else if (right && fitsWithin(size, right->largestFree))
node = right;
else if (left || right) {
// Neither sub-node has enough space to allocate from.
return WebCore::IntPoint(-1, -1);
} else if (fitsWithin(size, node->largestFree)) {
// Do we need to split this node into smaller pieces?
Split split;
if (fitsWithin(WebCore::IntSize(size.width() * 2, size.height() * 2), node->largestFree)) {
// Split in either direction: choose the inverse of
// the parent node's split direction to try to balance
// out the wasted space as further subdivisions happen.
if (node->parent
&& node->parent->left->rect.x() == node->parent->right->rect.x())
split = SplitOnX;
else if (node->parent)
split = SplitOnY;
else if (node->rect.width() >= node->rect.height())
split = SplitOnX;
else
split = SplitOnY;
} else if (fitsWithin(WebCore::IntSize(size.width() * 2, size.height()), node->largestFree)) {
// Split along the X direction.
split = SplitOnX;
} else if (fitsWithin(WebCore::IntSize(size.width(), size.height() * 2), node->largestFree)) {
// Split along the Y direction.
split = SplitOnY;
} else {
// Cannot split further - allocate this node.
node->largestFree = WebCore::IntSize(0, 0);
updateLargestFree(node);
return node->rect.location();
}
// Split the node, then go around again using the left sub-tree.
node = splitNode(node, split);
} else {
// Cannot possibly fit into this node.
break;
}
}
return WebCore::IntPoint(-1, -1);
}
GeneralAreaAllocator::Node* GeneralAreaAllocator::splitNode
(Node* node, Split split)
{
Node* left = new Node();
Node* right = new Node();
m_nodeCount += 2;
left->parent = node;
left->left = 0;
left->right = 0;
right->parent = node;
right->left = 0;
right->right = 0;
node->left = left;
node->right = right;
if (split == SplitOnX) {
left->rect = WebCore::IntRect(node->rect.x(), node->rect.y(),
node->rect.width() / 2, node->rect.height());
right->rect = WebCore::IntRect(left->rect.maxX(), node->rect.y(),
node->rect.width() / 2, node->rect.height());
} else {
left->rect = WebCore::IntRect(node->rect.x(), node->rect.y(),
node->rect.width(), node->rect.height() / 2);
right->rect = WebCore::IntRect(node->rect.x(), left->rect.maxY(),
node->rect.width(), node->rect.height() / 2);
}
left->largestFree = left->rect.size();
right->largestFree = right->rect.size();
node->largestFree = right->largestFree;
return left;
}
void GeneralAreaAllocator::updateLargestFree(Node* node)
{
while ((node = node->parent)) {
node->largestFree = WebCore::IntSize(
std::max(node->left->largestFree.width(), node->right->largestFree.width()),
std::max(node->left->largestFree.height(), node->right->largestFree.height())
);
}
}
void GeneralAreaAllocator::release(const WebCore::IntRect& rect)
{
// Locate the node that contains the allocated region.
Node* node = m_root;
WebCore::IntPoint point = rect.location();
while (node) {
if (node->left && node->left->rect.contains(point))
node = node->left;
else if (node->right && node->right->rect.contains(point))
node = node->right;
else if (node->rect.contains(point))
break;
else
return; // Point is completely outside the tree.
}
if (!node)
return;
// Mark the node as free and then work upwards through the tree
// recombining and deleting nodes until we reach a sibling
// that is still allocated.
node->largestFree = node->rect.size();
while (node->parent) {
if (node->parent->left == node) {
if (node->parent->right->largestFree != node->parent->right->rect.size())
break;
} else {
if (node->parent->left->largestFree != node->parent->left->rect.size())
break;
}
node = node->parent;
freeNode(node->left);
freeNode(node->right);
m_nodeCount -= 2;
node->left = 0;
node->right = 0;
node->largestFree = node->rect.size();
}
// Make the rest of our ancestors have the correct "largest free size".
updateLargestFree(node);
}
int GeneralAreaAllocator::overhead() const
{
return m_nodeCount * sizeof(Node);
}
} // namespace
|