summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/wayland/qwaylandglcontext.cpp
blob: 24abcf80bf7b0c245d9e5c02661c3b8ced9d0a56 (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
#include "qwaylandglcontext.h"

#include "qwaylanddisplay.h"
#include "qwaylandwindow.h"
#include "qwaylandwindowsurface.h"
#include "qfontconfigdatabase.h"

#include <QImageReader>
#include <QWindowSystemInterface>
#include <QPlatformCursor>
#include <QPaintEngine>

#include <QtGui/QPlatformGLContext>
#include <QtGui/QPlatformWindowFormat>

#include <QtGui/private/qpixmap_raster_p.h>
#include <QtGui/QPlatformWindow>

#include <private/qwindowsurface_gl_p.h>
#include <private/qpixmapdata_gl_p.h>
#include <private/qpaintengineex_opengl2_p.h>

#include <unistd.h>
#include <fcntl.h>

extern "C" {
#include <xf86drm.h>
}

QWaylandGLContext::QWaylandGLContext(QWaylandDisplay *wd, QWaylandWindow *window, const QPlatformWindowFormat &format)
    : QPlatformGLContext()
    , mFormat(format)
    , mDisplay(wd)
    , mWindow(window)
    , parentFbo(0)
    , parentRbo(0)
{
}

QWaylandGLContext::~QWaylandGLContext()
{
    glDeleteRenderbuffers(1, &parentRbo);
    glDeleteFramebuffers(1, &parentFbo);
}

void QWaylandGLContext::makeCurrent()
{
    QWaylandDrmBuffer *mBuffer = (QWaylandDrmBuffer *)mWindow->getBuffer();
    QRect geometry = mWindow->geometry();

    if (!mBuffer)
	return;

    eglMakeCurrent(mDisplay->eglDisplay(), 0, 0, mBuffer->mContext);

    glViewport(0, 0, geometry.width(), geometry.height());
    glBindFramebuffer(GL_FRAMEBUFFER, mBuffer->mFbo);
    glBindTexture(GL_TEXTURE_2D, mBuffer->mTexture);
    glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, mBuffer->mImage);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
			   GL_TEXTURE_2D, mBuffer->mTexture, 0);
}

void QWaylandGLContext::doneCurrent()
{
}

/* drawTexture - Draw from a texture into a the current framebuffer
 * @rect: GL normalized coords for drawing (between -1.0f and 1.0f)
 * @tex_id: texture source
 * @texSize: size of source rectangle in Qt coords
 * @br: bounding rect for drawing
 */
static void drawTexture(const QRectF &rect, GLuint tex_id,
			const QSize &texSize, const QRectF &br)
{
    QRectF src = br.isEmpty()
        ? QRectF(QPointF(), texSize)
        : QRectF(QPointF(br.x(), texSize.height() - br.bottom()), br.size());
    qreal width = texSize.width();
    qreal height = texSize.height();

    src.setLeft(src.left() / width);
    src.setRight(src.right() / width);
    src.setTop(src.top() / height);
    src.setBottom(src.bottom() / height);

    const GLfloat tx1 = src.left();
    const GLfloat tx2 = src.right();
    const GLfloat ty1 = src.top();
    const GLfloat ty2 = src.bottom();

    GLfloat texCoordArray[4*2] = {
        tx1, ty2, tx2, ty2, tx2, ty1, tx1, ty1
    };

    GLfloat vertexArray[4*2];
    extern void qt_add_rect_to_array(const QRectF &r, GLfloat *array);
    qt_add_rect_to_array(rect, vertexArray);

    glVertexAttribPointer(QT_VERTEX_COORDS_ATTR, 2, GL_FLOAT, GL_FALSE, 0, vertexArray);
    glVertexAttribPointer(QT_TEXTURE_COORDS_ATTR, 2, GL_FLOAT, GL_FALSE, 0, texCoordArray);

    glBindTexture(GL_TEXTURE_2D, tex_id);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    glEnableVertexAttribArray(QT_VERTEX_COORDS_ATTR);
    glEnableVertexAttribArray(QT_TEXTURE_COORDS_ATTR);
    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
    glDisableVertexAttribArray(QT_VERTEX_COORDS_ATTR);
    glDisableVertexAttribArray(QT_TEXTURE_COORDS_ATTR);

    glBindTexture(GL_TEXTURE_2D, 0);
}

void QWaylandGLContext::swapBuffers()
{
    QWaylandWindow *mParentWindow = mWindow->getParentWindow();
    QWaylandDrmBuffer *mBuffer, *mParentBuffer;
    QRect geometry = mWindow->geometry(), parentGeometry;
    QGLShaderProgram *blitProgram;
    QRectF r;
    qreal w;
    qreal h;

    if (!mParentWindow) {
	qDebug("swap without parent widget?\n");
	return;
    }

    if (!mParentWindow->surface()) {
	qDebug("parent has no surface??\n");
	return;
    }

    parentGeometry = mParentWindow->geometry();
    mBuffer = (QWaylandDrmBuffer *)mWindow->getBuffer();
    mParentBuffer = (QWaylandDrmBuffer *)mParentWindow->getBuffer();

    glDisable(GL_DEPTH_TEST);

    /* These need to be generated against the src context */
    if (!parentFbo)
	glGenFramebuffers(1, &parentFbo);
    if (!parentRbo)
	glGenRenderbuffers(1, &parentRbo);

    glBindFramebuffer(GL_FRAMEBUFFER, parentFbo);
    glBindRenderbuffer(GL_RENDERBUFFER, parentRbo);
    glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
					   mParentBuffer->mImage);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
			      GL_RENDERBUFFER, parentRbo);
    glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, mBuffer->mImage);
    glViewport(0, 0, parentGeometry.width(), parentGeometry.height());

    blitProgram = QGLEngineSharedShaders::shadersForContext(QGLContext::currentContext())->blitProgram();
    blitProgram->bind();
    blitProgram->setUniformValue("imageTexture", 0);

    /* Transform the target rect to the appropriate coords on the parent */
    w = parentGeometry.width();
    h = parentGeometry.height();

    r.setLeft((geometry.left() / w) * 2.0f - 1.0f);
    if (geometry.right() == (parentGeometry.width() - 1))
	r.setRight(1.0f);
    else
	r.setRight((geometry.right() / w) * 2.0f - 1.0f);

    r.setTop((geometry.top() / h) * 2.0f - 1.0f);
    if (geometry.bottom() == (parentGeometry.height() - 1))
       r.setBottom(-1.0f);
    else
	r.setBottom((geometry.bottom() / h) * 2.0f - 1.0f);

    drawTexture(r, mBuffer->mTexture, mParentWindow->widget()->size(), parentGeometry);

    wl_surface_damage(mParentWindow->surface(), geometry.left(), geometry.top(),
		      geometry.right(), geometry.bottom());
    /* restore things to the last valid GL state */
    makeCurrent();
    /* hack: avoid tight swapBuffers loops */
    usleep(20000);
}

void *QWaylandGLContext::getProcAddress(const QString &string)
{
    return (void *) eglGetProcAddress(string.toLatin1().data());
}

QPlatformGLContext *QWaylandWindow::glContext() const
{
    if (!mGLContext) {
        QWaylandWindow *that = const_cast<QWaylandWindow *>(this);
        that->mGLContext = new QWaylandGLContext(mDisplay, that, widget()->platformWindowFormat());
    }

    return mGLContext;
}