summaryrefslogtreecommitdiffstats
path: root/src/adaptationlayers/qsgthreadedtexturemanager.cpp
blob: 6e5dc9111b9f91b710e0e494b4cc38af0bdbe640 (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
#include "qsgthreadedtexturemanager.h"
#include "qsgtexturemanager_p.h"

#include <QThread>
#include <QMutex>
#include <QWaitCondition>

class QSGThreadedTexture : public QSGTexture
{
    Q_OBJECT
public:
    QSGThreadedTexture(QSGThreadedTextureManager *m)
        : manager(m)
    {
    }
    ~QSGThreadedTexture();

    QSGThreadedTextureManager *manager;
    QImage image;
};


class QSGThreadedTextureManagerThread : public QThread
{
public:
    QSGThreadedTextureManagerThread(QSGThreadedTextureManager *m)
        : manager(m)
        , currentlyUploading(0)
    {
        manager->createThreadContext();
        start();
    }

    void run() {

        manager->makeThreadContextCurrent();

        while (true) {

            mutex.lock();
            currentlyUploading = 0;
            condition.wakeOne();

            if (requests.isEmpty()) {
                condition.wait(&mutex);
            }

            if (requests.isEmpty()) {
                mutex.unlock();
                continue;
            }

            currentlyUploading = requests.takeFirst();
            mutex.unlock();

            manager->uploadInThread(currentlyUploading->image, currentlyUploading);
        }
    }

    QSGThreadedTextureManager *manager;
    QSGThreadedTexture *currentlyUploading;

    QWaitCondition condition;
    QMutex mutex;

    QList<QSGThreadedTexture *> requests;
};


class QSGThreadedTextureManagerPrivate : public QSGTextureManagerPrivate
{
public:
    QSGThreadedTextureManagerThread *thread;

    QGLWidget *widget;
    QGLContext *context;
};


QSGThreadedTexture::~QSGThreadedTexture()
{
    QSGThreadedTextureManagerPrivate *d = manager->d_func();
    d->thread->mutex.lock();
    d->thread->requests.removeOne(this);
    while (d->thread->currentlyUploading == this)
        d->thread->condition.wait(&d->thread->mutex);
    d->thread->mutex.unlock();

    d->removeTextureFromCache(this);
}


QSGThreadedTextureManager::QSGThreadedTextureManager()
    : QSGTextureManager(*(new QSGThreadedTextureManagerPrivate))
{
    Q_D(QSGThreadedTextureManager);
    d->thread = 0;
    d->widget = 0;
    d->context = 0;
}

QSGTextureRef QSGThreadedTextureManager::upload(const QImage &image)
{
    Q_D(QSGThreadedTextureManager);

    QSGTextureCacheKey key = { image.cacheKey() };
    QSGTexture *texture = d->cache.value(key);

    if (texture) {

        QSGThreadedTexture *ttex = qobject_cast<QSGThreadedTexture *>(texture);
        if ((ttex && ttex->status() == QSGTexture::Ready) || !ttex) {
            // Already fully uploaded... Just return
            return QSGTextureRef(texture);

        } else {

            // Lock and wait for the texture to uploaded...
            d->thread->mutex.lock();
            while (ttex->status() == QSGTexture::Loading) {
                d->thread->condition.wait(&d->thread->mutex);
            }
            d->thread->mutex.unlock();

            return QSGTextureRef(ttex);
        }
    }

    return d->upload(image, 0, 0);
}

QSGTextureRef QSGThreadedTextureManager::requestUpload(const QImage &image, const QObject *listener, const char *slot)
{
    Q_D(QSGThreadedTextureManager);

    QSGTextureCacheKey key = { image.cacheKey() };
    QSGTexture *texture = d->cache.value(key);
    if (texture)
        return QSGTextureRef(texture);

    QSGThreadedTexture *ttex = new QSGThreadedTexture(this);
    ttex->image = image;
    if (listener && slot)
        connect(ttex, SIGNAL(statusChanged(int)), listener, slot);

    ttex->setStatus(QSGTexture::Loading);

    d->cache.insert(key, ttex);

    if (!d->thread) {
        d->thread = new QSGThreadedTextureManagerThread(this);
    }

    d->thread->mutex.lock();
    d->thread->requests << ttex;
    d->thread->condition.wakeOne();
    d->thread->mutex.unlock();

    return QSGTextureRef(ttex);
}

/*!
   Called by the rendering thread to create a new context that can be used in the
   upload thread.
 */
void QSGThreadedTextureManager::createThreadContext()
{
    Q_D(QSGThreadedTextureManager);

    QGLContext *ctx = const_cast<QGLContext *>(QGLContext::currentContext());

    // Getting the share widget from the current context is rather nasty and
    // will not work for lighthouse based
    Q_ASSERT(ctx->device() && ctx->device()->devType() == QInternal::Widget);
    QGLWidget *share = static_cast<QGLWidget *>(ctx->device());

    d->widget = new QGLWidget(0, share);
    d->widget->resize(8, 8);

    d->context = const_cast<QGLContext *>(d->widget->context());
    if (!d->context)
        qFatal("QSGThreadedTextureManager: failed to create thread context...");

    d->widget->doneCurrent();

    ctx->makeCurrent();
}


/*!
    Reimplement this function to make the threaded context current.

    This function is called from the background thread. The default
    implementation makes the context created in createThreadContext
    current.
 */
void QSGThreadedTextureManager::makeThreadContextCurrent()
{
    Q_D(QSGThreadedTextureManager);
    d->context->makeCurrent();
}


/*!
    Reimplement this function to upload images in the background thread.

    This function is called from the background thread. The default
    implementation does this using a single glTexImage2D call.
 */

void QSGThreadedTextureManager::uploadInThread(const QImage &image, QSGTexture *texture)
{
    GLuint id;
    glGenTextures(1, &id);
    glBindTexture(GL_TEXTURE_2D, id);

    QImage copy = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
    swizzleBGRAToRGBA(&copy);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image.constBits());

    bool fail = glGetError() != GL_NO_ERROR;

    glBindTexture(GL_TEXTURE_2D, 0);

    if (fail) {
        texture->setStatus(QSGTexture::Null);
        glDeleteTextures(1, &id);
    } else {
        texture->setTextureId(id);
        texture->setTextureSize(image.size());
        texture->setAlphaChannel(image.hasAlphaChannel());
        texture->setStatus(QSGTexture::Ready);
    }
}

#include "qsgthreadedtexturemanager.moc"