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