src/opengl/qgl_wince.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtOpenGL module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <qgl.h>
       
    44 #include <qlist.h>
       
    45 #include <qmap.h>
       
    46 #include <qpixmap.h>
       
    47 #include <qevent.h>
       
    48 #include <private/qgl_p.h>
       
    49 #include <qcolormap.h>
       
    50 #include <qvarlengtharray.h>
       
    51 #include <qdebug.h>
       
    52 #include <qapplication.h>
       
    53 #include <qdesktopwidget>
       
    54 
       
    55 #include <windows.h>
       
    56 
       
    57 #include "qegl_p.h"
       
    58 #include "qgl_egl_p.h"
       
    59 #include "qgl_cl_p.h"
       
    60 
       
    61 
       
    62 QT_BEGIN_NAMESPACE
       
    63 
       
    64 
       
    65 
       
    66 class QGLCmapPrivate
       
    67 {
       
    68 public:
       
    69     QGLCmapPrivate() : count(1) { }
       
    70     void ref()                { ++count; }
       
    71     bool deref()        { return !--count; }
       
    72     uint count;
       
    73 
       
    74     enum AllocState{ UnAllocated = 0, Allocated = 0x01, Reserved = 0x02 };
       
    75 
       
    76     int maxSize;
       
    77     QVector<uint> colorArray;
       
    78     QVector<quint8> allocArray;
       
    79     QVector<quint8> contextArray;
       
    80     QMap<uint,int> colorMap;
       
    81 };
       
    82 
       
    83 /*****************************************************************************
       
    84   QColorMap class - temporarily here, until it is ready for prime time
       
    85  *****************************************************************************/
       
    86 
       
    87 /****************************************************************************
       
    88 **
       
    89 ** Definition of QColorMap class
       
    90 **
       
    91 ****************************************************************************/
       
    92 
       
    93 #ifndef QGLCMAP_H
       
    94 #define QGLCMAP_H
       
    95 
       
    96 #include <qcolor.h>
       
    97 
       
    98 
       
    99 
       
   100 
       
   101 /*****************************************************************************
       
   102   QGLFormat Win32/WGL-specific code
       
   103  *****************************************************************************/
       
   104 
       
   105 void qt_egl_add_platform_config(QEglProperties& props, QPaintDevice *device)
       
   106 {
       
   107     int devType = device->devType();
       
   108     if (devType == QInternal::Image)
       
   109         props.setPixelFormat(static_cast<QImage *>(device)->format());
       
   110     else
       
   111         props.setPixelFormat(QImage::Format_RGB16);
       
   112 }
       
   113 
       
   114 
       
   115 static bool opengl32dll = false;
       
   116 
       
   117 bool QGLFormat::hasOpenGLOverlays()
       
   118 {
       
   119     return false; // ###
       
   120 }
       
   121 
       
   122 
       
   123 bool QGLContext::chooseContext(const QGLContext* shareContext)
       
   124 {
       
   125         Q_D(QGLContext);
       
   126 
       
   127     // Validate the device.
       
   128     if (!device())
       
   129         return false;
       
   130     int devType = device()->devType();
       
   131     if (devType != QInternal::Pixmap && devType != QInternal::Image && devType != QInternal::Widget) {
       
   132         qWarning("QGLContext::chooseContext(): Cannot create QGLContext's for paint device type %d", devType);
       
   133         return false;
       
   134     }
       
   135 
       
   136     // Get the display and initialize it.
       
   137     d->eglContext = new QEglContext();
       
   138     d->eglContext->setApi(QEgl::OpenGL);
       
   139     if (!d->eglContext->openDisplay(device())) {
       
   140         delete d->eglContext;
       
   141         d->eglContext = 0;
       
   142         return false;
       
   143     }
       
   144 
       
   145     // Construct the configuration we need for this surface.
       
   146     QEglProperties configProps;
       
   147     qt_egl_add_platform_config(configProps, device());
       
   148     qt_egl_set_format(configProps, devType, d->glFormat);
       
   149     configProps.setRenderableType(QEgl::OpenGL);
       
   150 
       
   151     // Search for a matching configuration, reducing the complexity
       
   152     // each time until we get something that matches.
       
   153     if (!d->eglContext->chooseConfig(configProps)) {
       
   154         delete d->eglContext;
       
   155         d->eglContext = 0;
       
   156         return false;
       
   157     }
       
   158 
       
   159     // Inform the higher layers about the actual format properties.
       
   160     qt_egl_update_format(*(d->eglContext), d->glFormat);
       
   161 
       
   162     // Create a new context for the configuration.
       
   163     if (!d->eglContext->createContext
       
   164             (shareContext ? shareContext->d_func()->eglContext : 0)) {
       
   165         delete d->eglContext;
       
   166         d->eglContext = 0;
       
   167         return false;
       
   168     }
       
   169 
       
   170 #if defined(EGL_VERSION_1_1)
       
   171     if (d->glFormat.swapInterval() != -1 && devType == QInternal::Widget)
       
   172         eglSwapInterval(d->eglContext->display(), d->glFormat.swapInterval());
       
   173 #endif
       
   174 
       
   175     // Create the EGL surface to draw into.
       
   176     d->eglSurface = d->eglContext->createSurface(device());
       
   177     if (d->eglSurface == EGL_NO_SURFACE) {
       
   178         delete d->eglContext;
       
   179         d->eglContext = 0;
       
   180         return false;
       
   181     }
       
   182 
       
   183     return true;
       
   184 
       
   185 }
       
   186 
       
   187 
       
   188 
       
   189 static bool qLogEq(bool a, bool b)
       
   190 {
       
   191     return (((!a) && (!b)) || (a && b));
       
   192 }
       
   193 
       
   194 int QGLContext::choosePixelFormat(void* , HDC )
       
   195 {
       
   196 
       
   197     return 0;
       
   198 }
       
   199 
       
   200 class QGLCmapPrivate;
       
   201 
       
   202 class /*Q_EXPORT*/ QGLCmap
       
   203 {
       
   204 public:
       
   205     enum Flags { Reserved = 0x01 };
       
   206 
       
   207     QGLCmap(int maxSize = 256);
       
   208     QGLCmap(const QGLCmap& map);
       
   209     ~QGLCmap();
       
   210 
       
   211     QGLCmap& operator=(const QGLCmap& map);
       
   212 
       
   213     // isEmpty and/or isNull ?
       
   214     int size() const;
       
   215     int maxSize() const;
       
   216 
       
   217     void resize(int newSize);
       
   218 
       
   219     int find(QRgb color) const;
       
   220     int findNearest(QRgb color) const;
       
   221     int allocate(QRgb color, uint flags = 0, quint8 context = 0);
       
   222 
       
   223     void setEntry(int idx, QRgb color, uint flags = 0, quint8 context = 0);
       
   224 
       
   225     const QRgb* colors() const;
       
   226 
       
   227 private:
       
   228     void detach();
       
   229     QGLCmapPrivate* d;
       
   230 };
       
   231 
       
   232 #endif
       
   233 
       
   234 
       
   235 QGLCmap::QGLCmap(int maxSize) // add a bool prealloc?
       
   236 {
       
   237     d = new QGLCmapPrivate;
       
   238     d->maxSize = maxSize;
       
   239 }
       
   240 
       
   241 QGLCmap::QGLCmap(const QGLCmap& map)
       
   242 {
       
   243     d = map.d;
       
   244     d->ref();
       
   245 }
       
   246 
       
   247 QGLCmap::~QGLCmap()
       
   248 {
       
   249     if (d && d->deref())
       
   250         delete d;
       
   251     d = 0;
       
   252 }
       
   253 
       
   254 QGLCmap& QGLCmap::operator=(const QGLCmap& map)
       
   255 {
       
   256     map.d->ref();
       
   257     if (d->deref())
       
   258         delete d;
       
   259     d = map.d;
       
   260     return *this;
       
   261 }
       
   262 
       
   263 int QGLCmap::size() const
       
   264 {
       
   265     return d->colorArray.size();
       
   266 }
       
   267 
       
   268 int QGLCmap::maxSize() const
       
   269 {
       
   270     return d->maxSize;
       
   271 }
       
   272 
       
   273 void QGLCmap::detach()
       
   274 {
       
   275     if (d->count != 1) {
       
   276         d->deref();
       
   277         QGLCmapPrivate* newd = new QGLCmapPrivate;
       
   278         newd->maxSize = d->maxSize;
       
   279         newd->colorArray = d->colorArray;
       
   280         newd->allocArray = d->allocArray;
       
   281         newd->contextArray = d->contextArray;
       
   282         newd->colorArray.detach();
       
   283         newd->allocArray.detach();
       
   284         newd->contextArray.detach();
       
   285         newd->colorMap = d->colorMap;
       
   286         d = newd;
       
   287     }
       
   288 }
       
   289 
       
   290 
       
   291 void QGLCmap::resize(int newSize)
       
   292 {
       
   293     if (newSize < 0 || newSize > d->maxSize) {
       
   294         qWarning("QGLCmap::resize(): size out of range");
       
   295         return;
       
   296     }
       
   297     int oldSize = size();
       
   298     detach();
       
   299     //if shrinking; remove the lost elems from colorMap
       
   300     d->colorArray.resize(newSize);
       
   301     d->allocArray.resize(newSize);
       
   302     d->contextArray.resize(newSize);
       
   303     if (newSize > oldSize) {
       
   304         memset(d->allocArray.data() + oldSize, 0, newSize - oldSize);
       
   305         memset(d->contextArray.data() + oldSize, 0, newSize - oldSize);
       
   306     }
       
   307 }
       
   308 
       
   309 
       
   310 int QGLCmap::find(QRgb color) const
       
   311 {
       
   312     QMap<uint,int>::ConstIterator it = d->colorMap.find(color);
       
   313     if (it != d->colorMap.end())
       
   314         return *it;
       
   315     return -1;
       
   316 }
       
   317 
       
   318 
       
   319 int QGLCmap::findNearest(QRgb color) const
       
   320 {
       
   321     int idx = find(color);
       
   322     if (idx >= 0)
       
   323         return idx;
       
   324     int mapSize = size();
       
   325     int mindist = 200000;
       
   326     int r = qRed(color);
       
   327     int g = qGreen(color);
       
   328     int b = qBlue(color);
       
   329     int rx, gx, bx, dist;
       
   330     for (int i=0; i < mapSize; i++) {
       
   331         if (!(d->allocArray[i] & QGLCmapPrivate::Allocated))
       
   332             continue;
       
   333         QRgb ci = d->colorArray[i];
       
   334         rx = r - qRed(ci);
       
   335         gx = g - qGreen(ci);
       
   336         bx = b - qBlue(ci);
       
   337         dist = rx*rx + gx*gx + bx*bx;                // calculate distance
       
   338         if (dist < mindist) {                        // minimal?
       
   339             mindist = dist;
       
   340             idx = i;
       
   341         }
       
   342     }
       
   343     return idx;
       
   344 }
       
   345 
       
   346 
       
   347 // Does not always allocate; returns existing c idx if found
       
   348 
       
   349 int QGLCmap::allocate(QRgb color, uint flags, quint8 context)
       
   350 {
       
   351     int idx = find(color);
       
   352     if (idx >= 0)
       
   353         return idx;
       
   354 
       
   355     int mapSize = d->colorArray.size();
       
   356     int newIdx = d->allocArray.indexOf(QGLCmapPrivate::UnAllocated);
       
   357 
       
   358     if (newIdx < 0) {                        // Must allocate more room
       
   359         if (mapSize < d->maxSize) {
       
   360             newIdx = mapSize;
       
   361             mapSize++;
       
   362             resize(mapSize);
       
   363         }
       
   364         else {
       
   365             //# add a bool param that says what to do in case no more room -
       
   366             // fail (-1) or return nearest?
       
   367             return -1;
       
   368         }
       
   369     }
       
   370 
       
   371     d->colorArray[newIdx] = color;
       
   372     if (flags & QGLCmap::Reserved) {
       
   373         d->allocArray[newIdx] = QGLCmapPrivate::Reserved;
       
   374     }
       
   375     else {
       
   376         d->allocArray[newIdx] = QGLCmapPrivate::Allocated;
       
   377         d->colorMap.insert(color, newIdx);
       
   378     }
       
   379     d->contextArray[newIdx] = context;
       
   380     return newIdx;
       
   381 }
       
   382 
       
   383 
       
   384 void QGLCmap::setEntry(int idx, QRgb color, uint flags, quint8 context)
       
   385 {
       
   386     if (idx < 0 || idx >= d->maxSize) {
       
   387         qWarning("QGLCmap::set(): Index out of range");
       
   388         return;
       
   389     }
       
   390     detach();
       
   391     int mapSize = size();
       
   392     if (idx >= mapSize) {
       
   393         mapSize = idx + 1;
       
   394         resize(mapSize);
       
   395     }
       
   396     d->colorArray[idx] = color;
       
   397     if (flags & QGLCmap::Reserved) {
       
   398         d->allocArray[idx] = QGLCmapPrivate::Reserved;
       
   399     }
       
   400     else {
       
   401         d->allocArray[idx] = QGLCmapPrivate::Allocated;
       
   402         d->colorMap.insert(color, idx);
       
   403     }
       
   404     d->contextArray[idx] = context;
       
   405 }
       
   406 
       
   407 
       
   408 const QRgb* QGLCmap::colors() const
       
   409 {
       
   410     return d->colorArray.data();
       
   411 }
       
   412 
       
   413 
       
   414 /*****************************************************************************
       
   415   QGLWidget Win32/WGL-specific code
       
   416  *****************************************************************************/
       
   417 
       
   418 void QGLWidgetPrivate::init(QGLContext *ctx, const QGLWidget* shareWidget)
       
   419 {
       
   420     Q_Q(QGLWidget);
       
   421     olcx = 0;
       
   422     initContext(ctx, shareWidget);
       
   423 
       
   424     if (q->isValid() && q->context()->format().hasOverlay()) {
       
   425         olcx = new QGLContext(QGLFormat::defaultOverlayFormat(), q);
       
   426         if (!olcx->create(shareWidget ? shareWidget->overlayContext() : 0)) {
       
   427             delete olcx;
       
   428             olcx = 0;
       
   429             glcx->d_func()->glFormat.setOverlay(false);
       
   430         }
       
   431     } else {
       
   432         olcx = 0;
       
   433     }
       
   434 }
       
   435 
       
   436 /*\internal
       
   437   Store color values in the given colormap.
       
   438 */
       
   439 static void qStoreColors(HPALETTE cmap, const QGLColormap & cols)
       
   440 {
       
   441     QRgb color;
       
   442     PALETTEENTRY pe;
       
   443 
       
   444     for (int i = 0; i < cols.size(); i++) {
       
   445         color = cols.entryRgb(i);
       
   446         pe.peRed   = qRed(color);
       
   447         pe.peGreen = qGreen(color);
       
   448         pe.peBlue  = qBlue(color);
       
   449         pe.peFlags = 0;
       
   450 
       
   451         SetPaletteEntries(cmap, i, 1, &pe);
       
   452     }
       
   453 }
       
   454 
       
   455 void QGLWidgetPrivate::updateColormap()
       
   456 {
       
   457     Q_Q(QGLWidget);
       
   458     if (!cmap.handle())
       
   459         return;
       
   460     HDC hdc = GetDC(q->winId());
       
   461     SelectPalette(hdc, (HPALETTE) cmap.handle(), TRUE);
       
   462     qStoreColors((HPALETTE) cmap.handle(), cmap);
       
   463     RealizePalette(hdc);
       
   464     ReleaseDC(q->winId(), hdc);
       
   465 }
       
   466 
       
   467 bool QGLWidget::event(QEvent *e)
       
   468 {
       
   469     Q_D(QGLWidget);
       
   470     if (e->type() == QEvent::ParentChange) {
       
   471         setContext(new QGLContext(d->glcx->requestedFormat(), this));
       
   472         // the overlay needs to be recreated as well
       
   473         delete d->olcx;
       
   474         if (isValid() && context()->format().hasOverlay()) {
       
   475             d->olcx = new QGLContext(QGLFormat::defaultOverlayFormat(), this);
       
   476             if (!d->olcx->create(isSharing() ? d->glcx : 0)) {
       
   477                 delete d->olcx;
       
   478                 d->olcx = 0;
       
   479                 d->glcx->d_func()->glFormat.setOverlay(false);
       
   480             }
       
   481         } else {
       
   482             d->olcx = 0;
       
   483         }
       
   484     } else if (e->type() == QEvent::Show && !format().rgba()) {
       
   485         d->updateColormap();
       
   486     }
       
   487 
       
   488     return QWidget::event(e);
       
   489 }
       
   490 
       
   491 
       
   492 void QGLWidget::resizeEvent(QResizeEvent *)
       
   493 {
       
   494     Q_D(QGLWidget);
       
   495     if (!isValid())
       
   496         return;
       
   497     makeCurrent();
       
   498     if (!d->glcx->initialized())
       
   499         glInit();
       
   500     resizeGL(width(), height());
       
   501     if (d->olcx) {
       
   502         makeOverlayCurrent();
       
   503         resizeOverlayGL(width(), height());
       
   504     }
       
   505 }
       
   506 
       
   507 
       
   508 const QGLContext* QGLWidget::overlayContext() const
       
   509 {
       
   510     return d_func()->olcx;
       
   511 }
       
   512 
       
   513 
       
   514 void QGLWidget::makeOverlayCurrent()
       
   515 {
       
   516     Q_D(QGLWidget);
       
   517     if (d->olcx) {
       
   518         d->olcx->makeCurrent();
       
   519         if (!d->olcx->initialized()) {
       
   520             initializeOverlayGL();
       
   521             d->olcx->setInitialized(true);
       
   522         }
       
   523     }
       
   524 }
       
   525 
       
   526 
       
   527 void QGLWidget::updateOverlayGL()
       
   528 {
       
   529     Q_D(QGLWidget);
       
   530     if (d->olcx) {
       
   531         makeOverlayCurrent();
       
   532         paintOverlayGL();
       
   533         if (d->olcx->format().doubleBuffer()) {
       
   534             if (d->autoSwap)
       
   535                 d->olcx->swapBuffers();
       
   536         }
       
   537         else {
       
   538             glFlush();
       
   539         }
       
   540     }
       
   541 }
       
   542 
       
   543 void QGLWidget::setContext(QGLContext *context,
       
   544                             const QGLContext* shareContext,
       
   545                             bool deleteOldContext)
       
   546 {
       
   547     Q_D(QGLWidget);
       
   548     if (context == 0) {
       
   549         qWarning("QGLWidget::setContext: Cannot set null context");
       
   550         return;
       
   551     }
       
   552     if (!context->deviceIsPixmap() && context->device() != this) {
       
   553         qWarning("QGLWidget::setContext: Context must refer to this widget");
       
   554         return;
       
   555     }
       
   556 
       
   557     if (d->glcx)
       
   558         d->glcx->doneCurrent();
       
   559     QGLContext* oldcx = d->glcx;
       
   560     d->glcx = context;
       
   561 
       
   562     bool doShow = false;
       
   563     if (oldcx && oldcx->d_func()->win == winId() && !d->glcx->deviceIsPixmap()) {
       
   564         // We already have a context and must therefore create a new
       
   565         // window since Windows does not permit setting a new OpenGL
       
   566         // context for a window that already has one set.
       
   567         doShow = isVisible();
       
   568         QWidget *pW = static_cast<QWidget *>(parent());
       
   569         QPoint pos = geometry().topLeft();
       
   570         setParent(pW, windowFlags());
       
   571         move(pos);
       
   572     }
       
   573 
       
   574     if (!d->glcx->isValid()) {
       
   575         d->glcx->create(shareContext ? shareContext : oldcx);
       
   576         // the above is a trick to keep disp lists etc when a
       
   577         // QGLWidget has been reparented, so remove the sharing
       
   578         // flag if we don't actually have a sharing context.
       
   579         if (!shareContext)
       
   580             d->glcx->d_ptr->sharing = false;
       
   581     }
       
   582 
       
   583     if (deleteOldContext)
       
   584         delete oldcx;
       
   585 
       
   586     if (doShow)
       
   587         show();
       
   588 }
       
   589 
       
   590 
       
   591 void QGLWidgetPrivate::cleanupColormaps()
       
   592 {
       
   593     Q_Q(QGLWidget);
       
   594     if (cmap.handle()) {
       
   595         HDC hdc = GetDC(q->winId());
       
   596         SelectPalette(hdc, (HPALETTE) GetStockObject(DEFAULT_PALETTE), FALSE);
       
   597         DeleteObject((HPALETTE) cmap.handle());
       
   598         ReleaseDC(q->winId(), hdc);
       
   599         cmap.setHandle(0);
       
   600     }
       
   601     return;
       
   602 }
       
   603 
       
   604 const QGLColormap & QGLWidget::colormap() const
       
   605 {
       
   606     return d_func()->cmap;
       
   607 }
       
   608 
       
   609 void QGLWidget::setColormap(const QGLColormap & c)
       
   610 {
       
   611     Q_D(QGLWidget);
       
   612     d->cmap = c;
       
   613 
       
   614     if (d->cmap.handle()) { // already have an allocated cmap
       
   615         d->updateColormap();
       
   616     } else {
       
   617         LOGPALETTE *lpal = (LOGPALETTE *) malloc(sizeof(LOGPALETTE)
       
   618                                                  +c.size()*sizeof(PALETTEENTRY));
       
   619         lpal->palVersion    = 0x300;
       
   620         lpal->palNumEntries = c.size();
       
   621         d->cmap.setHandle(CreatePalette(lpal));
       
   622         free(lpal);
       
   623         d->updateColormap();
       
   624     }
       
   625 }
       
   626 
       
   627 void QGLExtensions::init()
       
   628 {
       
   629     static bool init_done = false;
       
   630 
       
   631     if (init_done)
       
   632         return;
       
   633     init_done = true;
       
   634 
       
   635     // We need a context current to initialize the extensions.
       
   636     QGLWidget tmpWidget;
       
   637     tmpWidget.makeCurrent();
       
   638 
       
   639     init_extensions();
       
   640 
       
   641     tmpWidget.doneCurrent();
       
   642 }
       
   643 
       
   644 QT_END_NAMESPACE