from pathlib import Path import os import shutil import subprocess import tempfile import unittest ROOT = Path(__file__).resolve().parents[2] PATCHES = ROOT / "tools/gamescope/patches" PICK = PATCHES / "0146-composite-waits-for-scanout.patch" FORWARD = PATCHES / "0147-forwarded-buffers-wait-for-scanout.patch" def post_image(patch, name): out, current = [], None for line in patch.read_text().splitlines(): if line.startswith("+++ "): current = line[6:] if line.startswith("+++ b/") else line[4:] continue if current != name or line.startswith("--- ") or line.startswith("@@"): continue if line.startswith("+") or line.startswith(" "): out.append(line[1:]) elif line == "": out.append("") return "\n".join(out) def block(text, start, end="\n }\n"): begin = text.index(start) return text[begin:text.index(end, begin) + len(end)] PREAMBLE = r''' #include #include #include #include #include #include #include #include #include #include #include struct wl_buffer {}; static struct { void debugf(const char *, ...) {} void errorf(const char *, ...) { ++oh_no; } int oh_no = 0; } xdg_log; namespace droiddeck::frame_trace { enum class Event { OutputRelease }; inline void Record(Event) {} } namespace gamescope { PICK class IWaitable { public: virtual ~IWaitable() {} virtual int GetFD() { return -1; } virtual void OnPollOut() {} virtual void OnPollHangUp() {} }; template class CAsyncWaiter { public: CAsyncWaiter( const char * ) {} bool AddWaitable( IWaitable *p, uint32_t ) { waiting.push_back( p ); return true; } void RemoveWaitable( IWaitable *p ) { std::erase( waiting, p ); } std::vector waiting; }; class CWaylandBackend; class CBaseBackendFb { public: uint32_t IncRef() { return refs++; } uint32_t DecRef() { return --refs; } bool ShowsClientBuffer() const { return client; } uint32_t refs = 1; bool client = false; }; class CWaylandFb final : public CBaseBackendFb { public: CWaylandFb( int nDmabufFd ) : m_pHostBuffer{ &m_Buffer }, m_nDmabufFd{ nDmabufFd } {} void OnCompositorAcquire(); void OnCompositorRelease(); void OnScanoutDone( uint64_t ulAcquireSerial ); void Wayland_Buffer_Release( wl_buffer *pBuffer ); wl_buffer m_Buffer; wl_buffer *m_pHostBuffer; int m_nDmabufFd = -1; std::mutex m_AcquireMutex; uint64_t m_ulAcquireSerial = 0; bool m_bCompositorAcquired = false; }; SCANOUT METHODS } ''' MAIN = r''' using namespace gamescope; static CAsyncWaiter<> &waiter() { return CWaylandScanoutRelease::Waiter(); } struct Display { int fds[2]; Display() { assert( pipe2( fds, O_NONBLOCK ) == 0 ); } ~Display() { close( fds[0] ); close( fds[1] ); } void scanning() { char b[4096] = {}; while ( write( fds[1], b, sizeof( b ) ) > 0 ) {} } void done() { char b[4096]; while ( read( fds[0], b, sizeof( b ) ) > 0 ) {} } int fd() { return fds[1]; } }; static void fire() { assert( waiter().waiting.size() == 1 ); waiter().waiting.front()->OnPollOut(); assert( waiter().waiting.empty() ); } int main( int argc, char **argv ) { std::string which = argv[1]; Display d; CWaylandFb fb{ fcntl( d.fd(), F_DUPFD_CLOEXEC, 0 ) }; if ( which == "client-scanning" ) { fb.client = true; fb.OnCompositorAcquire(); assert( fb.refs == 2 ); d.scanning(); fb.Wayland_Buffer_Release( fb.m_pHostBuffer ); assert( fb.refs == 2 && fb.m_bCompositorAcquired ); d.done(); fire(); assert( fb.refs == 1 && !fb.m_bCompositorAcquired && xdg_log.oh_no == 0 ); } else if ( which == "client-idle" ) { fb.client = true; fb.OnCompositorAcquire(); fb.Wayland_Buffer_Release( fb.m_pHostBuffer ); assert( fb.refs == 1 && waiter().waiting.empty() ); } else if ( which == "composite-scanning" ) { fb.OnCompositorAcquire(); d.scanning(); fb.Wayland_Buffer_Release( fb.m_pHostBuffer ); assert( fb.refs == 1 && waiter().waiting.empty() ); } else if ( which == "shown-again" ) { fb.client = true; fb.OnCompositorAcquire(); d.scanning(); fb.Wayland_Buffer_Release( fb.m_pHostBuffer ); fb.OnCompositorAcquire(); d.done(); fire(); assert( fb.refs == 2 && fb.m_bCompositorAcquired ); fb.Wayland_Buffer_Release( fb.m_pHostBuffer ); assert( fb.refs == 1 && !fb.m_bCompositorAcquired && xdg_log.oh_no == 0 ); } else if ( which == "no-dmabuf" ) { CWaylandFb plain{ -1 }; plain.client = true; plain.OnCompositorAcquire(); plain.Wayland_Buffer_Release( plain.m_pHostBuffer ); assert( plain.refs == 1 && waiter().waiting.empty() ); } else { return 2; } return 0; } ''' class ForwardedBufferScanoutTest(unittest.TestCase): @classmethod def setUpClass(cls): cxx = shutil.which(os.environ.get("CXX", "c++")) if not cxx: raise AssertionError("C++ compiler required") pick = post_image(PICK, "src/output_image_pick.hpp") pick = pick[pick.index("struct OutputImagePick"):pick.rindex("}")] wayland = post_image(FORWARD, "src/Backends/WaylandBackend.cpp") scanout = block(wayland, " class CWaylandScanoutRelease", "\n };\n") methods = "\n".join(block(wayland, " void CWaylandFb::" + name) for name in ("OnCompositorAcquire()", "OnCompositorRelease()", "OnScanoutDone(", "Wayland_Buffer_Release(")) source = PREAMBLE.replace("PICK", pick).replace("SCANOUT", scanout).replace("METHODS", methods) + MAIN cls.tmp = tempfile.TemporaryDirectory(prefix="droiddeck-forwarded-scanout-") cls.addClassCleanup(cls.tmp.cleanup) probe = Path(cls.tmp.name) / "probe.cpp" probe.write_text(source) cls.binary = probe.with_suffix("") built = subprocess.run([cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror", "-Wno-unused-parameter", "-UNDEBUG", "-include", "string", str(probe), "-o", str(cls.binary)], capture_output=True, text=True) if built.returncode: raise AssertionError(built.stdout + built.stderr) def scenario(self, name): result = subprocess.run([str(self.binary), name], capture_output=True, text=True, timeout=10) self.assertEqual(result.returncode, 0, result.stdout + result.stderr) def test_forwarded_buffer_is_held_until_scanout_ends(self): self.scenario("client-scanning") def test_idle_forwarded_buffer_is_released_at_once(self): self.scenario("client-idle") def test_composite_output_image_keeps_the_release_at_latch(self): self.scenario("composite-scanning") def test_buffer_shown_again_waits_for_its_next_release(self): self.scenario("shown-again") def test_buffer_without_dmabuf_is_released_at_once(self): self.scenario("no-dmabuf") if __name__ == "__main__": unittest.main()