179QSGRenderLoop *QSGRenderLoop::instance()
183 QSGRhiSupport::checkEnvQSgInfo();
185 s_instance = QSGContext::createWindowManager();
188 QSGRhiSupport *rhiSupport = QSGRhiSupport::instance();
190 QSGRenderLoopType loopType;
191 if (rhiSupport->rhiBackend() != QRhi::OpenGLES2) {
192 loopType = ThreadedRenderLoop;
194 if (QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ThreadedOpenGL))
195 loopType = ThreadedRenderLoop;
197 loopType = BasicRenderLoop;
200 switch (rhiSupport->rhiBackend()) {
202 loopType = BasicRenderLoop;
223 if (qmlNoThreadedRenderer())
224 loopType = BasicRenderLoop;
225 else if (qmlForceThreadedRenderer())
226 loopType = ThreadedRenderLoop;
228 if (Q_UNLIKELY(qEnvironmentVariableIsSet(
"QSG_RENDER_LOOP"))) {
229 const QByteArray loopName = qgetenv(
"QSG_RENDER_LOOP");
230 if (loopName ==
"windows") {
231 qWarning(
"The 'windows' render loop is no longer supported. Using 'basic' instead.");
232 loopType = BasicRenderLoop;
233 }
else if (loopName ==
"basic") {
234 loopType = BasicRenderLoop;
235 }
else if (loopName ==
"threaded") {
236 loopType = ThreadedRenderLoop;
242 case ThreadedRenderLoop:
243 qCDebug(QSG_LOG_INFO,
"threaded render loop");
244 s_instance =
new QSGThreadedRenderLoop();
248 qCDebug(QSG_LOG_INFO,
"basic render loop");
249 s_instance =
new QSGGuiThreadRenderLoop();
254 qAddPostRoutine(QSGRenderLoop::cleanup);
266void QSGRenderLoop::handleContextCreationFailure(QQuickWindow *window)
271 static QSet<QQuickWindow *> recurseGuard;
272 if (recurseGuard.contains(window))
275 recurseGuard.insert(window);
277 QString translatedMessage;
278 QString untranslatedMessage;
279 QQuickWindowPrivate::rhiCreationFailureMessage(QSGRhiSupport::instance()->rhiBackendName(),
281 &untranslatedMessage);
285 const bool signalEmitted =
286 QQuickWindowPrivate::get(window)->emitError(QQuickWindow::ContextNotAvailable,
289 if (!signalEmitted && !QLibraryInfo::isDebugBuild() && !GetConsoleWindow()) {
290 MessageBox(0, (LPCTSTR) translatedMessage.utf16(),
291 (LPCTSTR)(QCoreApplication::applicationName().utf16()),
292 MB_OK | MB_ICONERROR);
296 qFatal(
"%s", qPrintable(untranslatedMessage));
298 recurseGuard.remove(window);
458 QQuickWindowPrivate *cd = QQuickWindowPrivate::get(window);
459 QSGRhiSupport *rhiSupport = QSGRhiSupport::instance();
460 bool ok = data
.rhi !=
nullptr;
474 QSGRhiSupport::RhiCreateResult rhiResult =
475 rhiSupport->createRhi(window, data.offscreenSurface, forcePreferSwRenderer);
476 data
.rhi = rhiResult.rhi;
485 data
.rhi->makeThreadLocalNativeContextCurrent();
487 data
.sampleCount = rhiSupport->chooseSampleCountForWindowWithRhi(window, data.rhi);
491 QSGDefaultRenderContext::InitParams rcParams;
492 rcParams.rhi = data
.rhi;
494 rcParams.initialSurfacePixelSize = window->size() * window->effectiveDevicePixelRatio();
495 rcParams.maybeSurface = window;
496 cd->context->initialize(&rcParams);
500 handleContextCreationFailure(window);
506 if (data
.rhi && !cd->swapchain) {
509 rhiSupport->prepareWindowForRhi(window);
511 QRhiSwapChain::Flags flags = QRhiSwapChain::UsedAsTransferSource;
512 const QSurfaceFormat requestedFormat = window->requestedFormat();
518 const bool alpha = requestedFormat.alphaBufferSize() > 0;
520 flags |= QRhiSwapChain::SurfaceHasPreMulAlpha;
525 if (requestedFormat.swapInterval() == 0) {
526 qCDebug(QSG_LOG_INFO,
"Swap interval is 0, attempting to disable vsync when presenting.");
527 flags |= QRhiSwapChain::NoVSync;
530 cd->swapchain = data
.rhi->newSwapChain();
531 static bool depthBufferEnabled = qEnvironmentVariableIsEmpty(
"QSG_NO_DEPTH_BUFFER");
532 if (depthBufferEnabled) {
533 cd->depthStencilForSwapchain = data
.rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil,
536 QRhiRenderBuffer::UsedWithSwapChainOnly
537 | QSGRhiSupport::depthStencilBufferFlags());
538 cd->swapchain->setDepthStencil(cd->depthStencilForSwapchain);
540 cd->swapchain->setWindow(window);
541 rhiSupport->applySwapChainFormat(cd->swapchain, window);
542 qCDebug(QSG_LOG_INFO,
"MSAA sample count for the swapchain is %d. Alpha channel requested = %s",
545 cd->swapchain->setFlags(flags);
546 cd->rpDescForSwapchain = cd->swapchain->newCompatibleRenderPassDescriptor();
547 cd->swapchain->setRenderPassDescriptor(cd->rpDescForSwapchain);
549 window->installEventFilter(
this);
553 QSGRenderContext *rc = cd->context;
554 pendingRenderContexts.remove(rc);
557 qWarning(
"No QSGRenderContext for window %p, this should not happen", window);
565 auto winDataIt = m_windows.find(window);
566 if (winDataIt == m_windows.end())
573 QQuickWindowPrivate *cd = QQuickWindowPrivate::get(window);
574 if (!cd->isRenderable())
577 if (!cd->updatesEnabled)
583 cd->deliveryAgentPrivate()->flushFrameSynchronousEvents(window);
585 if (!m_windows.contains(window))
588 QSize effectiveOutputSize;
590 effectiveOutputSize = cd->swapchain->surfacePixelSize();
594 if (effectiveOutputSize.isEmpty())
598 Q_TRACE_SCOPE(QSG_renderWindow);
599 QElapsedTimer renderTimer;
600 qint64 renderTime = 0, syncTime = 0, polishTime = 0;
601 const bool profileFrames = QSG_LOG_TIME_RENDERLOOP().isDebugEnabled();
604 Q_TRACE(QSG_polishItems_entry);
605 Q_QUICK_SG_PROFILE_START(QQuickProfiler::SceneGraphPolishFrame);
612 polishTime = renderTimer.nsecsElapsed();
614 Q_TRACE(QSG_polishItems_exit);
615 Q_QUICK_SG_PROFILE_SWITCH(QQuickProfiler::SceneGraphPolishFrame,
616 QQuickProfiler::SceneGraphRenderLoopFrame,
617 QQuickProfiler::SceneGraphPolishPolish);
618 Q_TRACE(QSG_sync_entry);
620 emit window->afterAnimating();
627 Q_ASSERT(!effectiveOutputSize.isEmpty());
628 QSGRhiSupport *rhiSupport = QSGRhiSupport::instance();
629 const QSize previousOutputSize = cd->swapchain->currentPixelSize();
630 if (!cd->hasActiveSwapchain || previousOutputSize != effectiveOutputSize || cd->swapchainJustBecameRenderable || data
.swapchainOutOfDate) {
631 if (cd->swapchainJustBecameRenderable)
632 qCDebug(QSG_LOG_RENDERLOOP,
"just became exposed");
634 qCDebug(QSG_LOG_RENDERLOOP,
"swapchain out of date");
636 cd->hasActiveSwapchain = cd->swapchain->createOrResize();
637 if (!cd->hasActiveSwapchain) {
638 cd->hasRenderableSwapchain =
false;
639 if (data
.rhi->isDeviceLost()) {
642 }
else if (previousOutputSize.isEmpty() && !swRastFallbackDueToSwapchainFailure && rhiSupport->attemptReinitWithSwRastUponFail()) {
643 qWarning(
"Failed to create swapchain."
644 " Retrying by requesting a software rasterizer, if applicable for the 3D API implementation.");
649 qWarning(
"Failed to build or resize swapchain");
653 cd->swapchainJustBecameRenderable =
false;
655 cd->hasRenderableSwapchain = cd->hasActiveSwapchain;
660 effectiveOutputSize = cd->swapchain->currentPixelSize();
661 qCDebug(QSG_LOG_RENDERLOOP) <<
"rhi swapchain size" << effectiveOutputSize;
664 emit window->beforeFrameBegin();
666 Q_ASSERT(data
.rhi == cd->rhi);
667 QRhi::FrameOpResult frameResult = data
.rhi->beginFrame(cd->swapchain);
668 if (frameResult != QRhi::FrameOpSuccess) {
669 if (frameResult == QRhi::FrameOpDeviceLost)
671 else if (frameResult == QRhi::FrameOpError)
672 qWarning(
"Failed to start frame");
674 emit window->afterFrameEnd();
675 if (frameResult == QRhi::FrameOpSwapChainOutOfDate) {
687 data
.rhi->makeThreadLocalNativeContextCurrent();
689 cd->syncSceneGraph();
693 syncTime = renderTimer.nsecsElapsed();
695 Q_TRACE(QSG_sync_exit);
696 Q_QUICK_SG_PROFILE_RECORD(QQuickProfiler::SceneGraphRenderLoopFrame,
697 QQuickProfiler::SceneGraphRenderLoopSync);
698 Q_TRACE(QSG_render_entry);
700 cd->renderSceneGraph();
703 renderTime = renderTimer.nsecsElapsed();
704 Q_TRACE(QSG_render_exit);
705 Q_QUICK_SG_PROFILE_RECORD(QQuickProfiler::SceneGraphRenderLoopFrame,
706 QQuickProfiler::SceneGraphRenderLoopRender);
707 Q_TRACE(QSG_swap_entry);
709 const bool needsPresent = alsoSwap && window->isVisible();
710 double lastCompletedGpuTime = 0;
712 QRhi::EndFrameFlags flags;
714 flags |= QRhi::SkipPresent;
715 QRhi::FrameOpResult frameResult = data
.rhi->endFrame(cd->swapchain, flags);
716 if (frameResult != QRhi::FrameOpSuccess) {
717 if (frameResult == QRhi::FrameOpDeviceLost)
719 else if (frameResult == QRhi::FrameOpError)
720 qWarning(
"Failed to end frame");
721 if (frameResult == QRhi::FrameOpSwapChainOutOfDate) {
726 lastCompletedGpuTime = cd->swapchain->currentFrameCommandBuffer()->lastCompletedGpuTime();
730 cd->fireFrameSwapped();
732 emit window->afterFrameEnd();
736 swapTime = renderTimer.nsecsElapsed();
738 Q_TRACE(QSG_swap_exit);
739 Q_QUICK_SG_PROFILE_END(QQuickProfiler::SceneGraphRenderLoopFrame,
740 QQuickProfiler::SceneGraphRenderLoopSwap);
743 qCDebug(QSG_LOG_TIME_RENDERLOOP,
744 "[window %p][gui thread] syncAndRender: frame rendered in %dms, polish=%d, sync=%d, render=%d, swap=%d, perWindowFrameDelta=%d",
746 int(swapTime / 1000000),
747 int(polishTime / 1000000),
748 int((syncTime - polishTime) / 1000000),
749 int((renderTime - syncTime) / 1000000),
750 int((swapTime - renderTime) / 1000000),
751 int(data.timeBetweenRenders.restart()));
752 if (!qFuzzyIsNull(lastCompletedGpuTime) && cd->graphicsConfig.timestampsEnabled()) {
753 qCDebug(QSG_LOG_TIME_RENDERLOOP,
"[window %p][gui thread] syncAndRender: last retrieved GPU frame time was %.4f ms",
755 lastCompletedGpuTime * 1000.0);