mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-03 07:45:30 +08:00
qt 6.5.1 original
This commit is contained in:
18
tests/manual/qopenglcontext/CMakeLists.txt
Normal file
18
tests/manual/qopenglcontext/CMakeLists.txt
Normal file
@ -0,0 +1,18 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
#####################################################################
|
||||
## qopenglcontext Binary:
|
||||
#####################################################################
|
||||
|
||||
qt_internal_add_manual_test(qopenglcontext
|
||||
GUI
|
||||
SOURCES
|
||||
main.cpp
|
||||
qopenglcontextwindow.cpp qopenglcontextwindow.h
|
||||
LIBRARIES
|
||||
Qt::EglSupportPrivate
|
||||
Qt::Gui
|
||||
Qt::GuiPrivate
|
||||
Qt::OpenGL
|
||||
)
|
16
tests/manual/qopenglcontext/main.cpp
Normal file
16
tests/manual/qopenglcontext/main.cpp
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#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();
|
||||
}
|
9
tests/manual/qopenglcontext/qopenglcontext.pro
Normal file
9
tests/manual/qopenglcontext/qopenglcontext.pro
Normal file
@ -0,0 +1,9 @@
|
||||
TEMPLATE = app
|
||||
TARGET = qopenglcontext
|
||||
|
||||
QT += gui-private egl_support-private opengl
|
||||
|
||||
HEADERS += $$PWD/qopenglcontextwindow.h
|
||||
|
||||
SOURCES += $$PWD/main.cpp \
|
||||
$$PWD/qopenglcontextwindow.cpp
|
135
tests/manual/qopenglcontext/qopenglcontextwindow.cpp
Normal file
135
tests/manual/qopenglcontext/qopenglcontextwindow.cpp
Normal file
@ -0,0 +1,135 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#include "qopenglcontextwindow.h"
|
||||
#include <QtGui/QOpenGLFunctions>
|
||||
#include <QtGui/QOffscreenSurface>
|
||||
#include <QtGui/QGuiApplication>
|
||||
#include <QtGui/QMatrix4x4>
|
||||
#include <qpa/qplatformnativeinterface.h>
|
||||
|
||||
#include <QtEglSupport/private/qeglconvenience_p.h>
|
||||
|
||||
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.
|
||||
|
||||
using namespace QNativeInterface;
|
||||
auto *eglContext = m_context->nativeInterface<QEGLContext>();
|
||||
if (!eglContext)
|
||||
qFatal("Not running with EGL backend");
|
||||
|
||||
EGLContext shareCtx = eglContext->nativeContext();
|
||||
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);
|
||||
|
||||
QList<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 = QEGLContext::fromNative(ctx, dpy, m_context);
|
||||
Q_ASSERT(ctxWrap->nativeInterface<QEGLContext>()->nativeContext() == 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;
|
||||
}
|
38
tests/manual/qopenglcontext/qopenglcontextwindow.h
Normal file
38
tests/manual/qopenglcontext/qopenglcontextwindow.h
Normal file
@ -0,0 +1,38 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#ifndef QOPENGLCONTEXTWINDOW_H
|
||||
#define QOPENGLCONTEXTWINDOW_H
|
||||
|
||||
#include <QtGui/QWindow>
|
||||
#include <QtGui/QOpenGLContext>
|
||||
#include <QtOpenGL/QOpenGLTextureBlitter>
|
||||
#include <QtGui/QImage>
|
||||
#include <QtCore/QVariant>
|
||||
|
||||
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
|
BIN
tests/manual/qopenglcontext/qticon64.png
Normal file
BIN
tests/manual/qopenglcontext/qticon64.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.3 KiB |
Reference in New Issue
Block a user