Skip to content

Commit

Permalink
Fix the qt demo on Windows when building with Qt6. (#1971)
Browse files Browse the repository at this point in the history
* Fix the qt demo on Windows when building with Qt6.

* Fix the wrong display size.
  • Loading branch information
domchen authored Dec 7, 2023
1 parent 525ec2d commit 120fd5c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 43 deletions.
80 changes: 42 additions & 38 deletions qt/src/PAGView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,72 +68,75 @@ PAGView::~PAGView() {
QMetaObject::invokeMethod(renderThread, "shutDown", Qt::QueuedConnection);
renderThread->wait();
delete renderThread;
delete shareContext;
delete pagPlayer;
}

void PAGView::onCreateDrawable(QOpenGLContext* context) {
if (drawable == nullptr) {
drawable = GPUDrawable::MakeFrom(this, context);
drawable->moveToThread(renderThread);
}
}

#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))

void PAGView::geometryChange(const QRectF& newGeometry, const QRectF& oldGeometry) {
if (newGeometry == oldGeometry) {
return;
}
QQuickItem::geometryChange(newGeometry, oldGeometry);
onSizeChanged();
}

void PAGView::handleWindowChanged(QQuickWindow* window) {
if (drawable != nullptr || window == nullptr) {
return;
}
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
auto openglContext = reinterpret_cast<QOpenGLContext*>(window->rendererInterface()->getResource(
window, QSGRendererInterface::OpenGLContextResource));
#else
auto openglContext = window->openglContext();
#endif
if (openglContext != nullptr) {
onCreateDrawable(openglContext);
QMetaObject::invokeMethod(this, "createDrawable");
} else {
connect(window, SIGNAL(sceneGraphInitialized()), this, SLOT(handleOpenglContextCreated()));
connect(window, SIGNAL(sceneGraphInitialized()), this, SLOT(handleOpenglContextCreated()),
Qt::DirectConnection);
}
}

void PAGView::handleOpenglContextCreated() {
disconnect(window(), SIGNAL(sceneGraphInitialized()), this, SLOT(handleOpenglContextCreated()));
auto openglContext = reinterpret_cast<QOpenGLContext*>(window()->rendererInterface()->getResource(
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
auto context = reinterpret_cast<QOpenGLContext*>(window()->rendererInterface()->getResource(
window(), QSGRendererInterface::OpenGLContextResource));
onCreateDrawable(openglContext);
#else
auto context = window()->openglContext();
#endif
if (shareContext == nullptr) {
// Creating a context that shares with a context that is current on another thread is not safe,
// some drivers on windows can reject this.
// We work this around by introducing an additional context that lives on the same thread as the
// SceneGraph's context, shares with it, but is never current. The main thread's context will
// then share with this extra context.
shareContext = new QOpenGLContext();
shareContext->setFormat(context->format());
shareContext->setShareContext(context);
shareContext->create();
}
QMetaObject::invokeMethod(this, "createDrawable");
}

#else
void PAGView::createDrawable() {
if (drawable == nullptr) {
drawable = GPUDrawable::MakeFrom(this, shareContext);
drawable->moveToThread(renderThread);
}
}

void PAGView::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) {
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))

void PAGView::geometryChange(const QRectF& newGeometry, const QRectF& oldGeometry) {
if (newGeometry == oldGeometry) {
return;
}
QQuickItem::geometryChanged(newGeometry, oldGeometry);
QQuickItem::geometryChange(newGeometry, oldGeometry);
onSizeChanged();
}

void PAGView::handleWindowChanged(QQuickWindow* window) {
if (drawable != nullptr || window == nullptr) {
#else

void PAGView::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) {
if (newGeometry == oldGeometry) {
return;
}
if (window->openglContext() != nullptr) {
onCreateDrawable(window->openglContext());
} else {
connect(window, SIGNAL(openglContextCreated(QOpenGLContext*)), this,
SLOT(handleOpenglContextCreated(QOpenGLContext*)));
}
}

void PAGView::handleOpenglContextCreated(QOpenGLContext* context) {
disconnect(window(), SIGNAL(openglContextCreated(QOpenGLContext*)), this,
SLOT(handleOpenglContextCreated(QOpenGLContext*)));
onCreateDrawable(context);
QQuickItem::geometryChanged(newGeometry, oldGeometry);
onSizeChanged();
}

#endif
Expand All @@ -151,6 +154,7 @@ QSGNode* PAGView::updatePaintNode(QSGNode* oldNode, UpdatePaintNodeData*) {
auto pagSurface = PAGSurface::MakeFrom(drawable);
pagPlayer->setSurface(pagSurface);
lastDevicePixelRatio = window()->devicePixelRatio();
onSizeChanged();
renderThread->start();
}

Expand Down
8 changes: 3 additions & 5 deletions qt/src/PAGView.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,19 @@ class PAGView : public QQuickItem {
QString filePath = "";
PAGPlayer* pagPlayer = new PAGPlayer();
RenderThread* renderThread = nullptr;
QOpenGLContext* shareContext = nullptr;
std::shared_ptr<GPUDrawable> drawable = nullptr;

void onSizeChanged();
void onCreateDrawable(QOpenGLContext* context);

Q_SLOT
void handleWindowChanged(QQuickWindow* window);

#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
Q_SLOT
void handleOpenglContextCreated();
#else

Q_SLOT
void handleOpenglContextCreated(QOpenGLContext* context);
#endif
void createDrawable();

friend class RenderThread;
};
Expand Down

0 comments on commit 120fd5c

Please sign in to comment.