From 11af78340a536bc1566c1577238204fafbdee69f Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Wed, 2 Dec 2026 00:54:29 -0500 Subject: [PATCH] fix(gamescope): keep forwarded buffers alive while waiting for scanout, and composite at full rate (#3560) * fix(gamescope): a pending scanout wait keeps its forwarded buffer alive Patch 0147 waits for scanout on a forwarded client buffer with a raw pointer to its CWaylandFb. If the buffer was shown again and the next host release found scanout already over, that release dropped the last reference while the earlier wait was still queued, and the wait then called OnScanoutDone on a freed buffer. The wait now holds a reference until it finishes. * fix(gamescope): composite every frame when the host releases buffers before scanout Holding a forwarded client buffer until scanout ends (0147) costs a three-buffer client a third of the refresh rate: Steam's UI ran at 80 fps on a 120 Hz screen. Once the backend sees a forwarded buffer released while it is still being scanned out, it composites every frame into its own output images, which 0146 reuses at latch. The Steam UI runs at 120 fps again on the Pocket FIT. * Pin the gamescope build with the scanout wait reference and full-rate compositing --- tools/gamescope/PATCHES.md | 12 +- ...7-forwarded-buffers-wait-for-scanout.patch | 12 +- ...te-when-host-releases-before-scanout.patch | 116 ++++++++++++++++++ tools/gamescope/release.env | 4 +- ...test_gamescope_forwarded_buffer_scanout.py | 16 ++- 5 files changed, 152 insertions(+), 8 deletions(-) create mode 100644 tools/gamescope/patches/0148-composite-when-host-releases-before-scanout.patch diff --git a/tools/gamescope/PATCHES.md b/tools/gamescope/PATCHES.md index f322c5f38b..9c322dbff4 100644 --- a/tools/gamescope/PATCHES.md +++ b/tools/gamescope/PATCHES.md @@ -494,5 +494,15 @@ gamescope returned the buffer to the client on that release, and a client whose fences attached to a dma-buf then drew into it while the display was still scanning it out, which showed as black tiles in compressed buffers. A forwarded client buffer now stays held until `poll(POLLOUT)` on its dma-buf reports that the display has finished with it, on a waiter thread so nothing else blocks. A release that arrives after the -buffer was shown again is dropped, so the later release decides. Composite output images keep the 0146 path, and a +buffer was shown again is dropped, so the later release decides. A pending wait holds its own reference on the +buffer, since that later release can drop the host's reference before the wait ends. Composite output images keep the 0146 path, and a host that waits for the fence itself releases buffers that are already idle, so nothing changes there. + +## 0148 Wayland backend: composite every frame when the host releases buffers before scanout + +With 0147 a forwarded client buffer stays held until the display has finished scanning it out, about one refresh after +the host releases it. Steam's web helper cycles three buffers of its own, so forwarding them held the Steam UI at 80 fps +on a 120 Hz screen (frames alternately one and two refreshes apart). The first time a forwarded buffer comes back while +it is still being scanned out, the backend now composites every frame into its own output images. Those are reused at +latch under 0146, and the client's buffers return as soon as the composite has read them: the Steam UI then runs at +the full 120 fps. Hosts that release a buffer only after scanout never take this path and keep forwarding. diff --git a/tools/gamescope/patches/0147-forwarded-buffers-wait-for-scanout.patch b/tools/gamescope/patches/0147-forwarded-buffers-wait-for-scanout.patch index 928f1ba9b0..61e3af5b09 100644 --- a/tools/gamescope/patches/0147-forwarded-buffers-wait-for-scanout.patch +++ b/tools/gamescope/patches/0147-forwarded-buffers-wait-for-scanout.patch @@ -1,5 +1,5 @@ diff --git a/src/Backends/WaylandBackend.cpp b/src/Backends/WaylandBackend.cpp -index 12f0f50..a0fedd4 100644 +index 12f0f50..d9db2e6 100644 --- a/src/Backends/WaylandBackend.cpp +++ b/src/Backends/WaylandBackend.cpp @@ -3,24 +3,28 @@ @@ -31,7 +31,7 @@ index 12f0f50..a0fedd4 100644 #include #include "wlr_begin.hpp" -@@ -485,44 +489,82 @@ namespace gamescope +@@ -485,44 +489,86 @@ namespace gamescope // One backing plane plus every layer steamcompmgr can hand us. CWaylandPlane m_Planes[k_nMaxLayers + 1]; @@ -78,8 +78,12 @@ index 12f0f50..a0fedd4 100644 + , m_nFd{ nFd } + , m_ulAcquireSerial{ ulAcquireSerial } + { ++ // A later acquire and release can drop the compositor's reference before this wait ends. ++ m_pFb->IncRef(); + } + ++ ~CWaylandScanoutRelease() { m_pFb->DecRef(); } ++ + int GetFD() override { return m_nFd; } + void OnPollOut() override { Finish(); } + void OnPollHangUp() override { Finish(); } @@ -115,7 +119,7 @@ index 12f0f50..a0fedd4 100644 ~CWaylandInputThread(); bool Init( CWaylandBackend *pBackend ); -@@ -969,73 +1011,108 @@ namespace gamescope +@@ -969,73 +1015,108 @@ namespace gamescope .action = WAYLAND_NULL(), }; const zwp_primary_selection_source_v1_listener CWaylandBackend::s_PrimarySelectionSourceListener = @@ -235,7 +239,7 @@ index 12f0f50..a0fedd4 100644 , m_pBackend( pBackend ) , m_Planes{ this, this, this, this, this, this, this, this, this } { -@@ -2349,25 +2426,26 @@ namespace gamescope +@@ -2349,25 +2430,26 @@ namespace gamescope pDmaBuf->format, 0u ); diff --git a/tools/gamescope/patches/0148-composite-when-host-releases-before-scanout.patch b/tools/gamescope/patches/0148-composite-when-host-releases-before-scanout.patch new file mode 100644 index 0000000000..e7f872ac0e --- /dev/null +++ b/tools/gamescope/patches/0148-composite-when-host-releases-before-scanout.patch @@ -0,0 +1,116 @@ +diff --git a/src/Backends/WaylandBackend.cpp b/src/Backends/WaylandBackend.cpp +index d9db2e6..5c4cd86 100644 +--- a/src/Backends/WaylandBackend.cpp ++++ b/src/Backends/WaylandBackend.cpp +@@ -804,24 +804,31 @@ namespace gamescope + { + auto iter = m_pOutputs.find( pOutput ); + if ( iter == m_pOutputs.end() ) + return nullptr; + + return &iter->second; + } + + wl_region *GetEmptyRegion() const { return m_pEmptyRegion; } + wl_region *GetFullRegion() const { return m_pFullRegion; } + CWaylandFb *GetBlackFb() const { return m_BlackFb.get(); } + ++ bool HostReleasesBeforeScanout() const { return m_bHostReleasesBeforeScanout; } ++ void OnHostReleasedBeforeScanout() ++ { ++ if ( !m_bHostReleasesBeforeScanout.exchange( true ) ) ++ xdg_log.infof( "Host releases buffers before scanout ends: compositing every frame into our own images" ); ++ } ++ + void OnConnectorDestroyed( CWaylandConnector *pConnector ) + { + m_pFocusConnector.compare_exchange_strong( pConnector, nullptr ); + } + + private: + + void Wayland_Registry_Global( wl_registry *pRegistry, uint32_t uName, const char *pInterface, uint32_t uVersion ); + static const wl_registry_listener s_RegistryListener; + + void Wayland_Modifier( zwp_linux_dmabuf_v1 *pDmabuf, uint32_t uFormat, uint32_t uModifierHi, uint32_t uModifierLo ); + +@@ -871,24 +878,25 @@ namespace gamescope + + wl_display *m_pDisplay = nullptr; + wl_shm *m_pShm = nullptr; + wl_compositor *m_pCompositor = nullptr; + wp_single_pixel_buffer_manager_v1 *m_pSinglePixelBufferManager = nullptr; + wl_subcompositor *m_pSubcompositor = nullptr; + zwp_linux_dmabuf_v1 *m_pLinuxDmabuf = nullptr; + xdg_wm_base *m_pXdgWmBase = nullptr; + wp_viewporter *m_pViewporter = nullptr; + wl_region *m_pEmptyRegion = nullptr; + wl_region *m_pFullRegion = nullptr; + Rc m_BlackFb; ++ std::atomic m_bHostReleasesBeforeScanout = { false }; + OwningRc m_pOwnedBlackFb; + OwningRc m_pBlackTexture; + wp_presentation *m_pPresentation = nullptr; + droiddeck_refresh_v1 *m_pHostRefresh = nullptr; + std::array m_HostRefreshRates{}; + std::atomic m_uHostRefreshRateCount = { 0 }; + std::vector m_PendingHostRefreshRates; + int m_nSentHostRefresh = 0; + int m_nSentHostFrameRate = 0; + frog_color_management_factory_v1 *m_pFrogColorMgmtFactory = nullptr; + wp_color_manager_v1 *m_pWPColorManager = nullptr; + wp_image_description_v1 *m_pWPImageDescriptions[ GamescopeAppTextureColorspace_Count ]{}; +@@ -1083,24 +1091,25 @@ namespace gamescope + xdg_log.errorf( "Compositor released us but we were not acquired. Oh no." ); + } + + void CWaylandFb::Wayland_Buffer_Release( wl_buffer *pBuffer ) + { + assert( m_pHostBuffer ); + assert( m_pHostBuffer == pBuffer ); + + xdg_log.debugf( "buffer_release: %p", pBuffer ); + + if ( ShowsClientBuffer() && gamescope::DmabufScanoutPending( m_nDmabufFd ) ) + { ++ m_pBackend->OnHostReleasedBeforeScanout(); + int nFd = fcntl( m_nDmabufFd, F_DUPFD_CLOEXEC, 0 ); + if ( nFd >= 0 ) + { + uint64_t ulSerial; + { + std::scoped_lock lock( m_AcquireMutex ); + ulSerial = m_ulAcquireSerial; + } + auto *pWait = new CWaylandScanoutRelease( this, nFd, ulSerial ); + if ( CWaylandScanoutRelease::Waiter().AddWaitable( pWait, EPOLLOUT | EPOLLHUP | EPOLLONESHOT ) ) + return; + delete pWait; +@@ -1194,24 +1203,27 @@ namespace gamescope + plane.Present( nullptr ); + } + else + { + // TODO: Dedupe some of this composite check code between us and drm.cpp + bool bLayer0ScreenSize = close_enough(pFrameInfo->layers.get( 0 ).scale.x, 1.0f) && close_enough(pFrameInfo->layers.get( 0 ).scale.y, 1.0f); + + GamescopeUpscaleFilter eLayer0Filter = pFrameInfo->layers.get( 0 ).filter; + bool bNeedsCompositeFromFilter = ( eLayer0Filter == GamescopeUpscaleFilter::NEAREST || + eLayer0Filter == GamescopeUpscaleFilter::PIXEL ) && !bLayer0ScreenSize; + + bNeedsFullComposite |= cv_composite_force; ++ // A forwarded buffer would stay out of its client's hands until scanout ends, which caps a ++ // three-image client at two thirds of the refresh rate. Our own output images are reused at latch. ++ bNeedsFullComposite |= m_pBackend->HostReleasesBeforeScanout(); + bNeedsFullComposite |= cv_wayland_overlay_planes >= 0 && pFrameInfo->layers.count() > 1 + cv_wayland_overlay_planes; + bNeedsFullComposite |= pFrameInfo->useFSRLayer0; + bNeedsFullComposite |= pFrameInfo->useNISLayer0; + bNeedsFullComposite |= pFrameInfo->useSGSRLayer0; + bNeedsFullComposite |= pFrameInfo->blurLayer0; + bNeedsFullComposite |= bNeedsCompositeFromFilter; + bNeedsFullComposite |= g_bColorSliderInUse; + bNeedsFullComposite |= pFrameInfo->bFadingOut; + bNeedsFullComposite |= !g_reshade_effect.empty(); + + if ( g_bOutputHDREnabled ) + bNeedsFullComposite |= g_bHDRItmEnable; diff --git a/tools/gamescope/release.env b/tools/gamescope/release.env index f32967c4f2..bab97af41f 100644 --- a/tools/gamescope/release.env +++ b/tools/gamescope/release.env @@ -1,3 +1,3 @@ GAMESCOPE_REPO=Droid-Deck/DroidDeck-Components -GAMESCOPE_TAG=gamescope-3.16.29-cb9c4861 -GAMESCOPE_SHA256=89a3d70124a13bb5de0b4c67ff75d748a5d7ba6f29590194621754e3550b1f31 +GAMESCOPE_TAG=gamescope-3.16.29-17928792 +GAMESCOPE_SHA256=b25680d5f81c8ba1f3e0f6f49894686a148baa296647c3bf3dafa3a04697b7c8 diff --git a/tools/tests/test_gamescope_forwarded_buffer_scanout.py b/tools/tests/test_gamescope_forwarded_buffer_scanout.py index 77e6f32a30..79a581407f 100644 --- a/tools/tests/test_gamescope_forwarded_buffer_scanout.py +++ b/tools/tests/test_gamescope_forwarded_buffer_scanout.py @@ -136,10 +136,21 @@ int main( int argc, char **argv ) { assert( fb.refs == 2 ); d.scanning(); fb.Wayland_Buffer_Release( fb.m_pHostBuffer ); - assert( fb.refs == 2 && fb.m_bCompositorAcquired ); + assert( fb.refs == 3 && fb.m_bCompositorAcquired ); d.done(); fire(); assert( fb.refs == 1 && !fb.m_bCompositorAcquired && xdg_log.oh_no == 0 ); + } else if ( which == "stale-wait-keeps-buffer" ) { + fb.client = true; + fb.OnCompositorAcquire(); + d.scanning(); + fb.Wayland_Buffer_Release( fb.m_pHostBuffer ); + fb.OnCompositorAcquire(); + d.done(); + fb.Wayland_Buffer_Release( fb.m_pHostBuffer ); + assert( fb.refs == 2 && !fb.m_bCompositorAcquired ); + fire(); + assert( fb.refs == 1 && xdg_log.oh_no == 0 ); } else if ( which == "client-idle" ) { fb.client = true; fb.OnCompositorAcquire(); @@ -214,6 +225,9 @@ class ForwardedBufferScanoutTest(unittest.TestCase): def test_buffer_shown_again_waits_for_its_next_release(self): self.scenario("shown-again") + def test_pending_wait_keeps_the_buffer_after_a_later_release(self): + self.scenario("stale-wait-keeps-buffer") + def test_buffer_without_dmabuf_is_released_at_once(self): self.scenario("no-dmabuf")