Xbox Cloud Gaming app for PS5 Homebrew
C
psbox: stream screenshots on the desktop
Rafael Abud Ferreira237a652941a1
diff --git a/docs/multiplatform.md b/docs/multiplatform.mdindex b79731e..580935b 100644--- a/docs/multiplatform.md+++ b/docs/multiplatform.md@@ -169,7 +169,9 @@ SDL_VIDEO_DRIVER=offscreen SDL_AUDIO_DRIVER=dummy build-desktop/psbox --selftestvideo thread never waits for vsync) and `display::pump()`, each pass of themain loop, uploads it (YUV into a BT.709 limited IYUV texture; the menu asABGR8888), blends the overlay and presents. `idleWait()` wakes on a new- frame. `readBackRgb()` works on the main thread only. No upscalers,+ frame. `readBackRgb()` reads on the main thread; asked from another one+ (the video thread's screenshots: autoplay's `screen.ppm`) it waits up to+ 500 ms for the next `pump()` to do it. No upscalers,sharpening, block smoothing or 120 Hz (`caps()`).- **Shell** (`shell_desktop.cpp`): SDL starts before the display, every eventgoes to `input::sdlInputEvent()`, focus lost lets the mouse go, closing thediff --git a/src/app/desktop_selftest.cpp b/src/app/desktop_selftest.cppindex 0a78446..7691c69 100644--- a/src/app/desktop_selftest.cpp+++ b/src/app/desktop_selftest.cpp@@ -39,6 +39,7 @@#include <atomic>#include <cstdio>#include <cstdlib>+#include <thread>#include <filesystem>#include <vector>@@ -262,6 +263,26 @@ int desktopSelftest(const std::string& dir) {for (int b = 0; b < 8 && barsOk; ++b) barsOk = barMatches(shot, b, 540);check(barsOk, "colour bars through drawYuv420 + pump (3-video.png)");check(display::lastPresentId() > 0, "present ids counted");+ // A read back asked from another thread (the video thread's+ // screenshots), done by the main loop's pump().+ {+ Shot t;+ std::atomic<bool> finished{false};+ bool ok = false;+ std::thread reader([&] {+ ok = display::readBackRgb(t.rgb, t.w, t.h);+ finished = true;+ });+ for (int i = 0; i < 200 && !finished; ++i) {+ display::idleWait(5);+ display::pump();+ }+ reader.join();+ for (int b = 0; b < 8 && ok; ++b) ok = barMatches(t, b, 540);+ std::string path = dir + "/3b-readback-thread.png";+ ok = ok && stbi_write_png(path.c_str(), t.w, t.h, 3, t.rgb.data(), t.w * 3) != 0;+ check(ok, "read back off the main thread (3b-readback-thread.png)");+ }ui::StreamMenu menu(fonts);menu.setCapabilities(caps);diff --git a/src/display/display_sdl.cpp b/src/display/display_sdl.cppindex a4fd4b5..c016c31 100644--- a/src/display/display_sdl.cpp+++ b/src/display/display_sdl.cpp@@ -72,6 +72,15 @@ Frame g_mailbox; // the newest picture handed overbool g_mailboxFull = false;uint64_t g_handed = 0;std::atomic<uint64_t> g_lastHanded{0};+// readBackRgb() from another thread (the video thread's screenshots): asked+// here, done by the next pump() on the main thread (under g_mailboxMutex).+struct ReadBack {+ bool asked = false, done = false, ok = false;+ std::vector<uint8_t> rgb;+ int w = 0, h = 0;+};+ReadBack g_readBack;+std::condition_variable g_readBackCv;std::mutex g_overlayMutex;Overlay g_overlay;@@ -260,6 +269,25 @@ void present() {g_mailboxCv.notify_all();}+namespace {+bool readBackMain(std::vector<uint8_t>& rgb, int& width, int& height);++// Another thread's readBackRgb(), done here with the newest picture.+void serveReadBack() {+ {+ std::lock_guard<std::mutex> lock(g_mailboxMutex);+ if (!g_readBack.asked || g_readBack.done) return;+ }+ ReadBack r;+ r.ok = readBackMain(r.rgb, r.w, r.h);+ std::lock_guard<std::mutex> lock(g_mailboxMutex);+ if (!g_readBack.asked || g_readBack.done) return; // given up meanwhile+ r.asked = r.done = true;+ g_readBack = std::move(r);+ g_readBackCv.notify_all();+}+} // namespace+void pump() {if (!g_renderer || !onMainThread()) return;{@@ -270,6 +298,7 @@ void pump() {g_frontPending = true;}}+ serveReadBack();{std::lock_guard<std::mutex> lock(g_overlayMutex);if (g_overlayChanged) {@@ -300,7 +329,7 @@ void pump() {void idleWait(uint32_t ms) {std::unique_lock<std::mutex> lock(g_mailboxMutex);- g_mailboxCv.wait_for(lock, std::chrono::milliseconds(ms), [] { return g_mailboxFull; });+ g_mailboxCv.wait_for(lock, std::chrono::milliseconds(ms), [] { return g_mailboxFull || (g_readBack.asked && !g_readBack.done); });}void setOverlay(const uint32_t* pixels, int x, int y, int w, int h, uint8_t opacity) {@@ -344,9 +373,11 @@ bool lastShown(uint64_t& id, uint64_t& atUs) {return true;}-bool readBackRgb(std::vector<uint8_t>& rgb, int& width, int& height) {- // The renderer is the main thread's: the video thread's snapshots get nothing.- if (!g_renderer || !onMainThread() || g_front.kind == Kind::None) return false;+namespace {++// The picture on screen read back (main thread).+bool readBackMain(std::vector<uint8_t>& rgb, int& width, int& height) {+ if (g_front.kind == Kind::None) return false;upload();if (!g_readTex)g_readTex = SDL_CreateTexture(g_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, kWidth, kHeight);@@ -374,4 +405,27 @@ bool readBackRgb(std::vector<uint8_t>& rgb, int& width, int& height) {return true;}+} // namespace++bool readBackRgb(std::vector<uint8_t>& rgb, int& width, int& height) {+ if (!g_renderer) return false;+ if (onMainThread()) return readBackMain(rgb, width, height);+ // The renderer is the main thread's: asked of its next pump(), waited+ // for a while (the main loop pumps each pass; not while it is closing).+ std::unique_lock<std::mutex> lock(g_mailboxMutex);+ if (g_readBack.asked) return false; // one at a time+ g_readBack = ReadBack{};+ g_readBack.asked = true;+ g_mailboxCv.notify_all(); // idleWait() returns: the main loop pumps+ bool done = g_readBackCv.wait_for(lock, std::chrono::milliseconds(500), [] { return g_readBack.done; });+ g_readBack.asked = false;+ if (!done || !g_readBack.ok) {+ if (!done) XC_LOGW("display: read back from another thread: no pump() came");+ return false;+ }+ rgb = std::move(g_readBack.rgb);+ width = g_readBack.w, height = g_readBack.h;+ return true;+}+} // namespace xc::display