2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2014-04-24 14:40:22 +00:00
|
|
|
|
|
|
|
#include "qopenglcontextwindow.h"
|
|
|
|
#include <QtGui/QOpenGLFunctions>
|
|
|
|
#include <QtGui/QOffscreenSurface>
|
|
|
|
#include <QtGui/QGuiApplication>
|
|
|
|
#include <QtGui/QMatrix4x4>
|
|
|
|
#include <qpa/qplatformnativeinterface.h>
|
|
|
|
|
2016-09-30 11:08:03 +00:00
|
|
|
#include <QtEglSupport/private/qeglconvenience_p.h>
|
2014-04-24 14:40:22 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2020-10-07 10:11:46 +00:00
|
|
|
using namespace QNativeInterface;
|
|
|
|
auto *eglContext = m_context->nativeInterface<QEGLContext>();
|
Introduce platform API abstraction for QOpenGLContext
The API is available by including qopenglcontext.h as usual,
but scoped in the QPlatformInterface namespace. The namespace
exposes platform specific type-safe interfaces that provide:
a) Factory functions for adopting native contexts, e.g.
QCocoaGLContext::fromNative(nsContext, shareContext);
b) Access to underlying native handles, e.g.
openGLContext->platformInterface<QCocoaGLContext>->nativeContext()
c) Platform specific functionality, e.g.
static QWGLContext::openGLModuleHandle()
openGLContext->platformInterface<QEGLContext>->doSomething();
The platform interfaces live close to the classes they extend,
removing the need for complex indirection and plumbing, and
avoids kitchen-sink modules and APIs such as the extras modules,
QPlatformFunctions, or QPlatformNativeInterface.
In the case of QOpenGLContext these platform APIs are backed
by the platform plugin, so dynamic_cast is used to ensure the
platform plugin supports the requested interface, but this is
and implementation detail. The interface APIs are agnostic
to where the implementation lives, while still being available
to the user as part of the APIs they extend/augment.
The documentation will be restored when the dust settles.
Task-number: QTBUG-80233
Change-Id: Iac612403383991c4b24064332542a6e4bcbb3293
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
2020-06-08 18:00:36 +00:00
|
|
|
if (!eglContext)
|
|
|
|
qFatal("Not running with EGL backend");
|
|
|
|
|
|
|
|
EGLContext shareCtx = eglContext->nativeContext();
|
2014-04-24 14:40:22 +00:00
|
|
|
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);
|
|
|
|
|
2020-07-06 14:37:47 +00:00
|
|
|
QList<EGLint> contextAttrs;
|
2014-04-24 14:40:22 +00:00
|
|
|
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.
|
Introduce platform API abstraction for QOpenGLContext
The API is available by including qopenglcontext.h as usual,
but scoped in the QPlatformInterface namespace. The namespace
exposes platform specific type-safe interfaces that provide:
a) Factory functions for adopting native contexts, e.g.
QCocoaGLContext::fromNative(nsContext, shareContext);
b) Access to underlying native handles, e.g.
openGLContext->platformInterface<QCocoaGLContext>->nativeContext()
c) Platform specific functionality, e.g.
static QWGLContext::openGLModuleHandle()
openGLContext->platformInterface<QEGLContext>->doSomething();
The platform interfaces live close to the classes they extend,
removing the need for complex indirection and plumbing, and
avoids kitchen-sink modules and APIs such as the extras modules,
QPlatformFunctions, or QPlatformNativeInterface.
In the case of QOpenGLContext these platform APIs are backed
by the platform plugin, so dynamic_cast is used to ensure the
platform plugin supports the requested interface, but this is
and implementation detail. The interface APIs are agnostic
to where the implementation lives, while still being available
to the user as part of the APIs they extend/augment.
The documentation will be restored when the dust settles.
Task-number: QTBUG-80233
Change-Id: Iac612403383991c4b24064332542a6e4bcbb3293
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
2020-06-08 18:00:36 +00:00
|
|
|
QOpenGLContext *ctxWrap = QEGLContext::fromNative(ctx, dpy, m_context);
|
2020-10-07 10:11:46 +00:00
|
|
|
Q_ASSERT(ctxWrap->nativeInterface<QEGLContext>()->nativeContext() == ctx);
|
2014-04-24 14:40:22 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|