185QSGGeometry *QSGBasicInternalImageNode::updateGeometry(
const QRectF &targetRect,
186 const QRectF &innerTargetRect,
187 const QRectF &sourceRect,
188 const QRectF &innerSourceRect,
189 const QRectF &subSourceRect,
190 QSGGeometry *geometry,
191 bool mirrorHorizontally,
192 bool mirrorVertically,
195 int floorLeft = qFloor(subSourceRect.left());
196 int ceilRight = qCeil(subSourceRect.right());
197 int floorTop = qFloor(subSourceRect.top());
198 int ceilBottom = qCeil(subSourceRect.bottom());
199 int hTiles = ceilRight - floorLeft;
200 int vTiles = ceilBottom - floorTop;
204 if (innerTargetRect.width() == 0)
206 if (innerTargetRect.left() != targetRect.left())
208 if (innerTargetRect.right() != targetRect.right())
210 if (innerTargetRect.height() == 0)
212 if (innerTargetRect.top() != targetRect.top())
214 if (innerTargetRect.bottom() != targetRect.bottom())
217 QVarLengthArray<X, 32> xData(2 * hCells);
218 QVarLengthArray<Y, 32> yData(2 * vCells);
219 X *xs = xData.data();
220 Y *ys = yData.data();
222 if (innerTargetRect.left() != targetRect.left()) {
223 xs[0].x = targetRect.left();
224 xs[0].tx = sourceRect.left();
225 xs[1].x = innerTargetRect.left();
226 xs[1].tx = innerSourceRect.left();
229 if (innerTargetRect.width() != 0 && hTiles > 0) {
230 xs[0].x = innerTargetRect.left();
231 xs[0].tx = innerSourceRect.x() + (subSourceRect.left() - floorLeft) * innerSourceRect.width();
233 float b = innerTargetRect.width() / subSourceRect.width();
234 float a = innerTargetRect.x() - subSourceRect.x() * b;
235 for (
int i = floorLeft + 1; i <= ceilRight - 1; ++i) {
236 xs[0].x = xs[1].x = a + b * i;
237 xs[0].tx = innerSourceRect.right();
238 xs[1].tx = innerSourceRect.left();
241 xs[0].x = innerTargetRect.right();
242 xs[0].tx = innerSourceRect.x() + (subSourceRect.right() - ceilRight + 1) * innerSourceRect.width();
245 if (innerTargetRect.right() != targetRect.right()) {
246 xs[0].x = innerTargetRect.right();
247 xs[0].tx = innerSourceRect.right();
248 xs[1].x = targetRect.right();
249 xs[1].tx = sourceRect.right();
252 Q_ASSERT(xs == xData.data() + xData.size());
253 if (mirrorHorizontally) {
254 float leftPlusRight = targetRect.left() + targetRect.right();
255 int count = xData.size();
257 for (
int i = 0; i < (count >> 1); ++i)
258 qSwap(xs[i], xs[count - 1 - i]);
259 for (
int i = 0; i < count; ++i)
260 xs[i].x = leftPlusRight - xs[i].x;
263 if (innerTargetRect.top() != targetRect.top()) {
264 ys[0].y = targetRect.top();
265 ys[0].ty = sourceRect.top();
266 ys[1].y = innerTargetRect.top();
267 ys[1].ty = innerSourceRect.top();
270 if (innerTargetRect.height() != 0 && vTiles > 0) {
271 ys[0].y = innerTargetRect.top();
272 ys[0].ty = innerSourceRect.y() + (subSourceRect.top() - floorTop) * innerSourceRect.height();
274 float b = innerTargetRect.height() / subSourceRect.height();
275 float a = innerTargetRect.y() - subSourceRect.y() * b;
276 for (
int i = floorTop + 1; i <= ceilBottom - 1; ++i) {
277 ys[0].y = ys[1].y = a + b * i;
278 ys[0].ty = innerSourceRect.bottom();
279 ys[1].ty = innerSourceRect.top();
282 ys[0].y = innerTargetRect.bottom();
283 ys[0].ty = innerSourceRect.y() + (subSourceRect.bottom() - ceilBottom + 1) * innerSourceRect.height();
286 if (innerTargetRect.bottom() != targetRect.bottom()) {
287 ys[0].y = innerTargetRect.bottom();
288 ys[0].ty = innerSourceRect.bottom();
289 ys[1].y = targetRect.bottom();
290 ys[1].ty = sourceRect.bottom();
293 Q_ASSERT(ys == yData.data() + yData.size());
294 if (mirrorVertically) {
295 float topPlusBottom = targetRect.top() + targetRect.bottom();
296 int count = yData.size();
298 for (
int i = 0; i < (count >> 1); ++i)
299 qSwap(ys[i], ys[count - 1 - i]);
300 for (
int i = 0; i < count; ++i)
301 ys[i].y = topPlusBottom - ys[i].y;
304 QSGGeometry::Type indexType = QSGGeometry::UnsignedShortType;
307 if (hCells * vCells * 4 > 0x7fff)
308 indexType = QSGGeometry::UnsignedIntType;
311 if (!geometry || geometry->indexType() != indexType) {
312 geometry =
new QSGGeometry(smoothImageAttributeSet(),
313 hCells * vCells * 4 + (hCells + vCells - 1) * 4,
314 hCells * vCells * 6 + (hCells + vCells) * 12,
317 geometry->allocate(hCells * vCells * 4 + (hCells + vCells - 1) * 4,
318 hCells * vCells * 6 + (hCells + vCells) * 12);
320 QSGGeometry *g = geometry;
323 g->setDrawingMode(QSGGeometry::DrawTriangles);
324 auto *vertices =
reinterpret_cast<SmoothImageVertex *>(g->vertexData());
325 memset(vertices, 0, g->vertexCount() * g->sizeOfVertex());
326 void *indexData = g->indexData();
331 float leftDx = xData.at(1).x - xData.at(0).x;
332 float rightDx = xData.at(xData.size() - 1).x - xData.at(xData.size() - 2).x;
333 float topDy = yData.at(1).y - yData.at(0).y;
334 float bottomDy = yData.at(yData.size() - 1).y - yData.at(yData.size() - 2).y;
336 float leftDu = xData.at(1).tx - xData.at(0).tx;
337 float rightDu = xData.at(xData.size() - 1).tx - xData.at(xData.size() - 2).tx;
338 float topDv = yData.at(1).ty - yData.at(0).ty;
339 float bottomDv = yData.at(yData.size() - 1).ty - yData.at(yData.size() - 2).ty;
342 leftDx = rightDx *= 0.5f;
343 leftDu = rightDu *= 0.5f;
346 topDy = bottomDy *= 0.5f;
347 topDv = bottomDv *= 0.5f;
351 float delta =
float(qAbs(targetRect.width()) < qAbs(targetRect.height())
352 ? targetRect.width() : targetRect.height()) * 0.5f;
356 for (
int j = 0; j < vCells; ++j, ys += 2) {
359 bool isBottom = j == vCells - 1;
360 for (
int i = 0; i < hCells; ++i, xs += 2) {
361 bool isLeft = i == 0;
362 bool isRight = i == hCells - 1;
364 SmoothImageVertex *v = vertices + index;
367 for (
int k = (isTop || isLeft ? 2 : 1); k--; ++v, ++index) {
374 int topRight = index;
375 for (
int k = (isTop || isRight ? 2 : 1); k--; ++v, ++index) {
382 int bottomLeft = index;
383 for (
int k = (isBottom || isLeft ? 2 : 1); k--; ++v, ++index) {
390 int bottomRight = index;
391 for (
int k = (isBottom || isRight ? 2 : 1); k--; ++v, ++index) {
398 appendQuad(g->indexType(), &indexData, topLeft, topRight, bottomLeft, bottomRight);
401 vertices[topLeft].dy = vertices[topRight].dy = topDy;
402 vertices[topLeft].dv = vertices[topRight].dv = topDv;
403 vertices[topLeft + 1].dy = vertices[topRight + 1].dy = -delta;
404 appendQuad(g->indexType(), &indexData, topLeft + 1, topRight + 1, topLeft, topRight);
408 vertices[bottomLeft].dy = vertices[bottomRight].dy = -bottomDy;
409 vertices[bottomLeft].dv = vertices[bottomRight].dv = -bottomDv;
410 vertices[bottomLeft + 1].dy = vertices[bottomRight + 1].dy = delta;
411 appendQuad(g->indexType(), &indexData, bottomLeft, bottomRight, bottomLeft + 1, bottomRight + 1);
415 vertices[topLeft].dx = vertices[bottomLeft].dx = leftDx;
416 vertices[topLeft].du = vertices[bottomLeft].du = leftDu;
417 vertices[topLeft + 1].dx = vertices[bottomLeft + 1].dx = -delta;
418 appendQuad(g->indexType(), &indexData, topLeft + 1, topLeft, bottomLeft + 1, bottomLeft);
422 vertices[topRight].dx = vertices[bottomRight].dx = -rightDx;
423 vertices[topRight].du = vertices[bottomRight].du = -rightDu;
424 vertices[topRight + 1].dx = vertices[bottomRight + 1].dx = delta;
425 appendQuad(g->indexType(), &indexData, topRight, topRight + 1, bottomRight, bottomRight + 1);
430 Q_ASSERT(index == g->vertexCount());
432 if (!geometry || geometry->indexType() != indexType) {
433 geometry =
new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(),
434 hCells * vCells * 4, hCells * vCells * 6,
437 geometry->allocate(hCells * vCells * 4, hCells * vCells * 6);
439 geometry->setDrawingMode(QSGGeometry::DrawTriangles);
440 QSGGeometry::TexturedPoint2D *vertices = geometry->vertexDataAsTexturedPoint2D();
442 for (
int j = 0; j < vCells; ++j, ys += 2) {
444 for (
int i = 0; i < hCells; ++i, xs += 2) {
445 vertices[0].x = vertices[2].x = xs[0].x;
446 vertices[0].tx = vertices[2].tx = xs[0].tx;
447 vertices[1].x = vertices[3].x = xs[1].x;
448 vertices[1].tx = vertices[3].tx = xs[1].tx;
450 vertices[0].y = vertices[1].y = ys[0].y;
451 vertices[0].ty = vertices[1].ty = ys[0].ty;
452 vertices[2].y = vertices[3].y = ys[1].y;
453 vertices[2].ty = vertices[3].ty = ys[1].ty;
458 void *indexData = geometry->indexData();
459 for (
int i = 0; i < 4 * vCells * hCells; i += 4)
460 appendQuad(geometry->indexType(), &indexData, i, i + 1, i + 2, i + 3);
465void QSGBasicInternalImageNode::updateGeometry()
467 Q_ASSERT(!m_targetRect.isEmpty());
468 const QSGTexture *t = materialTexture();
470 QSGGeometry *g = geometry();
472 g->setDrawingMode(QSGGeometry::DrawTriangleStrip);
473 memset(g->vertexData(), 0, g->sizeOfVertex() * 4);
475 QRectF sourceRect = t->normalizedTextureSubRect();
477 QRectF innerSourceRect(sourceRect.x() + m_innerSourceRect.x() * sourceRect.width(),
478 sourceRect.y() + m_innerSourceRect.y() * sourceRect.height(),
479 m_innerSourceRect.width() * sourceRect.width(),
480 m_innerSourceRect.height() * sourceRect.height());
482 bool hasMargins = m_targetRect != m_innerTargetRect;
484 int floorLeft = qFloor(m_subSourceRect.left());
485 int ceilRight = qCeil(m_subSourceRect.right());
486 int floorTop = qFloor(m_subSourceRect.top());
487 int ceilBottom = qCeil(m_subSourceRect.bottom());
488 int hTiles = ceilRight - floorLeft;
489 int vTiles = ceilBottom - floorTop;
491 bool hasTiles = hTiles > 1 || vTiles > 1;
492 bool fullTexture = innerSourceRect == QRectF(0, 0, 1, 1);
499 if (!hasMargins && (!hasTiles || (fullTexture && supportsWrap(t->textureSize())))) {
502 sr = QRectF(innerSourceRect.x() + (m_subSourceRect.left() - floorLeft) * innerSourceRect.width(),
503 innerSourceRect.y() + (m_subSourceRect.top() - floorTop) * innerSourceRect.height(),
504 m_subSourceRect.width() * innerSourceRect.width(),
505 m_subSourceRect.height() * innerSourceRect.height());
507 sr = QRectF(m_subSourceRect.left() - floorLeft, m_subSourceRect.top() - floorTop,
508 m_subSourceRect.width(), m_subSourceRect.height());
510 if (m_mirrorHorizontally) {
511 qreal oldLeft = sr.left();
512 sr.setLeft(sr.right());
513 sr.setRight(oldLeft);
515 if (m_mirrorVertically) {
516 qreal oldTop = sr.top();
517 sr.setTop(sr.bottom());
518 sr.setBottom(oldTop);
521 if (m_antialiasing) {
522 QSGGeometry *g = geometry();
523 Q_ASSERT(g != &m_geometry);
524 if (g->indexType() != QSGGeometry::UnsignedShortType) {
525 setGeometry(
new QSGGeometry(smoothImageAttributeSet(), 0));
529 g->setDrawingMode(QSGGeometry::DrawTriangleStrip);
530 auto *vertices =
reinterpret_cast<SmoothImageVertex *>(g->vertexData());
531 float delta =
float(qAbs(m_targetRect.width()) < qAbs(m_targetRect.height())
532 ? m_targetRect.width() : m_targetRect.height()) * 0.5f;
533 float sx =
float(sr.width() / m_targetRect.width());
534 float sy =
float(sr.height() / m_targetRect.height());
535 for (
int d = -1; d <= 1; d += 2) {
536 for (
int j = 0; j < 2; ++j) {
537 for (
int i = 0; i < 2; ++i, ++vertices) {
538 vertices->x = m_targetRect.x() + i * m_targetRect.width();
539 vertices->y = m_targetRect.y() + j * m_targetRect.height();
540 vertices->u = sr.x() + i * sr.width();
541 vertices->v = sr.y() + j * sr.height();
542 vertices->dx = (i == 0 ? delta : -delta) * d;
543 vertices->dy = (j == 0 ? delta : -delta) * d;
544 vertices->du = (d < 0 ? 0 : vertices->dx * sx);
545 vertices->dv = (d < 0 ? 0 : vertices->dy * sy);
549 Q_ASSERT(vertices - g->vertexCount() == g->vertexData());
550 static const quint16 indices[] = {
551 0, 4, 1, 5, 3, 7, 2, 6, 0, 4,
554 Q_ASSERT(g->sizeOfIndex() * g->indexCount() ==
sizeof(indices));
555 memcpy(g->indexDataAsUShort(), indices,
sizeof(indices));
557 m_geometry.allocate(4);
558 m_geometry.setDrawingMode(QSGGeometry::DrawTriangleStrip);
559 QSGGeometry::updateTexturedRectGeometry(&m_geometry, m_targetRect, sr);
562 QSGGeometry *g = geometry();
563 g = updateGeometry(m_targetRect, m_innerTargetRect,
564 sourceRect, innerSourceRect, m_subSourceRect,
565 g, m_mirrorHorizontally, m_mirrorVertically, m_antialiasing);
566 if (g != geometry()) {
568 setFlag(OwnsGeometry,
true);
572 markDirty(DirtyGeometry);
573 m_dirtyGeometry =
false;