1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtQuick module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qsgbasicinternalimagenode_p.h" |
41 | |
42 | #include <QtCore/qvarlengtharray.h> |
43 | #include <QtCore/qmath.h> |
44 | |
45 | QT_BEGIN_NAMESPACE |
46 | |
47 | namespace |
48 | { |
49 | struct SmoothVertex |
50 | { |
51 | float x, y, u, v; |
52 | float dx, dy, du, dv; |
53 | }; |
54 | |
55 | const QSGGeometry::AttributeSet &smoothAttributeSet() |
56 | { |
57 | static QSGGeometry::Attribute data[] = { |
58 | QSGGeometry::Attribute::createWithAttributeType(pos: 0, tupleSize: 2, primitiveType: QSGGeometry::FloatType, attributeType: QSGGeometry::PositionAttribute), |
59 | QSGGeometry::Attribute::createWithAttributeType(pos: 1, tupleSize: 2, primitiveType: QSGGeometry::FloatType, attributeType: QSGGeometry::TexCoordAttribute), |
60 | QSGGeometry::Attribute::createWithAttributeType(pos: 2, tupleSize: 2, primitiveType: QSGGeometry::FloatType, attributeType: QSGGeometry::TexCoord1Attribute), |
61 | QSGGeometry::Attribute::createWithAttributeType(pos: 3, tupleSize: 2, primitiveType: QSGGeometry::FloatType, attributeType: QSGGeometry::TexCoord2Attribute) |
62 | }; |
63 | static QSGGeometry::AttributeSet attrs = { .count: 4, .stride: sizeof(SmoothVertex), .attributes: data }; |
64 | return attrs; |
65 | } |
66 | } |
67 | |
68 | QSGBasicInternalImageNode::QSGBasicInternalImageNode() |
69 | : m_innerSourceRect(0, 0, 1, 1) |
70 | , m_subSourceRect(0, 0, 1, 1) |
71 | , m_antialiasing(false) |
72 | , m_mirror(false) |
73 | , m_dirtyGeometry(false) |
74 | , m_geometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4) |
75 | , m_dynamicTexture(nullptr) |
76 | { |
77 | setGeometry(&m_geometry); |
78 | |
79 | #ifdef QSG_RUNTIME_DESCRIPTION |
80 | qsgnode_set_description(node: this, description: QLatin1String("internalimage" )); |
81 | #endif |
82 | } |
83 | |
84 | void QSGBasicInternalImageNode::setTargetRect(const QRectF &rect) |
85 | { |
86 | if (rect == m_targetRect) |
87 | return; |
88 | m_targetRect = rect; |
89 | m_dirtyGeometry = true; |
90 | } |
91 | |
92 | void QSGBasicInternalImageNode::setInnerTargetRect(const QRectF &rect) |
93 | { |
94 | if (rect == m_innerTargetRect) |
95 | return; |
96 | m_innerTargetRect = rect; |
97 | m_dirtyGeometry = true; |
98 | } |
99 | |
100 | void QSGBasicInternalImageNode::setInnerSourceRect(const QRectF &rect) |
101 | { |
102 | if (rect == m_innerSourceRect) |
103 | return; |
104 | m_innerSourceRect = rect; |
105 | m_dirtyGeometry = true; |
106 | } |
107 | |
108 | void QSGBasicInternalImageNode::setSubSourceRect(const QRectF &rect) |
109 | { |
110 | if (rect == m_subSourceRect) |
111 | return; |
112 | m_subSourceRect = rect; |
113 | m_dirtyGeometry = true; |
114 | } |
115 | |
116 | void QSGBasicInternalImageNode::setTexture(QSGTexture *texture) |
117 | { |
118 | Q_ASSERT(texture); |
119 | |
120 | setMaterialTexture(texture); |
121 | updateMaterialBlending(); |
122 | |
123 | markDirty(bits: DirtyMaterial); |
124 | |
125 | // Because the texture can be a different part of the atlas, we need to update it... |
126 | m_dirtyGeometry = true; |
127 | } |
128 | |
129 | void QSGBasicInternalImageNode::setAntialiasing(bool antialiasing) |
130 | { |
131 | if (antialiasing == m_antialiasing) |
132 | return; |
133 | m_antialiasing = antialiasing; |
134 | if (m_antialiasing) { |
135 | setGeometry(new QSGGeometry(smoothAttributeSet(), 0)); |
136 | setFlag(OwnsGeometry, true); |
137 | } else { |
138 | setGeometry(&m_geometry); |
139 | setFlag(OwnsGeometry, false); |
140 | } |
141 | updateMaterialAntialiasing(); |
142 | m_dirtyGeometry = true; |
143 | } |
144 | |
145 | void QSGBasicInternalImageNode::setMirror(bool mirror) |
146 | { |
147 | if (mirror == m_mirror) |
148 | return; |
149 | m_mirror = mirror; |
150 | m_dirtyGeometry = true; |
151 | } |
152 | |
153 | |
154 | void QSGBasicInternalImageNode::update() |
155 | { |
156 | if (m_dirtyGeometry) |
157 | updateGeometry(); |
158 | } |
159 | |
160 | void QSGBasicInternalImageNode::preprocess() |
161 | { |
162 | bool doDirty = false; |
163 | QSGDynamicTexture *t = qobject_cast<QSGDynamicTexture *>(object: materialTexture()); |
164 | if (t) { |
165 | doDirty = t->updateTexture(); |
166 | if (doDirty) { |
167 | // The geometry may need updating. This is expensive however, so do |
168 | // it only when something relevant has changed. |
169 | if (t != m_dynamicTexture |
170 | || t->textureSize() != m_dynamicTextureSize |
171 | || t->normalizedTextureSubRect() != m_dynamicTextureSubRect) { |
172 | updateGeometry(); |
173 | m_dynamicTextureSize = t->textureSize(); |
174 | m_dynamicTextureSubRect = t->normalizedTextureSubRect(); |
175 | } |
176 | } |
177 | } |
178 | m_dynamicTexture = t; |
179 | |
180 | if (updateMaterialBlending()) |
181 | doDirty = true; |
182 | |
183 | if (doDirty) |
184 | markDirty(bits: DirtyMaterial); |
185 | } |
186 | |
187 | namespace { |
188 | struct X { float x, tx; }; |
189 | struct Y { float y, ty; }; |
190 | } |
191 | |
192 | static inline void appendQuad(int indexType, void **indexData, |
193 | int topLeft, int topRight, |
194 | int bottomLeft, int bottomRight) |
195 | { |
196 | if (indexType == QSGGeometry::UnsignedIntType) { |
197 | quint32 *indices = static_cast<quint32 *>(*indexData); |
198 | *indices++ = topLeft; |
199 | *indices++ = bottomLeft; |
200 | *indices++ = bottomRight; |
201 | *indices++ = bottomRight; |
202 | *indices++ = topRight; |
203 | *indices++ = topLeft; |
204 | *indexData = indices; |
205 | } else { |
206 | Q_ASSERT(indexType == QSGGeometry::UnsignedShortType); |
207 | quint16 *indices = static_cast<quint16 *>(*indexData); |
208 | *indices++ = topLeft; |
209 | *indices++ = bottomLeft; |
210 | *indices++ = bottomRight; |
211 | *indices++ = bottomRight; |
212 | *indices++ = topRight; |
213 | *indices++ = topLeft; |
214 | *indexData = indices; |
215 | } |
216 | } |
217 | |
218 | QSGGeometry *QSGBasicInternalImageNode::updateGeometry(const QRectF &targetRect, |
219 | const QRectF &innerTargetRect, |
220 | const QRectF &sourceRect, |
221 | const QRectF &innerSourceRect, |
222 | const QRectF &subSourceRect, |
223 | QSGGeometry *geometry, |
224 | bool mirror, |
225 | bool antialiasing) |
226 | { |
227 | int floorLeft = qFloor(v: subSourceRect.left()); |
228 | int ceilRight = qCeil(v: subSourceRect.right()); |
229 | int floorTop = qFloor(v: subSourceRect.top()); |
230 | int ceilBottom = qCeil(v: subSourceRect.bottom()); |
231 | int hTiles = ceilRight - floorLeft; |
232 | int vTiles = ceilBottom - floorTop; |
233 | |
234 | int hCells = hTiles; |
235 | int vCells = vTiles; |
236 | if (innerTargetRect.width() == 0) |
237 | hCells = 0; |
238 | if (innerTargetRect.left() != targetRect.left()) |
239 | ++hCells; |
240 | if (innerTargetRect.right() != targetRect.right()) |
241 | ++hCells; |
242 | if (innerTargetRect.height() == 0) |
243 | vCells = 0; |
244 | if (innerTargetRect.top() != targetRect.top()) |
245 | ++vCells; |
246 | if (innerTargetRect.bottom() != targetRect.bottom()) |
247 | ++vCells; |
248 | |
249 | QVarLengthArray<X, 32> xData(2 * hCells); |
250 | QVarLengthArray<Y, 32> yData(2 * vCells); |
251 | X *xs = xData.data(); |
252 | Y *ys = yData.data(); |
253 | |
254 | if (innerTargetRect.left() != targetRect.left()) { |
255 | xs[0].x = targetRect.left(); |
256 | xs[0].tx = sourceRect.left(); |
257 | xs[1].x = innerTargetRect.left(); |
258 | xs[1].tx = innerSourceRect.left(); |
259 | xs += 2; |
260 | } |
261 | if (innerTargetRect.width() != 0 && hTiles > 0) { |
262 | xs[0].x = innerTargetRect.left(); |
263 | xs[0].tx = innerSourceRect.x() + (subSourceRect.left() - floorLeft) * innerSourceRect.width(); |
264 | ++xs; |
265 | float b = innerTargetRect.width() / subSourceRect.width(); |
266 | float a = innerTargetRect.x() - subSourceRect.x() * b; |
267 | for (int i = floorLeft + 1; i <= ceilRight - 1; ++i) { |
268 | xs[0].x = xs[1].x = a + b * i; |
269 | xs[0].tx = innerSourceRect.right(); |
270 | xs[1].tx = innerSourceRect.left(); |
271 | xs += 2; |
272 | } |
273 | xs[0].x = innerTargetRect.right(); |
274 | xs[0].tx = innerSourceRect.x() + (subSourceRect.right() - ceilRight + 1) * innerSourceRect.width(); |
275 | ++xs; |
276 | } |
277 | if (innerTargetRect.right() != targetRect.right()) { |
278 | xs[0].x = innerTargetRect.right(); |
279 | xs[0].tx = innerSourceRect.right(); |
280 | xs[1].x = targetRect.right(); |
281 | xs[1].tx = sourceRect.right(); |
282 | xs += 2; |
283 | } |
284 | Q_ASSERT(xs == xData.data() + xData.size()); |
285 | if (mirror) { |
286 | float leftPlusRight = targetRect.left() + targetRect.right(); |
287 | int count = xData.size(); |
288 | xs = xData.data(); |
289 | for (int i = 0; i < (count >> 1); ++i) |
290 | qSwap(value1&: xs[i], value2&: xs[count - 1 - i]); |
291 | for (int i = 0; i < count; ++i) |
292 | xs[i].x = leftPlusRight - xs[i].x; |
293 | } |
294 | |
295 | if (innerTargetRect.top() != targetRect.top()) { |
296 | ys[0].y = targetRect.top(); |
297 | ys[0].ty = sourceRect.top(); |
298 | ys[1].y = innerTargetRect.top(); |
299 | ys[1].ty = innerSourceRect.top(); |
300 | ys += 2; |
301 | } |
302 | if (innerTargetRect.height() != 0 && vTiles > 0) { |
303 | ys[0].y = innerTargetRect.top(); |
304 | ys[0].ty = innerSourceRect.y() + (subSourceRect.top() - floorTop) * innerSourceRect.height(); |
305 | ++ys; |
306 | float b = innerTargetRect.height() / subSourceRect.height(); |
307 | float a = innerTargetRect.y() - subSourceRect.y() * b; |
308 | for (int i = floorTop + 1; i <= ceilBottom - 1; ++i) { |
309 | ys[0].y = ys[1].y = a + b * i; |
310 | ys[0].ty = innerSourceRect.bottom(); |
311 | ys[1].ty = innerSourceRect.top(); |
312 | ys += 2; |
313 | } |
314 | ys[0].y = innerTargetRect.bottom(); |
315 | ys[0].ty = innerSourceRect.y() + (subSourceRect.bottom() - ceilBottom + 1) * innerSourceRect.height(); |
316 | ++ys; |
317 | } |
318 | if (innerTargetRect.bottom() != targetRect.bottom()) { |
319 | ys[0].y = innerTargetRect.bottom(); |
320 | ys[0].ty = innerSourceRect.bottom(); |
321 | ys[1].y = targetRect.bottom(); |
322 | ys[1].ty = sourceRect.bottom(); |
323 | ys += 2; |
324 | } |
325 | Q_ASSERT(ys == yData.data() + yData.size()); |
326 | |
327 | QSGGeometry::Type indexType = QSGGeometry::UnsignedShortType; |
328 | // We can handled up to 0xffff indices, but keep the limit lower here to |
329 | // merge better in the batch renderer. |
330 | if (hCells * vCells * 4 > 0x7fff) |
331 | indexType = QSGGeometry::UnsignedIntType; |
332 | |
333 | if (antialiasing) { |
334 | if (!geometry || geometry->indexType() != indexType) { |
335 | geometry = new QSGGeometry(smoothAttributeSet(), |
336 | hCells * vCells * 4 + (hCells + vCells - 1) * 4, |
337 | hCells * vCells * 6 + (hCells + vCells) * 12, |
338 | indexType); |
339 | } else { |
340 | geometry->allocate(vertexCount: hCells * vCells * 4 + (hCells + vCells - 1) * 4, |
341 | indexCount: hCells * vCells * 6 + (hCells + vCells) * 12); |
342 | } |
343 | QSGGeometry *g = geometry; |
344 | Q_ASSERT(g); |
345 | |
346 | g->setDrawingMode(QSGGeometry::DrawTriangles); |
347 | SmoothVertex *vertices = reinterpret_cast<SmoothVertex *>(g->vertexData()); |
348 | memset(s: vertices, c: 0, n: g->vertexCount() * g->sizeOfVertex()); |
349 | void *indexData = g->indexData(); |
350 | |
351 | // The deltas are how much the fuzziness can reach into the image. |
352 | // Only the border vertices are moved by the vertex shader, so the fuzziness |
353 | // can't reach further into the image than the closest interior vertices. |
354 | float leftDx = xData.at(idx: 1).x - xData.at(idx: 0).x; |
355 | float rightDx = xData.at(idx: xData.size() - 1).x - xData.at(idx: xData.size() - 2).x; |
356 | float topDy = yData.at(idx: 1).y - yData.at(idx: 0).y; |
357 | float bottomDy = yData.at(idx: yData.size() - 1).y - yData.at(idx: yData.size() - 2).y; |
358 | |
359 | float leftDu = xData.at(idx: 1).tx - xData.at(idx: 0).tx; |
360 | float rightDu = xData.at(idx: xData.size() - 1).tx - xData.at(idx: xData.size() - 2).tx; |
361 | float topDv = yData.at(idx: 1).ty - yData.at(idx: 0).ty; |
362 | float bottomDv = yData.at(idx: yData.size() - 1).ty - yData.at(idx: yData.size() - 2).ty; |
363 | |
364 | if (hCells == 1) { |
365 | leftDx = rightDx *= 0.5f; |
366 | leftDu = rightDu *= 0.5f; |
367 | } |
368 | if (vCells == 1) { |
369 | topDy = bottomDy *= 0.5f; |
370 | topDv = bottomDv *= 0.5f; |
371 | } |
372 | |
373 | // This delta is how much the fuzziness can reach out from the image. |
374 | float delta = float(qAbs(t: targetRect.width()) < qAbs(t: targetRect.height()) |
375 | ? targetRect.width() : targetRect.height()) * 0.5f; |
376 | |
377 | int index = 0; |
378 | ys = yData.data(); |
379 | for (int j = 0; j < vCells; ++j, ys += 2) { |
380 | xs = xData.data(); |
381 | bool isTop = j == 0; |
382 | bool isBottom = j == vCells - 1; |
383 | for (int i = 0; i < hCells; ++i, xs += 2) { |
384 | bool isLeft = i == 0; |
385 | bool isRight = i == hCells - 1; |
386 | |
387 | SmoothVertex *v = vertices + index; |
388 | |
389 | int topLeft = index; |
390 | for (int k = (isTop || isLeft ? 2 : 1); k--; ++v, ++index) { |
391 | v->x = xs[0].x; |
392 | v->u = xs[0].tx; |
393 | v->y = ys[0].y; |
394 | v->v = ys[0].ty; |
395 | } |
396 | |
397 | int topRight = index; |
398 | for (int k = (isTop || isRight ? 2 : 1); k--; ++v, ++index) { |
399 | v->x = xs[1].x; |
400 | v->u = xs[1].tx; |
401 | v->y = ys[0].y; |
402 | v->v = ys[0].ty; |
403 | } |
404 | |
405 | int bottomLeft = index; |
406 | for (int k = (isBottom || isLeft ? 2 : 1); k--; ++v, ++index) { |
407 | v->x = xs[0].x; |
408 | v->u = xs[0].tx; |
409 | v->y = ys[1].y; |
410 | v->v = ys[1].ty; |
411 | } |
412 | |
413 | int bottomRight = index; |
414 | for (int k = (isBottom || isRight ? 2 : 1); k--; ++v, ++index) { |
415 | v->x = xs[1].x; |
416 | v->u = xs[1].tx; |
417 | v->y = ys[1].y; |
418 | v->v = ys[1].ty; |
419 | } |
420 | |
421 | appendQuad(indexType: g->indexType(), indexData: &indexData, topLeft, topRight, bottomLeft, bottomRight); |
422 | |
423 | if (isTop) { |
424 | vertices[topLeft].dy = vertices[topRight].dy = topDy; |
425 | vertices[topLeft].dv = vertices[topRight].dv = topDv; |
426 | vertices[topLeft + 1].dy = vertices[topRight + 1].dy = -delta; |
427 | appendQuad(indexType: g->indexType(), indexData: &indexData, topLeft: topLeft + 1, topRight: topRight + 1, bottomLeft: topLeft, bottomRight: topRight); |
428 | } |
429 | |
430 | if (isBottom) { |
431 | vertices[bottomLeft].dy = vertices[bottomRight].dy = -bottomDy; |
432 | vertices[bottomLeft].dv = vertices[bottomRight].dv = -bottomDv; |
433 | vertices[bottomLeft + 1].dy = vertices[bottomRight + 1].dy = delta; |
434 | appendQuad(indexType: g->indexType(), indexData: &indexData, topLeft: bottomLeft, topRight: bottomRight, bottomLeft: bottomLeft + 1, bottomRight: bottomRight + 1); |
435 | } |
436 | |
437 | if (isLeft) { |
438 | vertices[topLeft].dx = vertices[bottomLeft].dx = leftDx; |
439 | vertices[topLeft].du = vertices[bottomLeft].du = leftDu; |
440 | vertices[topLeft + 1].dx = vertices[bottomLeft + 1].dx = -delta; |
441 | appendQuad(indexType: g->indexType(), indexData: &indexData, topLeft: topLeft + 1, topRight: topLeft, bottomLeft: bottomLeft + 1, bottomRight: bottomLeft); |
442 | } |
443 | |
444 | if (isRight) { |
445 | vertices[topRight].dx = vertices[bottomRight].dx = -rightDx; |
446 | vertices[topRight].du = vertices[bottomRight].du = -rightDu; |
447 | vertices[topRight + 1].dx = vertices[bottomRight + 1].dx = delta; |
448 | appendQuad(indexType: g->indexType(), indexData: &indexData, topLeft: topRight, topRight: topRight + 1, bottomLeft: bottomRight, bottomRight: bottomRight + 1); |
449 | } |
450 | } |
451 | } |
452 | |
453 | Q_ASSERT(index == g->vertexCount()); |
454 | } else { |
455 | if (!geometry || geometry->indexType() != indexType) { |
456 | geometry = new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), |
457 | hCells * vCells * 4, hCells * vCells * 6, |
458 | indexType); |
459 | } else { |
460 | geometry->allocate(vertexCount: hCells * vCells * 4, indexCount: hCells * vCells * 6); |
461 | } |
462 | geometry->setDrawingMode(QSGGeometry::DrawTriangles); |
463 | QSGGeometry::TexturedPoint2D *vertices = geometry->vertexDataAsTexturedPoint2D(); |
464 | ys = yData.data(); |
465 | for (int j = 0; j < vCells; ++j, ys += 2) { |
466 | xs = xData.data(); |
467 | for (int i = 0; i < hCells; ++i, xs += 2) { |
468 | vertices[0].x = vertices[2].x = xs[0].x; |
469 | vertices[0].tx = vertices[2].tx = xs[0].tx; |
470 | vertices[1].x = vertices[3].x = xs[1].x; |
471 | vertices[1].tx = vertices[3].tx = xs[1].tx; |
472 | |
473 | vertices[0].y = vertices[1].y = ys[0].y; |
474 | vertices[0].ty = vertices[1].ty = ys[0].ty; |
475 | vertices[2].y = vertices[3].y = ys[1].y; |
476 | vertices[2].ty = vertices[3].ty = ys[1].ty; |
477 | |
478 | vertices += 4; |
479 | } |
480 | } |
481 | void *indexData = geometry->indexData(); |
482 | for (int i = 0; i < 4 * vCells * hCells; i += 4) |
483 | appendQuad(indexType: geometry->indexType(), indexData: &indexData, topLeft: i, topRight: i + 1, bottomLeft: i + 2, bottomRight: i + 3); |
484 | } |
485 | return geometry; |
486 | } |
487 | |
488 | void QSGBasicInternalImageNode::updateGeometry() |
489 | { |
490 | Q_ASSERT(!m_targetRect.isEmpty()); |
491 | const QSGTexture *t = materialTexture(); |
492 | if (!t) { |
493 | QSGGeometry *g = geometry(); |
494 | g->allocate(vertexCount: 4); |
495 | g->setDrawingMode(QSGGeometry::DrawTriangleStrip); |
496 | memset(s: g->vertexData(), c: 0, n: g->sizeOfVertex() * 4); |
497 | } else { |
498 | QRectF sourceRect = t->normalizedTextureSubRect(); |
499 | |
500 | QRectF innerSourceRect(sourceRect.x() + m_innerSourceRect.x() * sourceRect.width(), |
501 | sourceRect.y() + m_innerSourceRect.y() * sourceRect.height(), |
502 | m_innerSourceRect.width() * sourceRect.width(), |
503 | m_innerSourceRect.height() * sourceRect.height()); |
504 | |
505 | bool hasMargins = m_targetRect != m_innerTargetRect; |
506 | |
507 | int floorLeft = qFloor(v: m_subSourceRect.left()); |
508 | int ceilRight = qCeil(v: m_subSourceRect.right()); |
509 | int floorTop = qFloor(v: m_subSourceRect.top()); |
510 | int ceilBottom = qCeil(v: m_subSourceRect.bottom()); |
511 | int hTiles = ceilRight - floorLeft; |
512 | int vTiles = ceilBottom - floorTop; |
513 | |
514 | bool hasTiles = hTiles > 1 || vTiles > 1; |
515 | bool fullTexture = innerSourceRect == QRectF(0, 0, 1, 1); |
516 | |
517 | // An image can be rendered as a single quad if: |
518 | // - There are no margins, and either: |
519 | // - the image isn't repeated |
520 | // - the source rectangle fills the entire texture so that texture wrapping can be used, |
521 | // and NPOT is supported |
522 | if (!hasMargins && (!hasTiles || (fullTexture && supportsWrap(size: t->textureSize())))) { |
523 | QRectF sr; |
524 | if (!fullTexture) { |
525 | sr = QRectF(innerSourceRect.x() + (m_subSourceRect.left() - floorLeft) * innerSourceRect.width(), |
526 | innerSourceRect.y() + (m_subSourceRect.top() - floorTop) * innerSourceRect.height(), |
527 | m_subSourceRect.width() * innerSourceRect.width(), |
528 | m_subSourceRect.height() * innerSourceRect.height()); |
529 | } else { |
530 | sr = QRectF(m_subSourceRect.left() - floorLeft, m_subSourceRect.top() - floorTop, |
531 | m_subSourceRect.width(), m_subSourceRect.height()); |
532 | } |
533 | if (m_mirror) { |
534 | qreal oldLeft = sr.left(); |
535 | sr.setLeft(sr.right()); |
536 | sr.setRight(oldLeft); |
537 | } |
538 | |
539 | if (m_antialiasing) { |
540 | QSGGeometry *g = geometry(); |
541 | Q_ASSERT(g != &m_geometry); |
542 | if (g->indexType() != QSGGeometry::UnsignedShortType) { |
543 | setGeometry(new QSGGeometry(smoothAttributeSet(), 0)); |
544 | g = geometry(); |
545 | } |
546 | g->allocate(vertexCount: 8, indexCount: 14); |
547 | g->setDrawingMode(QSGGeometry::DrawTriangleStrip); |
548 | SmoothVertex *vertices = reinterpret_cast<SmoothVertex *>(g->vertexData()); |
549 | float delta = float(qAbs(t: m_targetRect.width()) < qAbs(t: m_targetRect.height()) |
550 | ? m_targetRect.width() : m_targetRect.height()) * 0.5f; |
551 | float sx = float(sr.width() / m_targetRect.width()); |
552 | float sy = float(sr.height() / m_targetRect.height()); |
553 | for (int d = -1; d <= 1; d += 2) { |
554 | for (int j = 0; j < 2; ++j) { |
555 | for (int i = 0; i < 2; ++i, ++vertices) { |
556 | vertices->x = m_targetRect.x() + i * m_targetRect.width(); |
557 | vertices->y = m_targetRect.y() + j * m_targetRect.height(); |
558 | vertices->u = sr.x() + i * sr.width(); |
559 | vertices->v = sr.y() + j * sr.height(); |
560 | vertices->dx = (i == 0 ? delta : -delta) * d; |
561 | vertices->dy = (j == 0 ? delta : -delta) * d; |
562 | vertices->du = (d < 0 ? 0 : vertices->dx * sx); |
563 | vertices->dv = (d < 0 ? 0 : vertices->dy * sy); |
564 | } |
565 | } |
566 | } |
567 | Q_ASSERT(vertices - g->vertexCount() == g->vertexData()); |
568 | static const quint16 indices[] = { |
569 | 0, 4, 1, 5, 3, 7, 2, 6, 0, 4, |
570 | 4, 6, 5, 7 |
571 | }; |
572 | Q_ASSERT(g->sizeOfIndex() * g->indexCount() == sizeof(indices)); |
573 | memcpy(dest: g->indexDataAsUShort(), src: indices, n: sizeof(indices)); |
574 | } else { |
575 | m_geometry.allocate(vertexCount: 4); |
576 | m_geometry.setDrawingMode(QSGGeometry::DrawTriangleStrip); |
577 | QSGGeometry::updateTexturedRectGeometry(g: &m_geometry, rect: m_targetRect, sourceRect: sr); |
578 | } |
579 | } else { |
580 | QSGGeometry *g = geometry(); |
581 | g = updateGeometry(targetRect: m_targetRect, innerTargetRect: m_innerTargetRect, |
582 | sourceRect, innerSourceRect, subSourceRect: m_subSourceRect, |
583 | geometry: g, mirror: m_mirror, antialiasing: m_antialiasing); |
584 | if (g != geometry()) { |
585 | setGeometry(g); |
586 | setFlag(OwnsGeometry, true); |
587 | } |
588 | } |
589 | } |
590 | markDirty(bits: DirtyGeometry); |
591 | m_dirtyGeometry = false; |
592 | } |
593 | |
594 | QT_END_NAMESPACE |
595 | |