source: trunk/src/opengl/qgl_wince.cpp@ 561

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

  • Property svn:eol-style set to native
File size: 16.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtOpenGL module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
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 <private/qegl_p.h>
58#include <private/qgl_egl_p.h>
59#include <private/qgl_cl_p.h>
60
61
62QT_BEGIN_NAMESPACE
63
64
65
66class QGLCmapPrivate
67{
68public:
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
105void 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
115static bool opengl32dll = false;
116
117bool QGLFormat::hasOpenGLOverlays()
118{
119 return false; // ###
120}
121
122
123bool 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 d->sharing = d->eglContext->isSharing();
170 if (d->sharing && shareContext)
171 const_cast<QGLContext *>(shareContext)->d_func()->sharing = true;
172
173#if defined(EGL_VERSION_1_1)
174 if (d->glFormat.swapInterval() != -1 && devType == QInternal::Widget)
175 eglSwapInterval(d->eglContext->display(), d->glFormat.swapInterval());
176#endif
177
178 // Create the EGL surface to draw into.
179 d->eglSurface = d->eglContext->createSurface(device());
180 if (d->eglSurface == EGL_NO_SURFACE) {
181 delete d->eglContext;
182 d->eglContext = 0;
183 return false;
184 }
185
186 return true;
187
188}
189
190
191
192static bool qLogEq(bool a, bool b)
193{
194 return (((!a) && (!b)) || (a && b));
195}
196
197int QGLContext::choosePixelFormat(void* , HDC )
198{
199
200 return 0;
201}
202
203class QGLCmapPrivate;
204
205class /*Q_EXPORT*/ QGLCmap
206{
207public:
208 enum Flags { Reserved = 0x01 };
209
210 QGLCmap(int maxSize = 256);
211 QGLCmap(const QGLCmap& map);
212 ~QGLCmap();
213
214 QGLCmap& operator=(const QGLCmap& map);
215
216 // isEmpty and/or isNull ?
217 int size() const;
218 int maxSize() const;
219
220 void resize(int newSize);
221
222 int find(QRgb color) const;
223 int findNearest(QRgb color) const;
224 int allocate(QRgb color, uint flags = 0, quint8 context = 0);
225
226 void setEntry(int idx, QRgb color, uint flags = 0, quint8 context = 0);
227
228 const QRgb* colors() const;
229
230private:
231 void detach();
232 QGLCmapPrivate* d;
233};
234
235#endif
236
237
238QGLCmap::QGLCmap(int maxSize) // add a bool prealloc?
239{
240 d = new QGLCmapPrivate;
241 d->maxSize = maxSize;
242}
243
244QGLCmap::QGLCmap(const QGLCmap& map)
245{
246 d = map.d;
247 d->ref();
248}
249
250QGLCmap::~QGLCmap()
251{
252 if (d && d->deref())
253 delete d;
254 d = 0;
255}
256
257QGLCmap& QGLCmap::operator=(const QGLCmap& map)
258{
259 map.d->ref();
260 if (d->deref())
261 delete d;
262 d = map.d;
263 return *this;
264}
265
266int QGLCmap::size() const
267{
268 return d->colorArray.size();
269}
270
271int QGLCmap::maxSize() const
272{
273 return d->maxSize;
274}
275
276void QGLCmap::detach()
277{
278 if (d->count != 1) {
279 d->deref();
280 QGLCmapPrivate* newd = new QGLCmapPrivate;
281 newd->maxSize = d->maxSize;
282 newd->colorArray = d->colorArray;
283 newd->allocArray = d->allocArray;
284 newd->contextArray = d->contextArray;
285 newd->colorArray.detach();
286 newd->allocArray.detach();
287 newd->contextArray.detach();
288 newd->colorMap = d->colorMap;
289 d = newd;
290 }
291}
292
293
294void QGLCmap::resize(int newSize)
295{
296 if (newSize < 0 || newSize > d->maxSize) {
297 qWarning("QGLCmap::resize(): size out of range");
298 return;
299 }
300 int oldSize = size();
301 detach();
302 //if shrinking; remove the lost elems from colorMap
303 d->colorArray.resize(newSize);
304 d->allocArray.resize(newSize);
305 d->contextArray.resize(newSize);
306 if (newSize > oldSize) {
307 memset(d->allocArray.data() + oldSize, 0, newSize - oldSize);
308 memset(d->contextArray.data() + oldSize, 0, newSize - oldSize);
309 }
310}
311
312
313int QGLCmap::find(QRgb color) const
314{
315 QMap<uint,int>::ConstIterator it = d->colorMap.find(color);
316 if (it != d->colorMap.end())
317 return *it;
318 return -1;
319}
320
321
322int QGLCmap::findNearest(QRgb color) const
323{
324 int idx = find(color);
325 if (idx >= 0)
326 return idx;
327 int mapSize = size();
328 int mindist = 200000;
329 int r = qRed(color);
330 int g = qGreen(color);
331 int b = qBlue(color);
332 int rx, gx, bx, dist;
333 for (int i=0; i < mapSize; i++) {
334 if (!(d->allocArray[i] & QGLCmapPrivate::Allocated))
335 continue;
336 QRgb ci = d->colorArray[i];
337 rx = r - qRed(ci);
338 gx = g - qGreen(ci);
339 bx = b - qBlue(ci);
340 dist = rx*rx + gx*gx + bx*bx; // calculate distance
341 if (dist < mindist) { // minimal?
342 mindist = dist;
343 idx = i;
344 }
345 }
346 return idx;
347}
348
349
350// Does not always allocate; returns existing c idx if found
351
352int QGLCmap::allocate(QRgb color, uint flags, quint8 context)
353{
354 int idx = find(color);
355 if (idx >= 0)
356 return idx;
357
358 int mapSize = d->colorArray.size();
359 int newIdx = d->allocArray.indexOf(QGLCmapPrivate::UnAllocated);
360
361 if (newIdx < 0) { // Must allocate more room
362 if (mapSize < d->maxSize) {
363 newIdx = mapSize;
364 mapSize++;
365 resize(mapSize);
366 }
367 else {
368 //# add a bool param that says what to do in case no more room -
369 // fail (-1) or return nearest?
370 return -1;
371 }
372 }
373
374 d->colorArray[newIdx] = color;
375 if (flags & QGLCmap::Reserved) {
376 d->allocArray[newIdx] = QGLCmapPrivate::Reserved;
377 }
378 else {
379 d->allocArray[newIdx] = QGLCmapPrivate::Allocated;
380 d->colorMap.insert(color, newIdx);
381 }
382 d->contextArray[newIdx] = context;
383 return newIdx;
384}
385
386
387void QGLCmap::setEntry(int idx, QRgb color, uint flags, quint8 context)
388{
389 if (idx < 0 || idx >= d->maxSize) {
390 qWarning("QGLCmap::set(): Index out of range");
391 return;
392 }
393 detach();
394 int mapSize = size();
395 if (idx >= mapSize) {
396 mapSize = idx + 1;
397 resize(mapSize);
398 }
399 d->colorArray[idx] = color;
400 if (flags & QGLCmap::Reserved) {
401 d->allocArray[idx] = QGLCmapPrivate::Reserved;
402 }
403 else {
404 d->allocArray[idx] = QGLCmapPrivate::Allocated;
405 d->colorMap.insert(color, idx);
406 }
407 d->contextArray[idx] = context;
408}
409
410
411const QRgb* QGLCmap::colors() const
412{
413 return d->colorArray.data();
414}
415
416
417/*****************************************************************************
418 QGLWidget Win32/WGL-specific code
419 *****************************************************************************/
420
421void QGLWidgetPrivate::init(QGLContext *ctx, const QGLWidget* shareWidget)
422{
423 Q_Q(QGLWidget);
424 olcx = 0;
425 initContext(ctx, shareWidget);
426
427 if (q->isValid() && q->context()->format().hasOverlay()) {
428 olcx = new QGLContext(QGLFormat::defaultOverlayFormat(), q);
429 if (!olcx->create(shareWidget ? shareWidget->overlayContext() : 0)) {
430 delete olcx;
431 olcx = 0;
432 glcx->d_func()->glFormat.setOverlay(false);
433 }
434 } else {
435 olcx = 0;
436 }
437}
438
439/*\internal
440 Store color values in the given colormap.
441*/
442static void qStoreColors(HPALETTE cmap, const QGLColormap & cols)
443{
444 QRgb color;
445 PALETTEENTRY pe;
446
447 for (int i = 0; i < cols.size(); i++) {
448 color = cols.entryRgb(i);
449 pe.peRed = qRed(color);
450 pe.peGreen = qGreen(color);
451 pe.peBlue = qBlue(color);
452 pe.peFlags = 0;
453
454 SetPaletteEntries(cmap, i, 1, &pe);
455 }
456}
457
458void QGLWidgetPrivate::updateColormap()
459{
460 Q_Q(QGLWidget);
461 if (!cmap.handle())
462 return;
463 HDC hdc = GetDC(q->winId());
464 SelectPalette(hdc, (HPALETTE) cmap.handle(), TRUE);
465 qStoreColors((HPALETTE) cmap.handle(), cmap);
466 RealizePalette(hdc);
467 ReleaseDC(q->winId(), hdc);
468}
469
470bool QGLWidget::event(QEvent *e)
471{
472 Q_D(QGLWidget);
473 if (e->type() == QEvent::ParentChange) {
474 setContext(new QGLContext(d->glcx->requestedFormat(), this));
475 // the overlay needs to be recreated as well
476 delete d->olcx;
477 if (isValid() && context()->format().hasOverlay()) {
478 d->olcx = new QGLContext(QGLFormat::defaultOverlayFormat(), this);
479 if (!d->olcx->create(isSharing() ? d->glcx : 0)) {
480 delete d->olcx;
481 d->olcx = 0;
482 d->glcx->d_func()->glFormat.setOverlay(false);
483 }
484 } else {
485 d->olcx = 0;
486 }
487 } else if (e->type() == QEvent::Show && !format().rgba()) {
488 d->updateColormap();
489 }
490
491 return QWidget::event(e);
492}
493
494
495void QGLWidget::resizeEvent(QResizeEvent *)
496{
497 Q_D(QGLWidget);
498 if (!isValid())
499 return;
500 makeCurrent();
501 if (!d->glcx->initialized())
502 glInit();
503 resizeGL(width(), height());
504 if (d->olcx) {
505 makeOverlayCurrent();
506 resizeOverlayGL(width(), height());
507 }
508}
509
510
511const QGLContext* QGLWidget::overlayContext() const
512{
513 return d_func()->olcx;
514}
515
516
517void QGLWidget::makeOverlayCurrent()
518{
519 Q_D(QGLWidget);
520 if (d->olcx) {
521 d->olcx->makeCurrent();
522 if (!d->olcx->initialized()) {
523 initializeOverlayGL();
524 d->olcx->setInitialized(true);
525 }
526 }
527}
528
529
530void QGLWidget::updateOverlayGL()
531{
532 Q_D(QGLWidget);
533 if (d->olcx) {
534 makeOverlayCurrent();
535 paintOverlayGL();
536 if (d->olcx->format().doubleBuffer()) {
537 if (d->autoSwap)
538 d->olcx->swapBuffers();
539 }
540 else {
541 glFlush();
542 }
543 }
544}
545
546void QGLWidget::setContext(QGLContext *context,
547 const QGLContext* shareContext,
548 bool deleteOldContext)
549{
550 Q_D(QGLWidget);
551 if (context == 0) {
552 qWarning("QGLWidget::setContext: Cannot set null context");
553 return;
554 }
555 if (!context->deviceIsPixmap() && context->device() != this) {
556 qWarning("QGLWidget::setContext: Context must refer to this widget");
557 return;
558 }
559
560 if (d->glcx)
561 d->glcx->doneCurrent();
562 QGLContext* oldcx = d->glcx;
563 d->glcx = context;
564
565 bool doShow = false;
566 if (oldcx && oldcx->d_func()->win == winId() && !d->glcx->deviceIsPixmap()) {
567 // We already have a context and must therefore create a new
568 // window since Windows does not permit setting a new OpenGL
569 // context for a window that already has one set.
570 doShow = isVisible();
571 QWidget *pW = static_cast<QWidget *>(parent());
572 QPoint pos = geometry().topLeft();
573 setParent(pW, windowFlags());
574 move(pos);
575 }
576
577 if (!d->glcx->isValid()) {
578 d->glcx->create(shareContext ? shareContext : oldcx);
579 // the above is a trick to keep disp lists etc when a
580 // QGLWidget has been reparented, so remove the sharing
581 // flag if we don't actually have a sharing context.
582 if (!shareContext)
583 d->glcx->d_ptr->sharing = false;
584 }
585
586 if (deleteOldContext)
587 delete oldcx;
588
589 if (doShow)
590 show();
591}
592
593
594void QGLWidgetPrivate::cleanupColormaps()
595{
596 Q_Q(QGLWidget);
597 if (cmap.handle()) {
598 HDC hdc = GetDC(q->winId());
599 SelectPalette(hdc, (HPALETTE) GetStockObject(DEFAULT_PALETTE), FALSE);
600 DeleteObject((HPALETTE) cmap.handle());
601 ReleaseDC(q->winId(), hdc);
602 cmap.setHandle(0);
603 }
604 return;
605}
606
607const QGLColormap & QGLWidget::colormap() const
608{
609 return d_func()->cmap;
610}
611
612void QGLWidget::setColormap(const QGLColormap & c)
613{
614 Q_D(QGLWidget);
615 d->cmap = c;
616
617 if (d->cmap.handle()) { // already have an allocated cmap
618 d->updateColormap();
619 } else {
620 LOGPALETTE *lpal = (LOGPALETTE *) malloc(sizeof(LOGPALETTE)
621 +c.size()*sizeof(PALETTEENTRY));
622 lpal->palVersion = 0x300;
623 lpal->palNumEntries = c.size();
624 d->cmap.setHandle(CreatePalette(lpal));
625 free(lpal);
626 d->updateColormap();
627 }
628}
629
630void QGLExtensions::init()
631{
632 static bool init_done = false;
633
634 if (init_done)
635 return;
636 init_done = true;
637
638 // We need a context current to initialize the extensions.
639 QGLWidget tmpWidget;
640 tmpWidget.makeCurrent();
641
642 init_extensions();
643
644 tmpWidget.doneCurrent();
645}
646
647QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.