1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the plugins of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qeglfsbackingstore.h"
43 #include "qeglfscursor.h"
45 #include <QtGui/QOpenGLContext>
46 #include <QtGui/QOpenGLPaintDevice>
47 #include <QtGui/QOpenGLShaderProgram>
49 #include <QtGui/QScreen>
53 QEglFSBackingStore::QEglFSBackingStore(QWindow *window)
54 : QPlatformBackingStore(window)
55 , m_context(new QOpenGLContext)
59 m_context->setFormat(window->requestedFormat());
60 m_context->setScreen(window->screen());
64 QEglFSBackingStore::~QEglFSBackingStore()
69 QPaintDevice *QEglFSBackingStore::paintDevice()
74 void QEglFSBackingStore::flush(QWindow *window, const QRegion ®ion, const QPoint &offset)
79 #ifdef QEGL_EXTRA_DEBUG
80 qWarning("QEglBackingStore::flush %p", window);
84 static const char *textureVertexProgram =
85 "attribute highp vec2 vertexCoordEntry;\n"
86 "attribute highp vec2 textureCoordEntry;\n"
87 "varying highp vec2 textureCoord;\n"
89 " textureCoord = textureCoordEntry;\n"
90 " gl_Position = vec4(vertexCoordEntry, 0.0, 1.0);\n"
93 static const char *textureFragmentProgram =
94 "uniform sampler2D texture;\n"
95 "varying highp vec2 textureCoord;\n"
97 " gl_FragColor = texture2D(texture, textureCoord).bgra;\n"
100 m_program = new QOpenGLShaderProgram;
102 m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, textureVertexProgram);
103 m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, textureFragmentProgram);
106 m_vertexCoordEntry = m_program->attributeLocation("vertexCoordEntry");
107 m_textureCoordEntry = m_program->attributeLocation("textureCoordEntry");
112 const GLfloat textureCoordinates[] = {
119 QRectF r = window->geometry();
120 QRectF sr = window->screen()->geometry();
122 GLfloat x1 = (r.left() / sr.width()) * 2 - 1;
123 GLfloat x2 = (r.right() / sr.width()) * 2 - 1;
124 GLfloat y1 = (r.top() / sr.height()) * 2 - 1;
125 GLfloat y2 = (r.bottom() / sr.height()) * 2 - 1;
127 const GLfloat vertexCoordinates[] = {
134 glEnableVertexAttribArray(m_vertexCoordEntry);
135 glEnableVertexAttribArray(m_textureCoordEntry);
137 glVertexAttribPointer(m_vertexCoordEntry, 2, GL_FLOAT, GL_FALSE, 0, vertexCoordinates);
138 glVertexAttribPointer(m_textureCoordEntry, 2, GL_FLOAT, GL_FALSE, 0, textureCoordinates);
140 glBindTexture(GL_TEXTURE_2D, m_texture);
142 if (!m_dirty.isNull()) {
143 QRect imageRect = m_image.rect();
146 foreach (const QRect &rect, m_dirty.rects()) {
147 // intersect with image rect to be sure
148 QRect r = imageRect & rect;
150 // if the rect is wide enough it's cheaper to just
151 // extend it instead of doing an image copy
152 if (r.width() >= imageRect.width() / 2) {
154 r.setWidth(imageRect.width());
160 foreach (const QRect &rect, fixed.rects()) {
161 // if the sub-rect is full-width we can pass the image data directly to
162 // OpenGL instead of copying, since there's no gap between scanlines
163 if (rect.width() == imageRect.width()) {
164 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, rect.y(), rect.width(), rect.height(), GL_RGBA, GL_UNSIGNED_BYTE, m_image.constScanLine(rect.y()));
166 glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x(), rect.y(), rect.width(), rect.height(), GL_RGBA, GL_UNSIGNED_BYTE,
167 m_image.copy(rect).constBits());
174 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
176 m_program->release();
177 glBindTexture(GL_TEXTURE_2D, 0);
178 glDisableVertexAttribArray(m_vertexCoordEntry);
179 glDisableVertexAttribArray(m_textureCoordEntry);
182 if (QEglFSCursor *cursor = static_cast<QEglFSCursor *>(window->screen()->handle()->cursor()))
185 m_context->swapBuffers(window);
188 void QEglFSBackingStore::makeCurrent()
190 // needed to prevent QOpenGLContext::makeCurrent() from failing
191 window()->setSurfaceType(QSurface::OpenGLSurface);
193 m_context->makeCurrent(window());
196 void QEglFSBackingStore::beginPaint(const QRegion &rgn)
200 m_dirty = m_dirty | rgn;
203 void QEglFSBackingStore::endPaint()
207 void QEglFSBackingStore::resize(const QSize &size, const QRegion &staticContents)
209 Q_UNUSED(staticContents);
211 m_image = QImage(size, QImage::Format_RGB32);
214 glDeleteTextures(1, &m_texture);
215 glGenTextures(1, &m_texture);
216 glBindTexture(GL_TEXTURE_2D, m_texture);
217 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
218 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
219 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
220 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
222 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.width(), size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);