Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsgareaallocator.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
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>
13
15
21
22static const int maxMargin = 2;
23
37
39 : parent(parent)
40 , left(nullptr)
41 , right(nullptr)
42 , isOccupied(false)
43{
44}
45
47{
48 delete left;
49 delete right;
50}
51
53{
54 Q_ASSERT((left != nullptr) == (right != nullptr));
55 return !left;
56}
57
58
59// If sliceHeight is set, the area is treated as a vertical stack of slices of
60// that height, and no allocation will cross the boundary between two slices.
61QSGAreaAllocator::QSGAreaAllocator(const QSize &size, int sliceHeight)
62 : m_size(size)
63 , m_sliceHeight(sliceHeight)
64{
65 m_root = new QSGAreaAllocatorNode(nullptr);
66}
67
68QSGAreaAllocator::~QSGAreaAllocator()
69{
70 delete m_root;
71}
72
73QRect QSGAreaAllocator::allocate(const QSize &size)
74{
75 QPoint point;
76 bool result = allocateInNode(size, point, QRect(QPoint(0, 0), m_size), m_root);
77 return result ? QRect(point, size) : QRect();
78}
79
80bool QSGAreaAllocator::deallocate(const QRect &rect)
81{
82 return deallocateInNode(rect.topLeft(), m_root);
83}
84
85bool QSGAreaAllocator::allocateInNode(const QSize &size, QPoint &result, const QRect &currentRect, QSGAreaAllocatorNode *node)
86{
87 if (size.width() > currentRect.width() || size.height() > currentRect.height())
88 return false;
89
90 if (node->isLeaf()) {
91 if (node->isOccupied)
92 return false;
93 if (m_sliceHeight > 0) {
94 // Does this leaf contain the slice boundary? In that case, we pre-split it so
95 // that we never get an allocation that crosses two slices.
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;
102
103 // Re-call function with same arguments now that node is no longer leaf to
104 // trigger the search.
105 return allocateInNode(size, result, currentRect, node);
106 }
107 }
108 if (size.width() + maxMargin >= currentRect.width() && size.height() + maxMargin >= currentRect.height()) {
109 //Snug fit, occupy entire rectangle.
110 node->isOccupied = true;
111 result = currentRect.topLeft();
112 return true;
113 }
114 // TODO: Reuse nodes.
115 // Split node.
116 node->left = new QSGAreaAllocatorNode(node);
117 node->right = new QSGAreaAllocatorNode(node);
118 QRect splitRect = currentRect;
119 // When sliced, start a new row in a full-width region to avoid narrow columns
120 // spanning the whole slice.
121 const bool startsSlicedRow = m_sliceHeight > 0 && currentRect.width() == m_size.width()
122 && currentRect.height() > size.height();
123 // Otherwise, cut a row if the request fills relatively more of the width than 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());
130 } else {
131 node->splitType = VerticalSplit;
132 node->split = currentRect.left() + size.width();
133 splitRect.setWidth(size.width());
134 }
135 return allocateInNode(size, result, splitRect, node->left);
136 } else {
137 // TODO: avoid unnecessary recursion.
138 // has been split.
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);
144 } else {
145 leftRect.setWidth(node->split - leftRect.left());
146 rightRect.setLeft(node->split);
147 }
148 if (allocateInNode(size, result, leftRect, node->left))
149 return true;
150 if (allocateInNode(size, result, rightRect, node->right))
151 return true;
152 return false;
153 }
154}
155
156bool QSGAreaAllocator::deallocateInNode(const QPoint &pos, QSGAreaAllocatorNode *node)
157{
158 while (!node->isLeaf()) {
159 // has been split.
160 int cmp = node->splitType == HorizontalSplit ? pos.y() : pos.x();
161 node = cmp < node->split ? node->left : node->right;
162 }
163 if (!node->isOccupied)
164 return false;
165 node->isOccupied = false;
166 mergeNodeWithNeighbors(node);
167 return true;
168}
169
170void QSGAreaAllocator::mergeNodeWithNeighbors(QSGAreaAllocatorNode *node)
171{
172 bool done = false;
173 QSGAreaAllocatorNode *parent = nullptr;
174 QSGAreaAllocatorNode *current = nullptr;
175 QSGAreaAllocatorNode *sibling;
176 while (!done) {
177 Q_ASSERT(node->isLeaf());
178 Q_ASSERT(!node->isOccupied);
179 if (node->parent == nullptr)
180 return; // No neighbours.
181
182 SplitType splitType = SplitType(node->parent->splitType);
183 done = true;
184
185 /* Special case. Might be faster than going through the general code path.
186 // Merge with sibling.
187 parent = node->parent;
188 sibling = (node == parent->left ? parent->right : parent->left);
189 if (sibling->isLeaf() && !sibling->isOccupied) {
190 Q_ASSERT(!sibling->right);
191 node = parent;
192 parent->isOccupied = false;
193 delete parent->left;
194 delete parent->right;
195 parent->left = parent->right = 0;
196 done = false;
197 continue;
198 }
199 */
200
201 // Merge with left neighbour.
202 current = node;
203 parent = current->parent;
204 while (parent && current == parent->left && parent->splitType == splitType) {
205 current = parent;
206 parent = parent->parent;
207 }
208
209 if (parent && parent->splitType == splitType) {
210 Q_ASSERT(current == parent->right);
211 Q_ASSERT(parent->left);
212
213 QSGAreaAllocatorNode *neighbor = parent->left;
214 while (neighbor->right && neighbor->splitType == splitType)
215 neighbor = neighbor->right;
216
217 if (neighbor->isLeaf() && neighbor->parent->splitType == splitType && !neighbor->isOccupied) {
218 // Left neighbour can be merged.
219 parent->split = neighbor->parent->split;
220
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;
227 else
228 nodeRef = &parent->parent->right;
229 }
230 sibling->parent = parent->parent;
231 *nodeRef = sibling;
232 parent->left = parent->right = nullptr;
233 delete parent;
234 delete neighbor;
235 done = false;
236 }
237 }
238
239 // Merge with right neighbour.
240 current = node;
241 parent = current->parent;
242 while (parent && current == parent->right && parent->splitType == splitType) {
243 current = parent;
244 parent = parent->parent;
245 }
246
247 if (parent && parent->splitType == splitType) {
248 Q_ASSERT(current == parent->left);
249 Q_ASSERT(parent->right);
250
251 QSGAreaAllocatorNode *neighbor = parent->right;
252 while (neighbor->left && neighbor->splitType == splitType)
253 neighbor = neighbor->left;
254
255 if (neighbor->isLeaf() && neighbor->parent->splitType == splitType && !neighbor->isOccupied) {
256 // Right neighbour can be merged.
257 parent->split = neighbor->parent->split;
258
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;
265 else
266 nodeRef = &parent->parent->right;
267 }
268 sibling->parent = parent->parent;
269 *nodeRef = sibling;
270 parent->left = parent->right = nullptr;
271 delete parent;
272 delete neighbor;
273 done = false;
274 }
275 }
276 } // end while(!done)
277}
278
279namespace {
280 struct AreaAllocatorTable
281 {
282 enum TableSize {
283 HeaderSize = 10,
284 NodeSize = 9
285 };
286
287 enum Offset {
288 // Header
289 majorVersion = 0,
290 minorVersion = 1,
291 width = 2,
292 height = 6,
293
294 // Node
295 split = 0,
296 splitType = 4,
297 flags = 8
298 };
299
300 enum Flags {
301 IsOccupied = 1,
302 HasLeft = 2,
303 HasRight = 4
304 };
305
306 template <typename T>
307 static inline T fetch(const char *data, Offset offset)
308 {
309 return qFromBigEndian<T>(data + int(offset));
310 }
311
312 template <typename T>
313 static inline void put(char *data, Offset offset, T value)
314 {
315 qToBigEndian(value, data + int(offset));
316 }
317 };
318}
319
320QByteArray QSGAreaAllocator::serialize()
321{
322 QVarLengthArray<QSGAreaAllocatorNode *> nodesToProcess;
323
324 QStack<QSGAreaAllocatorNode *> nodes;
325 nodes.push(m_root);
326 while (!nodes.isEmpty()) {
327 QSGAreaAllocatorNode *node = nodes.pop();
328
329 nodesToProcess.append(node);
330 if (node->left != nullptr)
331 nodes.push(node->left);
332 if (node->right != nullptr)
333 nodes.push(node->right);
334 }
335
336 QByteArray ret;
337 ret.resize(AreaAllocatorTable::HeaderSize + AreaAllocatorTable::NodeSize * nodesToProcess.size());
338
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()));
344
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));
349
350 quint8 flags =
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;
356 }
357
358 return ret;
359}
360
361const char *QSGAreaAllocator::deserialize(const char *data, int size)
362{
363 if (uint(size) < AreaAllocatorTable::HeaderSize) {
364 qWarning("QSGAreaAllocator::deserialize: Data not long enough to fit header");
365 return nullptr;
366 }
367
368 const char *end = data + size;
369
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",
374 majorVersion,
375 minorVersion);
376 return nullptr;
377 }
378
379 m_size = QSize(AreaAllocatorTable::fetch<quint32>(data, AreaAllocatorTable::width),
380 AreaAllocatorTable::fetch<quint32>(data, AreaAllocatorTable::height));
381
382 Q_ASSERT(m_root != nullptr);
383 Q_ASSERT(m_root->left == nullptr);
384 Q_ASSERT(m_root->right == nullptr);
385
386 QStack<QSGAreaAllocatorNode *> nodes;
387 nodes.push(m_root);
388
389 data += AreaAllocatorTable::HeaderSize;
390 while (!nodes.isEmpty()) {
391 if (data + AreaAllocatorTable::NodeSize > end) {
392 qWarning("QSGAreaAllocator::deseriable: Data not long enough for nodes");
393 return nullptr;
394 }
395
396 QSGAreaAllocatorNode *node = nodes.pop();
397
398 node->split = AreaAllocatorTable::fetch<qint32>(data, AreaAllocatorTable::split);
399 node->splitType = SplitType(AreaAllocatorTable::fetch<quint32>(data, AreaAllocatorTable::splitType));
400
401 quint8 flags = AreaAllocatorTable::fetch<quint8>(data, AreaAllocatorTable::flags);
402 node->isOccupied = flags & AreaAllocatorTable::IsOccupied;
403
404 if (flags & AreaAllocatorTable::HasLeft) {
405 node->left = new QSGAreaAllocatorNode(node);
406 nodes.push(node->left);
407 }
408
409 if (flags & AreaAllocatorTable::HasRight) {
410 node->right = new QSGAreaAllocatorNode(node);
411 nodes.push(node->right);
412 }
413
414 data += AreaAllocatorTable::NodeSize;
415 }
416
417 return data;
418}
419
420QT_END_NAMESPACE
Combined button and popup list for selecting options.
static const int maxMargin
@ HorizontalSplit
@ VerticalSplit
QSGAreaAllocatorNode(QSGAreaAllocatorNode *parent)
QSGAreaAllocatorNode * parent
QSGAreaAllocatorNode * left
QSGAreaAllocatorNode * right