aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/scenegraph/qsgdefaultimagenode.cpp
blob: cadd18e8338eed6c5423f357e24b06b22a682226 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qsgdefaultimagenode_p.h"

#include <QtCore/qvarlengtharray.h>
#include <QtCore/qmath.h>
#include <QtGui/qopenglfunctions.h>

#include <qsgtexturematerial.h>
#include <private/qsgtexturematerial_p.h>
#include <qsgmaterial.h>

QT_BEGIN_NAMESPACE

namespace
{
    struct SmoothVertex
    {
        float x, y, u, v;
        float dx, dy, du, dv;
    };

    const QSGGeometry::AttributeSet &smoothAttributeSet()
    {
        static QSGGeometry::Attribute data[] = {
            QSGGeometry::Attribute::create(0, 2, GL_FLOAT, true),
            QSGGeometry::Attribute::create(1, 2, GL_FLOAT, false),
            QSGGeometry::Attribute::create(2, 2, GL_FLOAT, false),
            QSGGeometry::Attribute::create(3, 2, GL_FLOAT, false)
        };
        static QSGGeometry::AttributeSet attrs = { 4, sizeof(SmoothVertex), data };
        return attrs;
    }
}

class SmoothTextureMaterialShader : public QSGTextureMaterialShader
{
public:
    virtual void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect);
    virtual char const *const *attributeNames() const;

protected:
    virtual void initialize();
    virtual const char *vertexShader() const;
    virtual const char *fragmentShader() const;

    int m_pixelSizeLoc;
};


SmoothTextureMaterial::SmoothTextureMaterial()
{
    setFlag(RequiresFullMatrixExceptTranslate, true);
    setFlag(Blending, true);
}

void SmoothTextureMaterial::setTexture(QSGTexture *texture)
{
    m_texture = texture;
}

QSGMaterialType *SmoothTextureMaterial::type() const
{
    static QSGMaterialType type;
    return &type;
}

QSGMaterialShader *SmoothTextureMaterial::createShader() const
{
    return new SmoothTextureMaterialShader;
}

void SmoothTextureMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
{
    if (oldEffect == 0) {
        // The viewport is constant, so set the pixel size uniform only once.
        QRect r = state.viewportRect();
        program()->setUniformValue(m_pixelSizeLoc, 2.0f / r.width(), 2.0f / r.height());
    }
    QSGTextureMaterialShader::updateState(state, newEffect, oldEffect);
}

char const *const *SmoothTextureMaterialShader::attributeNames() const
{
    static char const *const attributes[] = {
        "vertex",
        "multiTexCoord",
        "vertexOffset",
        "texCoordOffset",
        0
    };
    return attributes;
}

void SmoothTextureMaterialShader::initialize()
{
    m_pixelSizeLoc = program()->uniformLocation("pixelSize");
    QSGTextureMaterialShader::initialize();
}

const char *SmoothTextureMaterialShader::vertexShader() const
{
    return
            "uniform highp vec2 pixelSize; \n"
            "uniform highp mat4 qt_Matrix; \n"
            "uniform lowp float opacity; \n"
            "attribute highp vec4 vertex; \n"
            "attribute highp vec2 multiTexCoord; \n"
            "attribute highp vec2 vertexOffset; \n"
            "attribute highp vec2 texCoordOffset; \n"
            "varying highp vec2 texCoord; \n"
            "varying lowp float vertexOpacity; \n"
            "void main() { \n"
            "    highp vec4 pos = qt_Matrix * vertex; \n"
            "    gl_Position = pos; \n"
            "    texCoord = multiTexCoord; \n"

            "    if (vertexOffset.x != 0.) { \n"
            "        highp vec4 delta = qt_Matrix[0] * vertexOffset.x; \n"
            "        highp vec2 dir = delta.xy * pos.w - pos.xy * delta.w; \n"
            "        highp vec2 ndir = .5 * pixelSize * normalize(dir / pixelSize);  \n"
            "        dir -= ndir * delta.w * pos.w; \n"
            "        highp float numerator = dot(dir, ndir * pos.w * pos.w); \n"
            "        highp float scale = 0.0; \n"
            "        if (numerator < 0.0) \n"
            "            scale = 1.0; \n"
            "        else \n"
            "            scale = min(1.0, numerator / dot(dir, dir)); \n"
            "        gl_Position += scale * delta; \n"
            "        texCoord.x += scale * texCoordOffset.x; \n"
            "    } \n"

            "    if (vertexOffset.y != 0.) { \n"
            "        highp vec4 delta = qt_Matrix[1] * vertexOffset.y; \n"
            "        highp vec2 dir = delta.xy * pos.w - pos.xy * delta.w; \n"
            "        highp vec2 ndir = .5 * pixelSize * normalize(dir / pixelSize);  \n"
            "        dir -= ndir * delta.w * pos.w; \n"
            "        highp float numerator = dot(dir, ndir * pos.w * pos.w); \n"
            "        highp float scale = 0.0; \n"
            "        if (numerator < 0.0) \n"
            "            scale = 1.0; \n"
            "        else \n"
            "            scale = min(1.0, numerator / dot(dir, dir)); \n"
            "        gl_Position += scale * delta; \n"
            "        texCoord.y += scale * texCoordOffset.y; \n"
            "    } \n"

            "    bool onEdge = any(notEqual(vertexOffset, vec2(0.))); \n"
            "    bool outerEdge = all(equal(texCoordOffset, vec2(0.))); \n"
            "    vertexOpacity = onEdge && outerEdge ? 0. : opacity; \n"
            "}";
}

const char *SmoothTextureMaterialShader::fragmentShader() const
{
    return
            "uniform sampler2D qt_Texture; \n"
            "varying highp vec2 texCoord; \n"
            "varying lowp float vertexOpacity; \n"
            "void main() { \n"
            "    gl_FragColor = texture2D(qt_Texture, texCoord) * vertexOpacity; \n"
            "}";
}

QSGDefaultImageNode::QSGDefaultImageNode()
    : m_innerSourceRect(0, 0, 1, 1)
    , m_subSourceRect(0, 0, 1, 1)
    , m_antialiasing(false)
    , m_mirror(false)
    , m_dirtyGeometry(false)
    , m_geometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4)
{
    setMaterial(&m_materialO);
    setOpaqueMaterial(&m_material);
    setGeometry(&m_geometry);

#ifdef QML_RUNTIME_TESTING
    description = QLatin1String("image");
#endif
}

void QSGDefaultImageNode::setTargetRect(const QRectF &rect)
{
    if (rect == m_targetRect)
        return;
    m_targetRect = rect;
    m_dirtyGeometry = true;
}

void QSGDefaultImageNode::setInnerTargetRect(const QRectF &rect)
{
    if (rect == m_innerTargetRect)
        return;
    m_innerTargetRect = rect;
    m_dirtyGeometry = true;
}

void QSGDefaultImageNode::setInnerSourceRect(const QRectF &rect)
{
    if (rect == m_innerSourceRect)
        return;
    m_innerSourceRect = rect;
    m_dirtyGeometry = true;
}

void QSGDefaultImageNode::setSubSourceRect(const QRectF &rect)
{
    if (rect == m_subSourceRect)
        return;
    m_subSourceRect = rect;
    m_dirtyGeometry = true;
}

void QSGDefaultImageNode::setFiltering(QSGTexture::Filtering filtering)
{
    if (m_material.filtering() == filtering)
        return;

    m_material.setFiltering(filtering);
    m_materialO.setFiltering(filtering);
    m_smoothMaterial.setFiltering(filtering);
    markDirty(DirtyMaterial);
}


void QSGDefaultImageNode::setMipmapFiltering(QSGTexture::Filtering filtering)
{
    if (m_material.mipmapFiltering() == filtering)
        return;

    m_material.setMipmapFiltering(filtering);
    m_materialO.setMipmapFiltering(filtering);
    m_smoothMaterial.setMipmapFiltering(filtering);
    markDirty(DirtyMaterial);
}

void QSGDefaultImageNode::setVerticalWrapMode(QSGTexture::WrapMode wrapMode)
{
    if (m_material.verticalWrapMode() == wrapMode)
        return;

    m_material.setVerticalWrapMode(wrapMode);
    m_materialO.setVerticalWrapMode(wrapMode);
    m_smoothMaterial.setVerticalWrapMode(wrapMode);
    markDirty(DirtyMaterial);
}

void QSGDefaultImageNode::setHorizontalWrapMode(QSGTexture::WrapMode wrapMode)
{
    if (m_material.horizontalWrapMode() == wrapMode)
        return;

    m_material.setHorizontalWrapMode(wrapMode);
    m_materialO.setHorizontalWrapMode(wrapMode);
    m_smoothMaterial.setHorizontalWrapMode(wrapMode);
    markDirty(DirtyMaterial);
}


void QSGDefaultImageNode::setTexture(QSGTexture *texture)
{
    if (texture == m_material.texture())
        return;

    m_material.setTexture(texture);
    m_materialO.setTexture(texture);
    m_smoothMaterial.setTexture(texture);
    // Texture cleanup
    if (texture)
        m_material.setFlag(QSGMaterial::Blending, texture->hasAlphaChannel());
    markDirty(DirtyMaterial);

    // Because the texture can be a different part of the atlas, we need to update it...
    m_dirtyGeometry = true;
}

void QSGDefaultImageNode::setAntialiasing(bool antialiasing)
{
    if (antialiasing == m_antialiasing)
        return;
    m_antialiasing = antialiasing;
    if (m_antialiasing) {
        setMaterial(&m_smoothMaterial);
        setOpaqueMaterial(0);
        setGeometry(new QSGGeometry(smoothAttributeSet(), 0));
        setFlag(OwnsGeometry, true);
    } else {
        setMaterial(&m_materialO);
        setOpaqueMaterial(&m_material);
        setGeometry(&m_geometry);
        setFlag(OwnsGeometry, false);
    }
    m_dirtyGeometry = true;
}

void QSGDefaultImageNode::setMirror(bool mirror)
{
    if (mirror == m_mirror)
        return;
    m_mirror = mirror;
    m_dirtyGeometry = true;
}


void QSGDefaultImageNode::update()
{
    if (m_dirtyGeometry)
        updateGeometry();
}

void QSGDefaultImageNode::preprocess()
{
    bool doDirty = false;
    QSGDynamicTexture *t = qobject_cast<QSGDynamicTexture *>(m_material.texture());
    if (t) {
        doDirty = t->updateTexture();
        updateGeometry();
    }
    bool alpha = m_material.flags() & QSGMaterial::Blending;
    if (m_material.texture() && alpha != m_material.texture()->hasAlphaChannel()) {
        m_material.setFlag(QSGMaterial::Blending, !alpha);
        doDirty = true;
    }

    if (doDirty)
        markDirty(DirtyMaterial);
}

inline static bool isPowerOfTwo(int x)
{
    // Assumption: x >= 1
    return x == (x & -x);
}

namespace {
    struct X { float x, tx; };
    struct Y { float y, ty; };
}

static inline void appendQuad(quint16 **indices, quint16 topLeft, quint16 topRight,
                              quint16 bottomLeft, quint16 bottomRight)
{
    *(*indices)++ = topLeft;
    *(*indices)++ = bottomLeft;
    *(*indices)++ = bottomRight;
    *(*indices)++ = bottomRight;
    *(*indices)++ = topRight;
    *(*indices)++ = topLeft;
}

void QSGDefaultImageNode::updateGeometry()
{
    Q_ASSERT(!m_targetRect.isEmpty());
    const QSGTexture *t = m_material.texture();
    if (!t) {
        QSGGeometry *g = geometry();
        g->allocate(4);
        g->setDrawingMode(GL_TRIANGLE_STRIP);
        memset(g->vertexData(), 0, g->sizeOfVertex() * 4);
    } else {
        QRectF sourceRect = t->normalizedTextureSubRect();

        QRectF innerSourceRect(sourceRect.x() + m_innerSourceRect.x() * sourceRect.width(),
                               sourceRect.y() + m_innerSourceRect.y() * sourceRect.height(),
                               m_innerSourceRect.width() * sourceRect.width(),
                               m_innerSourceRect.height() * sourceRect.height());

        bool hasMargins = m_targetRect != m_innerTargetRect;

        int floorLeft = qFloor(m_subSourceRect.left());
        int ceilRight = qCeil(m_subSourceRect.right());
        int floorTop = qFloor(m_subSourceRect.top());
        int ceilBottom = qCeil(m_subSourceRect.bottom());
        int hTiles = ceilRight - floorLeft;
        int vTiles = ceilBottom - floorTop;

        bool hasTiles = hTiles != 1 || vTiles != 1;
        bool fullTexture = innerSourceRect == QRectF(0, 0, 1, 1);

#ifdef QT_OPENGL_ES_2
        QOpenGLContext *ctx = QOpenGLContext::currentContext();
        bool npotSupported = ctx->functions()->hasOpenGLFeature(QOpenGLFunctions::NPOTTextureRepeat);
        QSize size = t->textureSize();
        bool isNpot = !isPowerOfTwo(size.width()) || !isPowerOfTwo(size.height());
        bool wrapSupported = npotSupported || !isNpot;
#else
        bool wrapSupported = true;
#endif

        // An image can be rendered as a single quad if:
        // - There are no margins, and either:
        //   - the image isn't repeated
        //   - the source rectangle fills the entire texture so that texture wrapping can be used,
        //     and NPOT is supported
        if (!hasMargins && (!hasTiles || (fullTexture && wrapSupported))) {
            QRectF sr;
            if (!fullTexture) {
                sr = QRectF(innerSourceRect.x() + (m_subSourceRect.left() - floorLeft) * innerSourceRect.width(),
                            innerSourceRect.y() + (m_subSourceRect.top() - floorTop) * innerSourceRect.height(),
                            m_subSourceRect.width() * innerSourceRect.width(),
                            m_subSourceRect.height() * innerSourceRect.height());
            } else {
                sr = QRectF(m_subSourceRect.left() - floorLeft, m_subSourceRect.top() - floorTop,
                            m_subSourceRect.width(), m_subSourceRect.height());
            }
            if (m_mirror) {
                qreal oldLeft = sr.left();
                sr.setLeft(sr.right());
                sr.setRight(oldLeft);
            }

            if (m_antialiasing) {
                QSGGeometry *g = geometry();
                Q_ASSERT(g != &m_geometry);
                g->allocate(8, 14);
                g->setDrawingMode(GL_TRIANGLE_STRIP);
                SmoothVertex *vertices = reinterpret_cast<SmoothVertex *>(g->vertexData());
                float delta = float(qAbs(m_targetRect.width()) < qAbs(m_targetRect.height())
                        ? m_targetRect.width() : m_targetRect.height()) * 0.5f;
                float sx = float(sr.width() / m_targetRect.width());
                float sy = float(sr.height() / m_targetRect.height());
                for (int d = -1; d <= 1; d += 2) {
                    for (int j = 0; j < 2; ++j) {
                        for (int i = 0; i < 2; ++i, ++vertices) {
                            vertices->x = m_targetRect.x() + i * m_targetRect.width();
                            vertices->y = m_targetRect.y() + j * m_targetRect.height();
                            vertices->u = sr.x() + i * sr.width();
                            vertices->v = sr.y() + j * sr.height();
                            vertices->dx = (i == 0 ? delta : -delta) * d;
                            vertices->dy = (j == 0 ? delta : -delta) * d;
                            vertices->du = (d < 0 ? 0 : vertices->dx * sx);
                            vertices->dv = (d < 0 ? 0 : vertices->dy * sy);
                        }
                    }
                }
                Q_ASSERT(vertices - g->vertexCount() == g->vertexData());
                static const quint16 indices[] = {
                    0, 4, 1, 5, 3, 7, 2, 6, 0, 4,
                    4, 6, 5, 7
                };
                Q_ASSERT(g->sizeOfIndex() * g->indexCount() == sizeof(indices));
                memcpy(g->indexDataAsUShort(), indices, sizeof(indices));
            } else {
                m_geometry.allocate(4);
                m_geometry.setDrawingMode(GL_TRIANGLE_STRIP);
                QSGGeometry::updateTexturedRectGeometry(&m_geometry, m_targetRect, sr);
            }
        } else {
            int hCells = hTiles;
            int vCells = vTiles;
            if (m_innerTargetRect.width() == 0)
                hCells = 0;
            if (m_innerTargetRect.left() != m_targetRect.left())
                ++hCells;
            if (m_innerTargetRect.right() != m_targetRect.right())
                ++hCells;
            if (m_innerTargetRect.height() == 0)
                vCells = 0;
            if (m_innerTargetRect.top() != m_targetRect.top())
                ++vCells;
            if (m_innerTargetRect.bottom() != m_targetRect.bottom())
                ++vCells;
            QVarLengthArray<X, 32> xData(2 * hCells);
            QVarLengthArray<Y, 32> yData(2 * vCells);
            X *xs = xData.data();
            Y *ys = yData.data();

            if (m_innerTargetRect.left() != m_targetRect.left()) {
                xs[0].x = m_targetRect.left();
                xs[0].tx = sourceRect.left();
                xs[1].x = m_innerTargetRect.left();
                xs[1].tx = innerSourceRect.left();
                xs += 2;
            }
            if (m_innerTargetRect.width() != 0) {
                xs[0].x = m_innerTargetRect.left();
                xs[0].tx = innerSourceRect.x() + (m_subSourceRect.left() - floorLeft) * innerSourceRect.width();
                ++xs;
                float b = m_innerTargetRect.width() / m_subSourceRect.width();
                float a = m_innerTargetRect.x() - m_subSourceRect.x() * b;
                for (int i = floorLeft + 1; i <= ceilRight - 1; ++i) {
                    xs[0].x = xs[1].x = a + b * i;
                    xs[0].tx = innerSourceRect.right();
                    xs[1].tx = innerSourceRect.left();
                    xs += 2;
                }
                xs[0].x = m_innerTargetRect.right();
                xs[0].tx = innerSourceRect.x() + (m_subSourceRect.right() - ceilRight + 1) * innerSourceRect.width();
                ++xs;
            }
            if (m_innerTargetRect.right() != m_targetRect.right()) {
                xs[0].x = m_innerTargetRect.right();
                xs[0].tx = innerSourceRect.right();
                xs[1].x = m_targetRect.right();
                xs[1].tx = sourceRect.right();
                xs += 2;
            }
            Q_ASSERT(xs == xData.data() + xData.size());
            if (m_mirror) {
                float leftPlusRight = m_targetRect.left() + m_targetRect.right();
                int count = xData.size();
                xs = xData.data();
                for (int i = 0; i < count >> 1; ++i)
                    qSwap(xs[i], xs[count - 1 - i]);
                for (int i = 0; i < count; ++i)
                    xs[i].x = leftPlusRight - xs[i].x;
            }

            if (m_innerTargetRect.top() != m_targetRect.top()) {
                ys[0].y = m_targetRect.top();
                ys[0].ty = sourceRect.top();
                ys[1].y = m_innerTargetRect.top();
                ys[1].ty = innerSourceRect.top();
                ys += 2;
            }
            if (m_innerTargetRect.height() != 0) {
                ys[0].y = m_innerTargetRect.top();
                ys[0].ty = innerSourceRect.y() + (m_subSourceRect.top() - floorTop) * innerSourceRect.height();
                ++ys;
                float b = m_innerTargetRect.height() / m_subSourceRect.height();
                float a = m_innerTargetRect.y() - m_subSourceRect.y() * b;
                for (int i = floorTop + 1; i <= ceilBottom - 1; ++i) {
                    ys[0].y = ys[1].y = a + b * i;
                    ys[0].ty = innerSourceRect.bottom();
                    ys[1].ty = innerSourceRect.top();
                    ys += 2;
                }
                ys[0].y = m_innerTargetRect.bottom();
                ys[0].ty = innerSourceRect.y() + (m_subSourceRect.bottom() - ceilBottom + 1) * innerSourceRect.height();
                ++ys;
            }
            if (m_innerTargetRect.bottom() != m_targetRect.bottom()) {
                ys[0].y = m_innerTargetRect.bottom();
                ys[0].ty = innerSourceRect.bottom();
                ys[1].y = m_targetRect.bottom();
                ys[1].ty = sourceRect.bottom();
                ys += 2;
            }
            Q_ASSERT(ys == yData.data() + yData.size());

            if (m_antialiasing) {
                QSGGeometry *g = geometry();
                Q_ASSERT(g != &m_geometry);

                g->allocate(hCells * vCells * 4 + (hCells + vCells - 1) * 4,
                            hCells * vCells * 6 + (hCells + vCells) * 12);
                g->setDrawingMode(GL_TRIANGLES);
                SmoothVertex *vertices = reinterpret_cast<SmoothVertex *>(g->vertexData());
                memset(vertices, 0, g->vertexCount() * g->sizeOfVertex());
                quint16 *indices = g->indexDataAsUShort();

                // The deltas are how much the fuzziness can reach into the image.
                // Only the border vertices are moved by the vertex shader, so the fuzziness
                // can't reach further into the image than the closest interior vertices.
                float leftDx = xData.at(1).x - xData.at(0).x;
                float rightDx = xData.at(xData.size() - 1).x - xData.at(xData.size() - 2).x;
                float topDy = yData.at(1).y - yData.at(0).y;
                float bottomDy = yData.at(yData.size() - 1).y - yData.at(yData.size() - 2).y;

                float leftDu = xData.at(1).tx - xData.at(0).tx;
                float rightDu = xData.at(xData.size() - 1).tx - xData.at(xData.size() - 2).tx;
                float topDv = yData.at(1).ty - yData.at(0).ty;
                float bottomDv = yData.at(yData.size() - 1).ty - yData.at(yData.size() - 2).ty;

                if (hCells == 1) {
                    leftDx = rightDx *= 0.5f;
                    leftDu = rightDu *= 0.5f;
                }
                if (vCells == 1) {
                    topDy = bottomDy *= 0.5f;
                    topDv = bottomDv *= 0.5f;
                }

                // This delta is how much the fuzziness can reach out from the image.
                float delta = float(qAbs(m_targetRect.width()) < qAbs(m_targetRect.height())
                                    ? m_targetRect.width() : m_targetRect.height()) * 0.5f;

                quint16 index = 0;
                ys = yData.data();
                for (int j = 0; j < vCells; ++j, ys += 2) {
                    xs = xData.data();
                    bool isTop = j == 0;
                    bool isBottom = j == vCells - 1;
                    for (int i = 0; i < hCells; ++i, xs += 2) {
                        bool isLeft = i == 0;
                        bool isRight = i == hCells - 1;

                        SmoothVertex *v = vertices + index;

                        quint16 topLeft = index;
                        for (int k = (isTop || isLeft ? 2 : 1); k--; ++v, ++index) {
                            v->x = xs[0].x;
                            v->u = xs[0].tx;
                            v->y = ys[0].y;
                            v->v = ys[0].ty;
                        }

                        quint16 topRight = index;
                        for (int k = (isTop || isRight ? 2 : 1); k--; ++v, ++index) {
                            v->x = xs[1].x;
                            v->u = xs[1].tx;
                            v->y = ys[0].y;
                            v->v = ys[0].ty;
                        }

                        quint16 bottomLeft = index;
                        for (int k = (isBottom || isLeft ? 2 : 1); k--; ++v, ++index) {
                            v->x = xs[0].x;
                            v->u = xs[0].tx;
                            v->y = ys[1].y;
                            v->v = ys[1].ty;
                        }

                        quint16 bottomRight = index;
                        for (int k = (isBottom || isRight ? 2 : 1); k--; ++v, ++index) {
                            v->x = xs[1].x;
                            v->u = xs[1].tx;
                            v->y = ys[1].y;
                            v->v = ys[1].ty;
                        }

                        appendQuad(&indices, topLeft, topRight, bottomLeft, bottomRight);

                        if (isTop) {
                            vertices[topLeft].dy = vertices[topRight].dy = topDy;
                            vertices[topLeft].dv = vertices[topRight].dv = topDv;
                            vertices[topLeft + 1].dy = vertices[topRight + 1].dy = -delta;
                            appendQuad(&indices, topLeft + 1, topRight + 1, topLeft, topRight);
                        }

                        if (isBottom) {
                            vertices[bottomLeft].dy = vertices[bottomRight].dy = -bottomDy;
                            vertices[bottomLeft].dv = vertices[bottomRight].dv = -bottomDv;
                            vertices[bottomLeft + 1].dy = vertices[bottomRight + 1].dy = delta;
                            appendQuad(&indices, bottomLeft, bottomRight, bottomLeft + 1, bottomRight + 1);
                        }

                        if (isLeft) {
                            vertices[topLeft].dx = vertices[bottomLeft].dx = leftDx;
                            vertices[topLeft].du = vertices[bottomLeft].du = leftDu;
                            vertices[topLeft + 1].dx = vertices[bottomLeft + 1].dx = -delta;
                            appendQuad(&indices, topLeft + 1, topLeft, bottomLeft + 1, bottomLeft);
                        }

                        if (isRight) {
                            vertices[topRight].dx = vertices[bottomRight].dx = -rightDx;
                            vertices[topRight].du = vertices[bottomRight].du = -rightDu;
                            vertices[topRight + 1].dx = vertices[bottomRight + 1].dx = delta;
                            appendQuad(&indices, topRight, topRight + 1, bottomRight, bottomRight + 1);
                        }
                    }
                }

                Q_ASSERT(index == g->vertexCount());
                Q_ASSERT(indices - g->indexCount() == g->indexData());
            } else {
                m_geometry.allocate(hCells * vCells * 4, hCells * vCells * 6);
                m_geometry.setDrawingMode(GL_TRIANGLES);
                QSGGeometry::TexturedPoint2D *vertices = m_geometry.vertexDataAsTexturedPoint2D();
                ys = yData.data();
                for (int j = 0; j < vCells; ++j, ys += 2) {
                    xs = xData.data();
                    for (int i = 0; i < hCells; ++i, xs += 2) {
                        vertices[0].x = vertices[2].x = xs[0].x;
                        vertices[0].tx = vertices[2].tx = xs[0].tx;
                        vertices[1].x = vertices[3].x = xs[1].x;
                        vertices[1].tx = vertices[3].tx = xs[1].tx;

                        vertices[0].y = vertices[1].y = ys[0].y;
                        vertices[0].ty = vertices[1].ty = ys[0].ty;
                        vertices[2].y = vertices[3].y = ys[1].y;
                        vertices[2].ty = vertices[3].ty = ys[1].ty;

                        vertices += 4;
                    }
                }

                quint16 *indices = m_geometry.indexDataAsUShort();
                for (int i = 0; i < 4 * vCells * hCells; i += 4)
                    appendQuad(&indices, i, i + 1, i + 2, i + 3);
            }
        }
    }
    markDirty(DirtyGeometry);
    m_dirtyGeometry = false;
}

QT_END_NAMESPACE