src/opengl/qgl_mac.mm
changeset 18 2f34d5167611
parent 3 41300fa6a67c
child 37 758a864f9613
--- a/src/opengl/qgl_mac.mm	Tue Feb 02 00:43:10 2010 +0200
+++ b/src/opengl/qgl_mac.mm	Fri Apr 16 15:50:13 2010 +0300
@@ -1,6 +1,6 @@
 /****************************************************************************
 **
-** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 ** All rights reserved.
 ** Contact: Nokia Corporation (qt-info@nokia.com)
 **
@@ -116,6 +116,68 @@
 extern QRegion qt_mac_convert_mac_region(RgnHandle); //qregion_mac.cpp
 extern void qt_mac_to_pascal_string(QString s, Str255 str, TextEncoding encoding=0, int len=-1);  //qglobal.cpp
 
+/*
+    QGLTemporaryContext implementation
+*/
+
+class QGLTemporaryContextPrivate
+{
+public:
+#ifndef QT_MAC_USE_COCOA
+    AGLContext ctx;
+#else
+    NSOpenGLContext *ctx;
+#endif
+};
+
+QGLTemporaryContext::QGLTemporaryContext(bool, QWidget *)
+    : d(new QGLTemporaryContextPrivate)
+{
+    d->ctx = 0;
+#ifndef QT_MAC_USE_COCOA
+    GLint attribs[] = {AGL_RGBA, AGL_NONE};
+    AGLPixelFormat fmt = aglChoosePixelFormat(0, 0, attribs);
+    if (!fmt) {
+        qDebug("QGLTemporaryContext: Couldn't find any RGB visuals");
+        return;
+    }
+    d->ctx = aglCreateContext(fmt, 0);
+    if (!d->ctx)
+        qDebug("QGLTemporaryContext: Unable to create context");
+    else
+        aglSetCurrentContext(d->ctx);
+    aglDestroyPixelFormat(fmt);
+#else
+    QMacCocoaAutoReleasePool pool;
+    NSOpenGLPixelFormatAttribute attribs[] = { 0 };
+    NSOpenGLPixelFormat *fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
+    if (!fmt) {
+        qWarning("QGLTemporaryContext: Cannot find any visuals");
+        return;
+    }
+
+    d->ctx = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:0];
+    if (!d->ctx)
+        qWarning("QGLTemporaryContext: Cannot create context");
+    else
+        [d->ctx makeCurrentContext];
+    [fmt release];
+#endif
+}
+
+QGLTemporaryContext::~QGLTemporaryContext()
+{
+    if (d->ctx) {
+#ifndef QT_MAC_USE_COCOA
+        aglSetCurrentContext(0);
+        aglDestroyContext(d->ctx);
+#else
+        [NSOpenGLContext clearCurrentContext];
+        [d->ctx release];
+#endif
+    }
+}
+
 bool QGLFormat::hasOpenGL()
 {
     return true;
@@ -476,7 +538,7 @@
     d->valid = false;
     d->transpColor = QColor();
     d->initDone = false;
-    qgl_share_reg()->removeShare(this);
+    QGLContextGroup::removeShare(this);
 }
 
 void QGLContext::makeCurrent()
@@ -606,10 +668,22 @@
             }
         }
     } else if (d->paintDevice->devType() == QInternal::Pixmap) {
-        QPixmap *pm = (QPixmap *)d->paintDevice;
-        PixMapHandle mac_pm = GetGWorldPixMap((GWorldPtr)pm->macQDHandle());
-        aglSetOffScreen((AGLContext)d->cx, pm->width(), pm->height(),
-                GetPixRowBytes(mac_pm), GetPixBaseAddr(mac_pm));
+        QPixmap *pm = reinterpret_cast<QPixmap *>(d->paintDevice);
+
+        unsigned long qdformat = k32ARGBPixelFormat;
+        if (QSysInfo::ByteOrder == QSysInfo::LittleEndian)
+            qdformat = k32BGRAPixelFormat;
+        Rect rect;
+        SetRect(&rect, 0, 0, pm->width(), pm->height());
+
+        GWorldPtr gworld;
+        NewGWorldFromPtr(&gworld, qdformat, &rect, 0, 0, 0,
+                         reinterpret_cast<char *>(qt_mac_pixmap_get_base(pm)), 
+                         qt_mac_pixmap_get_bytes_per_line(pm));
+
+        PixMapHandle pixmapHandle = GetGWorldPixMap(gworld);
+        aglSetOffScreen(reinterpret_cast<AGLContext>(d->cx), pm->width(), pm->height(),
+                        GetPixRowBytes(pixmapHandle), GetPixBaseAddr(pixmapHandle));
     } else {
         qWarning("QGLContext::updatePaintDevice(): Not sure how to render OpenGL on this device!");
     }
@@ -877,8 +951,6 @@
             break;
         current = current->parentWidget();
     }
-
-    isGLWidget = 1;
 }
 
 bool QGLWidgetPrivate::renderCxPm(QPixmap*)
@@ -906,54 +978,6 @@
     q->update();
 }
 
-
-void QGLExtensions::init()
-{
-    static bool init_done = false;
-
-    if (init_done)
-        return;
-    init_done = true;
-
-#ifndef QT_MAC_USE_COCOA
-    GLint attribs[] = { AGL_RGBA, AGL_NONE };
-    AGLPixelFormat fmt = aglChoosePixelFormat(0, 0, attribs);
-    if (!fmt) {
-        qDebug("QGLExtensions: Couldn't find any RGB visuals");
-        return;
-    }
-    AGLContext ctx = aglCreateContext(fmt, 0);
-    if (!ctx) {
-        qDebug("QGLExtensions: Unable to create context");
-    } else {
-        aglSetCurrentContext(ctx);
-        init_extensions();
-        aglSetCurrentContext(0);
-        aglDestroyContext(ctx);
-    }
-    aglDestroyPixelFormat(fmt);
-#else
-    QMacCocoaAutoReleasePool pool;
-    NSOpenGLPixelFormatAttribute attribs[] = { 0 };
-    NSOpenGLPixelFormat *fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
-    if (!fmt) {
-        qWarning("QGLExtensions: Cannot find any visuals");
-        return;
-    }
-
-    NSOpenGLContext *ctx = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:0];
-    if (!ctx) {
-        qWarning("QGLExtensions: Cannot create context");
-    } else {
-        [ctx makeCurrentContext];
-        init_extensions();
-        [NSOpenGLContext clearCurrentContext];
-        [ctx release];
-    }
-    [fmt release];
-#endif
-}
-
 #endif
 
 QT_END_NAMESPACE