Windows doesn't #define STD{IN,OUT,ERR}_FILENO
[qt-netbsd.git] / src / opengl / qgl_wince.cpp
blob53b9e277f3aeff22560c160d7a5ea9701caf0af3
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
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>
55 #include <windows.h>
57 #include "qegl_p.h"
58 #include "qgl_egl_p.h"
59 #include "qgl_cl_p.h"
62 QT_BEGIN_NAMESPACE
66 class QGLCmapPrivate
68 public:
69 QGLCmapPrivate() : count(1) { }
70 void ref() { ++count; }
71 bool deref() { return !--count; }
72 uint count;
74 enum AllocState{ UnAllocated = 0, Allocated = 0x01, Reserved = 0x02 };
76 int maxSize;
77 QVector<uint> colorArray;
78 QVector<quint8> allocArray;
79 QVector<quint8> contextArray;
80 QMap<uint,int> colorMap;
83 /*****************************************************************************
84 QColorMap class - temporarily here, until it is ready for prime time
85 *****************************************************************************/
87 /****************************************************************************
89 ** Definition of QColorMap class
91 ****************************************************************************/
93 #ifndef QGLCMAP_H
94 #define QGLCMAP_H
96 #include <qcolor.h>
101 /*****************************************************************************
102 QGLFormat Win32/WGL-specific code
103 *****************************************************************************/
105 void qt_egl_add_platform_config(QEglProperties& props, QPaintDevice *device)
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);
115 static bool opengl32dll = false;
117 bool QGLFormat::hasOpenGLOverlays()
119 return false; // ###
123 bool QGLContext::chooseContext(const QGLContext* shareContext)
125 Q_D(QGLContext);
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;
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;
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);
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;
159 // Inform the higher layers about the actual format properties.
160 qt_egl_update_format(*(d->eglContext), d->glFormat);
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;
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
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;
183 return true;
189 static bool qLogEq(bool a, bool b)
191 return (((!a) && (!b)) || (a && b));
194 int QGLContext::choosePixelFormat(void* , HDC )
197 return 0;
200 class QGLCmapPrivate;
202 class /*Q_EXPORT*/ QGLCmap
204 public:
205 enum Flags { Reserved = 0x01 };
207 QGLCmap(int maxSize = 256);
208 QGLCmap(const QGLCmap& map);
209 ~QGLCmap();
211 QGLCmap& operator=(const QGLCmap& map);
213 // isEmpty and/or isNull ?
214 int size() const;
215 int maxSize() const;
217 void resize(int newSize);
219 int find(QRgb color) const;
220 int findNearest(QRgb color) const;
221 int allocate(QRgb color, uint flags = 0, quint8 context = 0);
223 void setEntry(int idx, QRgb color, uint flags = 0, quint8 context = 0);
225 const QRgb* colors() const;
227 private:
228 void detach();
229 QGLCmapPrivate* d;
232 #endif
235 QGLCmap::QGLCmap(int maxSize) // add a bool prealloc?
237 d = new QGLCmapPrivate;
238 d->maxSize = maxSize;
241 QGLCmap::QGLCmap(const QGLCmap& map)
243 d = map.d;
244 d->ref();
247 QGLCmap::~QGLCmap()
249 if (d && d->deref())
250 delete d;
251 d = 0;
254 QGLCmap& QGLCmap::operator=(const QGLCmap& map)
256 map.d->ref();
257 if (d->deref())
258 delete d;
259 d = map.d;
260 return *this;
263 int QGLCmap::size() const
265 return d->colorArray.size();
268 int QGLCmap::maxSize() const
270 return d->maxSize;
273 void QGLCmap::detach()
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;
291 void QGLCmap::resize(int newSize)
293 if (newSize < 0 || newSize > d->maxSize) {
294 qWarning("QGLCmap::resize(): size out of range");
295 return;
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);
310 int QGLCmap::find(QRgb color) const
312 QMap<uint,int>::ConstIterator it = d->colorMap.find(color);
313 if (it != d->colorMap.end())
314 return *it;
315 return -1;
319 int QGLCmap::findNearest(QRgb color) const
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;
343 return idx;
347 // Does not always allocate; returns existing c idx if found
349 int QGLCmap::allocate(QRgb color, uint flags, quint8 context)
351 int idx = find(color);
352 if (idx >= 0)
353 return idx;
355 int mapSize = d->colorArray.size();
356 int newIdx = d->allocArray.indexOf(QGLCmapPrivate::UnAllocated);
358 if (newIdx < 0) { // Must allocate more room
359 if (mapSize < d->maxSize) {
360 newIdx = mapSize;
361 mapSize++;
362 resize(mapSize);
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;
371 d->colorArray[newIdx] = color;
372 if (flags & QGLCmap::Reserved) {
373 d->allocArray[newIdx] = QGLCmapPrivate::Reserved;
375 else {
376 d->allocArray[newIdx] = QGLCmapPrivate::Allocated;
377 d->colorMap.insert(color, newIdx);
379 d->contextArray[newIdx] = context;
380 return newIdx;
384 void QGLCmap::setEntry(int idx, QRgb color, uint flags, quint8 context)
386 if (idx < 0 || idx >= d->maxSize) {
387 qWarning("QGLCmap::set(): Index out of range");
388 return;
390 detach();
391 int mapSize = size();
392 if (idx >= mapSize) {
393 mapSize = idx + 1;
394 resize(mapSize);
396 d->colorArray[idx] = color;
397 if (flags & QGLCmap::Reserved) {
398 d->allocArray[idx] = QGLCmapPrivate::Reserved;
400 else {
401 d->allocArray[idx] = QGLCmapPrivate::Allocated;
402 d->colorMap.insert(color, idx);
404 d->contextArray[idx] = context;
408 const QRgb* QGLCmap::colors() const
410 return d->colorArray.data();
414 /*****************************************************************************
415 QGLWidget Win32/WGL-specific code
416 *****************************************************************************/
418 void QGLWidgetPrivate::init(QGLContext *ctx, const QGLWidget* shareWidget)
420 Q_Q(QGLWidget);
421 olcx = 0;
422 initContext(ctx, shareWidget);
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);
431 } else {
432 olcx = 0;
436 /*\internal
437 Store color values in the given colormap.
439 static void qStoreColors(HPALETTE cmap, const QGLColormap & cols)
441 QRgb color;
442 PALETTEENTRY pe;
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;
451 SetPaletteEntries(cmap, i, 1, &pe);
455 void QGLWidgetPrivate::updateColormap()
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);
467 bool QGLWidget::event(QEvent *e)
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);
481 } else {
482 d->olcx = 0;
484 } else if (e->type() == QEvent::Show && !format().rgba()) {
485 d->updateColormap();
488 return QWidget::event(e);
492 void QGLWidget::resizeEvent(QResizeEvent *)
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());
508 const QGLContext* QGLWidget::overlayContext() const
510 return d_func()->olcx;
514 void QGLWidget::makeOverlayCurrent()
516 Q_D(QGLWidget);
517 if (d->olcx) {
518 d->olcx->makeCurrent();
519 if (!d->olcx->initialized()) {
520 initializeOverlayGL();
521 d->olcx->setInitialized(true);
527 void QGLWidget::updateOverlayGL()
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();
537 else {
538 glFlush();
543 void QGLWidget::setContext(QGLContext *context,
544 const QGLContext* shareContext,
545 bool deleteOldContext)
547 Q_D(QGLWidget);
548 if (context == 0) {
549 qWarning("QGLWidget::setContext: Cannot set null context");
550 return;
552 if (!context->deviceIsPixmap() && context->device() != this) {
553 qWarning("QGLWidget::setContext: Context must refer to this widget");
554 return;
557 if (d->glcx)
558 d->glcx->doneCurrent();
559 QGLContext* oldcx = d->glcx;
560 d->glcx = context;
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);
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;
583 if (deleteOldContext)
584 delete oldcx;
586 if (doShow)
587 show();
591 void QGLWidgetPrivate::cleanupColormaps()
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);
601 return;
604 const QGLColormap & QGLWidget::colormap() const
606 return d_func()->cmap;
609 void QGLWidget::setColormap(const QGLColormap & c)
611 Q_D(QGLWidget);
612 d->cmap = c;
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();
627 void QGLExtensions::init()
629 static bool init_done = false;
631 if (init_done)
632 return;
633 init_done = true;
635 // We need a context current to initialize the extensions.
636 QGLWidget tmpWidget;
637 tmpWidget.makeCurrent();
639 init_extensions();
641 tmpWidget.doneCurrent();
644 QT_END_NAMESPACE