Xbox Cloud Gaming app for PS5 Homebrew
C
perf(linux): low-latency mailbox present, Vulkan NV12 decode, and handheld detection
Rafael Abud Ferreira63bf2a73a356
diff --git a/src/app/app_core.cpp b/src/app/app_core.cppindex 4b2c771..765b770 100644--- a/src/app/app_core.cpp+++ b/src/app/app_core.cpp@@ -288,6 +288,19 @@ bool coreStartup() {std::rename(settingsPath().c_str(), bad.c_str());XC_LOGW("settings: %s could not be read; defaults used, the file kept as %s", settingsPath().c_str(), bad.c_str());}+ // The handheld profile (the Steam Deck), once: 720p, which its 1280x800+ // screen shows nearly 1:1 (1080p is 2.25 times the pixels to decode, only+ // to be shrunk), and FSR without Anime4K Restore (its passes run at the+ // picture's size; only over the default upscaler). Both stay in Settings,+ // where the user can change them back, and are never changed again.+ if (platform::isHandheld() && !g_settings.handheldProfile) {+ g_settings.handheldProfile = true;+ // "1080p" and "best" (up to 1440p) both exceed what the screen shows.+ if (g_settings.resolution != "720p") g_settings.resolution = "720p";+ if (g_settings.upscaler == 2) g_settings.upscaler = 0;+ g_settings.save(settingsPath());+ XC_LOGI("settings: handheld profile applied (%s, upscaler %d)", g_settings.resolution.c_str(), g_settings.upscaler);+ }ui::setLanguage(ui::languageFromCode(g_settings.language));media::setPadSpeakerVolume(g_settings.padSpeakerVolume);XC_LOGI("settings: language %s, %s, region %s", g_settings.language.c_str(), g_settings.resolution.c_str(),diff --git a/src/app/autoplay.cpp b/src/app/autoplay.cppindex 408fff5..37b7e9d 100644--- a/src/app/autoplay.cpp+++ b/src/app/autoplay.cpp@@ -110,8 +110,10 @@ void loadAutoplay() {if (opt == "imgtest") g_autoplay.imgTest = true;if (opt == "cloudtimetest") g_autoplay.cloudTimeTest = true;if (opt.rfind("cloudleft=", 0) == 0) g_autoplay.cloudLeft = std::atoll(opt.c_str() + 10);+ if (opt.rfind("abcycle=", 0) == 0) g_autoplay.abCycleSec = std::atoi(opt.c_str() + 8);// Threads pinned by role (platform.h), to measure it again.if (opt == "pin") platform::setNoThreadPinning(false);+ if (opt == "handheld") platform::forceHandheld(true); // the handheld profile on this machine (A/B)if (opt == "rumbletest") input::setRumble(200, 200, 1500);if (opt == "triggertest") input::setTriggerRumble(255, 128, 3000);if (opt == "queuetest" || opt == "padspktest") g_autoplay.padSpkTest = true;@@ -132,6 +134,7 @@ void loadAutoplay() {if (opt == "restore") g_autoplay.restore = true;if (opt.rfind("swap=", 0) == 0) display::setSwapImages(std::atoi(opt.c_str() + 5));if (opt == "nopace") display::setPresentWait(false);+ if (opt == "fifo") display::setLowLatencyPresent(false); // the desktop's old FIFO presenting (A/B)if (opt == "detailtest" || opt == "detailstest") g_autoplay.detailTest = true;if (opt == "pad") g_autoplay.pad = true;if (opt == "consoles") g_autoplay.consoles = true;diff --git a/src/app/autoplay.h b/src/app/autoplay.hindex d68e534..0a2af54 100644--- a/src/app/autoplay.h+++ b/src/app/autoplay.h@@ -65,6 +65,7 @@ struct Autoplay {bool legacy = false; // legacy: the stream as it was tuned before 2026-11-04 (the A/B baseline)bool imgTest = false; // imgtest: 25 s before the end, one picture drawn with each look (img_*.ppm)int64_t cloudLeft = -1; // cloudleft=N: a stream counts down from N seconds left (the menu, the notices)+ int abCycleSec = 0; // abcycle=N: A/B conditions in one stream, the next every N s (worker_stream.cpp)bool cloudTimeTest = false; // cloudtimetest: the cloud time line (4 h 10 of 15 h, 1 h 30, none, unlimited), the game menu with it, the time-up errorbool cpuDisplay = false; // the CPU display even where the GPU comes upint upscaler = -1; // ai: Anime4K instead of the settingdiff --git a/src/app/settings.cpp b/src/app/settings.cppindex 73eb8e9..c013a58 100644--- a/src/app/settings.cpp+++ b/src/app/settings.cpp@@ -98,6 +98,7 @@ bool Settings::load(const std::string& path) {micEnabled = (*j)["micEnabled"].asBool(micEnabled);highRefresh = (*j)["highRefresh"].asBool(highRefresh);desktopFullscreen = (*j)["desktopFullscreen"].asBool(desktopFullscreen);+ handheldProfile = (*j)["handheldProfile"].asBool(handheldProfile);maxBitrateMbps = static_cast<int>(std::clamp<int64_t>((*j)["maxBitrateMbps"].asInt(maxBitrateMbps), 0, 50));gameVolume = static_cast<int>(std::clamp<int64_t>((*j)["gameVolume"].asInt(gameVolume), 0, 200));keyMap = keyMapFromJson((*j)["keyMap"], input::defaultKeyMap());@@ -260,6 +261,7 @@ bool Settings::save(const std::string& path) const {v.set("micEnabled", micEnabled);v.set("highRefresh", highRefresh);v.set("desktopFullscreen", desktopFullscreen);+ v.set("handheldProfile", handheldProfile);v.set("maxBitrateMbps", maxBitrateMbps);v.set("gameVolume", gameVolume);v.set("keyMap", keyMapToJson(keyMap));diff --git a/src/app/settings.h b/src/app/settings.hindex 681fc9f..523f961 100644--- a/src/app/settings.h+++ b/src/app/settings.h@@ -144,6 +144,9 @@ struct Settings {// A desktop app: its window fullscreen (the desktop's own mode) or not.// Unused on the PS5.bool desktopFullscreen = true; // streams that showed the touchpad gestures' hint (three do)+ // The handheld profile was applied (once, app_core.cpp): not again, so+ // the user's later choices stay.+ bool handheldProfile = false;// Per-game profiles overriding the general settings (productId or titleId -> profile).std::map<std::string, GameProfile> perGame;diff --git a/src/app/stream_player.cpp b/src/app/stream_player.cppindex 8de5dde..9db80c2 100644--- a/src/app/stream_player.cpp+++ b/src/app/stream_player.cpp@@ -135,6 +135,7 @@ struct StreamPlayer::Impl {media::AudioEncoder audioEncoder;std::atomic<bool> highRefreshOutput{false}; // setHighRefreshOutput()std::atomic<bool> legacy{false}; // setLegacyTuning()+ std::atomic<int> decoderSwitch{-1}; // switchDecoder(): 0 software, 1 hardwarestd::atomic<bool> micEnabled{true};std::atomic<bool> micMuted{false};std::atomic<float> micGain{1.0f};@@ -550,12 +551,12 @@ struct StreamPlayer::Impl {void videoLoop() {platform::setThreadAffinity(platform::ThreadRole::VideoDecode, "xcloud_vdec");- media::VideoDecoder decoder;- if (!decoder.init(decodeThreads, preferHwDecode)) {+ auto decoder = std::make_unique<media::VideoDecoder>();+ if (!decoder->init(decodeThreads, preferHwDecode)) {end("could not start the H.264 decoder");return;}- isHwDecode = decoder.isHardware();+ isHwDecode = decoder->isHardware();const bool useAsyncPresent = display::caps().asyncPresent;platform::Thread presentThread;if (useAsyncPresent) {@@ -587,8 +588,8 @@ struct StreamPlayer::Impl {// pictures can come out later than their input.std::deque<stream::FrameMetadata> inFlight;media::HwDecoder hw;- bool hwOk = hwProbe && !decoder.isHardware() && hw.init(1920, 1088);- if (hwProbe && !decoder.isHardware()) XC_LOGI("hwdec: probe %s", hwOk ? "running" : "failed to start");+ bool hwOk = hwProbe && !decoder->isHardware() && hw.init(1920, 1088);+ if (hwProbe && !decoder->isHardware()) XC_LOGI("hwdec: probe %s", hwOk ? "running" : "failed to start");uint64_t hwUs = 0, hwMaxUs = 0, hwPictures = 0, hwCalls = 0, hwLogAt = platform::nowMs() + 1000;constexpr uint64_t kShownRing = 8;struct OnItsWay {@@ -691,9 +692,20 @@ struct StreamPlayer::Impl {meta.submittedMs = stream::clockMs();inFlight.push_back(meta);if (inFlight.size() > 16) inFlight.pop_front();+ // A/B tests: another decoder while streaming, from the next key+ // frame (autoplay "abcycle").+ if (int want = decoderSwitch.exchange(-1); want >= 0) {+ auto next = std::make_unique<media::VideoDecoder>();+ if (next->init(decodeThreads, want == 1)) {+ decoder = std::move(next);+ keyframeWanted = true;+ requestKeyframeIfWanted();+ XC_LOGI("stream: decoder switched to %s", decoder->isHardware() ? "hardware" : "software");+ }+ }uint64_t t0 = platform::nowUs();- bool ok = decoder.decode(au.data(), au.size(), pic, frame.rtpTimestamp);- isHwDecode = decoder.isHardware();+ bool ok = decoder->decode(au.data(), au.size(), pic, frame.rtpTimestamp);+ isHwDecode = decoder->isHardware();uint64_t t1 = platform::nowUs();bool known = false;if (ok) {@@ -713,15 +725,15 @@ struct StreamPlayer::Impl {loggedFirstDecode = true;XC_LOGI("first video decode done: ok=%d", ok);}- if (decoder.needsKeyframe()) keyframeWanted = true;+ if (decoder->needsKeyframe()) keyframeWanted = true;requestKeyframeIfWanted();if (!ok) {- if (!decoder.pending()) ++decodeFailures;+ if (!decoder->pending()) ++decodeFailures;continue;}++decodedFrames;- if (decoder.lastQp() >= 0) {- qpSumX100 += static_cast<uint64_t>(decoder.lastQp() * 100);+ if (decoder->lastQp() >= 0) {+ qpSumX100 += static_cast<uint64_t>(decoder->lastQp() * 100);++qpCount;}pictureSize = (static_cast<uint32_t>(pic.width) << 16) | static_cast<uint32_t>(pic.height);@@ -734,7 +746,7 @@ struct StreamPlayer::Impl {latestPic.pic = pic;latestPic.meta = meta;latestPic.known = known;- latestPic.isHw = decoder.isHardware();+ latestPic.isHw = decoder->isHardware();latestPic.arrivalUs = frame.arrivalUs;++latestPic.seq;if (!latestPic.isHw) {@@ -776,7 +788,7 @@ struct StreamPlayer::Impl {if (decodedFrames == 1) {XC_LOGI("first draw %dx%d (%s, %s)", pic.width, pic.height,pic.format == media::Picture::Format::Nv12 ? "NV12" : "YUV420P",- decoder.isHardware() ? "hardware" : "software");+ decoder->isHardware() ? "hardware" : "software");}uint64_t t2 = platform::nowUs();// Try acquire with zero wait first; if not ready, wait for swapchain image.@@ -1378,6 +1390,8 @@ StreamPlayer::Stats StreamPlayer::stats() const {return st;}+void StreamPlayer::switchDecoder(bool hardware) { impl_->decoderSwitch = hardware ? 1 : 0; }+void StreamPlayer::setLegacyTuning(bool on) {impl_->legacy = on;if (on) XC_LOGI("stream: legacy tuning (measurement baseline)");diff --git a/src/app/stream_player.h b/src/app/stream_player.hindex dab8928..9de7903 100644--- a/src/app/stream_player.h+++ b/src/app/stream_player.h@@ -68,6 +68,9 @@ public:// The screen shows this stream at 119.88 Hz: a couple of frames that// came at once are shown instead of skipped (a refresh drains them).void setHighRefreshOutput(bool on);+ // Measurements only (autoplay "abcycle"): the hardware or the software+ // decoder from the next frame on, while streaming.+ void switchDecoder(bool hardware);void setMicEnabled(bool enabled);bool micEnabled() const;void setMicMuted(bool muted);diff --git a/src/app/worker_stream.cpp b/src/app/worker_stream.cppindex 1bf7212..70cfbc2 100644--- a/src/app/worker_stream.cpp+++ b/src/app/worker_stream.cpp@@ -6,6 +6,7 @@#include "app/crash_guard.h"#include "app/shell.h"#include "app/stream_player.h"+#include "display/display.h"#include "input/controller.h"#include "media/audio_out.h"#include "media/audio_queue.h"@@ -21,6 +22,7 @@#include <cmath>#include <cstdio>#include <cstdlib>+#include <iterator>#include <vector>namespace xc::app {@@ -242,9 +244,33 @@ std::string stream(xcloud::GssvClient& gssv, const auth::CloudTimeQuery& cloudQuwatch.step = step;watch.beatMs = platform::nowMs();};+ // A/B tests in one stream (autoplay "abcycle=N"): every N s the next+ // condition, so all of them share the queue wait, the game and the network.+ struct AbCondition {+ const char* name;+ bool mailbox, hardware, restore;+ };+ static constexpr AbCondition kAb[] = {{"new", true, true, true},+ {"fifo", false, true, true},+ {"sw", true, false, true},+ {"old", false, false, true},+ {"norestore", true, true, false}};+ int abIndex = -1;+ bool abHardware = true;+ uint64_t abNext = started + 20000;while (player.running() && !g_cancel) {beat("loop");uint64_t elapsed = platform::nowMs() - started;+ if (g_autoplay.abCycleSec > 0 && started + elapsed >= abNext) {+ abIndex = (abIndex + 1) % static_cast<int>(std::size(kAb));+ const AbCondition& c = kAb[abIndex];+ display::setLowLatencyPresent(c.mailbox);+ display::setRestore(c.restore);+ if (c.hardware != abHardware || abIndex == 0) player.switchDecoder(c.hardware);+ abHardware = c.hardware;+ abNext = started + elapsed + static_cast<uint64_t>(g_autoplay.abCycleSec) * 1000u;+ XC_LOGI("AUTOPLAY AB: condition %s", c.name);+ }beat("the cloud time");cloudTimeStep(started + elapsed);if (!g_autoplay.title.empty()) {diff --git a/src/display/display.h b/src/display/display.hindex 7cf5371..6cea428 100644--- a/src/display/display.h+++ b/src/display/display.h@@ -79,8 +79,12 @@ struct DisplayCaps {};DisplayCaps caps();-// Before init(): swapchain images (GPU only; 2 = lowest latency, the default).+// Before init(): swapchain images (GPU only; 3 by default).void setSwapImages(int count);+// The desktop's low-latency presenting (on by default there: MAILBOX when+// the window system offers it); before init() or while streaming (A/B+// tests). Off: FIFO, as before. The consoles keep their own: a no-op there.+void setLowLatencyPresent(bool on);// Before init(): wait for each frame to reach the screen before the next// (GPU only, on by default).void setPresentWait(bool on);diff --git a/src/display/display_null.cpp b/src/display/display_null.cppindex e2b53f3..8b7bb3a 100644--- a/src/display/display_null.cpp+++ b/src/display/display_null.cpp@@ -34,6 +34,7 @@ void setUpscaler(int) {}void setRestore(bool) {}DisplayCaps caps() { return {}; }void setSwapImages(int) {}+void setLowLatencyPresent(bool) {}void setPresentWait(bool) {}bool setHighRefresh(bool) { return false; }bool canSwitchRefresh() { return false; }diff --git a/src/display/display_ps4.cpp b/src/display/display_ps4.cppindex e618e9b..b77e873 100644--- a/src/display/display_ps4.cpp+++ b/src/display/display_ps4.cpp@@ -962,6 +962,7 @@ DisplayCaps caps() {return c;}void setSwapImages(int) {}+void setLowLatencyPresent(bool) {}void setPresentWait(bool) {}bool canSwitchRefresh() { return false; }bool setHighRefresh(bool) { return false; }diff --git a/src/display/display_ps5.cpp b/src/display/display_ps5.cppindex bbd6ab8..4f5bc38 100644--- a/src/display/display_ps5.cpp+++ b/src/display/display_ps5.cpp@@ -533,6 +533,7 @@ DisplayCaps caps() {// Straight to the GPU, whether it came up or not, as these always went.void setSwapImages(int count) { gpu::setSwapImages(count); }+void setLowLatencyPresent(bool) {} // the PS5 keeps FIFO and its pacingvoid setPresentWait(bool on) { gpu::setPresentWait(on); }bool setHighRefresh(bool on) { return gpu::setHighRefresh(on); }bool canSwitchRefresh() { return gpu::canSwitchRefresh(); }diff --git a/src/display/display_sdl.cpp b/src/display/display_sdl.cppindex 51f88d1..7df17a2 100644--- a/src/display/display_sdl.cpp+++ b/src/display/display_sdl.cpp@@ -25,6 +25,7 @@#include <condition_variable>#include <cstdlib>#include <cstring>+#include <deque>#include <utility>namespace xc::display {@@ -38,6 +39,7 @@ struct Frame {Kind kind = Kind::None;std::vector<uint32_t> rgba; // Menu: kWidth x kHeight, 0xAABBGGRRstd::vector<uint8_t> y, u, v; // Video: the planes, rows packed+ bool nv12 = false; // Video: u holds the interleaved UV plane (the Vulkan path)int width = 0, height = 0; // Videouint64_t id = 0; // present()'s count when handed over};@@ -62,6 +64,16 @@ SDL_ThreadID g_mainThread = 0;bool g_startFullscreen = true; // the setting's defaultbool g_startForced = false; // the command line chosebool g_shown = false; // the window shows from its first picture on+std::atomic<bool> g_lowLatencyPresent{true}; // setLowLatencyPresent(): MAILBOX when offered++// Video frames handed to the GPU, by present id, until the screen shows them+// (VK_KHR_present_wait, polled without waiting in every pump()): each frame+// gets the time it reached the screen itself, not the one an earlier present+// got there at. Main thread only.+struct PendingShow {+ uint64_t presentId, frameId;+};+std::deque<PendingShow> g_pendingShows;Frame g_front; // the picture on screenOverlay g_overlayShown;bool g_frontPending = false, g_overlayPending = false; // not uploaded yet@@ -166,14 +178,32 @@ void renderScene() {}}+// The newest handed frame the GPU says is on the screen, with that time.+void pollShown() {+ if (g_pendingShows.empty()) return;+ uint64_t gpuId = 0, atUs = 0;+ if (!gpu::lastShown(gpuId, atUs)) return;+ uint64_t frame = 0;+ while (!g_pendingShows.empty() && g_pendingShows.front().presentId <= gpuId) {+ frame = g_pendingShows.front().frameId;+ g_pendingShows.pop_front();+ }+ if (!frame) return;+ std::lock_guard<std::mutex> lock(g_shownMutex);+ g_shownId = frame;+ g_shownAtUs = atUs;+}+bool drawGpuFront() {if (g_front.kind == Kind::Menu) {gpu::drawRgba(g_front.rgba.data());} else if (g_front.kind == Kind::Video) {int cw = (g_front.width + 1) / 2;- if (!gpu::drawYuv420(g_front.y.data(), g_front.u.data(), g_front.v.data(), g_front.width, cw, cw,- g_front.width, g_front.height, true))- return false;+ bool drawn = g_front.nv12 ? gpu::drawNv12(g_front.y.data(), g_front.u.data(), g_front.width, cw * 2,+ g_front.width, g_front.height, true)+ : gpu::drawYuv420(g_front.y.data(), g_front.u.data(), g_front.v.data(), g_front.width,+ cw, cw, g_front.width, g_front.height, true);+ if (!drawn) return false;} else {return false;}@@ -185,11 +215,10 @@ bool drawGpuFront() {}gpu::present();if (g_front.kind == Kind::Video) {- std::lock_guard<std::mutex> lock(g_shownMutex);- g_shownId = g_front.id;- uint64_t gpuId = 0, gpuAtUs = 0;- g_shownAtUs = gpu::lastShown(gpuId, gpuAtUs) ? gpuAtUs : platform::nowUs();+ g_pendingShows.push_back({gpu::lastPresentId(), g_front.id});+ if (g_pendingShows.size() > 16) g_pendingShows.pop_front(); // never shown (hidden window)}+ pollShown();return true;}@@ -253,6 +282,7 @@ bool init(bool preferGpu) {};// Poll present completion without blocking the main thread's event loop.gpu::setPresentWait(false);+ gpu::setMailbox(g_lowLatencyPresent);if (wsi.getInstanceProcAddr && exts && gpu::init(wsi)) {XC_LOGI("display: SDL %d.%d.%d, video driver %s, renderer vulkan%s",SDL_VERSIONNUM_MAJOR(SDL_GetVersion()), SDL_VERSIONNUM_MINOR(SDL_GetVersion()),@@ -325,10 +355,19 @@ bool drawNv12(const uint8_t* y, const uint8_t* uv, int strideY, int strideUV,g_back.kind = Kind::Video;g_back.width = width, g_back.height = height;g_back.y.resize(static_cast<size_t>(width) * height);- g_back.u.resize(static_cast<size_t>(cw) * ch);- g_back.v.resize(static_cast<size_t>(cw) * ch);for (int r = 0; r < height; ++r)std::memcpy(&g_back.y[static_cast<size_t>(r) * width], y + static_cast<size_t>(r) * strideY, width);+ // The Vulkan path takes the UV plane as it is (a hardware decoder's+ // NV12); SDL_Renderer's IYUV texture needs it split.+ g_back.nv12 = gpu::ready();+ if (g_back.nv12) {+ const size_t row = static_cast<size_t>(cw) * 2;+ g_back.u.resize(row * ch);+ for (int r = 0; r < ch; ++r) std::memcpy(&g_back.u[r * row], uv + static_cast<size_t>(r) * strideUV, row);+ return true;+ }+ g_back.u.resize(static_cast<size_t>(cw) * ch);+ g_back.v.resize(static_cast<size_t>(cw) * ch);for (int r = 0; r < ch; ++r) {const uint8_t* src = uv + static_cast<size_t>(r) * strideUV;uint8_t* du = &g_back.u[static_cast<size_t>(r) * cw];@@ -347,6 +386,7 @@ bool drawYuv420(const uint8_t* y, const uint8_t* u, const uint8_t* v, int strideif (width <= 0 || height <= 0) return false;const int cw = (width + 1) / 2, ch = (height + 1) / 2;g_back.kind = Kind::Video;+ g_back.nv12 = false;g_back.width = width, g_back.height = height;g_back.y.resize(static_cast<size_t>(width) * height);g_back.u.resize(static_cast<size_t>(cw) * ch);@@ -392,6 +432,7 @@ void serveReadBack() {void pump() {if ((!g_renderer && !gpu::ready()) || !onMainThread()) return;+ if (gpu::ready()) pollShown();{std::lock_guard<std::mutex> lock(g_mailboxMutex);if (g_mailboxFull) {@@ -476,6 +517,10 @@ DisplayCaps caps() {return c;}void setSwapImages(int count) { gpu::setSwapImages(count); }+void setLowLatencyPresent(bool on) {+ g_lowLatencyPresent = on;+ gpu::setMailbox(on);+}void setPresentWait(bool on) { gpu::setPresentWait(on); }bool setHighRefresh(bool) { return false; }bool canSwitchRefresh() { return false; }diff --git a/src/display/gpu.cpp b/src/display/gpu.cppindex 457abd4..f0ab7e2 100644--- a/src/display/gpu.cpp+++ b/src/display/gpu.cpp@@ -172,6 +172,8 @@ std::atomic<int> g_upscaler{0}; // 0 FSR (EASU), 1 Anime4Kstd::atomic<bool> g_restore{false}; // Anime4K Restore before the upscaleuint32_t g_frame = 0;uint32_t g_swapImages = 3;+std::atomic<bool> g_mailbox{false}; // setMailbox(): any thread; the next frame rebuilds the swapchain+bool g_mailboxBuilt = false; // what the swapchain was built withbool g_noPresentWait = true;// The longest a frame waits for the GPU or a free swapchain image (250 ms).constexpr uint64_t kGpuWaitNs = 250'000'000ull;@@ -463,6 +465,17 @@ bool buildSwapchain(bool first) {: caps.currentTransform;info.compositeAlpha = alpha;info.presentMode = VK_PRESENT_MODE_FIFO_KHR;+ const bool mailboxWanted = g_mailbox;+ if (mailboxWanted) {+ VkPresentModeKHR modes[8];+ uint32_t count = 8;+ vkGetPhysicalDeviceSurfacePresentModesKHR(g.physical, g.surface, &count, modes);+ for (uint32_t i = 0; i < count; ++i)+ if (modes[i] == VK_PRESENT_MODE_MAILBOX_KHR) info.presentMode = VK_PRESENT_MODE_MAILBOX_KHR;+ }+ if (first || mailboxWanted != g_mailboxBuilt)+ XC_LOGI("gpu: present mode %s", info.presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? "mailbox" : "fifo");+ g_mailboxBuilt = mailboxWanted;info.clipped = VK_TRUE;VkResult r = vkCreateSwapchainKHR(g.device, &info, nullptr, &g.swapchain);if (r != VK_SUCCESS) return fail("vkCreateSwapchainKHR", r);@@ -945,6 +958,7 @@ bool beginFrame(bool wait) {if (w == 0 || h == 0) return false;if (w != g.extent.width || h != g.extent.height) g.needSwapchain = true;}+ if (g_mailbox != g_mailboxBuilt) g.needSwapchain = true; // setMailbox() changed itif (g.needSwapchain && !recreateSwapchain()) return false;if (g.presentWait && g.presented) {if (!g_noPresentWait) {@@ -1327,6 +1341,8 @@ bool canSwitchRefresh() { return wsi_videoout_set_high_refresh != nullptr; }void setSwapImages(int count) { g_swapImages = static_cast<uint32_t>(std::clamp(count, 2, 5)); }+void setMailbox(bool on) { g_mailbox = on; }+bool readBack(std::vector<uint8_t>& rgb, int& width, int& height) {if (!g.ready || g.lastPresented == UINT32_MAX) return false;if (g.fencePending) {diff --git a/src/display/gpu.h b/src/display/gpu.hindex dce2e1d..9025543 100644--- a/src/display/gpu.h+++ b/src/display/gpu.h@@ -61,8 +61,13 @@ void setRestore(bool on);bool setHighRefresh(bool on);// Whether setHighRefresh() can work at all (the patched RADV).bool canSwitchRefresh();-// Before init(): swapchain images (2 = lowest latency, the default).+// Before init(): swapchain images (3 by default; Wayland's minimum is 3).void setSwapImages(int count);+// The desktop's choice (before init(), or later from any thread: the next+// frame rebuilds the swapchain): MAILBOX when the surface offers it, so a+// new frame replaces one still waiting and nothing blocks; FIFO otherwise,+// and always when off (the default: the PS5's).+void setMailbox(bool on);// Before init(): waiting for each frame to reach the screen before the next// (VK_KHR_present_wait, on by default). Off, the display time is still// measured, by polling.diff --git a/src/media/decoder.cpp b/src/media/decoder.cppindex bb807be..4fa6a85 100644--- a/src/media/decoder.cpp+++ b/src/media/decoder.cpp@@ -149,6 +149,9 @@ bool VideoDecoder::init(int threads, bool preferHw) {enum AVHWDeviceType devType;enum AVPixelFormat pixFmt;} kHwConfigs[] = {+ // Vulkan Video first: the desktop build's FFmpeg has it (libvulkan+ // opened at run time); the others only where a build adds them.+ {AV_HWDEVICE_TYPE_VULKAN, AV_PIX_FMT_VULKAN},{AV_HWDEVICE_TYPE_VAAPI, AV_PIX_FMT_VAAPI},{AV_HWDEVICE_TYPE_CUDA, AV_PIX_FMT_CUDA},{AV_HWDEVICE_TYPE_VDPAU, AV_PIX_FMT_VDPAU},diff --git a/src/platform/android/platform_android.cpp b/src/platform/android/platform_android.cppindex 2904ff0..1b151ad 100644--- a/src/platform/android/platform_android.cpp+++ b/src/platform/android/platform_android.cpp@@ -121,6 +121,8 @@ std::string platformName() { return "Android"; }size_t imageCacheBudget() { return 96u << 20; }void setNoThreadPinning(bool) {}+bool isHandheld() { return false; }+void forceHandheld(bool) {}void setThreadAffinity(ThreadRole, const char*) {}void setThreadRealtimePriority() {}diff --git a/src/platform/handheld.h b/src/platform/handheld.hnew file mode 100644index 0000000..3dd04f6--- /dev/null+++ b/src/platform/handheld.h@@ -0,0 +1,23 @@+// SPDX-License-Identifier: GPL-3.0-or-later+// Copyright (C) 2026 RafaelNGP+// Whether the machine is a handheld PC (platform::isHandheld()): the Steam+// Deck by its DMI strings. Pure, so the tests can check it.+#pragma once++#include <string_view>++namespace xc::platform {++// /sys/class/dmi/id/sys_vendor and product_name, as read (with their newline):+// Valve's "Jupiter" (the LCD Deck) or "Galileo" (the OLED one).+inline bool steamDeckDmi(std::string_view vendor, std::string_view product) {+ auto trim = [](std::string_view s) {+ while (!s.empty() && (s.back() == '\n' || s.back() == '\r' || s.back() == ' ')) s.remove_suffix(1);+ return s;+ };+ vendor = trim(vendor);+ product = trim(product);+ return vendor == "Valve" && (product == "Jupiter" || product == "Galileo");+}++} // namespace xc::platformdiff --git a/src/platform/linux/platform_linux.cpp b/src/platform/linux/platform_linux.cppindex 81de612..a6e6b87 100644--- a/src/platform/linux/platform_linux.cpp+++ b/src/platform/linux/platform_linux.cpp@@ -5,10 +5,12 @@// executable, and notifications as toasts inside the desktop app (or on// stdout for the tools).#include "platform/platform.h"+#include "platform/handheld.h"#include "platform/toasts.h"#include "util/log.h"+#include <atomic>#include <cerrno>#include <csignal>#include <cstdio>@@ -172,12 +174,21 @@ std::string diagnostics() { return {}; }std::string platformName() { return "Linux"; }size_t imageCacheBudget() { return 160u << 20; }-void setNoThreadPinning(bool) {}+// Threads keep the scheduler's choice unless setNoThreadPinning(false), as+// on the PS5 (pinning by role made its sound run dry more often): pinned,+// FFmpeg's decode threads all sat on the video thread's one CPU, and on SMT+// machines (the Steam Deck) the workers shared that core's sibling.+namespace {+std::atomic<bool> g_noThreadPinning{true};+} // namespace++void setNoThreadPinning(bool on) { g_noThreadPinning = on; }void setThreadAffinity(ThreadRole role, const char* name) {if (name && *name) {pthread_setname_np(pthread_self(), name);}+ if (g_noThreadPinning) return;long numCores = sysconf(_SC_NPROCESSORS_ONLN);if (numCores < 4) return; // Do not restrict on systems with few cores@@ -204,6 +215,22 @@ void setThreadAffinity(ThreadRole role, const char* name) {pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &mask);}+namespace {+int g_handheldForced = -1; // forceHandheld(): -1 not forced+} // namespace++bool isHandheld() {+ if (g_handheldForced >= 0) return g_handheldForced == 1;+ if (const char* env = std::getenv("PSBOX_HANDHELD"); env && *env) return std::strcmp(env, "0") != 0;+ if (const char* env = std::getenv("SteamDeck"); env && std::strcmp(env, "1") == 0) return true;+ std::string vendor, product;+ readFile("/sys/class/dmi/id/sys_vendor", vendor);+ readFile("/sys/class/dmi/id/product_name", product);+ return steamDeckDmi(vendor, product);+}++void forceHandheld(bool on) { g_handheldForced = on ? 1 : 0; }+void setThreadRealtimePriority() {struct sched_param param{};int maxPrio = sched_get_priority_max(SCHED_RR);diff --git a/src/platform/platform.h b/src/platform/platform.hindex a3da60c..5e00797 100644--- a/src/platform/platform.h+++ b/src/platform/platform.h@@ -74,6 +74,13 @@ void setThreadAffinity(ThreadRole role, const char* name = nullptr);void setNoThreadPinning(bool on);void setThreadRealtimePriority();+// A handheld PC to spare (the handheld profile: app/app_core.cpp): the Steam+// Deck, by its DMI strings or Steam's SteamDeck=1; PSBOX_HANDHELD=1 or 0+// decides for any other machine. Always false on the consoles.+bool isHandheld();+// Tests: decides isHandheld() from now on (autoplay "handheld").+void forceHandheld(bool on);+// Short on-screen message: the PS5's system notification; in the desktop// app a toast in the window (platform/toasts.h); stdout for the tools.void notify(const std::string& text);diff --git a/src/platform/ps4/platform_ps4.cpp b/src/platform/ps4/platform_ps4.cppindex a49b972..9092731 100644--- a/src/platform/ps4/platform_ps4.cpp+++ b/src/platform/ps4/platform_ps4.cpp@@ -1129,6 +1129,8 @@ void notify(const std::string& text, const std::string& forLog) {}void setNoThreadPinning(bool) {}+bool isHandheld() { return false; }+void forceHandheld(bool) {}void setThreadAffinity(ThreadRole role, const char* name) {if (name && *name) {diff --git a/src/platform/ps5/platform_ps5.cpp b/src/platform/ps5/platform_ps5.cppindex acc493c..da7aa47 100644--- a/src/platform/ps5/platform_ps5.cpp+++ b/src/platform/ps5/platform_ps5.cpp@@ -279,6 +279,8 @@ std::atomic<bool> g_noThreadPinning{true};} // namespacevoid setNoThreadPinning(bool on) { g_noThreadPinning = on; }+bool isHandheld() { return false; }+void forceHandheld(bool) {}void setThreadAffinity(ThreadRole role, const char* name) {if (name && *name) {diff --git a/src/platform/windows/platform_windows.cpp b/src/platform/windows/platform_windows.cppindex 15e60f7..8eed197 100644--- a/src/platform/windows/platform_windows.cpp+++ b/src/platform/windows/platform_windows.cpp@@ -273,6 +273,8 @@ void Thread::join() {}void setNoThreadPinning(bool) {}+bool isHandheld() { return false; }+void forceHandheld(bool) {}void setThreadAffinity(ThreadRole role, const char* name) {#if defined(_WIN32) || defined(__CYGWIN__)diff --git a/tests/test_main.cpp b/tests/test_main.cppindex c0534fd..0635fa4 100644--- a/tests/test_main.cpp+++ b/tests/test_main.cpp@@ -24,6 +24,7 @@#include "net/http.h"#include "platform/platform.h"#include "platform/toasts.h"+#include "platform/handheld.h"#include "stream/input_packet.h"#include "stream/stream_session.h"#include "ui/app_ui.h"@@ -541,6 +542,15 @@ static std::string pbProduct(const std::string& id, const std::string& puid, intreturn pbMsg(2, pbMsg(1, id) + pbMsg(2, pbMsg(2, pbMsg(1, puid) + pbMsg(2, access))));}+static void testSteamDeckDmi() {+ using xc::platform::steamDeckDmi;+ CHECK(steamDeckDmi("Valve\n", "Jupiter\n"));+ CHECK(steamDeckDmi("Valve", "Galileo"));+ CHECK(!steamDeckDmi("Valve\n", "Index\n"));+ CHECK(!steamDeckDmi("ASUSTeK COMPUTER INC.\n", "ROG Ally RC71L_RC71L\n"));+ CHECK(!steamDeckDmi("", ""));+}+static void testCloudTimeClock() {using xc::ui::CloudTimeClock;using xc::ui::formatCloudTime;@@ -7179,6 +7189,7 @@ int main() {testPerGameSettings();testPlaytimeLimit();testCloudTimeClock();+ testSteamDeckDmi();testStrings();testRegions();testPrices();diff --git a/tools/build-ffmpeg.sh b/tools/build-ffmpeg.shindex 46ca8eb..556fcc9 100755--- a/tools/build-ffmpeg.sh+++ b/tools/build-ffmpeg.sh@@ -77,7 +77,25 @@ elif [[ $target == ps4 ]]; then--extra-cflags="$flags" --extra-ldflags="" \--disable-pthreads --enable-pthreadselse- "$src/configure" "${common[@]}" --cc=clang+ # Hardware H.264 decoding through Vulkan Video (FFmpeg opens libvulkan at+ # run time: no new dependency for the AppImage). Its configure wants+ # Vulkan headers 1.3.277 or newer: a pinned copy when the system's are+ # older (ubuntu-24.04 has 1.3.275), header files only.+ vkcheck='#include <vulkan/vulkan.h>+#if !(defined(VK_VERSION_1_4) || (defined(VK_VERSION_1_3) && VK_HEADER_VERSION >= 277))+#error old+#endif'+ vkflags=()+ if ! clang -E -x c - <<<"$vkcheck" >/dev/null 2>&1; then+ vkhdr=${XDG_CACHE_HOME:-$HOME/.cache}/xcloud-ps5/vulkan-headers-1.4.309+ if [[ ! -f $vkhdr/include/vulkan/vulkan.h ]]; then+ mkdir -p "$vkhdr"+ curl -sSLf https://github.com/KhronosGroup/Vulkan-Headers/archive/refs/tags/v1.4.309.tar.gz |+ tar xz -C "$vkhdr" --strip-components=1+ fi+ vkflags=(--extra-cflags="-I$vkhdr/include")+ fi+ "$src/configure" "${common[@]}" --cc=clang --enable-vulkan --enable-hwaccel=h264_vulkan "${vkflags[@]}"fimake -j"$(nproc)" >/dev/nullmake install >/dev/null