src/opengl/qgl_mac.mm
changeset 7 f7bc934e204c
parent 3 41300fa6a67c
child 37 758a864f9613
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
     1 /****************************************************************************
     1 /****************************************************************************
     2 **
     2 **
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     4 ** All rights reserved.
     4 ** All rights reserved.
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 **
     6 **
     7 ** This file is part of the QtOpenGL module of the Qt Toolkit.
     7 ** This file is part of the QtOpenGL module of the Qt Toolkit.
     8 **
     8 **
   113 extern int qt_mac_pixmap_get_bytes_per_line(const QPixmap *);
   113 extern int qt_mac_pixmap_get_bytes_per_line(const QPixmap *);
   114 extern RgnHandle qt_mac_get_rgn(); //qregion_mac.cpp
   114 extern RgnHandle qt_mac_get_rgn(); //qregion_mac.cpp
   115 extern void qt_mac_dispose_rgn(RgnHandle); //qregion_mac.cpp
   115 extern void qt_mac_dispose_rgn(RgnHandle); //qregion_mac.cpp
   116 extern QRegion qt_mac_convert_mac_region(RgnHandle); //qregion_mac.cpp
   116 extern QRegion qt_mac_convert_mac_region(RgnHandle); //qregion_mac.cpp
   117 extern void qt_mac_to_pascal_string(QString s, Str255 str, TextEncoding encoding=0, int len=-1);  //qglobal.cpp
   117 extern void qt_mac_to_pascal_string(QString s, Str255 str, TextEncoding encoding=0, int len=-1);  //qglobal.cpp
       
   118 
       
   119 /*
       
   120     QGLTemporaryContext implementation
       
   121 */
       
   122 
       
   123 class QGLTemporaryContextPrivate
       
   124 {
       
   125 public:
       
   126 #ifndef QT_MAC_USE_COCOA
       
   127     AGLContext ctx;
       
   128 #else
       
   129     NSOpenGLContext *ctx;
       
   130 #endif
       
   131 };
       
   132 
       
   133 QGLTemporaryContext::QGLTemporaryContext(bool, QWidget *)
       
   134     : d(new QGLTemporaryContextPrivate)
       
   135 {
       
   136     d->ctx = 0;
       
   137 #ifndef QT_MAC_USE_COCOA
       
   138     GLint attribs[] = {AGL_RGBA, AGL_NONE};
       
   139     AGLPixelFormat fmt = aglChoosePixelFormat(0, 0, attribs);
       
   140     if (!fmt) {
       
   141         qDebug("QGLTemporaryContext: Couldn't find any RGB visuals");
       
   142         return;
       
   143     }
       
   144     d->ctx = aglCreateContext(fmt, 0);
       
   145     if (!d->ctx)
       
   146         qDebug("QGLTemporaryContext: Unable to create context");
       
   147     else
       
   148         aglSetCurrentContext(d->ctx);
       
   149     aglDestroyPixelFormat(fmt);
       
   150 #else
       
   151     QMacCocoaAutoReleasePool pool;
       
   152     NSOpenGLPixelFormatAttribute attribs[] = { 0 };
       
   153     NSOpenGLPixelFormat *fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
       
   154     if (!fmt) {
       
   155         qWarning("QGLTemporaryContext: Cannot find any visuals");
       
   156         return;
       
   157     }
       
   158 
       
   159     d->ctx = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:0];
       
   160     if (!d->ctx)
       
   161         qWarning("QGLTemporaryContext: Cannot create context");
       
   162     else
       
   163         [d->ctx makeCurrentContext];
       
   164     [fmt release];
       
   165 #endif
       
   166 }
       
   167 
       
   168 QGLTemporaryContext::~QGLTemporaryContext()
       
   169 {
       
   170     if (d->ctx) {
       
   171 #ifndef QT_MAC_USE_COCOA
       
   172         aglSetCurrentContext(0);
       
   173         aglDestroyContext(d->ctx);
       
   174 #else
       
   175         [NSOpenGLContext clearCurrentContext];
       
   176         [d->ctx release];
       
   177 #endif
       
   178     }
       
   179 }
   118 
   180 
   119 bool QGLFormat::hasOpenGL()
   181 bool QGLFormat::hasOpenGL()
   120 {
   182 {
   121     return true;
   183     return true;
   122 }
   184 }
   474     d->crWin = false;
   536     d->crWin = false;
   475     d->sharing = false;
   537     d->sharing = false;
   476     d->valid = false;
   538     d->valid = false;
   477     d->transpColor = QColor();
   539     d->transpColor = QColor();
   478     d->initDone = false;
   540     d->initDone = false;
   479     qgl_share_reg()->removeShare(this);
   541     QGLContextGroup::removeShare(this);
   480 }
   542 }
   481 
   543 
   482 void QGLContext::makeCurrent()
   544 void QGLContext::makeCurrent()
   483 {
   545 {
   484     Q_D(QGLContext);
   546     Q_D(QGLContext);
   604                 const GLint offs[4] = { 0, 0,  w->width() * scale , w->height() * scale};
   666                 const GLint offs[4] = { 0, 0,  w->width() * scale , w->height() * scale};
   605                 aglSetInteger((AGLContext)d->cx, AGL_BUFFER_RECT, offs);
   667                 aglSetInteger((AGLContext)d->cx, AGL_BUFFER_RECT, offs);
   606             }
   668             }
   607         }
   669         }
   608     } else if (d->paintDevice->devType() == QInternal::Pixmap) {
   670     } else if (d->paintDevice->devType() == QInternal::Pixmap) {
   609         QPixmap *pm = (QPixmap *)d->paintDevice;
   671         QPixmap *pm = reinterpret_cast<QPixmap *>(d->paintDevice);
   610         PixMapHandle mac_pm = GetGWorldPixMap((GWorldPtr)pm->macQDHandle());
   672 
   611         aglSetOffScreen((AGLContext)d->cx, pm->width(), pm->height(),
   673         unsigned long qdformat = k32ARGBPixelFormat;
   612                 GetPixRowBytes(mac_pm), GetPixBaseAddr(mac_pm));
   674         if (QSysInfo::ByteOrder == QSysInfo::LittleEndian)
       
   675             qdformat = k32BGRAPixelFormat;
       
   676         Rect rect;
       
   677         SetRect(&rect, 0, 0, pm->width(), pm->height());
       
   678 
       
   679         GWorldPtr gworld;
       
   680         NewGWorldFromPtr(&gworld, qdformat, &rect, 0, 0, 0,
       
   681                          reinterpret_cast<char *>(qt_mac_pixmap_get_base(pm)), 
       
   682                          qt_mac_pixmap_get_bytes_per_line(pm));
       
   683 
       
   684         PixMapHandle pixmapHandle = GetGWorldPixMap(gworld);
       
   685         aglSetOffScreen(reinterpret_cast<AGLContext>(d->cx), pm->width(), pm->height(),
       
   686                         GetPixRowBytes(pixmapHandle), GetPixBaseAddr(pixmapHandle));
   613     } else {
   687     } else {
   614         qWarning("QGLContext::updatePaintDevice(): Not sure how to render OpenGL on this device!");
   688         qWarning("QGLContext::updatePaintDevice(): Not sure how to render OpenGL on this device!");
   615     }
   689     }
   616     aglUpdateContext((AGLContext)d->cx);
   690     aglUpdateContext((AGLContext)d->cx);
   617 
   691 
   875         qt_widget_private(current)->glWidgets.append(QWidgetPrivate::GlWidgetInfo(q));
   949         qt_widget_private(current)->glWidgets.append(QWidgetPrivate::GlWidgetInfo(q));
   876         if (current->isWindow())
   950         if (current->isWindow())
   877             break;
   951             break;
   878         current = current->parentWidget();
   952         current = current->parentWidget();
   879     }
   953     }
   880 
       
   881     isGLWidget = 1;
       
   882 }
   954 }
   883 
   955 
   884 bool QGLWidgetPrivate::renderCxPm(QPixmap*)
   956 bool QGLWidgetPrivate::renderCxPm(QPixmap*)
   885 {
   957 {
   886     return false;
   958     return false;
   904     Q_Q(QGLWidget);
   976     Q_Q(QGLWidget);
   905     glcx->updatePaintDevice();
   977     glcx->updatePaintDevice();
   906     q->update();
   978     q->update();
   907 }
   979 }
   908 
   980 
   909 
       
   910 void QGLExtensions::init()
       
   911 {
       
   912     static bool init_done = false;
       
   913 
       
   914     if (init_done)
       
   915         return;
       
   916     init_done = true;
       
   917 
       
   918 #ifndef QT_MAC_USE_COCOA
       
   919     GLint attribs[] = { AGL_RGBA, AGL_NONE };
       
   920     AGLPixelFormat fmt = aglChoosePixelFormat(0, 0, attribs);
       
   921     if (!fmt) {
       
   922         qDebug("QGLExtensions: Couldn't find any RGB visuals");
       
   923         return;
       
   924     }
       
   925     AGLContext ctx = aglCreateContext(fmt, 0);
       
   926     if (!ctx) {
       
   927         qDebug("QGLExtensions: Unable to create context");
       
   928     } else {
       
   929         aglSetCurrentContext(ctx);
       
   930         init_extensions();
       
   931         aglSetCurrentContext(0);
       
   932         aglDestroyContext(ctx);
       
   933     }
       
   934     aglDestroyPixelFormat(fmt);
       
   935 #else
       
   936     QMacCocoaAutoReleasePool pool;
       
   937     NSOpenGLPixelFormatAttribute attribs[] = { 0 };
       
   938     NSOpenGLPixelFormat *fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
       
   939     if (!fmt) {
       
   940         qWarning("QGLExtensions: Cannot find any visuals");
       
   941         return;
       
   942     }
       
   943 
       
   944     NSOpenGLContext *ctx = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:0];
       
   945     if (!ctx) {
       
   946         qWarning("QGLExtensions: Cannot create context");
       
   947     } else {
       
   948         [ctx makeCurrentContext];
       
   949         init_extensions();
       
   950         [NSOpenGLContext clearCurrentContext];
       
   951         [ctx release];
       
   952     }
       
   953     [fmt release];
       
   954 #endif
       
   955 }
       
   956 
       
   957 #endif
   981 #endif
   958 
   982 
   959 QT_END_NAMESPACE
   983 QT_END_NAMESPACE