206 qWarning(
"Attempted to initialize QOpenGLWindow without a platform window");
208 context.reset(
new QOpenGLContext);
209 context->setShareContext(shareContext);
210 context->setFormat(q->requestedFormat());
211 if (!context->create())
212 qWarning(
"QOpenGLWindow::beginPaint: Failed to create context");
213 if (!context->makeCurrent(q))
214 qWarning(
"QOpenGLWindow::beginPaint: Failed to make context current");
216 if (updateBehavior > QOpenGLWindow::NoPartialUpdate
217 && !context->functions()->hasOpenGLFeature(QOpenGLFunctions::Framebuffers))
219 qWarning(
"QOpenGLWindow: Framebuffer objects are not supported, falling back to NoPartialUpdate");
220 updateBehavior = QOpenGLWindow::NoPartialUpdate;
223 paintDevice.reset(
new QOpenGLWindowPaintDevice(q));
224 if (updateBehavior == QOpenGLWindow::PartialUpdateBlit)
225 hasFboBlit = QOpenGLFramebufferObject::hasOpenGLFramebufferBlit();
236 context->makeCurrent(q);
240 const QSize deviceSize = q->size() * q->devicePixelRatio();
241 const int deviceWidth = deviceSize.width();
242 const int deviceHeight = deviceSize.height();
243 if (updateBehavior > QOpenGLWindow::NoPartialUpdate) {
244 if (!fbo || fbo->size() != deviceSize) {
245 QOpenGLFramebufferObjectFormat fboFormat;
246 fboFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
247 const int samples = q->requestedFormat().samples();
249 if (updateBehavior != QOpenGLWindow::PartialUpdateBlend)
250 fboFormat.setSamples(samples);
252 qWarning(
"QOpenGLWindow: PartialUpdateBlend does not support multisampling");
254 fbo.reset(
new QOpenGLFramebufferObject(deviceSize, fboFormat));
261 paintDevice->setSize(QSize(deviceWidth, deviceHeight));
262 paintDevice->setDevicePixelRatio(q->devicePixelRatio());
263 context->functions()->glViewport(0, 0, deviceWidth, deviceHeight);
265 if (context->functions()->hasOpenGLFeature(QOpenGLFunctions::Framebuffers))
266 context->functions()->glBindFramebuffer(GL_FRAMEBUFFER, context->defaultFramebufferObject());
270 if (updateBehavior > QOpenGLWindow::NoPartialUpdate)
278 if (updateBehavior > QOpenGLWindow::NoPartialUpdate)
281 if (context->functions()->hasOpenGLFeature(QOpenGLFunctions::Framebuffers))
282 context->functions()->glBindFramebuffer(GL_FRAMEBUFFER, context->defaultFramebufferObject());
284 if (updateBehavior == QOpenGLWindow::PartialUpdateBlit && hasFboBlit) {
285 const QSize deviceSize = q->size() * q->devicePixelRatio();
286 const int deviceWidth = deviceSize.width();
287 const int deviceHeight = deviceSize.height();
288 QOpenGLExtensions extensions(context.data());
291 extensions.glBlitFramebuffer(0, 0, deviceWidth, deviceHeight,
292 0, 0, deviceWidth, deviceHeight,
293 GL_COLOR_BUFFER_BIT, GL_NEAREST);
294 }
else if (updateBehavior > QOpenGLWindow::NoPartialUpdate) {
295 if (updateBehavior == QOpenGLWindow::PartialUpdateBlend) {
296 context->functions()->glEnable(GL_BLEND);
297 context->functions()->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
299 if (!blitter.isCreated())
302 QRect windowRect(QPoint(0, 0), fbo->size());
303 QMatrix4x4 target = QOpenGLTextureBlitter::targetTransform(windowRect, windowRect);
305 blitter.blit(fbo->texture(), target, QOpenGLTextureBlitter::OriginBottomLeft);
308 if (updateBehavior == QOpenGLWindow::PartialUpdateBlend)
309 context->functions()->glDisable(GL_BLEND);
341QOpenGLWindow::QOpenGLWindow(QOpenGLWindow::UpdateBehavior updateBehavior, QWindow *parent)
342 : QPaintDeviceWindow(*(
new QOpenGLWindowPrivate(
nullptr, updateBehavior)), parent)
344 setSurfaceType(QSurface::OpenGLSurface);
353QOpenGLWindow::QOpenGLWindow(QOpenGLContext *shareContext, UpdateBehavior updateBehavior, QWindow *parent)
354 : QPaintDeviceWindow(*(
new QOpenGLWindowPrivate(shareContext, updateBehavior)), parent)
356 setSurfaceType(QSurface::OpenGLSurface);
431void QOpenGLWindow::makeCurrent()
441 d->context->makeCurrent(
this);
443 if (!d->offscreenSurface) {
444 d->offscreenSurface.reset(
new QOffscreenSurface(screen()));
445 d->offscreenSurface->setFormat(d->context->format());
446 d->offscreenSurface->create();
448 d->context->makeCurrent(d->offscreenSurface.data());
527QImage QOpenGLWindow::grabFramebuffer()
534 const bool hasAlpha = format().hasAlpha();
535 QImage img = qt_gl_read_framebuffer(size() * devicePixelRatio(), hasAlpha, hasAlpha);
536 img.setDevicePixelRatio(devicePixelRatio());