Files
DroidDeck/tools/vr/src/vk_layer_droiddeck_vr.c
T
+13 e7bc4b03a5 Add app-wide 3DoF for VITURE, RayNeo and XREAL glasses and 6DoF for Luma Ultra (#1035)
* Add VITURE glasses support: hardware control, 3D, head tracking

Glasses get their own controller over the MCU (permission, 120 Hz default, brightness, volume, sunblock, 2D/3D) and the game moves onto them when plugged in. The IMU drives Cardboard head tracking and the SteamVR driver. 3D falls back to one eye's view on phones that can't send the 3840x1080 signal. Options open from a glasses button in game and on the Steam page.

* Drop the glasses settings like Steam's: Play-style button, Settings lip, tabs

* Hang the glasses Settings lip over the top of the button

* Space glasses MCU commands so the IMU rate sticks, fit fixed resolutions to the glasses

* Keep Steam alive with SteamVR registered, run SteamVR at the glasses' rate, keep the glasses lip on top

* Fit VR games on the glasses: full view, seated origin at the head, pad hands reach with the d-pad

* Let the pad hands reach a metre out so buttons with deep colliders press

* Show every VR frame in order at 120 fps: wait for SteamVR's GPU work, present only finished copies, skip the grace on idle shared counters

* Glasses: SteamVR starts and stops with them, closer view with a field of view setting, Recenter on the pad

* Glasses: SteamVR field of view defaults to 90°

* VR pad hands: sticks move and aim the hands, left stick click switches the sticks to the game, d-pad pulls a held hand

* Glasses: Play SteamVR beside the glasses button instead of starting SteamVR with them, pad hands drawn as Index controllers and off while other VR controllers are connected, glasses sheet tabs sized like Steam's

* Track glasses mode, refresh-aware glasses window, align Play SteamVR with Play Steam

* Fix 3D on the glasses: full-res SteamVR presenter, revert VR 3D at session end, coalesce vsync ticks, no session restart after end

* Address review: glasses output after installs and session end, VR revert retry, display fallback, presenter swapchain retry, agent hex checks, drawer bumpers, glasses translations, docs

* Harden VITURE stereo negotiation and output refresh tracking

* Localize stereo recovery feedback and record Astra reconnect tests

* Improve test failure diagnostics and refresh Astra firmware findings

* Handle VITURE docks separately and reject truncated USB frames

* Glasses: remember a 3D fallback per model, docks as display only, VR timing fixes

- A 3D attempt that ends at the 640x480 fallback is remembered for that glasses
  model, so Play SteamVR no longer blacks out the glasses after every restart;
  Retry clears it and "3D for VR games" stays reachable
- A VITURE Mobile Dock on its own no longer counts as glasses; through a dock
  Play SteamVR is hidden and the sheet shows the output and why there is no control
- HDR is probed on the glasses when the session starts there
- Glasses frames with a mismatched checksum are marked in the status frame log
- SteamVR layer and driver vblank counters stay monotonic across refresh changes;
  one-eye render scale applies from activation and on mono/3D switches
- Compositor binds announce the refresh clients were last told; the presenter
  retries a failed first swapchain
- Dead strings and helpers removed; docs updated

* Glasses: drop USB frames with a bad checksum

* Glasses: count frames dropped for a bad checksum

* Feat/viture xr (#907)

* Add RayNeo Air and GT support to the XR glasses pipeline

* Add RayNeo Air and GT support to the XR glasses pipeline

* Checkpoint glasses tracking, calibration, and SteamVR launch integration

* Checkpoint glasses tracking, calibration, and SteamVR launch integration

* Add XREAL 1S support and correct GT and Pro 2 tracking

* Add XREAL 1S support and correct GT and Pro 2 tracking

* Fix mirrored glasses tracking and recover SteamVR after device loss

* Fix mirrored glasses tracking and recover SteamVR after device loss

* Document confirmed XREAL 1S brightness control

* Document confirmed XREAL 1S brightness control

* Fix SteamVR glasses defaults and rendering controls

* Fix SteamVR glasses defaults and rendering controls

* chore: keep only the 3 newest session log folders (#852)

* chore: add agent comment rules (#748)

Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>

* Fix SteamVR polling, VITURE checksums, and fence tracking

Keep manual SteamVR launches polled, discard corrupt VITURE packets, and retire Vulkan fences out of order without blocking submissions at a fixed capacity. Preserve the contiguous completion watermark used by the presenter.

Fix optional Windows launcher configuration and audited helper staging. Add Android, Windows, and native tracker regression coverage.

* Fix SteamVR polling, VITURE checksums, and fence tracking

Keep manual SteamVR launches polled, discard corrupt VITURE packets, and retire Vulkan fences out of order without blocking submissions at a fixed capacity. Preserve the contiguous completion watermark used by the presenter.

Fix optional Windows launcher configuration and audited helper staging. Add Android, Windows, and native tracker regression coverage.

* fix: wireless ADB page names the child-process switch the way Android shows it (#848)

* l10n: refresh stale and untranslated strings in es, fr, ko, ru (#850)

* l10n: refresh stale and untranslated strings in es, fr, ko, ru

- mode_added_art_hint: now mentions Steam then SteamGridDB (all four locales).
- wincomp_needs_installer / wincomp_installers_note: es and ko still described
  the old "coming soon" behaviour; align with the current English text.
- summary_remove_game: was still English in all four locales.

Machine-assisted translation; native-speaker review is welcome.

* l10n(es): say registro, not grabación, for the missing installer record

---------

Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com>

* fix: a failed cloud save recovery no longer ends the app (#846)

The cloud-recovery thread started at every app start had nothing to
catch an error, so one went to CrashHandler, which halts the process
with status 2. In unit tests the thread can outlive the test that
started it, fail on the torn-down preferences, and take the whole test
run down. The error is now logged and the thread ends.

Co-authored-by: Kirk <12691869+KirkPatrickJunsay@users.noreply.github.com>

* fix: an interrupted Linux runtime download resumes instead of starting again (#847)

* fix: an interrupted Linux runtime download resumes instead of starting again

installOnce() asked the downloader to resume but always deleted the archive in its finally block, so the next attempt began at zero. The archive is now named after the release version and checksum, a failed download keeps what it fetched, and archives of other releases (and the old fixed name) are removed. A checksum or extraction failure still deletes the file.

* test: Downloader resumes a cut-off download from the bytes it kept

Starts a socket server that promises the whole file, hangs up part way and then answers a Range request, and checks that the second downloadFile call resumes with the right offset and ends with identical bytes.

* fix: a resumed download discards a 206 that does not start where the kept bytes end

Downloader appended every 206 response to the partial file without
looking at Content-Range. A server or proxy that answers a Range request
with a different offset would leave a corrupt file. Check that the range
starts at the kept length; otherwise delete the partial file and start
again without a Range header.

Adds a test with a server that answers the Range request from offset 0.

---------

Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com>

* l10n: German, French, Spanish, Italian, Portuguese and Russian for the zRAM, reset-settings and store free-space strings (#849)

* l10n: German, French, Spanish, Italian, Portuguese and Russian for the zRAM, reset-settings and store free-space strings

Adds the strings values-de, values-it and values-pt were missing (14 each) and the six that values-fr, values-es and values-ru were missing. Machine-assisted drafts that follow each file's existing register and terms; native review is welcome.

* l10n: correct missing active swap translations

---------

Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com>
Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com>

* fix: droiddeckctl reaches variant-package builds (recover #842) (#854)

fix: droiddeckctl reaches builds shipped under a variant package

The provider authority and the start Activity's component were fixed to
com.droiddeck.launcher, so droiddeckctl could not talk to the antutu,
ludashi or pubg builds from tools/release/variants.txt. DROIDDECK_PACKAGE
now picks the package; the Activity's class name stays the same in every
variant.

Co-authored-by: angelrenefm <17342133+angelrenefm@users.noreply.github.com>

* feat: droiddeckctl reports on-screen pacing (recover #843) (#855)

feat: droiddeckctl pacing reports the frames that reached the screen

Reads SurfaceFlinger's timestats for the game's layer (or the session's
SurfaceView) over a capture: frames on screen, fps on screen, the
present-to-present histogram and how many intervals were off the usual
cadence. Records split by a refresh-rate change are summed. A game's own fps
counter counts frames handed over, so it can't show dropped or doubled frames.

Co-authored-by: angelrenefm <17342133+angelrenefm@users.noreply.github.com>

* fix: Steam can restart more than twice in a session (#754)

* fix: Steam can restart more than twice in a session

Every exit 42 counted toward the cap that stops an update which will
not install, and the count was never cleared, so the third restart of
a session ended it however far apart they were. A client that ran for
a minute before asking is not that loop: the count starts again.

* fix: measure the client's run time as it exits

The time spent collecting logs and checking FEX after the client exits
counted toward its run, so a slow cleanup could make a quick exit 42
look like a long one and clear the count.

---------

Co-authored-by: Kirk <12691869+KirkPatrickJunsay@users.noreply.github.com>
Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>

* fix: the front end keeps its page when the Fold is folded or unfolded (#832)

* fix: the front end keeps its page when the Fold is folded or unfolded

MainActivity, FileManagerActivity, FilePickerActivity and StoreLoginActivity did not declare the screen-size configuration changes, so folding or unfolding a foldable recreated them and dropped their in-memory state. They now handle orientation, screenSize, screenLayout, smallestScreenSize and density themselves, like SessionActivity already does. uiMode is left out on purpose.

* fix: store login keeps rebuilding its toolbar when the display density changes

StoreLoginActivity builds its toolbar in code with dp-to-pixel values taken at creation time and has no onConfigurationChanged. It no longer declares density in configChanges, so a density change recreates it as before this PR. Size and layout changes are still handled in place.

---------

Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com>

* Release pointer buttons without replaying stale motion (#803)

* Fix cursor warping on native touch down

* Preserve input cleanup without replaying stale pointer motion

---------

Co-authored-by: siyuan.qsy <siyuan.qsy@alibaba-inc.com>

* l10n: translate the strings added since #284 into zh-CN, zh-HK, zh-TW and ja (#539)

* l10n: translate the 184 strings added since #284 (zh-CN/HK/TW, ja)

The stores, cloud saves, Windows components, Steam touch, desktop
removal and background-downloads strings landed after #284's
translation snapshot, so every one of them fell back to English.
Translate all 184 into Simplified Chinese, Traditional Chinese
(Taiwan and Hong Kong) and Japanese, keeping each locale file's own
vocabulary (工作階段/會話, 解除安裝/卸載, 元件/組件, 設定).

Also fix four Hong Kong strings that kept the simplified 着色器.

* l10n(zh): fix stale and ambiguous strings found in review

Severe: summary_remove_game was untranslated English in all three
files; mode_added_art_hint, wincomp_installers_note and
wincomp_needs_installer still described the pre-#480/#492 English.

Also: distinguish the Setup wizard from Android system settings in
zh-HK/zh-TW (初始設定 / 系統設定 / Android 設定), unify library as
收藏庫, component as 組件 in zh-HK, session as 會話 in zh-HK, account
as 帳戶 in zh-HK, Trending as 趋势/趨勢, Forget as 清除, reword the
re-applied-at-launch, action-failed and already-in-Games strings, and
play as 執行/開啟遊戲 in zh-TW/zh-HK.

* l10n(ja): fix stale and ambiguous strings found in review

---------

Co-authored-by: Dantalian <Dantalian-dev@users.noreply.github.com>
Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com>

* Rebuild and verify Windows VR runtime components

* Rebuild and verify Windows VR runtime components

* Add top, bottom, and centered picture positions for Steam sessions (#871)

* fix: dependency scan ignores folders above the game folder (#859)

* DroidDeck-Proton: get releases from the Droid-Deck org repo

DroidDeck-Proton is now built and released on Droid-Deck/DroidDeck-Proton
(11-9.1 is Latest there), so the Components page, the update check and the
guest installer read releases from the org repo instead of The412Banner's.

* feat: low-latency controller and touch toggles in the session drawer (#808)

* fix: SteamGridDB lookups no longer stick as "no match" after a failure; add a search to the art chooser (#876)

* feat: Steam home wall fills from the signed-in library instead of store picks (#878)

* test: cover GPU family and support classification (#880)

* Fix local builds on Linux failing after the first Comet build (#866)

* feat: sort the store library and installed tabs, and switch between card and list views (#798)

* feat: three-finger tap toggles Steam's on-screen keyboard in game mode (#782)

* fix: keep Quest-patched Turnip as the auto default (#853)

* Fix deploy_local.sh and droiddeckctl not finding a phone connected over wireless debugging (#864)

* fix: keep proot sessions alive when the kernel loses fork events (#457)

* fix(quest): correct desktop and Steam display corruption across sessions (#733)

fix(quest): stabilize desktop and Steam layer presentation

Keep Quest SDR scenes and the black backdrop off the Android Vulkan swapchain regardless of session order or HDR preferences. Only report the first frame after client content is submitted.

Add backdrop lifecycle and first-frame regression coverage. Record the user's successful Quest 3 Test 2 result.

Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com>

* fix: keep SteamVR syscall hooks and report Steam crashes (#905)

* fix: preserve SteamVR syscall hooks

* fix: report Steam crashes through gamescope

* fix: honor glasses pacing and resolution in SteamVR launches

---------

Co-authored-by: connor g <connorsspam1992@gmail.com>
Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>
Co-authored-by: Yassine Gherbi <y.gherbi.dev@gmail.com>
Co-authored-by: Resul Övür <92828257+ovurrsl@users.noreply.github.com>
Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com>
Co-authored-by: Kirk <kirtoy.junsay@gmail.com>
Co-authored-by: Kirk <12691869+KirkPatrickJunsay@users.noreply.github.com>
Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com>
Co-authored-by: angelrenefm <17342133+angelrenefm@users.noreply.github.com>
Co-authored-by: asdf17128 <asdf17128@gmail.com>
Co-authored-by: siyuan.qsy <siyuan.qsy@alibaba-inc.com>
Co-authored-by: Dantalian <wsu091227@qq.com>
Co-authored-by: Dantalian <Dantalian-dev@users.noreply.github.com>
Co-authored-by: The412Banner <the412banner@users.noreply.github.com>
Co-authored-by: The412Banner <205237651+The412Banner@users.noreply.github.com>
Co-authored-by: FX1922 <120828496+FX192@users.noreply.github.com>
Co-authored-by: Muaz Kadan <32595066+muazkadan@users.noreply.github.com>
Co-authored-by: kingokksa <47888451+kingokksa@users.noreply.github.com>
Co-authored-by: Jamie Schenker <jam1esch3nk3r@gmail.com>
Co-authored-by: Jagger15 <jagger2333@live.com>

* Glasses: keep a pending 2D restore and the SteamVR recovery offer, show Play SteamVR only with a picture

* Translate the glasses calibration, XREAL and SteamVR recovery strings

* SteamVR recovery: give a finished teardown time to collect logs before restarting

* Credit the projects the XR glasses support builds on

* Joy-Cons: SteamVR hands from their gyro while SteamVR runs, one combined pad otherwise

* Add experimental Luma tracking and stabilize SteamVR launches

Add native Luma Ultra spatial tracking, optical controllers, and stereo
calibration with DroidDeck styling, imperial/metric measurements, and
pending recenter recovery. Include the pinned hand model and calibration board.

Keep live guest and SDK files outside Android's evictable cache. Filter the
Adreno 840 sysmem override in the shared SteamVR helper, serialize timeline
mirroring, retain invalid optical controllers without event floods, and
prevent an unprivileged scheduling fallback from lowering compositor RTTIME.

Validation: latest APK build and runtime freshness checks pass; 247 affected
glasses/calibration tests pass. Native VR, helper, and packaging regressions
pass. Two earlier phone library launches sustained 60 fps for 308 and 188
seconds without recovery or restart. The newer calibration APK is installed
and checksum-verified; calibrated-hand and sustained gameplay acceptance
remain pending. One existing Android test requires Windows symlink privileges.

* Headsets: settings badge on Play SteamVR instead of a top-right button, both shown only once connected

* Hand tracking: camera-tracked Joy-Cons and bare hands in SteamVR, drawn as white gloves

- Joy-Con movement from the accelerometer (report reordering, per-Joy-Con calibration, zero-velocity updates)
- MediaPipe pose and hand landmarks from the device camera, placement check, per-hand Kalman filter with the Joy-Con
- No controller connected: bare camera hands with Quest-style gestures, 1€ filtered, rendered as a white glove model built from a distance field
- SteamVR safe mode cleared before each start so one crash no longer blocks the driver

* Improve Luma Ultra tracking and isolate device-specific controls

Refine stereo hand and Joy-Con tracking, gesture release, calibration,
recovery UI, diagnostics, display modes, and compositor handling.
Keep Ultra controls and input routing behind a live SDK-confirmed Carina
connection; clear retained spatial mode when other glasses connect.
Add optional UVC transport with pinned dependencies and packaged notices.

Validation: 456 Android tests, ARM64 VR build and native regressions,
47 tooling tests, SDK/UVC-enabled and disabled builds, and 150 packaged
runtime checksums. Two shader compilation tests skipped because the
compiler is unavailable. Hardware interaction gates remain experimental.

* Delay Luma Ultra 6DoF until SteamVR settles

Suspend spatial SDK startup while SteamVR launches, then restore it two seconds
after RUNNING is confirmed. Preserve saved tracking preferences and automatically
recenter same-device restoration, including cancellation and early stop.

Discard superseded launch requests and status replies after session teardown.

Validation: 128 focused JVM tests passed on the isolated commit snapshot; 129
passed with the current local hand-tracking work. Debug build and runtime checks
passed, and the reviewed APK was installed and hash-verified on the phone.

* Clarify glasses and phone camera tracking options

Separate Luma Ultra camera controls from phone-camera tracking so the two
hand-tracking switches have clear sources. Label bare-hand controllers and
Joy-Con position tracking explicitly, explain physical Joy-Con inputs and
camera priority, and update the camera prompt and calibration guidance.

Use fresh phone section and toggle labels so existing translations do not
hide the distinction. Preserve tracking behavior and saved preferences.

Validation: 39 existing UI tests, debug build, and packaged runtime verification
passed. Reviewed changes and installed the APK on the connected phone.

* Fix tracking handoff and shared runtime regressions

Release spatial ownership and controller overrides when output returns to the phone. Preserve USB-loss recovery, keep Vulkan submissions asynchronous, clean persistent guest scratch on runtime removal, and clarify the 6DoF head-tracking control.

* Apply smart-glasses virtual screens across DroidDeck menus

* Finalize Luma hand baseline and document smart-glasses support

Keep raw grayscale-to-RGB Full hand inference, wrist-only Joy-Con reconstruction, bounded visual holds, and the dead-zone rollback. Retain opt-in steady aim, recenter controls, reproducible Lite models, and the JPEG-padding fix.

Remove unsuccessful benchmark-only and USB transport experiments. Document the cross-brand 3DoF matrix, Luma Ultra 6DoF, SDK requirements, and app-wide screen behavior.

* Fix executable permission for SteamVR launcher

* Address hand tracking and VR review findings

* Bound stereo recovery test image comparison memory

* Localize smart-glasses setup and tracking diagnostics

Move calibration controls, status messages, validation errors, distance labels,
and printed-board instructions into Android string resources. Add and update
machine-assisted translations in all 32 supported non-English locale catalogs,
with corrections for recentering, hand tracking, product names, and measurements.

Keep print instructions within the page using text layout that supports wrapping
and right-to-left text. Correct the Lite model toggle description to reflect its
Lite palm detector and Lite landmark model.

Add locale coverage, format-argument, line-break, and measurement checks, plus
French and Arabic calibration UI tests. Preserve tracking calculations and the
shared runtime storage behavior.

Validation: Android resource and Kotlin compilation; 3 localization checks;
68 focused tests; 1,597 JVM tests at a 512 MB heap; French and Arabic UI renders.

* Correct smart-glasses translations from PR review

Fix the 42 localization findings covering hand and head terminology, disabled states, calibration dimensions, camera visibility, capture rejection, and menu references. Correct the repeated capture-error ambiguity in related locales and clarify its English source. Keep Romanian setup instructions consistent with the existing informal voice.

Extend localization measurement validation to distance hints and retry guidance. Verify the recovery screen's palette-dependent text color with one row-sized pixel buffer and guaranteed bitmap cleanup.

Validation: Android resource and Kotlin compilation; 3 localization checks across all supported locales; 68 focused JVM tests at a 512 MB heap.

* Work around SteamVR device loss with Adreno 840 Turnip v0.2.0

* Withdraw ineffective SteamVR flushall workaround

---------

Co-authored-by: maxjivi05 <maxjividen@gmail.com>
Co-authored-by: MaxsTechReview <6025299+maxjivi05@users.noreply.github.com>
Co-authored-by: connor g <connorsspam1992@gmail.com>
Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>
Co-authored-by: Yassine Gherbi <y.gherbi.dev@gmail.com>
Co-authored-by: Resul Övür <92828257+ovurrsl@users.noreply.github.com>
Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com>
Co-authored-by: Kirk <kirtoy.junsay@gmail.com>
Co-authored-by: Kirk <12691869+KirkPatrickJunsay@users.noreply.github.com>
Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com>
Co-authored-by: angelrenefm <17342133+angelrenefm@users.noreply.github.com>
Co-authored-by: asdf17128 <asdf17128@gmail.com>
Co-authored-by: siyuan.qsy <siyuan.qsy@alibaba-inc.com>
Co-authored-by: Dantalian <wsu091227@qq.com>
Co-authored-by: Dantalian <Dantalian-dev@users.noreply.github.com>
Co-authored-by: The412Banner <the412banner@users.noreply.github.com>
Co-authored-by: The412Banner <205237651+The412Banner@users.noreply.github.com>
Co-authored-by: FX1922 <120828496+FX192@users.noreply.github.com>
Co-authored-by: Muaz Kadan <32595066+muazkadan@users.noreply.github.com>
Co-authored-by: kingokksa <47888451+kingokksa@users.noreply.github.com>
Co-authored-by: Jamie Schenker <jam1esch3nk3r@gmail.com>
Co-authored-by: Jagger15 <jagger2333@live.com>
2026-10-18 08:37:09 -04:00

1481 lines
64 KiB
C

#define _GNU_SOURCE
#include <vulkan/vulkan.h>
#include <vulkan/vk_layer.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#define DROIDDECK_PRODUCER_SERVER
#include "producer_lease.h"
#ifdef DROIDDECK_WSI_CAPTURE
#include "wsi_capture.h"
#endif
#define LAYER_NAME "VK_LAYER_DROIDDECK_vr"
#define EXPORT __attribute__((visibility("default")))
static void say(const char *fmt, const char *arg) {
if (getenv("DROIDDECK_VR_LAYER_QUIET")) return;
fprintf(stderr, "[droiddeck-vr-layer] ");
fprintf(stderr, fmt, arg);
fputc('\n', stderr);
}
#include <dlfcn.h>
#include <execinfo.h>
#include <signal.h>
#include <ucontext.h>
static void crash_handler(int sig, siginfo_t *si, void *uc_) {
ucontext_t *uc = uc_;
#if defined(__aarch64__)
void *program_counter = (void *)uc->uc_mcontext.pc;
#elif defined(__x86_64__)
void *program_counter = (void *)uc->uc_mcontext.gregs[REG_RIP];
#else
void *program_counter = NULL;
#endif
char buf[512];
int n = snprintf(buf, sizeof(buf), "\n[crashtrace] %s: signal %d addr %p pc %p\n", program_invocation_short_name, sig,
si->si_addr, program_counter);
if (write(2, buf, n)) {}
void *frames[48];
int count = backtrace(frames, 48);
if (count > 1) frames[1] = program_counter;
for (int i = 1; i < count; i++) {
Dl_info info;
if (dladdr(frames[i], &info) && info.dli_fname)
n = snprintf(buf, sizeof(buf), "[crashtrace] #%d %s+0x%lx (%s)\n", i, info.dli_fname,
(unsigned long)((char *)frames[i] - (char *)info.dli_fbase), info.dli_sname ? info.dli_sname : "?");
else
n = snprintf(buf, sizeof(buf), "[crashtrace] #%d %p\n", i, frames[i]);
if (write(2, buf, n)) {}
}
signal(sig, SIG_DFL);
raise(sig);
}
static void crash_trace_install(void) {
static int done;
if (done++) return;
if (!getenv("DROIDDECK_VR_CRASHTRACE")) {
const char *rt = getenv("XDG_RUNTIME_DIR");
char path[512];
snprintf(path, sizeof(path), "%s/droiddeck-steamvr/crashtrace", rt && *rt ? rt : "/run/droiddeck");
if (access(path, F_OK) != 0) return;
}
static char stack[65536];
stack_t old_ss, ss = {.ss_sp = stack, .ss_size = sizeof(stack)};
if (sigaltstack(NULL, &old_ss) == 0 && (old_ss.ss_flags & SS_DISABLE)) sigaltstack(&ss, NULL);
struct sigaction sa = {0};
sa.sa_sigaction = crash_handler;
sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
int sigs[] = {SIGSEGV, SIGABRT, SIGBUS, SIGILL, SIGFPE};
for (unsigned i = 0; i < sizeof(sigs) / sizeof(*sigs); i++) {
struct sigaction old;
if (sigaction(sigs[i], NULL, &old) == 0 && !(old.sa_flags & SA_SIGINFO) && old.sa_handler == SIG_DFL)
sigaction(sigs[i], &sa, NULL);
}
}
struct inst {
VkInstance handle;
PFN_vkGetInstanceProcAddr gipa;
PFN_vkDestroyInstance destroy;
int stripped;
int active;
int producer;
};
struct dev {
VkDevice handle;
PFN_vkGetDeviceProcAddr gdpa;
PFN_vkDestroyDevice destroy;
int active;
int display;
int producer;
int quest_images;
};
static int steamvr_running(void) {
const char *layers = getenv("VK_INSTANCE_LAYERS");
if (layers && strstr(layers, "VK_LAYER_DROIDDECK_vr")) return 1;
if (getenv("DROIDDECK_VR_LAYER_FORCE")) return 1;
const char *rt = getenv("XDG_RUNTIME_DIR");
char path[512];
snprintf(path, sizeof(path), "%s/droiddeck-steamvr/active", rt && *rt ? rt : "/run/droiddeck");
return access(path, F_OK) == 0;
}
static int bridge_wanted(void) {
if (steamvr_running()) return 1;
if (strcmp(program_invocation_short_name, "steamwebhelper")) return 0;
const char *steamvr = getenv("STEAMVR");
char path[512];
snprintf(path, sizeof(path), "%s/bin/linuxarm64/vrstartup", steamvr && *steamvr ? steamvr : "/opt/steamvr");
return access(path, F_OK) == 0;
}
#define MAX_OBJS 64
static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
static struct inst g_inst[MAX_OBJS];
static struct dev g_dev[MAX_OBJS];
static void *key(const void *obj) { return *(void **)obj; }
static struct inst *inst_for(const void *obj) {
struct inst *r = NULL;
pthread_mutex_lock(&g_lock);
for (int i = 0; i < MAX_OBJS; i++)
if (g_inst[i].handle && key(g_inst[i].handle) == key(obj)) { r = &g_inst[i]; break; }
pthread_mutex_unlock(&g_lock);
return r;
}
static struct dev *dev_for(const void *obj) {
struct dev *r = NULL;
pthread_mutex_lock(&g_lock);
for (int i = 0; i < MAX_OBJS; i++)
if (g_dev[i].handle && key(g_dev[i].handle) == key(obj)) { r = &g_dev[i]; break; }
pthread_mutex_unlock(&g_lock);
return r;
}
#define NEXT(dev, fn) ((PFN_vk##fn)dev_for(dev)->gdpa(dev, "vk" #fn))
#define HOOK_AS(str, fn) if (!strcmp(name, str)) return (PFN_vkVoidFunction)fn;
static const char *const DISPLAY_EXTS[] = {
"VK_KHR_display", "VK_EXT_direct_mode_display", "VK_EXT_display_surface_counter",
"VK_KHR_get_display_properties2", "VK_EXT_acquire_xlib_display", "VK_EXT_acquire_drm_display",
};
static int is_display_ext(const char *name) {
for (size_t i = 0; i < sizeof(DISPLAY_EXTS) / sizeof(*DISPLAY_EXTS); i++)
if (!strcmp(name, DISPLAY_EXTS[i])) return 1;
return 0;
}
static VkResult VKAPI_CALL CreateInstance(const VkInstanceCreateInfo *ci, const VkAllocationCallbacks *alloc,
VkInstance *out) {
crash_trace_install();
VkLayerInstanceCreateInfo *chain = (VkLayerInstanceCreateInfo *)ci->pNext;
while (chain && !(chain->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO && chain->function == VK_LAYER_LINK_INFO))
chain = (VkLayerInstanceCreateInfo *)chain->pNext;
if (!chain) return VK_ERROR_INITIALIZATION_FAILED;
PFN_vkGetInstanceProcAddr next_gipa = chain->u.pLayerInfo->pfnNextGetInstanceProcAddr;
chain->u.pLayerInfo = chain->u.pLayerInfo->pNext;
PFN_vkCreateInstance next_create = (PFN_vkCreateInstance)next_gipa(VK_NULL_HANDLE, "vkCreateInstance");
if (!next_create) return VK_ERROR_INITIALIZATION_FAILED;
const int vr = steamvr_running();
VkInstanceCreateInfo mine = *ci;
const char **names = calloc(ci->enabledExtensionCount + 1, sizeof(*names));
uint32_t n = 0;
int stripped = 0;
for (uint32_t i = 0; i < ci->enabledExtensionCount; i++) {
if (vr && is_display_ext(ci->ppEnabledExtensionNames[i])) { say("taking %s off the instance", ci->ppEnabledExtensionNames[i]); stripped = 1; continue; }
names[n++] = ci->ppEnabledExtensionNames[i];
}
if (stripped) {
int have = 0;
for (uint32_t i = 0; i < n; i++) have |= !strcmp(names[i], "VK_EXT_headless_surface");
const char *more[] = {"VK_EXT_headless_surface", "VK_KHR_surface"};
names = realloc(names, (n + 3) * sizeof(*names));
if (!have) names[n++] = more[0];
int surf = 0;
for (uint32_t i = 0; i < n; i++) surf |= !strcmp(names[i], "VK_KHR_surface");
if (!surf) names[n++] = more[1];
}
mine.enabledExtensionCount = n;
mine.ppEnabledExtensionNames = names;
VkResult r = next_create(&mine, alloc, out);
free(names);
if (r != VK_SUCCESS) return r;
const int active = vr || bridge_wanted();
pthread_mutex_lock(&g_lock);
for (int i = 0; i < MAX_OBJS; i++) {
if (!g_inst[i].handle) {
const char *lease_mode = getenv("DROIDDECK_VR_PRODUCER_LEASE");
int producer = lease_mode && !strcmp(lease_mode, "1") && !strcmp(program_invocation_short_name, "vrcompositor") &&
(!ci->pApplicationInfo || !ci->pApplicationInfo->pApplicationName ||
strcmp(ci->pApplicationInfo->pApplicationName, "droiddeck-vr-presenter"));
g_inst[i] = (struct inst){*out, next_gipa,
(PFN_vkDestroyInstance)next_gipa(*out, "vkDestroyInstance"), stripped, active, producer};
break;
}
}
pthread_mutex_unlock(&g_lock);
return VK_SUCCESS;
}
static void VKAPI_CALL DestroyInstance(VkInstance instance, const VkAllocationCallbacks *alloc) {
struct inst *in = inst_for(instance);
if (!in) return;
PFN_vkDestroyInstance d = in->destroy;
pthread_mutex_lock(&g_lock);
memset(in, 0, sizeof(*in));
pthread_mutex_unlock(&g_lock);
if (d) d(instance, alloc);
}
static VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice pd, const VkDeviceCreateInfo *ci,
const VkAllocationCallbacks *alloc, VkDevice *out) {
VkLayerDeviceCreateInfo *chain = (VkLayerDeviceCreateInfo *)ci->pNext;
while (chain && !(chain->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO && chain->function == VK_LAYER_LINK_INFO))
chain = (VkLayerDeviceCreateInfo *)chain->pNext;
if (!chain) return VK_ERROR_INITIALIZATION_FAILED;
PFN_vkGetInstanceProcAddr next_gipa = chain->u.pLayerInfo->pfnNextGetInstanceProcAddr;
PFN_vkGetDeviceProcAddr next_gdpa = chain->u.pLayerInfo->pfnNextGetDeviceProcAddr;
chain->u.pLayerInfo = chain->u.pLayerInfo->pNext;
struct inst *in = inst_for(pd);
PFN_vkCreateDevice next_create = (PFN_vkCreateDevice)next_gipa(in ? in->handle : VK_NULL_HANDLE, "vkCreateDevice");
if (!next_create) return VK_ERROR_INITIALIZATION_FAILED;
const int active = in && in->active;
const int display = in && in->stripped;
VkDeviceCreateInfo mine = *ci;
const char **names = calloc(ci->enabledExtensionCount + 4, sizeof(*names));
if (!names) return VK_ERROR_OUT_OF_HOST_MEMORY;
uint32_t n = 0;
for (uint32_t i = 0; i < ci->enabledExtensionCount; i++) {
if (display && !strcmp(ci->ppEnabledExtensionNames[i], "VK_EXT_display_control")) { say("taking %s off the device", ci->ppEnabledExtensionNames[i]); continue; }
names[n++] = ci->ppEnabledExtensionNames[i];
}
#ifdef DROIDDECK_WSI_CAPTURE
const char *present_mode = getenv("DROIDDECK_VR_PRESENT");
if (present_mode && !strcmp(present_mode, "questwsi") && !strcmp(program_invocation_short_name, "vrcompositor")) {
PFN_vkEnumerateDeviceExtensionProperties enumerate = (PFN_vkEnumerateDeviceExtensionProperties)
next_gipa(in ? in->handle : VK_NULL_HANDLE, "vkEnumerateDeviceExtensionProperties");
uint32_t count = 0;
if (enumerate && enumerate(pd, NULL, &count, NULL) == VK_SUCCESS && count) {
VkExtensionProperties *supported = calloc(count, sizeof(*supported));
if (supported && enumerate(pd, NULL, &count, supported) == VK_SUCCESS) {
const char *required[] = {"VK_KHR_external_memory_fd", "VK_EXT_external_memory_dma_buf", "VK_EXT_queue_family_foreign"};
for (unsigned j = 0; j < 3; j++) {
int enabled = 0, available = 0;
for (uint32_t k = 0; k < n; k++) if (!strcmp(names[k], required[j])) enabled = 1;
for (uint32_t k = 0; k < count; k++) if (!strcmp(supported[k].extensionName, required[j])) available = 1;
if (!enabled && available) names[n++] = required[j];
}
}
free(supported);
}
}
#endif
mine.enabledExtensionCount = n;
mine.ppEnabledExtensionNames = names;
VkResult r = next_create(pd, &mine, alloc, out);
if (r != VK_SUCCESS) { free(names); return r; }
pthread_mutex_lock(&g_lock);
for (int i = 0; i < MAX_OBJS; i++) {
if (!g_dev[i].handle) {
g_dev[i] = (struct dev){*out, next_gdpa, (PFN_vkDestroyDevice)next_gdpa(*out, "vkDestroyDevice"), active, display,
in && in->producer, active && access("/run/droiddeck/droiddeck-xr-frames", F_OK) == 0};
break;
}
}
pthread_mutex_unlock(&g_lock);
#ifdef DROIDDECK_WSI_CAPTURE
if (in) droiddeck_wsi_device(*out, pd, in->handle, in->gipa, next_gdpa, &mine);
#endif
free(names);
return VK_SUCCESS;
}
static void forget_device_semaphores(VkDevice dev);
static void track_forget_device(VkDevice dev);
static void producer_destroy_device(struct dev *device, const VkAllocationCallbacks *allocator);
static void VKAPI_CALL DestroyDevice(VkDevice device, const VkAllocationCallbacks *alloc) {
struct dev *d = dev_for(device);
if (!d) return;
#ifdef DROIDDECK_WSI_CAPTURE
droiddeck_wsi_destroy(device);
#endif
if (d->producer) producer_destroy_device(d, alloc);
forget_device_semaphores(device);
track_forget_device(device);
PFN_vkDestroyDevice f = d->destroy;
pthread_mutex_lock(&g_lock);
memset(d, 0, sizeof(*d));
pthread_mutex_unlock(&g_lock);
if (f) f(device, alloc);
}
#include <fcntl.h>
#include <stdatomic.h>
#include <time.h>
#include "../../../app/src/main/cpp/waylandcomp/src/headpose_shm.h"
#define FAKE_DISPLAY ((VkDisplayKHR)(uintptr_t)0xdd0001)
#define FAKE_MODE ((VkDisplayModeKHR)(uintptr_t)0xdd0002)
static VkSurfaceKHR g_hmd_surface;
static uint32_t g_mode_w = 1920, g_mode_h = 1080, g_mode_mhz = 60000;
static int g_mode_fixed;
static void mode_init(void) {
static int done;
if (done++) return;
const char *m = getenv("DROIDDECK_VR_DISPLAY_MODE");
unsigned w, h, hz;
if (m && sscanf(m, "%ux%u@%u", &w, &h, &hz) == 3 && w && h && hz) { g_mode_w = w; g_mode_h = h; g_mode_mhz = hz * 1000; g_mode_fixed = 1; }
}
static uint64_t now_ns(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
}
static _Atomic(const struct droiddeck_headpose *) g_pose;
static _Atomic uint64_t g_pose_tried;
static const struct droiddeck_headpose *pose_map(void) {
const struct droiddeck_headpose *p = atomic_load(&g_pose);
if (p) return p;
uint64_t t = now_ns(), last = atomic_load(&g_pose_tried);
if (last && t - last < 1000000000ull) return NULL;
atomic_store(&g_pose_tried, t);
int fd = open("/run/droiddeck/" DROIDDECK_HEADPOSE_FILE, O_RDONLY | O_CLOEXEC);
if (fd < 0) return NULL;
void *m = mmap(NULL, sizeof(struct droiddeck_headpose), PROT_READ, MAP_SHARED, fd, 0);
close(fd);
if (m == MAP_FAILED) return NULL;
const struct droiddeck_headpose *expected = NULL;
if (!atomic_compare_exchange_strong(&g_pose, &expected, (const struct droiddeck_headpose *)m)) {
munmap(m, sizeof(struct droiddeck_headpose));
return expected;
}
return m;
}
static uint32_t mode_mhz(void) {
mode_init();
if (g_mode_fixed) return g_mode_mhz;
const struct droiddeck_headpose *p = pose_map();
if (!p || __atomic_load_n(&p->magic, __ATOMIC_RELAXED) != DROIDDECK_HEADPOSE_MAGIC) return g_mode_mhz;
uint32_t bits = __atomic_load_n((const uint32_t *)&p->display_hz, __ATOMIC_RELAXED);
float hz;
memcpy(&hz, &bits, sizeof(hz));
return hz >= 24.0f && hz <= 240.0f ? (uint32_t)(hz * 1000.0f + 0.5f) : g_mode_mhz;
}
static uint64_t frame_ns(void) { return 1000000000000ull / mode_mhz(); }
static pthread_mutex_t g_vblank_lock = PTHREAD_MUTEX_INITIALIZER;
static uint64_t g_vblank_base, g_vblank_at, g_vblank_frame;
static uint64_t vblank_count(void) {
uint64_t f = frame_ns();
pthread_mutex_lock(&g_vblank_lock);
uint64_t t = now_ns();
if (f != g_vblank_frame) {
g_vblank_base = g_vblank_frame ? g_vblank_base + (t - g_vblank_at) / g_vblank_frame : t / f;
g_vblank_at = t;
g_vblank_frame = f;
}
uint64_t n = g_vblank_base + (t - g_vblank_at) / f;
pthread_mutex_unlock(&g_vblank_lock);
return n;
}
static VkDisplayPropertiesKHR display_props(void) {
mode_init();
VkDisplayPropertiesKHR p = {0};
p.display = FAKE_DISPLAY;
p.displayName = "DroidDeck Cardboard";
p.physicalDimensions = (VkExtent2D){120, 68};
p.physicalResolution = (VkExtent2D){g_mode_w, g_mode_h};
p.supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
p.planeReorderPossible = VK_FALSE;
p.persistentContent = VK_FALSE;
return p;
}
#define ONE(count, out, value) do { \
if (!(out)) { *(count) = 1; return VK_SUCCESS; } \
if (*(count) < 1) return VK_INCOMPLETE; \
(out)[0] = (value); *(count) = 1; return VK_SUCCESS; } while (0)
static VkResult VKAPI_CALL GetPhysicalDeviceDisplayPropertiesKHR(VkPhysicalDevice pd, uint32_t *count, VkDisplayPropertiesKHR *p) {
ONE(count, p, display_props());
}
static VkResult VKAPI_CALL GetPhysicalDeviceDisplayProperties2KHR(VkPhysicalDevice pd, uint32_t *count, VkDisplayProperties2KHR *p) {
if (!p) { *count = 1; return VK_SUCCESS; }
if (*count < 1) return VK_INCOMPLETE;
p[0].displayProperties = display_props();
*count = 1;
return VK_SUCCESS;
}
static VkDisplayPlanePropertiesKHR plane_props(void) {
VkDisplayPlanePropertiesKHR p = {FAKE_DISPLAY, 0};
return p;
}
static VkResult VKAPI_CALL GetPhysicalDeviceDisplayPlanePropertiesKHR(VkPhysicalDevice pd, uint32_t *count, VkDisplayPlanePropertiesKHR *p) {
ONE(count, p, plane_props());
}
static VkResult VKAPI_CALL GetPhysicalDeviceDisplayPlaneProperties2KHR(VkPhysicalDevice pd, uint32_t *count, VkDisplayPlaneProperties2KHR *p) {
if (!p) { *count = 1; return VK_SUCCESS; }
if (*count < 1) return VK_INCOMPLETE;
p[0].displayPlaneProperties = plane_props();
*count = 1;
return VK_SUCCESS;
}
static VkResult VKAPI_CALL GetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice pd, uint32_t plane, uint32_t *count, VkDisplayKHR *d) {
if (plane != 0) { *count = 0; return VK_SUCCESS; }
ONE(count, d, FAKE_DISPLAY);
}
static VkDisplayModePropertiesKHR mode_props(void) {
mode_init();
VkDisplayModePropertiesKHR m = {FAKE_MODE, {{g_mode_w, g_mode_h}, mode_mhz()}};
return m;
}
static VkResult VKAPI_CALL GetDisplayModePropertiesKHR(VkPhysicalDevice pd, VkDisplayKHR d, uint32_t *count, VkDisplayModePropertiesKHR *p) {
ONE(count, p, mode_props());
}
static VkResult VKAPI_CALL GetDisplayModeProperties2KHR(VkPhysicalDevice pd, VkDisplayKHR d, uint32_t *count, VkDisplayModeProperties2KHR *p) {
if (!p) { *count = 1; return VK_SUCCESS; }
if (*count < 1) return VK_INCOMPLETE;
p[0].displayModeProperties = mode_props();
*count = 1;
return VK_SUCCESS;
}
static VkResult VKAPI_CALL CreateDisplayModeKHR(VkPhysicalDevice pd, VkDisplayKHR d, const VkDisplayModeCreateInfoKHR *ci,
const VkAllocationCallbacks *a, VkDisplayModeKHR *m) {
*m = FAKE_MODE;
return VK_SUCCESS;
}
static VkResult VKAPI_CALL GetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice pd, VkDisplayModeKHR m, uint32_t plane, VkDisplayPlaneCapabilitiesKHR *c) {
mode_init();
memset(c, 0, sizeof(*c));
c->supportedAlpha = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
c->maxSrcExtent = c->maxDstExtent = (VkExtent2D){g_mode_w, g_mode_h};
c->minSrcExtent = c->minDstExtent = (VkExtent2D){1, 1};
c->maxSrcPosition = c->maxDstPosition = (VkOffset2D){0, 0};
return VK_SUCCESS;
}
static VkResult VKAPI_CALL GetDisplayPlaneCapabilities2KHR(VkPhysicalDevice pd, const VkDisplayPlaneInfo2KHR *info, VkDisplayPlaneCapabilities2KHR *c) {
return GetDisplayPlaneCapabilitiesKHR(pd, info->mode, info->planeIndex, &c->capabilities);
}
static VkResult VKAPI_CALL CreateDisplayPlaneSurfaceKHR(VkInstance i, const VkDisplaySurfaceCreateInfoKHR *ci,
const VkAllocationCallbacks *a, VkSurfaceKHR *s) {
struct inst *in = inst_for(i);
PFN_vkCreateHeadlessSurfaceEXT headless = in ? (PFN_vkCreateHeadlessSurfaceEXT)in->gipa(i, "vkCreateHeadlessSurfaceEXT") : NULL;
if (!headless) { say("%s: no headless surface to stand in", "vkCreateDisplayPlaneSurfaceKHR"); return VK_ERROR_INITIALIZATION_FAILED; }
VkHeadlessSurfaceCreateInfoEXT hci = {VK_STRUCTURE_TYPE_HEADLESS_SURFACE_CREATE_INFO_EXT, NULL, 0};
VkResult r = headless(i, &hci, a, s);
if (r == VK_SUCCESS) g_hmd_surface = *s;
say("the display's surface is a headless one (%s)", r == VK_SUCCESS ? "ok" : "failed");
return r;
}
static VkResult VKAPI_CALL GetPhysicalDeviceSurfaceCapabilities2EXT(VkPhysicalDevice pd, VkSurfaceKHR surface, VkSurfaceCapabilities2EXT *c) {
struct inst *in = inst_for(pd);
PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR caps =
in ? (PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR)in->gipa(in->handle, "vkGetPhysicalDeviceSurfaceCapabilitiesKHR") : NULL;
if (!caps) return VK_ERROR_INITIALIZATION_FAILED;
VkSurfaceCapabilitiesKHR k;
VkResult r = caps(pd, surface, &k);
if (r != VK_SUCCESS) return r;
mode_init();
if (k.currentExtent.width == UINT32_MAX) k.currentExtent = (VkExtent2D){g_mode_w, g_mode_h};
c->minImageCount = k.minImageCount; c->maxImageCount = k.maxImageCount;
c->currentExtent = k.currentExtent; c->minImageExtent = k.minImageExtent; c->maxImageExtent = k.maxImageExtent;
c->maxImageArrayLayers = k.maxImageArrayLayers; c->supportedTransforms = k.supportedTransforms;
c->currentTransform = k.currentTransform; c->supportedCompositeAlpha = k.supportedCompositeAlpha;
c->supportedUsageFlags = k.supportedUsageFlags;
c->supportedSurfaceCounters = VK_SURFACE_COUNTER_VBLANK_BIT_EXT;
return VK_SUCCESS;
}
static VkResult VKAPI_CALL ReleaseDisplayEXT(VkPhysicalDevice pd, VkDisplayKHR d) { return VK_SUCCESS; }
static VkResult VKAPI_CALL AcquireDrmDisplayEXT(VkPhysicalDevice pd, int32_t fd, VkDisplayKHR d) { return VK_SUCCESS; }
static VkResult VKAPI_CALL GetDrmDisplayEXT(VkPhysicalDevice pd, int32_t fd, uint32_t conn, VkDisplayKHR *d) {
*d = FAKE_DISPLAY; return VK_SUCCESS;
}
static VkResult VKAPI_CALL AcquireXlibDisplayEXT(VkPhysicalDevice pd, void *dpy, VkDisplayKHR d) { return VK_SUCCESS; }
static VkResult VKAPI_CALL GetRandROutputDisplayEXT(VkPhysicalDevice pd, void *dpy, unsigned long out, VkDisplayKHR *d) {
*d = FAKE_DISPLAY; return VK_SUCCESS;
}
static VkResult VKAPI_CALL DisplayPowerControlEXT(VkDevice d, VkDisplayKHR disp, const VkDisplayPowerInfoEXT *info) { return VK_SUCCESS; }
static VkResult signalled_fence(VkDevice d, const VkAllocationCallbacks *a, VkFence *fence) {
VkFenceCreateInfo fci = {VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, NULL, VK_FENCE_CREATE_SIGNALED_BIT};
return NEXT(d, CreateFence)(d, &fci, a, fence);
}
static VkResult VKAPI_CALL RegisterDeviceEventEXT(VkDevice d, const VkDeviceEventInfoEXT *info, const VkAllocationCallbacks *a, VkFence *fence) {
return signalled_fence(d, a, fence);
}
static VkResult VKAPI_CALL RegisterDisplayEventEXT(VkDevice d, VkDisplayKHR disp, const VkDisplayEventInfoEXT *info,
const VkAllocationCallbacks *a, VkFence *fence) {
mode_init();
uint64_t f = frame_ns(), t = now_ns();
uint64_t next = (t / f + 1) * f;
struct timespec ts = {(time_t)(next / 1000000000ull), (long)(next % 1000000000ull)};
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL);
return signalled_fence(d, a, fence);
}
static VkResult VKAPI_CALL GetSwapchainCounterEXT(VkDevice d, VkSwapchainKHR sc, VkSurfaceCounterFlagBitsEXT counter, uint64_t *value) {
mode_init();
*value = vblank_count();
return VK_SUCCESS;
}
static VkResult VKAPI_CALL CreateSwapchainKHR(VkDevice d, const VkSwapchainCreateInfoKHR *ci, const VkAllocationCallbacks *a,
VkSwapchainKHR *out) {
PFN_vkCreateSwapchainKHR next = NEXT(d, CreateSwapchainKHR);
VkSwapchainCreateInfoKHR sized = *ci;
#ifdef DROIDDECK_WSI_CAPTURE
const char *capture_mode = getenv("DROIDDECK_VR_PRESENT");
const int capture = capture_mode && !strcmp(capture_mode, "questwsi")
&& !strcmp(program_invocation_short_name, "vrcompositor") && ci->surface == g_hmd_surface;
if (capture)
sized.imageUsage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
#endif
mode_init();
if (sized.imageExtent.width == UINT32_MAX || sized.imageExtent.height == UINT32_MAX)
sized.imageExtent = (VkExtent2D){g_mode_w, g_mode_h};
ci = &sized;
VkResult r = next(d, ci, a, out);
if (r == VK_SUCCESS) {
#ifdef DROIDDECK_WSI_CAPTURE
if (capture) droiddeck_wsi_swapchain(d, *out, ci);
#endif
return r;
}
char line[256];
int n = snprintf(line, sizeof(line), "format %d space %d mode %d images %u %ux%u usage 0x%x chain:", ci->imageFormat,
ci->imageColorSpace, ci->presentMode, ci->minImageCount, ci->imageExtent.width, ci->imageExtent.height,
ci->imageUsage);
for (const VkBaseInStructure *p = ci->pNext; p && n < (int)sizeof(line) - 16; p = p->pNext) n += snprintf(line + n, sizeof(line) - n, " %d", p->sType);
say("vkCreateSwapchainKHR refused (%s); retrying for the headless surface", line);
VkSwapchainCreateInfoKHR mine = *ci;
mine.pNext = NULL;
mine.presentMode = VK_PRESENT_MODE_FIFO_KHR;
mine.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
if (mine.minImageCount < 2) mine.minImageCount = 2;
r = next(d, &mine, a, out);
if (r != VK_SUCCESS) {
mine.imageFormat = VK_FORMAT_B8G8R8A8_UNORM;
mine.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
#ifdef DROIDDECK_WSI_CAPTURE
if (capture) mine.imageUsage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
#endif
mine.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
mine.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
r = next(d, &mine, a, out);
}
say("headless swapchain: %s", r == VK_SUCCESS ? "ok" : "still refused");
#ifdef DROIDDECK_WSI_CAPTURE
if (r == VK_SUCCESS && capture) droiddeck_wsi_swapchain(d, *out, &mine);
#endif
return r;
}
static void VKAPI_CALL DestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks *allocator) {
#ifdef DROIDDECK_WSI_CAPTURE
droiddeck_wsi_forget(device, swapchain);
#endif
NEXT(device, DestroySwapchainKHR)(device, swapchain, allocator);
}
static PFN_vkVoidFunction display_device_hook(const char *name) {
HOOK_AS("vkCreateSwapchainKHR", CreateSwapchainKHR)
HOOK_AS("vkDestroySwapchainKHR", DestroySwapchainKHR)
HOOK_AS("vkDisplayPowerControlEXT", DisplayPowerControlEXT)
HOOK_AS("vkRegisterDeviceEventEXT", RegisterDeviceEventEXT)
HOOK_AS("vkRegisterDisplayEventEXT", RegisterDisplayEventEXT)
HOOK_AS("vkGetSwapchainCounterEXT", GetSwapchainCounterEXT)
return NULL;
}
#include <sched.h>
#include <stdatomic.h>
#define MAX_SEMS 256
#define MAX_TYPED 2048
#define SHARED_NAME "droiddeck-shared-sem"
#define POLL_US 300
static pthread_mutex_t g_submission_lock = PTHREAD_MUTEX_INITIALIZER;
static unsigned g_producer_idle_calls;
struct producer_queue {
VkQueue queue;
VkDevice device;
VkFence fence;
};
static struct producer_queue g_producer_queues[MAX_OBJS];
static int g_producer_overflow;
static int g_producer_started;
static int64_t g_producer_retry_at;
static struct droiddeck_producer_server g_producer_server;
__attribute__((destructor)) static void producer_process_exit(void) {
if (g_producer_started && g_producer_server.path[0]) unlink(g_producer_server.path);
}
static int producer_acquire(int64_t deadline) {
while (pthread_mutex_trylock(&g_submission_lock) != 0) {
if (droiddeck_producer_now() >= deadline) return 0;
usleep(100);
}
int queues = 0;
if (g_producer_overflow || g_producer_idle_calls) goto fail;
for (unsigned index = 0; index < MAX_OBJS; index++) {
struct producer_queue *entry = &g_producer_queues[index];
if (!entry->queue) continue;
queues++;
struct dev *device = dev_for(entry->device);
if (!device) goto fail;
PFN_vkGetFenceStatus status = (PFN_vkGetFenceStatus)device->gdpa(device->handle, "vkGetFenceStatus");
PFN_vkDestroyFence destroy = (PFN_vkDestroyFence)device->gdpa(device->handle, "vkDestroyFence");
if (entry->fence) {
if (status(device->handle, entry->fence) != VK_SUCCESS) goto fail;
destroy(device->handle, entry->fence, NULL);
entry->fence = VK_NULL_HANDLE;
}
VkFenceCreateInfo create = {VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, NULL, 0};
PFN_vkCreateFence create_fence = (PFN_vkCreateFence)device->gdpa(device->handle, "vkCreateFence");
if (create_fence(device->handle, &create, NULL, &entry->fence) != VK_SUCCESS) goto fail;
PFN_vkQueueSubmit submit = (PFN_vkQueueSubmit)device->gdpa(device->handle, "vkQueueSubmit");
if (submit(entry->queue, 0, NULL, entry->fence) != VK_SUCCESS) {
destroy(device->handle, entry->fence, NULL);
entry->fence = VK_NULL_HANDLE;
goto fail;
}
}
if (!queues) goto fail;
for (unsigned index = 0; index < MAX_OBJS; index++) {
struct producer_queue *entry = &g_producer_queues[index];
if (!entry->queue) continue;
int64_t remaining = deadline - droiddeck_producer_now();
if (remaining <= 0) goto fail;
struct dev *device = dev_for(entry->device);
PFN_vkWaitForFences wait = (PFN_vkWaitForFences)device->gdpa(device->handle, "vkWaitForFences");
if (wait(device->handle, 1, &entry->fence, VK_TRUE, (uint64_t)remaining) != VK_SUCCESS) goto fail;
}
return 1;
fail:
pthread_mutex_unlock(&g_submission_lock);
return 0;
}
static void producer_release(void) {
pthread_mutex_unlock(&g_submission_lock);
}
static void producer_quarantine(void) {
say("%s", "producer lease lost before capture completion: submissions quarantined; restart SteamVR");
}
static void producer_record(struct dev *device, VkQueue queue) {
if (!g_producer_started && droiddeck_producer_now() >= g_producer_retry_at) {
g_producer_retry_at = droiddeck_producer_now() + 1000000000LL;
g_producer_server.acquire = producer_acquire;
g_producer_server.release = producer_release;
g_producer_server.quarantine = producer_quarantine;
if (droiddeck_producer_start(&g_producer_server)) {
pthread_detach(g_producer_server.thread);
g_producer_started = 1;
}
}
for (unsigned index = 0; index < MAX_OBJS; index++) {
struct producer_queue *entry = &g_producer_queues[index];
if (entry->queue == queue) return;
if (!entry->queue) {
*entry = (struct producer_queue){queue, device->handle, VK_NULL_HANDLE};
return;
}
}
g_producer_overflow = 1;
}
static void producer_destroy_device(struct dev *device, const VkAllocationCallbacks *allocator) {
(void)allocator;
pthread_mutex_lock(&g_submission_lock);
PFN_vkDestroyFence destroy = (PFN_vkDestroyFence)device->gdpa(device->handle, "vkDestroyFence");
for (unsigned index = 0; index < MAX_OBJS; index++) {
struct producer_queue *entry = &g_producer_queues[index];
if (entry->device != device->handle) continue;
if (entry->fence) destroy(device->handle, entry->fence, NULL);
memset(entry, 0, sizeof(*entry));
}
pthread_mutex_unlock(&g_submission_lock);
}
struct bridged {
VkSemaphore sem;
VkDevice dev;
_Atomic uint64_t *counter;
int fd;
int timeline;
unsigned long ino;
uint64_t submitted;
uint64_t seen, seen_us;
int imported;
};
static pthread_mutex_t g_br_lock = PTHREAD_MUTEX_INITIALIZER;
static struct bridged g_br[MAX_SEMS];
static VkSemaphore g_timeline_sems[MAX_TYPED];
static pthread_t g_poller;
static int g_poller_started;
static int timeline_known(VkSemaphore s) {
int r = 0;
pthread_mutex_lock(&g_br_lock);
for (int i = 0; i < MAX_TYPED && !r; i++) r = g_timeline_sems[i] == s;
pthread_mutex_unlock(&g_br_lock);
return r;
}
static void timeline_note(VkSemaphore s, int add) {
pthread_mutex_lock(&g_br_lock);
for (int i = 0; i < MAX_TYPED; i++) {
if (add ? !g_timeline_sems[i] : g_timeline_sems[i] == s) { g_timeline_sems[i] = add ? s : VK_NULL_HANDLE; break; }
}
pthread_mutex_unlock(&g_br_lock);
}
static struct bridged *bridged_find(VkSemaphore s) {
for (int i = 0; i < MAX_SEMS; i++) if (g_br[i].sem == s) return &g_br[i];
return NULL;
}
static void bridged_drop(struct bridged *b) {
if (b->counter) munmap((void *)b->counter, 4096);
if (b->fd >= 0) close(b->fd);
memset(b, 0, sizeof(*b));
b->fd = -1;
}
static int sem_trace_on(void) {
static int checked, on;
if (!checked++) {
const char *rt = getenv("XDG_RUNTIME_DIR");
char path[512];
snprintf(path, sizeof(path), "%s/droiddeck-steamvr/semtrace", rt && *rt ? rt : "/run/droiddeck");
on = access(path, F_OK) == 0;
}
return on;
}
static uint64_t now_us(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000000u + (uint64_t)ts.tv_nsec / 1000u;
}
static void *poller(void *arg) {
unsigned ticks = 0;
for (;;) {
const int trace = sem_trace_on() && (++ticks % (2000000 / POLL_US) == 0);
pthread_mutex_lock(&g_br_lock);
for (int i = 0; i < MAX_SEMS; i++) {
struct bridged *b = &g_br[i];
if (!b->sem || !b->timeline) continue;
struct dev *d = dev_for(b->dev);
if (!d) continue;
PFN_vkGetSemaphoreCounterValue get = (PFN_vkGetSemaphoreCounterValue)d->gdpa(b->dev, "vkGetSemaphoreCounterValue");
if (!get) get = (PFN_vkGetSemaphoreCounterValue)d->gdpa(b->dev, "vkGetSemaphoreCounterValueKHR");
PFN_vkSignalSemaphore sig = (PFN_vkSignalSemaphore)d->gdpa(b->dev, "vkSignalSemaphore");
if (!sig) sig = (PFN_vkSignalSemaphore)d->gdpa(b->dev, "vkSignalSemaphoreKHR");
uint64_t local = 0;
if (!get || get(b->dev, b->sem, &local) != VK_SUCCESS) continue;
uint64_t shared = atomic_load(b->counter);
if (shared != b->seen) { b->seen = shared; b->seen_us = now_us(); }
if (trace) {
char line[192];
snprintf(line, sizeof(line), "%s sem %p %s counter %lu local %llu shared %llu", program_invocation_short_name,
(void *)b->sem, b->imported ? "imported" : "made here", b->ino, (unsigned long long)local, (unsigned long long)shared);
say("%s", line);
}
if (local > shared) {
while (local > shared && !atomic_compare_exchange_weak(b->counter, &shared, local)) {}
} else if (shared > local && sig) {
VkSemaphoreSignalInfo si = {VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO, NULL, b->sem, shared};
sig(b->dev, &si);
}
}
pthread_mutex_unlock(&g_br_lock);
usleep(POLL_US);
}
return arg;
}
static void poller_start(void) {
if (__atomic_exchange_n(&g_poller_started, 1, __ATOMIC_ACQ_REL)) return;
if (pthread_create(&g_poller, NULL, poller, NULL) == 0) pthread_detach(g_poller);
say("%s", "shared semaphores are bridged between processes");
}
static _Atomic uint64_t *map_counter(int fd) {
void *p = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
return p == MAP_FAILED ? NULL : (_Atomic uint64_t *)p;
}
static unsigned long fd_ino(int fd) {
struct stat st;
return fstat(fd, &st) == 0 ? (unsigned long)st.st_ino : 0;
}
static int is_shared_counter(int fd) {
char path[64], target[128];
snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
ssize_t n = readlink(path, target, sizeof(target) - 1);
if (n <= 0) return 0;
target[n] = 0;
return strstr(target, SHARED_NAME) != NULL;
}
static VkResult VKAPI_CALL CreateSemaphore(VkDevice d, const VkSemaphoreCreateInfo *ci, const VkAllocationCallbacks *a, VkSemaphore *out) {
PFN_vkCreateSemaphore next = NEXT(d, CreateSemaphore);
const VkBaseInStructure *export_info = NULL;
const VkSemaphoreTypeCreateInfo *type_info = NULL;
for (const VkBaseInStructure *p = ci->pNext; p; p = p->pNext) {
if (p->sType == VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO) export_info = p;
if (p->sType == VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO) type_info = (const void *)p;
}
const int timeline = type_info && type_info->semaphoreType == VK_SEMAPHORE_TYPE_TIMELINE;
VkResult r = next(d, ci, a, out);
if (r == VK_SUCCESS) {
if (timeline) timeline_note(*out, 1);
return r;
}
if (!export_info || !timeline ||
((const VkExportSemaphoreCreateInfo *)export_info)->handleTypes != VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT) return r;
for (const VkBaseInStructure *item = ci->pNext; item; item = item->pNext)
if (item != export_info && item != (const void *)type_info) return r;
VkSemaphoreTypeCreateInfo type = {0};
VkSemaphoreCreateInfo mine = *ci;
mine.pNext = NULL;
if (type_info) {
type = *type_info;
type.pNext = NULL;
mine.pNext = &type;
}
r = next(d, &mine, a, out);
if (r != VK_SUCCESS) return r;
if (timeline) timeline_note(*out, 1);
int fd = memfd_create(SHARED_NAME, MFD_CLOEXEC);
_Atomic uint64_t *counter = NULL;
if (fd >= 0 && ftruncate(fd, 4096) == 0) counter = map_counter(fd);
if (!counter) {
if (fd >= 0) close(fd);
NEXT(d, DestroySemaphore)(d, *out, a);
timeline_note(*out, 0);
*out = VK_NULL_HANDLE;
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
atomic_store(counter, timeline ? type_info->initialValue : 0);
pthread_mutex_lock(&g_br_lock);
struct bridged *slot = bridged_find(VK_NULL_HANDLE);
if (slot) *slot = (struct bridged){*out, d, counter, fd, timeline, fd_ino(fd), 0, atomic_load(counter), now_us(), 0};
pthread_mutex_unlock(&g_br_lock);
if (!slot) {
munmap((void *)counter, 4096);
close(fd);
NEXT(d, DestroySemaphore)(d, *out, a);
timeline_note(*out, 0);
*out = VK_NULL_HANDLE;
return VK_ERROR_TOO_MANY_OBJECTS;
}
if (timeline) poller_start();
return r;
}
static void forget_semaphore(VkSemaphore s) {
pthread_mutex_lock(&g_br_lock);
struct bridged *b = bridged_find(s);
if (b) bridged_drop(b);
pthread_mutex_unlock(&g_br_lock);
timeline_note(s, 0);
}
static void forget_device_semaphores(VkDevice dev) {
pthread_mutex_lock(&g_br_lock);
for (int i = 0; i < MAX_SEMS; i++) if (g_br[i].sem && g_br[i].dev == dev) bridged_drop(&g_br[i]);
pthread_mutex_unlock(&g_br_lock);
}
static void VKAPI_CALL DestroySemaphore(VkDevice d, VkSemaphore s, const VkAllocationCallbacks *a) {
forget_semaphore(s);
NEXT(d, DestroySemaphore)(d, s, a);
}
static VkResult VKAPI_CALL GetSemaphoreFdKHR(VkDevice d, const VkSemaphoreGetFdInfoKHR *info, int *fd) {
pthread_mutex_lock(&g_br_lock);
struct bridged *b = bridged_find(info->semaphore);
int mine = b && b->fd >= 0 ? dup(b->fd) : -2;
if (b && info->handleType != VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT) {
if (mine >= 0) close(mine);
pthread_mutex_unlock(&g_br_lock);
return VK_ERROR_INVALID_EXTERNAL_HANDLE;
}
pthread_mutex_unlock(&g_br_lock);
if (mine == -2) return NEXT(d, GetSemaphoreFdKHR)(d, info, fd);
*fd = mine;
return mine >= 0 ? VK_SUCCESS : VK_ERROR_TOO_MANY_OBJECTS;
}
static VkResult VKAPI_CALL ImportSemaphoreFdKHR(VkDevice d, const VkImportSemaphoreFdInfoKHR *info) {
if (info->fd < 0 || !is_shared_counter(info->fd)) return NEXT(d, ImportSemaphoreFdKHR)(d, info);
const int timeline = timeline_known(info->semaphore);
struct stat metadata;
if (!timeline || info->flags || info->handleType != VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT ||
fstat(info->fd, &metadata) != 0 || metadata.st_size != 4096) return VK_ERROR_INVALID_EXTERNAL_HANDLE;
_Atomic uint64_t *counter = map_counter(info->fd);
if (!counter) return VK_ERROR_OUT_OF_HOST_MEMORY;
const unsigned long ino = fd_ino(info->fd);
pthread_mutex_lock(&g_br_lock);
struct bridged *b = bridged_find(info->semaphore);
if (b) bridged_drop(b);
b = bridged_find(VK_NULL_HANDLE);
if (b) *b = (struct bridged){info->semaphore, d, counter, info->fd, timeline, ino, 0, atomic_load(counter), now_us(), 1};
pthread_mutex_unlock(&g_br_lock);
if (!b) {
munmap((void *)counter, 4096);
return VK_ERROR_TOO_MANY_OBJECTS;
}
if (timeline) poller_start();
static int said;
if (said++ < 4) say("imported a shared timeline semaphore (%s): bridged", program_invocation_short_name);
return VK_SUCCESS;
}
#define SUBMIT_GRACE_US 3000
#define SHARED_IDLE_US 100000
static int quest_frames(void) {
static _Atomic uint64_t checked_us;
static _Atomic int present;
const uint64_t t = now_us();
const uint64_t last = atomic_load(&checked_us);
if (!last || t - last > 1000000) {
atomic_store(&present, access("/run/droiddeck/droiddeck-xr-frames", F_OK) == 0);
atomic_store(&checked_us, t);
}
return atomic_load(&present);
}
static int submit_drop_wait(VkSemaphore s, uint64_t value) {
pthread_mutex_lock(&g_br_lock);
struct bridged *b = bridged_find(s);
if (!b || !b->timeline || !b->imported) { pthread_mutex_unlock(&g_br_lock); return 0; }
const uint64_t queued = b->submitted;
const uint64_t seen_us = b->seen_us;
VkDevice dev = b->dev;
pthread_mutex_unlock(&g_br_lock);
if (!value || value <= queued) return 0;
struct dev *d = dev_for(dev);
if (!d || quest_frames()) return 0;
PFN_vkGetSemaphoreCounterValue get = (PFN_vkGetSemaphoreCounterValue)d->gdpa(dev, "vkGetSemaphoreCounterValue");
if (!get) get = (PFN_vkGetSemaphoreCounterValue)d->gdpa(dev, "vkGetSemaphoreCounterValueKHR");
if (!get) return 0;
const uint64_t start = now_us();
const uint64_t until = start + (start - seen_us > SHARED_IDLE_US ? 0 : SUBMIT_GRACE_US);
for (;;) {
uint64_t local = 0;
if (get(dev, s, &local) == VK_SUCCESS && local >= value) return 0;
/* Only the poller mirrors shared values. A second host signal here can
* race its signal and submit the same timeline value twice. */
if (now_us() >= until) break;
usleep(100);
}
static _Atomic int dropped;
if (atomic_fetch_add(&dropped, 1) == 0)
say("%s: dropping a submit's wait on a shared value nobody has signalled yet (Turnip would block the submit)",
program_invocation_short_name);
return 1;
}
static void note_signal(VkSemaphore s, uint64_t value) {
pthread_mutex_lock(&g_br_lock);
struct bridged *b = bridged_find(s);
if (b && b->timeline && value > b->submitted) b->submitted = value;
pthread_mutex_unlock(&g_br_lock);
}
static void note_submit_signals(uint32_t count, const VkSubmitInfo *submits) {
for (uint32_t i = 0; i < count; i++) {
const VkTimelineSemaphoreSubmitInfo *t = NULL;
for (const VkBaseInStructure *p = submits[i].pNext; p; p = p->pNext)
if (p->sType == VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO) t = (const void *)p;
for (uint32_t j = 0; t && j < submits[i].signalSemaphoreCount && j < t->signalSemaphoreValueCount; j++)
note_signal(submits[i].pSignalSemaphores[j], t->pSignalSemaphoreValues[j]);
}
}
static void note_submit2_signals(uint32_t count, const VkSubmitInfo2 *submits) {
for (uint32_t i = 0; i < count; i++)
for (uint32_t j = 0; j < submits[i].signalSemaphoreInfoCount; j++)
note_signal(submits[i].pSignalSemaphoreInfos[j].semaphore, submits[i].pSignalSemaphoreInfos[j].value);
}
static uint32_t keep_waits(const VkSemaphore *in, const VkPipelineStageFlags *stages, const uint64_t *values, uint32_t n,
VkSemaphore *out, VkPipelineStageFlags *out_stages, uint64_t *out_values) {
uint32_t k = 0;
for (uint32_t i = 0; i < n; i++) {
if (submit_drop_wait(in[i], values ? values[i] : 0)) continue;
out[k] = in[i];
if (stages) out_stages[k] = stages[i];
if (values) out_values[k] = values[i];
k++;
}
return k;
}
static void trace_sem(const char *what, VkSemaphore sem, uint64_t value) {
if (!sem_trace_on() || value > 3) return;
pthread_mutex_lock(&g_br_lock);
struct bridged *b = bridged_find(sem);
unsigned long ino = b ? b->ino : 0;
pthread_mutex_unlock(&g_br_lock);
if (!ino) return;
char line[160];
snprintf(line, sizeof(line), "%s %s %p value %llu counter %lu", program_invocation_short_name, what, (void *)sem,
(unsigned long long)value, ino);
say("%s", line);
}
#include <limits.h>
#include <linux/futex.h>
#include <sys/syscall.h>
#include "present_sync.h"
#include "fence_tracker.h"
#include <time.h>
static pthread_mutex_t g_trk_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t g_trk_cond = PTHREAD_COND_INITIALIZER;
static struct tracked *g_trk;
static struct droiddeck_present_sync *g_sync;
static pthread_once_t g_sync_once = PTHREAD_ONCE_INIT;
static void sync_wake(struct droiddeck_present_sync *s) {
syscall(SYS_futex, &s->done, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
}
static void *completer(void *arg) {
struct droiddeck_present_sync *s = arg;
pthread_mutex_lock(&g_trk_lock);
for (;;) {
uint32_t done;
int pending = track_pool_reap(g_trk, __atomic_load_n(&s->issued, __ATOMIC_ACQUIRE), &done);
if (done != __atomic_load_n(&s->done, __ATOMIC_RELAXED)) {
__atomic_store_n(&s->done, done, __ATOMIC_RELEASE);
sync_wake(s);
}
pthread_cond_broadcast(&g_trk_cond);
if (pending) {
struct timespec until;
clock_gettime(CLOCK_REALTIME, &until);
until.tv_nsec += 1000000; /* Poll pending GPU work at most every 1 ms. */
if (until.tv_nsec >= 1000000000) { until.tv_sec++; until.tv_nsec -= 1000000000; }
pthread_cond_timedwait(&g_trk_cond, &g_trk_lock, &until);
} else {
pthread_cond_wait(&g_trk_cond, &g_trk_lock);
}
}
return NULL;
}
static void sync_init(void) {
if (strcmp(program_invocation_short_name, "vrcompositor")) return;
const char *rt = getenv("XDG_RUNTIME_DIR");
char path[512];
snprintf(path, sizeof(path), "%s/droiddeck-steamvr/%s", rt && *rt ? rt : "/run/droiddeck", DROIDDECK_PRESENT_SYNC_NAME);
int fd = open(path, O_RDWR | O_CREAT | O_CLOEXEC, 0600);
if (fd < 0) return;
void *m = ftruncate(fd, DROIDDECK_PRESENT_SYNC_SIZE) == 0
? mmap(NULL, DROIDDECK_PRESENT_SYNC_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0) : MAP_FAILED;
close(fd);
if (m == MAP_FAILED) return;
struct droiddeck_present_sync *s = m;
__atomic_store_n(&s->done, __atomic_load_n(&s->issued, __ATOMIC_ACQUIRE), __ATOMIC_RELEASE);
sync_wake(s);
pthread_t t;
if (pthread_create(&t, NULL, completer, s) != 0) { munmap(m, DROIDDECK_PRESENT_SYNC_SIZE); return; }
pthread_detach(t);
g_sync = s;
say("%s", "frames go to the presenter once their GPU work is done");
}
static struct tracked *track_take(struct dev *d) {
pthread_mutex_lock(&g_trk_lock);
struct tracked *t = track_pool_take(&g_trk, d->handle, d->gdpa);
pthread_mutex_unlock(&g_trk_lock);
return t;
}
static void track_submitted(struct tracked *t, VkResult r) {
pthread_mutex_lock(&g_trk_lock);
uint32_t issued = track_pool_submitted(t, r, __atomic_load_n(&g_sync->issued, __ATOMIC_RELAXED));
__atomic_store_n(&g_sync->issued, issued, __ATOMIC_RELEASE);
pthread_cond_broadcast(&g_trk_cond);
pthread_mutex_unlock(&g_trk_lock);
}
static void track_forget_device(VkDevice dev) {
pthread_mutex_lock(&g_trk_lock);
while (track_pool_device_busy(g_trk, dev))
pthread_cond_wait(&g_trk_cond, &g_trk_lock);
track_pool_forget(&g_trk, dev);
pthread_mutex_unlock(&g_trk_lock);
}
static VkResult submit_tracked(PFN_vkQueueSubmit next, VkQueue q, uint32_t count, const VkSubmitInfo *submits, VkFence fence) {
if (strcmp(program_invocation_short_name, "vrcompositor")) return next(q, count, submits, fence);
pthread_once(&g_sync_once, sync_init);
struct dev *d = dev_for(q);
if (!g_sync || !d) return next(q, count, submits, fence);
struct tracked *slot = track_take(d);
if (!slot) return VK_ERROR_OUT_OF_HOST_MEMORY;
const VkFence mine = slot->fence;
VkResult r = next(q, count, submits, fence ? fence : mine);
if (r == VK_SUCCESS && fence) r = next(q, 0, NULL, mine);
track_submitted(slot, r);
return r;
}
static VkResult submit2_tracked(PFN_vkQueueSubmit2 next, VkQueue q, uint32_t count, const VkSubmitInfo2 *submits, VkFence fence) {
if (strcmp(program_invocation_short_name, "vrcompositor")) return next(q, count, submits, fence);
pthread_once(&g_sync_once, sync_init);
struct dev *d = dev_for(q);
if (!g_sync || !d) return next(q, count, submits, fence);
struct tracked *slot = track_take(d);
if (!slot) return VK_ERROR_OUT_OF_HOST_MEMORY;
const VkFence mine = slot->fence;
VkResult r = next(q, count, submits, fence ? fence : mine);
if (r == VK_SUCCESS && fence) r = next(q, 0, NULL, mine);
track_submitted(slot, r);
return r;
}
static VkResult VKAPI_CALL QueueSubmit(VkQueue q, uint32_t count, const VkSubmitInfo *submits, VkFence fence) {
PFN_vkQueueSubmit next = (PFN_vkQueueSubmit)dev_for(q)->gdpa(dev_for(q)->handle, "vkQueueSubmit");
if (sem_trace_on())
for (uint32_t i = 0; i < count; i++) {
const VkTimelineSemaphoreSubmitInfo *t = NULL;
for (const VkBaseInStructure *p = submits[i].pNext; p; p = p->pNext)
if (p->sType == VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO) t = (const void *)p;
for (uint32_t j = 0; j < submits[i].waitSemaphoreCount; j++)
trace_sem("waits", submits[i].pWaitSemaphores[j], t && j < t->waitSemaphoreValueCount ? t->pWaitSemaphoreValues[j] : 0);
for (uint32_t j = 0; j < submits[i].signalSemaphoreCount; j++)
trace_sem("signals", submits[i].pSignalSemaphores[j], t && j < t->signalSemaphoreValueCount ? t->pSignalSemaphoreValues[j] : 0);
}
int any = 0;
for (uint32_t i = 0; i < count && !any; i++) {
const VkTimelineSemaphoreSubmitInfo *t = NULL;
for (const VkBaseInStructure *p = submits[i].pNext; p; p = p->pNext)
if (p->sType == VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO) t = (const void *)p;
for (uint32_t j = 0; submits[i].pWaitSemaphores && j < submits[i].waitSemaphoreCount && !any; j++)
any = submit_drop_wait(submits[i].pWaitSemaphores[j], t && j < t->waitSemaphoreValueCount ? t->pWaitSemaphoreValues[j] : 0);
}
VkSubmitInfo *copy = NULL;
VkTimelineSemaphoreSubmitInfo *tl = NULL;
void **bufs = NULL;
if (any) {
copy = calloc(count, sizeof(*copy));
tl = calloc(count, sizeof(*tl));
bufs = calloc(count * 3, sizeof(void *));
if (!copy || !tl || !bufs) { free(copy); free(tl); free(bufs); return VK_ERROR_OUT_OF_HOST_MEMORY; }
for (uint32_t i = 0; i < count; i++) {
copy[i] = submits[i];
uint32_t n = submits[i].waitSemaphoreCount;
if (!n) continue;
VkSemaphore *sems = calloc(n, sizeof(*sems));
VkPipelineStageFlags *stages = calloc(n, sizeof(*stages));
uint64_t *vals = calloc(n, sizeof(*vals));
bufs[i * 3] = sems; bufs[i * 3 + 1] = stages; bufs[i * 3 + 2] = vals;
if (!sems || !stages || !vals) {
for (uint32_t k = 0; k < count * 3; k++) free(bufs[k]);
free(bufs); free(tl); free(copy);
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
const VkTimelineSemaphoreSubmitInfo *t = NULL;
for (const VkBaseInStructure *p = submits[i].pNext; p; p = p->pNext)
if (p->sType == VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO) t = (const void *)p;
uint32_t k = keep_waits(submits[i].pWaitSemaphores, submits[i].pWaitDstStageMask,
t && t->waitSemaphoreValueCount ? t->pWaitSemaphoreValues : NULL, n, sems, stages, vals);
copy[i].waitSemaphoreCount = k;
copy[i].pWaitSemaphores = sems;
copy[i].pWaitDstStageMask = stages;
if (t) {
tl[i] = *t;
tl[i].waitSemaphoreValueCount = t->waitSemaphoreValueCount ? k : 0;
tl[i].pWaitSemaphoreValues = vals;
tl[i].pNext = t->pNext;
copy[i].pNext = &tl[i];
}
}
}
struct dev *device = dev_for(q);
if (device->producer) { pthread_mutex_lock(&g_submission_lock); producer_record(device, q); }
VkResult result = submit_tracked(next, q, count, any ? copy : submits, fence);
if (device->producer) pthread_mutex_unlock(&g_submission_lock);
note_submit_signals(count, submits);
if (any) {
for (uint32_t i = 0; i < count * 3; i++) free(bufs[i]);
free(bufs); free(tl); free(copy);
}
return result;
}
static VkResult VKAPI_CALL QueueSubmit2(VkQueue q, uint32_t count, const VkSubmitInfo2 *submits, VkFence fence) {
PFN_vkQueueSubmit2 next = (PFN_vkQueueSubmit2)dev_for(q)->gdpa(dev_for(q)->handle, "vkQueueSubmit2");
if (!next) next = (PFN_vkQueueSubmit2)dev_for(q)->gdpa(dev_for(q)->handle, "vkQueueSubmit2KHR");
if (sem_trace_on())
for (uint32_t i = 0; i < count; i++) {
for (uint32_t j = 0; j < submits[i].waitSemaphoreInfoCount; j++)
trace_sem("waits", submits[i].pWaitSemaphoreInfos[j].semaphore, submits[i].pWaitSemaphoreInfos[j].value);
for (uint32_t j = 0; j < submits[i].signalSemaphoreInfoCount; j++)
trace_sem("signals", submits[i].pSignalSemaphoreInfos[j].semaphore, submits[i].pSignalSemaphoreInfos[j].value);
}
int any = 0;
for (uint32_t i = 0; i < count && !any; i++)
for (uint32_t j = 0; submits[i].pWaitSemaphoreInfos && j < submits[i].waitSemaphoreInfoCount && !any; j++)
any = submit_drop_wait(submits[i].pWaitSemaphoreInfos[j].semaphore, submits[i].pWaitSemaphoreInfos[j].value);
VkSubmitInfo2 *copy = NULL;
VkSemaphoreSubmitInfo **waits = NULL;
if (any) {
copy = calloc(count, sizeof(*copy));
waits = calloc(count, sizeof(*waits));
if (!copy || !waits) { free(copy); free(waits); return VK_ERROR_OUT_OF_HOST_MEMORY; }
for (uint32_t i = 0; i < count; i++) {
copy[i] = submits[i];
uint32_t n = submits[i].waitSemaphoreInfoCount, k = 0;
waits[i] = calloc(n + 1, sizeof(VkSemaphoreSubmitInfo));
if (!waits[i]) {
for (uint32_t j = 0; j < i; j++) free(waits[j]);
free(waits); free(copy);
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
for (uint32_t j = 0; j < n; j++)
if (!submit_drop_wait(submits[i].pWaitSemaphoreInfos[j].semaphore, submits[i].pWaitSemaphoreInfos[j].value))
waits[i][k++] = submits[i].pWaitSemaphoreInfos[j];
copy[i].waitSemaphoreInfoCount = k;
copy[i].pWaitSemaphoreInfos = waits[i];
}
}
struct dev *device = dev_for(q);
if (device->producer) { pthread_mutex_lock(&g_submission_lock); producer_record(device, q); }
VkResult result = submit2_tracked(next, q, count, any ? copy : submits, fence);
if (device->producer) pthread_mutex_unlock(&g_submission_lock);
note_submit2_signals(count, submits);
if (any) {
for (uint32_t i = 0; i < count; i++) free(waits[i]);
free(waits); free(copy);
}
return result;
}
static VkResult VKAPI_CALL QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR *info) {
struct dev *device = dev_for(queue);
PFN_vkQueuePresentKHR next = (PFN_vkQueuePresentKHR)device->gdpa(device->handle, "vkQueuePresentKHR");
if (device->producer) { pthread_mutex_lock(&g_submission_lock); producer_record(device, queue); }
#ifdef DROIDDECK_WSI_CAPTURE
VkResult result = droiddeck_wsi_present(device->handle, queue, info, next);
#else
VkResult result = next(queue, info);
#endif
if (device->producer) pthread_mutex_unlock(&g_submission_lock);
return result;
}
static VkResult VKAPI_CALL QueueBindSparse(VkQueue queue, uint32_t count, const VkBindSparseInfo *binds, VkFence fence) {
struct dev *device = dev_for(queue);
PFN_vkQueueBindSparse next = (PFN_vkQueueBindSparse)device->gdpa(device->handle, "vkQueueBindSparse");
if (device->producer) { pthread_mutex_lock(&g_submission_lock); producer_record(device, queue); }
VkResult result = next(queue, count, binds, fence);
if (device->producer) pthread_mutex_unlock(&g_submission_lock);
return result;
}
static VkResult VKAPI_CALL QueueWaitIdle(VkQueue queue) {
struct dev *device = dev_for(queue);
PFN_vkQueueWaitIdle next = (PFN_vkQueueWaitIdle)device->gdpa(device->handle, "vkQueueWaitIdle");
if (device->producer) {
pthread_mutex_lock(&g_submission_lock);
g_producer_idle_calls++;
pthread_mutex_unlock(&g_submission_lock);
}
VkResult result = next(queue);
if (device->producer) {
pthread_mutex_lock(&g_submission_lock);
g_producer_idle_calls--;
pthread_mutex_unlock(&g_submission_lock);
}
return result;
}
static VkResult VKAPI_CALL DeviceWaitIdle(VkDevice handle) {
struct dev *device = dev_for(handle);
PFN_vkDeviceWaitIdle next = (PFN_vkDeviceWaitIdle)device->gdpa(handle, "vkDeviceWaitIdle");
if (device->producer) {
pthread_mutex_lock(&g_submission_lock);
g_producer_idle_calls++;
pthread_mutex_unlock(&g_submission_lock);
}
VkResult result = next(handle);
if (device->producer) {
pthread_mutex_lock(&g_submission_lock);
g_producer_idle_calls--;
pthread_mutex_unlock(&g_submission_lock);
}
return result;
}
static VkResult VKAPI_CALL WaitSemaphores(VkDevice d, const VkSemaphoreWaitInfo *info, uint64_t timeout) {
PFN_vkWaitSemaphores next = (PFN_vkWaitSemaphores)dev_for(d)->gdpa(d, "vkWaitSemaphores");
if (!next) next = (PFN_vkWaitSemaphores)dev_for(d)->gdpa(d, "vkWaitSemaphoresKHR");
return next(d, info, timeout);
}
static VkResult VKAPI_CALL GetSemaphoreCounterValue(VkDevice d, VkSemaphore s, uint64_t *value) {
PFN_vkGetSemaphoreCounterValue next = (PFN_vkGetSemaphoreCounterValue)dev_for(d)->gdpa(d, "vkGetSemaphoreCounterValue");
if (!next) next = (PFN_vkGetSemaphoreCounterValue)dev_for(d)->gdpa(d, "vkGetSemaphoreCounterValueKHR");
return next(d, s, value);
}
static VkResult VKAPI_CALL CreateImage(VkDevice d, const VkImageCreateInfo *ci, const VkAllocationCallbacks *a, VkImage *out) {
VkImageCreateInfo compatible = *ci;
VkExternalMemoryImageCreateInfo shared;
const VkBaseInStructure *first = ci->pNext;
// SteamVR's OpenXR stereo arrays omit a format list, while DXVK adds one on import.
// Leave single-layer OpenVR images alone: the application owns their export layout.
if (dev_for(d)->quest_images && ci->arrayLayers > 1 && (ci->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) && ci->tiling == VK_IMAGE_TILING_OPTIMAL
&& first && first->sType == VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO) {
const VkBaseInStructure *second = first->pNext;
if (second && second->sType == VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO
&& ((const VkExternalMemoryImageCreateInfo *)second)->handleTypes == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT) {
compatible.pNext = second; ci = &compatible;
say("%s", "matching opaque shared image layout to SteamVR's unrestricted format list");
}
}
if (dev_for(d)->quest_images && ci->arrayLayers > 1 && (ci->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) && ci->tiling == VK_IMAGE_TILING_OPTIMAL
&& first && first->sType == VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO) {
const VkExternalMemoryImageCreateInfo *external = (const void *)first;
const VkBaseInStructure *second = external->pNext;
if (external->handleTypes == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT && second
&& second->sType == VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO) {
shared = *external; shared.pNext = second->pNext; compatible.pNext = &shared; ci = &compatible;
say("%s", "matching opaque shared image layout to SteamVR's unrestricted format list");
}
}
VkResult r = NEXT(d, CreateImage)(d, ci, a, out);
if (!sem_trace_on()) return r;
const VkExternalMemoryImageCreateInfo *ext = NULL;
const VkImageFormatListCreateInfo *list = NULL;
for (const VkBaseInStructure *p = ci->pNext; p; p = p->pNext) {
if (p->sType == VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO) ext = (const void *)p;
if (p->sType == VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO) list = (const void *)p;
}
if (!ext) return r;
char line[256];
int n = snprintf(line, sizeof(line), "%s image %ux%u format %d flags 0x%x usage 0x%x tiling %d layers %u mips %u samples %u handles 0x%x list",
program_invocation_short_name, ci->extent.width, ci->extent.height, ci->format, ci->flags, ci->usage,
ci->tiling, ci->arrayLayers, ci->mipLevels, ci->samples, ext->handleTypes);
for (uint32_t i = 0; list && i < list->viewFormatCount && n < (int)sizeof(line) - 8; i++)
n += snprintf(line + n, sizeof(line) - n, " %d", list->pViewFormats[i]);
for (const VkBaseInStructure *p = ci->pNext; p && n < (int)sizeof(line) - 16; p = p->pNext)
n += snprintf(line + n, sizeof(line) - n, " chain:%d", p->sType);
say("%s", line);
return r;
}
static PFN_vkVoidFunction semaphore_hook(const char *name) {
HOOK_AS("vkCreateImage", CreateImage)
HOOK_AS("vkCreateSemaphore", CreateSemaphore)
HOOK_AS("vkDestroySemaphore", DestroySemaphore)
HOOK_AS("vkGetSemaphoreFdKHR", GetSemaphoreFdKHR)
HOOK_AS("vkImportSemaphoreFdKHR", ImportSemaphoreFdKHR)
HOOK_AS("vkQueueSubmit", QueueSubmit)
HOOK_AS("vkQueueSubmit2", QueueSubmit2)
HOOK_AS("vkQueueSubmit2KHR", QueueSubmit2)
HOOK_AS("vkQueuePresentKHR", QueuePresentKHR)
HOOK_AS("vkQueueBindSparse", QueueBindSparse)
HOOK_AS("vkQueueWaitIdle", QueueWaitIdle)
HOOK_AS("vkDeviceWaitIdle", DeviceWaitIdle)
HOOK_AS("vkWaitSemaphores", WaitSemaphores)
HOOK_AS("vkWaitSemaphoresKHR", WaitSemaphores)
HOOK_AS("vkGetSemaphoreCounterValue", GetSemaphoreCounterValue)
HOOK_AS("vkGetSemaphoreCounterValueKHR", GetSemaphoreCounterValue)
return NULL;
}
#define HOOK(fn) if (!strcmp(name, "vk" #fn)) return (PFN_vkVoidFunction)fn;
static PFN_vkVoidFunction display_hook(const char *name) {
HOOK(GetPhysicalDeviceDisplayPropertiesKHR)
HOOK(GetPhysicalDeviceDisplayProperties2KHR)
HOOK(GetPhysicalDeviceDisplayPlanePropertiesKHR)
HOOK(GetPhysicalDeviceDisplayPlaneProperties2KHR)
HOOK(GetDisplayPlaneSupportedDisplaysKHR)
HOOK(GetDisplayModePropertiesKHR)
HOOK(GetDisplayModeProperties2KHR)
HOOK(CreateDisplayModeKHR)
HOOK(GetDisplayPlaneCapabilitiesKHR)
HOOK(GetDisplayPlaneCapabilities2KHR)
HOOK(GetPhysicalDeviceSurfaceCapabilities2EXT)
HOOK(CreateDisplayPlaneSurfaceKHR)
HOOK(ReleaseDisplayEXT)
HOOK(AcquireDrmDisplayEXT)
HOOK(GetDrmDisplayEXT)
HOOK(AcquireXlibDisplayEXT)
HOOK(GetRandROutputDisplayEXT)
return NULL;
}
EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL DroidDeckVr_GetDeviceProcAddr(VkDevice device, const char *name);
EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL DroidDeckVr_GetInstanceProcAddr(VkInstance instance, const char *name) {
HOOK(CreateInstance)
HOOK(DestroyInstance)
HOOK(CreateDevice)
HOOK(DestroyDevice)
if (!strcmp(name, "vkGetInstanceProcAddr")) return (PFN_vkVoidFunction)DroidDeckVr_GetInstanceProcAddr;
if (!strcmp(name, "vkGetDeviceProcAddr")) return (PFN_vkVoidFunction)DroidDeckVr_GetDeviceProcAddr;
if (!instance) return NULL;
struct inst *in = inst_for(instance);
if (in && !in->active) return in->gipa(instance, name);
if (in && in->stripped) {
PFN_vkVoidFunction f = display_hook(name);
if (!f) f = display_device_hook(name);
if (f) return f;
}
if (in) {
PFN_vkVoidFunction f = semaphore_hook(name);
if (f && in->gipa(instance, name)) return f;
}
return in ? in->gipa(instance, name) : NULL;
}
EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL DroidDeckVr_GetDeviceProcAddr(VkDevice device, const char *name) {
HOOK(DestroyDevice)
if (!strcmp(name, "vkGetDeviceProcAddr")) return (PFN_vkVoidFunction)DroidDeckVr_GetDeviceProcAddr;
struct dev *d = dev_for(device);
if (d && !d->active) return d->gdpa(device, name);
if (d && d->display) {
PFN_vkVoidFunction f = display_device_hook(name);
if (f) return f;
}
if (d) {
PFN_vkVoidFunction mine = semaphore_hook(name);
if (mine && d->gdpa(device, name)) return mine;
}
return d ? d->gdpa(device, name) : NULL;
}
EXPORT VKAPI_ATTR VkResult VKAPI_CALL DroidDeckVr_NegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface *v) {
if (v->loaderLayerInterfaceVersion > 2) v->loaderLayerInterfaceVersion = 2;
v->pfnGetInstanceProcAddr = DroidDeckVr_GetInstanceProcAddr;
v->pfnGetDeviceProcAddr = DroidDeckVr_GetDeviceProcAddr;
v->pfnGetPhysicalDeviceProcAddr = NULL;
return VK_SUCCESS;
}