| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtOpenGL module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** 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 are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 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 <qcolor.h>
|
|---|
| 53 |
|
|---|
| 54 | #include <windows.h>
|
|---|
| 55 |
|
|---|
| 56 | typedef bool (APIENTRY *PFNWGLGETPIXELFORMATATTRIBIVARB)(HDC hdc,
|
|---|
| 57 | int iPixelFormat,
|
|---|
| 58 | int iLayerPlane,
|
|---|
| 59 | uint nAttributes,
|
|---|
| 60 | const int *piAttributes,
|
|---|
| 61 | int *piValues);
|
|---|
| 62 | typedef bool (APIENTRY *PFNWGLCHOOSEPIXELFORMATARB)(HDC hdc,
|
|---|
| 63 | const int *piAttribList,
|
|---|
| 64 | const float *pfAttribFList,
|
|---|
| 65 | uint nMaxFormats,
|
|---|
| 66 | int *piFormats,
|
|---|
| 67 | UINT *nNumFormats);
|
|---|
| 68 | #ifndef WGL_ARB_multisample
|
|---|
| 69 | #define WGL_SAMPLE_BUFFERS_ARB 0x2041
|
|---|
| 70 | #define WGL_SAMPLES_ARB 0x2042
|
|---|
| 71 | #endif
|
|---|
| 72 |
|
|---|
| 73 | #ifndef WGL_ARB_pixel_format
|
|---|
| 74 | #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000
|
|---|
| 75 | #define WGL_DRAW_TO_WINDOW_ARB 0x2001
|
|---|
| 76 | #define WGL_DRAW_TO_BITMAP_ARB 0x2002
|
|---|
| 77 | #define WGL_ACCELERATION_ARB 0x2003
|
|---|
| 78 | #define WGL_NEED_PALETTE_ARB 0x2004
|
|---|
| 79 | #define WGL_NEED_SYSTEM_PALETTE_ARB 0x2005
|
|---|
| 80 | #define WGL_SWAP_LAYER_BUFFERS_ARB 0x2006
|
|---|
| 81 | #define WGL_SWAP_METHOD_ARB 0x2007
|
|---|
| 82 | #define WGL_NUMBER_OVERLAYS_ARB 0x2008
|
|---|
| 83 | #define WGL_NUMBER_UNDERLAYS_ARB 0x2009
|
|---|
| 84 | #define WGL_TRANSPARENT_ARB 0x200A
|
|---|
| 85 | #define WGL_TRANSPARENT_RED_VALUE_ARB 0x2037
|
|---|
| 86 | #define WGL_TRANSPARENT_GREEN_VALUE_ARB 0x2038
|
|---|
| 87 | #define WGL_TRANSPARENT_BLUE_VALUE_ARB 0x2039
|
|---|
| 88 | #define WGL_TRANSPARENT_ALPHA_VALUE_ARB 0x203A
|
|---|
| 89 | #define WGL_TRANSPARENT_INDEX_VALUE_ARB 0x203B
|
|---|
| 90 | #define WGL_SHARE_DEPTH_ARB 0x200C
|
|---|
| 91 | #define WGL_SHARE_STENCIL_ARB 0x200D
|
|---|
| 92 | #define WGL_SHARE_ACCUM_ARB 0x200E
|
|---|
| 93 | #define WGL_SUPPORT_GDI_ARB 0x200F
|
|---|
| 94 | #define WGL_SUPPORT_OPENGL_ARB 0x2010
|
|---|
| 95 | #define WGL_DOUBLE_BUFFER_ARB 0x2011
|
|---|
| 96 | #define WGL_STEREO_ARB 0x2012
|
|---|
| 97 | #define WGL_PIXEL_TYPE_ARB 0x2013
|
|---|
| 98 | #define WGL_COLOR_BITS_ARB 0x2014
|
|---|
| 99 | #define WGL_RED_BITS_ARB 0x2015
|
|---|
| 100 | #define WGL_RED_SHIFT_ARB 0x2016
|
|---|
| 101 | #define WGL_GREEN_BITS_ARB 0x2017
|
|---|
| 102 | #define WGL_GREEN_SHIFT_ARB 0x2018
|
|---|
| 103 | #define WGL_BLUE_BITS_ARB 0x2019
|
|---|
| 104 | #define WGL_BLUE_SHIFT_ARB 0x201A
|
|---|
| 105 | #define WGL_ALPHA_BITS_ARB 0x201B
|
|---|
| 106 | #define WGL_ALPHA_SHIFT_ARB 0x201C
|
|---|
| 107 | #define WGL_ACCUM_BITS_ARB 0x201D
|
|---|
| 108 | #define WGL_ACCUM_RED_BITS_ARB 0x201E
|
|---|
| 109 | #define WGL_ACCUM_GREEN_BITS_ARB 0x201F
|
|---|
| 110 | #define WGL_ACCUM_BLUE_BITS_ARB 0x2020
|
|---|
| 111 | #define WGL_ACCUM_ALPHA_BITS_ARB 0x2021
|
|---|
| 112 | #define WGL_DEPTH_BITS_ARB 0x2022
|
|---|
| 113 | #define WGL_STENCIL_BITS_ARB 0x2023
|
|---|
| 114 | #define WGL_AUX_BUFFERS_ARB 0x2024
|
|---|
| 115 | #define WGL_NO_ACCELERATION_ARB 0x2025
|
|---|
| 116 | #define WGL_GENERIC_ACCELERATION_ARB 0x2026
|
|---|
| 117 | #define WGL_FULL_ACCELERATION_ARB 0x2027
|
|---|
| 118 | #define WGL_SWAP_EXCHANGE_ARB 0x2028
|
|---|
| 119 | #define WGL_SWAP_COPY_ARB 0x2029
|
|---|
| 120 | #define WGL_SWAP_UNDEFINED_ARB 0x202A
|
|---|
| 121 | #define WGL_TYPE_RGBA_ARB 0x202B
|
|---|
| 122 | #define WGL_TYPE_COLORINDEX_ARB 0x202C
|
|---|
| 123 | #endif
|
|---|
| 124 |
|
|---|
| 125 | QT_BEGIN_NAMESPACE
|
|---|
| 126 |
|
|---|
| 127 | class QGLCmapPrivate
|
|---|
| 128 | {
|
|---|
| 129 | public:
|
|---|
| 130 | QGLCmapPrivate() : count(1) { }
|
|---|
| 131 | void ref() { ++count; }
|
|---|
| 132 | bool deref() { return !--count; }
|
|---|
| 133 | uint count;
|
|---|
| 134 |
|
|---|
| 135 | enum AllocState{ UnAllocated = 0, Allocated = 0x01, Reserved = 0x02 };
|
|---|
| 136 |
|
|---|
| 137 | int maxSize;
|
|---|
| 138 | QVector<uint> colorArray;
|
|---|
| 139 | QVector<quint8> allocArray;
|
|---|
| 140 | QVector<quint8> contextArray;
|
|---|
| 141 | QMap<uint,int> colorMap;
|
|---|
| 142 | };
|
|---|
| 143 |
|
|---|
| 144 | /*****************************************************************************
|
|---|
| 145 | QColorMap class - temporarily here, until it is ready for prime time
|
|---|
| 146 | *****************************************************************************/
|
|---|
| 147 |
|
|---|
| 148 | /****************************************************************************
|
|---|
| 149 | **
|
|---|
| 150 | ** Definition of QColorMap class
|
|---|
| 151 | **
|
|---|
| 152 | ****************************************************************************/
|
|---|
| 153 |
|
|---|
| 154 | class QGLCmapPrivate;
|
|---|
| 155 |
|
|---|
| 156 | class /*Q_EXPORT*/ QGLCmap
|
|---|
| 157 | {
|
|---|
| 158 | public:
|
|---|
| 159 | enum Flags { Reserved = 0x01 };
|
|---|
| 160 |
|
|---|
| 161 | QGLCmap(int maxSize = 256);
|
|---|
| 162 | QGLCmap(const QGLCmap& map);
|
|---|
| 163 | ~QGLCmap();
|
|---|
| 164 |
|
|---|
| 165 | QGLCmap& operator=(const QGLCmap& map);
|
|---|
| 166 |
|
|---|
| 167 | // isEmpty and/or isNull ?
|
|---|
| 168 | int size() const;
|
|---|
| 169 | int maxSize() const;
|
|---|
| 170 |
|
|---|
| 171 | void resize(int newSize);
|
|---|
| 172 |
|
|---|
| 173 | int find(QRgb color) const;
|
|---|
| 174 | int findNearest(QRgb color) const;
|
|---|
| 175 | int allocate(QRgb color, uint flags = 0, quint8 context = 0);
|
|---|
| 176 |
|
|---|
| 177 | void setEntry(int idx, QRgb color, uint flags = 0, quint8 context = 0);
|
|---|
| 178 |
|
|---|
| 179 | const QRgb* colors() const;
|
|---|
| 180 |
|
|---|
| 181 | private:
|
|---|
| 182 | void detach();
|
|---|
| 183 | QGLCmapPrivate* d;
|
|---|
| 184 | };
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 | QGLCmap::QGLCmap(int maxSize) // add a bool prealloc?
|
|---|
| 188 | {
|
|---|
| 189 | d = new QGLCmapPrivate;
|
|---|
| 190 | d->maxSize = maxSize;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 | QGLCmap::QGLCmap(const QGLCmap& map)
|
|---|
| 195 | {
|
|---|
| 196 | d = map.d;
|
|---|
| 197 | d->ref();
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 | QGLCmap::~QGLCmap()
|
|---|
| 202 | {
|
|---|
| 203 | if (d && d->deref())
|
|---|
| 204 | delete d;
|
|---|
| 205 | d = 0;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 | QGLCmap& QGLCmap::operator=(const QGLCmap& map)
|
|---|
| 210 | {
|
|---|
| 211 | map.d->ref();
|
|---|
| 212 | if (d->deref())
|
|---|
| 213 | delete d;
|
|---|
| 214 | d = map.d;
|
|---|
| 215 | return *this;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 | int QGLCmap::size() const
|
|---|
| 220 | {
|
|---|
| 221 | return d->colorArray.size();
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 | int QGLCmap::maxSize() const
|
|---|
| 226 | {
|
|---|
| 227 | return d->maxSize;
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 | void QGLCmap::detach()
|
|---|
| 232 | {
|
|---|
| 233 | if (d->count != 1) {
|
|---|
| 234 | d->deref();
|
|---|
| 235 | QGLCmapPrivate* newd = new QGLCmapPrivate;
|
|---|
| 236 | newd->maxSize = d->maxSize;
|
|---|
| 237 | newd->colorArray = d->colorArray;
|
|---|
| 238 | newd->allocArray = d->allocArray;
|
|---|
| 239 | newd->contextArray = d->contextArray;
|
|---|
| 240 | newd->colorArray.detach();
|
|---|
| 241 | newd->allocArray.detach();
|
|---|
| 242 | newd->contextArray.detach();
|
|---|
| 243 | newd->colorMap = d->colorMap;
|
|---|
| 244 | d = newd;
|
|---|
| 245 | }
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 | void QGLCmap::resize(int newSize)
|
|---|
| 250 | {
|
|---|
| 251 | if (newSize < 0 || newSize > d->maxSize) {
|
|---|
| 252 | qWarning("QGLCmap::resize(): size out of range");
|
|---|
| 253 | return;
|
|---|
| 254 | }
|
|---|
| 255 | int oldSize = size();
|
|---|
| 256 | detach();
|
|---|
| 257 | //if shrinking; remove the lost elems from colorMap
|
|---|
| 258 | d->colorArray.resize(newSize);
|
|---|
| 259 | d->allocArray.resize(newSize);
|
|---|
| 260 | d->contextArray.resize(newSize);
|
|---|
| 261 | if (newSize > oldSize) {
|
|---|
| 262 | memset(d->allocArray.data() + oldSize, 0, newSize - oldSize);
|
|---|
| 263 | memset(d->contextArray.data() + oldSize, 0, newSize - oldSize);
|
|---|
| 264 | }
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 | int QGLCmap::find(QRgb color) const
|
|---|
| 269 | {
|
|---|
| 270 | QMap<uint,int>::ConstIterator it = d->colorMap.find(color);
|
|---|
| 271 | if (it != d->colorMap.end())
|
|---|
| 272 | return *it;
|
|---|
| 273 | return -1;
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 | int QGLCmap::findNearest(QRgb color) const
|
|---|
| 278 | {
|
|---|
| 279 | int idx = find(color);
|
|---|
| 280 | if (idx >= 0)
|
|---|
| 281 | return idx;
|
|---|
| 282 | int mapSize = size();
|
|---|
| 283 | int mindist = 200000;
|
|---|
| 284 | int r = qRed(color);
|
|---|
| 285 | int g = qGreen(color);
|
|---|
| 286 | int b = qBlue(color);
|
|---|
| 287 | int rx, gx, bx, dist;
|
|---|
| 288 | for (int i=0; i < mapSize; i++) {
|
|---|
| 289 | if (!(d->allocArray[i] & QGLCmapPrivate::Allocated))
|
|---|
| 290 | continue;
|
|---|
| 291 | QRgb ci = d->colorArray[i];
|
|---|
| 292 | rx = r - qRed(ci);
|
|---|
| 293 | gx = g - qGreen(ci);
|
|---|
| 294 | bx = b - qBlue(ci);
|
|---|
| 295 | dist = rx*rx + gx*gx + bx*bx; // calculate distance
|
|---|
| 296 | if (dist < mindist) { // minimal?
|
|---|
| 297 | mindist = dist;
|
|---|
| 298 | idx = i;
|
|---|
| 299 | }
|
|---|
| 300 | }
|
|---|
| 301 | return idx;
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 | // Does not always allocate; returns existing c idx if found
|
|---|
| 308 |
|
|---|
| 309 | int QGLCmap::allocate(QRgb color, uint flags, quint8 context)
|
|---|
| 310 | {
|
|---|
| 311 | int idx = find(color);
|
|---|
| 312 | if (idx >= 0)
|
|---|
| 313 | return idx;
|
|---|
| 314 |
|
|---|
| 315 | int mapSize = d->colorArray.size();
|
|---|
| 316 | int newIdx = d->allocArray.indexOf(QGLCmapPrivate::UnAllocated);
|
|---|
| 317 |
|
|---|
| 318 | if (newIdx < 0) { // Must allocate more room
|
|---|
| 319 | if (mapSize < d->maxSize) {
|
|---|
| 320 | newIdx = mapSize;
|
|---|
| 321 | mapSize++;
|
|---|
| 322 | resize(mapSize);
|
|---|
| 323 | }
|
|---|
| 324 | else {
|
|---|
| 325 | //# add a bool param that says what to do in case no more room -
|
|---|
| 326 | // fail (-1) or return nearest?
|
|---|
| 327 | return -1;
|
|---|
| 328 | }
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | d->colorArray[newIdx] = color;
|
|---|
| 332 | if (flags & QGLCmap::Reserved) {
|
|---|
| 333 | d->allocArray[newIdx] = QGLCmapPrivate::Reserved;
|
|---|
| 334 | }
|
|---|
| 335 | else {
|
|---|
| 336 | d->allocArray[newIdx] = QGLCmapPrivate::Allocated;
|
|---|
| 337 | d->colorMap.insert(color, newIdx);
|
|---|
| 338 | }
|
|---|
| 339 | d->contextArray[newIdx] = context;
|
|---|
| 340 | return newIdx;
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 | void QGLCmap::setEntry(int idx, QRgb color, uint flags, quint8 context)
|
|---|
| 345 | {
|
|---|
| 346 | if (idx < 0 || idx >= d->maxSize) {
|
|---|
| 347 | qWarning("QGLCmap::set(): Index out of range");
|
|---|
| 348 | return;
|
|---|
| 349 | }
|
|---|
| 350 | detach();
|
|---|
| 351 | int mapSize = size();
|
|---|
| 352 | if (idx >= mapSize) {
|
|---|
| 353 | mapSize = idx + 1;
|
|---|
| 354 | resize(mapSize);
|
|---|
| 355 | }
|
|---|
| 356 | d->colorArray[idx] = color;
|
|---|
| 357 | if (flags & QGLCmap::Reserved) {
|
|---|
| 358 | d->allocArray[idx] = QGLCmapPrivate::Reserved;
|
|---|
| 359 | }
|
|---|
| 360 | else {
|
|---|
| 361 | d->allocArray[idx] = QGLCmapPrivate::Allocated;
|
|---|
| 362 | d->colorMap.insert(color, idx);
|
|---|
| 363 | }
|
|---|
| 364 | d->contextArray[idx] = context;
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| 368 | const QRgb* QGLCmap::colors() const
|
|---|
| 369 | {
|
|---|
| 370 | return d->colorArray.data();
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 |
|
|---|
| 375 | /*****************************************************************************
|
|---|
| 376 | QGLFormat Win32/WGL-specific code
|
|---|
| 377 | *****************************************************************************/
|
|---|
| 378 |
|
|---|
| 379 |
|
|---|
| 380 | void qwglError(const char* method, const char* func)
|
|---|
| 381 | {
|
|---|
| 382 | #ifndef QT_NO_DEBUG
|
|---|
| 383 | char* lpMsgBuf;
|
|---|
| 384 | FormatMessageA(
|
|---|
| 385 | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
|
|---|
| 386 | 0, GetLastError(),
|
|---|
| 387 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|---|
| 388 | (char*) &lpMsgBuf, 0, 0);
|
|---|
| 389 | qWarning("%s : %s failed: %s", method, func, lpMsgBuf);
|
|---|
| 390 | LocalFree(lpMsgBuf);
|
|---|
| 391 | #else
|
|---|
| 392 | Q_UNUSED(method);
|
|---|
| 393 | Q_UNUSED(func);
|
|---|
| 394 | #endif
|
|---|
| 395 | }
|
|---|
| 396 |
|
|---|
| 397 |
|
|---|
| 398 |
|
|---|
| 399 | bool QGLFormat::hasOpenGL()
|
|---|
| 400 | {
|
|---|
| 401 | return true;
|
|---|
| 402 | }
|
|---|
| 403 |
|
|---|
| 404 | static bool opengl32dll = false;
|
|---|
| 405 |
|
|---|
| 406 | bool QGLFormat::hasOpenGLOverlays()
|
|---|
| 407 | {
|
|---|
| 408 | // workaround for matrox driver:
|
|---|
| 409 | // make a cheap call to opengl to force loading of DLL
|
|---|
| 410 | if (!opengl32dll) {
|
|---|
| 411 | GLint params;
|
|---|
| 412 | glGetIntegerv(GL_DEPTH_BITS, ¶ms);
|
|---|
| 413 | opengl32dll = true;
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| 416 | static bool checkDone = false;
|
|---|
| 417 | static bool hasOl = false;
|
|---|
| 418 |
|
|---|
| 419 | if (!checkDone) {
|
|---|
| 420 | checkDone = true;
|
|---|
| 421 | HDC display_dc = GetDC(0);
|
|---|
| 422 | int pfiMax = DescribePixelFormat(display_dc, 0, 0, NULL);
|
|---|
| 423 | PIXELFORMATDESCRIPTOR pfd;
|
|---|
| 424 | for (int pfi = 1; pfi <= pfiMax; pfi++) {
|
|---|
| 425 | DescribePixelFormat(display_dc, pfi, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
|
|---|
| 426 | if ((pfd.bReserved & 0x0f) && (pfd.dwFlags & PFD_SUPPORT_OPENGL)) {
|
|---|
| 427 | // This format has overlays/underlays
|
|---|
| 428 | LAYERPLANEDESCRIPTOR lpd;
|
|---|
| 429 | wglDescribeLayerPlane(display_dc, pfi, 1,
|
|---|
| 430 | sizeof(LAYERPLANEDESCRIPTOR), &lpd);
|
|---|
| 431 | if (lpd.dwFlags & LPD_SUPPORT_OPENGL) {
|
|---|
| 432 | hasOl = true;
|
|---|
| 433 | break;
|
|---|
| 434 | }
|
|---|
| 435 | }
|
|---|
| 436 | }
|
|---|
| 437 | ReleaseDC(0, display_dc);
|
|---|
| 438 | }
|
|---|
| 439 | return hasOl;
|
|---|
| 440 | }
|
|---|
| 441 |
|
|---|
| 442 |
|
|---|
| 443 | /*****************************************************************************
|
|---|
| 444 | QGLContext Win32/WGL-specific code
|
|---|
| 445 | *****************************************************************************/
|
|---|
| 446 |
|
|---|
| 447 | static uchar qgl_rgb_palette_comp(int idx, uint nbits, uint shift)
|
|---|
| 448 | {
|
|---|
| 449 | const uchar map_3_to_8[8] = {
|
|---|
| 450 | 0, 0111>>1, 0222>>1, 0333>>1, 0444>>1, 0555>>1, 0666>>1, 0377
|
|---|
| 451 | };
|
|---|
| 452 | const uchar map_2_to_8[4] = {
|
|---|
| 453 | 0, 0x55, 0xaa, 0xff
|
|---|
| 454 | };
|
|---|
| 455 | const uchar map_1_to_8[2] = {
|
|---|
| 456 | 0, 255
|
|---|
| 457 | };
|
|---|
| 458 |
|
|---|
| 459 | uchar val = (uchar) (idx >> shift);
|
|---|
| 460 | uchar res = 0;
|
|---|
| 461 | switch (nbits) {
|
|---|
| 462 | case 1:
|
|---|
| 463 | val &= 0x1;
|
|---|
| 464 | res = map_1_to_8[val];
|
|---|
| 465 | break;
|
|---|
| 466 | case 2:
|
|---|
| 467 | val &= 0x3;
|
|---|
| 468 | res = map_2_to_8[val];
|
|---|
| 469 | break;
|
|---|
| 470 | case 3:
|
|---|
| 471 | val &= 0x7;
|
|---|
| 472 | res = map_3_to_8[val];
|
|---|
| 473 | break;
|
|---|
| 474 | default:
|
|---|
| 475 | res = 0;
|
|---|
| 476 | }
|
|---|
| 477 | return res;
|
|---|
| 478 | }
|
|---|
| 479 |
|
|---|
| 480 |
|
|---|
| 481 | static QRgb* qgl_create_rgb_palette(const PIXELFORMATDESCRIPTOR* pfd)
|
|---|
| 482 | {
|
|---|
| 483 | if ((pfd->iPixelType != PFD_TYPE_RGBA) ||
|
|---|
| 484 | !(pfd->dwFlags & PFD_NEED_PALETTE) ||
|
|---|
| 485 | (pfd->cColorBits != 8))
|
|---|
| 486 | return 0;
|
|---|
| 487 | int numEntries = 1 << pfd->cColorBits;
|
|---|
| 488 | QRgb* pal = new QRgb[numEntries];
|
|---|
| 489 | for (int i = 0; i < numEntries; i++) {
|
|---|
| 490 | int r = qgl_rgb_palette_comp(i, pfd->cRedBits, pfd->cRedShift);
|
|---|
| 491 | int g = qgl_rgb_palette_comp(i, pfd->cGreenBits, pfd->cGreenShift);
|
|---|
| 492 | int b = qgl_rgb_palette_comp(i, pfd->cBlueBits, pfd->cBlueShift);
|
|---|
| 493 | pal[i] = qRgb(r, g, b);
|
|---|
| 494 | }
|
|---|
| 495 |
|
|---|
| 496 | const int syscol_indices[12] = {
|
|---|
| 497 | 3, 24, 27, 64, 67, 88, 173, 181, 236, 247, 164, 91
|
|---|
| 498 | };
|
|---|
| 499 |
|
|---|
| 500 | const uint syscols[20] = {
|
|---|
| 501 | 0x000000, 0x800000, 0x008000, 0x808000, 0x000080, 0x800080,
|
|---|
| 502 | 0x008080, 0xc0c0c0, 0xc0dcc0, 0xa6caf0, 0xfffbf0, 0xa0a0a4,
|
|---|
| 503 | 0x808080, 0xff0000, 0x00ff00, 0xffff00, 0x0000ff, 0xff00ff,
|
|---|
| 504 | 0x00ffff, 0xffffff
|
|---|
| 505 | }; // colors #1 - #12 are not present in pal; gets added below
|
|---|
| 506 |
|
|---|
| 507 | if ((pfd->cColorBits == 8) &&
|
|---|
| 508 | (pfd->cRedBits == 3) && (pfd->cRedShift == 0) &&
|
|---|
| 509 | (pfd->cGreenBits == 3) && (pfd->cGreenShift == 3) &&
|
|---|
| 510 | (pfd->cBlueBits == 2) && (pfd->cBlueShift == 6)) {
|
|---|
| 511 | for (int j = 0 ; j < 12 ; j++)
|
|---|
| 512 | pal[syscol_indices[j]] = QRgb(syscols[j+1]);
|
|---|
| 513 | }
|
|---|
| 514 |
|
|---|
| 515 | return pal;
|
|---|
| 516 | }
|
|---|
| 517 |
|
|---|
| 518 | static QGLFormat pfdToQGLFormat(const PIXELFORMATDESCRIPTOR* pfd)
|
|---|
| 519 | {
|
|---|
| 520 | QGLFormat fmt;
|
|---|
| 521 | fmt.setDoubleBuffer(pfd->dwFlags & PFD_DOUBLEBUFFER);
|
|---|
| 522 | fmt.setDepth(pfd->cDepthBits);
|
|---|
| 523 | if (fmt.depth())
|
|---|
| 524 | fmt.setDepthBufferSize(pfd->cDepthBits);
|
|---|
| 525 | fmt.setRgba(pfd->iPixelType == PFD_TYPE_RGBA);
|
|---|
| 526 | fmt.setRedBufferSize(pfd->cRedBits);
|
|---|
| 527 | fmt.setGreenBufferSize(pfd->cGreenBits);
|
|---|
| 528 | fmt.setBlueBufferSize(pfd->cBlueBits);
|
|---|
| 529 | fmt.setAlpha(pfd->cAlphaBits);
|
|---|
| 530 | if (fmt.alpha())
|
|---|
| 531 | fmt.setAlphaBufferSize(pfd->cAlphaBits);
|
|---|
| 532 | fmt.setAccum(pfd->cAccumBits);
|
|---|
| 533 | if (fmt.accum())
|
|---|
| 534 | fmt.setAccumBufferSize(pfd->cAccumRedBits);
|
|---|
| 535 | fmt.setStencil(pfd->cStencilBits);
|
|---|
| 536 | if (fmt.stencil())
|
|---|
| 537 | fmt.setStencilBufferSize(pfd->cStencilBits);
|
|---|
| 538 | fmt.setStereo(pfd->dwFlags & PFD_STEREO);
|
|---|
| 539 | fmt.setDirectRendering((pfd->dwFlags & PFD_GENERIC_ACCELERATED) ||
|
|---|
| 540 | !(pfd->dwFlags & PFD_GENERIC_FORMAT));
|
|---|
| 541 | fmt.setOverlay((pfd->bReserved & 0x0f) != 0);
|
|---|
| 542 | return fmt;
|
|---|
| 543 | }
|
|---|
| 544 |
|
|---|
| 545 | /*
|
|---|
| 546 | NB! requires a current GL context to work
|
|---|
| 547 | */
|
|---|
| 548 | QGLFormat pfiToQGLFormat(HDC hdc, int pfi)
|
|---|
| 549 | {
|
|---|
| 550 | QGLFormat fmt;
|
|---|
| 551 | QVarLengthArray<int> iAttributes(40);
|
|---|
| 552 | QVarLengthArray<int> iValues(40);
|
|---|
| 553 | int i = 0;
|
|---|
| 554 | bool has_sample_buffers = QGLExtensions::glExtensions & QGLExtensions::SampleBuffers;
|
|---|
| 555 |
|
|---|
| 556 | iAttributes[i++] = WGL_DOUBLE_BUFFER_ARB; // 0
|
|---|
| 557 | iAttributes[i++] = WGL_DEPTH_BITS_ARB; // 1
|
|---|
| 558 | iAttributes[i++] = WGL_PIXEL_TYPE_ARB; // 2
|
|---|
| 559 | iAttributes[i++] = WGL_RED_BITS_ARB; // 3
|
|---|
| 560 | iAttributes[i++] = WGL_GREEN_BITS_ARB; // 4
|
|---|
| 561 | iAttributes[i++] = WGL_BLUE_BITS_ARB; // 5
|
|---|
| 562 | iAttributes[i++] = WGL_ALPHA_BITS_ARB; // 6
|
|---|
| 563 | iAttributes[i++] = WGL_ACCUM_BITS_ARB; // 7
|
|---|
| 564 | iAttributes[i++] = WGL_STENCIL_BITS_ARB; // 8
|
|---|
| 565 | iAttributes[i++] = WGL_STEREO_ARB; // 9
|
|---|
| 566 | iAttributes[i++] = WGL_ACCELERATION_ARB; // 10
|
|---|
| 567 | iAttributes[i++] = WGL_NUMBER_OVERLAYS_ARB; // 11
|
|---|
| 568 | if (has_sample_buffers) {
|
|---|
| 569 | iAttributes[i++] = WGL_SAMPLE_BUFFERS_ARB; // 12
|
|---|
| 570 | iAttributes[i++] = WGL_SAMPLES_ARB; // 13
|
|---|
| 571 | }
|
|---|
| 572 | PFNWGLGETPIXELFORMATATTRIBIVARB wglGetPixelFormatAttribivARB =
|
|---|
| 573 | (PFNWGLGETPIXELFORMATATTRIBIVARB) wglGetProcAddress("wglGetPixelFormatAttribivARB");
|
|---|
| 574 |
|
|---|
| 575 | if (wglGetPixelFormatAttribivARB
|
|---|
| 576 | && wglGetPixelFormatAttribivARB(hdc, pfi, 0, i,
|
|---|
| 577 | iAttributes.constData(),
|
|---|
| 578 | iValues.data()))
|
|---|
| 579 | {
|
|---|
| 580 | fmt.setDoubleBuffer(iValues[0]);
|
|---|
| 581 | fmt.setDepth(iValues[1]);
|
|---|
| 582 | if (fmt.depth())
|
|---|
| 583 | fmt.setDepthBufferSize(iValues[1]);
|
|---|
| 584 | fmt.setRgba(iValues[2] == WGL_TYPE_RGBA_ARB);
|
|---|
| 585 | fmt.setRedBufferSize(iValues[3]);
|
|---|
| 586 | fmt.setGreenBufferSize(iValues[4]);
|
|---|
| 587 | fmt.setBlueBufferSize(iValues[5]);
|
|---|
| 588 | fmt.setAlpha(iValues[6]);
|
|---|
| 589 | if (fmt.alpha())
|
|---|
| 590 | fmt.setAlphaBufferSize(iValues[6]);
|
|---|
| 591 | fmt.setAccum(iValues[7]);
|
|---|
| 592 | if (fmt.accum())
|
|---|
| 593 | fmt.setAccumBufferSize(iValues[7]);
|
|---|
| 594 | fmt.setStencil(iValues[8]);
|
|---|
| 595 | if (fmt.stencil())
|
|---|
| 596 | fmt.setStencilBufferSize(iValues[8]);
|
|---|
| 597 | fmt.setStereo(iValues[9]);
|
|---|
| 598 | if (iValues[10] == WGL_FULL_ACCELERATION_ARB)
|
|---|
| 599 | fmt.setDirectRendering(true);
|
|---|
| 600 | else
|
|---|
| 601 | fmt.setDirectRendering(false);
|
|---|
| 602 | fmt.setOverlay(iValues[11]);
|
|---|
| 603 | if (has_sample_buffers) {
|
|---|
| 604 | fmt.setSampleBuffers(iValues[12]);
|
|---|
| 605 | if (fmt.sampleBuffers())
|
|---|
| 606 | fmt.setSamples(iValues[13]);
|
|---|
| 607 | }
|
|---|
| 608 | }
|
|---|
| 609 | #if 0
|
|---|
| 610 | qDebug() << "values for pfi:" << pfi;
|
|---|
| 611 | qDebug() << "doublebuffer 0:" << fmt.doubleBuffer();
|
|---|
| 612 | qDebug() << "depthbuffer 1:" << fmt.depthBufferSize();
|
|---|
| 613 | qDebug() << "rgba 2:" << fmt.rgba();
|
|---|
| 614 | qDebug() << "red size 3:" << fmt.redBufferSize();
|
|---|
| 615 | qDebug() << "green size 4:" << fmt.greenBufferSize();
|
|---|
| 616 | qDebug() << "blue size 5:" << fmt.blueBufferSize();
|
|---|
| 617 | qDebug() << "alpha size 6:" << fmt.alphaBufferSize();
|
|---|
| 618 | qDebug() << "accum size 7:" << fmt.accumBufferSize();
|
|---|
| 619 | qDebug() << "stencil size 8:" << fmt.stencilBufferSize();
|
|---|
| 620 | qDebug() << "stereo 9:" << fmt.stereo();
|
|---|
| 621 | qDebug() << "direct 10:" << fmt.directRendering();
|
|---|
| 622 | qDebug() << "has overlays 11:" << fmt.hasOverlay();
|
|---|
| 623 | qDebug() << "sample buff 12:" << fmt.sampleBuffers();
|
|---|
| 624 | qDebug() << "num samples 13:" << fmt.samples();
|
|---|
| 625 | #endif
|
|---|
| 626 | return fmt;
|
|---|
| 627 | }
|
|---|
| 628 |
|
|---|
| 629 |
|
|---|
| 630 | /*
|
|---|
| 631 | Creates a temporary GL context and makes it current
|
|---|
| 632 | - cleans up when the object is destructed.
|
|---|
| 633 | */
|
|---|
| 634 |
|
|---|
| 635 | Q_GUI_EXPORT const QString qt_getRegisteredWndClass();
|
|---|
| 636 |
|
|---|
| 637 | class QGLTempContext
|
|---|
| 638 | {
|
|---|
| 639 | public:
|
|---|
| 640 | QGLTempContext(bool directRendering, QWidget *parent = 0)
|
|---|
| 641 | {
|
|---|
| 642 | QString windowClassName = qt_getRegisteredWndClass();
|
|---|
| 643 | if (parent && !parent->internalWinId())
|
|---|
| 644 | parent = parent->nativeParentWidget();
|
|---|
| 645 | QT_WA({
|
|---|
| 646 | const TCHAR *cname = (TCHAR*)windowClassName.utf16();
|
|---|
| 647 | dmy_id = CreateWindow(cname, 0, 0, 0, 0, 1, 1,
|
|---|
| 648 | parent ? parent->winId() : 0, 0, qWinAppInst(), 0);
|
|---|
| 649 | } , {
|
|---|
| 650 | dmy_id = CreateWindowA(windowClassName.toLatin1(), 0, 0, 0, 0, 1, 1,
|
|---|
| 651 | parent ? parent->winId() : 0, 0, qWinAppInst(), 0);
|
|---|
| 652 | });
|
|---|
| 653 |
|
|---|
| 654 | dmy_pdc = GetDC(dmy_id);
|
|---|
| 655 | PIXELFORMATDESCRIPTOR dmy_pfd;
|
|---|
| 656 | memset(&dmy_pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
|
|---|
| 657 | dmy_pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
|
|---|
| 658 | dmy_pfd.nVersion = 1;
|
|---|
| 659 | dmy_pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
|
|---|
| 660 | dmy_pfd.iPixelType = PFD_TYPE_RGBA;
|
|---|
| 661 | if (!directRendering)
|
|---|
| 662 | dmy_pfd.dwFlags |= PFD_GENERIC_FORMAT;
|
|---|
| 663 |
|
|---|
| 664 | int dmy_pf = ChoosePixelFormat(dmy_pdc, &dmy_pfd);
|
|---|
| 665 | SetPixelFormat(dmy_pdc, dmy_pf, &dmy_pfd);
|
|---|
| 666 | dmy_rc = wglCreateContext(dmy_pdc);
|
|---|
| 667 | wglMakeCurrent(dmy_pdc, dmy_rc);
|
|---|
| 668 | }
|
|---|
| 669 |
|
|---|
| 670 | ~QGLTempContext() {
|
|---|
| 671 | wglMakeCurrent(dmy_pdc, 0);
|
|---|
| 672 | wglDeleteContext(dmy_rc);
|
|---|
| 673 | ReleaseDC(dmy_id, dmy_pdc);
|
|---|
| 674 | DestroyWindow(dmy_id);
|
|---|
| 675 | }
|
|---|
| 676 |
|
|---|
| 677 | HDC dmy_pdc;
|
|---|
| 678 | HGLRC dmy_rc;
|
|---|
| 679 | WId dmy_id;
|
|---|
| 680 | };
|
|---|
| 681 |
|
|---|
| 682 | bool QGLContext::chooseContext(const QGLContext* shareContext)
|
|---|
| 683 | {
|
|---|
| 684 | Q_D(QGLContext);
|
|---|
| 685 | // workaround for matrox driver:
|
|---|
| 686 | // make a cheap call to opengl to force loading of DLL
|
|---|
| 687 | if (!opengl32dll) {
|
|---|
| 688 | GLint params;
|
|---|
| 689 | glGetIntegerv(GL_DEPTH_BITS, ¶ms);
|
|---|
| 690 | opengl32dll = true;
|
|---|
| 691 | }
|
|---|
| 692 |
|
|---|
| 693 | bool result = true;
|
|---|
| 694 | HDC myDc;
|
|---|
| 695 | QWidget *widget = 0;
|
|---|
| 696 |
|
|---|
| 697 | if (deviceIsPixmap()) {
|
|---|
| 698 | if (d->glFormat.plane())
|
|---|
| 699 | return false; // Pixmaps can't have overlay
|
|---|
| 700 | d->win = 0;
|
|---|
| 701 | HDC display_dc = GetDC(0);
|
|---|
| 702 | myDc = d->hbitmap_hdc = CreateCompatibleDC(display_dc);
|
|---|
| 703 | QPixmap *px = static_cast<QPixmap *>(d->paintDevice);
|
|---|
| 704 |
|
|---|
| 705 | BITMAPINFO bmi;
|
|---|
| 706 | memset(&bmi, 0, sizeof(bmi));
|
|---|
| 707 | bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
|---|
| 708 | bmi.bmiHeader.biWidth = px->width();
|
|---|
| 709 | bmi.bmiHeader.biHeight = px->height();
|
|---|
| 710 | bmi.bmiHeader.biPlanes = 1;
|
|---|
| 711 | bmi.bmiHeader.biBitCount = 32;
|
|---|
| 712 | bmi.bmiHeader.biCompression = BI_RGB;
|
|---|
| 713 | d->hbitmap = CreateDIBSection(display_dc, &bmi, DIB_RGB_COLORS, 0, 0, 0);
|
|---|
| 714 | SelectObject(myDc, d->hbitmap);
|
|---|
| 715 | ReleaseDC(0, display_dc);
|
|---|
| 716 | } else {
|
|---|
| 717 | widget = static_cast<QWidget *>(d->paintDevice);
|
|---|
| 718 | d->win = widget->winId();
|
|---|
| 719 | myDc = GetDC(d->win);
|
|---|
| 720 | }
|
|---|
| 721 |
|
|---|
| 722 | // NB! the QGLTempContext object is needed for the
|
|---|
| 723 | // wglGetProcAddress() calls to succeed and are absolutely
|
|---|
| 724 | // necessary - don't remove!
|
|---|
| 725 | QGLTempContext tmp_ctx(d->glFormat.directRendering(), widget);
|
|---|
| 726 |
|
|---|
| 727 | if (!myDc) {
|
|---|
| 728 | qWarning("QGLContext::chooseContext(): Paint device cannot be null");
|
|---|
| 729 | result = false;
|
|---|
| 730 | goto end;
|
|---|
| 731 | }
|
|---|
| 732 |
|
|---|
| 733 | if (d->glFormat.plane()) {
|
|---|
| 734 | d->pixelFormatId = ((QGLWidget*)d->paintDevice)->context()->d_func()->pixelFormatId;
|
|---|
| 735 | if (!d->pixelFormatId) { // I.e. the glwidget is invalid
|
|---|
| 736 | qWarning("QGLContext::chooseContext(): Cannot create overlay context for invalid widget");
|
|---|
| 737 | result = false;
|
|---|
| 738 | goto end;
|
|---|
| 739 | }
|
|---|
| 740 |
|
|---|
| 741 | d->rc = wglCreateLayerContext(myDc, d->glFormat.plane());
|
|---|
| 742 | if (!d->rc) {
|
|---|
| 743 | qwglError("QGLContext::chooseContext()", "CreateLayerContext");
|
|---|
| 744 | result = false;
|
|---|
| 745 | goto end;
|
|---|
| 746 | }
|
|---|
| 747 |
|
|---|
| 748 | LAYERPLANEDESCRIPTOR lpfd;
|
|---|
| 749 | wglDescribeLayerPlane(myDc, d->pixelFormatId, d->glFormat.plane(), sizeof(LAYERPLANEDESCRIPTOR), &lpfd);
|
|---|
| 750 | d->glFormat.setDoubleBuffer(lpfd.dwFlags & LPD_DOUBLEBUFFER);
|
|---|
| 751 | d->glFormat.setDepth(lpfd.cDepthBits);
|
|---|
| 752 | d->glFormat.setRgba(lpfd.iPixelType == PFD_TYPE_RGBA);
|
|---|
| 753 | if (d->glFormat.rgba()) {
|
|---|
| 754 | if (d->glFormat.redBufferSize() != -1)
|
|---|
| 755 | d->glFormat.setRedBufferSize(lpfd.cRedBits);
|
|---|
| 756 | if (d->glFormat.greenBufferSize() != -1)
|
|---|
| 757 | d->glFormat.setGreenBufferSize(lpfd.cGreenBits);
|
|---|
| 758 | if (d->glFormat.blueBufferSize() != -1)
|
|---|
| 759 | d->glFormat.setBlueBufferSize(lpfd.cBlueBits);
|
|---|
| 760 | }
|
|---|
| 761 | d->glFormat.setAlpha(lpfd.cAlphaBits);
|
|---|
| 762 | d->glFormat.setAccum(lpfd.cAccumBits);
|
|---|
| 763 | d->glFormat.setStencil(lpfd.cStencilBits);
|
|---|
| 764 | d->glFormat.setStereo(lpfd.dwFlags & LPD_STEREO);
|
|---|
| 765 | d->glFormat.setDirectRendering(false);
|
|---|
| 766 | if (d->glFormat.depth())
|
|---|
| 767 | d->glFormat.setDepthBufferSize(lpfd.cDepthBits);
|
|---|
| 768 | if (d->glFormat.alpha())
|
|---|
| 769 | d->glFormat.setAlphaBufferSize(lpfd.cAlphaBits);
|
|---|
| 770 | if (d->glFormat.accum())
|
|---|
| 771 | d->glFormat.setAccumBufferSize(lpfd.cAccumRedBits);
|
|---|
| 772 | if (d->glFormat.stencil())
|
|---|
| 773 | d->glFormat.setStencilBufferSize(lpfd.cStencilBits);
|
|---|
| 774 |
|
|---|
| 775 | if (d->glFormat.rgba()) {
|
|---|
| 776 | if (lpfd.dwFlags & LPD_TRANSPARENT)
|
|---|
| 777 | d->transpColor = QColor(lpfd.crTransparent & 0xff,
|
|---|
| 778 | (lpfd.crTransparent >> 8) & 0xff,
|
|---|
| 779 | (lpfd.crTransparent >> 16) & 0xff);
|
|---|
| 780 | else
|
|---|
| 781 | d->transpColor = QColor(0, 0, 0);
|
|---|
| 782 | }
|
|---|
| 783 | else {
|
|---|
| 784 | if (lpfd.dwFlags & LPD_TRANSPARENT)
|
|---|
| 785 | d->transpColor = QColor(qRgb(1, 2, 3));//, lpfd.crTransparent);
|
|---|
| 786 | else
|
|---|
| 787 | d->transpColor = QColor(qRgb(1, 2, 3));//, 0);
|
|---|
| 788 |
|
|---|
| 789 | d->cmap = new QGLCmap(1 << lpfd.cColorBits);
|
|---|
| 790 | d->cmap->setEntry(lpfd.crTransparent, qRgb(1, 2, 3));//, QGLCmap::Reserved);
|
|---|
| 791 | }
|
|---|
| 792 |
|
|---|
| 793 | if (shareContext && shareContext->isValid()) {
|
|---|
| 794 | QGLContext *share = const_cast<QGLContext *>(shareContext);
|
|---|
| 795 | d->sharing = (wglShareLists(shareContext->d_func()->rc, d->rc) != 0);
|
|---|
| 796 | share->d_func()->sharing = d->sharing;
|
|---|
| 797 | }
|
|---|
| 798 |
|
|---|
| 799 | goto end;
|
|---|
| 800 | }
|
|---|
| 801 | {
|
|---|
| 802 | PIXELFORMATDESCRIPTOR pfd;
|
|---|
| 803 | PIXELFORMATDESCRIPTOR realPfd;
|
|---|
| 804 | d->pixelFormatId = choosePixelFormat(&pfd, myDc);
|
|---|
| 805 | if (d->pixelFormatId == 0) {
|
|---|
| 806 | qwglError("QGLContext::chooseContext()", "ChoosePixelFormat");
|
|---|
| 807 | result = false;
|
|---|
| 808 | goto end;
|
|---|
| 809 | }
|
|---|
| 810 |
|
|---|
| 811 | bool overlayRequested = d->glFormat.hasOverlay();
|
|---|
| 812 | DescribePixelFormat(myDc, d->pixelFormatId, sizeof(PIXELFORMATDESCRIPTOR), &realPfd);
|
|---|
| 813 |
|
|---|
| 814 | if (!deviceIsPixmap() && wglGetProcAddress("wglGetPixelFormatAttribivARB"))
|
|---|
| 815 | d->glFormat = pfiToQGLFormat(myDc, d->pixelFormatId);
|
|---|
| 816 | else
|
|---|
| 817 | d->glFormat = pfdToQGLFormat(&realPfd);
|
|---|
| 818 |
|
|---|
| 819 | d->glFormat.setOverlay(d->glFormat.hasOverlay() && overlayRequested);
|
|---|
| 820 |
|
|---|
| 821 | if (deviceIsPixmap() && !(realPfd.dwFlags & PFD_DRAW_TO_BITMAP)) {
|
|---|
| 822 | qWarning("QGLContext::chooseContext(): Failed to get pixmap rendering context.");
|
|---|
| 823 | result = false;
|
|---|
| 824 | goto end;
|
|---|
| 825 | }
|
|---|
| 826 |
|
|---|
| 827 | if (deviceIsPixmap() &&
|
|---|
| 828 | (((QPixmap*)d->paintDevice)->depth() != realPfd.cColorBits)) {
|
|---|
| 829 | qWarning("QGLContext::chooseContext(): Failed to get pixmap rendering context of suitable depth.");
|
|---|
| 830 | result = false;
|
|---|
| 831 | goto end;
|
|---|
| 832 | }
|
|---|
| 833 |
|
|---|
| 834 | if (!SetPixelFormat(myDc, d->pixelFormatId, &realPfd)) {
|
|---|
| 835 | qwglError("QGLContext::chooseContext()", "SetPixelFormat");
|
|---|
| 836 | result = false;
|
|---|
| 837 | goto end;
|
|---|
| 838 | }
|
|---|
| 839 |
|
|---|
| 840 | if (!(d->rc = wglCreateLayerContext(myDc, 0))) {
|
|---|
| 841 | qwglError("QGLContext::chooseContext()", "wglCreateContext");
|
|---|
| 842 | result = false;
|
|---|
| 843 | goto end;
|
|---|
| 844 | }
|
|---|
| 845 |
|
|---|
| 846 | if (shareContext && shareContext->isValid()) {
|
|---|
| 847 | d->sharing = (wglShareLists(shareContext->d_func()->rc, d->rc) != 0);
|
|---|
| 848 | const_cast<QGLContext *>(shareContext)->d_func()->sharing = d->sharing;
|
|---|
| 849 | }
|
|---|
| 850 |
|
|---|
| 851 | if(!deviceIsPixmap()) {
|
|---|
| 852 | QRgb* pal = qgl_create_rgb_palette(&realPfd);
|
|---|
| 853 | if (pal) {
|
|---|
| 854 | QGLColormap cmap;
|
|---|
| 855 | cmap.setEntries(256, pal);
|
|---|
| 856 | ((QGLWidget*)d->paintDevice)->setColormap(cmap);
|
|---|
| 857 | delete[] pal;
|
|---|
| 858 | }
|
|---|
| 859 | }
|
|---|
| 860 | }
|
|---|
| 861 |
|
|---|
| 862 | end:
|
|---|
| 863 | // vblanking
|
|---|
| 864 | wglMakeCurrent(myDc, d->rc);
|
|---|
| 865 | typedef BOOL (APIENTRYP PFNWGLSWAPINTERVALEXT) (int interval);
|
|---|
| 866 | typedef int (APIENTRYP PFNWGLGETSWAPINTERVALEXT) (void);
|
|---|
| 867 | PFNWGLSWAPINTERVALEXT wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXT) wglGetProcAddress("wglSwapIntervalEXT");
|
|---|
| 868 | PFNWGLGETSWAPINTERVALEXT wglGetSwapIntervalEXT = (PFNWGLGETSWAPINTERVALEXT) wglGetProcAddress("wglGetSwapIntervalEXT");
|
|---|
| 869 | if (wglSwapIntervalEXT && wglGetSwapIntervalEXT) {
|
|---|
| 870 | if (d->reqFormat.swapInterval() != -1)
|
|---|
| 871 | wglSwapIntervalEXT(d->reqFormat.swapInterval());
|
|---|
| 872 | d->glFormat.setSwapInterval(wglGetSwapIntervalEXT());
|
|---|
| 873 | }
|
|---|
| 874 |
|
|---|
| 875 | if (d->win)
|
|---|
| 876 | ReleaseDC(d->win, myDc);
|
|---|
| 877 | return result;
|
|---|
| 878 | }
|
|---|
| 879 |
|
|---|
| 880 |
|
|---|
| 881 |
|
|---|
| 882 | static bool qLogEq(bool a, bool b)
|
|---|
| 883 | {
|
|---|
| 884 | return (((!a) && (!b)) || (a && b));
|
|---|
| 885 | }
|
|---|
| 886 |
|
|---|
| 887 | /*!
|
|---|
| 888 | \bold{Win32 only:} This virtual function chooses a pixel
|
|---|
| 889 | format that matches the OpenGL \link setFormat() format\endlink.
|
|---|
| 890 | Reimplement this function in a subclass if you need a custom
|
|---|
| 891 | context.
|
|---|
| 892 |
|
|---|
| 893 | \warning The \a dummyPfd pointer and \a pdc are used as a \c
|
|---|
| 894 | PIXELFORMATDESCRIPTOR*. We use \c void to avoid using
|
|---|
| 895 | Windows-specific types in our header files.
|
|---|
| 896 |
|
|---|
| 897 | \sa chooseContext()
|
|---|
| 898 | */
|
|---|
| 899 |
|
|---|
| 900 | int QGLContext::choosePixelFormat(void* dummyPfd, HDC pdc)
|
|---|
| 901 | {
|
|---|
| 902 | Q_D(QGLContext);
|
|---|
| 903 | // workaround for matrox driver:
|
|---|
| 904 | // make a cheap call to opengl to force loading of DLL
|
|---|
| 905 | if (!opengl32dll) {
|
|---|
| 906 | GLint params;
|
|---|
| 907 | glGetIntegerv(GL_DEPTH_BITS, ¶ms);
|
|---|
| 908 | opengl32dll = true;
|
|---|
| 909 | }
|
|---|
| 910 |
|
|---|
| 911 | PFNWGLCHOOSEPIXELFORMATARB wglChoosePixelFormatARB =
|
|---|
| 912 | (PFNWGLCHOOSEPIXELFORMATARB) wglGetProcAddress("wglChoosePixelFormatARB");
|
|---|
| 913 | int chosenPfi = 0;
|
|---|
| 914 | if (!deviceIsPixmap() && wglChoosePixelFormatARB) {
|
|---|
| 915 | bool valid;
|
|---|
| 916 | int pixelFormat = 0;
|
|---|
| 917 | uint numFormats = 0;
|
|---|
| 918 | QVarLengthArray<int> iAttributes(40);
|
|---|
| 919 | int i = 0;
|
|---|
| 920 | iAttributes[i++] = WGL_ACCELERATION_ARB;
|
|---|
| 921 | if (d->glFormat.directRendering())
|
|---|
| 922 | iAttributes[i++] = WGL_FULL_ACCELERATION_ARB;
|
|---|
| 923 | else
|
|---|
| 924 | iAttributes[i++] = WGL_NO_ACCELERATION_ARB;
|
|---|
| 925 | iAttributes[i++] = WGL_SUPPORT_OPENGL_ARB;
|
|---|
| 926 | iAttributes[i++] = TRUE;
|
|---|
| 927 | iAttributes[i++] = WGL_DRAW_TO_WINDOW_ARB;
|
|---|
| 928 | iAttributes[i++] = TRUE;
|
|---|
| 929 | iAttributes[i++] = WGL_COLOR_BITS_ARB;
|
|---|
| 930 | iAttributes[i++] = 32;
|
|---|
| 931 | iAttributes[i++] = WGL_DOUBLE_BUFFER_ARB;
|
|---|
| 932 | iAttributes[i++] = d->glFormat.doubleBuffer();
|
|---|
| 933 | if (d->glFormat.stereo()) {
|
|---|
| 934 | iAttributes[i++] = WGL_STEREO_ARB;
|
|---|
| 935 | iAttributes[i++] = TRUE;
|
|---|
| 936 | }
|
|---|
| 937 | if (d->glFormat.depth()) {
|
|---|
| 938 | iAttributes[i++] = WGL_DEPTH_BITS_ARB;
|
|---|
| 939 | iAttributes[i++] = d->glFormat.depthBufferSize() == -1 ? 24 : d->glFormat.depthBufferSize();
|
|---|
| 940 | }
|
|---|
| 941 | iAttributes[i++] = WGL_PIXEL_TYPE_ARB;
|
|---|
| 942 | if (d->glFormat.rgba()) {
|
|---|
| 943 | iAttributes[i++] = WGL_TYPE_RGBA_ARB;
|
|---|
| 944 | if (d->glFormat.redBufferSize() != -1) {
|
|---|
| 945 | iAttributes[i++] = WGL_RED_BITS_ARB;
|
|---|
| 946 | iAttributes[i++] = d->glFormat.redBufferSize();
|
|---|
| 947 | }
|
|---|
| 948 | if (d->glFormat.greenBufferSize() != -1) {
|
|---|
| 949 | iAttributes[i++] = WGL_GREEN_BITS_ARB;
|
|---|
| 950 | iAttributes[i++] = d->glFormat.greenBufferSize();
|
|---|
| 951 | }
|
|---|
| 952 | if (d->glFormat.blueBufferSize() != -1) {
|
|---|
| 953 | iAttributes[i++] = WGL_BLUE_BITS_ARB;
|
|---|
| 954 | iAttributes[i++] = d->glFormat.blueBufferSize();
|
|---|
| 955 | }
|
|---|
| 956 | } else {
|
|---|
| 957 | iAttributes[i++] = WGL_TYPE_COLORINDEX_ARB;
|
|---|
| 958 | }
|
|---|
| 959 | if (d->glFormat.alpha()) {
|
|---|
| 960 | iAttributes[i++] = WGL_ALPHA_BITS_ARB;
|
|---|
| 961 | iAttributes[i++] = d->glFormat.alphaBufferSize() == -1 ? 8 : d->glFormat.alphaBufferSize();
|
|---|
| 962 | }
|
|---|
| 963 | if (d->glFormat.accum()) {
|
|---|
| 964 | iAttributes[i++] = WGL_ACCUM_BITS_ARB;
|
|---|
| 965 | iAttributes[i++] = d->glFormat.accumBufferSize() == -1 ? 16 : d->glFormat.accumBufferSize();
|
|---|
| 966 | }
|
|---|
| 967 | if (d->glFormat.stencil()) {
|
|---|
| 968 | iAttributes[i++] = WGL_STENCIL_BITS_ARB;
|
|---|
| 969 | iAttributes[i++] = d->glFormat.stencilBufferSize() == -1 ? 8 : d->glFormat.stencilBufferSize();
|
|---|
| 970 | }
|
|---|
| 971 | if (d->glFormat.hasOverlay()) {
|
|---|
| 972 | iAttributes[i++] = WGL_NUMBER_OVERLAYS_ARB;
|
|---|
| 973 | iAttributes[i++] = 1;
|
|---|
| 974 | }
|
|---|
| 975 | int si = 0;
|
|---|
| 976 | bool trySampleBuffers = QGLExtensions::glExtensions & QGLExtensions::SampleBuffers;
|
|---|
| 977 | if (trySampleBuffers && d->glFormat.sampleBuffers()) {
|
|---|
| 978 | iAttributes[i++] = WGL_SAMPLE_BUFFERS_ARB;
|
|---|
| 979 | iAttributes[i++] = TRUE;
|
|---|
| 980 | iAttributes[i++] = WGL_SAMPLES_ARB;
|
|---|
| 981 | si = i;
|
|---|
| 982 | iAttributes[i++] = d->glFormat.samples() == -1 ? 4 : d->glFormat.samples();
|
|---|
| 983 | }
|
|---|
| 984 | iAttributes[i] = 0;
|
|---|
| 985 |
|
|---|
| 986 | do {
|
|---|
| 987 | valid = wglChoosePixelFormatARB(pdc, iAttributes.constData(), 0, 1,
|
|---|
| 988 | &pixelFormat, &numFormats);
|
|---|
| 989 | if (trySampleBuffers && (!valid || numFormats < 1) && d->glFormat.sampleBuffers())
|
|---|
| 990 | iAttributes[si] /= 2; // try different no. samples - we aim for the best one
|
|---|
| 991 | else
|
|---|
| 992 | break;
|
|---|
| 993 | } while ((!valid || numFormats < 1) && iAttributes[si] > 1);
|
|---|
| 994 | chosenPfi = pixelFormat;
|
|---|
| 995 | }
|
|---|
| 996 |
|
|---|
| 997 | if (!chosenPfi) { // fallback if wglChoosePixelFormatARB() failed
|
|---|
| 998 | int pmDepth = deviceIsPixmap() ? ((QPixmap*)d->paintDevice)->depth() : 0;
|
|---|
| 999 | PIXELFORMATDESCRIPTOR* p = (PIXELFORMATDESCRIPTOR*)dummyPfd;
|
|---|
| 1000 | memset(p, 0, sizeof(PIXELFORMATDESCRIPTOR));
|
|---|
| 1001 | p->nSize = sizeof(PIXELFORMATDESCRIPTOR);
|
|---|
| 1002 | p->nVersion = 1;
|
|---|
| 1003 | p->dwFlags = PFD_SUPPORT_OPENGL;
|
|---|
| 1004 | if (deviceIsPixmap())
|
|---|
| 1005 | p->dwFlags |= PFD_DRAW_TO_BITMAP;
|
|---|
| 1006 | else
|
|---|
| 1007 | p->dwFlags |= PFD_DRAW_TO_WINDOW;
|
|---|
| 1008 | if (!d->glFormat.directRendering())
|
|---|
| 1009 | p->dwFlags |= PFD_GENERIC_FORMAT;
|
|---|
| 1010 | if (d->glFormat.doubleBuffer() && !deviceIsPixmap())
|
|---|
| 1011 | p->dwFlags |= PFD_DOUBLEBUFFER;
|
|---|
| 1012 | if (d->glFormat.stereo())
|
|---|
| 1013 | p->dwFlags |= PFD_STEREO;
|
|---|
| 1014 | if (d->glFormat.depth())
|
|---|
| 1015 | p->cDepthBits = d->glFormat.depthBufferSize() == -1 ? 32 : d->glFormat.depthBufferSize();
|
|---|
| 1016 | else
|
|---|
| 1017 | p->dwFlags |= PFD_DEPTH_DONTCARE;
|
|---|
| 1018 | if (d->glFormat.rgba()) {
|
|---|
| 1019 | p->iPixelType = PFD_TYPE_RGBA;
|
|---|
| 1020 | if (d->glFormat.redBufferSize() != -1)
|
|---|
| 1021 | p->cRedBits = d->glFormat.redBufferSize();
|
|---|
| 1022 | if (d->glFormat.greenBufferSize() != -1)
|
|---|
| 1023 | p->cGreenBits = d->glFormat.greenBufferSize();
|
|---|
| 1024 | if (d->glFormat.blueBufferSize() != -1)
|
|---|
| 1025 | p->cBlueBits = d->glFormat.blueBufferSize();
|
|---|
| 1026 | if (deviceIsPixmap())
|
|---|
| 1027 | p->cColorBits = pmDepth;
|
|---|
| 1028 | else
|
|---|
| 1029 | p->cColorBits = 32;
|
|---|
| 1030 | } else {
|
|---|
| 1031 | p->iPixelType = PFD_TYPE_COLORINDEX;
|
|---|
| 1032 | p->cColorBits = 8;
|
|---|
| 1033 | }
|
|---|
| 1034 | if (d->glFormat.alpha())
|
|---|
| 1035 | p->cAlphaBits = d->glFormat.alphaBufferSize() == -1 ? 8 : d->glFormat.alphaBufferSize();
|
|---|
| 1036 | if (d->glFormat.accum()) {
|
|---|
| 1037 | p->cAccumRedBits = p->cAccumGreenBits = p->cAccumBlueBits = p->cAccumAlphaBits =
|
|---|
| 1038 | d->glFormat.accumBufferSize() == -1 ? 16 : d->glFormat.accumBufferSize();
|
|---|
| 1039 | }
|
|---|
| 1040 | if (d->glFormat.stencil())
|
|---|
| 1041 | p->cStencilBits = d->glFormat.stencilBufferSize() == -1 ? 8 : d->glFormat.stencilBufferSize();
|
|---|
| 1042 | p->iLayerType = PFD_MAIN_PLANE;
|
|---|
| 1043 | chosenPfi = ChoosePixelFormat(pdc, p);
|
|---|
| 1044 |
|
|---|
| 1045 | if (!chosenPfi)
|
|---|
| 1046 | qErrnoWarning("QGLContext: ChoosePixelFormat failed");
|
|---|
| 1047 |
|
|---|
| 1048 | // Since the GDI function ChoosePixelFormat() does not handle
|
|---|
| 1049 | // overlay and direct-rendering requests, we must roll our own here
|
|---|
| 1050 |
|
|---|
| 1051 | bool doSearch = chosenPfi <= 0;
|
|---|
| 1052 | PIXELFORMATDESCRIPTOR pfd;
|
|---|
| 1053 | QGLFormat fmt;
|
|---|
| 1054 | if (!doSearch) {
|
|---|
| 1055 | DescribePixelFormat(pdc, chosenPfi, sizeof(PIXELFORMATDESCRIPTOR),
|
|---|
| 1056 | &pfd);
|
|---|
| 1057 | fmt = pfdToQGLFormat(&pfd);
|
|---|
| 1058 | if (d->glFormat.hasOverlay() && !fmt.hasOverlay())
|
|---|
| 1059 | doSearch = true;
|
|---|
| 1060 | else if (!qLogEq(d->glFormat.directRendering(), fmt.directRendering()))
|
|---|
| 1061 | doSearch = true;
|
|---|
| 1062 | else if (deviceIsPixmap() && (!(pfd.dwFlags & PFD_DRAW_TO_BITMAP) ||
|
|---|
| 1063 | pfd.cColorBits != pmDepth))
|
|---|
| 1064 | doSearch = true;
|
|---|
| 1065 | else if (!deviceIsPixmap() && !(pfd.dwFlags & PFD_DRAW_TO_WINDOW))
|
|---|
| 1066 | doSearch = true;
|
|---|
| 1067 | else if (!qLogEq(d->glFormat.rgba(), fmt.rgba()))
|
|---|
| 1068 | doSearch = true;
|
|---|
| 1069 | }
|
|---|
| 1070 |
|
|---|
| 1071 | if (doSearch) {
|
|---|
| 1072 | int pfiMax = DescribePixelFormat(pdc, 0, 0, NULL);
|
|---|
| 1073 | int bestScore = -1;
|
|---|
| 1074 | int bestPfi = -1;
|
|---|
| 1075 | for (int pfi = 1; pfi <= pfiMax; pfi++) {
|
|---|
| 1076 | DescribePixelFormat(pdc, pfi, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
|
|---|
| 1077 | if (!(pfd.dwFlags & PFD_SUPPORT_OPENGL))
|
|---|
| 1078 | continue;
|
|---|
| 1079 | if (deviceIsPixmap() && (!(pfd.dwFlags & PFD_DRAW_TO_BITMAP) ||
|
|---|
| 1080 | pfd.cColorBits != pmDepth))
|
|---|
| 1081 | continue;
|
|---|
| 1082 | if (!deviceIsPixmap() && !(pfd.dwFlags & PFD_DRAW_TO_WINDOW))
|
|---|
| 1083 | continue;
|
|---|
| 1084 |
|
|---|
| 1085 | fmt = pfdToQGLFormat(&pfd);
|
|---|
| 1086 | if (d->glFormat.hasOverlay() && !fmt.hasOverlay())
|
|---|
| 1087 | continue;
|
|---|
| 1088 |
|
|---|
| 1089 | int score = pfd.cColorBits;
|
|---|
| 1090 | if (qLogEq(d->glFormat.depth(), fmt.depth()))
|
|---|
| 1091 | score += pfd.cDepthBits;
|
|---|
| 1092 | if (qLogEq(d->glFormat.alpha(), fmt.alpha()))
|
|---|
| 1093 | score += pfd.cAlphaBits;
|
|---|
| 1094 | if (qLogEq(d->glFormat.accum(), fmt.accum()))
|
|---|
| 1095 | score += pfd.cAccumBits;
|
|---|
| 1096 | if (qLogEq(d->glFormat.stencil(), fmt.stencil()))
|
|---|
| 1097 | score += pfd.cStencilBits;
|
|---|
| 1098 | if (qLogEq(d->glFormat.doubleBuffer(), fmt.doubleBuffer()))
|
|---|
| 1099 | score += 1000;
|
|---|
| 1100 | if (qLogEq(d->glFormat.stereo(), fmt.stereo()))
|
|---|
| 1101 | score += 2000;
|
|---|
| 1102 | if (qLogEq(d->glFormat.directRendering(), fmt.directRendering()))
|
|---|
| 1103 | score += 4000;
|
|---|
| 1104 | if (qLogEq(d->glFormat.rgba(), fmt.rgba()))
|
|---|
| 1105 | score += 8000;
|
|---|
| 1106 | if (score > bestScore) {
|
|---|
| 1107 | bestScore = score;
|
|---|
| 1108 | bestPfi = pfi;
|
|---|
| 1109 | }
|
|---|
| 1110 | }
|
|---|
| 1111 |
|
|---|
| 1112 | if (bestPfi > 0)
|
|---|
| 1113 | chosenPfi = bestPfi;
|
|---|
| 1114 | }
|
|---|
| 1115 | }
|
|---|
| 1116 | return chosenPfi;
|
|---|
| 1117 | }
|
|---|
| 1118 |
|
|---|
| 1119 |
|
|---|
| 1120 |
|
|---|
| 1121 | void QGLContext::reset()
|
|---|
| 1122 | {
|
|---|
| 1123 | Q_D(QGLContext);
|
|---|
| 1124 | // workaround for matrox driver:
|
|---|
| 1125 | // make a cheap call to opengl to force loading of DLL
|
|---|
| 1126 | if (!opengl32dll) {
|
|---|
| 1127 | GLint params;
|
|---|
| 1128 | glGetIntegerv(GL_DEPTH_BITS, ¶ms);
|
|---|
| 1129 | opengl32dll = true;
|
|---|
| 1130 | }
|
|---|
| 1131 |
|
|---|
| 1132 | if (!d->valid)
|
|---|
| 1133 | return;
|
|---|
| 1134 | d->cleanup();
|
|---|
| 1135 | doneCurrent();
|
|---|
| 1136 | if (d->rc)
|
|---|
| 1137 | wglDeleteContext(d->rc);
|
|---|
| 1138 | d->rc = 0;
|
|---|
| 1139 | if (d->win && d->dc)
|
|---|
| 1140 | ReleaseDC(d->win, d->dc);
|
|---|
| 1141 | if (deviceIsPixmap()) {
|
|---|
| 1142 | DeleteDC(d->hbitmap_hdc);
|
|---|
| 1143 | DeleteObject(d->hbitmap);
|
|---|
| 1144 | d->hbitmap_hdc = 0;
|
|---|
| 1145 | d->hbitmap = 0;
|
|---|
| 1146 | }
|
|---|
| 1147 | d->dc = 0;
|
|---|
| 1148 | d->win = 0;
|
|---|
| 1149 | d->pixelFormatId = 0;
|
|---|
| 1150 | d->sharing = false;
|
|---|
| 1151 | d->valid = false;
|
|---|
| 1152 | d->transpColor = QColor();
|
|---|
| 1153 | delete d->cmap;
|
|---|
| 1154 | d->cmap = 0;
|
|---|
| 1155 | d->initDone = false;
|
|---|
| 1156 | qgl_share_reg()->removeShare(this);
|
|---|
| 1157 | }
|
|---|
| 1158 |
|
|---|
| 1159 | //
|
|---|
| 1160 | // NOTE: In a multi-threaded environment, each thread has a current
|
|---|
| 1161 | // context. If we want to make this code thread-safe, we probably
|
|---|
| 1162 | // have to use TLS (thread local storage) for keeping current contexts.
|
|---|
| 1163 | //
|
|---|
| 1164 |
|
|---|
| 1165 | void QGLContext::makeCurrent()
|
|---|
| 1166 | {
|
|---|
| 1167 | Q_D(QGLContext);
|
|---|
| 1168 | if (d->rc == wglGetCurrentContext() || !d->valid) // already current
|
|---|
| 1169 | return;
|
|---|
| 1170 | if (d->win) {
|
|---|
| 1171 | d->dc = GetDC(d->win);
|
|---|
| 1172 | if (!d->dc) {
|
|---|
| 1173 | qwglError("QGLContext::makeCurrent()", "GetDC()");
|
|---|
| 1174 | return;
|
|---|
| 1175 | }
|
|---|
| 1176 | } else if (deviceIsPixmap()) {
|
|---|
| 1177 | d->dc = d->hbitmap_hdc;
|
|---|
| 1178 | }
|
|---|
| 1179 |
|
|---|
| 1180 | HPALETTE hpal = QColormap::hPal();
|
|---|
| 1181 | if (hpal) {
|
|---|
| 1182 | SelectPalette(d->dc, hpal, FALSE);
|
|---|
| 1183 | RealizePalette(d->dc);
|
|---|
| 1184 | }
|
|---|
| 1185 | if (d->glFormat.plane()) {
|
|---|
| 1186 | wglRealizeLayerPalette(d->dc, d->glFormat.plane(), TRUE);
|
|---|
| 1187 | }
|
|---|
| 1188 |
|
|---|
| 1189 | if (wglMakeCurrent(d->dc, d->rc)) {
|
|---|
| 1190 | if (!qgl_context_storage.hasLocalData() && QThread::currentThread())
|
|---|
| 1191 | qgl_context_storage.setLocalData(new QGLThreadContext);
|
|---|
| 1192 | if (qgl_context_storage.hasLocalData())
|
|---|
| 1193 | qgl_context_storage.localData()->context = this;
|
|---|
| 1194 | currentCtx = this;
|
|---|
| 1195 | } else {
|
|---|
| 1196 | qwglError("QGLContext::makeCurrent()", "wglMakeCurrent");
|
|---|
| 1197 | }
|
|---|
| 1198 | }
|
|---|
| 1199 |
|
|---|
| 1200 |
|
|---|
| 1201 | void QGLContext::doneCurrent()
|
|---|
| 1202 | {
|
|---|
| 1203 | Q_D(QGLContext);
|
|---|
| 1204 | currentCtx = 0;
|
|---|
| 1205 | wglMakeCurrent(0, 0);
|
|---|
| 1206 | if (qgl_context_storage.hasLocalData())
|
|---|
| 1207 | qgl_context_storage.localData()->context = 0;
|
|---|
| 1208 | if (deviceIsPixmap() && d->hbitmap) {
|
|---|
| 1209 | QPixmap *pm = static_cast<QPixmap *>(d->paintDevice);
|
|---|
| 1210 | *pm = QPixmap::fromWinHBITMAP(d->hbitmap);
|
|---|
| 1211 | }
|
|---|
| 1212 | if (d->win && d->dc) {
|
|---|
| 1213 | ReleaseDC(d->win, d->dc);
|
|---|
| 1214 | d->dc = 0;
|
|---|
| 1215 | }
|
|---|
| 1216 | }
|
|---|
| 1217 |
|
|---|
| 1218 | void QGLContext::swapBuffers() const
|
|---|
| 1219 | {
|
|---|
| 1220 | Q_D(const QGLContext);
|
|---|
| 1221 | if (d->dc && d->glFormat.doubleBuffer() && !deviceIsPixmap()) {
|
|---|
| 1222 | if (d->glFormat.plane())
|
|---|
| 1223 | wglSwapLayerBuffers(d->dc, WGL_SWAP_OVERLAY1);
|
|---|
| 1224 | else {
|
|---|
| 1225 | if (d->glFormat.hasOverlay())
|
|---|
| 1226 | wglSwapLayerBuffers(d->dc, WGL_SWAP_MAIN_PLANE);
|
|---|
| 1227 | else
|
|---|
| 1228 | SwapBuffers(d->dc);
|
|---|
| 1229 | }
|
|---|
| 1230 | }
|
|---|
| 1231 | }
|
|---|
| 1232 |
|
|---|
| 1233 |
|
|---|
| 1234 | QColor QGLContext::overlayTransparentColor() const
|
|---|
| 1235 | {
|
|---|
| 1236 | return d_func()->transpColor;
|
|---|
| 1237 | }
|
|---|
| 1238 |
|
|---|
| 1239 |
|
|---|
| 1240 | uint QGLContext::colorIndex(const QColor& c) const
|
|---|
| 1241 | {
|
|---|
| 1242 | Q_D(const QGLContext);
|
|---|
| 1243 | if (!isValid())
|
|---|
| 1244 | return 0;
|
|---|
| 1245 | if (d->cmap) {
|
|---|
| 1246 | int idx = d->cmap->find(c.rgb());
|
|---|
| 1247 | if (idx >= 0)
|
|---|
| 1248 | return idx;
|
|---|
| 1249 | if (d->dc && d->glFormat.plane()) {
|
|---|
| 1250 | idx = d->cmap->allocate(c.rgb());
|
|---|
| 1251 | if (idx >= 0) {
|
|---|
| 1252 | COLORREF r = RGB(qRed(c.rgb()),qGreen(c.rgb()),qBlue(c.rgb()));
|
|---|
| 1253 | wglSetLayerPaletteEntries(d->dc, d->glFormat.plane(), idx, 1, &r);
|
|---|
| 1254 | wglRealizeLayerPalette(d->dc, d->glFormat.plane(), TRUE);
|
|---|
| 1255 | return idx;
|
|---|
| 1256 | }
|
|---|
| 1257 | }
|
|---|
| 1258 | return d->cmap->findNearest(c.rgb());
|
|---|
| 1259 | }
|
|---|
| 1260 | QColormap cmap = QColormap::instance();
|
|---|
| 1261 | return cmap.pixel(c) & 0x00ffffff; // Assumes standard palette
|
|---|
| 1262 | }
|
|---|
| 1263 |
|
|---|
| 1264 | void QGLContext::generateFontDisplayLists(const QFont & fnt, int listBase)
|
|---|
| 1265 | {
|
|---|
| 1266 | if (!isValid())
|
|---|
| 1267 | return;
|
|---|
| 1268 |
|
|---|
| 1269 | HDC display_dc = GetDC(0);
|
|---|
| 1270 | HDC tmp_dc = CreateCompatibleDC(display_dc);
|
|---|
| 1271 | HGDIOBJ old_font = SelectObject(tmp_dc, fnt.handle());
|
|---|
| 1272 |
|
|---|
| 1273 | ReleaseDC(0, display_dc);
|
|---|
| 1274 |
|
|---|
| 1275 | if (!wglUseFontBitmaps(tmp_dc, 0, 256, listBase))
|
|---|
| 1276 | qWarning("QGLContext::generateFontDisplayLists: Could not generate display lists for font '%s'", fnt.family().toLatin1().data());
|
|---|
| 1277 |
|
|---|
| 1278 | SelectObject(tmp_dc, old_font);
|
|---|
| 1279 | DeleteDC(tmp_dc);
|
|---|
| 1280 | }
|
|---|
| 1281 |
|
|---|
| 1282 | void *QGLContext::getProcAddress(const QString &proc) const
|
|---|
| 1283 | {
|
|---|
| 1284 | return (void *)wglGetProcAddress(proc.toLatin1());
|
|---|
| 1285 | }
|
|---|
| 1286 |
|
|---|
| 1287 | /*****************************************************************************
|
|---|
| 1288 | QGLWidget Win32/WGL-specific code
|
|---|
| 1289 | *****************************************************************************/
|
|---|
| 1290 |
|
|---|
| 1291 | void QGLWidgetPrivate::init(QGLContext *ctx, const QGLWidget* shareWidget)
|
|---|
| 1292 | {
|
|---|
| 1293 | Q_Q(QGLWidget);
|
|---|
| 1294 | olcx = 0;
|
|---|
| 1295 | initContext(ctx, shareWidget);
|
|---|
| 1296 |
|
|---|
| 1297 | if (q->isValid() && q->context()->format().hasOverlay()) {
|
|---|
| 1298 | olcx = new QGLContext(QGLFormat::defaultOverlayFormat(), q);
|
|---|
| 1299 | if (!olcx->create(shareWidget ? shareWidget->overlayContext() : 0)) {
|
|---|
| 1300 | delete olcx;
|
|---|
| 1301 | olcx = 0;
|
|---|
| 1302 | glcx->d_func()->glFormat.setOverlay(false);
|
|---|
| 1303 | }
|
|---|
| 1304 | } else {
|
|---|
| 1305 | olcx = 0;
|
|---|
| 1306 | }
|
|---|
| 1307 | }
|
|---|
| 1308 |
|
|---|
| 1309 | /*\internal
|
|---|
| 1310 | Store color values in the given colormap.
|
|---|
| 1311 | */
|
|---|
| 1312 | static void qStoreColors(HPALETTE cmap, const QGLColormap & cols)
|
|---|
| 1313 | {
|
|---|
| 1314 | QRgb color;
|
|---|
| 1315 | PALETTEENTRY pe;
|
|---|
| 1316 |
|
|---|
| 1317 | for (int i = 0; i < cols.size(); i++) {
|
|---|
| 1318 | color = cols.entryRgb(i);
|
|---|
| 1319 | pe.peRed = qRed(color);
|
|---|
| 1320 | pe.peGreen = qGreen(color);
|
|---|
| 1321 | pe.peBlue = qBlue(color);
|
|---|
| 1322 | pe.peFlags = 0;
|
|---|
| 1323 |
|
|---|
| 1324 | SetPaletteEntries(cmap, i, 1, &pe);
|
|---|
| 1325 | }
|
|---|
| 1326 | }
|
|---|
| 1327 |
|
|---|
| 1328 | void QGLWidgetPrivate::updateColormap()
|
|---|
| 1329 | {
|
|---|
| 1330 | Q_Q(QGLWidget);
|
|---|
| 1331 | if (!cmap.handle())
|
|---|
| 1332 | return;
|
|---|
| 1333 | HDC hdc = GetDC(q->winId());
|
|---|
| 1334 | SelectPalette(hdc, (HPALETTE) cmap.handle(), TRUE);
|
|---|
| 1335 | qStoreColors((HPALETTE) cmap.handle(), cmap);
|
|---|
| 1336 | RealizePalette(hdc);
|
|---|
| 1337 | ReleaseDC(q->winId(), hdc);
|
|---|
| 1338 | }
|
|---|
| 1339 |
|
|---|
| 1340 | void QGLWidget::setMouseTracking(bool enable)
|
|---|
| 1341 | {
|
|---|
| 1342 | QWidget::setMouseTracking(enable);
|
|---|
| 1343 | }
|
|---|
| 1344 |
|
|---|
| 1345 |
|
|---|
| 1346 | void QGLWidget::resizeEvent(QResizeEvent *)
|
|---|
| 1347 | {
|
|---|
| 1348 | Q_D(QGLWidget);
|
|---|
| 1349 | if (!isValid())
|
|---|
| 1350 | return;
|
|---|
| 1351 | makeCurrent();
|
|---|
| 1352 | if (!d->glcx->initialized())
|
|---|
| 1353 | glInit();
|
|---|
| 1354 | resizeGL(width(), height());
|
|---|
| 1355 | if (d->olcx) {
|
|---|
| 1356 | makeOverlayCurrent();
|
|---|
| 1357 | resizeOverlayGL(width(), height());
|
|---|
| 1358 | }
|
|---|
| 1359 | }
|
|---|
| 1360 |
|
|---|
| 1361 |
|
|---|
| 1362 | const QGLContext* QGLWidget::overlayContext() const
|
|---|
| 1363 | {
|
|---|
| 1364 | return d_func()->olcx;
|
|---|
| 1365 | }
|
|---|
| 1366 |
|
|---|
| 1367 |
|
|---|
| 1368 | void QGLWidget::makeOverlayCurrent()
|
|---|
| 1369 | {
|
|---|
| 1370 | Q_D(QGLWidget);
|
|---|
| 1371 | if (d->olcx) {
|
|---|
| 1372 | d->olcx->makeCurrent();
|
|---|
| 1373 | if (!d->olcx->initialized()) {
|
|---|
| 1374 | initializeOverlayGL();
|
|---|
| 1375 | d->olcx->setInitialized(true);
|
|---|
| 1376 | }
|
|---|
| 1377 | }
|
|---|
| 1378 | }
|
|---|
| 1379 |
|
|---|
| 1380 |
|
|---|
| 1381 | void QGLWidget::updateOverlayGL()
|
|---|
| 1382 | {
|
|---|
| 1383 | Q_D(QGLWidget);
|
|---|
| 1384 | if (d->olcx) {
|
|---|
| 1385 | makeOverlayCurrent();
|
|---|
| 1386 | paintOverlayGL();
|
|---|
| 1387 | if (d->olcx->format().doubleBuffer()) {
|
|---|
| 1388 | if (d->autoSwap)
|
|---|
| 1389 | d->olcx->swapBuffers();
|
|---|
| 1390 | }
|
|---|
| 1391 | else {
|
|---|
| 1392 | glFlush();
|
|---|
| 1393 | }
|
|---|
| 1394 | }
|
|---|
| 1395 | }
|
|---|
| 1396 |
|
|---|
| 1397 |
|
|---|
| 1398 | void QGLWidget::setContext(QGLContext *context,
|
|---|
| 1399 | const QGLContext* shareContext,
|
|---|
| 1400 | bool deleteOldContext)
|
|---|
| 1401 | {
|
|---|
| 1402 | Q_D(QGLWidget);
|
|---|
| 1403 | if (context == 0) {
|
|---|
| 1404 | qWarning("QGLWidget::setContext: Cannot set null context");
|
|---|
| 1405 | return;
|
|---|
| 1406 | }
|
|---|
| 1407 | if (!context->deviceIsPixmap() && context->device() != this) {
|
|---|
| 1408 | qWarning("QGLWidget::setContext: Context must refer to this widget");
|
|---|
| 1409 | return;
|
|---|
| 1410 | }
|
|---|
| 1411 |
|
|---|
| 1412 | if (d->glcx)
|
|---|
| 1413 | d->glcx->doneCurrent();
|
|---|
| 1414 | QGLContext* oldcx = d->glcx;
|
|---|
| 1415 | d->glcx = context;
|
|---|
| 1416 |
|
|---|
| 1417 | bool doShow = false;
|
|---|
| 1418 | if (oldcx && oldcx->d_func()->win == winId() && !d->glcx->deviceIsPixmap()) {
|
|---|
| 1419 | // We already have a context and must therefore create a new
|
|---|
| 1420 | // window since Windows does not permit setting a new OpenGL
|
|---|
| 1421 | // context for a window that already has one set.
|
|---|
| 1422 | doShow = isVisible();
|
|---|
| 1423 | QWidget *pW = static_cast<QWidget *>(parent());
|
|---|
| 1424 | QPoint pos = geometry().topLeft();
|
|---|
| 1425 | setParent(pW, windowFlags());
|
|---|
| 1426 | move(pos);
|
|---|
| 1427 | }
|
|---|
| 1428 |
|
|---|
| 1429 | if (!d->glcx->isValid()) {
|
|---|
| 1430 | bool wasSharing = shareContext || oldcx && oldcx->isSharing();
|
|---|
| 1431 | d->glcx->create(shareContext ? shareContext : oldcx);
|
|---|
| 1432 | // the above is a trick to keep disp lists etc when a
|
|---|
| 1433 | // QGLWidget has been reparented, so remove the sharing
|
|---|
| 1434 | // flag if we don't actually have a sharing context.
|
|---|
| 1435 | if (!wasSharing)
|
|---|
| 1436 | d->glcx->d_ptr->sharing = false;
|
|---|
| 1437 | }
|
|---|
| 1438 |
|
|---|
| 1439 | if (deleteOldContext)
|
|---|
| 1440 | delete oldcx;
|
|---|
| 1441 |
|
|---|
| 1442 | if (doShow)
|
|---|
| 1443 | show();
|
|---|
| 1444 | }
|
|---|
| 1445 |
|
|---|
| 1446 |
|
|---|
| 1447 | bool QGLWidgetPrivate::renderCxPm(QPixmap*)
|
|---|
| 1448 | {
|
|---|
| 1449 | return false;
|
|---|
| 1450 | }
|
|---|
| 1451 |
|
|---|
| 1452 | void QGLWidgetPrivate::cleanupColormaps()
|
|---|
| 1453 | {
|
|---|
| 1454 | Q_Q(QGLWidget);
|
|---|
| 1455 | if (cmap.handle()) {
|
|---|
| 1456 | HDC hdc = GetDC(q->winId());
|
|---|
| 1457 | SelectPalette(hdc, (HPALETTE) GetStockObject(DEFAULT_PALETTE), FALSE);
|
|---|
| 1458 | DeleteObject((HPALETTE) cmap.handle());
|
|---|
| 1459 | ReleaseDC(q->winId(), hdc);
|
|---|
| 1460 | cmap.setHandle(0);
|
|---|
| 1461 | }
|
|---|
| 1462 | return;
|
|---|
| 1463 | }
|
|---|
| 1464 |
|
|---|
| 1465 | const QGLColormap & QGLWidget::colormap() const
|
|---|
| 1466 | {
|
|---|
| 1467 | return d_func()->cmap;
|
|---|
| 1468 | }
|
|---|
| 1469 |
|
|---|
| 1470 | void QGLWidget::setColormap(const QGLColormap & c)
|
|---|
| 1471 | {
|
|---|
| 1472 | Q_D(QGLWidget);
|
|---|
| 1473 | d->cmap = c;
|
|---|
| 1474 |
|
|---|
| 1475 | if (d->cmap.handle()) { // already have an allocated cmap
|
|---|
| 1476 | d->updateColormap();
|
|---|
| 1477 | } else {
|
|---|
| 1478 | LOGPALETTE *lpal = (LOGPALETTE *) malloc(sizeof(LOGPALETTE)
|
|---|
| 1479 | +c.size()*sizeof(PALETTEENTRY));
|
|---|
| 1480 | lpal->palVersion = 0x300;
|
|---|
| 1481 | lpal->palNumEntries = c.size();
|
|---|
| 1482 | d->cmap.setHandle(CreatePalette(lpal));
|
|---|
| 1483 | free(lpal);
|
|---|
| 1484 | d->updateColormap();
|
|---|
| 1485 | }
|
|---|
| 1486 | }
|
|---|
| 1487 |
|
|---|
| 1488 | void QGLExtensions::init()
|
|---|
| 1489 | {
|
|---|
| 1490 | static bool init_done = false;
|
|---|
| 1491 |
|
|---|
| 1492 | if (init_done)
|
|---|
| 1493 | return;
|
|---|
| 1494 | init_done = true;
|
|---|
| 1495 | QGLTempContext temp_ctx(QGLFormat::defaultFormat().directRendering());
|
|---|
| 1496 | init_extensions();
|
|---|
| 1497 | }
|
|---|
| 1498 |
|
|---|
| 1499 | QT_END_NAMESPACE
|
|---|