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

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

trunk: Merged in qt 4.6.2 sources.

  • Property svn:eol-style set to native
File size: 16.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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 QGLTemporaryContext implementation
100*/
101
102class QGLTemporaryContextPrivate
103{
104public:
105 QGLWidget *widget;
106};
107
108QGLTemporaryContext::QGLTemporaryContext(bool, QWidget *)
109 : d(new QGLTemporaryContextPrivate)
110{
111 d->widget = new QGLWidget;
112 d->widget->makeCurrent();
113}
114
115QGLTemporaryContext::~QGLTemporaryContext()
116{
117 delete d->widget;
118}
119
120/*****************************************************************************
121 QGLFormat Win32/WGL-specific code
122 *****************************************************************************/
123
124void qt_egl_add_platform_config(QEglProperties& props, QPaintDevice *device)
125{
126 int devType = device->devType();
127 if (devType == QInternal::Image)
128 props.setPixelFormat(static_cast<QImage *>(device)->format());
129 else
130 props.setPixelFormat(QImage::Format_RGB16);
131}
132
133
134static bool opengl32dll = false;
135
136bool QGLFormat::hasOpenGLOverlays()
137{
138 return false; // ###
139}
140
141
142bool QGLContext::chooseContext(const QGLContext* shareContext)
143{
144 Q_D(QGLContext);
145
146 // Validate the device.
147 if (!device())
148 return false;
149 int devType = device()->devType();
150 if (devType != QInternal::Pixmap && devType != QInternal::Image && devType != QInternal::Widget) {
151 qWarning("QGLContext::chooseContext(): Cannot create QGLContext's for paint device type %d", devType);
152 return false;
153 }
154
155 // Get the display and initialize it.
156 d->eglContext = new QEglContext();
157 d->eglContext->setApi(QEgl::OpenGL);
158 if (!d->eglContext->openDisplay(device())) {
159 delete d->eglContext;
160 d->eglContext = 0;
161 return false;
162 }
163
164 // Construct the configuration we need for this surface.
165 QEglProperties configProps;
166 qt_egl_add_platform_config(configProps, device());
167 qt_egl_set_format(configProps, devType, d->glFormat);
168 configProps.setRenderableType(QEgl::OpenGL);
169
170 // Search for a matching configuration, reducing the complexity
171 // each time until we get something that matches.
172 if (!d->eglContext->chooseConfig(configProps)) {
173 delete d->eglContext;
174 d->eglContext = 0;
175 return false;
176 }
177
178 // Inform the higher layers about the actual format properties.
179 qt_egl_update_format(*(d->eglContext), d->glFormat);
180
181 // Create a new context for the configuration.
182 if (!d->eglContext->createContext
183 (shareContext ? shareContext->d_func()->eglContext : 0)) {
184 delete d->eglContext;
185 d->eglContext = 0;
186 return false;
187 }
188 d->sharing = d->eglContext->isSharing();
189 if (d->sharing && shareContext)
190 const_cast<QGLContext *>(shareContext)->d_func()->sharing = true;
191
192#if defined(EGL_VERSION_1_1)
193 if (d->glFormat.swapInterval() != -1 && devType == QInternal::Widget)
194 eglSwapInterval(d->eglContext->display(), d->glFormat.swapInterval());
195#endif
196
197 // Create the EGL surface to draw into.
198 d->eglSurface = d->eglContext->createSurface(device());
199 if (d->eglSurface == EGL_NO_SURFACE) {
200 delete d->eglContext;
201 d->eglContext = 0;
202 return false;
203 }
204
205 return true;
206
207}
208
209
210
211static bool qLogEq(bool a, bool b)
212{
213 return (((!a) && (!b)) || (a && b));
214}
215
216int QGLContext::choosePixelFormat(void* , HDC )
217{
218
219 return 0;
220}
221
222class QGLCmapPrivate;
223
224class /*Q_EXPORT*/ QGLCmap
225{
226public:
227 enum Flags { Reserved = 0x01 };
228
229 QGLCmap(int maxSize = 256);
230 QGLCmap(const QGLCmap& map);
231 ~QGLCmap();
232
233 QGLCmap& operator=(const QGLCmap& map);
234
235 // isEmpty and/or isNull ?
236 int size() const;
237 int maxSize() const;
238
239 void resize(int newSize);
240
241 int find(QRgb color) const;
242 int findNearest(QRgb color) const;
243 int allocate(QRgb color, uint flags = 0, quint8 context = 0);
244
245 void setEntry(int idx, QRgb color, uint flags = 0, quint8 context = 0);
246
247 const QRgb* colors() const;
248
249private:
250 void detach();
251 QGLCmapPrivate* d;
252};
253
254#endif
255
256
257QGLCmap::QGLCmap(int maxSize) // add a bool prealloc?
258{
259 d = new QGLCmapPrivate;
260 d->maxSize = maxSize;
261}
262
263QGLCmap::QGLCmap(const QGLCmap& map)
264{
265 d = map.d;
266 d->ref();
267}
268
269QGLCmap::~QGLCmap()
270{
271 if (d && d->deref())
272 delete d;
273 d = 0;
274}
275
276QGLCmap& QGLCmap::operator=(const QGLCmap& map)
277{
278 map.d->ref();
279 if (d->deref())
280 delete d;
281 d = map.d;
282 return *this;
283}
284
285int QGLCmap::size() const
286{
287 return d->colorArray.size();
288}
289
290int QGLCmap::maxSize() const
291{
292 return d->maxSize;
293}
294
295void QGLCmap::detach()
296{
297 if (d->count != 1) {
298 d->deref();
299 QGLCmapPrivate* newd = new QGLCmapPrivate;
300 newd->maxSize = d->maxSize;
301 newd->colorArray = d->colorArray;
302 newd->allocArray = d->allocArray;
303 newd->contextArray = d->contextArray;
304 newd->colorArray.detach();
305 newd->allocArray.detach();
306 newd->contextArray.detach();
307 newd->colorMap = d->colorMap;
308 d = newd;
309 }
310}
311
312
313void QGLCmap::resize(int newSize)
314{
315 if (newSize < 0 || newSize > d->maxSize) {
316 qWarning("QGLCmap::resize(): size out of range");
317 return;
318 }
319 int oldSize = size();
320 detach();
321 //if shrinking; remove the lost elems from colorMap
322 d->colorArray.resize(newSize);
323 d->allocArray.resize(newSize);
324 d->contextArray.resize(newSize);
325 if (newSize > oldSize) {
326 memset(d->allocArray.data() + oldSize, 0, newSize - oldSize);
327 memset(d->contextArray.data() + oldSize, 0, newSize - oldSize);
328 }
329}
330
331
332int QGLCmap::find(QRgb color) const
333{
334 QMap<uint,int>::ConstIterator it = d->colorMap.find(color);
335 if (it != d->colorMap.end())
336 return *it;
337 return -1;
338}
339
340
341int QGLCmap::findNearest(QRgb color) const
342{
343 int idx = find(color);
344 if (idx >= 0)
345 return idx;
346 int mapSize = size();
347 int mindist = 200000;
348 int r = qRed(color);
349 int g = qGreen(color);
350 int b = qBlue(color);
351 int rx, gx, bx, dist;
352 for (int i=0; i < mapSize; i++) {
353 if (!(d->allocArray[i] & QGLCmapPrivate::Allocated))
354 continue;
355 QRgb ci = d->colorArray[i];
356 rx = r - qRed(ci);
357 gx = g - qGreen(ci);
358 bx = b - qBlue(ci);
359 dist = rx*rx + gx*gx + bx*bx; // calculate distance
360 if (dist < mindist) { // minimal?
361 mindist = dist;
362 idx = i;
363 }
364 }
365 return idx;
366}
367
368
369// Does not always allocate; returns existing c idx if found
370
371int QGLCmap::allocate(QRgb color, uint flags, quint8 context)
372{
373 int idx = find(color);
374 if (idx >= 0)
375 return idx;
376
377 int mapSize = d->colorArray.size();
378 int newIdx = d->allocArray.indexOf(QGLCmapPrivate::UnAllocated);
379
380 if (newIdx < 0) { // Must allocate more room
381 if (mapSize < d->maxSize) {
382 newIdx = mapSize;
383 mapSize++;
384 resize(mapSize);
385 }
386 else {
387 //# add a bool param that says what to do in case no more room -
388 // fail (-1) or return nearest?
389 return -1;
390 }
391 }
392
393 d->colorArray[newIdx] = color;
394 if (flags & QGLCmap::Reserved) {
395 d->allocArray[newIdx] = QGLCmapPrivate::Reserved;
396 }
397 else {
398 d->allocArray[newIdx] = QGLCmapPrivate::Allocated;
399 d->colorMap.insert(color, newIdx);
400 }
401 d->contextArray[newIdx] = context;
402 return newIdx;
403}
404
405
406void QGLCmap::setEntry(int idx, QRgb color, uint flags, quint8 context)
407{
408 if (idx < 0 || idx >= d->maxSize) {
409 qWarning("QGLCmap::set(): Index out of range");
410 return;
411 }
412 detach();
413 int mapSize = size();
414 if (idx >= mapSize) {
415 mapSize = idx + 1;
416 resize(mapSize);
417 }
418 d->colorArray[idx] = color;
419 if (flags & QGLCmap::Reserved) {
420 d->allocArray[idx] = QGLCmapPrivate::Reserved;
421 }
422 else {
423 d->allocArray[idx] = QGLCmapPrivate::Allocated;
424 d->colorMap.insert(color, idx);
425 }
426 d->contextArray[idx] = context;
427}
428
429
430const QRgb* QGLCmap::colors() const
431{
432 return d->colorArray.data();
433}
434
435
436/*****************************************************************************
437 QGLWidget Win32/WGL-specific code
438 *****************************************************************************/
439
440void QGLWidgetPrivate::init(QGLContext *ctx, const QGLWidget* shareWidget)
441{
442 Q_Q(QGLWidget);
443 olcx = 0;
444 initContext(ctx, shareWidget);
445
446 if (q->isValid() && q->context()->format().hasOverlay()) {
447 olcx = new QGLContext(QGLFormat::defaultOverlayFormat(), q);
448 if (!olcx->create(shareWidget ? shareWidget->overlayContext() : 0)) {
449 delete olcx;
450 olcx = 0;
451 glcx->d_func()->glFormat.setOverlay(false);
452 }
453 } else {
454 olcx = 0;
455 }
456}
457
458/*\internal
459 Store color values in the given colormap.
460*/
461static void qStoreColors(HPALETTE cmap, const QGLColormap & cols)
462{
463 QRgb color;
464 PALETTEENTRY pe;
465
466 for (int i = 0; i < cols.size(); i++) {
467 color = cols.entryRgb(i);
468 pe.peRed = qRed(color);
469 pe.peGreen = qGreen(color);
470 pe.peBlue = qBlue(color);
471 pe.peFlags = 0;
472
473 SetPaletteEntries(cmap, i, 1, &pe);
474 }
475}
476
477void QGLWidgetPrivate::updateColormap()
478{
479 Q_Q(QGLWidget);
480 if (!cmap.handle())
481 return;
482 HDC hdc = GetDC(q->winId());
483 SelectPalette(hdc, (HPALETTE) cmap.handle(), TRUE);
484 qStoreColors((HPALETTE) cmap.handle(), cmap);
485 RealizePalette(hdc);
486 ReleaseDC(q->winId(), hdc);
487}
488
489bool QGLWidget::event(QEvent *e)
490{
491 Q_D(QGLWidget);
492 if (e->type() == QEvent::ParentChange) {
493 setContext(new QGLContext(d->glcx->requestedFormat(), this));
494 // the overlay needs to be recreated as well
495 delete d->olcx;
496 if (isValid() && context()->format().hasOverlay()) {
497 d->olcx = new QGLContext(QGLFormat::defaultOverlayFormat(), this);
498 if (!d->olcx->create(isSharing() ? d->glcx : 0)) {
499 delete d->olcx;
500 d->olcx = 0;
501 d->glcx->d_func()->glFormat.setOverlay(false);
502 }
503 } else {
504 d->olcx = 0;
505 }
506 } else if (e->type() == QEvent::Show && !format().rgba()) {
507 d->updateColormap();
508 }
509
510 return QWidget::event(e);
511}
512
513
514void QGLWidget::resizeEvent(QResizeEvent *)
515{
516 Q_D(QGLWidget);
517 if (!isValid())
518 return;
519 makeCurrent();
520 if (!d->glcx->initialized())
521 glInit();
522 resizeGL(width(), height());
523 if (d->olcx) {
524 makeOverlayCurrent();
525 resizeOverlayGL(width(), height());
526 }
527}
528
529
530const QGLContext* QGLWidget::overlayContext() const
531{
532 return d_func()->olcx;
533}
534
535
536void QGLWidget::makeOverlayCurrent()
537{
538 Q_D(QGLWidget);
539 if (d->olcx) {
540 d->olcx->makeCurrent();
541 if (!d->olcx->initialized()) {
542 initializeOverlayGL();
543 d->olcx->setInitialized(true);
544 }
545 }
546}
547
548
549void QGLWidget::updateOverlayGL()
550{
551 Q_D(QGLWidget);
552 if (d->olcx) {
553 makeOverlayCurrent();
554 paintOverlayGL();
555 if (d->olcx->format().doubleBuffer()) {
556 if (d->autoSwap)
557 d->olcx->swapBuffers();
558 }
559 else {
560 glFlush();
561 }
562 }
563}
564
565void QGLWidget::setContext(QGLContext *context,
566 const QGLContext* shareContext,
567 bool deleteOldContext)
568{
569 Q_D(QGLWidget);
570 if (context == 0) {
571 qWarning("QGLWidget::setContext: Cannot set null context");
572 return;
573 }
574 if (!context->deviceIsPixmap() && context->device() != this) {
575 qWarning("QGLWidget::setContext: Context must refer to this widget");
576 return;
577 }
578
579 if (d->glcx)
580 d->glcx->doneCurrent();
581 QGLContext* oldcx = d->glcx;
582 d->glcx = context;
583
584 bool doShow = false;
585 if (oldcx && oldcx->d_func()->win == winId() && !d->glcx->deviceIsPixmap()) {
586 // We already have a context and must therefore create a new
587 // window since Windows does not permit setting a new OpenGL
588 // context for a window that already has one set.
589 doShow = isVisible();
590 QWidget *pW = static_cast<QWidget *>(parent());
591 QPoint pos = geometry().topLeft();
592 setParent(pW, windowFlags());
593 move(pos);
594 }
595
596 if (!d->glcx->isValid()) {
597 d->glcx->create(shareContext ? shareContext : oldcx);
598 // the above is a trick to keep disp lists etc when a
599 // QGLWidget has been reparented, so remove the sharing
600 // flag if we don't actually have a sharing context.
601 if (!shareContext)
602 d->glcx->d_ptr->sharing = false;
603 }
604
605 if (deleteOldContext)
606 delete oldcx;
607
608 if (doShow)
609 show();
610}
611
612
613void QGLWidgetPrivate::cleanupColormaps()
614{
615 Q_Q(QGLWidget);
616 if (cmap.handle()) {
617 HDC hdc = GetDC(q->winId());
618 SelectPalette(hdc, (HPALETTE) GetStockObject(DEFAULT_PALETTE), FALSE);
619 DeleteObject((HPALETTE) cmap.handle());
620 ReleaseDC(q->winId(), hdc);
621 cmap.setHandle(0);
622 }
623 return;
624}
625
626const QGLColormap & QGLWidget::colormap() const
627{
628 return d_func()->cmap;
629}
630
631void QGLWidget::setColormap(const QGLColormap & c)
632{
633 Q_D(QGLWidget);
634 d->cmap = c;
635
636 if (d->cmap.handle()) { // already have an allocated cmap
637 d->updateColormap();
638 } else {
639 LOGPALETTE *lpal = (LOGPALETTE *) malloc(sizeof(LOGPALETTE)
640 +c.size()*sizeof(PALETTEENTRY));
641 lpal->palVersion = 0x300;
642 lpal->palNumEntries = c.size();
643 d->cmap.setHandle(CreatePalette(lpal));
644 free(lpal);
645 d->updateColormap();
646 }
647}
648
649QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.