6#include <QtCore/qglobal.h>
7#include <QtCore/qrect.h>
8#include <QtCore/qpoint.h>
9#include <QtCore/qdatastream.h>
10#include <QtCore/qstack.h>
11#include <QtCore/qendian.h>
12#include <QtCore/qvarlengtharray.h>
54 Q_ASSERT((
left !=
nullptr) == (
right !=
nullptr));
61QSGAreaAllocator::QSGAreaAllocator(
const QSize &size,
int sliceHeight)
63 , m_sliceHeight(sliceHeight)
65 m_root =
new QSGAreaAllocatorNode(
nullptr);
68QSGAreaAllocator::~QSGAreaAllocator()
73QRect QSGAreaAllocator::allocate(
const QSize &size)
76 bool result = allocateInNode(size, point, QRect(QPoint(0, 0), m_size), m_root);
77 return result ? QRect(point, size) : QRect();
80bool QSGAreaAllocator::deallocate(
const QRect &rect)
82 return deallocateInNode(rect.topLeft(), m_root);
85bool QSGAreaAllocator::allocateInNode(
const QSize &size, QPoint &result,
const QRect ¤tRect, QSGAreaAllocatorNode *node)
87 if (size.width() > currentRect.width() || size.height() > currentRect.height())
93 if (m_sliceHeight > 0) {
96 const int boundary = (currentRect.top() / m_sliceHeight + 1) * m_sliceHeight;
97 if (boundary <= currentRect.bottom()) {
98 node->left =
new QSGAreaAllocatorNode(node);
99 node->right =
new QSGAreaAllocatorNode(node);
100 node->splitType = HorizontalSplit;
101 node->split = boundary;
105 return allocateInNode(size, result, currentRect, node);
108 if (size.width() + maxMargin >= currentRect.width() && size.height() + maxMargin >= currentRect.height()) {
110 node->isOccupied =
true;
111 result = currentRect.topLeft();
116 node->left =
new QSGAreaAllocatorNode(node);
117 node->right =
new QSGAreaAllocatorNode(node);
118 QRect splitRect = currentRect;
121 const bool startsSlicedRow = m_sliceHeight > 0 && currentRect.width() == m_size.width()
122 && currentRect.height() > size.height();
124 const bool fillsMoreOfWidth = (currentRect.width() - size.width()) * currentRect.height()
125 < (currentRect.height() - size.height()) * currentRect.width();
126 if (startsSlicedRow || fillsMoreOfWidth) {
127 node->splitType = HorizontalSplit;
128 node->split = currentRect.top() + size.height();
129 splitRect.setHeight(size.height());
131 node->splitType = VerticalSplit;
132 node->split = currentRect.left() + size.width();
133 splitRect.setWidth(size.width());
135 return allocateInNode(size, result, splitRect, node->left);
139 QRect leftRect = currentRect;
140 QRect rightRect = currentRect;
141 if (node->splitType == HorizontalSplit) {
142 leftRect.setHeight(node->split - leftRect.top());
143 rightRect.setTop(node->split);
145 leftRect.setWidth(node->split - leftRect.left());
146 rightRect.setLeft(node->split);
148 if (allocateInNode(size, result, leftRect, node->left))
150 if (allocateInNode(size, result, rightRect, node->right))
156bool QSGAreaAllocator::deallocateInNode(
const QPoint &pos, QSGAreaAllocatorNode *node)
158 while (!node->isLeaf()) {
160 int cmp = node->splitType == HorizontalSplit ? pos.y() : pos.x();
161 node = cmp < node->split ? node->left : node->right;
163 if (!node->isOccupied)
165 node->isOccupied =
false;
166 mergeNodeWithNeighbors(node);
170void QSGAreaAllocator::mergeNodeWithNeighbors(QSGAreaAllocatorNode *node)
173 QSGAreaAllocatorNode *parent =
nullptr;
174 QSGAreaAllocatorNode *current =
nullptr;
175 QSGAreaAllocatorNode *sibling;
177 Q_ASSERT(node->isLeaf());
178 Q_ASSERT(!node->isOccupied);
179 if (node->parent ==
nullptr)
182 SplitType splitType = SplitType(node->parent->splitType);
186
187
188
189
190
191
192
193
194
195
196
197
198
199
203 parent = current->parent;
204 while (parent && current == parent->left && parent->splitType == splitType) {
206 parent = parent->parent;
209 if (parent && parent->splitType == splitType) {
210 Q_ASSERT(current == parent->right);
211 Q_ASSERT(parent->left);
213 QSGAreaAllocatorNode *neighbor = parent->left;
214 while (neighbor->right && neighbor->splitType == splitType)
215 neighbor = neighbor->right;
217 if (neighbor->isLeaf() && neighbor->parent->splitType == splitType && !neighbor->isOccupied) {
219 parent->split = neighbor->parent->split;
221 parent = neighbor->parent;
222 sibling = neighbor == parent->left ? parent->right : parent->left;
223 QSGAreaAllocatorNode **nodeRef = &m_root;
224 if (parent->parent) {
225 if (parent == parent->parent->left)
226 nodeRef = &parent->parent->left;
228 nodeRef = &parent->parent->right;
230 sibling->parent = parent->parent;
232 parent->left = parent->right =
nullptr;
241 parent = current->parent;
242 while (parent && current == parent->right && parent->splitType == splitType) {
244 parent = parent->parent;
247 if (parent && parent->splitType == splitType) {
248 Q_ASSERT(current == parent->left);
249 Q_ASSERT(parent->right);
251 QSGAreaAllocatorNode *neighbor = parent->right;
252 while (neighbor->left && neighbor->splitType == splitType)
253 neighbor = neighbor->left;
255 if (neighbor->isLeaf() && neighbor->parent->splitType == splitType && !neighbor->isOccupied) {
257 parent->split = neighbor->parent->split;
259 parent = neighbor->parent;
260 sibling = neighbor == parent->left ? parent->right : parent->left;
261 QSGAreaAllocatorNode **nodeRef = &m_root;
262 if (parent->parent) {
263 if (parent == parent->parent->left)
264 nodeRef = &parent->parent->left;
266 nodeRef = &parent->parent->right;
268 sibling->parent = parent->parent;
270 parent->left = parent->right =
nullptr;
280 struct AreaAllocatorTable
306 template <
typename T>
307 static inline T fetch(
const char *data, Offset offset)
309 return qFromBigEndian<T>(data +
int(offset));
312 template <
typename T>
313 static inline void put(
char *data, Offset offset, T value)
315 qToBigEndian(value, data +
int(offset));
320QByteArray QSGAreaAllocator::serialize()
322 QVarLengthArray<QSGAreaAllocatorNode *> nodesToProcess;
324 QStack<QSGAreaAllocatorNode *> nodes;
326 while (!nodes.isEmpty()) {
327 QSGAreaAllocatorNode *node = nodes.pop();
329 nodesToProcess.append(node);
330 if (node->left !=
nullptr)
331 nodes.push(node->left);
332 if (node->right !=
nullptr)
333 nodes.push(node->right);
337 ret.resize(AreaAllocatorTable::HeaderSize + AreaAllocatorTable::NodeSize * nodesToProcess.size());
339 char *data = ret.data();
340 AreaAllocatorTable::put(data, AreaAllocatorTable::majorVersion, quint8(5));
341 AreaAllocatorTable::put(data, AreaAllocatorTable::minorVersion, quint8(12));
342 AreaAllocatorTable::put(data, AreaAllocatorTable::width, quint32(m_size.width()));
343 AreaAllocatorTable::put(data, AreaAllocatorTable::height, quint32(m_size.height()));
345 data += AreaAllocatorTable::HeaderSize;
346 for (QSGAreaAllocatorNode *node : nodesToProcess) {
347 AreaAllocatorTable::put(data, AreaAllocatorTable::split, qint32(node->split));
348 AreaAllocatorTable::put(data, AreaAllocatorTable::splitType, quint32(node->splitType));
351 (node->isOccupied ? AreaAllocatorTable::IsOccupied : 0)
352 | (node->left !=
nullptr ? AreaAllocatorTable::HasLeft : 0)
353 | (node->right !=
nullptr ? AreaAllocatorTable::HasRight : 0);
354 AreaAllocatorTable::put(data, AreaAllocatorTable::flags, flags);
355 data += AreaAllocatorTable::NodeSize;
361const char *QSGAreaAllocator::deserialize(
const char *data,
int size)
363 if (uint(size) < AreaAllocatorTable::HeaderSize) {
364 qWarning(
"QSGAreaAllocator::deserialize: Data not long enough to fit header");
368 const char *end = data + size;
370 quint8 majorVersion = AreaAllocatorTable::fetch<quint8>(data, AreaAllocatorTable::majorVersion);
371 quint8 minorVersion = AreaAllocatorTable::fetch<quint8>(data, AreaAllocatorTable::minorVersion);
372 if (majorVersion != 5 || minorVersion != 12) {
373 qWarning(
"Unrecognized version %d.%d of QSGAreaAllocator",
379 m_size = QSize(AreaAllocatorTable::fetch<quint32>(data, AreaAllocatorTable::width),
380 AreaAllocatorTable::fetch<quint32>(data, AreaAllocatorTable::height));
382 Q_ASSERT(m_root !=
nullptr);
383 Q_ASSERT(m_root->left ==
nullptr);
384 Q_ASSERT(m_root->right ==
nullptr);
386 QStack<QSGAreaAllocatorNode *> nodes;
389 data += AreaAllocatorTable::HeaderSize;
390 while (!nodes.isEmpty()) {
391 if (data + AreaAllocatorTable::NodeSize > end) {
392 qWarning(
"QSGAreaAllocator::deseriable: Data not long enough for nodes");
396 QSGAreaAllocatorNode *node = nodes.pop();
398 node->split = AreaAllocatorTable::fetch<qint32>(data, AreaAllocatorTable::split);
399 node->splitType = SplitType(AreaAllocatorTable::fetch<quint32>(data, AreaAllocatorTable::splitType));
401 quint8 flags = AreaAllocatorTable::fetch<quint8>(data, AreaAllocatorTable::flags);
402 node->isOccupied = flags & AreaAllocatorTable::IsOccupied;
404 if (flags & AreaAllocatorTable::HasLeft) {
405 node->left =
new QSGAreaAllocatorNode(node);
406 nodes.push(node->left);
409 if (flags & AreaAllocatorTable::HasRight) {
410 node->right =
new QSGAreaAllocatorNode(node);
411 nodes.push(node->right);
414 data += AreaAllocatorTable::NodeSize;
Combined button and popup list for selecting options.
static const int maxMargin
QSGAreaAllocatorNode(QSGAreaAllocatorNode *parent)
QSGAreaAllocatorNode * parent
QSGAreaAllocatorNode * left
QSGAreaAllocatorNode * right