Qt 4.8
Public Functions | Static Public Functions | Private Functions | Static Private Functions | Properties | Static Private Attributes | List of all members
QBBGLContext Class Reference

#include <qbbglcontext.h>

Inheritance diagram for QBBGLContext:
QPlatformGLContext

Public Functions

virtual void doneCurrent ()
 Reimplement in subclass to release current context. More...
 
virtual void * getProcAddress (const QString &procName)
 Reimplement in subclass to native getProcAddr calls. More...
 
virtual void makeCurrent ()
 Reimplement in subclass to do makeCurrent on native GL context. More...
 
virtual QPlatformWindowFormat platformWindowFormat () const
 QWidget has the function qplatformWindowFormat(). More...
 
 QBBGLContext (QBBWindow *platformWindow)
 
void resizeSurface (const QSize &size)
 
QSize surfaceSize () const
 
virtual void swapBuffers ()
 Reimplement in subclass to native swap buffers calls. More...
 
virtual ~QBBGLContext ()
 
- Public Functions inherited from QPlatformGLContext
 QPlatformGLContext ()
 All subclasses needs to specify the platformWindow. More...
 
virtual ~QPlatformGLContext ()
 If this is the current context for the thread, doneCurrent is called. More...
 

Static Public Functions

static void initialize ()
 
static void shutdown ()
 
- Static Public Functions inherited from QPlatformGLContext
static const QPlatformGLContextcurrentContext ()
 Returns the last context which called makeCurrent. More...
 

Private Functions

void createSurface ()
 
void destroySurface ()
 
bool isCurrent () const
 

Static Private Functions

static EGLint * contextAttrs ()
 

Properties

EGLConfig mEglConfig
 
EGLContext mEglContext
 
EGLSurface mEglSurface
 
QBBWindowmPlatformWindow
 
QSize mSurfaceSize
 
QPlatformWindowFormat mWindowFormat
 

Static Private Attributes

static EGLDisplay sEglDisplay = EGL_NO_DISPLAY
 

Additional Inherited Members

- Protected Variables inherited from QPlatformGLContext
QScopedPointer< QPlatformGLContextPrivated_ptr
 

Detailed Description

Definition at line 55 of file qbbglcontext.h.

Constructors and Destructors

◆ QBBGLContext()

QBBGLContext::QBBGLContext ( QBBWindow platformWindow)

Definition at line 57 of file qbbglcontext.cpp.

59  mPlatformWindow(platformWindow),
60  mEglSurface(EGL_NO_SURFACE)
61 {
62 #if defined(QBBGLCONTEXT_DEBUG)
63  qDebug() << "QBBGLContext::QBBGLContext - w=" << mPlatformWindow->widget();
64 #endif
65 
66  // verify rendering API is correct
67  QPlatformWindowFormat format = mPlatformWindow->widget()->platformWindowFormat();
68  if (format.windowApi() != QPlatformWindowFormat::OpenGL) {
69  qFatal("QBB: window API is not OpenGL");
70  }
71 
72  // set current rendering API
73  EGLBoolean eglResult = eglBindAPI(EGL_OPENGL_ES_API);
74  if (eglResult != EGL_TRUE) {
75  qFatal("QBB: failed to set EGL API, err=%d", eglGetError());
76  }
77 
78  // get colour channel sizes from window format
79  int alphaSize = format.alphaBufferSize();
80  int redSize = format.redBufferSize();
81  int greenSize = format.greenBufferSize();
82  int blueSize = format.blueBufferSize();
83 
84  // check if all channels are don't care
85  if (alphaSize == -1 && redSize == -1 && greenSize == -1 && blueSize == -1) {
86 
87  // set colour channels based on depth of window's screen
88  int depth = platformWindow->screen()->depth();
89  if (depth == 32) {
90  // SCREEN_FORMAT_RGBA8888
91  alphaSize = 8;
92  redSize = 8;
93  greenSize = 8;
94  blueSize = 8;
95  } else {
96  // SCREEN_FORMAT_RGB565
97  alphaSize = 0;
98  redSize = 5;
99  greenSize = 6;
100  blueSize = 5;
101  }
102 
103  } else {
104 
105  // choose best match based on supported pixel formats
106  if (alphaSize <= 0 && redSize <= 5 && greenSize <= 6 && blueSize <= 5) {
107  // SCREEN_FORMAT_RGB565
108  alphaSize = 0;
109  redSize = 5;
110  greenSize = 6;
111  blueSize = 5;
112  } else {
113  // SCREEN_FORMAT_RGBA8888
114  alphaSize = 8;
115  redSize = 8;
116  greenSize = 8;
117  blueSize = 8;
118  }
119  }
120 
121  // update colour channel sizes in window format
122  format.setAlphaBufferSize(alphaSize);
123  format.setRedBufferSize(redSize);
124  format.setGreenBufferSize(greenSize);
125  format.setBlueBufferSize(blueSize);
126 
127  // select EGL config based on requested window format
129  if (mEglConfig == 0) {
130  qFatal("QBB: failed to find EGL config");
131  }
132 
133  mEglContext = eglCreateContext(sEglDisplay, mEglConfig, EGL_NO_CONTEXT, contextAttrs());
134  if (mEglContext == EGL_NO_CONTEXT) {
135  qFatal("QBB: failed to create EGL context, err=%d", eglGetError());
136  }
137 
138  // query/cache window format of selected EGL config
140 }
int greenBufferSize() const
Returns the green buffer size.
EGLConfig q_configFromQPlatformWindowFormat(EGLDisplay display, const QPlatformWindowFormat &format, bool highestPixelFormat, int surfaceType)
The QPlatformWindowFormat class specifies the display format of an OpenGL rendering context and if po...
void setBlueBufferSize(int size)
Set the preferred blue buffer size to size.
Q_CORE_EXPORT void qDebug(const char *,...)
QBBScreen * screen() const
Definition: qbbwindow.h:93
void setAlphaBufferSize(int size)
Set the preferred alpha buffer size to size.
QBBWindow * mPlatformWindow
Definition: qbbglcontext.h:79
EGLConfig mEglConfig
Definition: qbbglcontext.h:78
virtual int depth() const
Reimplement in subclass to return current depth of the screen.
Definition: qbbscreen.cpp:216
int alphaBufferSize() const
Returns the alpha buffer size.
EGLContext mEglContext
Definition: qbbglcontext.h:80
static EGLint * contextAttrs()
EGLSurface mEglSurface
Definition: qbbglcontext.h:81
Q_CORE_EXPORT void qFatal(const char *,...)
QPlatformWindowFormat qt_qPlatformWindowFormatFromConfig(EGLDisplay display, const EGLConfig config)
int blueBufferSize() const
Returns the blue buffer size.
QPlatformWindowFormat mWindowFormat
Definition: qbbglcontext.h:77
int redBufferSize() const
Returns the red buffer size.
void setRedBufferSize(int size)
Set the preferred red buffer size to size.
QPlatformGLContext()
All subclasses needs to specify the platformWindow.
static EGLDisplay sEglDisplay
Definition: qbbglcontext.h:75
void setGreenBufferSize(int size)
Set the preferred green buffer size to size.
QWidget * widget() const
Returnes the widget which belongs to the QPlatformWindow.

◆ ~QBBGLContext()

QBBGLContext::~QBBGLContext ( )
virtual

Definition at line 142 of file qbbglcontext.cpp.

143 {
144 #if defined(QBBGLCONTEXT_DEBUG)
145  qDebug() << "QBBGLContext::~QBBGLContext - w=" << mPlatformWindow->widget();
146 #endif
147 
148  // cleanup EGL context if it exists
149  if (mEglContext != EGL_NO_CONTEXT) {
150  eglDestroyContext(sEglDisplay, mEglContext);
151  }
152 
153  // cleanup EGL surface if it exists
154  destroySurface();
155 }
Q_CORE_EXPORT void qDebug(const char *,...)
QBBWindow * mPlatformWindow
Definition: qbbglcontext.h:79
EGLContext mEglContext
Definition: qbbglcontext.h:80
void destroySurface()
static EGLDisplay sEglDisplay
Definition: qbbglcontext.h:75
QWidget * widget() const
Returnes the widget which belongs to the QPlatformWindow.

Functions

◆ contextAttrs()

EGLint * QBBGLContext::contextAttrs ( )
staticprivate

Definition at line 303 of file qbbglcontext.cpp.

Referenced by QBBGLContext().

304 {
305  // choose EGL settings based on OpenGL version
306 #if defined(QT_OPENGL_ES_2)
307  static EGLint attrs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
308  return attrs;
309 #else
310  return NULL;
311 #endif
312 }

◆ createSurface()

void QBBGLContext::createSurface ( )
private

Definition at line 319 of file qbbglcontext.cpp.

Referenced by makeCurrent(), and resizeSurface().

320 {
321 #if defined(QBBGLCONTEXT_DEBUG)
322  qDebug() << "QBBGLContext::createSurface - w=" << mPlatformWindow->widget();
323 #endif
324 
325  // create EGL surface
326  mEglSurface = eglCreateWindowSurface(sEglDisplay, mEglConfig, (EGLNativeWindowType)mPlatformWindow->winId(), NULL);
327  if (mEglSurface == EGL_NO_SURFACE) {
328  qFatal("QBB: failed to create EGL surface, err=%d", eglGetError());
329  }
330 }
NativeWindowType EGLNativeWindowType
Definition: qegl_p.h:116
Q_CORE_EXPORT void qDebug(const char *,...)
QBBWindow * mPlatformWindow
Definition: qbbglcontext.h:79
EGLConfig mEglConfig
Definition: qbbglcontext.h:78
EGLSurface mEglSurface
Definition: qbbglcontext.h:81
Q_CORE_EXPORT void qFatal(const char *,...)
virtual WId winId() const
Reimplement in subclasses to return a handle to the native window.
Definition: qbbwindow.h:70
static EGLDisplay sEglDisplay
Definition: qbbglcontext.h:75
QWidget * widget() const
Returnes the widget which belongs to the QPlatformWindow.

◆ destroySurface()

void QBBGLContext::destroySurface ( )
private

Definition at line 332 of file qbbglcontext.cpp.

Referenced by resizeSurface(), and ~QBBGLContext().

333 {
334 #if defined(QBBGLCONTEXT_DEBUG)
335  qDebug() << "QBBGLContext::destroySurface - w=" << mPlatformWindow->widget();
336 #endif
337 
338  // destroy EGL surface if it exists
339  if (mEglSurface != EGL_NO_SURFACE) {
340  EGLBoolean eglResult = eglDestroySurface(sEglDisplay, mEglSurface);
341  if (eglResult != EGL_TRUE) {
342  qFatal("QBB: failed to destroy EGL surface, err=%d", eglGetError());
343  }
344  }
345 }
Q_CORE_EXPORT void qDebug(const char *,...)
QBBWindow * mPlatformWindow
Definition: qbbglcontext.h:79
EGLSurface mEglSurface
Definition: qbbglcontext.h:81
Q_CORE_EXPORT void qFatal(const char *,...)
static EGLDisplay sEglDisplay
Definition: qbbglcontext.h:75
QWidget * widget() const
Returnes the widget which belongs to the QPlatformWindow.

◆ doneCurrent()

void QBBGLContext::doneCurrent ( )
virtual

Reimplement in subclass to release current context.

Typically this is calling makeCurrent with 0 "surface"

Reimplemented from QPlatformGLContext.

Definition at line 214 of file qbbglcontext.cpp.

Referenced by resizeSurface().

215 {
216 #if defined(QBBGLCONTEXT_DEBUG)
217  qDebug() << "QBBGLContext::doneCurrent - w=" << mPlatformWindow->widget();
218 #endif
219 
220  // call the parent method
222 
223  // set current rendering API
224  EGLBoolean eglResult = eglBindAPI(EGL_OPENGL_ES_API);
225  if (eglResult != EGL_TRUE) {
226  qFatal("QBB: failed to set EGL API, err=%d", eglGetError());
227  }
228 
229  // clear curent EGL context and unbind EGL surface
230  eglResult = eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
231  if (eglResult != EGL_TRUE) {
232  qFatal("QBB: failed to clear current EGL context, err=%d", eglGetError());
233  }
234 }
virtual void doneCurrent()
Reimplement in subclass to release current context.
Q_CORE_EXPORT void qDebug(const char *,...)
QBBWindow * mPlatformWindow
Definition: qbbglcontext.h:79
Q_CORE_EXPORT void qFatal(const char *,...)
static EGLDisplay sEglDisplay
Definition: qbbglcontext.h:75
QWidget * widget() const
Returnes the widget which belongs to the QPlatformWindow.

◆ getProcAddress()

void * QBBGLContext::getProcAddress ( const QString procName)
virtual

Reimplement in subclass to native getProcAddr calls.

Note: its convenient to use qPrintable(const QString &str) to get the const char * pointer

Implements QPlatformGLContext.

Definition at line 261 of file qbbglcontext.cpp.

262 {
263 #if defined(QBBGLCONTEXT_DEBUG)
264  qDebug() << "QBBGLContext::getProcAddress - w=" << mPlatformWindow->widget();
265 #endif
266 
267  // set current rendering API
268  EGLBoolean eglResult = eglBindAPI(EGL_OPENGL_ES_API);
269  if (eglResult != EGL_TRUE) {
270  qFatal("QBB: failed to set EGL API, err=%d", eglGetError());
271  }
272 
273  // lookup EGL extension function pointer
274  return (void *)eglGetProcAddress( procName.toAscii().constData() );
275 }
Q_CORE_EXPORT void qDebug(const char *,...)
QBBWindow * mPlatformWindow
Definition: qbbglcontext.h:79
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
Q_CORE_EXPORT void qFatal(const char *,...)
QByteArray toAscii() const Q_REQUIRED_RESULT
Returns an 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4014
QWidget * widget() const
Returnes the widget which belongs to the QPlatformWindow.

◆ initialize()

void QBBGLContext::initialize ( )
static

Definition at line 157 of file qbbglcontext.cpp.

Referenced by QBBIntegration::QBBIntegration().

158 {
159  // initialize connection to EGL
160  sEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
161  if (sEglDisplay == EGL_NO_DISPLAY) {
162  qFatal("QBB: failed to obtain EGL display");
163  }
164 
165  EGLBoolean eglResult = eglInitialize(sEglDisplay, NULL, NULL);
166  if (eglResult != EGL_TRUE) {
167  qFatal("QBB: failed to initialize EGL display, err=%d", eglGetError());
168  }
169 
170  // choose EGL settings based on OpenGL version
171 #if defined(QT_OPENGL_ES_2)
172  EGLint renderableType = EGL_OPENGL_ES2_BIT;
173 #else
174  EGLint renderableType = EGL_OPENGL_ES_BIT;
175 #endif
176 }
Q_CORE_EXPORT void qFatal(const char *,...)
static EGLDisplay sEglDisplay
Definition: qbbglcontext.h:75

◆ isCurrent()

bool QBBGLContext::isCurrent ( ) const
private

Definition at line 314 of file qbbglcontext.cpp.

Referenced by resizeSurface().

315 {
316  return (eglGetCurrentContext() == mEglContext);
317 }
EGLContext mEglContext
Definition: qbbglcontext.h:80

◆ makeCurrent()

void QBBGLContext::makeCurrent ( )
virtual

Reimplement in subclass to do makeCurrent on native GL context.

Reimplemented from QPlatformGLContext.

Definition at line 184 of file qbbglcontext.cpp.

Referenced by resizeSurface().

185 {
186 #if defined(QBBGLCONTEXT_DEBUG)
187  qDebug() << "QBBGLContext::makeCurrent - w=" << mPlatformWindow->widget();
188 #endif
189 
190  if (!mPlatformWindow->hasBuffers()) {
191  // NOTE: create a dummy EGL surface since Qt will call makeCurrent() before
192  // setting the geometry on the associated window
193  mSurfaceSize = QSize(1, 1);
195  createSurface();
196  }
197 
198  // call the parent method
200 
201  // set current rendering API
202  EGLBoolean eglResult = eglBindAPI(EGL_OPENGL_ES_API);
203  if (eglResult != EGL_TRUE) {
204  qFatal("QBB: failed to set EGL API, err=%d", eglGetError());
205  }
206 
207  // set current EGL context and bind with EGL surface
208  eglResult = eglMakeCurrent(sEglDisplay, mEglSurface, mEglSurface, mEglContext);
209  if (eglResult != EGL_TRUE) {
210  qFatal("QBB: failed to set current EGL context, err=%d", eglGetError());
211  }
212 }
bool hasBuffers() const
Definition: qbbwindow.h:74
void setBufferSize(const QSize &size)
Definition: qbbwindow.cpp:288
Q_CORE_EXPORT void qDebug(const char *,...)
QBBWindow * mPlatformWindow
Definition: qbbglcontext.h:79
EGLContext mEglContext
Definition: qbbglcontext.h:80
virtual void makeCurrent()
Reimplement in subclass to do makeCurrent on native GL context.
EGLSurface mEglSurface
Definition: qbbglcontext.h:81
Q_CORE_EXPORT void qFatal(const char *,...)
void createSurface()
QSize mSurfaceSize
Definition: qbbglcontext.h:82
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
static EGLDisplay sEglDisplay
Definition: qbbglcontext.h:75
QWidget * widget() const
Returnes the widget which belongs to the QPlatformWindow.

◆ platformWindowFormat()

virtual QPlatformWindowFormat QBBGLContext::platformWindowFormat ( ) const
inlinevirtual

QWidget has the function qplatformWindowFormat().

That function is for the application programmer to request the format of the window and the context that he wants.

Reimplement this function in a subclass to indicate what format the glContext actually has.

Implements QPlatformGLContext.

Definition at line 69 of file qbbglcontext.h.

Referenced by QBBWindow::setBufferSize().

69 { return mWindowFormat; }
QPlatformWindowFormat mWindowFormat
Definition: qbbglcontext.h:77

◆ resizeSurface()

void QBBGLContext::resizeSurface ( const QSize size)

Definition at line 277 of file qbbglcontext.cpp.

Referenced by QBBGLWindowSurface::beginPaint(), platformWindowFormat(), and swapBuffers().

278 {
279  // need to destroy surface so make sure its not current
280  bool restoreCurrent = false;
281  if (isCurrent()) {
282  doneCurrent();
283  restoreCurrent = true;
284  }
285 
286  // destroy old EGL surface
287  destroySurface();
288 
289  // resize window's buffers
291 
292  // re-create EGL surface with new size
293  mSurfaceSize = size;
294  createSurface();
295 
296  // make context current again
297  if (restoreCurrent) {
298  makeCurrent();
300  }
301 }
virtual void makeCurrent()
Reimplement in subclass to do makeCurrent on native GL context.
void setBufferSize(const QSize &size)
Definition: qbbwindow.cpp:288
void update()
Updates the widget unless updates are disabled or the widget is hidden.
Definition: qwidget.cpp:10883
QBBWindow * mPlatformWindow
Definition: qbbglcontext.h:79
bool isCurrent() const
void destroySurface()
void createSurface()
QSize mSurfaceSize
Definition: qbbglcontext.h:82
virtual void doneCurrent()
Reimplement in subclass to release current context.
QWidget * widget() const
Returnes the widget which belongs to the QPlatformWindow.

◆ shutdown()

void QBBGLContext::shutdown ( )
static

Definition at line 178 of file qbbglcontext.cpp.

Referenced by QBBIntegration::~QBBIntegration().

179 {
180  // close connection to EGL
181  eglTerminate(sEglDisplay);
182 }
static EGLDisplay sEglDisplay
Definition: qbbglcontext.h:75

◆ surfaceSize()

QSize QBBGLContext::surfaceSize ( ) const
inline

Definition at line 72 of file qbbglcontext.h.

Referenced by QBBGLWindowSurface::beginPaint(), and QBBGLPaintDevice::size().

72 { return mSurfaceSize; }
QSize mSurfaceSize
Definition: qbbglcontext.h:82

◆ swapBuffers()

void QBBGLContext::swapBuffers ( )
virtual

Reimplement in subclass to native swap buffers calls.

Implements QPlatformGLContext.

Definition at line 236 of file qbbglcontext.cpp.

Referenced by QBBGLWindowSurface::flush().

237 {
238 #if defined(QBBGLCONTEXT_DEBUG)
239  qDebug() << "QBBGLContext::swapBuffers - w=" << mPlatformWindow->widget();
240 #endif
241 
242  // set current rendering API
243  EGLBoolean eglResult = eglBindAPI(EGL_OPENGL_ES_API);
244  if (eglResult != EGL_TRUE) {
245  qFatal("QBB: failed to set EGL API, err=%d", eglGetError());
246  }
247 
248  // post EGL surface to window
249  eglResult = eglSwapBuffers(sEglDisplay, mEglSurface);
250  if (eglResult != EGL_TRUE) {
251  qFatal("QBB: failed to swap EGL buffers, err=%d", eglGetError());
252  }
253 
254  // resize surface if window was resized
256  if (s != mSurfaceSize) {
257  resizeSurface(s);
258  }
259 }
virtual QRect geometry() const
Returnes the current geometry of a window.
Q_CORE_EXPORT void qDebug(const char *,...)
void resizeSurface(const QSize &size)
QSize size() const
Returns the size of the rectangle.
Definition: qrect.h:309
QBBWindow * mPlatformWindow
Definition: qbbglcontext.h:79
EGLSurface mEglSurface
Definition: qbbglcontext.h:81
Q_CORE_EXPORT void qFatal(const char *,...)
QSize mSurfaceSize
Definition: qbbglcontext.h:82
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
static EGLDisplay sEglDisplay
Definition: qbbglcontext.h:75
QWidget * widget() const
Returnes the widget which belongs to the QPlatformWindow.

Properties

◆ mEglConfig

EGLConfig QBBGLContext::mEglConfig
private

Definition at line 78 of file qbbglcontext.h.

Referenced by createSurface(), and QBBGLContext().

◆ mEglContext

EGLContext QBBGLContext::mEglContext
private

Definition at line 80 of file qbbglcontext.h.

Referenced by isCurrent(), makeCurrent(), QBBGLContext(), and ~QBBGLContext().

◆ mEglSurface

EGLSurface QBBGLContext::mEglSurface
private

Definition at line 81 of file qbbglcontext.h.

Referenced by createSurface(), destroySurface(), makeCurrent(), and swapBuffers().

◆ mPlatformWindow

QBBWindow* QBBGLContext::mPlatformWindow
private

◆ mSurfaceSize

QSize QBBGLContext::mSurfaceSize
private

Definition at line 82 of file qbbglcontext.h.

Referenced by makeCurrent(), resizeSurface(), surfaceSize(), and swapBuffers().

◆ mWindowFormat

QPlatformWindowFormat QBBGLContext::mWindowFormat
private

Definition at line 77 of file qbbglcontext.h.

Referenced by platformWindowFormat(), and QBBGLContext().

◆ sEglDisplay

EGLDisplay QBBGLContext::sEglDisplay = EGL_NO_DISPLAY
staticprivate

The documentation for this class was generated from the following files: