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);
238 const int deviceWidth = q->width() * q->devicePixelRatio();
239 const int deviceHeight = q->height() * q->devicePixelRatio();
240 const QSize deviceSize(deviceWidth, deviceHeight);
241 if (updateBehavior > QOpenGLWindow::NoPartialUpdate) {
242 if (!fbo || fbo->size() != deviceSize) {
243 QOpenGLFramebufferObjectFormat fboFormat;
244 fboFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
245 const int samples = q->requestedFormat().samples();
247 if (updateBehavior != QOpenGLWindow::PartialUpdateBlend)
248 fboFormat.setSamples(samples);
250 qWarning(
"QOpenGLWindow: PartialUpdateBlend does not support multisampling");
252 fbo.reset(
new QOpenGLFramebufferObject(deviceSize, fboFormat));
259 paintDevice->setSize(QSize(deviceWidth, deviceHeight));
260 paintDevice->setDevicePixelRatio(q->devicePixelRatio());
261 context->functions()->glViewport(0, 0, deviceWidth, deviceHeight);
263 if (context->functions()->hasOpenGLFeature(QOpenGLFunctions::Framebuffers))
264 context->functions()->glBindFramebuffer(GL_FRAMEBUFFER, context->defaultFramebufferObject());
268 if (updateBehavior > QOpenGLWindow::NoPartialUpdate)
276 if (updateBehavior > QOpenGLWindow::NoPartialUpdate)
279 if (context->functions()->hasOpenGLFeature(QOpenGLFunctions::Framebuffers))
280 context->functions()->glBindFramebuffer(GL_FRAMEBUFFER, context->defaultFramebufferObject());
282 if (updateBehavior == QOpenGLWindow::PartialUpdateBlit && hasFboBlit) {
283 const int deviceWidth = q->width() * q->devicePixelRatio();
284 const int deviceHeight = q->height() * q->devicePixelRatio();
285 QOpenGLExtensions extensions(context.data());
288 extensions.glBlitFramebuffer(0, 0, deviceWidth, deviceHeight,
289 0, 0, deviceWidth, deviceHeight,
290 GL_COLOR_BUFFER_BIT, GL_NEAREST);
291 }
else if (updateBehavior > QOpenGLWindow::NoPartialUpdate) {
292 if (updateBehavior == QOpenGLWindow::PartialUpdateBlend) {
293 context->functions()->glEnable(GL_BLEND);
294 context->functions()->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
296 if (!blitter.isCreated())
299 QRect windowRect(QPoint(0, 0), fbo->size());
300 QMatrix4x4 target = QOpenGLTextureBlitter::targetTransform(windowRect, windowRect);
302 blitter.blit(fbo->texture(), target, QOpenGLTextureBlitter::OriginBottomLeft);
305 if (updateBehavior == QOpenGLWindow::PartialUpdateBlend)
306 context->functions()->glDisable(GL_BLEND);
338QOpenGLWindow::QOpenGLWindow(QOpenGLWindow::UpdateBehavior updateBehavior, QWindow *parent)
339 : QPaintDeviceWindow(*(
new QOpenGLWindowPrivate(
nullptr, updateBehavior)), parent)
341 setSurfaceType(QSurface::OpenGLSurface);
350QOpenGLWindow::QOpenGLWindow(QOpenGLContext *shareContext, UpdateBehavior updateBehavior, QWindow *parent)
351 : QPaintDeviceWindow(*(
new QOpenGLWindowPrivate(shareContext, updateBehavior)), parent)
353 setSurfaceType(QSurface::OpenGLSurface);
428void QOpenGLWindow::makeCurrent()
438 d->context->makeCurrent(
this);
440 if (!d->offscreenSurface) {
441 d->offscreenSurface.reset(
new QOffscreenSurface(screen()));
442 d->offscreenSurface->setFormat(d->context->format());
443 d->offscreenSurface->create();
445 d->context->makeCurrent(d->offscreenSurface.data());
524QImage QOpenGLWindow::grabFramebuffer()
531 const bool hasAlpha = format().hasAlpha();
532 QImage img = qt_gl_read_framebuffer(size() * devicePixelRatio(), hasAlpha, hasAlpha);
533 img.setDevicePixelRatio(devicePixelRatio());