2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2012-01-05 04:03:39 +00:00
|
|
|
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
2012-01-20 03:06:31 +00:00
|
|
|
** Contact: http://www.qt-project.org/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the plugins of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
|
|
|
** GNU Lesser General Public License Usage
|
2011-05-24 09:34:08 +00:00
|
|
|
** 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.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-05-24 09:34:08 +00:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2011-04-27 10:05:43 +00:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
2011-05-24 09:34:08 +00:00
|
|
|
** 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.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
**
|
|
|
|
**
|
|
|
|
**
|
|
|
|
**
|
2012-01-24 06:17:24 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-07-25 12:55:45 +00:00
|
|
|
#include "qeglfsbackingstore.h"
|
2012-04-09 21:16:19 +00:00
|
|
|
#include "qeglfscursor.h"
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-03-23 07:51:15 +00:00
|
|
|
#include <QtGui/QOpenGLContext>
|
|
|
|
#include <QtGui/QOpenGLPaintDevice>
|
2012-04-16 19:24:41 +00:00
|
|
|
#include <QtGui/QOpenGLShaderProgram>
|
|
|
|
|
|
|
|
#include <QtGui/QScreen>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
2012-03-23 07:51:15 +00:00
|
|
|
QEglFSBackingStore::QEglFSBackingStore(QWindow *window)
|
|
|
|
: QPlatformBackingStore(window)
|
|
|
|
, m_context(new QOpenGLContext)
|
2012-04-16 19:24:41 +00:00
|
|
|
#ifdef EGLFS_BACKINGSTORE_USE_IMAGE
|
|
|
|
, m_texture(0)
|
|
|
|
, m_program(0)
|
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2012-03-23 07:51:15 +00:00
|
|
|
m_context->setFormat(window->requestedFormat());
|
|
|
|
m_context->setScreen(window->screen());
|
|
|
|
m_context->create();
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-03-23 07:51:15 +00:00
|
|
|
QEglFSBackingStore::~QEglFSBackingStore()
|
|
|
|
{
|
|
|
|
delete m_context;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-03-23 07:51:15 +00:00
|
|
|
QPaintDevice *QEglFSBackingStore::paintDevice()
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2012-04-16 19:24:41 +00:00
|
|
|
#ifdef EGLFS_BACKINGSTORE_USE_IMAGE
|
|
|
|
return &m_image;
|
|
|
|
#else
|
2012-03-23 07:51:15 +00:00
|
|
|
return m_device;
|
2012-04-16 19:24:41 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2011-12-13 10:00:56 +00:00
|
|
|
void QEglFSBackingStore::flush(QWindow *window, const QRegion ®ion, const QPoint &offset)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
Q_UNUSED(region);
|
|
|
|
Q_UNUSED(offset);
|
2012-03-23 07:51:15 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
#ifdef QEGL_EXTRA_DEBUG
|
2011-12-13 10:00:56 +00:00
|
|
|
qWarning("QEglBackingStore::flush %p", window);
|
2011-04-27 10:05:43 +00:00
|
|
|
#endif
|
2012-03-23 07:51:15 +00:00
|
|
|
|
2012-04-16 19:24:41 +00:00
|
|
|
#ifdef EGLFS_BACKINGSTORE_USE_IMAGE
|
|
|
|
if (!m_program) {
|
|
|
|
static const char *textureVertexProgram =
|
|
|
|
"attribute highp vec2 vertexCoordEntry;\n"
|
|
|
|
"attribute highp vec2 textureCoordEntry;\n"
|
|
|
|
"varying highp vec2 textureCoord;\n"
|
|
|
|
"void main() {\n"
|
|
|
|
" textureCoord = textureCoordEntry;\n"
|
|
|
|
" gl_Position = vec4(vertexCoordEntry, 0.0, 1.0);\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
static const char *textureFragmentProgram =
|
|
|
|
"uniform sampler2D texture;\n"
|
|
|
|
"varying highp vec2 textureCoord;\n"
|
|
|
|
"void main() {\n"
|
2012-04-17 06:50:47 +00:00
|
|
|
" gl_FragColor = texture2D(texture, textureCoord).bgra;\n"
|
2012-04-16 19:24:41 +00:00
|
|
|
"}\n";
|
|
|
|
|
|
|
|
m_program = new QOpenGLShaderProgram;
|
|
|
|
|
|
|
|
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, textureVertexProgram);
|
|
|
|
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, textureFragmentProgram);
|
|
|
|
m_program->link();
|
|
|
|
|
|
|
|
m_vertexCoordEntry = m_program->attributeLocation("vertexCoordEntry");
|
|
|
|
m_textureCoordEntry = m_program->attributeLocation("textureCoordEntry");
|
|
|
|
}
|
|
|
|
|
|
|
|
m_program->bind();
|
|
|
|
|
|
|
|
const GLfloat textureCoordinates[] = {
|
|
|
|
0, 1,
|
|
|
|
1, 1,
|
|
|
|
1, 0,
|
|
|
|
0, 0
|
|
|
|
};
|
|
|
|
|
|
|
|
QRectF r = window->geometry();
|
|
|
|
QRectF sr = window->screen()->geometry();
|
|
|
|
|
|
|
|
GLfloat x1 = (r.left() / sr.width()) * 2 - 1;
|
|
|
|
GLfloat x2 = (r.right() / sr.width()) * 2 - 1;
|
|
|
|
GLfloat y1 = (r.top() / sr.height()) * 2 - 1;
|
|
|
|
GLfloat y2 = (r.bottom() / sr.height()) * 2 - 1;
|
|
|
|
|
|
|
|
const GLfloat vertexCoordinates[] = {
|
|
|
|
x1, y1,
|
|
|
|
x2, y1,
|
|
|
|
x2, y2,
|
|
|
|
x1, y2
|
|
|
|
};
|
|
|
|
|
|
|
|
glEnableVertexAttribArray(m_vertexCoordEntry);
|
|
|
|
glEnableVertexAttribArray(m_textureCoordEntry);
|
|
|
|
|
|
|
|
glVertexAttribPointer(m_vertexCoordEntry, 2, GL_FLOAT, GL_FALSE, 0, vertexCoordinates);
|
|
|
|
glVertexAttribPointer(m_textureCoordEntry, 2, GL_FLOAT, GL_FALSE, 0, textureCoordinates);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_texture);
|
|
|
|
|
2012-04-17 06:46:13 +00:00
|
|
|
if (!m_dirty.isNull()) {
|
|
|
|
QRect imageRect = m_image.rect();
|
|
|
|
|
|
|
|
QRegion fixed;
|
|
|
|
foreach (const QRect &rect, m_dirty.rects()) {
|
|
|
|
// intersect with image rect to be sure
|
|
|
|
QRect r = imageRect & rect;
|
|
|
|
|
|
|
|
// if the rect is wide enough it's cheaper to just
|
|
|
|
// extend it instead of doing an image copy
|
|
|
|
if (r.width() >= imageRect.width() / 2) {
|
|
|
|
r.setX(0);
|
|
|
|
r.setWidth(imageRect.width());
|
|
|
|
}
|
|
|
|
|
|
|
|
fixed |= r;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (const QRect &rect, fixed.rects()) {
|
|
|
|
// if the sub-rect is full-width we can pass the image data directly to
|
|
|
|
// OpenGL instead of copying, since there's no gap between scanlines
|
|
|
|
if (rect.width() == imageRect.width()) {
|
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, rect.y(), rect.width(), rect.height(), GL_RGBA, GL_UNSIGNED_BYTE, m_image.constScanLine(rect.y()));
|
|
|
|
} else {
|
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x(), rect.y(), rect.width(), rect.height(), GL_RGBA, GL_UNSIGNED_BYTE,
|
|
|
|
m_image.copy(rect).constBits());
|
|
|
|
}
|
2012-04-16 19:24:41 +00:00
|
|
|
}
|
|
|
|
|
2012-04-17 06:46:13 +00:00
|
|
|
m_dirty = QRegion();
|
|
|
|
}
|
2012-04-16 19:24:41 +00:00
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
|
|
|
glDisableVertexAttribArray(m_vertexCoordEntry);
|
|
|
|
glDisableVertexAttribArray(m_textureCoordEntry);
|
|
|
|
#endif
|
|
|
|
|
2012-04-09 21:16:19 +00:00
|
|
|
// draw the cursor
|
|
|
|
if (QEglFSCursor *cursor = static_cast<QEglFSCursor *>(window->screen()->handle()->cursor()))
|
|
|
|
cursor->render();
|
|
|
|
|
2012-03-23 07:51:15 +00:00
|
|
|
m_context->swapBuffers(window);
|
|
|
|
}
|
|
|
|
|
2012-04-16 19:24:41 +00:00
|
|
|
void QEglFSBackingStore::makeCurrent()
|
2012-03-23 07:51:15 +00:00
|
|
|
{
|
|
|
|
// needed to prevent QOpenGLContext::makeCurrent() from failing
|
|
|
|
window()->setSurfaceType(QSurface::OpenGLSurface);
|
|
|
|
|
|
|
|
m_context->makeCurrent(window());
|
2012-04-16 19:24:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void QEglFSBackingStore::beginPaint(const QRegion &rgn)
|
|
|
|
{
|
|
|
|
makeCurrent();
|
|
|
|
|
|
|
|
#ifdef EGLFS_BACKINGSTORE_USE_IMAGE
|
|
|
|
m_dirty = m_dirty | rgn;
|
|
|
|
#else
|
|
|
|
Q_UNUSED(rgn);
|
2012-03-23 07:51:15 +00:00
|
|
|
m_device = new QOpenGLPaintDevice(window()->size());
|
2012-04-16 19:24:41 +00:00
|
|
|
#endif
|
2012-03-23 07:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void QEglFSBackingStore::endPaint()
|
|
|
|
{
|
2012-04-16 19:24:41 +00:00
|
|
|
#ifndef EGLFS_BACKINGSTORE_USE_IMAGE
|
2012-03-23 07:51:15 +00:00
|
|
|
delete m_device;
|
2012-04-16 19:24:41 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2011-07-25 12:55:45 +00:00
|
|
|
void QEglFSBackingStore::resize(const QSize &size, const QRegion &staticContents)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2011-07-25 12:55:45 +00:00
|
|
|
Q_UNUSED(staticContents);
|
2012-04-16 19:24:41 +00:00
|
|
|
|
|
|
|
#ifdef EGLFS_BACKINGSTORE_USE_IMAGE
|
|
|
|
m_image = QImage(size, QImage::Format_RGB32);
|
|
|
|
makeCurrent();
|
|
|
|
if (m_texture)
|
|
|
|
glDeleteTextures(1, &m_texture);
|
|
|
|
glGenTextures(1, &m_texture);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_texture);
|
|
|
|
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);
|
|
|
|
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.width(), size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
|
|
|
|
#else
|
|
|
|
Q_UNUSED(size);
|
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|