source: trunk/src/opengl/qgl_p.h@ 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.

File size: 19.2 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#ifndef QGL_P_H
43#define QGL_P_H
44
45//
46// W A R N I N G
47// -------------
48//
49// This file is not part of the Qt API. It exists for the convenience
50// of the QGLWidget class. This header file may change from
51// version to version without notice, or even be removed.
52//
53// We mean it.
54//
55
56#include "QtOpenGL/qgl.h"
57#include "QtOpenGL/qglcolormap.h"
58#include "QtCore/qmap.h"
59#include "QtCore/qthread.h"
60#include "QtCore/qthreadstorage.h"
61#include "QtCore/qhash.h"
62#include "QtCore/qatomic.h"
63#include "private/qwidget_p.h"
64#include "qcache.h"
65#include "qglpaintdevice_p.h"
66
67#ifndef QT_OPENGL_ES_1_CL
68#define q_vertexType float
69#define q_vertexTypeEnum GL_FLOAT
70#define f2vt(f) (f)
71#define vt2f(x) (x)
72#define i2vt(i) (float(i))
73#else
74#define FLOAT2X(f) (int( (f) * (65536)))
75#define X2FLOAT(x) (float(x) / 65536.0f)
76#define f2vt(f) FLOAT2X(f)
77#define i2vt(i) ((i)*65536)
78#define vt2f(x) X2FLOAT(x)
79#define q_vertexType GLfixed
80#define q_vertexTypeEnum GL_FIXED
81#endif //QT_OPENGL_ES_1_CL
82
83#ifdef QT_OPENGL_ES
84QT_BEGIN_INCLUDE_NAMESPACE
85#if defined(QT_OPENGL_ES_2)
86#include <EGL/egl.h>
87#else
88#include <GLES/egl.h>
89#endif
90QT_END_INCLUDE_NAMESPACE
91#endif
92
93QT_BEGIN_NAMESPACE
94
95class QGLContext;
96class QGLOverlayWidget;
97class QPixmap;
98class QPixmapFilter;
99#ifdef Q_WS_MAC
100# ifdef qDebug
101# define old_qDebug qDebug
102# undef qDebug
103# endif
104QT_BEGIN_INCLUDE_NAMESPACE
105#ifndef QT_MAC_USE_COCOA
106# include <AGL/agl.h>
107#endif
108QT_END_INCLUDE_NAMESPACE
109# ifdef old_qDebug
110# undef qDebug
111# define qDebug QT_QDEBUG_MACRO
112# undef old_qDebug
113# endif
114class QMacWindowChangeEvent;
115#endif
116
117#ifdef Q_WS_QWS
118class QWSGLWindowSurface;
119#endif
120
121#if defined(QT_OPENGL_ES)
122class QEglContext;
123#endif
124
125QT_BEGIN_INCLUDE_NAMESPACE
126#include <QtOpenGL/private/qglextensions_p.h>
127QT_END_INCLUDE_NAMESPACE
128
129class QGLFormatPrivate
130{
131public:
132 QGLFormatPrivate()
133 : ref(1)
134 {
135 opts = QGL::DoubleBuffer | QGL::DepthBuffer | QGL::Rgba | QGL::DirectRendering | QGL::StencilBuffer;
136 pln = 0;
137 depthSize = accumSize = stencilSize = redSize = greenSize = blueSize = alphaSize = -1;
138 numSamples = -1;
139 swapInterval = -1;
140 }
141 QGLFormatPrivate(const QGLFormatPrivate *other)
142 : ref(1),
143 opts(other->opts),
144 pln(other->pln),
145 depthSize(other->depthSize),
146 accumSize(other->accumSize),
147 stencilSize(other->stencilSize),
148 redSize(other->redSize),
149 greenSize(other->greenSize),
150 blueSize(other->blueSize),
151 alphaSize(other->alphaSize),
152 numSamples(other->numSamples),
153 swapInterval(other->swapInterval)
154 {
155 }
156 QAtomicInt ref;
157 QGL::FormatOptions opts;
158 int pln;
159 int depthSize;
160 int accumSize;
161 int stencilSize;
162 int redSize;
163 int greenSize;
164 int blueSize;
165 int alphaSize;
166 int numSamples;
167 int swapInterval;
168};
169
170class QGLWidgetPrivate : public QWidgetPrivate
171{
172 Q_DECLARE_PUBLIC(QGLWidget)
173public:
174 QGLWidgetPrivate() : QWidgetPrivate()
175 , disable_clear_on_painter_begin(false)
176#ifdef Q_WS_QWS
177 , wsurf(0)
178#endif
179#if defined(Q_WS_X11) && defined(QT_OPENGL_ES)
180 , eglSurfaceWindowId(0)
181#endif
182 {}
183
184 ~QGLWidgetPrivate() {}
185
186 void init(QGLContext *context, const QGLWidget* shareWidget);
187 void initContext(QGLContext *context, const QGLWidget* shareWidget);
188 bool renderCxPm(QPixmap *pixmap);
189 void cleanupColormaps();
190
191 QGLContext *glcx;
192 QGLWidgetGLPaintDevice glDevice;
193 bool autoSwap;
194
195 QGLColormap cmap;
196#ifndef QT_OPENGL_ES
197 QMap<QString, int> displayListCache;
198#endif
199
200 bool disable_clear_on_painter_begin;
201
202#if defined(Q_WS_WIN)
203 void updateColormap();
204 QGLContext *olcx;
205#elif defined(Q_WS_X11)
206 QGLOverlayWidget *olw;
207#if defined(QT_OPENGL_ES)
208 void recreateEglSurface(bool force);
209 WId eglSurfaceWindowId;
210#endif
211#elif defined(Q_WS_MAC)
212 QGLContext *olcx;
213 void updatePaintDevice();
214#elif defined(Q_WS_QWS)
215 QWSGLWindowSurface *wsurf;
216#endif
217};
218
219class QGLContextResource;
220class QGLSharedResourceGuard;
221
222typedef QHash<QString, GLuint> QGLDDSCache;
223
224// QGLContextPrivate has the responsibility of creating context groups.
225// QGLContextPrivate and QGLShareRegister will both maintain the reference counter and destroy
226// context groups when needed.
227// QGLShareRegister has the responsibility of keeping the context pointer up to date.
228class QGLContextGroup
229{
230public:
231 ~QGLContextGroup();
232
233 QGLExtensionFuncs &extensionFuncs() {return m_extensionFuncs;}
234 const QGLContext *context() const {return m_context;}
235 bool isSharing() const { return m_shares.size() >= 2; }
236
237 void addGuard(QGLSharedResourceGuard *guard);
238 void removeGuard(QGLSharedResourceGuard *guard);
239private:
240 QGLContextGroup(const QGLContext *context) : m_context(context), m_guards(0), m_refs(1) { }
241
242 QGLExtensionFuncs m_extensionFuncs;
243 const QGLContext *m_context; // context group's representative
244 QList<const QGLContext *> m_shares;
245 QHash<QGLContextResource *, void *> m_resources;
246 QGLSharedResourceGuard *m_guards; // double-linked list of active guards.
247 QAtomicInt m_refs;
248 QGLDDSCache m_dds_cache;
249
250 void cleanupResources(const QGLContext *ctx);
251
252 friend class QGLShareRegister;
253 friend class QGLContext;
254 friend class QGLContextPrivate;
255 friend class QGLContextResource;
256};
257
258class QGLTexture;
259
260class QGLContextPrivate
261{
262 Q_DECLARE_PUBLIC(QGLContext)
263public:
264 explicit QGLContextPrivate(QGLContext *context) : internal_context(false), q_ptr(context) {group = new QGLContextGroup(context);}
265 ~QGLContextPrivate();
266 QGLTexture *bindTexture(const QImage &image, GLenum target, GLint format,
267 QGLContext::BindOptions options);
268 QGLTexture *bindTexture(const QImage &image, GLenum target, GLint format, const qint64 key,
269 QGLContext::BindOptions options);
270 QGLTexture *bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
271 QGLContext::BindOptions options);
272 QGLTexture *textureCacheLookup(const qint64 key, GLenum target);
273 void init(QPaintDevice *dev, const QGLFormat &format);
274 QImage convertToGLFormat(const QImage &image, bool force_premul, GLenum texture_format);
275 int maxTextureSize();
276
277 void cleanup();
278
279#if defined(Q_WS_WIN)
280 HGLRC rc;
281 HDC dc;
282 WId win;
283 int pixelFormatId;
284 QGLCmap* cmap;
285 HBITMAP hbitmap;
286 HDC hbitmap_hdc;
287#endif
288#if defined(QT_OPENGL_ES)
289 QEglContext *eglContext;
290 EGLSurface eglSurface;
291 void destroyEglSurfaceForDevice();
292#elif defined(Q_WS_X11) || defined(Q_WS_MAC)
293 void* cx;
294#endif
295#if defined(Q_WS_X11) || defined(Q_WS_MAC)
296 void* vi;
297#endif
298#if defined(Q_WS_X11)
299 void* pbuf;
300 quint32 gpm;
301 int screen;
302 QHash<QPixmapData*, QPixmap> boundPixmaps;
303 QGLTexture *bindTextureFromNativePixmap(QPixmapData*, const qint64 key,
304 QGLContext::BindOptions options);
305 static void destroyGlSurfaceForPixmap(QPixmapData*);
306 static void unbindPixmapFromTexture(QPixmapData*);
307#endif
308#if defined(Q_WS_MAC)
309 bool update;
310 void *tryFormat(const QGLFormat &format);
311 void clearDrawable();
312#endif
313 QGLFormat glFormat;
314 QGLFormat reqFormat;
315 GLuint fbo;
316
317 uint valid : 1;
318 uint sharing : 1;
319 uint initDone : 1;
320 uint crWin : 1;
321 uint internal_context : 1;
322 uint version_flags_cached : 1;
323 QPaintDevice *paintDevice;
324 QColor transpColor;
325 QGLContext *q_ptr;
326 QGLFormat::OpenGLVersionFlags version_flags;
327
328 QGLContextGroup *group;
329 GLint max_texture_size;
330
331 GLuint current_fbo;
332 GLuint default_fbo;
333 QPaintEngine *active_engine;
334
335 static inline QGLContextGroup *contextGroup(const QGLContext *ctx) { return ctx->d_ptr->group; }
336
337#ifdef Q_WS_WIN
338 static inline QGLExtensionFuncs& extensionFuncs(const QGLContext *ctx) { return ctx->d_ptr->group->extensionFuncs(); }
339#endif
340
341#if defined(Q_WS_X11) || defined(Q_WS_MAC) || defined(Q_WS_QWS)
342 static QGLExtensionFuncs qt_extensionFuncs;
343 static inline QGLExtensionFuncs& extensionFuncs(const QGLContext *) { return qt_extensionFuncs; }
344#endif
345
346 static void setCurrentContext(QGLContext *context);
347};
348
349// ### make QGLContext a QObject in 5.0 and remove the proxy stuff
350class Q_OPENGL_EXPORT QGLSignalProxy : public QObject
351{
352 Q_OBJECT
353public:
354 QGLSignalProxy() : QObject() {}
355 void emitAboutToDestroyContext(const QGLContext *context) {
356 emit aboutToDestroyContext(context);
357 }
358 static QGLSignalProxy *instance();
359Q_SIGNALS:
360 void aboutToDestroyContext(const QGLContext *context);
361};
362
363// GL extension definitions
364class QGLExtensions {
365public:
366 enum Extension {
367 TextureRectangle = 0x00000001,
368 SampleBuffers = 0x00000002,
369 GenerateMipmap = 0x00000004,
370 TextureCompression = 0x00000008,
371 FragmentProgram = 0x00000010,
372 MirroredRepeat = 0x00000020,
373 FramebufferObject = 0x00000040,
374 StencilTwoSide = 0x00000080,
375 StencilWrap = 0x00000100,
376 PackedDepthStencil = 0x00000200,
377 NVFloatBuffer = 0x00000400,
378 PixelBufferObject = 0x00000800,
379 FramebufferBlit = 0x00001000,
380 NPOTTextures = 0x00002000,
381 BGRATextureFormat = 0x00004000,
382 DDSTextureCompression = 0x00008000,
383 ETC1TextureCompression = 0x00010000,
384 PVRTCTextureCompression = 0x00020000,
385 FragmentShader = 0x00040000
386 };
387 Q_DECLARE_FLAGS(Extensions, Extension)
388
389 static Extensions glExtensions;
390 static bool nvidiaFboNeedsFinish;
391 static void init(); // sys dependent
392 static void init_extensions(); // general: called by init()
393};
394
395Q_DECLARE_OPERATORS_FOR_FLAGS(QGLExtensions::Extensions)
396
397
398class Q_OPENGL_EXPORT QGLShareRegister
399{
400public:
401 QGLShareRegister() {}
402 ~QGLShareRegister() {}
403
404 void addShare(const QGLContext *context, const QGLContext *share);
405 QList<const QGLContext *> shares(const QGLContext *context);
406 void removeShare(const QGLContext *context);
407};
408
409extern Q_OPENGL_EXPORT QGLShareRegister* qgl_share_reg();
410
411// Temporarily make a context current if not already current or
412// shared with the current contex. The previous context is made
413// current when the object goes out of scope.
414class Q_OPENGL_EXPORT QGLShareContextScope
415{
416public:
417 QGLShareContextScope(const QGLContext *ctx)
418 : m_oldContext(0)
419 {
420 QGLContext *currentContext = const_cast<QGLContext *>(QGLContext::currentContext());
421 if (currentContext != ctx && !QGLContext::areSharing(ctx, currentContext)) {
422 m_oldContext = currentContext;
423 m_ctx = const_cast<QGLContext *>(ctx);
424 m_ctx->makeCurrent();
425 } else {
426 m_ctx = currentContext;
427 }
428 }
429
430 operator QGLContext *()
431 {
432 return m_ctx;
433 }
434
435 QGLContext *operator->()
436 {
437 return m_ctx;
438 }
439
440 ~QGLShareContextScope()
441 {
442 if (m_oldContext)
443 m_oldContext->makeCurrent();
444 }
445
446private:
447 QGLContext *m_oldContext;
448 QGLContext *m_ctx;
449};
450
451class QGLTexture {
452public:
453 QGLTexture(QGLContext *ctx = 0, GLuint tx_id = 0, GLenum tx_target = GL_TEXTURE_2D,
454 QGLContext::BindOptions opt = QGLContext::DefaultBindOption)
455 : context(ctx),
456 id(tx_id),
457 target(tx_target),
458 options(opt)
459#if defined(Q_WS_X11)
460 , boundPixmap(0)
461#endif
462 {}
463
464 ~QGLTexture() {
465 if (options & QGLContext::MemoryManagedBindOption) {
466 Q_ASSERT(context);
467 QGLShareContextScope scope(context);
468#if defined(Q_WS_X11)
469 // Although glXReleaseTexImage is a glX call, it must be called while there
470 // is a current context - the context the pixmap was bound to a texture in.
471 // Otherwise the release doesn't do anything and you get BadDrawable errors
472 // when you come to delete the context.
473 if (boundPixmap)
474 QGLContextPrivate::unbindPixmapFromTexture(boundPixmap);
475#endif
476 glDeleteTextures(1, &id);
477 }
478 }
479
480 QGLContext *context;
481 GLuint id;
482 GLenum target;
483
484 QGLContext::BindOptions options;
485
486#if defined(Q_WS_X11)
487 QPixmapData* boundPixmap;
488#endif
489
490 bool canBindCompressedTexture
491 (const char *buf, int len, const char *format, bool *hasAlpha);
492 QSize bindCompressedTexture
493 (const QString& fileName, const char *format = 0);
494 QSize bindCompressedTexture
495 (const char *buf, int len, const char *format = 0);
496 QSize bindCompressedTextureDDS(const char *buf, int len);
497 QSize bindCompressedTexturePVR(const char *buf, int len);
498};
499
500class QGLTextureCache {
501public:
502 QGLTextureCache();
503 ~QGLTextureCache();
504
505 void insert(QGLContext *ctx, qint64 key, QGLTexture *texture, int cost);
506 void remove(quint64 key) { m_cache.remove(key); }
507 bool remove(QGLContext *ctx, GLuint textureId);
508 void removeContextTextures(QGLContext *ctx);
509 int size() { return m_cache.size(); }
510 void setMaxCost(int newMax) { m_cache.setMaxCost(newMax); }
511 int maxCost() {return m_cache.maxCost(); }
512 QGLTexture* getTexture(quint64 key) { return m_cache.object(key); }
513
514 static QGLTextureCache *instance();
515 static void deleteIfEmpty();
516 static void imageCleanupHook(qint64 cacheKey);
517 static void cleanupTextures(QPixmap* pixmap);
518#ifdef Q_WS_X11
519 // X11 needs to catch pixmap data destruction to delete EGL/GLX pixmap surfaces
520 static void cleanupPixmapSurfaces(QPixmap* pixmap);
521#endif
522
523private:
524 QCache<qint64, QGLTexture> m_cache;
525};
526
527
528extern Q_OPENGL_EXPORT QPaintEngine* qt_qgl_paint_engine();
529
530bool qt_gl_preferGL2Engine();
531
532inline GLenum qt_gl_preferredTextureFormat()
533{
534 return (QGLExtensions::glExtensions & QGLExtensions::BGRATextureFormat) && QSysInfo::ByteOrder == QSysInfo::LittleEndian
535 ? GL_BGRA : GL_RGBA;
536}
537
538inline GLenum qt_gl_preferredTextureTarget()
539{
540#if defined(QT_OPENGL_ES_2)
541 return GL_TEXTURE_2D;
542#else
543 return (QGLExtensions::glExtensions & QGLExtensions::TextureRectangle)
544 && !qt_gl_preferGL2Engine()
545 ? GL_TEXTURE_RECTANGLE_NV
546 : GL_TEXTURE_2D;
547#endif
548}
549
550// One resource per group of shared contexts.
551class Q_AUTOTEST_EXPORT QGLContextResource
552{
553public:
554 typedef void (*FreeFunc)(void *);
555 QGLContextResource(FreeFunc f);
556 ~QGLContextResource();
557 // Set resource 'value' for 'key' and all its shared contexts.
558 void insert(const QGLContext *key, void *value);
559 // Return resource for 'key' or a shared context.
560 void *value(const QGLContext *key);
561 // Cleanup 'value' in response to a context group being destroyed.
562 void cleanup(const QGLContext *ctx, void *value);
563private:
564 FreeFunc free;
565 QAtomicInt active;
566};
567
568// Put a guard around a GL object identifier and its context.
569// When the context goes away, a shared context will be used
570// in its place. If there are no more shared contexts, then
571// the identifier is returned as zero - it is assumed that the
572// context destruction cleaned up the identifier in this case.
573class Q_OPENGL_EXPORT QGLSharedResourceGuard
574{
575public:
576 QGLSharedResourceGuard(const QGLContext *context)
577 : m_group(0), m_id(0), m_next(0), m_prev(0)
578 {
579 setContext(context);
580 }
581 QGLSharedResourceGuard(const QGLContext *context, GLuint id)
582 : m_group(0), m_id(id), m_next(0), m_prev(0)
583 {
584 setContext(context);
585 }
586 ~QGLSharedResourceGuard();
587
588 const QGLContext *context() const
589 {
590 return m_group ? m_group->context() : 0;
591 }
592
593 void setContext(const QGLContext *context);
594
595 GLuint id() const
596 {
597 return m_id;
598 }
599
600 void setId(GLuint id)
601 {
602 m_id = id;
603 }
604
605private:
606 QGLContextGroup *m_group;
607 GLuint m_id;
608 QGLSharedResourceGuard *m_next;
609 QGLSharedResourceGuard *m_prev;
610
611 friend class QGLContextGroup;
612};
613
614
615// This class can be used to match GL extensions with doing any mallocs. The
616// class assumes that the GL extension string ends with a space character,
617// which it should do on all conformant platforms. Create the object and pass
618// in a pointer to the extension string, then call match() on each extension
619// that should be matched. The match() function takes the extension name
620// *without* the terminating space character as input.
621
622class QGLExtensionMatcher
623{
624public:
625 QGLExtensionMatcher(const char *str)
626 : gl_extensions(str), gl_extensions_length(qstrlen(str))
627 {}
628
629 bool match(const char *str) {
630 int str_length = qstrlen(str);
631 const char *extensions = gl_extensions;
632 int extensions_length = gl_extensions_length;
633
634 while (1) {
635 // the total length that needs to be matched is the str_length +
636 // the space character that terminates the extension name
637 if (extensions_length < str_length + 1)
638 return false;
639 if (qstrncmp(extensions, str, str_length) == 0 && extensions[str_length] == ' ')
640 return true;
641
642 int split_pos = 0;
643 while (split_pos < extensions_length && extensions[split_pos] != ' ')
644 ++split_pos;
645 ++split_pos; // added for the terminating space character
646 extensions += split_pos;
647 extensions_length -= split_pos;
648 }
649 return false;
650 }
651
652private:
653 const char *gl_extensions;
654 int gl_extensions_length;
655};
656
657QT_END_NAMESPACE
658
659#endif // QGL_P_H
Note: See TracBrowser for help on using the repository browser.