Respect the QSurfaceFormat when constructing a QOpenGLContext on Mac
authorGunnar Sletta <gunnar.sletta@nokia.com>
Sat, 24 Sep 2011 09:42:12 +0000 (11:42 +0200)
committerQt by Nokia <qt-info@nokia.com>
Tue, 27 Sep 2011 03:15:38 +0000 (05:15 +0200)
Change-Id: I1a17d2e7e060d9931d84afeb0fd42bc3b1f44e5d
Reviewed-on: http://codereview.qt-project.org/5527
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Morten Johan Sørvig <morten.sorvig@nokia.com>
src/platformsupport/cglconvenience/cglconvenience.mm
src/platformsupport/cglconvenience/cglconvenience_p.h
src/plugins/platforms/cocoa/qcocoaglcontext.h
src/plugins/platforms/cocoa/qcocoaglcontext.mm

index 6b03548..280baf4 100644 (file)
@@ -42,6 +42,7 @@
 #include "cglconvenience_p.h"
 #include <QtCore/private/qcore_mac_p.h>
 #include <Cocoa/Cocoa.h>
+#include <QVector>
 
 void (*qcgl_getProcAddress(const QByteArray &procName))()
 {
@@ -81,26 +82,36 @@ QSurfaceFormat qcgl_surfaceFormat()
     return format;
 }
 
-void *qcgl_createNSOpenGLPixelFormat()
+void *qcgl_createNSOpenGLPixelFormat(const QSurfaceFormat &format)
 {
-    NSOpenGLPixelFormatAttribute attrs[] =
-    {
-        NSOpenGLPFADoubleBuffer,
-        NSOpenGLPFADepthSize, 32,
-        NSOpenGLPFAMultisample,
-        NSOpenGLPFASampleBuffers, (NSOpenGLPixelFormatAttribute)1,
-        NSOpenGLPFASamples, (NSOpenGLPixelFormatAttribute) 8,
-        0
-    };
 
-    NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
+    QVector<NSOpenGLPixelFormatAttribute> attrs;
+
+    attrs.append(NSOpenGLPFADoubleBuffer);
+
+    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.samples() > 0) {
+        attrs << NSOpenGLPFAMultisample
+              << NSOpenGLPFASampleBuffers << (NSOpenGLPixelFormatAttribute) 1
+              << NSOpenGLPFASamples << (NSOpenGLPixelFormatAttribute) format.samples();
+    }
+
+    attrs << 0;
+
+    NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs.constData()];
     return pixelFormat;
 }
 
 CGLContextObj qcgl_createGlContext()
 {
     CGLContextObj context;
-    NSOpenGLPixelFormat *format = reinterpret_cast<NSOpenGLPixelFormat *>(qcgl_createNSOpenGLPixelFormat());
+    NSOpenGLPixelFormat *format = reinterpret_cast<NSOpenGLPixelFormat *>(qcgl_createNSOpenGLPixelFormat(qcgl_surfaceFormat()));
     CGLPixelFormatObj cglFormat = static_cast<CGLPixelFormatObj>([format CGLPixelFormatObj]);
     CGLCreateContext(cglFormat ,NULL, &context);
     return context;
index 96433b6..facf8c9 100644 (file)
@@ -48,7 +48,7 @@
 
 void (*qcgl_getProcAddress(const QByteArray &procName))();
 QSurfaceFormat qcgl_surfaceFormat();
-void *qcgl_createNSOpenGLPixelFormat();
+void *qcgl_createNSOpenGLPixelFormat(const QSurfaceFormat &format);
 CGLContextObj qcgl_createGlContext();
 
 #endif // QMACGLCONVENIENCE_H
index 0cc2c2c..dc8a428 100644 (file)
@@ -68,7 +68,7 @@ public:
 
     void update();
 
-    static NSOpenGLPixelFormat *createNSOpenGLPixelFormat();
+    static NSOpenGLPixelFormat *createNSOpenGLPixelFormat(const QSurfaceFormat &format);
     NSOpenGLContext *nsOpenGLContext() const;
 
 private:
index 20b637a..bc9f55d 100644 (file)
@@ -53,7 +53,7 @@ QCocoaGLContext::QCocoaGLContext(const QSurfaceFormat &format, QPlatformOpenGLCo
 {
     QCocoaAutoReleasePool pool; // For the SG Canvas render thread.
 
-    NSOpenGLPixelFormat *pixelFormat = static_cast <NSOpenGLPixelFormat *>(qcgl_createNSOpenGLPixelFormat());
+    NSOpenGLPixelFormat *pixelFormat = static_cast <NSOpenGLPixelFormat *>(qcgl_createNSOpenGLPixelFormat(format));
     NSOpenGLContext *actualShare = share ? static_cast<QCocoaGLContext *>(share)->m_context : 0;
 
     m_context = [NSOpenGLContext alloc];
@@ -128,9 +128,9 @@ void QCocoaGLContext::update()
     [m_context update];
 }
 
-NSOpenGLPixelFormat *QCocoaGLContext::createNSOpenGLPixelFormat()
+NSOpenGLPixelFormat *QCocoaGLContext::createNSOpenGLPixelFormat(const QSurfaceFormat &format)
 {
-    return static_cast<NSOpenGLPixelFormat *>(qcgl_createNSOpenGLPixelFormat());
+    return static_cast<NSOpenGLPixelFormat *>(qcgl_createNSOpenGLPixelFormat(format));
 }
 
 NSOpenGLContext *QCocoaGLContext::nsOpenGLContext() const