From 1d42241d3708c448579ef4744086fd5ca2a3f9dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Wed, 1 Aug 2018 12:52:00 +0200 Subject: macOS: Clean up and rename QCocoaGLContext::pixelFormatForSurfaceFormat() Change-Id: I1a439d8cec950cb23c977eedfcc1b8810c6cd1c5 Reviewed-by: Gabriel de Dietrich --- src/plugins/platforms/cocoa/qcocoaglcontext.h | 2 +- src/plugins/platforms/cocoa/qcocoaglcontext.mm | 49 ++++++++++++++------------ 2 files changed, 27 insertions(+), 24 deletions(-) (limited to 'src/plugins/platforms/cocoa') diff --git a/src/plugins/platforms/cocoa/qcocoaglcontext.h b/src/plugins/platforms/cocoa/qcocoaglcontext.h index 1bfe906942..909b1f1ee4 100644 --- a/src/plugins/platforms/cocoa/qcocoaglcontext.h +++ b/src/plugins/platforms/cocoa/qcocoaglcontext.h @@ -72,7 +72,7 @@ public: QFunctionPointer getProcAddress(const char *procName) override; private: - static NSOpenGLPixelFormat *createNSOpenGLPixelFormat(const QSurfaceFormat &format); + static NSOpenGLPixelFormat *pixelFormatForSurfaceFormat(const QSurfaceFormat &format); bool setActiveWindow(QWindow *window); void updateSurfaceFormat(); diff --git a/src/plugins/platforms/cocoa/qcocoaglcontext.mm b/src/plugins/platforms/cocoa/qcocoaglcontext.mm index 306a143f98..2c7628f397 100644 --- a/src/plugins/platforms/cocoa/qcocoaglcontext.mm +++ b/src/plugins/platforms/cocoa/qcocoaglcontext.mm @@ -113,8 +113,9 @@ QCocoaGLContext::QCocoaGLContext(const QSurfaceFormat &format, QPlatformOpenGLCo } } - // create native context for the requested pixel format and share - NSOpenGLPixelFormat *pixelFormat = createNSOpenGLPixelFormat(m_format); + // ------------------------- Create NSOpenGLContext ------------------------- + + NSOpenGLPixelFormat *pixelFormat = [pixelFormatForSurfaceFormat(m_format) autorelease]; m_context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:m_shareContext]; // retry without sharing on context creation failure. @@ -125,8 +126,6 @@ QCocoaGLContext::QCocoaGLContext(const QSurfaceFormat &format, QPlatformOpenGLCo m_context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil]; } - // give up if we still did not get a native context - [pixelFormat release]; if (!m_context) { qCWarning(lcQpaOpenGLContext, "Failed to create NSOpenGLContext"); return; @@ -148,18 +147,10 @@ QCocoaGLContext::QCocoaGLContext(const QSurfaceFormat &format, QPlatformOpenGLCo updateSurfaceFormat(); } -NSOpenGLPixelFormat *QCocoaGLContext::createNSOpenGLPixelFormat(const QSurfaceFormat &format) +NSOpenGLPixelFormat *QCocoaGLContext::pixelFormatForSurfaceFormat(const QSurfaceFormat &format) { QVector attrs; - if (format.swapBehavior() == QSurfaceFormat::DoubleBuffer - || format.swapBehavior() == QSurfaceFormat::DefaultSwapBehavior) - attrs.append(NSOpenGLPFADoubleBuffer); - else if (format.swapBehavior() == QSurfaceFormat::TripleBuffer) - attrs.append(NSOpenGLPFATripleBuffer); - - - // Select OpenGL profile attrs << NSOpenGLPFAOpenGLProfile; if (format.profile() == QSurfaceFormat::CoreProfile) { if (format.version() >= qMakePair(4, 1)) @@ -172,32 +163,45 @@ NSOpenGLPixelFormat *QCocoaGLContext::createNSOpenGLPixelFormat(const QSurfaceFo attrs << NSOpenGLProfileVersionLegacy; } + switch (format.swapBehavior()) { + case QSurfaceFormat::SingleBuffer: + break; // The NSOpenGLPixelFormat default, no attribute to set + case QSurfaceFormat::DefaultSwapBehavior: + // Technically this should be single-buffered, but we force double-buffered + // FIXME: Why do we force double-buffered? + Q_FALLTHROUGH(); + case QSurfaceFormat::DoubleBuffer: + attrs.append(NSOpenGLPFADoubleBuffer); + break; + case QSurfaceFormat::TripleBuffer: + attrs.append(NSOpenGLPFATripleBuffer); + break; + } + if (format.depthBufferSize() > 0) attrs << NSOpenGLPFADepthSize << format.depthBufferSize(); if (format.stencilBufferSize() > 0) attrs << NSOpenGLPFAStencilSize << format.stencilBufferSize(); if (format.alphaBufferSize() > 0) attrs << NSOpenGLPFAAlphaSize << format.alphaBufferSize(); - if ((format.redBufferSize() > 0) && - (format.greenBufferSize() > 0) && - (format.blueBufferSize() > 0)) { - const int colorSize = format.redBufferSize() + - format.greenBufferSize() + - format.blueBufferSize(); + if (format.redBufferSize() > 0 && format.greenBufferSize() > 0 && format.blueBufferSize() > 0) { + const int colorSize = format.redBufferSize() + format.greenBufferSize() + format.blueBufferSize(); attrs << NSOpenGLPFAColorSize << colorSize << NSOpenGLPFAMinimumPolicy; } if (format.samples() > 0) { attrs << NSOpenGLPFAMultisample - << NSOpenGLPFASampleBuffers << (NSOpenGLPixelFormatAttribute) 1 - << NSOpenGLPFASamples << (NSOpenGLPixelFormatAttribute) format.samples(); + << NSOpenGLPFASampleBuffers << NSOpenGLPixelFormatAttribute(1) + << NSOpenGLPFASamples << NSOpenGLPixelFormatAttribute(format.samples()); } if (format.stereo()) attrs << NSOpenGLPFAStereo; + // Allow rendering on GPUs without a connected display attrs << NSOpenGLPFAAllowOfflineRenderers; + // FIXME: Pull this information out of the NSView QByteArray useLayer = qgetenv("QT_MAC_WANTS_LAYER"); if (!useLayer.isEmpty() && useLayer.toInt() > 0) { // Disable the software rendering fallback. This makes compositing @@ -205,8 +209,7 @@ NSOpenGLPixelFormat *QCocoaGLContext::createNSOpenGLPixelFormat(const QSurfaceFo attrs << NSOpenGLPFANoRecovery; } - attrs << 0; - + attrs << 0; // 0-terminate array return [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs.constData()]; } -- cgit v1.2.3