177QSGRenderLoop *QSGRenderLoop::instance()
181 QSGRhiSupport::checkEnvQSgInfo();
183 s_instance = QSGContext::createWindowManager();
186 QSGRhiSupport *rhiSupport = QSGRhiSupport::instance();
188 QSGRenderLoopType loopType;
189 if (rhiSupport->rhiBackend() != QRhi::OpenGLES2) {
190 loopType = ThreadedRenderLoop;
192 if (QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ThreadedOpenGL))
193 loopType = ThreadedRenderLoop;
195 loopType = BasicRenderLoop;
198 switch (rhiSupport->rhiBackend()) {
200 loopType = BasicRenderLoop;
221 if (qmlNoThreadedRenderer())
222 loopType = BasicRenderLoop;
223 else if (qmlForceThreadedRenderer())
224 loopType = ThreadedRenderLoop;
226 if (Q_UNLIKELY(qEnvironmentVariableIsSet(
"QSG_RENDER_LOOP"))) {
227 const QByteArray loopName = qgetenv(
"QSG_RENDER_LOOP");
228 if (loopName ==
"windows") {
229 qWarning(
"The 'windows' render loop is no longer supported. Using 'basic' instead.");
230 loopType = BasicRenderLoop;
231 }
else if (loopName ==
"basic") {
232 loopType = BasicRenderLoop;
233 }
else if (loopName ==
"threaded") {
234 loopType = ThreadedRenderLoop;
240 case ThreadedRenderLoop:
241 qCDebug(QSG_LOG_INFO,
"threaded render loop");
242 s_instance =
new QSGThreadedRenderLoop();
246 qCDebug(QSG_LOG_INFO,
"basic render loop");
247 s_instance =
new QSGGuiThreadRenderLoop();
252 qAddPostRoutine(QSGRenderLoop::cleanup);
264void QSGRenderLoop::handleContextCreationFailure(QQuickWindow *window)
269 static QSet<QQuickWindow *> recurseGuard;
270 if (recurseGuard.contains(window))
273 recurseGuard.insert(window);
275 QString translatedMessage;
276 QString untranslatedMessage;
277 QQuickWindowPrivate::rhiCreationFailureMessage(QSGRhiSupport::instance()->rhiBackendName(),
279 &untranslatedMessage);
283 const bool signalEmitted =
284 QQuickWindowPrivate::get(window)->emitError(QQuickWindow::ContextNotAvailable,
287 if (!signalEmitted && !QLibraryInfo::isDebugBuild() && !GetConsoleWindow()) {
288 MessageBox(0, (LPCTSTR) translatedMessage.utf16(),
289 (LPCTSTR)(QCoreApplication::applicationName().utf16()),
290 MB_OK | MB_ICONERROR);
294 qFatal(
"%s", qPrintable(untranslatedMessage));
296 recurseGuard.remove(window);
459 QQuickWindowPrivate *cd = QQuickWindowPrivate::get(window);
460 QSGRhiSupport *rhiSupport = QSGRhiSupport::instance();
461 bool ok = data
.rhi !=
nullptr;
471 if (!offscreenSurface)
472 offscreenSurface = rhiSupport->maybeCreateOffscreenSurface(window);
475 QSGRhiSupport::RhiCreateResult rhiResult = rhiSupport->createRhi(window, offscreenSurface, forcePreferSwRenderer);
476 data
.rhi = rhiResult.rhi;
485 data
.rhi->makeThreadLocalNativeContextCurrent();
489 data.sampleCount = rhiSupport->chooseSampleCountForWindowWithRhi(window, data.rhi);
493 QSGDefaultRenderContext::InitParams rcParams;
494 rcParams.rhi = data
.rhi;
496 rcParams.initialSurfacePixelSize = window->size() * window->effectiveDevicePixelRatio();
497 rcParams.maybeSurface = window;
498 cd->context->initialize(&rcParams);
502 handleContextCreationFailure(window);
508 if (data
.rhi && !cd->swapchain) {
516 rhiSupport->prepareWindowForRhi(window);
518 QRhiSwapChain::Flags flags = QRhiSwapChain::UsedAsTransferSource;
519 const QSurfaceFormat requestedFormat = window->requestedFormat();
525 const bool alpha = requestedFormat.alphaBufferSize() > 0;
527 flags |= QRhiSwapChain::SurfaceHasPreMulAlpha;
532 if (requestedFormat.swapInterval() == 0) {
533 qCDebug(QSG_LOG_INFO,
"Swap interval is 0, attempting to disable vsync when presenting.");
534 flags |= QRhiSwapChain::NoVSync;
537 cd->swapchain = data
.rhi->newSwapChain();
538 static bool depthBufferEnabled = qEnvironmentVariableIsEmpty(
"QSG_NO_DEPTH_BUFFER");
539 if (depthBufferEnabled) {
540 cd->depthStencilForSwapchain = data
.rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil,
543 QRhiRenderBuffer::UsedWithSwapChainOnly);
544 cd->swapchain->setDepthStencil(cd->depthStencilForSwapchain);
546 cd->swapchain->setWindow(window);
547 rhiSupport->applySwapChainFormat(cd->swapchain, window);
548 qCDebug(QSG_LOG_INFO,
"MSAA sample count for the swapchain is %d. Alpha channel requested = %s",
551 cd->swapchain->setFlags(flags);
552 cd->rpDescForSwapchain = cd->swapchain->newCompatibleRenderPassDescriptor();
553 cd->swapchain->setRenderPassDescriptor(cd->rpDescForSwapchain);
555 window->installEventFilter(
this);
559 QSGRenderContext *rc = cd->context;
560 pendingRenderContexts.remove(rc);
563 qWarning(
"No QSGRenderContext for window %p, this should not happen", window);
571 auto winDataIt = m_windows.find(window);
572 if (winDataIt == m_windows.end())
579 QQuickWindowPrivate *cd = QQuickWindowPrivate::get(window);
580 if (!cd->isRenderable())
583 if (!cd->updatesEnabled)
589 cd->deliveryAgentPrivate()->flushFrameSynchronousEvents(window);
591 if (!m_windows.contains(window))
594 QSize effectiveOutputSize;
596 effectiveOutputSize = cd->swapchain->surfacePixelSize();
600 if (effectiveOutputSize.isEmpty())
604 Q_TRACE_SCOPE(QSG_renderWindow);
605 QElapsedTimer renderTimer;
606 qint64 renderTime = 0, syncTime = 0, polishTime = 0;
607 const bool profileFrames = QSG_LOG_TIME_RENDERLOOP().isDebugEnabled();
610 Q_TRACE(QSG_polishItems_entry);
611 Q_QUICK_SG_PROFILE_START(QQuickProfiler::SceneGraphPolishFrame);
618 polishTime = renderTimer.nsecsElapsed();
620 Q_TRACE(QSG_polishItems_exit);
621 Q_QUICK_SG_PROFILE_SWITCH(QQuickProfiler::SceneGraphPolishFrame,
622 QQuickProfiler::SceneGraphRenderLoopFrame,
623 QQuickProfiler::SceneGraphPolishPolish);
624 Q_TRACE(QSG_sync_entry);
626 emit window->afterAnimating();
633 Q_ASSERT(!effectiveOutputSize.isEmpty());
634 QSGRhiSupport *rhiSupport = QSGRhiSupport::instance();
635 const QSize previousOutputSize = cd->swapchain->currentPixelSize();
636 if (previousOutputSize != effectiveOutputSize || cd->swapchainJustBecameRenderable) {
637 if (cd->swapchainJustBecameRenderable)
638 qCDebug(QSG_LOG_RENDERLOOP,
"just became exposed");
640 cd->hasActiveSwapchain = cd->swapchain->createOrResize();
641 if (!cd->hasActiveSwapchain) {
642 if (data
.rhi->isDeviceLost()) {
645 }
else if (previousOutputSize.isEmpty() && !swRastFallbackDueToSwapchainFailure && rhiSupport->attemptReinitWithSwRastUponFail()) {
646 qWarning(
"Failed to create swapchain."
647 " Retrying by requesting a software rasterizer, if applicable for the 3D API implementation.");
654 cd->swapchainJustBecameRenderable =
false;
655 cd->hasRenderableSwapchain = cd->hasActiveSwapchain;
657 if (cd->hasActiveSwapchain) {
661 effectiveOutputSize = cd->swapchain->currentPixelSize();
662 qCDebug(QSG_LOG_RENDERLOOP) <<
"rhi swapchain size" << effectiveOutputSize;
664 qWarning(
"Failed to build or resize swapchain");
668 emit window->beforeFrameBegin();
670 Q_ASSERT(data
.rhi == cd->rhi);
671 QRhi::FrameOpResult frameResult = data
.rhi->beginFrame(cd->swapchain);
672 if (frameResult != QRhi::FrameOpSuccess) {
673 if (frameResult == QRhi::FrameOpDeviceLost)
675 else if (frameResult == QRhi::FrameOpError)
676 qWarning(
"Failed to start frame");
678 emit window->afterFrameEnd();
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");
722 lastCompletedGpuTime = cd->swapchain->currentFrameCommandBuffer()->lastCompletedGpuTime();
726 cd->fireFrameSwapped();
728 emit window->afterFrameEnd();
732 swapTime = renderTimer.nsecsElapsed();
734 Q_TRACE(QSG_swap_exit);
735 Q_QUICK_SG_PROFILE_END(QQuickProfiler::SceneGraphRenderLoopFrame,
736 QQuickProfiler::SceneGraphRenderLoopSwap);
739 qCDebug(QSG_LOG_TIME_RENDERLOOP,
740 "[window %p][gui thread] syncAndRender: frame rendered in %dms, polish=%d, sync=%d, render=%d, swap=%d, perWindowFrameDelta=%d",
742 int(swapTime / 1000000),
743 int(polishTime / 1000000),
744 int((syncTime - polishTime) / 1000000),
745 int((renderTime - syncTime) / 1000000),
746 int((swapTime - renderTime) / 1000000),
747 int(data.timeBetweenRenders.restart()));
748 if (!qFuzzyIsNull(lastCompletedGpuTime) && cd->graphicsConfig.timestampsEnabled()) {
749 qCDebug(QSG_LOG_TIME_RENDERLOOP,
"[window %p][gui thread] syncAndRender: last retrieved GPU frame time was %.4f ms",
751 lastCompletedGpuTime * 1000.0);