summaryrefslogtreecommitdiffstats
path: root/src/adaptationlayers/qsgeglfsthreadedtexturemanager.cpp
blob: b868979f68123f533e9f48c89b01f9feaa824279 (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
#include "qsgeglfsthreadedtexturemanager.h"

#include <QThread>
#include <qdatetime.h>

#include <EGL/egl.h>
#include <GLES2/gl2.h>


class QSGEglFSThreadedTextureManagerPrivate
{
public:
    EGLDisplay display;
    EGLSurface surface;
    EGLContext context;

    bool uploadsScanlines;
};


QSGEglFSThreadedTextureManager::QSGEglFSThreadedTextureManager()
    : d(new QSGEglFSThreadedTextureManagerPrivate)
{
    d->uploadsScanlines = false;
}


void QSGEglFSThreadedTextureManager::setUploadsScanlines(bool b)
{
    d->uploadsScanlines = b;
}


bool QSGEglFSThreadedTextureManager::uploadsScanlines() const
{
    return d->uploadsScanlines;
}


void QSGEglFSThreadedTextureManager::initializeThreadContext()
{
    d->display = eglGetCurrentDisplay();

    EGLint attribs[] = {
        EGL_WIDTH, 8,
        EGL_HEIGHT, 8,

        EGL_NONE
    };

    EGLConfig configs[256];
    EGLint count;

    d->surface = 0;
    EGLConfig *config = 0;
    if (eglGetConfigs(d->display, configs, 256, &count)) {
        for (int i=0; i<count; ++i) {
            int type;
            eglGetConfigAttrib(d->display, configs[i], EGL_RENDERABLE_TYPE, &type);
            if (!(type & EGL_OPENGL_ES2_BIT))
                continue;

            int r, g, b, a;
            eglGetConfigAttrib(d->display, configs[i], EGL_RED_SIZE, &r);
            eglGetConfigAttrib(d->display, configs[i], EGL_GREEN_SIZE, &g);
            eglGetConfigAttrib(d->display, configs[i], EGL_BLUE_SIZE, &b);
            eglGetConfigAttrib(d->display, configs[i], EGL_ALPHA_SIZE, &a);

            if ((r & g & b & a) == 8) {
                d->surface = eglCreatePbufferSurface(d->display, configs[i], attribs);
                config = configs + i;
                break;
            }
        }
    }
    d->context = eglCreateContext(d->display, *config, eglGetCurrentContext(), 0);
}


void QSGEglFSThreadedTextureManager::makeThreadContextCurrent()
{
    eglMakeCurrent(d->display, d->surface, d->surface, d->context);
}

class ThreadAccess  : public QThread
{
public:
    static void doSleep(int us) {
        usleep(us);
    }
};


static inline void qgl_byteSwapImage(QImage &img)
{
    const int width = img.width();
    const int height = img.height();

    for (int i = 0; i < height; ++i) {
        uint *p = (uint *) img.scanLine(i);
        for (int x = 0; x < width; ++x)
            p[x] = ((p[x] << 16) & 0xff0000) | ((p[x] >> 16) & 0xff) | (p[x] & 0xff00ff00);
    }
}

#define DO_TIMING

void QSGEglFSThreadedTextureManager::uploadInThread(TextureReference *texture, const QImage &im, UploadHints hints)
{
    while (glGetError() != GL_NO_ERROR) printf("bust...\n");

#ifdef DO_TIMING
    QTime time;
    time.start();
#endif

    QImage image = im;
    qgl_byteSwapImage(image);

#ifdef DO_TIMING
    int time_byteswap = time.elapsed();
#endif

    int w = image.width();
    int h = image.height();
    int bpl = image.bytesPerLine();
    const uchar *bits = image.constBits();

    // This should not be neccesary...
    if (d->uploadsScanlines)
        makeThreadContextCurrent();

    GLuint id;
    glGenTextures(1, &id);
    glBindTexture(GL_TEXTURE_2D, id);

    if (d->uploadsScanlines) {
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
        glBindTexture(GL_TEXTURE_2D, id); // Should also not be required, but it fails without... driver bug, for sure..
        for (int y=0; y<h; ++y) {
            glTexSubImage2D(GL_TEXTURE_2D, 0, 0, y, w, 1, GL_RGBA, GL_UNSIGNED_BYTE, y * bpl + bits);
            if (glGetError() != GL_NO_ERROR) {
                qWarning("ERROR: failed to upload chunk...\n");
                break;
            }
            ThreadAccess::doSleep(2);
        }
    } else {
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, bits);
    }

    glBindTexture(GL_TEXTURE_2D, 0);

#ifdef DO_TIMING
    int time_upload = time.elapsed();
    printf("EGLFS Threaded upload: byteSwap: %d ms, upload: %d ms\n",
           time_byteswap,
           time_upload);
#endif


    texture->setOwnsTexture(true);
    texture->setTextureId(id);
    texture->setTextureSize(image.size());
    texture->setMipmaps(hints & TextureManager::GenerateMipmapUploadHint);
    texture->setStatus(TextureReference::Uploaded);
}