202 qWarning(
"Attempted to initialize QOpenGLWindow without a platform window");
204 context.reset(
new QOpenGLContext);
205 context->setShareContext(shareContext);
206 context->setFormat(q->requestedFormat());
207 if (!context->create())
208 qWarning(
"QOpenGLWindow::beginPaint: Failed to create context");
209 if (!context->makeCurrent(q))
210 qWarning(
"QOpenGLWindow::beginPaint: Failed to make context current");
212 paintDevice.reset(
new QOpenGLWindowPaintDevice(q));
213 if (updateBehavior == QOpenGLWindow::PartialUpdateBlit)
214 hasFboBlit = QOpenGLFramebufferObject::hasOpenGLFramebufferBlit();
225 context->makeCurrent(q);
227 const int deviceWidth = q->width() * q->devicePixelRatio();
228 const int deviceHeight = q->height() * q->devicePixelRatio();
229 const QSize deviceSize(deviceWidth, deviceHeight);
230 if (updateBehavior > QOpenGLWindow::NoPartialUpdate) {
231 if (!fbo || fbo->size() != deviceSize) {
232 QOpenGLFramebufferObjectFormat fboFormat;
233 fboFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
234 const int samples = q->requestedFormat().samples();
236 if (updateBehavior != QOpenGLWindow::PartialUpdateBlend)
237 fboFormat.setSamples(samples);
239 qWarning(
"QOpenGLWindow: PartialUpdateBlend does not support multisampling");
241 fbo.reset(
new QOpenGLFramebufferObject(deviceSize, fboFormat));
248 paintDevice->setSize(QSize(deviceWidth, deviceHeight));
249 paintDevice->setDevicePixelRatio(q->devicePixelRatio());
250 context->functions()->glViewport(0, 0, deviceWidth, deviceHeight);
252 context->functions()->glBindFramebuffer(GL_FRAMEBUFFER, context->defaultFramebufferObject());
256 if (updateBehavior > QOpenGLWindow::NoPartialUpdate)
264 if (updateBehavior > QOpenGLWindow::NoPartialUpdate)
267 context->functions()->glBindFramebuffer(GL_FRAMEBUFFER, context->defaultFramebufferObject());
269 if (updateBehavior == QOpenGLWindow::PartialUpdateBlit && hasFboBlit) {
270 const int deviceWidth = q->width() * q->devicePixelRatio();
271 const int deviceHeight = q->height() * q->devicePixelRatio();
272 QOpenGLExtensions extensions(context.data());
275 extensions.glBlitFramebuffer(0, 0, deviceWidth, deviceHeight,
276 0, 0, deviceWidth, deviceHeight,
277 GL_COLOR_BUFFER_BIT, GL_NEAREST);
278 }
else if (updateBehavior > QOpenGLWindow::NoPartialUpdate) {
279 if (updateBehavior == QOpenGLWindow::PartialUpdateBlend) {
280 context->functions()->glEnable(GL_BLEND);
281 context->functions()->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
283 if (!blitter.isCreated())
286 QRect windowRect(QPoint(0, 0), fbo->size());
287 QMatrix4x4 target = QOpenGLTextureBlitter::targetTransform(windowRect, windowRect);
289 blitter.blit(fbo->texture(), target, QOpenGLTextureBlitter::OriginBottomLeft);
292 if (updateBehavior == QOpenGLWindow::PartialUpdateBlend)
293 context->functions()->glDisable(GL_BLEND);
325QOpenGLWindow::QOpenGLWindow(QOpenGLWindow::UpdateBehavior updateBehavior, QWindow *parent)
326 : QPaintDeviceWindow(*(
new QOpenGLWindowPrivate(
nullptr, updateBehavior)), parent)
328 setSurfaceType(QSurface::OpenGLSurface);
336QOpenGLWindow::QOpenGLWindow(QOpenGLContext *shareContext, UpdateBehavior updateBehavior, QWindow *parent)
337 : QPaintDeviceWindow(*(
new QOpenGLWindowPrivate(shareContext, updateBehavior)), parent)
339 setSurfaceType(QSurface::OpenGLSurface);
414void QOpenGLWindow::makeCurrent()
424 d->context->makeCurrent(
this);
426 if (!d->offscreenSurface) {
427 d->offscreenSurface.reset(
new QOffscreenSurface(screen()));
428 d->offscreenSurface->setFormat(d->context->format());
429 d->offscreenSurface->create();
431 d->context->makeCurrent(d->offscreenSurface.data());
510QImage QOpenGLWindow::grabFramebuffer()
517 const bool hasAlpha = format().hasAlpha();
518 QImage img = qt_gl_read_framebuffer(size() * devicePixelRatio(), hasAlpha, hasAlpha);
519 img.setDevicePixelRatio(devicePixelRatio());