Compare commits

..
5 changed files with 298 additions and 2 deletions

No files matched your search

@@ -133,10 +133,10 @@ static void handle_released(uint64_t id, int fd) {
if (fd >= 0) {
struct droiddeck_dma_buf_sync_file imp = {.flags = DROIDDECK_DMA_BUF_SYNC_READ, .fd = fd};
int dmabuf_fd = droiddeck_dmabuf_fd(ab->b);
if (!ab->display && dmabuf_fd >= 0 && ioctl(dmabuf_fd, DROIDDECK_DMA_BUF_IOCTL_IMPORT_SYNC_FILE, &imp) == 0) {
if (dmabuf_fd >= 0 && ioctl(dmabuf_fd, DROIDDECK_DMA_BUF_IOCTL_IMPORT_SYNC_FILE, &imp) == 0) {
close(fd);
} else {
if (!ab->display && !g_import_failed_logged) {
if (!g_import_failed_logged) {
g_import_failed_logged = 1;
droiddeck_log("layer", "zero-copy: DMA_BUF_IOCTL_IMPORT_SYNC_FILE failed (%s): releases wait for the display's fence here instead",
strerror(errno));
+10
View File
@@ -474,3 +474,13 @@ Short strokes never released it, and taps kept landing at the relative pointer i
counts its touch-down warps, and a frame that contains one is left out of the hold decision. Touch drags, mouse motion
and mouse warps still count, so a drag on a game that follows the pointer releases the hold and a recentring game is
still held. `tools/tests/test_gamescope_pointer_hold_touch_down.py` compiles the rule with the 0133 rule.
## 0146 rendervulkan: composite only into output images the display has finished scanning out
The host compositor now releases a zero-copy display buffer as soon as the display latches the next one, and attaches
the display's release fence to the buffer's dma-buf instead of waiting for it on the CPU. That wait had held every
release back by about one refresh, so a chain of three buffers ran at two thirds of the refresh rate. KGSL Turnip does
not wait on fences attached to a dma-buf, so the composite now checks each output image's dma-buf with `poll(POLLOUT)`.
It picks the next image that is neither held by the host nor still being scanned out. Only when every free image is
still on the display does it wait for one, up to 100 ms. On kernels that cannot attach the fence, the host keeps
waiting itself and nothing changes. `tools/gamescope/tests/nested-swapchain.cpp` covers the choice and the busy check.
@@ -0,0 +1,106 @@
diff --git a/src/output_image_pick.hpp b/src/output_image_pick.hpp
new file mode 100644
index 0000000..193d9de
--- /dev/null
+++ b/src/output_image_pick.hpp
@@ -0,0 +1,50 @@
+#pragma once
+#include <cstdint>
+#include <poll.h>
+
+namespace gamescope
+{
+struct OutputImagePick
+{
+ uint32_t image;
+ bool scanoutPending;
+};
+
+template <typename Held, typename Busy>
+inline OutputImagePick PickOutputImage( uint32_t count, uint32_t first, uint32_t last, uint32_t deferred,
+ Held &&held, Busy &&busy )
+{
+ for ( int pass = 0; pass < 2; pass++ )
+ {
+ for ( uint32_t i = 0; i < count; i++ )
+ {
+ const uint32_t image = ( first + i ) % count;
+ if ( count > 3 && ( image == last || image == deferred ) )
+ continue;
+ if ( held( image ) )
+ continue;
+ const bool pending = busy( image );
+ if ( pass == 0 && pending )
+ continue;
+ return { image, pending };
+ }
+ }
+ return { first, false };
+}
+
+inline bool DmabufScanoutPending( int fd )
+{
+ if ( fd < 0 )
+ return false;
+ pollfd p = { .fd = fd, .events = POLLOUT, .revents = 0 };
+ return poll( &p, 1, 0 ) == 0;
+}
+
+inline void WaitDmabufScanout( int fd, int timeoutMs )
+{
+ if ( fd < 0 )
+ return;
+ pollfd p = { .fd = fd, .events = POLLOUT, .revents = 0 };
+ poll( &p, 1, timeoutMs );
+}
+}
diff --git a/src/rendervulkan.cpp b/src/rendervulkan.cpp
index 1d3a80c..3668a10 100644
--- a/src/rendervulkan.cpp
+++ b/src/rendervulkan.cpp
@@ -37,6 +37,7 @@
#include "steamcompmgr.hpp"
#include "log.hpp"
#include "Utils/Process.h"
+#include "output_image_pick.hpp"
#include "cs_composite_blit.h"
#include "cs_composite_blur.h"
@@ -4297,18 +4298,29 @@ static bool vulkan_output_image_held( uint32_t nImage )
return false;
}
+static bool vulkan_output_image_scanout_pending( uint32_t nImage )
+{
+ for ( const auto &pImage : { g_output.outputImages[ nImage ], g_output.outputImagesPartialOverlay[ nImage ] } )
+ {
+ if ( pImage != nullptr && pImage->GetBackendFb() != nullptr && gamescope::DmabufScanoutPending( pImage->dmabuf().fd[0] ) )
+ return true;
+ }
+ return false;
+}
+
static uint32_t vulkan_free_output_image( uint32_t nFirst )
{
- const uint32_t uCount = g_output.outputImages.size();
- for ( uint32_t i = 0; i < uCount; i++ )
+ const gamescope::OutputImagePick pick = gamescope::PickOutputImage( g_output.outputImages.size(), nFirst,
+ g_output.nLastOutImage, g_output.nDeferredOutImage, vulkan_output_image_held, vulkan_output_image_scanout_pending );
+ if ( pick.scanoutPending )
{
- const uint32_t nImage = ( nFirst + i ) % uCount;
- if ( uCount > 3 && ( nImage == g_output.nLastOutImage || nImage == g_output.nDeferredOutImage ) )
- continue;
- if ( !vulkan_output_image_held( nImage ) )
- return nImage;
+ for ( const auto &pImage : { g_output.outputImages[ pick.image ], g_output.outputImagesPartialOverlay[ pick.image ] } )
+ {
+ if ( pImage != nullptr && pImage->GetBackendFb() != nullptr )
+ gamescope::WaitDmabufScanout( pImage->dmabuf().fd[0], 100 );
+ }
}
- return nFirst;
+ return pick.image;
}
std::optional<uint64_t> vulkan_composite( struct FrameInfo_t *frameInfo, gamescope::Rc<CVulkanTexture> pPipewireTexture, bool partial, gamescope::Rc<CVulkanTexture> pOutputOverride, bool increment, std::unique_ptr<CVulkanCmdBuffer> pInCommandBuffer )
@@ -2,6 +2,9 @@
#include "nested_surface_format.hpp"
#include "dmabuf_import.hpp"
#include "sdl_refresh.hpp"
#include "output_image_pick.hpp"
#include <fcntl.h>
#include <unistd.h>
#include <cassert>
#include <cstdio>
@@ -152,5 +155,41 @@ int main()
assert(DmabufMemoryType(memory, 7, 0, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) == UINT32_MAX);
assert(DmabufMemoryType(memory, 7, 4, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) == UINT32_MAX);
using gamescope::PickOutputImage;
auto none = [](uint32_t) { return false; };
auto pick = PickOutputImage(5, 2, 1, 0, none, none);
assert(pick.image == 2 && !pick.scanoutPending);
auto heldTwo = [](uint32_t i) { return i == 2; };
pick = PickOutputImage(5, 2, 1, 0, heldTwo, none);
assert(pick.image == 3 && !pick.scanoutPending);
auto scanningTwo = [](uint32_t i) { return i == 2; };
pick = PickOutputImage(5, 2, 1, 0, none, scanningTwo);
assert(pick.image == 3 && !pick.scanoutPending);
auto scanningAll = [](uint32_t) { return true; };
pick = PickOutputImage(5, 2, 1, 0, heldTwo, scanningAll);
assert(pick.image == 3 && pick.scanoutPending);
auto heldAll = [](uint32_t) { return true; };
pick = PickOutputImage(5, 2, 1, 0, heldAll, none);
assert(pick.image == 2 && !pick.scanoutPending);
pick = PickOutputImage(3, 1, 0, 2, none, none);
assert(pick.image == 1 && !pick.scanoutPending);
auto scanningOne = [](uint32_t i) { return i == 1; };
pick = PickOutputImage(3, 1, 0, 2, none, scanningOne);
assert(pick.image == 2 && !pick.scanoutPending);
int fds[2];
assert(pipe(fds) == 0);
assert(!gamescope::DmabufScanoutPending(fds[1]));
assert(!gamescope::DmabufScanoutPending(-1));
fcntl(fds[1], F_SETFL, O_NONBLOCK);
char block[4096] = {};
while (write(fds[1], block, sizeof(block)) > 0) {}
assert(gamescope::DmabufScanoutPending(fds[1]));
gamescope::WaitDmabufScanout(fds[1], 1);
while (read(fds[0], block, sizeof(block)) == sizeof(block) && gamescope::DmabufScanoutPending(fds[1])) {}
assert(!gamescope::DmabufScanoutPending(fds[1]));
close(fds[0]);
close(fds[1]);
std::puts("Gamescope presentation and DMA-BUF import tests passed");
}
@@ -0,0 +1,141 @@
import os
from pathlib import Path
import re
import shutil
import subprocess
import tempfile
import unittest
from test_vk_pending_completion import function
ROOT = Path(__file__).resolve().parents[2]
SRC = ROOT / 'app/src/main/cpp/waylandcomp/src'
HARNESS = r'''
#define _GNU_SOURCE
#include <assert.h>
#include <errno.h>
#include <poll.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wayland-util.h>
struct wl_listener { struct wl_list link; void (*notify)(struct wl_listener *, void *); };
struct wl_event_source { int unused; };
struct wl_event_loop;
enum { WL_EVENT_READABLE = 1 };
struct wl_client;
struct wl_resource { int unused; };
struct surface;
struct dmabuf_buffer { int fd; };
typedef struct AHardwareBuffer { int unused; } AHardwareBuffer;
DEFINES
STRUCT
static struct wl_list g_bufs;
static struct wl_event_loop *g_loop;
static int g_import_failed_logged;
static int import_ok, imports, imported_flags, deferred_releases, frees, sources;
static struct wl_event_source g_source;
void wl_list_init(struct wl_list *l) { l->prev = l; l->next = l; }
void wl_list_insert(struct wl_list *l, struct wl_list *e) {
e->next = l->next; e->prev = l; l->next->prev = e; l->next = e;
}
static int fake_ioctl(int fd, unsigned long req, void *arg) {
struct droiddeck_dma_buf_sync_file *f = arg;
(void)fd;
assert(req == DROIDDECK_DMA_BUF_IOCTL_IMPORT_SYNC_FILE);
++imports; imported_flags = (int)f->flags;
if (import_ok) return 0;
errno = ENOTTY; return -1;
}
#define ioctl fake_ioctl
static int droiddeck_dmabuf_fd(struct dmabuf_buffer *b) { return b->fd; }
static void droiddeck_log(const char *tag, const char *fmt, ...) { (void)tag; (void)fmt; }
static struct wl_event_source *wl_event_loop_add_fd(struct wl_event_loop *l, int fd, uint32_t m,
int (*cb)(int, uint32_t, void *), void *d) {
(void)l; (void)fd; (void)m; (void)cb; (void)d; ++sources; return &g_source;
}
static int on_fence_readable(int fd, uint32_t mask, void *data) { (void)fd; (void)mask; (void)data; return 0; }
static void send_deferred_release(struct ahb_buf *ab) { ++deferred_releases; ab->release_pending = 0; }
static void maybe_free(struct ahb_buf *ab) { (void)ab; ++frees; }
FUNCTIONS
static void reset(void) { import_ok = imports = imported_flags = deferred_releases = frees = sources = 0; }
static void run(int display, int can_import, int fence_signalled) {
reset();
wl_list_init(&g_bufs);
struct dmabuf_buffer b = {.fd = 7};
struct ahb_buf ab;
memset(&ab, 0, sizeof(ab));
ab.id = 42; ab.b = &b; ab.display = display; ab.on_layer = 1; ab.release_pending = 1; ab.fence_fd = -1;
wl_list_insert(&g_bufs, &ab.link);
int fds[2]; assert(pipe(fds) == 0);
if (fence_signalled) assert(write(fds[1], "x", 1) == 1);
import_ok = can_import;
handle_released(42, fds[0]);
assert(!ab.on_layer && imports == 1 && imported_flags == DROIDDECK_DMA_BUF_SYNC_READ);
if (can_import || fence_signalled) {
assert(deferred_releases == 1 && frees == 1 && !ab.fence_src && ab.fence_fd == -1);
errno = 0; assert(fcntl(fds[0], F_GETFD) == -1 && errno == EBADF);
} else {
assert(deferred_releases == 0 && sources == 1 && ab.fence_src == &g_source && ab.fence_fd == fds[0]);
close(fds[0]);
}
close(fds[1]);
}
int main(int argc, char **argv) {
assert(argc == 4);
run(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]));
return 0;
}
'''
class ZeroCopyReleaseAtLatchTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cc = shutil.which(os.environ.get('CC', 'cc'))
if not cc:
raise AssertionError('C compiler required')
source_dir = Path(os.environ.get('DROIDDECK_DMABUF_TEST_SOURCE', SRC))
ahb = (source_dir / 'ahb_swapchain.c').read_text()
defines = '\n'.join(re.findall(r'^(?:struct droiddeck_dma_buf_sync_file \{.*?\};|#define DROIDDECK_DMA_BUF_\w+ .*)$',
ahb, re.M))
struct = re.search(r'struct ahb_buf \{.*?\n\};', ahb, re.S).group()
functions = '\n'.join(function(ahb, n) for n in ('find_buf', 'handle_released'))
source = HARNESS.replace('DEFINES', '#include <fcntl.h>\n#include <sys/ioctl.h>\n' + defines)
source = source.replace('STRUCT', struct).replace('FUNCTIONS', functions)
cls.tmp = tempfile.TemporaryDirectory(prefix='droiddeck-release-latch-')
cls.addClassCleanup(cls.tmp.cleanup)
probe = Path(cls.tmp.name) / 'probe.c'
probe.write_text(source)
cls.binary = probe.with_suffix('')
built = subprocess.run([cc, '-std=c11', '-Wall', '-Wextra', '-Werror', '-Wno-unused-function', '-UNDEBUG',
'-I', str(SRC.parent / 'prebuilt/include'), str(probe), '-o', str(cls.binary)],
capture_output=True, text=True)
if built.returncode:
raise AssertionError(built.stdout + built.stderr)
def scenario(self, display, can_import, fence_signalled):
result = subprocess.run([str(self.binary), str(display), str(can_import), str(fence_signalled)],
capture_output=True, text=True, timeout=10)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
def test_display_buffer_released_at_latch_when_fence_attaches(self):
self.scenario(1, 1, 0)
def test_swapchain_buffer_released_at_latch_when_fence_attaches(self):
self.scenario(0, 1, 0)
def test_display_buffer_waits_for_fence_when_kernel_cannot_attach(self):
self.scenario(1, 0, 0)
def test_swapchain_buffer_waits_for_fence_when_kernel_cannot_attach(self):
self.scenario(0, 0, 0)
def test_signalled_fence_releases_without_waiting(self):
self.scenario(1, 0, 1)
if __name__ == '__main__':
unittest.main()