From 1696091c5b0f2eae4d25f671f3b7eabd0d384a73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Mon, 28 Oct 2024 16:05:10 +0100 Subject: [PATCH] macOS: Update NSOpenGLContext drawable on beginning of frame if needed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the size or DPR of the window we're drawing to with QOpenGLContext changes we need to call [NSOpenGLContext update] to recreate the backing texture. We were doing this in QCocoaGLContext::makeCurrent(), based on the assumption that clients would always call makeCurrent() before drawing, which is a documented requirement of QOpenGLContext: "To be able to support certain platforms, QOpenGLContext requires that you call makeCurrent() again before starting rendering a new frame, after calling swapBuffers()." However RHI doesn't follow this rule, and skips makeCurrent() from QRhiGles2::ensureContext() if the context is already current. This was causing issues when moving a window between screens with different device-pixel-ratios, as we were correctly rendering to the window with the updated DPR, but we had not resized the target texture to match. Changing QRhiGles2::ensureContext() might be an option, but nowadays we have QPlatformOpenGLContext::beginFrame(), which is guaranteed to be called (by RHI, not by generic QOpenGLContext clients). To cater for RHI we now call [NSOpenGLContext update] also from the beginFrame callback. Fixes: QTBUG-114873 Pick-to: 6.5 Change-Id: I9461135109fb1473ef9965f72f52beeb41b03680 Reviewed-by: Morten Johan Sørvig (cherry picked from commit 48c176e1b891428af504087b117a72bcaa806930) Reviewed-by: Qt Cherry-pick Bot --- src/plugins/platforms/cocoa/qcocoaglcontext.h | 1 + .../platforms/cocoa/qcocoaglcontext.mm | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/plugins/platforms/cocoa/qcocoaglcontext.h b/src/plugins/platforms/cocoa/qcocoaglcontext.h index 919f7c240bc..36546879ae4 100644 --- a/src/plugins/platforms/cocoa/qcocoaglcontext.h +++ b/src/plugins/platforms/cocoa/qcocoaglcontext.h @@ -29,6 +29,7 @@ public: void initialize() override; bool makeCurrent(QPlatformSurface *surface) override; + void beginFrame() override; void swapBuffers(QPlatformSurface *surface) override; void doneCurrent() override; diff --git a/src/plugins/platforms/cocoa/qcocoaglcontext.mm b/src/plugins/platforms/cocoa/qcocoaglcontext.mm index a65311175ff..bfac716b633 100644 --- a/src/plugins/platforms/cocoa/qcocoaglcontext.mm +++ b/src/plugins/platforms/cocoa/qcocoaglcontext.mm @@ -332,6 +332,25 @@ bool QCocoaGLContext::makeCurrent(QPlatformSurface *surface) return true; } +void QCocoaGLContext::beginFrame() +{ + QMacAutoReleasePool pool; + + Q_ASSERT(context() && context()->surface()); + auto *surface = context()->surface()->surfaceHandle(); + Q_ASSERT(surface); + + qCDebug(lcQpaOpenGLContext) << "Beginning frame for" << this + << "in" << QThread::currentThread() << "for" << surface; + + Q_ASSERT(surface->surface()->supportsOpenGL()); + + if (surface->surface()->surfaceClass() == QSurface::Window) { + if (m_needsUpdate.fetchAndStoreRelaxed(false)) + update(); + } +} + /*! Sets the drawable object of the NSOpenGLContext, which is the frame buffer that is the target of OpenGL drawing operations.