Support adopting an existing EGLContext in eglfs and xcb

Add also a manual test application. For GLX there is an autotest since
that is likely to be run on one of the CIs. For EGL and especially
eglfs this is likely not the case so a manual test is better.

Task-number: QTBUG-37552
Change-Id: Ib09db5d909befb68d16f69abd401a56abe55f28a
Reviewed-by: Gunnar Sletta <gunnar.sletta@jollamobile.com>
Reviewed-by: Jørgen Lind <jorgen.lind@digia.com>
This commit is contained in:
Laszlo Agocs 2014-04-24 16:40:22 +02:00 committed by The Qt Project
parent 3b6d8ba49b
commit 0b5c0ad2ef
18 changed files with 573 additions and 36 deletions

View File

@ -1 +1,2 @@
HEADERS += $$PWD/qglxnativecontext.h
HEADERS += $$PWD/qglxnativecontext.h \
$$PWD/qeglnativecontext.h

View File

@ -0,0 +1,73 @@
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** 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.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QEGLNATIVECONTEXT_H
#define QEGLNATIVECONTEXT_H
#include <EGL/egl.h>
QT_BEGIN_NAMESPACE
struct QEGLNativeContext
{
QEGLNativeContext()
: m_context(0),
m_display(0)
{ }
QEGLNativeContext(EGLContext ctx, EGLDisplay dpy)
: m_context(ctx),
m_display(dpy)
{ }
EGLContext context() const { return m_context; }
EGLDisplay display() const { return m_display; }
private:
EGLContext m_context;
EGLDisplay m_display;
};
QT_END_NAMESPACE
Q_DECLARE_METATYPE(QEGLNativeContext)
#endif // QEGLNATIVECONTEXT_H

View File

@ -0,0 +1,64 @@
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file. Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: http://www.gnu.org/copyleft/fdl.html.
** $QT_END_LICENSE$
**
****************************************************************************/
/*!
\class QEGLNativeContext
\inmodule QtPlatformHeaders
\since 5.4
\brief A class encapsulating an EGL context and display handle
\note As of Qt 5.4 there is no binary compatibility guarantee for this class, meaning
that an application using it is only guaranteed to work with the Qt version it was
developed against.
\sa QOpenGLContext::setNativeHandle(), QOpenGLContext::nativeHandle()
*/
/*!
\fn EGLContext QEGLNativeContext::context() const
\return the EGL context.
*/
/*!
\fn EGLDisplay QEGLNativeContext::display() const
\return the EGL display.
*/
/*!
\fn QEGLNativeContext::QEGLNativeContext()
Construct a new instance with no handles.
*/
/*!
\fn QEGLNativeContext::QEGLNativeContext(EGLContext ctx, EGLDisplay dpy)
Constructs a new instance with the provided \a ctx and \a dpy handles.
*/

View File

@ -44,6 +44,7 @@
#include "qeglpbuffer_p.h"
#include <qpa/qplatformwindow.h>
#include <QOpenGLContext>
#include <QtPlatformHeaders/QEGLNativeContext>
#include <QDebug>
QT_BEGIN_NAMESPACE
@ -108,25 +109,21 @@ QT_BEGIN_NAMESPACE
#define GL_CONTEXT_COMPATIBILITY_PROFILE_BIT 0x00000002
#endif
QEGLPlatformContext::QEGLPlatformContext(const QSurfaceFormat &format, QPlatformOpenGLContext *share, EGLDisplay display)
: m_eglDisplay(display)
, m_eglConfig(q_configFromGLFormat(display, format))
, m_swapInterval(-1)
, m_swapIntervalEnvChecked(false)
, m_swapIntervalFromEnv(-1)
{
init(format, share);
}
QEGLPlatformContext::QEGLPlatformContext(const QSurfaceFormat &format, QPlatformOpenGLContext *share, EGLDisplay display,
EGLConfig config)
EGLConfig *config, const QVariant &nativeHandle)
: m_eglDisplay(display)
, m_eglConfig(config)
, m_swapInterval(-1)
, m_swapIntervalEnvChecked(false)
, m_swapIntervalFromEnv(-1)
{
init(format, share);
if (nativeHandle.isNull()) {
m_eglConfig = config ? *config : q_configFromGLFormat(display, format);
m_ownsContext = true;
init(format, share);
} else {
m_ownsContext = false;
adopt(nativeHandle, share);
}
}
void QEGLPlatformContext::init(const QSurfaceFormat &format, QPlatformOpenGLContext *share)
@ -198,6 +195,59 @@ void QEGLPlatformContext::init(const QSurfaceFormat &format, QPlatformOpenGLCont
q_printEglConfig(m_eglDisplay, m_eglConfig);
}
updateFormatFromGL();
}
void QEGLPlatformContext::adopt(const QVariant &nativeHandle, QPlatformOpenGLContext *share)
{
if (!nativeHandle.canConvert<QEGLNativeContext>()) {
qWarning("QEGLPlatformContext: Requires a QEGLNativeContext");
return;
}
QEGLNativeContext handle = nativeHandle.value<QEGLNativeContext>();
EGLContext context = handle.context();
if (!context) {
qWarning("QEGLPlatformContext: No EGLContext given");
return;
}
// A context belonging to a given EGLDisplay cannot be used with another one.
if (handle.display() != m_eglDisplay) {
qWarning("QEGLPlatformContext: Cannot adopt context from different display");
return;
}
// Figure out the EGLConfig.
EGLint value = 0;
eglQueryContext(m_eglDisplay, context, EGL_CONFIG_ID, &value);
EGLint n = 0;
EGLConfig cfg;
const EGLint attribs[] = { EGL_CONFIG_ID, value, EGL_NONE };
if (eglChooseConfig(m_eglDisplay, attribs, &cfg, 1, &n) && n == 1) {
m_eglConfig = cfg;
m_format = q_glFormatFromConfig(m_eglDisplay, m_eglConfig);
} else {
qWarning("QEGLPlatformContext: Failed to get framebuffer configuration for context");
}
// Fetch client API type.
value = 0;
eglQueryContext(m_eglDisplay, context, EGL_CONTEXT_CLIENT_TYPE, &value);
if (value == EGL_OPENGL_API || value == EGL_OPENGL_ES_API) {
m_api = value;
eglBindAPI(m_api);
} else {
qWarning("QEGLPlatformContext: Failed to get client API type");
m_api = EGL_OPENGL_ES_API;
}
m_eglContext = context;
m_shareContext = share ? static_cast<QEGLPlatformContext *>(share)->m_eglContext : 0;
updateFormatFromGL();
}
void QEGLPlatformContext::updateFormatFromGL()
{
#ifndef QT_NO_OPENGL
// Make the context current to ensure the GL version query works. This needs a surface too.
const EGLint pbufferAttributes[] = {
@ -296,10 +346,10 @@ bool QEGLPlatformContext::makeCurrent(QPlatformSurface *surface)
QEGLPlatformContext::~QEGLPlatformContext()
{
if (m_eglContext != EGL_NO_CONTEXT) {
if (m_ownsContext && m_eglContext != EGL_NO_CONTEXT)
eglDestroyContext(m_eglDisplay, m_eglContext);
m_eglContext = EGL_NO_CONTEXT;
}
m_eglContext = EGL_NO_CONTEXT;
}
void QEGLPlatformContext::doneCurrent()

View File

@ -44,6 +44,7 @@
#include <qpa/qplatformwindow.h>
#include <qpa/qplatformopenglcontext.h>
#include <QtCore/QVariant>
#include <EGL/egl.h>
QT_BEGIN_NAMESPACE
@ -51,9 +52,8 @@ QT_BEGIN_NAMESPACE
class QEGLPlatformContext : public QPlatformOpenGLContext
{
public:
QEGLPlatformContext(const QSurfaceFormat &format, QPlatformOpenGLContext *share, EGLDisplay display);
QEGLPlatformContext(const QSurfaceFormat &format, QPlatformOpenGLContext *share, EGLDisplay display,
EGLConfig config);
EGLConfig *config = 0, const QVariant &nativeHandle = QVariant());
~QEGLPlatformContext();
bool makeCurrent(QPlatformSurface *surface);
@ -74,6 +74,8 @@ protected:
private:
void init(const QSurfaceFormat &format, QPlatformOpenGLContext *share);
void adopt(const QVariant &nativeHandle, QPlatformOpenGLContext *share);
void updateFormatFromGL();
EGLContext m_eglContext;
EGLContext m_shareContext;
@ -84,6 +86,7 @@ private:
int m_swapInterval;
bool m_swapIntervalEnvChecked;
int m_swapIntervalFromEnv;
bool m_ownsContext;
};
QT_END_NAMESPACE

View File

@ -158,9 +158,14 @@ QPlatformOpenGLContext *QEGLPlatformIntegration::createPlatformOpenGLContext(QOp
// If there is a "root" window into which raster and QOpenGLWidget content is
// composited, all other contexts must share with its context.
QOpenGLContext *compositingContext = screen ? screen->compositingContext() : 0;
return createContext(context->format(),
compositingContext ? compositingContext->handle() : context->shareHandle(),
display());
QPlatformOpenGLContext *share = compositingContext ? compositingContext->handle() : context->shareHandle();
QVariant nativeHandle = context->nativeHandle();
QPlatformOpenGLContext *platformContext = createContext(context->format(),
share,
display(),
&nativeHandle);
context->setNativeHandle(nativeHandle);
return platformContext;
}
QPlatformOffscreenSurface *QEGLPlatformIntegration::createPlatformOffscreenSurface(QOffscreenSurface *surface) const

View File

@ -44,6 +44,7 @@
#include <qpa/qplatformintegration.h>
#include <qpa/qplatformnativeinterface.h>
#include <QtCore/QVariant>
#include <EGL/egl.h>
QT_BEGIN_NAMESPACE
@ -89,7 +90,8 @@ protected:
virtual QEGLPlatformWindow *createWindow(QWindow *window) const = 0;
virtual QEGLPlatformContext *createContext(const QSurfaceFormat &format,
QPlatformOpenGLContext *shareContext,
EGLDisplay display) const = 0;
EGLDisplay display,
QVariant *nativeHandle) const = 0;
virtual QPlatformOffscreenSurface *createOffscreenSurface(EGLDisplay display,
const QSurfaceFormat &format,
QOffscreenSurface *surface) const = 0;

View File

@ -42,7 +42,6 @@
#include "qeglfscontext.h"
#include "qeglfswindow.h"
#include "qeglfshooks.h"
#include "qeglfsintegration.h"
#include <QtPlatformSupport/private/qeglconvenience_p.h>
#include <QtPlatformSupport/private/qeglpbuffer_p.h>
@ -52,9 +51,9 @@
QT_BEGIN_NAMESPACE
QEglFSContext::QEglFSContext(const QSurfaceFormat &format, QPlatformOpenGLContext *share, EGLDisplay display)
: QEGLPlatformContext(format, share, display,
QEglFSIntegration::chooseConfig(display, format))
QEglFSContext::QEglFSContext(const QSurfaceFormat &format, QPlatformOpenGLContext *share, EGLDisplay display,
EGLConfig *config, const QVariant &nativeHandle)
: QEGLPlatformContext(format, share, display, config, nativeHandle)
{
}

View File

@ -43,13 +43,15 @@
#define QEGLFSCONTEXT_H
#include <QtPlatformSupport/private/qeglplatformcontext_p.h>
#include <QtCore/QVariant>
QT_BEGIN_NAMESPACE
class QEglFSContext : public QEGLPlatformContext
{
public:
QEglFSContext(const QSurfaceFormat &format, QPlatformOpenGLContext *share, EGLDisplay display);
QEglFSContext(const QSurfaceFormat &format, QPlatformOpenGLContext *share, EGLDisplay display,
EGLConfig *config, const QVariant &nativeHandle);
EGLSurface eglSurfaceForPlatformSurface(QPlatformSurface *surface) Q_DECL_OVERRIDE;
void swapBuffers(QPlatformSurface *surface) Q_DECL_OVERRIDE;
};

View File

@ -50,6 +50,7 @@
#include <QtPlatformSupport/private/qeglconvenience_p.h>
#include <QtPlatformSupport/private/qeglplatformcontext_p.h>
#include <QtPlatformSupport/private/qeglpbuffer_p.h>
#include <QtPlatformHeaders/QEGLNativeContext>
#include <qpa/qplatformwindow.h>
#include <QtGui/QSurfaceFormat>
@ -115,9 +116,20 @@ QEGLPlatformWindow *QEglFSIntegration::createWindow(QWindow *window) const
QEGLPlatformContext *QEglFSIntegration::createContext(const QSurfaceFormat &format,
QPlatformOpenGLContext *shareContext,
EGLDisplay display) const
EGLDisplay display,
QVariant *nativeHandle) const
{
return new QEglFSContext(QEglFSHooks::hooks()->surfaceFormatFor(format), shareContext, display);
QEglFSContext *ctx;
if (!nativeHandle || nativeHandle->isNull()) {
EGLConfig config = QEglFSIntegration::chooseConfig(display, format);
ctx = new QEglFSContext(QEglFSHooks::hooks()->surfaceFormatFor(format), shareContext, display,
&config, QVariant());
} else {
ctx = new QEglFSContext(QEglFSHooks::hooks()->surfaceFormatFor(format), shareContext, display,
0, *nativeHandle);
}
*nativeHandle = QVariant::fromValue<QEGLNativeContext>(QEGLNativeContext(ctx->eglContext(), display));
return ctx;
}
QPlatformOffscreenSurface *QEglFSIntegration::createOffscreenSurface(EGLDisplay display,

View File

@ -66,7 +66,8 @@ protected:
QEGLPlatformWindow *createWindow(QWindow *window) const Q_DECL_OVERRIDE;
QEGLPlatformContext *createContext(const QSurfaceFormat &format,
QPlatformOpenGLContext *shareContext,
EGLDisplay display) const Q_DECL_OVERRIDE;
EGLDisplay display,
QVariant *nativeHandle) const Q_DECL_OVERRIDE;
QPlatformOffscreenSurface *createOffscreenSurface(EGLDisplay display,
const QSurfaceFormat &format,
QOffscreenSurface *surface) const Q_DECL_OVERRIDE;

View File

@ -83,6 +83,7 @@
#include "qxcbeglsurface.h"
#include <QtPlatformSupport/private/qeglplatformcontext_p.h>
#include <QtPlatformSupport/private/qeglpbuffer_p.h>
#include <QtPlatformHeaders/QEGLNativeContext>
#endif
#include <QtGui/QOpenGLContext>
@ -186,8 +187,8 @@ class QEGLXcbPlatformContext : public QEGLPlatformContext
{
public:
QEGLXcbPlatformContext(const QSurfaceFormat &glFormat, QPlatformOpenGLContext *share,
EGLDisplay display, QXcbConnection *c)
: QEGLPlatformContext(glFormat, share, display)
EGLDisplay display, QXcbConnection *c, const QVariant &nativeHandle)
: QEGLPlatformContext(glFormat, share, display, 0, nativeHandle)
, m_connection(c)
{
Q_XCB_NOOP(m_connection);
@ -223,6 +224,10 @@ public:
return static_cast<QEGLPbuffer *>(surface)->pbuffer();
}
QVariant nativeHandle() const {
return QVariant::fromValue<QEGLNativeContext>(QEGLNativeContext(eglContext(), eglDisplay()));
}
private:
QXcbConnection *m_connection;
};
@ -238,8 +243,13 @@ QPlatformOpenGLContext *QXcbIntegration::createPlatformOpenGLContext(QOpenGLCont
context->setNativeHandle(platformContext->nativeHandle());
return platformContext;
#elif defined(XCB_USE_EGL)
return new QEGLXcbPlatformContext(context->format(), context->shareHandle(),
screen->connection()->egl_display(), screen->connection());
QEGLXcbPlatformContext *platformContext = new QEGLXcbPlatformContext(context->format(),
context->shareHandle(),
screen->connection()->egl_display(),
screen->connection(),
context->nativeHandle());
context->setNativeHandle(platformContext->nativeHandle());
return platformContext;
#else
Q_UNUSED(screen);
qWarning("QXcbIntegration: Cannot create platform OpenGL context, neither GLX nor EGL are enabled");

View File

@ -47,7 +47,10 @@ unc
!contains(QT_CONFIG, openssl):!contains(QT_CONFIG, openssl-linked):SUBDIRS -= qssloptions
contains(QT_CONFIG, opengl):SUBDIRS += qopengltextureblitter
contains(QT_CONFIG, opengl) {
SUBDIRS += qopengltextureblitter
contains(QT_CONFIG, egl): SUBDIRS += qopenglcontext
}
win32 {
SUBDIRS -= network_remote_stresstest network_stresstest

View File

@ -0,0 +1,54 @@
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** 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.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtGui/QGuiApplication>
#include "qopenglcontextwindow.h"
int main(int argc, char **argv)
{
QGuiApplication app(argc, argv);
QOpenGLContextWindow window;
window.resize(300, 300);
window.show();
return app.exec();
}

View File

@ -0,0 +1,9 @@
TEMPLATE = app
TARGET = qopenglcontext
QT += gui-private platformsupport-private
HEADERS += $$PWD/qopenglcontextwindow.h
SOURCES += $$PWD/main.cpp \
$$PWD/qopenglcontextwindow.cpp

View File

@ -0,0 +1,173 @@
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** 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.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qopenglcontextwindow.h"
#include <QtGui/QOpenGLFunctions>
#include <QtGui/QOffscreenSurface>
#include <QtGui/QGuiApplication>
#include <QtGui/QMatrix4x4>
#include <qpa/qplatformnativeinterface.h>
#include <QtPlatformSupport/private/qeglconvenience_p.h>
#include <QtPlatformHeaders/QEGLNativeContext>
QOpenGLContextWindow::QOpenGLContextWindow()
: m_blitter(0)
{
setSurfaceType(OpenGLSurface);
m_context = new QOpenGLContext(this);
m_context->setFormat(requestedFormat());
m_context->create();
m_image = QImage(QStringLiteral("qticon64.png")).convertToFormat(QImage::Format_RGBA8888);
Q_ASSERT(!m_image.isNull());
create(); // to make sure format() returns something real
createForeignContext();
}
QOpenGLContextWindow::~QOpenGLContextWindow()
{
if (m_blitter) {
m_blitter->destroy(); // the dtor does not call this for some reason
delete m_blitter;
}
}
void QOpenGLContextWindow::render()
{
if (!m_context->makeCurrent(this))
qFatal("makeCurrent() failed");
QOpenGLFunctions *f = m_context->functions();
f->glViewport(0, 0, dWidth(), dHeight());
f->glClearColor(0, 0, 0, 1);
f->glClear(GL_COLOR_BUFFER_BIT);
if (!m_blitter) {
m_blitter = new QOpenGLTextureBlitter;
m_blitter->create();
}
// Draw the image. If nothing gets shown, then something went wrong with the context
// adoption or sharing was not successfully enabled.
m_blitter->bind();
QRectF r(0, 0, dWidth(), dHeight());
QMatrix4x4 target = QOpenGLTextureBlitter::targetTransform(QRectF(100, 100, 100, 100), r.toRect());
m_blitter->blit(m_textureId, target, QOpenGLTextureBlitter::OriginTopLeft);
m_blitter->release();
m_context->swapBuffers(this);
}
void QOpenGLContextWindow::exposeEvent(QExposeEvent *)
{
if (isExposed())
render();
}
void QOpenGLContextWindow::createForeignContext()
{
// Here a context will be created manually. This context will share with m_context's
// underlying native context. This way the texture, that belongs to the context
// created here, will be accessible from m_context too.
EGLContext shareCtx = m_context->nativeHandle().value<QEGLNativeContext>().context();
Q_ASSERT(shareCtx != EGL_NO_CONTEXT);
EGLDisplay dpy = (EGLDisplay) qGuiApp->platformNativeInterface()->nativeResourceForWindow(
QByteArrayLiteral("egldisplay"), this);
Q_ASSERT(dpy != EGL_NO_DISPLAY);
QSurfaceFormat fmt = format();
EGLConfig config = q_configFromGLFormat(dpy, fmt);
QVector<EGLint> contextAttrs;
contextAttrs.append(EGL_CONTEXT_CLIENT_VERSION);
contextAttrs.append(fmt.majorVersion());
contextAttrs.append(EGL_NONE);
switch (fmt.renderableType()) {
#ifdef EGL_VERSION_1_4
case QSurfaceFormat::OpenGL:
eglBindAPI(EGL_OPENGL_API);
break;
#endif // EGL_VERSION_1_4
default:
eglBindAPI(EGL_OPENGL_ES_API);
break;
}
EGLContext ctx = eglCreateContext(dpy, config, shareCtx, contextAttrs.constData());
Q_ASSERT(ctx != EGL_NO_CONTEXT);
// Wrap ctx into a QOpenGLContext.
QOpenGLContext *ctxWrap = new QOpenGLContext;
ctxWrap->setNativeHandle(QVariant::fromValue<QEGLNativeContext>(QEGLNativeContext(ctx, dpy)));
ctxWrap->setShareContext(m_context); // only needed for correct bookkeeping
if (!ctxWrap->create())
qFatal("Failed to created wrapping context");
Q_ASSERT(ctxWrap->nativeHandle().value<QEGLNativeContext>().context() == ctx);
QOffscreenSurface surface;
surface.setFormat(fmt);
surface.create();
if (!ctxWrap->makeCurrent(&surface))
qFatal("Failed to make pbuffer surface current");
// Create the texture.
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
GLuint textureId = 0;
f->glGenTextures(1, &textureId);
f->glBindTexture(GL_TEXTURE_2D, textureId);
f->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
f->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_image.width(), m_image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE,
m_image.constBits());
Q_ASSERT(f->glGetError() == GL_NO_ERROR);
ctxWrap->doneCurrent();
delete ctxWrap; // ctx is not destroyed
eglDestroyContext(dpy, ctx); // resources like the texture stay alive until any context on the share list is alive
Q_ASSERT(eglGetError() == EGL_SUCCESS);
m_textureId = textureId;
}

View File

@ -0,0 +1,76 @@
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** 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.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QOPENGLCONTEXTWINDOW_H
#define QOPENGLCONTEXTWINDOW_H
#include <QtGui/QWindow>
#include <QtGui/QOpenGLContext>
#include <QtGui/QImage>
#include <QtCore/QVariant>
#include <QtGui/private/qopengltextureblitter_p.h>
class QOpenGLContextWindow : public QWindow
{
Q_OBJECT
public:
QOpenGLContextWindow();
~QOpenGLContextWindow();
void render();
protected:
void exposeEvent(QExposeEvent *event);
private:
qreal dWidth() const { return width() * devicePixelRatio(); }
qreal dHeight() const { return height() * devicePixelRatio(); }
void createForeignContext();
QOpenGLContext *m_context;
QImage m_image;
QVariant m_nativeHandle;
uint m_textureId;
QOpenGLTextureBlitter *m_blitter;
};
#endif

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB