aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/scenegraph/qsgshareddistancefieldglyphcache.cpp
blob: 799d3544008a4eb60c9df1c3bceba951757916b8 (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
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** 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, Nokia gives you certain additional
** rights. These rights are described in the Nokia 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.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#define EGL_EGLEXT_PROTOTYPES
#define GL_GLEXT_PROTOTYPES
#if defined(QSGSHAREDDISTANCEFIELDGLYPHCACHE_DEBUG)
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#endif

#include "qsgshareddistancefieldglyphcache_p.h"

#include <QtCore/qhash.h>
#include <QtCore/qthread.h>
#include <QtGui/qplatformsharedgraphicscache_qpa.h>

#include <QtQuick/qquickcanvas.h>

#include <QtOpenGL/qglframebufferobject.h>

// #define QSGSHAREDDISTANCEFIELDGLYPHCACHE_DEBUG

Q_DECLARE_METATYPE(QVector<quint32>)
Q_DECLARE_METATYPE(QVector<QImage>)

QT_BEGIN_NAMESPACE

QSGSharedDistanceFieldGlyphCache::QSGSharedDistanceFieldGlyphCache(const QByteArray &cacheId,
                                                                   QPlatformSharedGraphicsCache *sharedGraphicsCache,
                                                                   QSGDistanceFieldGlyphCacheManager *man,
                                                                   QOpenGLContext *c,
                                                                   const QRawFont &font)
    : QSGDistanceFieldGlyphCache(man, c, font)
    , m_cacheId(cacheId)
    , m_sharedGraphicsCache(sharedGraphicsCache)
{
#if defined(QSGSHAREDDISTANCEFIELDGLYPHCACHE_DEBUG)
    qDebug("QSGSharedDistanceFieldGlyphCache with id %s created in thread %p",
           cacheId.constData(), QThread::currentThreadId());
#endif

    Q_ASSERT(sizeof(glyph_t) == sizeof(quint32));
    Q_ASSERT(sharedGraphicsCache != 0);

    qRegisterMetaType<QVector<quint32> >();
    qRegisterMetaType<QVector<QImage> >();

    connect(sharedGraphicsCache, SIGNAL(itemsMissing(QByteArray,QVector<quint32>)),
            this, SLOT(reportItemsMissing(QByteArray,QVector<quint32>)),
            Qt::DirectConnection);
    connect(sharedGraphicsCache, SIGNAL(itemsAvailable(QByteArray,void*,QSize,QVector<quint32>,QVector<QPoint>)),
            this, SLOT(reportItemsAvailable(QByteArray,void*,QSize,QVector<quint32>,QVector<QPoint>)),
            Qt::DirectConnection);
    connect(sharedGraphicsCache, SIGNAL(itemsUpdated(QByteArray,void*,QSize,QVector<quint32>,QVector<QPoint>)),
            this, SLOT(reportItemsAvailable(QByteArray,void*,QSize,QVector<quint32>,QVector<QPoint>)),
            Qt::DirectConnection);
    connect(sharedGraphicsCache, SIGNAL(itemsInvalidated(QByteArray,QVector<quint32>)),
            this, SLOT(reportItemsInvalidated(QByteArray,QVector<quint32>)),
            Qt::DirectConnection);
}

QSGSharedDistanceFieldGlyphCache::~QSGSharedDistanceFieldGlyphCache()
{
    {
        QHash<glyph_t, void *>::const_iterator it = m_bufferForGlyph.constBegin();
        while (it != m_bufferForGlyph.constEnd()) {
            m_sharedGraphicsCache->dereferenceBuffer(it.value());
            ++it;
        }
    }

    {
        QHash<quint32, PendingGlyph>::const_iterator it = m_pendingReadyGlyphs.constBegin();
        while (it != m_pendingReadyGlyphs.constEnd()) {
            m_sharedGraphicsCache->dereferenceBuffer(it.value().buffer);
            ++it;
        }
    }
}

void QSGSharedDistanceFieldGlyphCache::requestGlyphs(const QSet<glyph_t> &glyphs)
{
    QMutexLocker locker(&m_pendingGlyphsMutex);

#if defined(QSGSHAREDDISTANCEFIELDGLYPHCACHE_DEBUG)
    qDebug("QSGSharedDistanceFieldGlyphCache::requestGlyphs() called for %s (%d glyphs)",
           m_cacheId.constData(), glyphs.size());
#endif

    m_requestedGlyphsThatHaveNotBeenReturned.unite(glyphs);

    QVector<quint32> glyphsVector;
    glyphsVector.reserve(glyphs.size());

    QSet<glyph_t>::const_iterator it;
    for (it = glyphs.constBegin(); it != glyphs.constEnd(); ++it) {
        Q_ASSERT(!m_bufferForGlyph.contains(*it));
        glyphsVector.append(*it);
    }

    // Invoke method on queued connection to make sure it's called asynchronously on the
    // correct thread (requestGlyphs() is called from the rendering thread.)
    QMetaObject::invokeMethod(m_sharedGraphicsCache, "requestItems", Qt::QueuedConnection,
                              Q_ARG(QByteArray, m_cacheId),
                              Q_ARG(QVector<quint32>, glyphsVector));
}

void QSGSharedDistanceFieldGlyphCache::waitForGlyphs()
{
    {
        QMutexLocker locker(&m_pendingGlyphsMutex);
        while (!m_requestedGlyphsThatHaveNotBeenReturned.isEmpty())
            m_pendingGlyphsCondition.wait(&m_pendingGlyphsMutex);
    }
}

void QSGSharedDistanceFieldGlyphCache::storeGlyphs(const QHash<glyph_t, QImage> &glyphs)
{
    {
        QMutexLocker locker(&m_pendingGlyphsMutex);
#if defined(QSGSHAREDDISTANCEFIELDGLYPHCACHE_DEBUG)
        qDebug("QSGSharedDistanceFieldGlyphCache::storeGlyphs() called for %s (%d glyphs)",
               m_cacheId.constData(), glyphs.size());
#endif

        int glyphCount = glyphs.size();
        QVector<quint32> glyphIds(glyphCount);
        QVector<QImage> images(glyphCount);
        QHash<glyph_t, QImage>::const_iterator it = glyphs.constBegin();
        int i=0;
        while (it != glyphs.constEnd()) {
            m_requestedGlyphsThatHaveNotBeenReturned.insert(it.key());
            glyphIds[i] = it.key();
            images[i] = it.value();

            ++it; ++i;
        }

        QMetaObject::invokeMethod(m_sharedGraphicsCache, "insertItems", Qt::QueuedConnection,
                                  Q_ARG(QByteArray, m_cacheId),
                                  Q_ARG(QVector<quint32>, glyphIds),
                                  Q_ARG(QVector<QImage>, images));
    }

    processPendingGlyphs();
}

void QSGSharedDistanceFieldGlyphCache::referenceGlyphs(const QSet<glyph_t> &glyphs)
{
    Q_UNUSED(glyphs);

    // Intentionally empty. Not required in this implementation, since the glyphs are reference
    // counted outside and releaseGlyphs() will only be called when there are no more references.
}

void QSGSharedDistanceFieldGlyphCache::releaseGlyphs(const QSet<glyph_t> &glyphs)
{
#if defined(QSGSHAREDDISTANCEFIELDGLYPHCACHE_DEBUG)
    qDebug("QSGSharedDistanceFieldGlyphCache::releaseGlyphs() called for %s (%d glyphs)",
           m_cacheId.constData(), glyphs.size());
#endif

    QVector<quint32> glyphsVector;
    glyphsVector.reserve(glyphs.size());

    QSet<glyph_t>::const_iterator glyphsIt;
    for (glyphsIt = glyphs.constBegin(); glyphsIt != glyphs.constEnd(); ++glyphsIt) {
        QHash<glyph_t, void *>::iterator bufferIt = m_bufferForGlyph.find(*glyphsIt);
        if (bufferIt != m_bufferForGlyph.end()) {
            void *buffer = bufferIt.value();
            removeGlyph(*glyphsIt);
            m_bufferForGlyph.erase(bufferIt);
            Q_ASSERT(!m_bufferForGlyph.contains(*glyphsIt));

            if (!m_sharedGraphicsCache->dereferenceBuffer(buffer)) {
#if !defined(QT_NO_DEBUG)
                bufferIt = m_bufferForGlyph.begin();
                while (bufferIt != m_bufferForGlyph.end()) {
                    Q_ASSERT(bufferIt.value() != buffer);
                    ++bufferIt;
                }
#endif
            }
        }

        glyphsVector.append(*glyphsIt);
    }

    QMetaObject::invokeMethod(m_sharedGraphicsCache, "releaseItems", Qt::QueuedConnection,
                              Q_ARG(QByteArray, m_cacheId),
                              Q_ARG(QVector<quint32>, glyphsVector));
}

void QSGSharedDistanceFieldGlyphCache::registerOwnerElement(QQuickItem *ownerElement)
{
    bool ok = connect(this, SIGNAL(glyphsPending()), ownerElement, SLOT(triggerPreprocess()));
    Q_ASSERT_X(ok, Q_FUNC_INFO, "QML element that owns a glyph node must have triggerPreprocess() slot");
    Q_UNUSED(ok);
}

void QSGSharedDistanceFieldGlyphCache::unregisterOwnerElement(QQuickItem *ownerElement)
{
    disconnect(this, SIGNAL(glyphsPending()), ownerElement, SLOT(triggerPreprocess()));
}

#if defined(QSGSHAREDDISTANCEFIELDGLYPHCACHE_DEBUG_)
#  include <QtOpenGL/private/qglextensions_p.h>

void QSGSharedDistanceFieldGlyphCache::saveTexture(GLuint textureId, int width, int height)
{
    GLuint fboId;
    glGenFramebuffers(1, &fboId);

    GLuint tmpTexture = 0;
    glGenTextures(1, &tmpTexture);
    glBindTexture(GL_TEXTURE_2D, tmpTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glBindTexture(GL_TEXTURE_2D, 0);

    glBindFramebuffer(GL_FRAMEBUFFER_EXT, fboId);
    glFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D,
                           tmpTexture, 0);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, textureId);

    glDisable(GL_STENCIL_TEST);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_SCISSOR_TEST);
    glDisable(GL_BLEND);

    GLfloat textureCoordinateArray[8];
    textureCoordinateArray[0] = 0.0f;
    textureCoordinateArray[1] = 0.0f;
    textureCoordinateArray[2] = 1.0f;
    textureCoordinateArray[3] = 0.0f;
    textureCoordinateArray[4] = 1.0f;
    textureCoordinateArray[5] = 1.0f;
    textureCoordinateArray[6] = 0.0f;
    textureCoordinateArray[7] = 1.0f;

    GLfloat vertexCoordinateArray[8];
    vertexCoordinateArray[0] = -1.0f;
    vertexCoordinateArray[1] = -1.0f;
    vertexCoordinateArray[2] =  1.0f;
    vertexCoordinateArray[3] = -1.0f;
    vertexCoordinateArray[4] =  1.0f;
    vertexCoordinateArray[5] =  1.0f;
    vertexCoordinateArray[6] = -1.0f;
    vertexCoordinateArray[7] =  1.0f;

    glViewport(0, 0, width, height);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertexCoordinateArray);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, textureCoordinateArray);

    {
        static const char *vertexShaderSource =
                "attribute highp   vec4      vertexCoordsArray; \n"
                "attribute highp   vec2      textureCoordArray; \n"
                "varying   highp   vec2      textureCoords;     \n"
                "void main(void) \n"
                "{ \n"
                "    gl_Position = vertexCoordsArray;   \n"
                "    textureCoords = textureCoordArray; \n"
                "} \n";

        static const char *fragmentShaderSource =
                "varying   highp   vec2      textureCoords; \n"
                "uniform   sampler2D         texture;       \n"
                "void main() \n"
                "{ \n"
                "    gl_FragColor = texture2D(texture, textureCoords); \n"
                "} \n";

        GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
        GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

        if (vertexShader == 0 || fragmentShader == 0) {
            GLenum error = glGetError();
            qWarning("SharedGraphicsCacheServer::setupShaderPrograms: Failed to create shaders. (GL error: %x)",
                     error);
            return;
        }

        glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
        glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
        glCompileShader(vertexShader);

        GLint len = 1;
        glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &len);

        char infoLog[2048];
        glGetShaderInfoLog(vertexShader, 2048, NULL, infoLog);
        if (qstrlen(infoLog) > 0) {
            qWarning("SharedGraphicsCacheServer::setupShaderPrograms, problems compiling vertex shader:\n %s",
                     infoLog);
            //return;
        }

        glCompileShader(fragmentShader);
        glGetShaderInfoLog(fragmentShader, 2048, NULL, infoLog);
        if (qstrlen(infoLog) > 0) {
            qWarning("SharedGraphicsCacheServer::setupShaderPrograms, problems compiling fragent shader:\n %s",
                     infoLog);
            //return;
        }

        GLuint shaderProgram = glCreateProgram();
        glAttachShader(shaderProgram, vertexShader);
        glAttachShader(shaderProgram, fragmentShader);

        glBindAttribLocation(shaderProgram, 0, "vertexCoordsArray");
        glBindAttribLocation(shaderProgram, 1, "textureCoordArray");

        glLinkProgram(shaderProgram);
        glGetProgramInfoLog(shaderProgram, 2048, NULL, infoLog);
        if (qstrlen(infoLog) > 0) {
            qWarning("SharedGraphicsCacheServer::setupShaderPrograms, problems linking shaders:\n %s",
                     infoLog);
            //return;
        }

        glUseProgram(shaderProgram);
        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);

        int textureUniformLocation = glGetUniformLocation(shaderProgram, "texture");
        glUniform1i(textureUniformLocation, 0);
    }

    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

    {
        GLenum error = glGetError();
        if (error != GL_NO_ERROR) {
            qWarning("SharedGraphicsCacheServer::readBackBuffer: glDrawArrays reported error 0x%x",
                     error);
        }
    }

    uchar *data = new uchar[width * height * 4];

    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);

    QImage image(width, height, QImage::Format_ARGB32);
    quint32 *dest = reinterpret_cast<quint32 *>(image.bits());
    for (int i=0; i<width*height; ++i)
        dest[i] = qRgba(0xff, 0xff, 0xff, data[i]);

    QByteArray fileName = m_cacheId + " " + QByteArray::number(textureId);
    fileName = fileName.replace('/', '_').replace(' ', '_') + ".png";
    image.save(QString::fromLocal8Bit(fileName));

    {
        GLenum error = glGetError();
        if (error != GL_NO_ERROR) {
            qWarning("SharedGraphicsCacheServer::readBackBuffer: glReadPixels reported error 0x%x",
                     error);
        }
    }

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);

    glDeleteFramebuffers(1, &fboId);
    glDeleteTextures(1, &tmpTexture);

    delete[] data;
}
#endif

namespace {
    struct TextureContent {
        QSize size;
        QVector<glyph_t> glyphs;
    };
}

void QSGSharedDistanceFieldGlyphCache::processPendingGlyphs()
{
    Q_ASSERT(QThread::currentThread() == thread());

    waitForGlyphs();

    {
        QMutexLocker locker(&m_pendingGlyphsMutex);
        if (m_pendingMissingGlyphs.isEmpty()
            && m_pendingReadyGlyphs.isEmpty()
            && m_pendingInvalidatedGlyphs.isEmpty()) {
            return;
        }

        {
            QVector<glyph_t> pendingMissingGlyphs;
            pendingMissingGlyphs.reserve(m_pendingMissingGlyphs.size());

            QSet<glyph_t>::const_iterator it = m_pendingMissingGlyphs.constBegin();
            while (it != m_pendingMissingGlyphs.constEnd()) {
                pendingMissingGlyphs.append(*it);
                ++it;
            }

            markGlyphsToRender(pendingMissingGlyphs);
        }

        {
            QVector<glyph_t> filteredPendingInvalidatedGlyphs;
            filteredPendingInvalidatedGlyphs.reserve(m_pendingInvalidatedGlyphs.size());

            QSet<glyph_t>::const_iterator it = m_pendingInvalidatedGlyphs.constBegin();
            while (it != m_pendingInvalidatedGlyphs.constEnd()) {
                bool rerequestGlyph = false;

                // The glyph was invalidated right after being posted as ready, we throw away
                // the ready glyph and rerequest it to be certain
                QHash<quint32, PendingGlyph>::iterator pendingGlyphIt = m_pendingReadyGlyphs.find(*it);
                if (pendingGlyphIt != m_pendingReadyGlyphs.end()) {
                    m_sharedGraphicsCache->dereferenceBuffer(pendingGlyphIt.value().buffer);
                    pendingGlyphIt = m_pendingReadyGlyphs.erase(pendingGlyphIt);
                    rerequestGlyph = true;
                }

                void *bufferId = m_bufferForGlyph.value(*it, 0);
                if (bufferId != 0) {
                    m_sharedGraphicsCache->dereferenceBuffer(bufferId);
                    m_bufferForGlyph.remove(*it);
                    rerequestGlyph = true;
                }

                if (rerequestGlyph)
                    filteredPendingInvalidatedGlyphs.append(*it);

                ++it;
            }

            // If this cache is still using the glyphs, reset the texture held by them, and mark them
            // to be rendered again since they are still needed.
            if (!filteredPendingInvalidatedGlyphs.isEmpty()) {
                setGlyphsTexture(filteredPendingInvalidatedGlyphs, Texture());
                markGlyphsToRender(filteredPendingInvalidatedGlyphs);
            }
        }

        {
            QList<GlyphPosition> glyphPositions;

            QHash<void *, TextureContent> textureContentForBuffer;
            {
                QHash<quint32, PendingGlyph>::iterator it = m_pendingReadyGlyphs.begin();
                while (it != m_pendingReadyGlyphs.end()) {
                    void *currentGlyphBuffer = m_bufferForGlyph.value(it.key(), 0);
                    if (currentGlyphBuffer != 0) {
                        if (!m_sharedGraphicsCache->dereferenceBuffer(currentGlyphBuffer)) {
                            Q_ASSERT(!textureContentForBuffer.contains(currentGlyphBuffer));
                        }
                    }

                    PendingGlyph &pendingGlyph  = it.value();

                    // We don't ref or deref the buffer here, since it was already referenced when
                    // added to the pending ready glyphs
                    m_bufferForGlyph[it.key()] = pendingGlyph.buffer;

                    textureContentForBuffer[pendingGlyph.buffer].size = pendingGlyph.bufferSize;
                    textureContentForBuffer[pendingGlyph.buffer].glyphs.append(it.key());

                    GlyphPosition glyphPosition;
                    glyphPosition.glyph = it.key();
                    glyphPosition.position = pendingGlyph.position;

                    glyphPositions.append(glyphPosition);

                    ++it;
                }
            }

            setGlyphsPosition(glyphPositions);

            {
                QHash<void *, TextureContent>::const_iterator it = textureContentForBuffer.constBegin();
                while (it != textureContentForBuffer.constEnd()) {
                    Texture texture;
                    texture.textureId = m_sharedGraphicsCache->textureIdForBuffer(it.key());
                    texture.size = it.value().size;

#if defined(QSGSHAREDDISTANCEFIELDGLYPHCACHE_DEBUG_)
                    saveTexture(texture.textureId, texture.size.width(), texture.size.height());
#endif
                    setGlyphsTexture(it.value().glyphs, texture);

                    ++it;
                }
            }
        }

        m_pendingMissingGlyphs.clear();
        m_pendingInvalidatedGlyphs.clear();
        m_pendingReadyGlyphs.clear();
    }
}

void QSGSharedDistanceFieldGlyphCache::reportItemsAvailable(const QByteArray &cacheId,
                                                        void *bufferId, const QSize &bufferSize,
                                                        const QVector<quint32> &itemIds,
                                                        const QVector<QPoint> &positions)
{
    {
        QMutexLocker locker(&m_pendingGlyphsMutex);
        if (m_cacheId != cacheId)
            return;

        Q_ASSERT(itemIds.size() == positions.size());

#if defined(QSGSHAREDDISTANCEFIELDGLYPHCACHE_DEBUG)
        qDebug("QSGSharedDistanceFieldGlyphCache::reportItemsAvailable() called for %s (%d glyphs, bufferSize: %dx%d)",
               cacheId.constData(), itemIds.size(), bufferSize.width(), bufferSize.height());
#endif

        for (int i=0; i<itemIds.size(); ++i) {
            PendingGlyph &pendingGlyph = m_pendingReadyGlyphs[itemIds.at(i)];
            void *oldBuffer = pendingGlyph.buffer;
            Q_ASSERT(bufferSize.height() >= pendingGlyph.bufferSize.height());

            pendingGlyph.buffer = bufferId;
            pendingGlyph.position = positions.at(i);
            pendingGlyph.bufferSize = bufferSize;

            m_sharedGraphicsCache->referenceBuffer(bufferId);
            if (oldBuffer != 0)
                m_sharedGraphicsCache->dereferenceBuffer(oldBuffer);

            m_requestedGlyphsThatHaveNotBeenReturned.remove(itemIds.at(i));
        }
    }

    m_pendingGlyphsCondition.wakeAll();
    emit glyphsPending();
}

void QSGSharedDistanceFieldGlyphCache::reportItemsInvalidated(const QByteArray &cacheId,
                                                              const QVector<quint32> &itemIds)
{
    {
        QMutexLocker locker(&m_pendingGlyphsMutex);
        if (m_cacheId != cacheId)
            return;

        for (int i=0; i<itemIds.size(); ++i)
            m_pendingInvalidatedGlyphs.insert(itemIds.at(i));
    }

    emit glyphsPending();
}


void QSGSharedDistanceFieldGlyphCache::reportItemsMissing(const QByteArray &cacheId,
                                                          const QVector<quint32> &itemIds)
{
    {
        QMutexLocker locker(&m_pendingGlyphsMutex);
        if (m_cacheId != cacheId)
            return;

#if defined(QSGSHAREDDISTANCEFIELDGLYPHCACHE_DEBUG)
        qDebug("QSGSharedDistanceFieldGlyphCache::reportItemsMissing() called for %s (%d glyphs)",
               cacheId.constData(), itemIds.size());
#endif

        for (int i=0; i<itemIds.size(); ++i) {
            m_pendingMissingGlyphs.insert(itemIds.at(i));
            m_requestedGlyphsThatHaveNotBeenReturned.remove(itemIds.at(i));
        }
    }

    m_pendingGlyphsCondition.wakeAll();
    emit glyphsPending();
}

QT_END_NAMESPACE