diff --git a/.github/workflows/build-gamescope.yml b/.github/workflows/build-gamescope.yml index f18c9ad84e..1c0e28c49e 100644 --- a/.github/workflows/build-gamescope.yml +++ b/.github/workflows/build-gamescope.yml @@ -2,25 +2,10 @@ name: Build gamescope (patched) on: push: - branches: [main] - paths: - - 'tools/gamescope/build-in-arch.sh' - - 'tools/gamescope/patches/**' - - 'tools/gamescope/runtime-sonames.txt' - - '.github/workflows/build-gamescope.yml' - workflow_dispatch: - inputs: - tag: - description: 'Release tag to publish as (e.g. gamescope-3.16.29-p1); empty = artifact only' - required: false - default: '' + branches: [build/thor-producer-fences-17928792] permissions: - contents: write - -concurrency: - group: ${{ inputs.tag && format('gamescope-tag-{0}', inputs.tag) || format('gamescope-{0}-{1}', github.event_name, github.ref) }} - cancel-in-progress: ${{ github.event_name == 'push' }} + contents: read jobs: build: @@ -28,6 +13,8 @@ jobs: timeout-minutes: 120 steps: - uses: actions/checkout@v4 + with: + persist-credentials: false - name: Build in Arch Linux ARM run: | set -euo pipefail @@ -39,25 +26,3 @@ jobs: path: | gamescope.tzst gamescope.tzst.sha256 - - uses: actions/create-github-app-token@v2 - id: components-token - if: github.event_name == 'workflow_dispatch' && inputs.tag != '' - with: - app-id: ${{ vars.DROIDDECK_CI_APP_ID }} - private-key: ${{ secrets.DROIDDECK_CI_APP_KEY }} - owner: Droid-Deck - repositories: DroidDeck-Components - - name: Publish - if: github.event_name == 'workflow_dispatch' && inputs.tag != '' - env: - GH_TOKEN: ${{ steps.components-token.outputs.token }} - GH_REPO: Droid-Deck/DroidDeck-Components - TAG: ${{ inputs.tag }} - run: | - set -euo pipefail - if gh release view "$TAG" >/dev/null 2>&1; then - echo "::error::$TAG is already published in DroidDeck-Components, and published tags are never replaced: use a new tag" - exit 1 - fi - gh release create "$TAG" --latest=false --prerelease=false --title "$TAG" --notes "gamescope 3.16.29 with tools/gamescope/patches ($GITHUB_REPOSITORY $GITHUB_SHA, run $GITHUB_RUN_ID); staged by the apk over /usr/local/bin/gamescope. sha256 $(cut -d' ' -f1 gamescope.tzst.sha256)" gamescope.tzst gamescope.tzst.sha256 - gh release edit "$TAG" --latest=false --prerelease=false diff --git a/tools/gamescope/patches/0998-probe-producer-syncfile-transport.patch b/tools/gamescope/patches/0998-probe-producer-syncfile-transport.patch new file mode 100644 index 0000000000..3d96323da7 --- /dev/null +++ b/tools/gamescope/patches/0998-probe-producer-syncfile-transport.patch @@ -0,0 +1,656 @@ +diff --git a/protocol/droiddeck-frame-acquire-v1.xml b/protocol/droiddeck-frame-acquire-v1.xml +new file mode 100644 +index 0000000..f056b6e +--- /dev/null ++++ b/protocol/droiddeck-frame-acquire-v1.xml +@@ -0,0 +1,43 @@ ++ ++ ++ ++ ++ Optional explicit acquire sideband independent of DRM syncobj and buffer ++ allocation. Binding does not opt the client out of ordinary wl_buffer ++ release or existing synchronization protocols. The server advertises it ++ only when it implements all semantics for the selected display path. ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ After wl_surface.attach of a non-null buffer, and before its next ++ wl_surface.commit, send the producer's final writer fence for that ++ exact attachment. This outer probe requires that fence to have already ++ successfully signaled. The fence must be a real Linux sync_file, not a ++ dma-buf, eventfd, or Vulkan OPAQUE_FD. The server consumes its received ++ descriptor once for that attachment; the caller closes its local FD ++ after marshalling. ++ ++ The receiver validates the matching pending surface and buffer and ++ validates the descriptor. A duplicate, invalid, pending or mismatched ++ request is a protocol error; it is never downgraded to implicit sync. Another attach (even the same ++ wl_buffer), detach, surface/resource loss or supersession makes the ++ old proof inapplicable. Metadata-only commits may amend receiver-side ++ still-pending state but cannot introduce or re-use a fence themselves. ++ If no request was made, the receiver keeps its established safe path. ++ If a valid fence has been accepted and later the layer is ineligible, ++ the buffer must not be copied/read before that fence is satisfied or ++ imported as the source dependency. Transport never implies readiness ++ on missed deadlines and never changes release/retirement requirements. ++ ++ ++ ++ ++ ++ ++ +diff --git a/protocol/droiddeck-game-frame-acquire-v1.xml b/protocol/droiddeck-game-frame-acquire-v1.xml +new file mode 100644 +index 0000000..d0c0f33 +--- /dev/null ++++ b/protocol/droiddeck-game-frame-acquire-v1.xml +@@ -0,0 +1,17 @@ ++ ++ ++ ++ ++ Immediately after wl_surface.attach and before the associated nonempty ++ commit, submit the successful or pending Linux sync_file representing the ++ final writer of that exact attachment. The caller closes its descriptor ++ after marshalling. This does not change wl_buffer release semantics. ++ Without a genuine positive descriptor, omit the sideband. ++ ++ ++ ++ ++ ++ ++ ++ +diff --git a/protocol/meson.build b/protocol/meson.build +index 71fe31b..1e56fbf 100644 +--- a/protocol/meson.build ++++ b/protocol/meson.build +@@ -44,6 +44,8 @@ protocols = [ + 'gamescope-action-binding.xml', + + 'droiddeck-refresh-v1.xml', ++ 'droiddeck-game-frame-acquire-v1.xml', ++ 'droiddeck-frame-acquire-v1.xml', + + # wlroots protocols + 'wlr-layer-shell-unstable-v1.xml', +diff --git a/src/Backends/WaylandBackend.cpp b/src/Backends/WaylandBackend.cpp +index 45e32f3..50b9feb 100644 +--- a/src/Backends/WaylandBackend.cpp ++++ b/src/Backends/WaylandBackend.cpp +@@ -43,6 +43,7 @@ + #include + #include + #include ++#include + #include "wlr_end.hpp" + + #include "drm_include.h" +@@ -143,6 +144,7 @@ namespace gamescope + struct WaylandPlaneState + { + wl_buffer *pBuffer; ++ std::shared_ptr pProducerFence; + int32_t nDestX; + int32_t nDestY; + double flSrcX; +@@ -755,6 +757,7 @@ namespace gamescope + virtual bool HackTemporarySetDynamicRefresh( int nRefresh ) override; + + std::span GetHostRefreshRates() const; ++ droiddeck_frame_acquire_v1 *GetHostAcquire() const { return m_pHostAcquire; } + void SyncHostRefreshTarget(); + virtual bool SupportsExplicitSync() const override; + +@@ -893,6 +896,7 @@ namespace gamescope + OwningRc m_pBlackTexture; + wp_presentation *m_pPresentation = nullptr; + droiddeck_refresh_v1 *m_pHostRefresh = nullptr; ++ droiddeck_frame_acquire_v1 *m_pHostAcquire = nullptr; + std::array m_HostRefreshRates{}; + std::atomic m_uHostRefreshRateCount = { 0 }; + std::vector m_PendingHostRefreshRates; +@@ -1613,6 +1617,8 @@ namespace gamescope + { + std::unique_lock lock( m_PlaneStateLock ); + m_oCurrentPlaneState = oState; ++ if (m_oCurrentPlaneState) ++ m_oCurrentPlaneState->pProducerFence.reset(); + } + + if ( oState ) +@@ -1760,6 +1766,12 @@ namespace gamescope + // The x/y here does nothing? Why? What is it for... + // Use the subsurface set_position thing instead. + wl_surface_attach( m_pSurface, oState->pBuffer, 0, 0 ); ++ if ( auto *manager = m_pBackend->GetHostAcquire(); manager && oState->pProducerFence ) ++ { ++ const int fd = oState->pProducerFence->SignaledFd(); ++ if (fd >= 0) ++ droiddeck_frame_acquire_v1_set_acquire_fence(manager, m_pSurface, oState->pBuffer, fd); ++ } + wl_surface_damage( m_pSurface, 0, 0, INT32_MAX, INT32_MAX ); + wl_surface_set_opaque_region( m_pSurface, oState->bOpaque ? m_pBackend->GetFullRegion() : nullptr ); + wl_surface_set_buffer_scale( m_pSurface, 1 ); +@@ -1815,6 +1827,7 @@ namespace gamescope + ClipPlane( WaylandPlaneState + { + .pBuffer = pBuffer, ++ .pProducerFence = pLayer->pProducerFence, + .nDestX = int32_t( -pLayer->offset.x ), + .nDestY = int32_t( -pLayer->offset.y ), + .flSrcX = 0.0, +@@ -2812,6 +2825,12 @@ namespace gamescope + wl_output_add_listener( pOutput , &s_OutputListener, this ); + m_pOutputs.emplace( std::make_pair( std::move( pOutput ), WaylandOutputInfo{} ) ); + } ++ else if ( droiddeck::ProducerFenceProbeEnabled() && ++ !strcmp( pInterface, droiddeck_frame_acquire_v1_interface.name ) && !m_pHostAcquire ) ++ { ++ m_pHostAcquire = static_cast( ++ wl_registry_bind( pRegistry, uName, &droiddeck_frame_acquire_v1_interface, 1u ) ); ++ } + else if ( !strcmp( pInterface, droiddeck_refresh_v1_interface.name ) ) + { + m_pHostRefresh = (droiddeck_refresh_v1 *)wl_registry_bind( pRegistry, uName, &droiddeck_refresh_v1_interface, 1u ); +diff --git a/src/WaylandServer/WaylandServerLegacy.h b/src/WaylandServer/WaylandServerLegacy.h +index 63ee2ca..0164853 100644 +--- a/src/WaylandServer/WaylandServerLegacy.h ++++ b/src/WaylandServer/WaylandServerLegacy.h +@@ -3,9 +3,11 @@ + #include + #include "WaylandDecls.h" + #include ++#include + #include + #include + #include "vulkan_include.h" ++#include "droiddeck_producer_fence.hpp" + + + #include "wlr_begin.hpp" +@@ -57,6 +59,10 @@ struct wlserver_wl_surface_info + uint64_t desired_present_time = 0; + + uint64_t last_refresh_cycle = 0; ++ const void *producerPendingAttachment = nullptr; ++ droiddeck::PendingProducerFence producerFence; ++ std::unordered_map committedProducerFences; ++ struct wl_listener producerFenceClientCommit; + }; + + wlserver_wl_surface_info *get_wl_surface_info(struct wlr_surface *wlr_surf); +diff --git a/src/commit.cpp b/src/commit.cpp +index fbfb550..cd35202 100644 +--- a/src/commit.cpp ++++ b/src/commit.cpp +@@ -55,7 +55,9 @@ void commit_t::OnPollIn() + + { + std::unique_lock lock( m_WaitableCommitStateMutex ); +- if ( !CloseFenceInternal() ) ++ const bool bFailedProducerFence = m_bProducerFence && ++ droiddeck::ProducerFenceStatus(m_nCommitFence) != 1; ++ if ( !CloseFenceInternal() || bFailedProducerFence ) + return; + } + +@@ -115,12 +117,13 @@ bool commit_t::CloseFenceInternal() + return true; + } + +-void commit_t::SetFence( int nFence, bool bMangoNudge, uint32_t uMangoMsgType, const std::shared_ptr &pDoneCommits, bool bDroidDeckHudFrame ) ++void commit_t::SetFence( int nFence, bool bMangoNudge, uint32_t uMangoMsgType, const std::shared_ptr &pDoneCommits, bool bDroidDeckHudFrame, bool bProducerFence ) + { + std::unique_lock lock( m_WaitableCommitStateMutex ); + CloseFenceInternal(); + + m_nCommitFence = nFence; ++ m_bProducerFence = bProducerFence; + m_bMangoNudge = bMangoNudge; + m_uMangoMsgType = uMangoMsgType; + m_bDroidDeckHudFrame = bDroidDeckHudFrame; +diff --git a/src/commit.h b/src/commit.h +index ae767b6..99b32ae 100644 +--- a/src/commit.h ++++ b/src/commit.h +@@ -35,13 +35,14 @@ struct commit_t final : public gamescope::RcObject, public gamescope::IWaitable, + + // Returns true if we had a fence that was closed. + bool CloseFenceInternal(); +- void SetFence( int nFence, bool bMangoNudge, uint32_t uMangoMsgType, const std::shared_ptr &pDoneCommits, bool bDroidDeckHudFrame = false ); ++ void SetFence( int nFence, bool bMangoNudge, uint32_t uMangoMsgType, const std::shared_ptr &pDoneCommits, bool bDroidDeckHudFrame = false, bool bProducerFence = false ); + + bool ShouldPreemptivelyUpscale( GamescopeUpscaleFilter eFilter, GamescopeUpscaleScaler eScaler ); + + struct wlr_buffer *buf = nullptr; + gamescope::Rc vulkanTex; + std::optional upscaledTexture; ++ std::shared_ptr pProducerFence; + + gamescope::Rc GetTexture( GamescopeUpscaleFilter eFilter, GamescopeUpscaleScaler eScaler, GamescopeAppTextureColorspace &colorspace ) + { +@@ -79,6 +80,7 @@ struct commit_t final : public gamescope::RcObject, public gamescope::IWaitable, + + std::mutex m_WaitableCommitStateMutex; + int m_nCommitFence = -1; ++ bool m_bProducerFence = false; + bool m_bMangoNudge = false; + // Typed mangoapp stream to nudge, 0 for none, never the legacy type. + uint32_t m_uMangoMsgType = 0; +diff --git a/src/droiddeck_producer_fence.hpp b/src/droiddeck_producer_fence.hpp +new file mode 100644 +index 0000000..2137e39 +--- /dev/null ++++ b/src/droiddeck_producer_fence.hpp +@@ -0,0 +1,67 @@ ++#pragma once ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++namespace droiddeck ++{ ++ inline bool ProducerFenceProbeEnabled() ++ { ++ static const bool enabled = [] { ++ const char *value = std::getenv("GAMESCOPE_DROIDDECK_PRODUCER_FENCE_PROBE"); ++ return value && std::strcmp(value, "1") == 0; ++ }(); ++ return enabled; ++ } ++ ++ // Status 1 is success, 0 is pending, everything else is unsafe. ++ inline int ProducerFenceStatus(int fd) ++ { ++ sync_file_info info{}; ++ if (fd < 0 || ioctl(fd, SYNC_IOC_FILE_INFO, &info) < 0 || !info.num_fences) ++ return -1; ++ return info.status; ++ } ++ ++ class ProducerFence ++ { ++ public: ++ explicit ProducerFence(int fd) : m_fd(fd) {} ++ ProducerFence(ProducerFence &&other) noexcept : m_fd(other.TakeFd()) {} ++ ProducerFence(const ProducerFence &) = delete; ++ ProducerFence &operator=(const ProducerFence &) = delete; ++ ~ProducerFence() { if (m_fd >= 0) close(m_fd); } ++ ++ int Status() const { return ProducerFenceStatus(m_fd); } ++ int Dup() const { return m_fd < 0 ? -1 : fcntl(m_fd, F_DUPFD_CLOEXEC, 0); } ++ int SignaledFd() const { return Status() == 1 ? m_fd : -1; } ++ int Fd() const { return m_fd; } ++ int TakeFd() { return std::exchange(m_fd, -1); } ++ ++ private: ++ int m_fd; ++ }; ++ ++ // This slot belongs to one nested surface and is cleared by *every* ++ // attach request, even if the client reuses the same wl_buffer object. ++ struct PendingProducerFence ++ { ++ std::shared_ptr fence; ++ uint32_t sequence = 0; ++ const void *buffer = nullptr; ++ ++ std::shared_ptr Consume(bool attached, uint32_t seq, const void *current) ++ { ++ auto owned = std::exchange(fence, {}); ++ const bool same = attached && seq == sequence && current && current == buffer; ++ return same ? owned : nullptr; ++ } ++ }; ++} +diff --git a/src/rendervulkan.hpp b/src/rendervulkan.hpp +index 8ae3b71..235cfc0 100644 +--- a/src/rendervulkan.hpp ++++ b/src/rendervulkan.hpp +@@ -281,6 +281,8 @@ enum AlphaBlendingMode_t + //#define DRM_MODE_BLEND_COVERAGE 1 + //#define DRM_MODE_BLEND_PIXEL_NONE 2 + ++namespace droiddeck { class ProducerFence; } ++ + struct FrameInfo_t + { + bool useFSRLayer0; +@@ -309,6 +311,7 @@ struct FrameInfo_t + struct Layer_t + { + gamescope::Rc tex; ++ std::shared_ptr pProducerFence; + int zpos; + + vec2_t offset; +@@ -378,6 +381,7 @@ struct FrameInfo_t + if ( m_nCount >= k_nMaxLayers ) + return nullptr; + ++ m_Layers[m_nCount].pProducerFence.reset(); + return &m_Layers[ m_nCount++ ]; + } + +diff --git a/src/steamcompmgr.cpp b/src/steamcompmgr.cpp +index c2e04ab..9f7d162 100644 +--- a/src/steamcompmgr.cpp ++++ b/src/steamcompmgr.cpp +@@ -2559,6 +2559,7 @@ paint_cached_base_layer(const gamescope::Rc& commit, const BaseLayerIn + + layer->tex = commit->GetTexture( base.eUpscaleFilter, base.eUpscaleScaler, layer->colorspace ); + layer->filter = base.filter; ++ layer->pProducerFence = layer->tex == commit->vulkanTex ? commit->pProducerFence : nullptr; + if ( layer->tex == commit->vulkanTex ) + { + if ( base.uOutputWidth == currentOutputWidth && base.uOutputHeight == currentOutputHeight ) +@@ -2670,6 +2671,7 @@ paint_window_commit( const gamescope::Rc &lastCommit, steamcompmgr_win + layer->filter = ( flags & PaintWindowFlag::NoFilter ) ? GamescopeUpscaleFilter::LINEAR : frameInfo->eUpscaleFilter; + + layer->tex = lastCommit->GetTexture( layer->filter, frameInfo->eUpscaleScaler, layer->colorspace ); ++ layer->pProducerFence = layer->tex == lastCommit->vulkanTex ? lastCommit->pProducerFence : nullptr; + + if ( flags & PaintWindowFlag::NoScale ) + { +@@ -8915,7 +8917,7 @@ void update_wayland_res(const std::shared_ptr &doneCommits, st + already_exists = true; + } + +- if ( already_exists && !reslistentry.feedback && reslistentry.presentation_feedbacks.empty() ) ++ if ( already_exists && !reslistentry.pProducerFence && !reslistentry.feedback && reslistentry.presentation_feedbacks.empty() ) + { + wlserver_lock(); + wlr_buffer_unlock( buf ); +@@ -8957,6 +8959,8 @@ void update_wayland_res(const std::shared_ptr &doneCommits, st + droiddeck::frame_trace::Event::ImportSucceededOther, + bTraceCurrent ); + ++ newCommit->pProducerFence = std::move(reslistentry.pProducerFence); ++ const bool bProducerFence = bool(newCommit->pProducerFence); + int fence = -1; + global_focus_t *pCurrentFocus = GetCurrentFocus(); + +@@ -9104,7 +9108,24 @@ void update_wayland_res(const std::shared_ptr &doneCommits, st + pBackendFb->SetBuffer( buf ); + } + +- if ( eventFd != gamescope::CAcquireTimelinePoint::k_InvalidEvent ) ++ if ( bProducerFence ) ++ { ++ const int status = newCommit->pProducerFence->Status(); ++ if ( status != 0 && status != 1 ) ++ return; // A failed producer write may not be marked ready. ++ bKnownReady = status == 1; ++ if ( !bKnownReady ) ++ { ++ fence = newCommit->pProducerFence->Dup(); ++ if ( fence < 0 ) ++ { ++ // Still wait on original; this commit cannot forward proof. ++ fence = newCommit->pProducerFence->TakeFd(); ++ newCommit->pProducerFence.reset(); ++ } ++ } ++ } ++ else if ( eventFd != gamescope::CAcquireTimelinePoint::k_InvalidEvent ) + { + fence = eventFd.first; + bKnownReady = eventFd.second; +@@ -9127,13 +9148,13 @@ void update_wayland_res(const std::shared_ptr &doneCommits, st + + if ( cv_commit_ready_check ) + { +- if ( !bKnownReady && fence >= 0 && commit_fence_ready_now( fence ) ) ++ if ( !bProducerFence && !bKnownReady && fence >= 0 && commit_fence_ready_now( fence ) ) + bKnownReady = true; + note_commit_fence( bKnownReady ); + } + gpuvis_trace_printf( "pushing wait for commit %lu win %lx", newCommit->commitID, w->type == steamcompmgr_win_type_t::XWAYLAND ? w->xwayland().id : 0 ); + { +- newCommit->SetFence( fence, mango_nudge, uMangoMsgType, doneCommits, bDroidDeckHudFrame && droiddeck_hud_frames_wanted() ); ++ newCommit->SetFence( fence, mango_nudge, uMangoMsgType, doneCommits, bDroidDeckHudFrame && droiddeck_hud_frames_wanted(), bProducerFence ); + if ( bKnownReady ) + { + newCommit->Signal(); +diff --git a/src/wlserver.cpp b/src/wlserver.cpp +index 84602c0..7c84979 100644 +--- a/src/wlserver.cpp ++++ b/src/wlserver.cpp +@@ -57,6 +57,7 @@ + #include "gamescope-private-protocol.h" + #include "gamescope-swapchain-protocol.h" + #include "presentation-time-protocol.h" ++#include "droiddeck-game-frame-acquire-v1-protocol.h" + + #include "wlserver.hpp" + #include "hdmi.h" +@@ -136,7 +137,8 @@ std::vector& gamescope_xwayland_server_t::retrieve_commits() + + gamescope::ConVar cv_drm_debug_syncobj_force_wait_on_commit( "drm_debug_syncobj_force_wait_on_commit", false, "Force a wait on DRM sync objects before committing buffers" ); + +-ResListEntry_t PrepareCommit( struct wlr_surface *surf, struct wlr_buffer *buf ) ++ResListEntry_t PrepareCommit( struct wlr_surface *surf, struct wlr_buffer *buf, ++ std::shared_ptr pProducerFence ) + { + auto wl_surf = get_wl_surface_info( surf ); + +@@ -168,7 +170,8 @@ ResListEntry_t PrepareCommit( struct wlr_surface *surf, struct wlr_buffer *buf ) + wl_surf->present_id, + wl_surf->desired_present_time, + std::move( pAcquirePoint ), +- std::move( pReleasePoint ) ++ std::move( pReleasePoint ), ++ bExplicitSync ? nullptr : std::move( pProducerFence ) + }; + wl_surf->present_id = std::nullopt; + wl_surf->desired_present_time = 0; +@@ -216,6 +219,12 @@ void xwayland_surface_commit(struct wlr_surface *wlr_surface) { + ? droiddeck::frame_trace::Event::SurfaceCommitBuffer + : droiddeck::frame_trace::Event::SurfaceCommitNoBuffer ); + ++ // Capture state before the existing damage/attach flags are reset. ++ const bool bAttached = wlr_surface->current.committed & WLR_SURFACE_STATE_BUFFER; ++ auto &proofs = get_wl_surface_info(wlr_surface)->committedProducerFences; ++ auto proof = proofs.extract(wlr_surface->current.seq); ++ auto pProducerFence = proof.empty() ? nullptr : proof.mapped().Consume( ++ bAttached, wlr_surface->current.seq, wlr_surface->current.buffer ); + wlr_surface->current.committed = 0; + + wlserver_x11_surface_info *wlserver_x11_surface_info = get_wl_surface_info(wlr_surface)->x11_surface; +@@ -249,7 +258,14 @@ void xwayland_surface_commit(struct wlr_surface *wlr_surface) { + + gpuvis_trace_printf( "xwayland_surface_commit wlr_surface %p", wlr_surface ); + +- ResListEntry_t entry = PrepareCommit( wlr_surface, buf ); ++ // A texture may still designate older storage after a rejected/no-op import. ++ if ( pProducerFence ) ++ { ++ struct wlr_dmabuf_attributes dmabuf{}; ++ if ( wlr_surface->current.buffer != buf || !wlr_buffer_get_dmabuf(buf, &dmabuf) ) ++ pProducerFence.reset(); ++ } ++ ResListEntry_t entry = PrepareCommit( wlr_surface, buf, std::move(pProducerFence) ); + + if (wlserver_x11_surface_info) + { +@@ -612,6 +628,29 @@ wlserver_wl_surface_info *get_wl_surface_info(struct wlr_surface *wlr_surf) + return reinterpret_cast(wlr_surf->data); + } + ++// This wlroots signal runs with its own pending.buffer lock, after the ++// pending object has been resolved and before the commit is applied or cached. ++static void handle_producer_fence_client_commit(wl_listener *listener, void *) ++{ ++ wlserver_wl_surface_info *info = wl_container_of(listener, info, producerFenceClientCommit); ++ wlr_surface *surface = info->wlr; ++ info->producerPendingAttachment = nullptr; ++ auto proof = std::exchange(info->producerFence, {}); ++ if (!proof.fence || !surface->pending.buffer || proof.sequence != surface->pending.seq || ++ !(surface->pending.committed & WLR_SURFACE_STATE_BUFFER)) ++ return; ++ struct wlr_dmabuf_attributes dmabuf{}; ++ if (!wlr_buffer_get_dmabuf(surface->pending.buffer, &dmabuf) || ++ info->committedProducerFences.size() >= 64 || ++ info->committedProducerFences.contains(proof.sequence)) ++ { ++ wlr_surface_reject_pending(surface, surface->resource, 0, "invalid or excessive producer fence commits"); ++ return; ++ } ++ proof.buffer = surface->pending.buffer; // Borrowed from the state lock, no extra release path. ++ info->committedProducerFences.emplace(proof.sequence, std::move(proof)); ++} ++ + static void handle_wl_surface_commit( struct wl_listener *l, void *data ) + { + wlserver_wl_surface_info *surf = wl_container_of( l, surf, commit ); +@@ -707,6 +746,8 @@ static void handle_wl_surface_destroy( struct wl_listener *l, void *data ) + } + + wl_list_remove( &surf->commit.link ); ++ if (droiddeck::ProducerFenceProbeEnabled()) ++ wl_list_remove(&surf->producerFenceClientCommit.link); + wl_list_remove( &surf->destroy.link ); + + delete surf; +@@ -725,6 +766,11 @@ static void wlserver_new_surface(struct wl_listener *l, void *data) + + wl_surface_info->commit.notify = handle_wl_surface_commit; + wl_signal_add( &wlr_surf->events.commit, &wl_surface_info->commit ); ++ if (droiddeck::ProducerFenceProbeEnabled()) ++ { ++ wl_surface_info->producerFenceClientCommit.notify = handle_producer_fence_client_commit; ++ wl_signal_add(&wlr_surf->events.client_commit, &wl_surface_info->producerFenceClientCommit); ++ } + + wlr_surf->data = wl_surface_info; + +@@ -1811,6 +1857,71 @@ static bool filter_global(const struct wl_client *client, const struct wl_global + return server && server->get_output() == output; + } + ++static wl_protocol_logger *s_pProducerFenceLogger; ++ ++static void producer_fence_protocol_log( void *, wl_protocol_logger_type direction, ++ const wl_protocol_logger_message *message ) ++{ ++ if ( direction != WL_PROTOCOL_LOGGER_REQUEST || std::strcmp(message->message->name, "attach") || ++ std::strcmp(wl_resource_get_class(message->resource), wl_surface_interface.name) || ++ message->arguments_count != 3 ) ++ return; ++ auto *surf = wlr_surface_from_resource(message->resource); ++ if ( auto *info = get_wl_surface_info(surf) ) ++ { ++ info->producerFence = {}; ++ // Wayland's server demarshaller gives the wl_object at the start ++ // of the wl_resource. Compare identity only, never dereference it. ++ info->producerPendingAttachment = message->arguments[0].o; ++ } ++} ++ ++static void set_producer_fence( wl_client *client, wl_resource *manager, wl_resource *surface, ++ wl_resource *buffer, int32_t fd ) ++{ ++ droiddeck::ProducerFence fence(fd); ++ wlr_surface *surf = surface && wl_resource_get_client(surface) == client ++ ? wlr_surface_from_resource(surface) : nullptr; ++ auto *info = get_wl_surface_info(surf); ++ if (!info || !buffer || wl_resource_get_client(buffer) != client || ++ !(surf->pending.committed & WLR_SURFACE_STATE_BUFFER) || ++ info->producerPendingAttachment != buffer || info->producerFence.fence) ++ { ++ wl_resource_post_error(manager, 0, "invalid or duplicate producer fence attachment"); ++ return; ++ } ++ ++ const int status = fence.Status(); ++ if (status != 0 && status != 1) ++ { ++ wl_resource_post_error(manager, 0, "producer fence requires a real sync_file"); ++ return; ++ } ++ info->producerFence = { std::make_shared(std::move(fence)), ++ surf->pending.seq }; ++} ++ ++static const struct droiddeck_game_frame_acquire_v1_interface producer_fence_impl = { ++ .set_acquire_fence = set_producer_fence, ++}; ++ ++static void bind_producer_fence(wl_client *client, void *, uint32_t, uint32_t id) ++{ ++ auto *resource = wl_resource_create(client, &droiddeck_game_frame_acquire_v1_interface, 1, id); ++ if (!resource) ++ { ++ wl_client_post_no_memory(client); ++ return; ++ } ++ wl_resource_set_implementation(resource, &producer_fence_impl, nullptr, nullptr); ++} ++ ++static void stop_producer_fence_protocol() ++{ ++ if (s_pProducerFenceLogger) ++ wl_protocol_logger_destroy(std::exchange(s_pProducerFenceLogger, nullptr)); ++} ++ + bool wlsession_init( void ) { + static bool s_bInitted = false; + if ( s_bInitted ) +@@ -1823,6 +1934,12 @@ bool wlsession_init( void ) { + wlserver.wlr.headless_backend = wlr_headless_backend_create( wlserver.event_loop ); + + wl_display_set_global_filter(wlserver.display, filter_global, nullptr); ++ if (droiddeck::ProducerFenceProbeEnabled()) ++ { ++ s_pProducerFenceLogger = wl_display_add_protocol_logger(wlserver.display, producer_fence_protocol_log, nullptr); ++ if (s_pProducerFenceLogger) ++ wl_global_create(wlserver.display, &droiddeck_game_frame_acquire_v1_interface, 1, nullptr, bind_producer_fence); ++ } + + const struct wlserver_output_info output_info = { + .description = "Virtual gamescope output", +@@ -2345,6 +2462,7 @@ bool wlserver_init( void ) { + { + wl_log.errorf("Failed to start backend"); + wlr_backend_destroy( wlserver.wlr.multi_backend ); ++ stop_producer_fence_protocol(); + wl_display_destroy(wlserver.display); + return false; + } +@@ -2527,6 +2645,7 @@ void wlserver_run(void) + wl_list_remove( &wlserver.session_active.link ); + #endif + ++ stop_producer_fence_protocol(); + wl_display_destroy_clients(wlserver.display); + wl_display_destroy(wlserver.display); + wlserver.display = NULL; +diff --git a/src/wlserver.hpp b/src/wlserver.hpp +index 63542ef..12141b6 100644 +--- a/src/wlserver.hpp ++++ b/src/wlserver.hpp +@@ -50,6 +50,7 @@ struct ResListEntry_t { + uint64_t desired_present_time; + std::shared_ptr pAcquirePoint; + std::shared_ptr pReleasePoint; ++ std::shared_ptr pProducerFence; + }; + + struct wlserver_content_override;