-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOpenGlOffscreenSurface.cpp
413 lines (353 loc) · 13.1 KB
/
OpenGlOffscreenSurface.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include "OpenGlOffscreenSurface.h"
#include <QtCore/QCoreApplication>
#include <QtGui/QPainter>
OpenGlOffscreenSurface::OpenGlOffscreenSurface(
QScreen* targetScreen,
const QSize& size)
: QOffscreenSurface(targetScreen)
, m_size(size)
{
setFormat(QSurfaceFormat::defaultFormat());
m_initialized = false;
m_updatePending = false;
create(); // Some platforms require this function to be called on the main (GUI) thread
initializeInternal();
}
OpenGlOffscreenSurface::~OpenGlOffscreenSurface()
{
// to delete the FBOs we first need to make the context current
m_context->makeCurrent(this);
// destroy framebuffer objects
if (m_fbo) {
m_fbo->release();
delete m_fbo;
m_fbo = nullptr;
}
if (m_resolvedFbo) {
m_resolvedFbo->release();
delete m_resolvedFbo;
m_resolvedFbo = nullptr;
}
// destroy shader
delete m_blitShader;
m_blitShader = nullptr;
// free context
m_context->doneCurrent();
delete m_context;
m_context = nullptr;
// free paint device
delete m_paintDevice;
m_paintDevice = nullptr;
m_initialized = false;
m_updatePending = false;
destroy();
}
QOpenGLContext* OpenGlOffscreenSurface::context() const
{
return (m_context);
}
QOpenGLFunctions* OpenGlOffscreenSurface::functions() const
{
return (m_functions);
}
GLuint OpenGlOffscreenSurface::framebufferObjectHandle() const
{
return (m_fbo ? m_fbo->handle() : 0);
}
const QOpenGLFramebufferObject* OpenGlOffscreenSurface::getFramebufferObject() const
{
return (m_fbo);
}
QPaintDevice* OpenGlOffscreenSurface::getPaintDevice() const
{
return (m_paintDevice);
}
void OpenGlOffscreenSurface::bindFramebufferObject()
{
if (m_fbo) {
m_fbo->bind();
} else {
QOpenGLFramebufferObject::bindDefault();
}
}
bool OpenGlOffscreenSurface::isValid() const
{
return (m_initialized && m_context && m_fbo);
}
void OpenGlOffscreenSurface::makeCurrent()
{
makeCurrentInternal();
}
void OpenGlOffscreenSurface::makeCurrentInternal()
{
if (isValid()) {
m_context->makeCurrent(this);
} else {
throw ("OpenGlOffscreenSurface::makeCurrent() - Window not yet properly initialized!");
}
}
void OpenGlOffscreenSurface::doneCurrent()
{
if (m_context) {
m_context->doneCurrent();
}
}
QImage OpenGlOffscreenSurface::grabFramebuffer()
{
std::lock_guard <std::mutex> locker(m_mutex);
makeCurrentInternal();
// blit framebuffer to resolve framebuffer first if needed
if (m_fbo->format().samples() > 0) {
// check if we have glFrameBufferBlit support. this is true for desktop OpenGL 3.0+, but not
// OpenGL ES 2.0
if (m_functions_3_0) {
// only blit the color buffer attachment
m_functions_3_0->glBindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo->handle());
m_functions_3_0->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_resolvedFbo->handle());
m_functions_3_0->glBlitFramebuffer(0, 0, bufferSize().width(),
bufferSize().height(), 0, 0, bufferSize().width(),
bufferSize().height(), GL_COLOR_BUFFER_BIT, GL_NEAREST);
m_functions_3_0->glBindFramebuffer(GL_FRAMEBUFFER, 0);
} else {
// we must unbind the FBO here, so we can use its texture and bind the default
// back-buffer
m_functions->glBindFramebuffer(GL_FRAMEBUFFER, m_resolvedFbo->handle());
// now use its texture for drawing in the shader
// --> bind shader and draw textured quad here
// bind regular FBO again
m_functions->glBindFramebuffer(GL_FRAMEBUFFER, m_fbo->handle());
}
// check if OpenGL errors happened
if (GLenum error = m_functions->glGetError() != GL_NO_ERROR) {
qDebug() << "OpenGlOffscreenSurface::grabFramebuffer() - OpenGL error" << error;
}
// now grab from resolve FBO
return (grabFramebufferInternal(m_resolvedFbo));
} else {
// no multi-sampling. grab directly from FBO
return (grabFramebufferInternal(m_fbo));
}
} // OpenGlOffscreenSurface::grabFramebuffer
QImage OpenGlOffscreenSurface::grabFramebufferInternal(QOpenGLFramebufferObject* fbo)
{
QImage image;
// bind framebuffer first
m_functions->glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo->handle());
if (m_functions_3_0) {
m_functions_3_0->glReadBuffer(GL_COLOR_ATTACHMENT0);
}
GLenum internalFormat = fbo->format().internalTextureFormat();
bool hasAlpha = internalFormat == GL_RGBA || internalFormat == GL_BGRA
|| internalFormat == GL_RGBA8;
if (internalFormat == GL_BGRA) {
image = QImage(fbo->size(), hasAlpha ? QImage::Format_ARGB32 : QImage::Format_RGB32);
m_functions->glReadPixels(0, 0, fbo->size().width(),
fbo->size().height(), GL_BGRA, GL_UNSIGNED_BYTE, image.bits());
} else if ((internalFormat == GL_RGBA) || (internalFormat == GL_RGBA8)) {
image = QImage(fbo->size(), hasAlpha ? QImage::Format_RGBA8888 : QImage::Format_RGBX8888);
m_functions->glReadPixels(0, 0, fbo->size().width(),
fbo->size().height(), GL_RGBA, GL_UNSIGNED_BYTE, image.bits());
} else {
qDebug() << "OpenGlOffscreenSurface::grabFramebuffer() - Unsupported framebuffer format"
<< internalFormat << "!";
}
m_functions->glBindFramebuffer(GL_FRAMEBUFFER, m_fbo->handle());
return (image.mirrored());
} // OpenGlOffscreenSurface::grabFramebufferInternal
void OpenGlOffscreenSurface::swapBuffers()
{
swapBuffersInternal();
emit frameSwapped();
}
void OpenGlOffscreenSurface::swapBuffersInternal()
{
// blit framebuffer to back buffer
m_context->makeCurrent(this);
// make sure all paint operation have been processed
m_functions->glFlush();
// check if we have glFrameBufferBlit support. this is true for desktop OpenGL 3.0+, but not
// OpenGL ES 2.0
if (m_functions_3_0) {
// if our framebuffer has multi-sampling, the resolve should be done automagically
m_functions_3_0->glBindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo->handle());
m_functions_3_0->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
// blit all buffers including depth buffer for further rendering
m_functions_3_0->glBlitFramebuffer(0, 0, bufferSize().width(),
bufferSize().height(), 0, 0, bufferSize().width(),
bufferSize().height(), GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT,
GL_NEAREST);
m_functions_3_0->glBindFramebuffer(GL_FRAMEBUFFER, m_fbo->handle());
} else {
// we must unbind the FBO here, so we can use its texture and bind the default back-buffer
m_functions->glBindFramebuffer(GL_FRAMEBUFFER, 0);
// now use its texture for drawing in the shader
// --> bind shader and draw textured quad here
// bind regular FBO again
m_functions->glBindFramebuffer(GL_FRAMEBUFFER, m_fbo->handle());
}
// check if OpenGL errors happened
if (GLenum error = m_functions->glGetError() != GL_NO_ERROR) {
qDebug() << "OpenGlOffscreenSurface::swapBuffersInternal() - OpenGL error" << error;
}
// now swap back buffer to front buffer
m_context->swapBuffers(this);
} // OpenGlOffscreenSurface::swapBuffersInternal
void OpenGlOffscreenSurface::recreateFBOAndPaintDevice()
{
if (m_context && ((m_fbo == nullptr) || (m_fbo->size() != bufferSize()))) {
m_context->makeCurrent(this);
// free old FBOs
if (m_fbo) {
m_fbo->release();
delete m_fbo;
m_fbo = nullptr;
}
if (m_resolvedFbo) {
m_resolvedFbo->release();
delete m_resolvedFbo;
m_resolvedFbo = nullptr;
}
// create new frame buffer
// QOpenGLFramebufferObjectFormat format = QGLInfo::DefaultFramebufferFormat();
// format.setSamples(QGLInfo::HasMultisamplingSupport(m_context) ? 4 : 0);
QOpenGLFramebufferObjectFormat format;
format.setSamples(0);
m_fbo = new QOpenGLFramebufferObject(bufferSize(), format);
if (!m_fbo->isValid()) {
throw ("OpenGlOffscreenSurface::recreateFbo() - Failed to create background FBO!");
}
// clear framebuffer
m_fbo->bind();
m_functions->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
m_fbo->release();
// if multi sampling is requested and supported we need a resolve FBO
if (format.samples() > 0) {
// create resolve framebuffer with only a color attachment
format.setAttachment(QOpenGLFramebufferObject::NoAttachment);
format.setSamples(0);
m_resolvedFbo = new QOpenGLFramebufferObject(bufferSize(), format);
if (!m_resolvedFbo->isValid()) {
throw ("OpenGlOffscreenSurface::recreateFbo() - Failed to create resolve FBO!");
}
// clear resolve framebuffer
m_resolvedFbo->bind();
m_functions->glClear(GL_COLOR_BUFFER_BIT);
m_resolvedFbo->release();
}
}
// create paint device for painting with QPainter if needed
if (!m_paintDevice) {
m_paintDevice = new QOpenGLPaintDevice;
}
// update paint device size if needed
if (m_paintDevice->size() != bufferSize()) {
m_paintDevice->setSize(bufferSize());
}
} // OpenGlOffscreenSurface::recreateFBOAndPaintDevice
void OpenGlOffscreenSurface::initializeInternal()
{
if (!m_initialized.exchange(true)) {
// create OpenGL context. we set the format requested by the user (default:
// QWindow::requestedFormat())
m_context = new QOpenGLContext(this);
m_context->setFormat(format());
if (m_context->create()) {
m_context->makeCurrent(this);
// initialize the OpenGL 2.1 / ES 2.0 functions for this object
m_functions = m_context->functions();
m_functions->initializeOpenGLFunctions();
// try initializing the OpenGL 3.0 functions for this object
m_functions_3_0 = m_context->versionFunctions <QOpenGLFunctions_3_0>();
if (m_functions_3_0) {
m_functions_3_0->initializeOpenGLFunctions();
} else {
// if we do not have OpenGL 3.0 functions, glBlitFrameBuffer is not available, so we
// must do the blit
// using a shader and the framebuffer texture, so we need to create the shader
// here...
// --> allocate m_blitShader, a simple shader for drawing a textured quad
// --> build quad geometry, VBO, whatever
}
// now we have a context, create the FBO
recreateFBOAndPaintDevice();
} else {
m_initialized = false;
delete m_context;
m_context = nullptr;
throw ("Failed to create OpenGL context!");
}
}
} // OpenGlOffscreenSurface::initializeInternal
void OpenGlOffscreenSurface::update()
{
// only queue an update if there's not already an update pending
if (!m_updatePending.exchange(true)) {
QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
}
}
void OpenGlOffscreenSurface::render()
{
std::lock_guard <std::mutex> locker(m_mutex);
// check if we need to initialize stuff
initializeInternal();
// check if we need to call the user initialization
// makeCurrent(); // TODO: may be makeCurrent() must be here, as noted for QOpenGLWidget.initializeGL()
if (!m_initializedGL) {
m_initializedGL = true;
initializeGL();
}
// make context current and bind framebuffer
makeCurrent();
bindFramebufferObject();
// call user paint function
paintGL();
doneCurrent();
// mark that we're done with updating
m_updatePending = false;
} // OpenGlOffscreenSurface::render
void OpenGlOffscreenSurface::exposeEvent(QExposeEvent* e)
{
// render window content if window is exposed
render();
} // OpenGlOffscreenSurface::exposeEvent
void OpenGlOffscreenSurface::resizeEvent(QResizeEvent* e)
{
// call base implementation
resize(e->size());
emit resized();
}
void OpenGlOffscreenSurface::resize(const QSize& newSize)
{
m_mutex.lock();
// make context current first
makeCurrent();
m_size = QSize(newSize);
// update FBO and paint device
recreateFBOAndPaintDevice();
m_mutex.unlock();
// call user-defined resize method
resizeGL(bufferSize().width(), bufferSize().height());
} // OpenGlOffscreenSurface::resize
void OpenGlOffscreenSurface::resize(
int w,
int h)
{
resize(QSize(w, h));
}
bool OpenGlOffscreenSurface::event(QEvent* event)
{
switch (event->type()) {
case QEvent::UpdateLater:
update();
return (true);
case QEvent::UpdateRequest:
render();
return (true);
default:
return (false);
} // switch
} // OpenGlOffscreenSurface::event
QSize OpenGlOffscreenSurface::bufferSize() const
{
return (m_size);
}