mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 12:35:42 +00:00
Compare commits
18
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a80300c357 | ||
|
|
356bdede67 | ||
|
|
b60fac6dda | ||
|
|
023daf57d2 | ||
|
|
bb58d0cc7b | ||
|
|
9b91c02e07 | ||
|
|
35a4a0750f | ||
|
|
48300cc002 | ||
|
|
0d9e0600f4 | ||
|
|
8bf7ba7eca | ||
|
|
dbd277d5bd | ||
|
|
2ef2d4b53d | ||
|
|
040d31c1ee | ||
|
|
db0c32c72f | ||
|
|
7277386cd3 | ||
|
|
e1d096127c | ||
|
|
f0934018df | ||
|
|
07f5e5f83d |
No files matched your search
@@ -193,7 +193,7 @@ jobs:
|
||||
run: |
|
||||
set -euo pipefail
|
||||
sudo apt-get update -qq
|
||||
sudo apt-get install -y -qq gcc-aarch64-linux-gnu g++-aarch64-linux-gnu >/dev/null
|
||||
sudo apt-get install -y -qq gcc-aarch64-linux-gnu g++-aarch64-linux-gnu libvulkan1 mesa-vulkan-drivers >/dev/null
|
||||
D=app/src/main/assets/linuxfs
|
||||
mkdir -p "$D"
|
||||
aarch64-linux-gnu-g++ -shared -fPIC -O2 -Wall -Wno-attributes -Wno-nonnull-compare \
|
||||
@@ -314,8 +314,11 @@ jobs:
|
||||
docker run --rm -v "$PWD:/src" -w /src public.ecr.aws/docker/library/debian:bullseye bash -c '
|
||||
set -euo pipefail
|
||||
printf "deb http://archive.debian.org/debian bullseye main\ndeb http://archive.debian.org/debian-security bullseye-security main\n" > /etc/apt/sources.list
|
||||
apt-get -o Acquire::Check-Valid-Until=false update -qq
|
||||
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends gcc g++ gcc-multilib g++-multilib binutils >/dev/null
|
||||
apt-get -o Acquire::Retries=3 -o Acquire::Check-Valid-Until=false update -qq
|
||||
DEBIAN_FRONTEND=noninteractive apt-get -o Acquire::Retries=3 install -y -qq --no-install-recommends ca-certificates >/dev/null
|
||||
sed -i "s|http://archive.debian.org|https://archive.debian.org|g" /etc/apt/sources.list
|
||||
apt-get -o Acquire::Retries=3 -o Acquire::Check-Valid-Until=false update -qq
|
||||
DEBIAN_FRONTEND=noninteractive apt-get -o Acquire::Retries=3 install -y -qq --no-install-recommends gcc g++ gcc-multilib g++-multilib binutils >/dev/null
|
||||
tools/linuxfs/build-x86-preloads.sh app/src/main/assets/linuxfs
|
||||
'
|
||||
sudo chown -R "$(id -u):$(id -g)" app/src/main/assets/linuxfs
|
||||
@@ -396,6 +399,24 @@ jobs:
|
||||
set -euo pipefail
|
||||
bash tools/linuxfs/check-preloads.sh app/src/main/assets/linuxfs
|
||||
|
||||
- name: Check Quest tracking IPC
|
||||
run: |
|
||||
g++ -std=c++17 -Wall -Wextra -Werror -pthread tools/vr/tests/test_xr_tracking.cpp -o /tmp/test-xr-tracking
|
||||
/tmp/test-xr-tracking
|
||||
g++ -std=c++17 -Wall -Wextra -Werror tools/vr/tests/test_xr_touch.cpp -o /tmp/test-xr-touch
|
||||
/tmp/test-xr-touch
|
||||
g++ -std=c++17 -Wall -Wextra -Werror -pthread tools/vr/tests/test_xr_vsync.cpp -o /tmp/test-xr-vsync
|
||||
/tmp/test-xr-vsync
|
||||
g++ -std=c++17 -Wall -Wextra -Werror -pthread tools/vr/tests/test_xr_frames.cpp -o /tmp/test-xr-frames
|
||||
/tmp/test-xr-frames
|
||||
cc -Itools/vr/third_party/wayland -c tools/vr/src/wayland_client_stub.c -o /tmp/test-wayland-stub.o
|
||||
cc -Itools/vr/third_party/wayland -c app/src/main/cpp/waylandcomp/generated/xdg-shell-protocol.c -o /tmp/test-xdg-shell.o
|
||||
g++ -std=c++17 -Wall -Wextra -Werror -Wno-unused-parameter -Wno-missing-field-initializers -pthread -Ibuild/vr/include \
|
||||
-Itools/vr/third_party/wayland -Iapp/src/main/cpp/waylandcomp/generated \
|
||||
tools/vr/tests/test_presenter_shutdown.cpp tools/vr/src/presenter.cpp /tmp/test-wayland-stub.o /tmp/test-xdg-shell.o \
|
||||
-ldl -o /tmp/test-presenter-shutdown
|
||||
timeout 10 /tmp/test-presenter-shutdown
|
||||
|
||||
- name: Rust toolchain for the store engines (aarch64-linux-android)
|
||||
id: rust
|
||||
run: |
|
||||
@@ -429,7 +450,7 @@ jobs:
|
||||
path: |
|
||||
app/.cxx
|
||||
app/build/intermediates/cxx
|
||||
key: cxx-${{ env.NDK_VERSION }}-${{ hashFiles('app/src/main/cpp/**') }}
|
||||
key: cxx-${{ env.NDK_VERSION }}-${{ hashFiles('app/src/main/cpp/**', 'questxr/src/main/cpp/**', 'tools/vr/src/xr_*.h') }}
|
||||
restore-keys: |
|
||||
cxx-${{ env.NDK_VERSION }}-
|
||||
|
||||
@@ -482,7 +503,12 @@ jobs:
|
||||
run: python3 -m unittest discover -s tools/tests -p 'test_*.py'
|
||||
|
||||
- name: Unit tests
|
||||
run: ./gradlew testReleaseUnitTest --console=plain -PndkVersion="$NDK_VERSION"
|
||||
env:
|
||||
JAVA_TOOL_OPTIONS: ${{ github.event_name == 'workflow_dispatch' && '-Drobolectric.logging=stderr' || '' }}
|
||||
run: |
|
||||
args=()
|
||||
if [ "$GITHUB_EVENT_NAME" = workflow_dispatch ]; then args+=(--info --stacktrace); fi
|
||||
./gradlew testReleaseUnitTest --console=plain -PndkVersion="$NDK_VERSION" "${args[@]}"
|
||||
|
||||
- name: Sign with the testkey (v1 + v2 + v3)
|
||||
run: |
|
||||
|
||||
@@ -33,3 +33,5 @@ __pycache__/
|
||||
.signing.env
|
||||
app/src/main/assets/directaudio/
|
||||
/tools/vr/out/
|
||||
/questxr-build.log
|
||||
/questxr-validation/
|
||||
@@ -150,6 +150,7 @@ android {
|
||||
}
|
||||
|
||||
buildFeatures {
|
||||
prefab true
|
||||
compose true
|
||||
buildConfig true
|
||||
}
|
||||
@@ -225,6 +226,7 @@ if (!skipRust) android.sourceSets.main.jniLibs.srcDir(rustJniLibsDir)
|
||||
tasks.named('preBuild').configure { dependsOn(buildRustStores) }
|
||||
|
||||
dependencies {
|
||||
implementation 'org.khronos.openxr:openxr_loader_for_android:1.1.49'
|
||||
implementation 'com.github.luben:zstd-jni:1.5.2-3@aar'
|
||||
implementation 'org.apache.commons:commons-compress:1.27.1'
|
||||
implementation 'org.tukaani:xz:1.10'
|
||||
|
||||
@@ -22,6 +22,9 @@
|
||||
|
||||
<uses-feature android:name="android.hardware.vulkan.version" android:version="0x401000" android:required="true" />
|
||||
<uses-feature android:name="android.hardware.gamepad" android:required="false" />
|
||||
<uses-feature android:name="android.hardware.vr.headtracking" android:version="1" android:required="false" />
|
||||
<uses-feature android:name="oculus.software.handtracking" android:required="false" />
|
||||
<uses-permission android:name="com.oculus.permission.HAND_TRACKING" />
|
||||
|
||||
<application
|
||||
android:name=".App"
|
||||
@@ -37,6 +40,17 @@
|
||||
android:isGame="true">
|
||||
|
||||
<meta-data android:name="android.game_mode_config" android:resource="@xml/game_mode_config" />
|
||||
<activity android:name=".QuestXrActivity" android:exported="true" android:launchMode="singleTask"
|
||||
android:resizeableActivity="false" android:screenOrientation="landscape"
|
||||
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden|keyboard|navigation|uiMode"
|
||||
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
|
||||
<meta-data android:name="android.app.lib_name" android:value="droiddeckxr" />
|
||||
<intent-filter>
|
||||
<action android:name="com.droiddeck.launcher.XR" />
|
||||
<category android:name="com.oculus.intent.category.VR" />
|
||||
<category android:name="org.khronos.openxr.intent.category.IMMERSIVE_HMD" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
|
||||
@@ -30,6 +30,7 @@ string(SUBSTRING "${WINFG_CHAIN_HASH_FULL}" 0 12 WINFG_CHAIN_HASH)
|
||||
message(STATUS "win-fg chain: ${WINFG_UPSTREAM} (src ${WINFG_CHAIN_HASH})")
|
||||
|
||||
add_subdirectory(waylandcomp)
|
||||
add_subdirectory(../../../../questxr/src/main/cpp questxr)
|
||||
add_library(sdnative SHARED sdnative/sdnative.c)
|
||||
target_link_libraries(sdnative log)
|
||||
|
||||
|
||||
@@ -121,6 +121,11 @@ static pthread_mutex_t g_req_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static ANativeWindow *g_req_window;
|
||||
static int g_req_pending;
|
||||
static int g_req_detached;
|
||||
static uint64_t g_req_generation, g_req_applied;
|
||||
static pthread_cond_t g_req_condition = PTHREAD_COND_INITIALIZER;
|
||||
static uint64_t g_presented_frames;
|
||||
|
||||
uint64_t vkp_presented_frames(void) { return __atomic_load_n(&g_presented_frames, __ATOMIC_ACQUIRE); }
|
||||
|
||||
static volatile int g_mode = VKP_MODE_STRETCH, g_align = VKP_ALIGN_CENTER;
|
||||
|
||||
@@ -198,26 +203,53 @@ void vk_present_set_window(ANativeWindow *window) {
|
||||
if (g_req_pending && g_req_window && g_req_window != window) superseded = g_req_window;
|
||||
g_req_window = window;
|
||||
g_req_pending = 1;
|
||||
g_req_generation++;
|
||||
if (!window) g_req_detached = 1;
|
||||
pthread_mutex_unlock(&g_req_lock);
|
||||
if (superseded) ANativeWindow_release(superseded);
|
||||
}
|
||||
|
||||
static void window_request_applied(uint64_t generation) {
|
||||
pthread_mutex_lock(&g_req_lock);
|
||||
g_req_applied = generation;
|
||||
pthread_cond_broadcast(&g_req_condition);
|
||||
pthread_mutex_unlock(&g_req_lock);
|
||||
}
|
||||
|
||||
int vkp_wait_window_request(int timeout_ms) {
|
||||
if (timeout_ms <= 0 || timeout_ms > 10000) return 0;
|
||||
struct timespec deadline;
|
||||
clock_gettime(CLOCK_REALTIME, &deadline);
|
||||
deadline.tv_sec += timeout_ms / 1000;
|
||||
deadline.tv_nsec += (timeout_ms % 1000) * 1000000L;
|
||||
if (deadline.tv_nsec >= 1000000000L) { deadline.tv_sec++; deadline.tv_nsec -= 1000000000L; }
|
||||
pthread_mutex_lock(&g_req_lock);
|
||||
const uint64_t generation = g_req_generation;
|
||||
int error = 0;
|
||||
while (g_req_applied < generation && error == 0)
|
||||
error = pthread_cond_timedwait(&g_req_condition, &g_req_lock, &deadline);
|
||||
int applied = g_req_applied >= generation;
|
||||
pthread_mutex_unlock(&g_req_lock);
|
||||
return applied;
|
||||
}
|
||||
|
||||
int vkp_apply_window_request(void) {
|
||||
ANativeWindow *w;
|
||||
pthread_mutex_lock(&g_req_lock);
|
||||
if (!g_req_pending) { pthread_mutex_unlock(&g_req_lock); return 0; }
|
||||
w = g_req_window;
|
||||
const int detached = g_req_detached;
|
||||
const uint64_t generation = g_req_generation;
|
||||
g_req_window = NULL;
|
||||
g_req_pending = 0;
|
||||
g_req_detached = 0;
|
||||
pthread_mutex_unlock(&g_req_lock);
|
||||
if (w == g_window) {
|
||||
if (w) ANativeWindow_release(w);
|
||||
if (!w || !detached) return 0;
|
||||
if (!w || !detached) { window_request_applied(generation); return 0; }
|
||||
sc_layer_window_gone();
|
||||
destroy_swapchain();
|
||||
window_request_applied(generation);
|
||||
droiddeck_log("gpu", "screen surface rebound: rebuilding the swapchain at its new size");
|
||||
return 1;
|
||||
}
|
||||
@@ -228,6 +260,7 @@ int vkp_apply_window_request(void) {
|
||||
g_window = w;
|
||||
g_swap_retry_at_ns = 0;
|
||||
g_swap_fail_logged = 0;
|
||||
window_request_applied(generation);
|
||||
droiddeck_log("gpu", w ? "screen surface attached" : "screen surface gone: presenting paused");
|
||||
return 1;
|
||||
}
|
||||
@@ -1638,6 +1671,7 @@ static int render_impl(int scene_w, int scene_h, const struct vkp_draw *draws, i
|
||||
}
|
||||
|
||||
g_base_black = (g_plain_frame && n == 0 && !hf) ? 1 : 0;
|
||||
if (fr == VK_SUCCESS) __atomic_add_fetch(&g_presented_frames, 1u, __ATOMIC_RELEASE);
|
||||
|
||||
if (drawn && !g_first_frame_done) {
|
||||
g_first_frame_done = 1;
|
||||
|
||||
@@ -48,6 +48,8 @@ const char *vkp_gpu_name(void);
|
||||
|
||||
int vkp_ready(void);
|
||||
int vkp_has_window(void);
|
||||
int vkp_wait_window_request(int timeout_ms);
|
||||
uint64_t vkp_presented_frames(void);
|
||||
int vkp_device_lost(void);
|
||||
|
||||
int vkp_render(int scene_w, int scene_h, const struct vkp_draw *draws, int n);
|
||||
|
||||
@@ -582,6 +582,17 @@ static char *dup_bytes(JNIEnv *env, jbyteArray arr, int *len) {
|
||||
return buf;
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeWaitSurface(
|
||||
JNIEnv *env, jclass clazz, jint timeout_ms) {
|
||||
return vkp_wait_window_request(timeout_ms) ? JNI_TRUE : JNI_FALSE;
|
||||
}
|
||||
|
||||
JNIEXPORT jlong JNICALL
|
||||
Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativePresentedFrames(JNIEnv *env, jclass clazz) {
|
||||
return (jlong)vkp_presented_frames();
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeSetClipboardText(JNIEnv *env, jclass clazz, jbyteArray utf8) {
|
||||
int len;
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.droiddeck.launcher
|
||||
|
||||
import android.app.NativeActivity
|
||||
import android.os.Bundle
|
||||
import android.view.Surface
|
||||
import android.util.Log
|
||||
import com.droiddeck.launcher.gpu.SteamVr
|
||||
import com.droiddeck.launcher.session.SessionService
|
||||
import com.droiddeck.launcher.wayland.CompositorHost
|
||||
import com.droiddeck.launcher.wayland.WaylandCompositor
|
||||
|
||||
class QuestXrActivity : NativeActivity() {
|
||||
private var presentedBaseline = 0L
|
||||
private var steamVrStarted = false
|
||||
@Volatile private var closing = false
|
||||
@Volatile private var steamVrStatus = SteamVr.Status.UNAVAILABLE
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
if (intent.getBooleanExtra("guest_surface", false) || intent.getBooleanExtra("guest_frames", false)) {
|
||||
if (intent.getBooleanExtra("guest_surface", false)) intent.putExtra("surface_test", true)
|
||||
SessionService.setXrActivityVisible(this, true)
|
||||
}
|
||||
super.onCreate(savedInstanceState)
|
||||
}
|
||||
|
||||
fun onXrSurface(surface: Surface?, width: Int, height: Int) {
|
||||
if (surface != null) {
|
||||
presentedBaseline = WaylandCompositor.nativePresentedFrames()
|
||||
WaylandCompositor.nativeSetCardboard(false, 1, 0.064f, 3f, 4f, 0f, 0f, 90f, 0f, 0f)
|
||||
WaylandCompositor.nativeSetFrameGenArmed(false, 0, 0)
|
||||
WaylandCompositor.nativeSetZeroCopy(false)
|
||||
WaylandCompositor.nativeSetOutputSize(width, height)
|
||||
WaylandCompositor.nativeSetFpsLimit(0)
|
||||
}
|
||||
CompositorHost.attachXr(surface)
|
||||
SessionService.setXrActivityVisible(this, surface != null)
|
||||
WaylandCompositor.nativeVsync(System.nanoTime())
|
||||
check(WaylandCompositor.nativeWaitSurface(3000)) { "XR surface GPU handshake timed out" }
|
||||
if (surface != null) startSteamVr()
|
||||
}
|
||||
|
||||
fun onXrActive(active: Boolean) {
|
||||
if (active) {
|
||||
check(CompositorHost.isStarted) { "DroidDeck compositor is not running" }
|
||||
SessionService.resume(this)
|
||||
WaylandCompositor.nativeSetCardboard(false, 1, 0.064f, 3f, 4f, 0f, 0f, 90f, 0f, 0f)
|
||||
WaylandCompositor.nativeSetFrameGenArmed(false, 0, 0)
|
||||
WaylandCompositor.nativeSetZeroCopy(false)
|
||||
CompositorHost.attachXr(null)
|
||||
WaylandCompositor.nativeVsync(System.nanoTime())
|
||||
check(WaylandCompositor.nativeWaitSurface(3000)) { "XR frame GPU handshake timed out" }
|
||||
}
|
||||
SessionService.setXrActivityVisible(this, active)
|
||||
if (active) startSteamVr()
|
||||
}
|
||||
|
||||
private fun startSteamVr() {
|
||||
if (intent.getBooleanExtra("start_steamvr", false) && !steamVrStarted) {
|
||||
steamVrStarted = true
|
||||
Thread({
|
||||
if (!SteamVr.run(this, "start")) Log.e("DroidDeckXR", "SteamVR start failed")
|
||||
while (!closing) {
|
||||
steamVrStatus = SteamVr.status(this)
|
||||
Thread.sleep(1000)
|
||||
}
|
||||
SteamVr.run(this, "stop")
|
||||
}, "quest-steamvr-start").start()
|
||||
}
|
||||
}
|
||||
|
||||
fun onXrFrame(frameTimeNanos: Long): Long {
|
||||
WaylandCompositor.nativeVsync(frameTimeNanos)
|
||||
return (WaylandCompositor.nativePresentedFrames() - presentedBaseline).coerceAtLeast(0)
|
||||
}
|
||||
|
||||
fun onXrStatus(): Int = steamVrStatus.ordinal
|
||||
|
||||
override fun onDestroy() {
|
||||
closing = true
|
||||
SessionService.setXrActivityVisible(this, false)
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
SessionService.setXrActivityVisible(this, false)
|
||||
super.onStop()
|
||||
}
|
||||
}
|
||||
@@ -2325,6 +2325,11 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
}
|
||||
|
||||
private fun steamVrAction(action: String) {
|
||||
if (action == "start" && com.droiddeck.launcher.session.QuestExperimental.isSupported()) {
|
||||
startActivity(android.content.Intent(this, QuestXrActivity::class.java)
|
||||
.putExtra("guest_surface", true).putExtra("start_steamvr", true))
|
||||
return
|
||||
}
|
||||
if (action == "start") {
|
||||
switchPictureForSteamVr()
|
||||
steamVrSeenRunning = false
|
||||
|
||||
@@ -104,6 +104,7 @@ object SessionFiles {
|
||||
listOf("NOTICE", "GPL-2", "GPL-3", "LGPL-2.1", "7zip-License", "7zip-unRarLicense").map { "usr/local/share/licenses/droiddeck-msitools/$it" }
|
||||
val vr = listOf(
|
||||
"droiddeck/bin/linuxarm64/driver_droiddeck.so",
|
||||
"droiddeck/bin/linuxarm64/xr-display-mode",
|
||||
"droiddeck/driver.vrdrivermanifest",
|
||||
"droiddeck/resources/settings/default.vrsettings",
|
||||
"layer/libVkLayer_droiddeck_vr.so",
|
||||
|
||||
@@ -71,6 +71,8 @@ class SessionService : Service() {
|
||||
private var suspendPolicy = SessionPrefs.SUSPEND_MANUAL
|
||||
private var steamDownloadsInBackground = false
|
||||
private var activityVisible = true
|
||||
private var xrActivityVisible = false
|
||||
private val hasVisibleSurface get() = sessionHasVisibleSurface(activityVisible, screenOn, xrActivityVisible)
|
||||
private var screenOn = true
|
||||
private var manualPauseRequested = false
|
||||
private var steamSleepToken: String? = null
|
||||
@@ -170,6 +172,17 @@ class SessionService : Service() {
|
||||
updateSuspendPolicy()
|
||||
return START_NOT_STICKY
|
||||
}
|
||||
ACTION_XR_ACTIVITY_VISIBLE -> {
|
||||
xrActivityVisible = true
|
||||
resumeSession(panelVisible = false)
|
||||
return START_NOT_STICKY
|
||||
}
|
||||
ACTION_XR_ACTIVITY_HIDDEN -> {
|
||||
xrActivityVisible = false
|
||||
suspendAttemptFailed = false
|
||||
updateSuspendPolicy()
|
||||
return START_NOT_STICKY
|
||||
}
|
||||
ACTION_SUSPEND_POLICY_CHANGED -> {
|
||||
if (!SessionState.running) return START_NOT_STICKY
|
||||
suspendPolicy = SessionPrefs.suspendPolicy(this, SessionState.mode)
|
||||
@@ -205,6 +218,7 @@ class SessionService : Service() {
|
||||
suspendPolicy = SessionPrefs.suspendPolicy(this, SessionState.mode)
|
||||
steamDownloadsInBackground = SessionPrefs.steamDownloadsInBackground(this)
|
||||
activityVisible = true
|
||||
xrActivityVisible = false
|
||||
screenOn = (getSystemService(Context.POWER_SERVICE) as? PowerManager)?.isInteractive ?: true
|
||||
manualPauseRequested = false
|
||||
suspendDownloadNow = false
|
||||
@@ -918,8 +932,8 @@ class SessionService : Service() {
|
||||
Log.i(TAG, "queued Steam game $gameId for the running client")
|
||||
}
|
||||
|
||||
private fun resumeSession() {
|
||||
activityVisible = true
|
||||
private fun resumeSession(panelVisible: Boolean = true) {
|
||||
if (panelVisible) activityVisible = true
|
||||
screenOn = (getSystemService(Context.POWER_SERVICE) as? PowerManager)?.isInteractive ?: screenOn
|
||||
manualPauseRequested = false
|
||||
suspendDownloadNow = false
|
||||
@@ -1110,11 +1124,11 @@ class SessionService : Service() {
|
||||
|
||||
private fun updateSuspendPolicy() {
|
||||
if (!SessionState.running) return
|
||||
if (suspendPolicy == SessionPrefs.SUSPEND_MANUAL && (!activityVisible || !screenOn)) {
|
||||
if (suspendPolicy == SessionPrefs.SUSPEND_MANUAL && !hasVisibleSurface) {
|
||||
manualPauseRequested = true
|
||||
}
|
||||
val controller = suspendController ?: return
|
||||
val hidden = !activityVisible || !screenOn
|
||||
val hidden = !hasVisibleSurface
|
||||
val downloadsBlockingSuspend = steamDownloadsInBackground && SessionState.mode == MODE_STEAM &&
|
||||
suspendPolicy != SessionPrefs.SUSPEND_NEVER && hidden && downloadActive && !suspendDownloadNow
|
||||
if (downloadsBlockingSuspend) acquireLocks()
|
||||
@@ -1136,6 +1150,7 @@ class SessionService : Service() {
|
||||
SessionEvents.record(if (shouldSuspend) "session.suspend_requested" else "session.resume_requested", mapOf(
|
||||
"policy" to suspendPolicy, "activityVisible" to activityVisible,
|
||||
"screenOn" to screenOn, "steamSleep" to (steamSleepToken != null),
|
||||
"xrActivityVisible" to xrActivityVisible,
|
||||
"nativeSleep" to (nativeSleepToken != null),
|
||||
))
|
||||
if (shouldSuspend) {
|
||||
@@ -1193,7 +1208,7 @@ class SessionService : Service() {
|
||||
}
|
||||
|
||||
private fun resumeSteamSleepOnReturn() {
|
||||
if (suspendPolicy in setOf(SessionPrefs.SUSPEND_AUTO, SessionPrefs.SUSPEND_NATIVE) && activityVisible && screenOn) {
|
||||
if (suspendPolicy in setOf(SessionPrefs.SUSPEND_AUTO, SessionPrefs.SUSPEND_NATIVE) && hasVisibleSurface) {
|
||||
if (steamSleepToken == null && (nativeSleepToken == null || nativeSleepFallback)) requestSteamWake()
|
||||
completeNativeSleep()
|
||||
completeSteamSleep()
|
||||
@@ -1304,6 +1319,7 @@ class SessionService : Service() {
|
||||
if (!SessionState.running) return
|
||||
SessionState.running = false
|
||||
}
|
||||
xrActivityVisible = false
|
||||
updateDownloadMonitoring()
|
||||
val stoppedGen = sessionGen
|
||||
val stoppedSession = SessionState.sessionId
|
||||
@@ -1356,8 +1372,8 @@ class SessionService : Service() {
|
||||
}
|
||||
|
||||
override fun onTaskRemoved(rootIntent: Intent?) {
|
||||
if (pipTask) {
|
||||
Log.i(TAG, "PiP task dismissed - retaining the session under its background policy")
|
||||
if (pipTask || xrActivityVisible) {
|
||||
Log.i(TAG, "panel task dismissed - retaining the session under its active surface policy")
|
||||
activityVisible = false
|
||||
updateSuspendPolicy()
|
||||
return
|
||||
@@ -1558,6 +1574,8 @@ class SessionService : Service() {
|
||||
private const val ACTION_SUSPEND_POLICY_CHANGED = "com.droiddeck.launcher.SUSPEND_POLICY_CHANGED"
|
||||
private const val ACTION_ACTIVITY_VISIBLE = "com.droiddeck.launcher.ACTIVITY_VISIBLE"
|
||||
private const val ACTION_ACTIVITY_HIDDEN = "com.droiddeck.launcher.ACTIVITY_HIDDEN"
|
||||
private const val ACTION_XR_ACTIVITY_VISIBLE = "com.droiddeck.launcher.XR_ACTIVITY_VISIBLE"
|
||||
private const val ACTION_XR_ACTIVITY_HIDDEN = "com.droiddeck.launcher.XR_ACTIVITY_HIDDEN"
|
||||
private const val ACTION_TRACK_AUXILIARY = "com.droiddeck.launcher.TRACK_AUXILIARY"
|
||||
private const val ACTION_STOP_AUXILIARY = "com.droiddeck.launcher.STOP_AUXILIARY"
|
||||
private const val ACTION_AUXILIARY_EXITED = "com.droiddeck.launcher.AUXILIARY_EXITED"
|
||||
@@ -1617,6 +1635,12 @@ class SessionService : Service() {
|
||||
context.startService(Intent(context, SessionService::class.java).setAction(action))
|
||||
}
|
||||
|
||||
fun setXrActivityVisible(context: Context, visible: Boolean) {
|
||||
if (!SessionState.running) return
|
||||
val action = if (visible) ACTION_XR_ACTIVITY_VISIBLE else ACTION_XR_ACTIVITY_HIDDEN
|
||||
context.startService(Intent(context, SessionService::class.java).setAction(action))
|
||||
}
|
||||
|
||||
fun resume(context: Context) {
|
||||
if (!SessionState.running) return
|
||||
context.startService(Intent(context, SessionService::class.java).setAction(ACTION_RESUME))
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.droiddeck.launcher.session
|
||||
|
||||
internal fun sessionHasVisibleSurface(panelVisible: Boolean, screenOn: Boolean, xrVisible: Boolean): Boolean =
|
||||
xrVisible || (panelVisible && screenOn)
|
||||
@@ -7,6 +7,7 @@ import com.droiddeck.launcher.session.SessionState
|
||||
object CompositorHost {
|
||||
@Volatile
|
||||
private var started = false
|
||||
@Volatile
|
||||
private var vsyncRunning = false
|
||||
|
||||
private val vsync = object : Choreographer.FrameCallback {
|
||||
@@ -71,6 +72,14 @@ object CompositorHost {
|
||||
|
||||
private var attached: Surface? = null
|
||||
|
||||
@Synchronized
|
||||
fun attachXr(surface: Surface?) {
|
||||
check(started) { "DroidDeck compositor is not running" }
|
||||
pauseVsync()
|
||||
attached = surface
|
||||
WaylandCompositor.nativeSetSurface(surface)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun detach(surface: Surface?) {
|
||||
if (!started) return
|
||||
|
||||
@@ -148,6 +148,8 @@ public final class WaylandCompositor {
|
||||
String nativeLibDir);
|
||||
|
||||
public static native void nativeSetSurface(Surface surface);
|
||||
public static native boolean nativeWaitSurface(int timeoutMs);
|
||||
public static native long nativePresentedFrames();
|
||||
|
||||
public static native void nativeSendPointer(int action, int x, int y);
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.droiddeck.launcher.session
|
||||
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class SessionVisibilityTest {
|
||||
@Test fun xrVisibilitySurvivesPanelHandoffAndPanelScreenState() {
|
||||
for (panelVisible in listOf(false, true)) {
|
||||
for (screenOn in listOf(false, true)) {
|
||||
assertTrue(sessionHasVisibleSurface(panelVisible, screenOn, true))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun normalPanelStillRequiresAnInteractiveScreen() {
|
||||
assertTrue(sessionHasVisibleSurface(true, true, false))
|
||||
assertFalse(sessionHasVisibleSurface(false, true, false))
|
||||
assertFalse(sessionHasVisibleSurface(true, false, false))
|
||||
assertFalse(sessionHasVisibleSurface(false, false, false))
|
||||
}
|
||||
|
||||
@Test fun losingXrVisibilityReturnsToNormalSuspendRules() {
|
||||
assertTrue(sessionHasVisibleSurface(false, false, true))
|
||||
assertFalse(sessionHasVisibleSurface(false, false, false))
|
||||
assertTrue(sessionHasVisibleSurface(true, true, false))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,408 @@
|
||||
# Quest OpenXR implementation
|
||||
|
||||
Branch: `feat/quest-openxr`. The goal is standalone immersive SteamVR on Quest,
|
||||
using DroidDeck's Linux guest and an Android OpenXR frontend.
|
||||
|
||||
## Milestone 1: native headset frontend
|
||||
|
||||
`questxr` builds a separate `com.droiddeck.questxr` companion so the frontend can
|
||||
be validated without replacing the installed DroidDeck runtime. It uses the
|
||||
Khronos Android loader, GLES swapchains, predicted stereo views and head poses,
|
||||
Touch grip/trigger actions, and trigger-edge haptic feedback. The test scene has
|
||||
world-fixed geometry and tracked controller cubes. Controller availability is
|
||||
optional for the diagnostic scene; sleeping controllers remain inactive.
|
||||
|
||||
Build with `:questxr:assembleDebug`. `tools/deploy_quest_xr.ps1` builds, aligns,
|
||||
signs with an external key directory, verifies the expected certificate and
|
||||
installs the companion. Private keys must remain outside the checkout.
|
||||
|
||||
Run `python tools/test_quest_xr.py --adb <adb executable>`. The runner restarts
|
||||
the companion, simulates wearing the headset using Meta's documented
|
||||
`com.oculus.vrpowermanager.prox_close` broadcast, checks fresh advancing frame
|
||||
telemetry and valid tracked head pose, and stores local JSON/logcat results in
|
||||
the ignored `questxr-validation` directory. Use `--require-controllers` when
|
||||
both Touch controllers are awake. That option must pass before claiming tracked
|
||||
controller validation. Trigger haptics require an actual trigger press.
|
||||
|
||||
## Device evidence, 2026-10-14
|
||||
|
||||
Quest 3, Android 14, DroidDeck 0.4.0. Local Windows build with NDK 27.2.12479018
|
||||
passed. The deployed companion certificate matches `keystore/release-signer.sha256`.
|
||||
The automated 15-second test passed with 792 rendered stereo frames at the last
|
||||
sample, a 13,888,888 ns predicted display period (72 Hz), and head location flags
|
||||
15 (position/orientation valid and tracked). A focused state was observed before
|
||||
an OS panel took input focus; stereo frame submission continued in VISIBLE.
|
||||
Neither controller was active. Controller motion and physical haptics have not
|
||||
been validated. This establishes native OpenXR rendering, not SteamVR gameplay.
|
||||
|
||||
Quest reported `XR_KHR_android_surface_swapchain` support. Next, validate an
|
||||
Android-produced surface swapchain as a projection source, then connect the
|
||||
existing Android Wayland compositor to it. This may avoid transferring Linux
|
||||
dmabufs directly into the runtime's graphics device. Surface support alone does
|
||||
not establish that projection, synchronization, or SteamVR rendering works.
|
||||
|
||||
The first launch left a controller-required OS dialog from an earlier manifest.
|
||||
After declaring optional controller/hand capability and restarting VR Shell,
|
||||
the app launched. Automated tests should report launch blockers rather than
|
||||
treating a successful `am start` as successful rendering.
|
||||
|
||||
## Milestone 2: Android projection surface and guest integration
|
||||
|
||||
The Android Surface producer test passed repeatedly, including 1,079 submitted
|
||||
stereo frames in a 15-second test at 72 Hz. The new stage-space test reports
|
||||
tracked floor-relative position, and the runtime supports monotonic-time
|
||||
conversion. A JNI abort during the first surface test was traced to treating
|
||||
the runtime's global Surface reference as a local reference and corrected.
|
||||
|
||||
The same native frontend is now compiled into the main app behind the explicit
|
||||
`QuestXrActivity` entrypoint. With `guest_surface=true`, it hands its Android
|
||||
projection Surface to the existing compositor. Surface transitions wait for a
|
||||
compositor acknowledgement after the old Vulkan swapchain's GPU work drains.
|
||||
Successful guest GPU presentations are counted separately from XR submissions.
|
||||
The main app's Kotlin and native targets compile locally; an on-device main-app
|
||||
guest test is still required before considering this bridge functional.
|
||||
|
||||
The frontend publishes a versioned 296-byte seqlock tracking record under the
|
||||
session runtime directory. It contains floor-relative head/controller poses,
|
||||
per-eye view geometry, predicted display time converted to CLOCK_MONOTONIC,
|
||||
display period, and Touch action values. The OpenVR driver consumes fresh
|
||||
records for real head/controller poses and asymmetric FOV; it marks stale
|
||||
tracking invalid. The modified ARM64 Linux driver links successfully in WSL.
|
||||
Driver timing, eye-to-head transforms, input completeness, render-pose/image
|
||||
association and return haptics still need implementation/validation.
|
||||
|
||||
The companion's release build now targets Android 14 and passes release lint.
|
||||
|
||||
## Milestone 3: input, timing and return haptics
|
||||
|
||||
The driver now supplies eye-to-head transforms and runtime-derived IPD, consumes
|
||||
Touch trigger/squeeze/stick/button state, reports predicted pose timestamps and
|
||||
paces vsync against converted OpenXR display timing. Vive-profile compatibility
|
||||
maps primary buttons to trackpad click and secondary/menu buttons to application
|
||||
menu. A versioned 64-byte feedback channel carries bounded driver haptic events
|
||||
back to OpenXR. Repeated surface tests still submit 1,079 stereo frames at 72 Hz.
|
||||
The native and Kotlin targets compile, release lint passes, and the ARM64 Linux
|
||||
driver links with no undefined symbols. IPC tests cover valid packets, odd
|
||||
seqlocks, version mismatch, stale/future timestamps, inactive state, excessive
|
||||
dimensions and truncated files; they run in CI as well.
|
||||
|
||||
The first full integration CI APK compiled successfully but failed the ELF
|
||||
closure check because its system-library list omitted `libGLESv3.so`. The list
|
||||
now recognizes that Android system dependency. A subsequent full CI run is
|
||||
required before deployment of the integrated main-app build.
|
||||
|
||||
On-device prerequisites: the installed app's Debugging tools toggle explicitly
|
||||
rejects agent UI activation (`PROTECTED_CONTROL`); it must be switched on-device.
|
||||
Steam is signed out and both Touch controllers remain inactive. These block
|
||||
guest command diagnostics and actual game/controller/haptic validation, but do
|
||||
not block native rendering or independent engineering. No guard was bypassed.
|
||||
|
||||
## Milestone 4: full build and deployment
|
||||
|
||||
Full CI run 38641552327 passed at `e1d09612`, including APK compilation, tracking
|
||||
IPC tests and ELF closure verification. Its 62,211,156-byte APK was downloaded,
|
||||
aligned and signed locally with the provided key. The installed original APK's
|
||||
certificate matched the expected release certificate. Updating the standard
|
||||
`com.droiddeck.launcher` package with `adb install -r` succeeded; a copy of the
|
||||
previous APK is retained locally under ignored `build/installed-original.apk`.
|
||||
No uninstall or data reset was performed.
|
||||
|
||||
At that stage, source added the Quest-specific SteamVR Start flow, ongoing SteamVR
|
||||
status telemetry, a clean native Back/exit path, and bounded Android event
|
||||
draining. It removes UI-thread waits from native surface handoff to avoid a
|
||||
NativeActivity destruction deadlock, and restores proximity after tests unless
|
||||
`--keep-awake` is specified. These changes compiled locally and subsequently
|
||||
shipped in the integrated APK described in the final build evidence.
|
||||
|
||||
Later device runs stopped rendering because the OS displayed **Finding position
|
||||
in room**, saying it cannot detect headset movement. Logs show the XR session
|
||||
going through STOPPING to IDLE; a sample had only flags 7, lacking tracked
|
||||
position. State changes are now written immediately to health telemetry, and
|
||||
tests require all four valid/tracked flags to establish 6DoF. Earlier successful
|
||||
72 Hz runs remain evidence for those earlier device conditions; the latest
|
||||
tests are not counted as passes. Physical headset positioning/lighting needs
|
||||
attention before sustained 6DoF and game validation can resume.
|
||||
|
||||
The actual shared tracking file was read through the debuggable companion:
|
||||
296 bytes, correct magic/version and even sequence, converted prediction about
|
||||
21 ms beyond the monotonic sample time. This confirms the IPC time-domain
|
||||
conversion on the headset.
|
||||
|
||||
## Milestone 5: owned image and original render-pose protocol
|
||||
|
||||
The opt-in `DROIDDECK_VR_PRESENT=questframes` presenter captures an owned RGBA
|
||||
side-by-side image and its original SteamVR render geometry. The driver retains
|
||||
each submitted `mHmdPose` and `mProjection` and composes the same eye-to-head rig
|
||||
configured at HMD activation. It does not substitute the newest tracked pose
|
||||
for an older image. The versioned 264-byte header and pixels travel together
|
||||
over a private, same-UID Unix socket. Acknowledgement means the receiver owns a
|
||||
complete CPU copy, not that an XR layer was displayed.
|
||||
|
||||
The capture path holds `Present` until its Vulkan copy fence completes. It
|
||||
refuses missing, empty, errored or timed-out producer DMA fences, unsupported
|
||||
multiple layers and malformed render geometry. It never substitutes the old
|
||||
4 ms delay for synchronization. **Kernel producer-fence registration on the
|
||||
Quest graphics chain is not yet established**, and KGSL may reject the export
|
||||
entirely. Consequently this is an experimental correctness-first path, not a
|
||||
verified synchronization solution. The existing compatibility Vulkan layer
|
||||
still drops some imported semaphore waits; that also needs resolution before
|
||||
gameplay correctness can be claimed.
|
||||
|
||||
With `frame_input=true`, the native frontend uses explicitly acquired GLES XR
|
||||
swapchain images, uploads receiver-owned pixels, and submits the captured
|
||||
per-eye poses/FOV. It omits stale images. `frame_verify=true` reads uploaded
|
||||
pixels back from the GPU and compares every byte. This mode is separate from
|
||||
the asynchronous Android Surface diagnostic. The main activity's explicit
|
||||
`guest_frames=true` entrypoint detaches the desktop Surface, keeps the guest
|
||||
active and starts the same SteamVR helper when requested. The Linux helper
|
||||
selects the experimental presenter only when this private receiver exists.
|
||||
`tools/test_quest_xr.py --guest --steamvr` uses that entrypoint and requires owned
|
||||
source images/frame IDs to advance, not generic desktop presentations. It is
|
||||
not yet the normal Quest Start flow and does not provide a zero-copy or
|
||||
performance-ready bridge.
|
||||
|
||||
Host tests cover image/pose pairing, rotation and asymmetric projection math,
|
||||
latest-frame replacement, copying before acknowledgement, private socket
|
||||
ownership, restart/stale-socket recovery, replay rejection, malformed/truncated
|
||||
input and deadlines. Address/undefined-behavior sanitizer tests pass. A separate
|
||||
test confirms eight concurrent blocking presenters exit when Vulkan cannot
|
||||
initialize. Android test executables compile with NDK 27.2.12479018; the main
|
||||
native target and companion APK also compile. These are host/build results,
|
||||
not headset GPU evidence. The Quest subsequently disappeared from ADB, so the
|
||||
new path cannot yet be deployed or validated on-device.
|
||||
|
||||
`tools/build_quest_xr_tests.ps1` builds the three Android test executables.
|
||||
`tools/test_quest_frame_channel.py --adb <adb> --native-tests <directory>` runs
|
||||
the Android protocol test and a known-pattern sender under the companion's own
|
||||
UID. It checks the final source frame ID and GPU checksum. Add
|
||||
`--require-rendering` to additionally require sustained tracked projection.
|
||||
Without that option it proves only private image transport/GLES upload, not
|
||||
6DoF, SteamVR or games. It never enables the main app's protected Debugging tools.
|
||||
|
||||
The failed full build at `7277386c` was a Debian archive HTTP package 404. CI now
|
||||
bootstraps CA certificates, uses HTTPS for compiler packages and retries
|
||||
downloads while retaining APT signature/package-hash verification.
|
||||
|
||||
The signed companion for this stage is retained locally under ignored
|
||||
`build/questxr-owned-frames-signed.apk`. Deployment supports `-SignOnly` while
|
||||
disconnected; before installing it now requires an authorized, explicitly
|
||||
selected Quest 3, rather than risking an unrelated Android device.
|
||||
|
||||
### Reconnected device evidence
|
||||
|
||||
Full CI run 38649944478 passed at `040d31c1` and its APK was downloaded and
|
||||
release-signed locally. After USB reconnection, the updated diagnostic companion
|
||||
installed successfully. The private image test passed on the actual Quest:
|
||||
144 complete frames received, zero rejections, 137 uploaded, final source and
|
||||
GPU-verified frame ID 144, and expected GPU checksum 1815165381. Every byte of
|
||||
the owned image matched GLES readback. The protocol unit executable also passed
|
||||
on Android. These prove image ownership/transport and real GPU upload, not
|
||||
Linux producer synchronization or SteamVR gameplay.
|
||||
|
||||
A later repeat passed both tracking and frame protocol unit executables on
|
||||
Android but lost the sustained session at image 138. The native tracking test
|
||||
also failed: 9 rendered frames, session stopped to IDLE, head flags 7 and no
|
||||
active controllers. State reports still show Debugging tools off and no running
|
||||
guest. Steam sign-in is pending. Test failures are retained, not reclassified
|
||||
as passes. The user cannot attend these prerequisites yet and requested a pause
|
||||
after the remaining independent work.
|
||||
|
||||
The driver now marks position/orientation that are merely valid but untracked
|
||||
as out-of-range and rejects nonfinite or non-unit poses. The importer requests
|
||||
Vulkan 1.2 for its core image-format-list use. Focused Kotlin tests and the new
|
||||
validation classifier tests pass. The broad Linux suite had one unrelated
|
||||
AppImage log-tail failure under WSL; the full CI suite passed at `040d31c1`.
|
||||
|
||||
### Final build and requested pause
|
||||
|
||||
Full CI run 38652176155 passed at `2ef2d4b5`, including the expanded IPC tests,
|
||||
Kotlin/Python tests and ELF checks. The APK was downloaded, release-signed with
|
||||
the external key and installed over the standard package with `adb install -r`.
|
||||
The device provider confirms `remote clean · feat/quest-openxr at 2ef2d4b`,
|
||||
version 0.4.0, retained r9 runtime and an idle session. MainActivity launches
|
||||
without an AndroidRuntime error. No uninstall or data reset was used.
|
||||
|
||||
A final cosmetic shader-variable cleanup compiled for both native targets and
|
||||
was deployed in the companion. Its repeated device test passed both Android IPC
|
||||
executables and the known-pattern GPU upload: 144 received, zero rejected,
|
||||
126 uploaded, source/GPU-verified ID 144 and checksum 1815165381. The earlier
|
||||
interrupted repeat remains a recorded failure; sustained tracked rendering and
|
||||
guest SteamVR remain unverified. The unrelated AppImage test suite passed all
|
||||
eight cases on a focused rerun without code changes.
|
||||
|
||||
The diagnostic companion is stopped, simulated proximity is restored to far,
|
||||
and the USB stay-awake setting introduced during testing is restored to off.
|
||||
The user requested pausing the goal until they can help with Steam sign-in,
|
||||
on-device Debugging tools, tracking and controllers. The goal is not complete.
|
||||
|
||||
### Resumed device work
|
||||
|
||||
Steam sign-in and the on-device Debugging tools prerequisite are now satisfied.
|
||||
The user's left Touch controller tracks in space and gives a short trigger
|
||||
pulse in the native scene. The right controller also fails in Quest Home, so
|
||||
two-controller validation remains blocked by a controller/Quest-side issue.
|
||||
The native scene reports tracked head flags 15 and a roughly 8.333 ms display
|
||||
period (120 Hz). This does not establish guest SteamVR image correctness.
|
||||
|
||||
The selected imported Android/Linux drivers failed frame import. Selecting the
|
||||
bundled Quest-patched Android driver and the Linux runtime default, disabling
|
||||
Cardboard for the ordinary 2D panel, and restarting the app process restored
|
||||
Steam's first frame and READY. These were combined configuration changes, not
|
||||
an isolated single-driver comparison. Original preferences are backed up
|
||||
locally. No uninstall or data reset was used.
|
||||
|
||||
Valve's pinned ARM64 SteamVR package installed successfully. The guest driver
|
||||
receives real per-eye texture handles and imports them on the Quest GPU, but
|
||||
the correctness-first capture path rejects the unavailable producer fences.
|
||||
The first owned-image test submitted zero projection images; it remains a
|
||||
failure. The ordinary Wayland diagnostic can present SteamVR frames but does
|
||||
not establish producer synchronization or image/pose association.
|
||||
|
||||
The Vsync scheduler now rejects repeated predicted targets and PostPresent
|
||||
uses the same atomically paired runtime tick/period instead of a fixed 60 Hz
|
||||
interval. Host and actual Android CPU regression tests pass for 72/90/120 Hz,
|
||||
throttling, prediction reuse, concurrent tick/period publication and restart.
|
||||
The updated ARM64 driver links and was copied into the live runtime with its
|
||||
SHA-256 verified. This does not yet establish sustained guest rendering.
|
||||
|
||||
The 2D panel's onStop could also suspend the guest after the XR surface became
|
||||
visible. XR visibility now has a separate service lease; panel transitions
|
||||
cannot overwrite it, and normal suspend rules return when XR visibility is
|
||||
lost. Focused visibility/background-policy tests pass. A signed full-app build
|
||||
and live validation of this lifecycle fix are pending. The lease is acquired
|
||||
before native initialization so removing the old panel task cannot end the
|
||||
guest during the handoff, and it is explicitly released on stop/destruction.
|
||||
|
||||
SteamVR's virtual display still advertised 1920x1080 at 60 Hz while the XR
|
||||
driver advertised 3360x1760 at 120 Hz. The compositor could not find a matching
|
||||
surface and crashed after its first submission. A controlled runtime override
|
||||
to the XR geometry restores the headless display surface and sustained 120 Hz
|
||||
submission: over 10,000 Present calls were observed. Owned images remain at
|
||||
zero because the producer-fence check correctly refuses them.
|
||||
|
||||
The helper now obtains display geometry from fresh, validated tracking using a
|
||||
small ARM64 reader built against the same IPC headers. Explicit user overrides
|
||||
remain unchanged and older/stale sessions retain the normal fallback. Host
|
||||
tracking tests and four shell-helper regressions pass. The actual Quest reader
|
||||
prints 3360x1760@120, and automatic selection reproduces sustained submission.
|
||||
Startup stdout is now retained in Steam's guest log directory, with the old
|
||||
runtime-log path preserved as a symlink; a session restart no longer loses it.
|
||||
|
||||
A minimal native Vulkan probe on the real Quest reports export/import support
|
||||
for SYNC_FD fences/semaphores, but not OPAQUE_FD handles. This distinguishes the
|
||||
unsupported implicit DMA-buf fence export from available explicit Vulkan GPU
|
||||
completion primitives. Implementing the explicit producer synchronization is
|
||||
still necessary. The desktop's dashboard helper also
|
||||
lacks X display authorization; no authentication or permission check was
|
||||
bypassed to work around that error.
|
||||
|
||||
The Linux Vulkan GPU regression in `tools/vr/tests/test_vulkan_sync_fd.cpp`
|
||||
submits an actual GPU fill with an exportable SYNC_FD fence, checks its kernel
|
||||
sync-file payload, waits for the signal, and verifies every mapped value.
|
||||
The Quest run exported descriptor 5, reported one initially pending kernel
|
||||
fence, signaled successfully and matched all 1,024 values. This proves an
|
||||
explicit Vulkan GPU completion primitive on this graphics stack, not the
|
||||
as-yet-unimplemented cross-process producer lease or SteamVR game correctness.
|
||||
`tools/vr/build.sh` builds its ARM64 executable under `build/vr`; it is not
|
||||
bundled into the user-facing runtime. A valid already-signaled SYNC_FD value of
|
||||
-1 is handled according to the Vulkan specification.
|
||||
|
||||
## Remaining stages
|
||||
|
||||
### Sustained validation telemetry
|
||||
|
||||
`tools/test_quest_xr.py --metrics` records public battery, thermal-service and
|
||||
package-memory dumps before and after the measured frame window. Unavailable
|
||||
metrics remain explicitly unavailable; no privilege escalation or alternate
|
||||
private-file access is attempted. Reports now distinguish observed XR submits,
|
||||
guest GPU presentations, owned-image uploads and rejected-frame rates from the
|
||||
runtime's nominal refresh rate, and record the focused/tracked sample fraction.
|
||||
A run that loses focused 6DoF tracking at the end no longer passes merely because
|
||||
one earlier sample was tracked. These reporting paths have host tests; no new
|
||||
Quest performance or thermal result is claimed while the headset is disconnected.
|
||||
|
||||
### Touch input profile
|
||||
|
||||
Fresh XR controller activation now selects SteamVR's built-in `oculus_touch`
|
||||
profile and handed Quest Plus render model instead of presenting real Touch
|
||||
controllers as Vive wands. Face buttons, joystick click, left menu, analog grip
|
||||
and capacitive touches are separate inputs. A/B and X/Y no longer double as
|
||||
trackpad or menu clicks; the Quest's reserved system button is not fabricated
|
||||
from its left application menu. Native OpenXR captures trigger, thumbstick,
|
||||
thumbrest and face-button touch actions in previously unused button bits, keeping
|
||||
the 296-byte tracking layout and existing low click bits compatible. Inactive
|
||||
hands clear every input; invalid analog values are sanitized. The flat Cardboard
|
||||
gamepad path retains its existing Vive profile and mappings.
|
||||
Hand poses now carry the converted OpenXR prediction timestamp relative to
|
||||
SteamVR's clock, matching the headset instead of reporting a future hand pose
|
||||
as a present-time sample. The common timestamp conversion is host-tested for
|
||||
future, past and unavailable prediction times.
|
||||
|
||||
Host tests cover the input decoder's independent clicks, touch-versus-press,
|
||||
handed menu, analog ranges and inactive reset. The Android native frontend and
|
||||
Linux driver compile locally. SteamVR profile acceptance, game bindings and the
|
||||
physical right controller still require connected-device validation. Profile
|
||||
and component paths follow the [OpenXR Touch profile](https://registry.khronos.org/OpenXR/specs/1.0-khr/html/xrspec.html#_oculus_touch_controller_profile)
|
||||
and ALVR's [OpenVR properties](https://github.com/alvr-org/ALVR/blob/ebec107b7e5f81677fb9c631d239daf2110bf573/alvr/server_openvr/src/props.rs)
|
||||
and [input paths](https://github.com/alvr-org/ALVR/blob/ebec107b7e5f81677fb9c631d239daf2110bf573/alvr/server_openvr/cpp/alvr_server/Paths.cpp).
|
||||
|
||||
### Experimental producer lease
|
||||
|
||||
`DROIDDECK_VR_PRODUCER_LEASE=1` opts the compositor into a private, same-UID,
|
||||
PID-authenticated producer endpoint. It serializes producer queue submissions,
|
||||
inserts real GPU fence markers on every observed producer queue, and grants a
|
||||
lease only after those fences complete. Producer submissions remain frozen
|
||||
until the presenter's GPU readback finishes and the release handshake succeeds.
|
||||
The presenter device is excluded from the producer lock. Multiple producer
|
||||
PIDs in one eye pair are rejected. Pending timeout fences are retained, not
|
||||
destroyed while the GPU can still use them. Losing a granted lease quarantines
|
||||
submissions instead of resuming writes against a potentially incomplete copy;
|
||||
restarting SteamVR is required in that case. The layer remains resident for its
|
||||
process lifetime so its service threads cannot execute unloaded code.
|
||||
|
||||
The protocol regression verifies private sockets, peer PID, completion gating,
|
||||
retention beyond client acquisition deadlines, release acknowledgement and
|
||||
disconnect quarantine. A real Vulkan Lavapipe regression waits for deliberately
|
||||
delayed GPU work, verifies all 1,024 buffer words, blocks a concurrent overwriter
|
||||
through the lease, then verifies its writes after release; it also exercises
|
||||
an outstanding fence through repeated timeouts. CI installs the software Vulkan
|
||||
driver so this is not merely a source-shaped or mocked GPU test.
|
||||
|
||||
This route is intentionally opt-in, not declared complete. Live Quest validation
|
||||
must establish that the swap-set PID identifies the actual final-image writer,
|
||||
audit the compositor's external-memory image-layout/queue-family release
|
||||
contract, and validate the matching acquire/release barriers with real images.
|
||||
The host buffer test does not establish any of those image-sharing properties.
|
||||
Owned-image submission still fails closed without a successfully retained lease.
|
||||
|
||||
The Vulkan layer no longer drops imported binary waits or unsignaled timeline
|
||||
waits after a three-millisecond grace period. Queue submissions and host waits
|
||||
retain their original arguments, deadlines and downstream results. The
|
||||
software fallback supports permanent OPAQUE_FD timeline payloads only; binary,
|
||||
temporary and malformed fallback imports fail explicitly without consuming
|
||||
the caller's descriptor. Native handles still pass through unchanged. Host
|
||||
regressions exercise both submit APIs, pending values, device-loss propagation,
|
||||
65-semaphore host waits, import failures, re-export and descriptor cleanup.
|
||||
These are API-contract tests, not Quest GPU or SteamVR game validation. Native
|
||||
binary SYNC_FD support remains available, but adapting SteamVR's unsupported
|
||||
OPAQUE_FD binary usage still requires a correctly synchronized implementation.
|
||||
|
||||
Steam's ARM64 watchdog also aborts with `futex robust_list not initialized by
|
||||
pthreads`. A captured preload trace showed successful own-thread queries
|
||||
leaving `errno=EFAULT`. The preload now prefers the real kernel query, falls
|
||||
back only for `ENOSYS`, preserves errno on fallback success and safely rejects
|
||||
invalid output pointers. A host regression compares native and fallback
|
||||
results with the kernel's actual head, including a held robust mutex and a
|
||||
second thread, and preserves permission failures. This is not yet a verified
|
||||
fix for Valve's assertion: the Quest disconnected before a new device trace.
|
||||
|
||||
- Android/Linux eye-image bridge and frame ownership/synchronization.
|
||||
- OpenVR driver IPC for predicted 6DoF head, per-eye views and tracked hands.
|
||||
- Real Touch input mapping and haptics returning through the bridge.
|
||||
- Runtime-paced driver timing and render-pose association.
|
||||
- Guest SteamVR dashboard and owned-game validation on Quest.
|
||||
- Sustained timing, memory and thermal measurements; existing semaphore
|
||||
workarounds must be assessed under the real graphics chain.
|
||||
|
||||
References: [Meta scriptable testing](https://developers.meta.com/vr/documentation/native/android/ts-scriptable-testing/),
|
||||
[Khronos Android surface swapchain](https://registry.khronos.org/OpenXR/specs/1.1/man/html/xrCreateSwapchainAndroidSurfaceKHR.html).
|
||||
@@ -0,0 +1,32 @@
|
||||
plugins {
|
||||
id 'com.android.application'
|
||||
}
|
||||
|
||||
android {
|
||||
namespace 'com.droiddeck.questxr'
|
||||
compileSdk 34
|
||||
ndkVersion project.findProperty('ndkVersion') ?: '27.3.13750724'
|
||||
defaultConfig {
|
||||
applicationId 'com.droiddeck.questxr'
|
||||
minSdk 26
|
||||
targetSdk 34
|
||||
versionCode 1
|
||||
versionName '0.1.0'
|
||||
ndk { abiFilters 'arm64-v8a' }
|
||||
externalNativeBuild {
|
||||
cmake { arguments '-DANDROID_STL=c++_static' }
|
||||
}
|
||||
}
|
||||
buildFeatures { prefab true }
|
||||
packagingOptions { jniLibs { useLegacyPackaging = true } }
|
||||
externalNativeBuild {
|
||||
cmake {
|
||||
path 'src/main/cpp/CMakeLists.txt'
|
||||
version '3.22.1'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'org.khronos.openxr:openxr_loader_for_android:1.1.49'
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<uses-feature android:name="android.hardware.vr.headtracking" android:version="1" android:required="true" />
|
||||
<uses-feature android:glEsVersion="0x00030000" android:required="true" />
|
||||
<uses-feature android:name="oculus.software.handtracking" android:required="false" />
|
||||
<uses-feature android:name="android.hardware.vr.controller" android:required="false" />
|
||||
<uses-permission android:name="com.oculus.permission.HAND_TRACKING" />
|
||||
<uses-permission android:name="android.permission.VIBRATE" />
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<application android:label="DroidDeck XR" android:hasCode="false" android:allowBackup="false" android:extractNativeLibs="true">
|
||||
<meta-data android:name="com.oculus.supportedDevices" android:value="quest2|questpro|quest3|quest3s" />
|
||||
<activity android:name="android.app.NativeActivity" android:exported="true" android:launchMode="singleTask"
|
||||
android:resizeableActivity="false" android:screenOrientation="landscape"
|
||||
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden|keyboard|navigation|uiMode"
|
||||
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
|
||||
<meta-data android:name="android.app.lib_name" android:value="droiddeckxr" />
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
<category android:name="com.oculus.intent.category.VR" />
|
||||
<category android:name="org.khronos.openxr.intent.category.IMMERSIVE_HMD" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -0,0 +1,11 @@
|
||||
cmake_minimum_required(VERSION 3.22.1)
|
||||
project(droiddeckxr C CXX)
|
||||
find_package(OpenXR REQUIRED CONFIG)
|
||||
add_library(native_app_glue STATIC ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)
|
||||
target_include_directories(native_app_glue PUBLIC ${ANDROID_NDK}/sources/android/native_app_glue)
|
||||
add_library(droiddeckxr SHARED quest_xr.cpp)
|
||||
target_compile_features(droiddeckxr PRIVATE cxx_std_17)
|
||||
target_compile_definitions(droiddeckxr PRIVATE XR_USE_PLATFORM_ANDROID XR_USE_GRAPHICS_API_OPENGL_ES XR_USE_TIMESPEC)
|
||||
target_compile_options(droiddeckxr PRIVATE -Wall -Wextra -Wno-missing-field-initializers -Werror=return-type)
|
||||
target_link_options(droiddeckxr PRIVATE -Wl,-z,max-page-size=16384 -uANativeActivity_onCreate)
|
||||
target_link_libraries(droiddeckxr PRIVATE native_app_glue OpenXR::openxr_loader EGL GLESv3 android log)
|
||||
@@ -0,0 +1,852 @@
|
||||
#include <jni.h>
|
||||
#include <EGL/egl.h>
|
||||
#include <GLES3/gl3.h>
|
||||
#include <android/log.h>
|
||||
#include <android/native_window_jni.h>
|
||||
#include <android_native_app_glue.h>
|
||||
#include <time.h>
|
||||
#include <openxr/openxr.h>
|
||||
#include <openxr/openxr_platform.h>
|
||||
#include <array>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <chrono>
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <cerrno>
|
||||
#include <cstddef>
|
||||
#include <ctime>
|
||||
#include "../../../../tools/vr/src/xr_tracking.h"
|
||||
#include "../../../../tools/vr/src/xr_frame_channel.h"
|
||||
#include "../../../../tools/vr/src/xr_frame_math.h"
|
||||
|
||||
static void check(XrResult result, const char *operation) {
|
||||
if (XR_FAILED(result)) throw std::runtime_error(std::string(operation) + ": " + std::to_string(result));
|
||||
}
|
||||
|
||||
using Matrix = std::array<float, 16>;
|
||||
|
||||
static Matrix multiply(const Matrix &left, const Matrix &right) {
|
||||
Matrix out{};
|
||||
for (int column = 0; column < 4; column++)
|
||||
for (int row = 0; row < 4; row++)
|
||||
for (int inner = 0; inner < 4; inner++)
|
||||
out[column * 4 + row] += left[inner * 4 + row] * right[column * 4 + inner];
|
||||
return out;
|
||||
}
|
||||
|
||||
static Matrix transform(const XrPosef &pose) {
|
||||
const auto &rotation = pose.orientation;
|
||||
Matrix matrix{
|
||||
1 - 2 * (rotation.y * rotation.y + rotation.z * rotation.z),
|
||||
2 * (rotation.x * rotation.y + rotation.w * rotation.z),
|
||||
2 * (rotation.x * rotation.z - rotation.w * rotation.y), 0,
|
||||
2 * (rotation.x * rotation.y - rotation.w * rotation.z),
|
||||
1 - 2 * (rotation.x * rotation.x + rotation.z * rotation.z),
|
||||
2 * (rotation.y * rotation.z + rotation.w * rotation.x), 0,
|
||||
2 * (rotation.x * rotation.z + rotation.w * rotation.y),
|
||||
2 * (rotation.y * rotation.z - rotation.w * rotation.x),
|
||||
1 - 2 * (rotation.x * rotation.x + rotation.y * rotation.y), 0,
|
||||
pose.position.x, pose.position.y, pose.position.z, 1
|
||||
};
|
||||
return matrix;
|
||||
}
|
||||
|
||||
static Matrix viewProjection(const XrView &view) {
|
||||
XrPosef inverse{};
|
||||
inverse.orientation = {-view.pose.orientation.x, -view.pose.orientation.y,
|
||||
-view.pose.orientation.z, view.pose.orientation.w};
|
||||
Matrix camera = transform(inverse);
|
||||
for (int row = 0; row < 3; row++)
|
||||
camera[12 + row] = -(camera[row] * view.pose.position.x + camera[4 + row] * view.pose.position.y
|
||||
+ camera[8 + row] * view.pose.position.z);
|
||||
float left = std::tan(view.fov.angleLeft), right = std::tan(view.fov.angleRight);
|
||||
float down = std::tan(view.fov.angleDown), up = std::tan(view.fov.angleUp);
|
||||
const float nearPlane = 0.05f, farPlane = 50.f;
|
||||
Matrix projection{};
|
||||
projection[0] = 2 / (right - left);
|
||||
projection[5] = 2 / (up - down);
|
||||
projection[8] = (right + left) / (right - left);
|
||||
projection[9] = (up + down) / (up - down);
|
||||
projection[10] = -(farPlane + nearPlane) / (farPlane - nearPlane);
|
||||
projection[11] = -1;
|
||||
projection[14] = -2 * farPlane * nearPlane / (farPlane - nearPlane);
|
||||
return multiply(projection, camera);
|
||||
}
|
||||
|
||||
struct Eye {
|
||||
XrSwapchain swapchain = XR_NULL_HANDLE;
|
||||
int width = 0, height = 0;
|
||||
std::vector<XrSwapchainImageOpenGLESKHR> images;
|
||||
GLuint depth = 0;
|
||||
};
|
||||
|
||||
class QuestXR {
|
||||
public:
|
||||
explicit QuestXR(android_app *app) : app_(app) {}
|
||||
~QuestXR() { cleanup(); }
|
||||
|
||||
void run() {
|
||||
initialize();
|
||||
while (!app_->destroyRequested && !exit_) {
|
||||
android_poll_source *source = nullptr;
|
||||
int events = 0;
|
||||
int pollTimeout = running_ ? 0 : 100;
|
||||
for (int eventCount = 0; eventCount < 32; eventCount++) {
|
||||
if (ALooper_pollOnce(pollTimeout, nullptr, &events, reinterpret_cast<void **>(&source)) < 0) break;
|
||||
if (source) source->process(app_, source);
|
||||
if (app_->destroyRequested) break;
|
||||
pollTimeout = 0;
|
||||
}
|
||||
receiveFrame();
|
||||
pollEvents();
|
||||
if (running_ && !app_->destroyRequested) frame();
|
||||
}
|
||||
if (exit_ && !app_->destroyRequested) ANativeActivity_finish(app_->activity);
|
||||
}
|
||||
|
||||
void health(const char *error = "") {
|
||||
int steamvrState = -1;
|
||||
if ((guestSurface_ || guestFramesInput_) && statusCallback_) {
|
||||
steamvrState = env_->CallIntMethod(app_->activity->clazz, statusCallback_);
|
||||
if (env_->ExceptionCheck()) { env_->ExceptionClear(); steamvrState = -1; }
|
||||
}
|
||||
std::string path = std::string(app_->activity->externalDataPath) + "/xr-health.json";
|
||||
std::string temp = path + ".tmp";
|
||||
FILE *file = std::fopen(temp.c_str(), "w");
|
||||
if (!file) return;
|
||||
std::fprintf(file,
|
||||
"{\"frames\":%llu,\"rendered\":%llu,\"sessionState\":%d,\"headFlags\":%llu,"
|
||||
"\"headPosition\":[%.6f,%.6f,%.6f],\"controllersActive\":[%s,%s],"
|
||||
"\"periodNs\":%lld,\"surfaceSwapchain\":%s,\"source\":\"%s\",\"guestFrames\":%lld,\"steamvrState\":%d,"
|
||||
"\"receivedFrames\":%llu,\"rejectedFrames\":%llu,\"uploadedFrames\":%llu,\"sourceFrameId\":%llu,"
|
||||
"\"gpuVerifiedFrameId\":%llu,\"gpuChecksum\":%u,\"error\":\"%s\"}\n",
|
||||
(unsigned long long)frames_, (unsigned long long)rendered_, state_,
|
||||
(unsigned long long)head_.locationFlags,
|
||||
head_.pose.position.x, head_.pose.position.y, head_.pose.position.z,
|
||||
handActive_[0] ? "true" : "false", handActive_[1] ? "true" : "false",
|
||||
(long long)period_, surfaceSupported_ ? "true" : "false", frameInput_ ? "frames" : guestSurface_ ? "guest" : surfaceTest_ ? "surface" : "native",
|
||||
(long long)guestFrames_, steamvrState, (unsigned long long)receiver_.Received(), (unsigned long long)receiver_.Rejected(),
|
||||
(unsigned long long)uploadedFrames_, (unsigned long long)sourceHeader_.frame_id,
|
||||
(unsigned long long)verifiedFrame_, gpuChecksum_, error);
|
||||
std::fclose(file);
|
||||
std::rename(temp.c_str(), path.c_str());
|
||||
}
|
||||
|
||||
private:
|
||||
android_app *app_;
|
||||
JNIEnv *env_ = nullptr;
|
||||
XrInstance instance_ = XR_NULL_HANDLE;
|
||||
XrSession session_ = XR_NULL_HANDLE;
|
||||
XrSystemId system_ = XR_NULL_SYSTEM_ID;
|
||||
XrSpace world_ = XR_NULL_HANDLE, view_ = XR_NULL_HANDLE;
|
||||
XrActionSet actions_ = XR_NULL_HANDLE;
|
||||
XrAction grip_ = XR_NULL_HANDLE, trigger_ = XR_NULL_HANDLE, vibration_ = XR_NULL_HANDLE;
|
||||
XrAction squeeze_ = XR_NULL_HANDLE, stick_ = XR_NULL_HANDLE, stickClick_ = XR_NULL_HANDLE;
|
||||
XrAction primary_ = XR_NULL_HANDLE, secondary_ = XR_NULL_HANDLE, menu_ = XR_NULL_HANDLE;
|
||||
XrAction triggerTouch_ = XR_NULL_HANDLE, stickTouch_ = XR_NULL_HANDLE, primaryTouch_ = XR_NULL_HANDLE;
|
||||
XrAction secondaryTouch_ = XR_NULL_HANDLE, thumbrestTouch_ = XR_NULL_HANDLE;
|
||||
DroidDeckXrHand inputs_[2]{};
|
||||
PFN_xrConvertTimeToTimespecTimeKHR convertTime_ = nullptr;
|
||||
bool timespecSupported_ = false;
|
||||
XrPath hands_[2]{};
|
||||
XrSpace handSpaces_[2]{};
|
||||
XrSpaceLocation head_{XR_TYPE_SPACE_LOCATION};
|
||||
XrSpaceLocation handLocations_[2]{{XR_TYPE_SPACE_LOCATION}, {XR_TYPE_SPACE_LOCATION}};
|
||||
bool handActive_[2]{}, pressed_[2]{};
|
||||
bool running_ = false, exit_ = false, surfaceSupported_ = false, quitRequested_ = false;
|
||||
bool surfaceTest_ = false;
|
||||
bool guestSurface_ = false, guestAttached_ = false;
|
||||
bool frameInput_ = false, verifyPixels_ = false, guestFramesInput_ = false;
|
||||
XrFrameReceiver receiver_;
|
||||
DroidDeckXrFrameHeader sourceHeader_{};
|
||||
uint64_t uploadedFrames_ = 0, verifiedFrame_ = 0;
|
||||
uint32_t gpuChecksum_ = 0;
|
||||
GLuint sourceTexture_ = 0, sourceProgram_ = 0;
|
||||
jmethodID surfaceCallback_ = nullptr, frameCallback_ = nullptr;
|
||||
jmethodID activeCallback_ = nullptr;
|
||||
jmethodID statusCallback_ = nullptr;
|
||||
jobject androidJavaSurface_ = nullptr;
|
||||
DroidDeckXrTracking *tracking_ = nullptr;
|
||||
DroidDeckXrHaptics *haptics_ = nullptr;
|
||||
uint64_t lastPulse_[2]{};
|
||||
XrSwapchain androidSwapchain_ = XR_NULL_HANDLE;
|
||||
ANativeWindow *androidWindow_ = nullptr;
|
||||
EGLSurface androidSurface_ = EGL_NO_SURFACE;
|
||||
XrSessionState state_ = XR_SESSION_STATE_UNKNOWN;
|
||||
uint64_t frames_ = 0, rendered_ = 0;
|
||||
int64_t guestFrames_ = 0;
|
||||
XrDuration period_ = 0;
|
||||
EGLDisplay display_ = EGL_NO_DISPLAY;
|
||||
EGLContext context_ = EGL_NO_CONTEXT;
|
||||
EGLSurface surface_ = EGL_NO_SURFACE;
|
||||
std::array<Eye, 2> eyes_;
|
||||
GLuint framebuffer_ = 0, program_ = 0, buffer_ = 0, vertexArray_ = 0;
|
||||
GLint matrixUniform_ = -1, colorUniform_ = -1;
|
||||
|
||||
XrPath path(const char *text) {
|
||||
XrPath result{};
|
||||
check(xrStringToPath(instance_, text, &result), "xrStringToPath");
|
||||
return result;
|
||||
}
|
||||
|
||||
void initialize() {
|
||||
app_->userData = this;
|
||||
app_->onInputEvent = [](android_app *app, AInputEvent *event) -> int32_t {
|
||||
if (AInputEvent_getType(event) != AINPUT_EVENT_TYPE_KEY || AKeyEvent_getKeyCode(event) != AKEYCODE_BACK
|
||||
|| AKeyEvent_getAction(event) != AKEY_EVENT_ACTION_UP) return 0;
|
||||
auto *runtime = static_cast<QuestXR *>(app->userData);
|
||||
if (runtime->running_) {
|
||||
XrResult result = xrRequestExitSession(runtime->session_);
|
||||
if (XR_SUCCEEDED(result)) runtime->quitRequested_ = true;
|
||||
else __android_log_print(ANDROID_LOG_ERROR, "DroidDeckXR", "exit request failed: %d", result);
|
||||
} else ANativeActivity_finish(app->activity);
|
||||
return 1;
|
||||
};
|
||||
app_->activity->vm->AttachCurrentThread(&env_, nullptr);
|
||||
jclass activityClass = env_->GetObjectClass(app_->activity->clazz);
|
||||
jmethodID getIntent = env_->GetMethodID(activityClass, "getIntent", "()Landroid/content/Intent;");
|
||||
jobject intent = env_->CallObjectMethod(app_->activity->clazz, getIntent);
|
||||
jclass intentClass = env_->GetObjectClass(intent);
|
||||
jmethodID getBoolean = env_->GetMethodID(intentClass, "getBooleanExtra", "(Ljava/lang/String;Z)Z");
|
||||
jstring extra = env_->NewStringUTF("surface_test");
|
||||
surfaceTest_ = env_->CallBooleanMethod(intent, getBoolean, extra, JNI_FALSE);
|
||||
env_->DeleteLocalRef(extra);
|
||||
extra = env_->NewStringUTF("frame_input");
|
||||
frameInput_ = env_->CallBooleanMethod(intent, getBoolean, extra, JNI_FALSE);
|
||||
env_->DeleteLocalRef(extra);
|
||||
extra = env_->NewStringUTF("frame_verify");
|
||||
verifyPixels_ = env_->CallBooleanMethod(intent, getBoolean, extra, JNI_FALSE);
|
||||
env_->DeleteLocalRef(extra);
|
||||
extra = env_->NewStringUTF("guest_surface");
|
||||
guestSurface_ = env_->CallBooleanMethod(intent, getBoolean, extra, JNI_FALSE);
|
||||
env_->DeleteLocalRef(extra);
|
||||
extra = env_->NewStringUTF("guest_frames");
|
||||
guestFramesInput_ = env_->CallBooleanMethod(intent, getBoolean, extra, JNI_FALSE);
|
||||
if (guestFramesInput_) frameInput_ = true;
|
||||
if (guestSurface_ || guestFramesInput_) {
|
||||
if (guestSurface_) {
|
||||
surfaceTest_ = true;
|
||||
surfaceCallback_ = env_->GetMethodID(activityClass, "onXrSurface", "(Landroid/view/Surface;II)V");
|
||||
} else activeCallback_ = env_->GetMethodID(activityClass, "onXrActive", "(Z)V");
|
||||
frameCallback_ = env_->GetMethodID(activityClass, "onXrFrame", "(J)J");
|
||||
statusCallback_ = env_->GetMethodID(activityClass, "onXrStatus", "()I");
|
||||
if (env_->ExceptionCheck()) { env_->ExceptionClear(); throw std::runtime_error("XR activity bridge methods unavailable"); }
|
||||
}
|
||||
if (frameInput_ && (surfaceTest_ || guestSurface_)) throw std::runtime_error("Frame input cannot use an asynchronous surface projection");
|
||||
env_->DeleteLocalRef(extra); env_->DeleteLocalRef(intentClass);
|
||||
env_->DeleteLocalRef(intent); env_->DeleteLocalRef(activityClass);
|
||||
PFN_xrInitializeLoaderKHR initializeLoader = nullptr;
|
||||
check(xrGetInstanceProcAddr(XR_NULL_HANDLE, "xrInitializeLoaderKHR",
|
||||
reinterpret_cast<PFN_xrVoidFunction *>(&initializeLoader)), "loader init entrypoint");
|
||||
XrLoaderInitInfoAndroidKHR loader{XR_TYPE_LOADER_INIT_INFO_ANDROID_KHR};
|
||||
loader.applicationVM = app_->activity->vm;
|
||||
loader.applicationContext = app_->activity->clazz;
|
||||
check(initializeLoader(reinterpret_cast<XrLoaderInitInfoBaseHeaderKHR *>(&loader)), "xrInitializeLoaderKHR");
|
||||
uint32_t count = 0;
|
||||
check(xrEnumerateInstanceExtensionProperties(nullptr, 0, &count, nullptr), "extensions count");
|
||||
std::vector<XrExtensionProperties> extensions(count, {XR_TYPE_EXTENSION_PROPERTIES});
|
||||
check(xrEnumerateInstanceExtensionProperties(nullptr, count, &count, extensions.data()), "extensions");
|
||||
for (const auto &extension : extensions) {
|
||||
if (!std::strcmp(extension.extensionName, XR_KHR_ANDROID_SURFACE_SWAPCHAIN_EXTENSION_NAME))
|
||||
surfaceSupported_ = true;
|
||||
if (!std::strcmp(extension.extensionName, XR_KHR_CONVERT_TIMESPEC_TIME_EXTENSION_NAME))
|
||||
timespecSupported_ = true;
|
||||
}
|
||||
if (surfaceTest_ && !surfaceSupported_) throw std::runtime_error("Android surface swapchains unavailable");
|
||||
std::vector<const char *> enabled{XR_KHR_ANDROID_CREATE_INSTANCE_EXTENSION_NAME, XR_KHR_OPENGL_ES_ENABLE_EXTENSION_NAME};
|
||||
if (surfaceSupported_) enabled.push_back(XR_KHR_ANDROID_SURFACE_SWAPCHAIN_EXTENSION_NAME);
|
||||
if (timespecSupported_) enabled.push_back(XR_KHR_CONVERT_TIMESPEC_TIME_EXTENSION_NAME);
|
||||
XrInstanceCreateInfoAndroidKHR androidInfo{XR_TYPE_INSTANCE_CREATE_INFO_ANDROID_KHR};
|
||||
androidInfo.applicationVM = app_->activity->vm;
|
||||
androidInfo.applicationActivity = app_->activity->clazz;
|
||||
XrInstanceCreateInfo create{XR_TYPE_INSTANCE_CREATE_INFO};
|
||||
create.next = &androidInfo;
|
||||
std::strcpy(create.applicationInfo.applicationName, "DroidDeck XR");
|
||||
create.applicationInfo.applicationVersion = 1;
|
||||
create.applicationInfo.apiVersion = XR_MAKE_VERSION(1, 0, 0);
|
||||
create.enabledExtensionCount = enabled.size();
|
||||
create.enabledExtensionNames = enabled.data();
|
||||
check(xrCreateInstance(&create, &instance_), "xrCreateInstance");
|
||||
if (timespecSupported_) check(xrGetInstanceProcAddr(instance_, "xrConvertTimeToTimespecTimeKHR",
|
||||
reinterpret_cast<PFN_xrVoidFunction *>(&convertTime_)), "time conversion entrypoint");
|
||||
XrSystemGetInfo systemInfo{XR_TYPE_SYSTEM_GET_INFO};
|
||||
systemInfo.formFactor = XR_FORM_FACTOR_HEAD_MOUNTED_DISPLAY;
|
||||
check(xrGetSystem(instance_, &systemInfo, &system_), "xrGetSystem");
|
||||
PFN_xrGetOpenGLESGraphicsRequirementsKHR requirementsFunction = nullptr;
|
||||
check(xrGetInstanceProcAddr(instance_, "xrGetOpenGLESGraphicsRequirementsKHR",
|
||||
reinterpret_cast<PFN_xrVoidFunction *>(&requirementsFunction)), "GLES requirements entrypoint");
|
||||
XrGraphicsRequirementsOpenGLESKHR requirements{XR_TYPE_GRAPHICS_REQUIREMENTS_OPENGL_ES_KHR};
|
||||
check(requirementsFunction(instance_, system_, &requirements), "GLES requirements");
|
||||
if (requirements.minApiVersionSupported > XR_MAKE_VERSION(3, 0, 0)
|
||||
|| requirements.maxApiVersionSupported < XR_MAKE_VERSION(3, 0, 0))
|
||||
throw std::runtime_error("Runtime does not support GLES 3.0");
|
||||
display_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
||||
if (!eglInitialize(display_, nullptr, nullptr)) throw std::runtime_error("eglInitialize failed");
|
||||
const EGLint attributes[] = {EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT, EGL_SURFACE_TYPE, EGL_PBUFFER_BIT | EGL_WINDOW_BIT,
|
||||
EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_DEPTH_SIZE, 24, EGL_NONE};
|
||||
EGLConfig config{};
|
||||
EGLint found = 0;
|
||||
if (!eglChooseConfig(display_, attributes, &config, 1, &found) || found != 1)
|
||||
throw std::runtime_error("EGL config unavailable");
|
||||
const EGLint contextAttributes[] = {EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE};
|
||||
context_ = eglCreateContext(display_, config, EGL_NO_CONTEXT, contextAttributes);
|
||||
const EGLint surfaceAttributes[] = {EGL_WIDTH, 16, EGL_HEIGHT, 16, EGL_NONE};
|
||||
surface_ = eglCreatePbufferSurface(display_, config, surfaceAttributes);
|
||||
if (!eglMakeCurrent(display_, surface_, surface_, context_)) throw std::runtime_error("eglMakeCurrent failed");
|
||||
XrGraphicsBindingOpenGLESAndroidKHR binding{XR_TYPE_GRAPHICS_BINDING_OPENGL_ES_ANDROID_KHR};
|
||||
binding.display = display_; binding.config = config; binding.context = context_;
|
||||
XrSessionCreateInfo sessionInfo{XR_TYPE_SESSION_CREATE_INFO};
|
||||
sessionInfo.next = &binding; sessionInfo.systemId = system_;
|
||||
check(xrCreateSession(instance_, &sessionInfo, &session_), "xrCreateSession");
|
||||
XrReferenceSpaceCreateInfo reference{XR_TYPE_REFERENCE_SPACE_CREATE_INFO};
|
||||
reference.poseInReferenceSpace.orientation.w = 1;
|
||||
reference.referenceSpaceType = XR_REFERENCE_SPACE_TYPE_STAGE;
|
||||
XrResult referenceResult = xrCreateReferenceSpace(session_, &reference, &world_);
|
||||
if (referenceResult == XR_ERROR_REFERENCE_SPACE_UNSUPPORTED) {
|
||||
reference.referenceSpaceType = XR_REFERENCE_SPACE_TYPE_LOCAL;
|
||||
reference.poseInReferenceSpace.position.y = -1.6f;
|
||||
check(xrCreateReferenceSpace(session_, &reference, &world_), "local standing space");
|
||||
} else check(referenceResult, "stage space");
|
||||
reference.referenceSpaceType = XR_REFERENCE_SPACE_TYPE_VIEW;
|
||||
reference.poseInReferenceSpace.position.y = 0;
|
||||
check(xrCreateReferenceSpace(session_, &reference, &view_), "view space");
|
||||
initializeActions();
|
||||
initializeImages();
|
||||
if (surfaceTest_) initializeAndroidSurface(config);
|
||||
initializeGraphics();
|
||||
initializeTracking();
|
||||
if (frameInput_) {
|
||||
std::string filename = std::string(app_->activity->internalDataPath) + "/.wayland-rt/" DROIDDECK_XR_FRAME_SOCKET;
|
||||
if (!receiver_.Start(filename)) throw std::runtime_error("XR frame receiver unavailable");
|
||||
}
|
||||
__android_log_print(ANDROID_LOG_INFO, "DroidDeckXR", "initialized GLES; Android surface swapchain supported=%d", surfaceSupported_);
|
||||
health();
|
||||
}
|
||||
|
||||
void initializeActions() {
|
||||
hands_[0] = path("/user/hand/left"); hands_[1] = path("/user/hand/right");
|
||||
XrActionSetCreateInfo setInfo{XR_TYPE_ACTION_SET_CREATE_INFO};
|
||||
std::strcpy(setInfo.actionSetName, "quest"); std::strcpy(setInfo.localizedActionSetName, "Quest controllers");
|
||||
check(xrCreateActionSet(instance_, &setInfo, &actions_), "action set");
|
||||
auto action = [&](const char *name, XrActionType type, XrAction *handle) {
|
||||
XrActionCreateInfo info{XR_TYPE_ACTION_CREATE_INFO};
|
||||
std::strcpy(info.actionName, name); std::strcpy(info.localizedActionName, name);
|
||||
info.actionType = type; info.countSubactionPaths = 2; info.subactionPaths = hands_;
|
||||
check(xrCreateAction(actions_, &info, handle), "create action");
|
||||
};
|
||||
action("grip_pose", XR_ACTION_TYPE_POSE_INPUT, &grip_);
|
||||
action("trigger", XR_ACTION_TYPE_FLOAT_INPUT, &trigger_);
|
||||
action("squeeze", XR_ACTION_TYPE_FLOAT_INPUT, &squeeze_);
|
||||
action("stick", XR_ACTION_TYPE_VECTOR2F_INPUT, &stick_);
|
||||
action("stick_click", XR_ACTION_TYPE_BOOLEAN_INPUT, &stickClick_);
|
||||
action("primary", XR_ACTION_TYPE_BOOLEAN_INPUT, &primary_);
|
||||
action("secondary", XR_ACTION_TYPE_BOOLEAN_INPUT, &secondary_);
|
||||
action("menu", XR_ACTION_TYPE_BOOLEAN_INPUT, &menu_);
|
||||
action("trigger_touch", XR_ACTION_TYPE_BOOLEAN_INPUT, &triggerTouch_);
|
||||
action("stick_touch", XR_ACTION_TYPE_BOOLEAN_INPUT, &stickTouch_);
|
||||
action("primary_touch", XR_ACTION_TYPE_BOOLEAN_INPUT, &primaryTouch_);
|
||||
action("secondary_touch", XR_ACTION_TYPE_BOOLEAN_INPUT, &secondaryTouch_);
|
||||
action("thumbrest_touch", XR_ACTION_TYPE_BOOLEAN_INPUT, &thumbrestTouch_);
|
||||
action("vibration", XR_ACTION_TYPE_VIBRATION_OUTPUT, &vibration_);
|
||||
std::vector<XrActionSuggestedBinding> bindings;
|
||||
for (const char *hand : {"left", "right"}) {
|
||||
std::string root = std::string("/user/hand/") + hand;
|
||||
bindings.push_back({grip_, path((root + "/input/grip/pose").c_str())});
|
||||
bindings.push_back({trigger_, path((root + "/input/trigger/value").c_str())});
|
||||
bindings.push_back({triggerTouch_, path((root + "/input/trigger/touch").c_str())});
|
||||
bindings.push_back({squeeze_, path((root + "/input/squeeze/value").c_str())});
|
||||
bindings.push_back({stick_, path((root + "/input/thumbstick").c_str())});
|
||||
bindings.push_back({stickClick_, path((root + "/input/thumbstick/click").c_str())});
|
||||
bindings.push_back({stickTouch_, path((root + "/input/thumbstick/touch").c_str())});
|
||||
bindings.push_back({thumbrestTouch_, path((root + "/input/thumbrest/touch").c_str())});
|
||||
bindings.push_back({primary_, path((root + (std::strcmp(hand, "left") == 0 ? "/input/x/click" : "/input/a/click")).c_str())});
|
||||
bindings.push_back({secondary_, path((root + (std::strcmp(hand, "left") == 0 ? "/input/y/click" : "/input/b/click")).c_str())});
|
||||
bindings.push_back({primaryTouch_, path((root + (std::strcmp(hand, "left") == 0 ? "/input/x/touch" : "/input/a/touch")).c_str())});
|
||||
bindings.push_back({secondaryTouch_, path((root + (std::strcmp(hand, "left") == 0 ? "/input/y/touch" : "/input/b/touch")).c_str())});
|
||||
bindings.push_back({vibration_, path((root + "/output/haptic").c_str())});
|
||||
}
|
||||
bindings.push_back({menu_, path("/user/hand/left/input/menu/click")});
|
||||
XrInteractionProfileSuggestedBinding suggestion{XR_TYPE_INTERACTION_PROFILE_SUGGESTED_BINDING};
|
||||
suggestion.interactionProfile = path("/interaction_profiles/oculus/touch_controller");
|
||||
suggestion.countSuggestedBindings = bindings.size(); suggestion.suggestedBindings = bindings.data();
|
||||
check(xrSuggestInteractionProfileBindings(instance_, &suggestion), "Touch bindings");
|
||||
XrSessionActionSetsAttachInfo attach{XR_TYPE_SESSION_ACTION_SETS_ATTACH_INFO};
|
||||
attach.countActionSets = 1; attach.actionSets = &actions_;
|
||||
check(xrAttachSessionActionSets(session_, &attach), "attach actions");
|
||||
for (int hand = 0; hand < 2; hand++) {
|
||||
XrActionSpaceCreateInfo space{XR_TYPE_ACTION_SPACE_CREATE_INFO};
|
||||
space.action = grip_; space.subactionPath = hands_[hand]; space.poseInActionSpace.orientation.w = 1;
|
||||
check(xrCreateActionSpace(session_, &space, &handSpaces_[hand]), "controller space");
|
||||
}
|
||||
}
|
||||
|
||||
void initializeImages() {
|
||||
uint32_t count = 0;
|
||||
check(xrEnumerateViewConfigurationViews(instance_, system_, XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO,
|
||||
0, &count, nullptr), "view count");
|
||||
if (count != 2) throw std::runtime_error("Expected two stereo views");
|
||||
std::vector<XrViewConfigurationView> views(count, {XR_TYPE_VIEW_CONFIGURATION_VIEW});
|
||||
check(xrEnumerateViewConfigurationViews(instance_, system_, XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO,
|
||||
count, &count, views.data()), "view configuration");
|
||||
check(xrEnumerateSwapchainFormats(session_, 0, &count, nullptr), "format count");
|
||||
std::vector<int64_t> formats(count);
|
||||
check(xrEnumerateSwapchainFormats(session_, count, &count, formats.data()), "formats");
|
||||
int64_t format = 0;
|
||||
for (int64_t candidate : formats) if (candidate == GL_SRGB8_ALPHA8) format = candidate;
|
||||
if (!format) throw std::runtime_error("sRGB swapchain unavailable");
|
||||
for (int eye = 0; eye < 2; eye++) {
|
||||
Eye &target = eyes_[eye];
|
||||
target.width = views[eye].recommendedImageRectWidth;
|
||||
target.height = views[eye].recommendedImageRectHeight;
|
||||
if (surfaceTest_) continue;
|
||||
XrSwapchainCreateInfo create{XR_TYPE_SWAPCHAIN_CREATE_INFO};
|
||||
create.usageFlags = XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT;
|
||||
create.format = format; create.sampleCount = 1;
|
||||
create.width = target.width; create.height = target.height;
|
||||
create.faceCount = 1; create.arraySize = 1; create.mipCount = 1;
|
||||
check(xrCreateSwapchain(session_, &create, &target.swapchain), "swapchain");
|
||||
check(xrEnumerateSwapchainImages(target.swapchain, 0, &count, nullptr), "image count");
|
||||
target.images.resize(count, {XR_TYPE_SWAPCHAIN_IMAGE_OPENGL_ES_KHR});
|
||||
check(xrEnumerateSwapchainImages(target.swapchain, count, &count,
|
||||
reinterpret_cast<XrSwapchainImageBaseHeader *>(target.images.data())), "images");
|
||||
glGenRenderbuffers(1, &target.depth);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, target.depth);
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, target.width, target.height);
|
||||
}
|
||||
}
|
||||
|
||||
void initializeAndroidSurface(EGLConfig config) {
|
||||
PFN_xrCreateSwapchainAndroidSurfaceKHR createSurface = nullptr;
|
||||
check(xrGetInstanceProcAddr(instance_, "xrCreateSwapchainAndroidSurfaceKHR",
|
||||
reinterpret_cast<PFN_xrVoidFunction *>(&createSurface)), "surface swapchain entrypoint");
|
||||
if (eyes_[0].width != eyes_[1].width || eyes_[0].height != eyes_[1].height)
|
||||
throw std::runtime_error("Surface path requires matching eye dimensions");
|
||||
XrSwapchainCreateInfo info{XR_TYPE_SWAPCHAIN_CREATE_INFO};
|
||||
info.usageFlags = XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT;
|
||||
info.width = eyes_[0].width * 2; info.height = eyes_[0].height;
|
||||
jobject surface = nullptr;
|
||||
check(createSurface(session_, &info, &androidSwapchain_, &surface), "Android surface swapchain");
|
||||
androidWindow_ = ANativeWindow_fromSurface(env_, surface);
|
||||
androidJavaSurface_ = env_->NewGlobalRef(surface);
|
||||
if (env_->GetObjectRefType(surface) == JNILocalRefType) env_->DeleteLocalRef(surface);
|
||||
if (!androidWindow_) throw std::runtime_error("Android swapchain has no native window");
|
||||
if (!guestSurface_) {
|
||||
androidSurface_ = eglCreateWindowSurface(display_, config, androidWindow_, nullptr);
|
||||
if (androidSurface_ == EGL_NO_SURFACE) throw std::runtime_error("Android EGL window surface failed");
|
||||
}
|
||||
__android_log_print(ANDROID_LOG_INFO, "DroidDeckXR", "Android projection surface %ux%u", info.width, info.height);
|
||||
}
|
||||
|
||||
void initializeTracking() {
|
||||
std::string directory = std::string(app_->activity->internalDataPath) + "/.wayland-rt";
|
||||
if (mkdir(directory.c_str(), 0700) != 0 && errno != EEXIST)
|
||||
throw std::runtime_error("XR tracking directory unavailable");
|
||||
auto mapFile = [&](const char *name, size_t size) {
|
||||
std::string filename = directory + "/" + name;
|
||||
int descriptor = open(filename.c_str(), O_CREAT | O_RDWR | O_CLOEXEC, 0600);
|
||||
if (descriptor < 0) throw std::runtime_error("XR IPC file unavailable");
|
||||
if (ftruncate(descriptor, size) != 0) {
|
||||
close(descriptor); throw std::runtime_error("XR IPC file size failed");
|
||||
}
|
||||
void *mapping = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, descriptor, 0);
|
||||
close(descriptor);
|
||||
if (mapping == MAP_FAILED) throw std::runtime_error("XR IPC map failed");
|
||||
return mapping;
|
||||
};
|
||||
tracking_ = static_cast<DroidDeckXrTracking *>(mapFile(DROIDDECK_XR_FILE, sizeof(DroidDeckXrTracking)));
|
||||
haptics_ = static_cast<DroidDeckXrHaptics *>(mapFile(DROIDDECK_XR_HAPTIC_FILE, sizeof(DroidDeckXrHaptics)));
|
||||
haptics_->magic = DROIDDECK_XR_HAPTIC_MAGIC; haptics_->version = DROIDDECK_XR_VERSION;
|
||||
for (int hand = 0; hand < 2; hand++) lastPulse_[hand] = haptics_->hands[hand].id;
|
||||
}
|
||||
|
||||
static DroidDeckXrPose posePacket(const XrPosef &pose, XrSpaceLocationFlags flags) {
|
||||
return {{pose.orientation.x, pose.orientation.y, pose.orientation.z, pose.orientation.w},
|
||||
{pose.position.x, pose.position.y, pose.position.z}, (uint32_t)flags};
|
||||
}
|
||||
|
||||
void publishTracking(const XrView views[2], XrTime predicted) {
|
||||
if (!tracking_) return;
|
||||
timespec now{}; clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
DroidDeckXrTracking packet{};
|
||||
packet.magic = DROIDDECK_XR_MAGIC; packet.version = DROIDDECK_XR_VERSION;
|
||||
packet.active = state_ == XR_SESSION_STATE_VISIBLE || state_ == XR_SESSION_STATE_FOCUSED;
|
||||
packet.sampled_ns = (int64_t)now.tv_sec * 1000000000LL + now.tv_nsec;
|
||||
if (convertTime_) {
|
||||
timespec displayTime{};
|
||||
check(convertTime_(instance_, predicted, &displayTime), "display time conversion");
|
||||
packet.predicted_ns = (int64_t)displayTime.tv_sec * 1000000000LL + displayTime.tv_nsec;
|
||||
}
|
||||
packet.period_ns = period_;
|
||||
packet.head = posePacket(head_.pose, head_.locationFlags);
|
||||
for (int eye = 0; eye < 2; eye++) {
|
||||
packet.eyes[eye].pose = posePacket(views[eye].pose, head_.locationFlags);
|
||||
packet.eyes[eye].fov[0] = views[eye].fov.angleLeft;
|
||||
packet.eyes[eye].fov[1] = views[eye].fov.angleRight;
|
||||
packet.eyes[eye].fov[2] = views[eye].fov.angleUp;
|
||||
packet.eyes[eye].fov[3] = views[eye].fov.angleDown;
|
||||
packet.eyes[eye].width = eyes_[eye].width; packet.eyes[eye].height = eyes_[eye].height;
|
||||
packet.hands[eye] = inputs_[eye];
|
||||
packet.hands[eye].pose = posePacket(handLocations_[eye].pose, handLocations_[eye].locationFlags);
|
||||
packet.hands[eye].active = handActive_[eye];
|
||||
}
|
||||
uint32_t sequence = __atomic_load_n(&tracking_->seq, __ATOMIC_RELAXED);
|
||||
__atomic_store_n(&tracking_->seq, sequence | 1u, __ATOMIC_SEQ_CST);
|
||||
__atomic_thread_fence(__ATOMIC_SEQ_CST);
|
||||
std::memcpy(tracking_, &packet, offsetof(DroidDeckXrTracking, seq));
|
||||
std::memcpy(reinterpret_cast<char *>(tracking_) + offsetof(DroidDeckXrTracking, active),
|
||||
reinterpret_cast<const char *>(&packet) + offsetof(DroidDeckXrTracking, active),
|
||||
sizeof(packet) - offsetof(DroidDeckXrTracking, active));
|
||||
__atomic_store_n(&tracking_->seq, (sequence | 1u) + 1u, __ATOMIC_RELEASE);
|
||||
}
|
||||
|
||||
void attachGuest(bool attach) {
|
||||
if ((!guestSurface_ && !guestFramesInput_) || attach == guestAttached_) return;
|
||||
if (guestFramesInput_) env_->CallVoidMethod(app_->activity->clazz, activeCallback_, static_cast<jboolean>(attach));
|
||||
else env_->CallVoidMethod(app_->activity->clazz, surfaceCallback_, attach ? androidJavaSurface_ : nullptr,
|
||||
eyes_[0].width * 2, eyes_[0].height);
|
||||
if (env_->ExceptionCheck()) {
|
||||
env_->ExceptionDescribe(); env_->ExceptionClear(); throw std::runtime_error("Guest surface handshake failed");
|
||||
}
|
||||
guestAttached_ = attach;
|
||||
}
|
||||
|
||||
void pollHaptics() {
|
||||
if (!haptics_) return;
|
||||
DroidDeckXrHaptics packet{};
|
||||
uint32_t sequence = __atomic_load_n(&haptics_->seq, __ATOMIC_ACQUIRE);
|
||||
if (sequence & 1u) return;
|
||||
std::memcpy(&packet, haptics_, sizeof(packet));
|
||||
__atomic_thread_fence(__ATOMIC_ACQUIRE);
|
||||
if (__atomic_load_n(&haptics_->seq, __ATOMIC_RELAXED) != sequence
|
||||
|| packet.magic != DROIDDECK_XR_HAPTIC_MAGIC || packet.version != DROIDDECK_XR_VERSION) return;
|
||||
for (int hand = 0; hand < 2; hand++) {
|
||||
const auto &pulse = packet.hands[hand];
|
||||
if (pulse.id == lastPulse_[hand]) continue;
|
||||
lastPulse_[hand] = pulse.id;
|
||||
if (state_ != XR_SESSION_STATE_FOCUSED || !handActive_[hand]
|
||||
|| pulse.duration_ns < 1000000 || pulse.duration_ns > 1000000000
|
||||
|| !std::isfinite(pulse.amplitude) || pulse.amplitude < 0 || pulse.amplitude > 1
|
||||
|| !std::isfinite(pulse.frequency) || pulse.frequency < 0) continue;
|
||||
XrHapticActionInfo info{XR_TYPE_HAPTIC_ACTION_INFO}; info.action = vibration_; info.subactionPath = hands_[hand];
|
||||
XrHapticVibration vibration{XR_TYPE_HAPTIC_VIBRATION};
|
||||
vibration.duration = pulse.duration_ns; vibration.amplitude = pulse.amplitude; vibration.frequency = pulse.frequency;
|
||||
check(xrApplyHapticFeedback(session_, &info, reinterpret_cast<XrHapticBaseHeader *>(&vibration)), "guest haptics");
|
||||
}
|
||||
}
|
||||
|
||||
GLuint shader(GLenum type, const char *text) {
|
||||
GLuint handle = glCreateShader(type);
|
||||
glShaderSource(handle, 1, &text, nullptr); glCompileShader(handle);
|
||||
GLint success = 0; glGetShaderiv(handle, GL_COMPILE_STATUS, &success);
|
||||
if (!success) { glDeleteShader(handle); throw std::runtime_error("Shader compile failed"); }
|
||||
return handle;
|
||||
}
|
||||
|
||||
void initializeGraphics() {
|
||||
GLuint vertex = shader(GL_VERTEX_SHADER,
|
||||
"#version 300 es\nlayout(location=0) in vec3 position; uniform mat4 matrix; void main(){gl_Position=matrix*vec4(position,1.0);}");
|
||||
GLuint fragment = shader(GL_FRAGMENT_SHADER,
|
||||
"#version 300 es\nprecision mediump float; uniform vec3 color; out vec4 outputColor; void main(){outputColor=vec4(color,1.0);}");
|
||||
program_ = glCreateProgram(); glAttachShader(program_, vertex); glAttachShader(program_, fragment);
|
||||
glLinkProgram(program_); glDeleteShader(vertex); glDeleteShader(fragment);
|
||||
GLint linked = 0; glGetProgramiv(program_, GL_LINK_STATUS, &linked);
|
||||
if (!linked) throw std::runtime_error("Shader link failed");
|
||||
matrixUniform_ = glGetUniformLocation(program_, "matrix"); colorUniform_ = glGetUniformLocation(program_, "color");
|
||||
const float vertices[] = {
|
||||
-1,-1,-1, 1,-1,-1, 1,1,-1, -1,-1,-1, 1,1,-1, -1,1,-1,
|
||||
-1,-1,1, 1,1,1, 1,-1,1, -1,-1,1, -1,1,1, 1,1,1,
|
||||
-1,-1,-1, -1,1,-1, -1,1,1, -1,-1,-1, -1,1,1, -1,-1,1,
|
||||
1,-1,-1, 1,-1,1, 1,1,1, 1,-1,-1, 1,1,1, 1,1,-1,
|
||||
-1,-1,-1, -1,-1,1, 1,-1,1, -1,-1,-1, 1,-1,1, 1,-1,-1,
|
||||
-1,1,-1, 1,1,-1, 1,1,1, -1,1,-1, 1,1,1, -1,1,1};
|
||||
glGenVertexArrays(1, &vertexArray_); glBindVertexArray(vertexArray_);
|
||||
glGenBuffers(1, &buffer_); glBindBuffer(GL_ARRAY_BUFFER, buffer_);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr); glEnableVertexAttribArray(0);
|
||||
glGenFramebuffers(1, &framebuffer_);
|
||||
if (frameInput_) {
|
||||
vertex = shader(GL_VERTEX_SHADER,
|
||||
"#version 300 es\nout vec2 uv; uniform float eye; void main(){vec2 coordinate=vec2(float((gl_VertexID<<1)&2),float(gl_VertexID&2));uv=vec2(coordinate.x*0.5+eye*0.5,1.0-coordinate.y);gl_Position=vec4(coordinate*2.0-1.0,0.0,1.0);}");
|
||||
fragment = shader(GL_FRAGMENT_SHADER,
|
||||
"#version 300 es\nprecision highp float; in vec2 uv; uniform sampler2D source; uniform float eye; out vec4 outputColor; void main(){float edge=0.5/float(textureSize(source,0).x);vec2 coordinate=vec2(clamp(uv.x,eye*0.5+edge,(eye+1.0)*0.5-edge),uv.y);outputColor=texture(source,coordinate);}");
|
||||
sourceProgram_ = glCreateProgram(); glAttachShader(sourceProgram_, vertex); glAttachShader(sourceProgram_, fragment);
|
||||
glLinkProgram(sourceProgram_); glDeleteShader(vertex); glDeleteShader(fragment);
|
||||
glGetProgramiv(sourceProgram_, GL_LINK_STATUS, &linked);
|
||||
if (!linked) throw std::runtime_error("Frame shader link failed");
|
||||
glGenTextures(1, &sourceTexture_); glBindTexture(GL_TEXTURE_2D, sourceTexture_);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
}
|
||||
}
|
||||
|
||||
void receiveFrame() {
|
||||
if (!frameInput_) return;
|
||||
DroidDeckXrPixels frame;
|
||||
if (!receiver_.Take(&frame)) return;
|
||||
GLint maximum = 0; glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maximum);
|
||||
if (frame.header.width > static_cast<uint32_t>(maximum) || frame.header.height > static_cast<uint32_t>(maximum))
|
||||
throw std::runtime_error("XR source exceeds GPU texture limit");
|
||||
glBindTexture(GL_TEXTURE_2D, sourceTexture_);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, frame.header.width, frame.header.height, 0,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, frame.pixels.data());
|
||||
if (verifyPixels_) {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_);
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, sourceTexture_, 0);
|
||||
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
||||
throw std::runtime_error("Frame readback framebuffer incomplete");
|
||||
std::vector<unsigned char> readback(frame.header.bytes);
|
||||
glReadPixels(0, 0, frame.header.width, frame.header.height, GL_RGBA, GL_UNSIGNED_BYTE, readback.data());
|
||||
if (readback != frame.pixels) throw std::runtime_error("Frame GPU readback does not match owned pixels");
|
||||
gpuChecksum_ = 2166136261u;
|
||||
for (unsigned char value : readback) gpuChecksum_ = (gpuChecksum_ ^ value) * 16777619u;
|
||||
verifiedFrame_ = frame.header.frame_id;
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
if (glGetError() != GL_NO_ERROR) throw std::runtime_error("Frame texture upload failed");
|
||||
sourceHeader_ = frame.header;
|
||||
uploadedFrames_++;
|
||||
if (verifyPixels_ || uploadedFrames_ % 72 == 1) health();
|
||||
}
|
||||
|
||||
void cube(const Matrix &camera, const XrPosef &pose, float size, float red, float green, float blue) {
|
||||
Matrix model = transform(pose);
|
||||
for (int column = 0; column < 3; column++)
|
||||
for (int row = 0; row < 3; row++) model[column * 4 + row] *= size;
|
||||
Matrix matrix = multiply(camera, model);
|
||||
glUniformMatrix4fv(matrixUniform_, 1, GL_FALSE, matrix.data());
|
||||
glUniform3f(colorUniform_, red, green, blue); glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
}
|
||||
|
||||
void render(const XrView &view, Eye &eye, uint32_t image) {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, eye.images[image].image, 0);
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, eye.depth);
|
||||
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
||||
throw std::runtime_error("Incomplete eye framebuffer");
|
||||
if (frameInput_) {
|
||||
glViewport(0, 0, eye.width, eye.height); glDisable(GL_DEPTH_TEST);
|
||||
glUseProgram(sourceProgram_); glBindVertexArray(vertexArray_);
|
||||
glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, sourceTexture_);
|
||||
glUniform1i(glGetUniformLocation(sourceProgram_, "source"), 0);
|
||||
glUniform1f(glGetUniformLocation(sourceProgram_, "eye"), &eye == &eyes_[1] ? 1.f : 0.f);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
} else scene(view, eye.width, eye.height, 0);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
if (glGetError() != GL_NO_ERROR) throw std::runtime_error("OpenGL render error");
|
||||
glFlush();
|
||||
}
|
||||
|
||||
void scene(const XrView &view, int width, int height, int offset) {
|
||||
glViewport(offset, 0, width, height);
|
||||
glEnable(GL_SCISSOR_TEST); glScissor(offset, 0, width, height);
|
||||
glEnable(GL_DEPTH_TEST); glClearColor(0.015f, 0.025f, 0.06f, 1);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glUseProgram(program_); glBindVertexArray(vertexArray_);
|
||||
Matrix camera = viewProjection(view);
|
||||
XrPosef pose{}; pose.orientation.w = 1;
|
||||
for (int column = -4; column <= 4; column++) {
|
||||
pose.position = {column * 0.6f, 0.2f, -3};
|
||||
cube(camera, pose, 0.08f, 0.1f, 0.3f, 0.8f);
|
||||
}
|
||||
pose.position = {0, 1.4f, -2}; cube(camera, pose, 0.2f, 0.1f, 0.9f, 0.4f);
|
||||
for (int hand = 0; hand < 2; hand++)
|
||||
if (handActive_[hand] && (handLocations_[hand].locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT))
|
||||
cube(camera, handLocations_[hand].pose, 0.06f, hand == 0 ? 0.9f : 0.2f, 0.3f, hand == 1 ? 0.9f : 0.2f);
|
||||
glDisable(GL_SCISSOR_TEST);
|
||||
}
|
||||
|
||||
void pollEvents() {
|
||||
XrEventDataBuffer event{XR_TYPE_EVENT_DATA_BUFFER};
|
||||
XrResult result;
|
||||
while ((result = xrPollEvent(instance_, &event)) == XR_SUCCESS) {
|
||||
if (event.type == XR_TYPE_EVENT_DATA_SESSION_STATE_CHANGED) {
|
||||
state_ = reinterpret_cast<XrEventDataSessionStateChanged *>(&event)->state;
|
||||
__android_log_print(ANDROID_LOG_INFO, "DroidDeckXR", "session state=%d", state_);
|
||||
attachGuest(state_ == XR_SESSION_STATE_VISIBLE || state_ == XR_SESSION_STATE_FOCUSED);
|
||||
if (state_ == XR_SESSION_STATE_READY) {
|
||||
XrSessionBeginInfo begin{XR_TYPE_SESSION_BEGIN_INFO};
|
||||
begin.primaryViewConfigurationType = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO;
|
||||
check(xrBeginSession(session_, &begin), "xrBeginSession"); running_ = true;
|
||||
} else if (state_ == XR_SESSION_STATE_STOPPING) {
|
||||
check(xrEndSession(session_), "xrEndSession"); running_ = false;
|
||||
if (quitRequested_) exit_ = true;
|
||||
} else if (state_ == XR_SESSION_STATE_EXITING || state_ == XR_SESSION_STATE_LOSS_PENDING) exit_ = true;
|
||||
health();
|
||||
} else if (event.type == XR_TYPE_EVENT_DATA_INSTANCE_LOSS_PENDING) exit_ = true;
|
||||
event = {XR_TYPE_EVENT_DATA_BUFFER};
|
||||
}
|
||||
check(result, "xrPollEvent");
|
||||
}
|
||||
|
||||
void frame() {
|
||||
XrFrameWaitInfo wait{XR_TYPE_FRAME_WAIT_INFO};
|
||||
XrFrameState frameState{XR_TYPE_FRAME_STATE};
|
||||
check(xrWaitFrame(session_, &wait, &frameState), "xrWaitFrame");
|
||||
period_ = frameState.predictedDisplayPeriod;
|
||||
XrFrameBeginInfo begin{XR_TYPE_FRAME_BEGIN_INFO}; check(xrBeginFrame(session_, &begin), "xrBeginFrame");
|
||||
check(xrLocateSpace(view_, world_, frameState.predictedDisplayTime, &head_), "head pose");
|
||||
if (state_ == XR_SESSION_STATE_FOCUSED) {
|
||||
XrActiveActionSet active{actions_, XR_NULL_PATH};
|
||||
XrActionsSyncInfo sync{XR_TYPE_ACTIONS_SYNC_INFO}; sync.countActiveActionSets = 1; sync.activeActionSets = &active;
|
||||
check(xrSyncActions(session_, &sync), "xrSyncActions");
|
||||
for (int hand = 0; hand < 2; hand++) {
|
||||
XrActionStateGetInfo get{XR_TYPE_ACTION_STATE_GET_INFO}; get.action = grip_; get.subactionPath = hands_[hand];
|
||||
XrActionStatePose pose{XR_TYPE_ACTION_STATE_POSE}; check(xrGetActionStatePose(session_, &get, &pose), "grip state");
|
||||
handActive_[hand] = pose.isActive;
|
||||
check(xrLocateSpace(handSpaces_[hand], world_, frameState.predictedDisplayTime, &handLocations_[hand]), "hand pose");
|
||||
get.action = trigger_;
|
||||
XrActionStateFloat trigger{XR_TYPE_ACTION_STATE_FLOAT}; check(xrGetActionStateFloat(session_, &get, &trigger), "trigger state");
|
||||
inputs_[hand].trigger = trigger.isActive ? trigger.currentState : 0;
|
||||
get.action = squeeze_;
|
||||
XrActionStateFloat squeeze{XR_TYPE_ACTION_STATE_FLOAT}; check(xrGetActionStateFloat(session_, &get, &squeeze), "squeeze state");
|
||||
inputs_[hand].squeeze = squeeze.isActive ? squeeze.currentState : 0;
|
||||
get.action = stick_;
|
||||
XrActionStateVector2f stick{XR_TYPE_ACTION_STATE_VECTOR2F}; check(xrGetActionStateVector2f(session_, &get, &stick), "stick state");
|
||||
inputs_[hand].stick[0] = stick.isActive ? stick.currentState.x : 0;
|
||||
inputs_[hand].stick[1] = stick.isActive ? stick.currentState.y : 0;
|
||||
inputs_[hand].buttons = 0;
|
||||
const std::pair<XrAction, uint32_t> buttons[] = {
|
||||
{stickClick_, DROIDDECK_XR_STICK_CLICK}, {primary_, DROIDDECK_XR_PRIMARY_CLICK},
|
||||
{secondary_, DROIDDECK_XR_SECONDARY_CLICK}, {menu_, DROIDDECK_XR_MENU_CLICK},
|
||||
{triggerTouch_, DROIDDECK_XR_TRIGGER_TOUCH}, {stickTouch_, DROIDDECK_XR_STICK_TOUCH},
|
||||
{primaryTouch_, DROIDDECK_XR_PRIMARY_TOUCH}, {secondaryTouch_, DROIDDECK_XR_SECONDARY_TOUCH},
|
||||
{thumbrestTouch_, DROIDDECK_XR_THUMBREST_TOUCH}};
|
||||
for (const auto &button : buttons) {
|
||||
get.action = button.first;
|
||||
XrActionStateBoolean value{XR_TYPE_ACTION_STATE_BOOLEAN}; check(xrGetActionStateBoolean(session_, &get, &value), "button state");
|
||||
if (value.isActive && value.currentState) inputs_[hand].buttons |= button.second;
|
||||
}
|
||||
bool pressed = trigger.isActive && trigger.currentState > 0.8f;
|
||||
if (pressed && !pressed_[hand]) {
|
||||
XrHapticActionInfo info{XR_TYPE_HAPTIC_ACTION_INFO}; info.action = vibration_; info.subactionPath = hands_[hand];
|
||||
XrHapticVibration pulse{XR_TYPE_HAPTIC_VIBRATION}; pulse.duration = 30000000; pulse.frequency = XR_FREQUENCY_UNSPECIFIED; pulse.amplitude = 0.3f;
|
||||
check(xrApplyHapticFeedback(session_, &info, reinterpret_cast<XrHapticBaseHeader *>(&pulse)), "haptic pulse");
|
||||
}
|
||||
pressed_[hand] = pressed;
|
||||
}
|
||||
} else handActive_[0] = handActive_[1] = false;
|
||||
pollHaptics();
|
||||
XrViewLocateInfo locate{XR_TYPE_VIEW_LOCATE_INFO}; locate.viewConfigurationType = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO;
|
||||
locate.displayTime = frameState.predictedDisplayTime; locate.space = world_;
|
||||
XrViewState viewState{XR_TYPE_VIEW_STATE}; uint32_t count = 0;
|
||||
XrView views[2]{{XR_TYPE_VIEW}, {XR_TYPE_VIEW}};
|
||||
check(xrLocateViews(session_, &locate, &viewState, 2, &count, views), "xrLocateViews");
|
||||
publishTracking(views, frameState.predictedDisplayTime);
|
||||
if ((guestSurface_ || guestFramesInput_) && guestAttached_) {
|
||||
timespec now{}; clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
guestFrames_ = env_->CallLongMethod(app_->activity->clazz, frameCallback_, (jlong)now.tv_sec * 1000000000LL + now.tv_nsec);
|
||||
if (env_->ExceptionCheck()) { env_->ExceptionClear(); throw std::runtime_error("Guest frame callback failed"); }
|
||||
}
|
||||
XrCompositionLayerProjectionView projection[2]{{XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW}, {XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW}};
|
||||
bool valid = frameState.shouldRender && (!surfaceTest_ || state_ == XR_SESSION_STATE_VISIBLE || state_ == XR_SESSION_STATE_FOCUSED) && count == 2 &&
|
||||
(viewState.viewStateFlags & (XR_VIEW_STATE_ORIENTATION_VALID_BIT | XR_VIEW_STATE_POSITION_VALID_BIT))
|
||||
== (XR_VIEW_STATE_ORIENTATION_VALID_BIT | XR_VIEW_STATE_POSITION_VALID_BIT);
|
||||
if (frameInput_) {
|
||||
valid = valid && sourceHeader_.frame_id && xr_frame_now() - sourceHeader_.captured_ns < 500000000LL;
|
||||
if (valid) for (int eye = 0; eye < 2; eye++) {
|
||||
auto source = xr_frame_view(sourceHeader_, eye);
|
||||
views[eye].pose.orientation = {source.orientation[0], source.orientation[1], source.orientation[2], source.orientation[3]};
|
||||
views[eye].pose.position = {source.position[0], source.position[1], source.position[2]};
|
||||
views[eye].fov = {source.fov[0], source.fov[1], source.fov[2], source.fov[3]};
|
||||
}
|
||||
}
|
||||
if (valid && surfaceTest_) {
|
||||
if (!guestSurface_ && !eglMakeCurrent(display_, androidSurface_, androidSurface_, context_))
|
||||
throw std::runtime_error("Surface context binding failed");
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
for (int eye = 0; eye < 2; eye++) {
|
||||
if (!guestSurface_) scene(views[eye], eyes_[eye].width, eyes_[eye].height, eye * eyes_[eye].width);
|
||||
projection[eye].pose = views[eye].pose; projection[eye].fov = views[eye].fov;
|
||||
projection[eye].subImage.swapchain = androidSwapchain_;
|
||||
projection[eye].subImage.imageRect.offset = {eye * eyes_[eye].width, 0};
|
||||
projection[eye].subImage.imageRect.extent = {eyes_[eye].width, eyes_[eye].height};
|
||||
}
|
||||
if (glGetError() != GL_NO_ERROR) throw std::runtime_error("Surface rendering error");
|
||||
if (!guestSurface_ && !eglSwapBuffers(display_, androidSurface_)) throw std::runtime_error("Surface presentation failed");
|
||||
} else if (valid) for (int eye = 0; eye < 2; eye++) {
|
||||
uint32_t image = 0;
|
||||
XrSwapchainImageAcquireInfo acquire{XR_TYPE_SWAPCHAIN_IMAGE_ACQUIRE_INFO};
|
||||
check(xrAcquireSwapchainImage(eyes_[eye].swapchain, &acquire, &image), "acquire image");
|
||||
XrSwapchainImageWaitInfo imageWait{XR_TYPE_SWAPCHAIN_IMAGE_WAIT_INFO}; imageWait.timeout = XR_INFINITE_DURATION;
|
||||
check(xrWaitSwapchainImage(eyes_[eye].swapchain, &imageWait), "wait image");
|
||||
render(views[eye], eyes_[eye], image);
|
||||
XrSwapchainImageReleaseInfo release{XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO};
|
||||
check(xrReleaseSwapchainImage(eyes_[eye].swapchain, &release), "release image");
|
||||
projection[eye].pose = views[eye].pose; projection[eye].fov = views[eye].fov;
|
||||
projection[eye].subImage.swapchain = eyes_[eye].swapchain;
|
||||
projection[eye].subImage.imageRect.extent = {eyes_[eye].width, eyes_[eye].height};
|
||||
}
|
||||
XrCompositionLayerProjection layer{XR_TYPE_COMPOSITION_LAYER_PROJECTION};
|
||||
layer.space = world_; layer.viewCount = 2; layer.views = projection;
|
||||
const XrCompositionLayerBaseHeader *layers[] = {reinterpret_cast<XrCompositionLayerBaseHeader *>(&layer)};
|
||||
XrFrameEndInfo end{XR_TYPE_FRAME_END_INFO}; end.displayTime = frameState.predictedDisplayTime;
|
||||
end.environmentBlendMode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE; end.layerCount = valid ? 1 : 0; end.layers = layers;
|
||||
check(xrEndFrame(session_, &end), "xrEndFrame");
|
||||
frames_++; if (valid) rendered_++;
|
||||
if (frames_ % 72 == 0) {
|
||||
health();
|
||||
__android_log_print(ANDROID_LOG_INFO, "DroidDeckXR", "frames=%llu rendered=%llu head=%llu hands=%d,%d period=%lld",
|
||||
(unsigned long long)frames_, (unsigned long long)rendered_, (unsigned long long)head_.locationFlags,
|
||||
handActive_[0], handActive_[1], (long long)period_);
|
||||
}
|
||||
}
|
||||
|
||||
void cleanup() {
|
||||
receiver_.Stop();
|
||||
try { attachGuest(false); } catch (const std::exception &error) {
|
||||
__android_log_print(ANDROID_LOG_ERROR, "DroidDeckXR", "cleanup: %s", error.what());
|
||||
}
|
||||
if (tracking_) {
|
||||
__atomic_store_n(&tracking_->active, 0u, __ATOMIC_RELEASE);
|
||||
munmap(tracking_, sizeof(DroidDeckXrTracking));
|
||||
}
|
||||
if (haptics_) munmap(haptics_, sizeof(DroidDeckXrHaptics));
|
||||
if (display_ != EGL_NO_DISPLAY && context_ != EGL_NO_CONTEXT) {
|
||||
glDeleteFramebuffers(1, &framebuffer_); glDeleteProgram(program_);
|
||||
glDeleteBuffers(1, &buffer_); glDeleteVertexArrays(1, &vertexArray_);
|
||||
glDeleteTextures(1, &sourceTexture_); glDeleteProgram(sourceProgram_);
|
||||
for (auto &eye : eyes_) glDeleteRenderbuffers(1, &eye.depth);
|
||||
}
|
||||
if (androidSurface_ != EGL_NO_SURFACE) {
|
||||
eglMakeCurrent(display_, surface_, surface_, context_);
|
||||
eglDestroySurface(display_, androidSurface_);
|
||||
}
|
||||
if (androidWindow_) ANativeWindow_release(androidWindow_);
|
||||
if (androidSwapchain_) xrDestroySwapchain(androidSwapchain_);
|
||||
if (androidJavaSurface_) env_->DeleteGlobalRef(androidJavaSurface_);
|
||||
for (auto &eye : eyes_) if (eye.swapchain) xrDestroySwapchain(eye.swapchain);
|
||||
for (auto space : handSpaces_) if (space) xrDestroySpace(space);
|
||||
if (view_) xrDestroySpace(view_); if (world_) xrDestroySpace(world_);
|
||||
if (session_) xrDestroySession(session_);
|
||||
if (actions_) xrDestroyActionSet(actions_);
|
||||
if (instance_) xrDestroyInstance(instance_);
|
||||
if (display_ != EGL_NO_DISPLAY) {
|
||||
eglMakeCurrent(display_, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
||||
if (surface_ != EGL_NO_SURFACE) eglDestroySurface(display_, surface_);
|
||||
if (context_ != EGL_NO_CONTEXT) eglDestroyContext(display_, context_);
|
||||
eglTerminate(display_);
|
||||
}
|
||||
if (env_) app_->activity->vm->DetachCurrentThread();
|
||||
}
|
||||
};
|
||||
|
||||
void android_main(android_app *app) {
|
||||
QuestXR runtime(app);
|
||||
try { runtime.run(); }
|
||||
catch (const std::exception &error) {
|
||||
__android_log_print(ANDROID_LOG_ERROR, "DroidDeckXR", "%s", error.what());
|
||||
runtime.health(error.what());
|
||||
ANativeActivity_finish(app->activity);
|
||||
}
|
||||
}
|
||||
@@ -16,3 +16,4 @@ dependencyResolutionManagement {
|
||||
|
||||
rootProject.name = "DroidDeck"
|
||||
include ':app'
|
||||
include ':questxr'
|
||||
@@ -0,0 +1,19 @@
|
||||
param(
|
||||
[string]$Sdk = "$env:USERPROFILE\android-sdk",
|
||||
[string]$NdkVersion = '27.3.13750724'
|
||||
)
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$repo = Split-Path $PSScriptRoot -Parent
|
||||
$compiler = Join-Path $Sdk "ndk\$NdkVersion\toolchains\llvm\prebuilt\windows-x86_64\bin\aarch64-linux-android26-clang++.cmd"
|
||||
if (-not (Test-Path -LiteralPath $compiler -PathType Leaf)) { throw 'Android ARM64 compiler is unavailable' }
|
||||
Push-Location $repo
|
||||
try {
|
||||
New-Item -ItemType Directory -Path build\vr\android-tests -Force | Out-Null
|
||||
foreach ($name in @('test_xr_tracking', 'test_xr_frames', 'test_xr_vsync', 'send_xr_frame')) {
|
||||
& $compiler -std=c++17 -Wall -Wextra -Werror -static-libstdc++ -pthread "tools/vr/tests/$name.cpp" -o "build/vr/android-tests/$name"
|
||||
if ($LASTEXITCODE -ne 0) { throw "Android test compilation failed: $name" }
|
||||
}
|
||||
Write-Output "Built Android test executables: $(Join-Path $repo 'build\vr\android-tests')"
|
||||
} finally {
|
||||
Pop-Location
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
param(
|
||||
[Parameter(Mandatory = $true)][string]$KeyDirectory,
|
||||
[Parameter(Mandatory = $true)][string]$Adb,
|
||||
[string]$Sdk = "$env:USERPROFILE\android-sdk",
|
||||
[string]$NdkVersion = '27.3.13750724',
|
||||
[string]$BuildTools = '35.0.0',
|
||||
[string]$Apk,
|
||||
[string]$Serial,
|
||||
[switch]$SignOnly
|
||||
)
|
||||
$ErrorActionPreference = 'Stop'
|
||||
function Invoke-Checked([string]$Program, [string[]]$Arguments) {
|
||||
$ErrorActionPreference = 'Continue'
|
||||
& $Program @Arguments
|
||||
if ($LASTEXITCODE -ne 0) { throw "$Program exited with $LASTEXITCODE" }
|
||||
}
|
||||
$repo = Split-Path $PSScriptRoot -Parent
|
||||
Push-Location $repo
|
||||
try {
|
||||
if (-not $Apk) {
|
||||
Invoke-Checked .\gradlew.bat @(':questxr:assembleDebug', "-PndkVersion=$NdkVersion", '-PskipRust=true', '--console=plain')
|
||||
$Apk = 'questxr\build\outputs\apk\debug\questxr-debug.apk'
|
||||
}
|
||||
if (-not (Test-Path -LiteralPath $Apk -PathType Leaf)) { throw 'APK input does not exist' }
|
||||
New-Item -ItemType Directory -Path build\quest-deploy -Force | Out-Null
|
||||
$tools = Join-Path $Sdk "build-tools\$BuildTools"
|
||||
$aligned = 'build\quest-deploy\aligned.apk'
|
||||
$signed = 'build\quest-deploy\signed.apk'
|
||||
Invoke-Checked "$tools\zipalign.exe" @('-f', '-p', '4', $Apk, $aligned)
|
||||
Invoke-Checked "$tools\apksigner.bat" @('sign', '--key', "$KeyDirectory\droiddeck-release.pk8", '--cert', "$KeyDirectory\droiddeck-release.x509.pem", '--out', $signed, $aligned)
|
||||
$certificate = Invoke-Checked "$tools\apksigner.bat" @('verify', '--print-certs', $signed)
|
||||
$expected = (Get-Content keystore\release-signer.sha256 -Raw).Trim()
|
||||
if (-not ($certificate -match "certificate SHA-256 digest: $expected")) { throw 'Unexpected signing certificate' }
|
||||
if (-not $SignOnly) {
|
||||
$connected = @(Invoke-Checked $Adb @('devices') | ForEach-Object {
|
||||
if ($_ -match '^(\S+)\s+device\s*$') { $matches[1] }
|
||||
})
|
||||
if (-not $Serial) {
|
||||
if ($connected.Count -ne 1) { throw 'Specify -Serial with exactly one connected, authorized Quest target' }
|
||||
$Serial = $connected[0]
|
||||
}
|
||||
if ($Serial -notin $connected) { throw 'The selected headset is not connected and authorized' }
|
||||
$model = (Invoke-Checked $Adb @('-s', $Serial, 'shell', 'getprop', 'ro.product.model') | Out-String).Trim()
|
||||
if ($model -notmatch '^Quest[ _]3$') { throw "Refusing Quest 3 deployment to model: $model" }
|
||||
Invoke-Checked $Adb @('-s', $Serial, 'install', '--no-incremental', '-r', $signed)
|
||||
}
|
||||
Write-Output "Verified signed APK: $(Join-Path $repo $signed)"
|
||||
} finally {
|
||||
Pop-Location
|
||||
}
|
||||
@@ -7,7 +7,9 @@ driver=$vrdir/droiddeck
|
||||
layer=$vrdir/layer
|
||||
logs=$HOME/.local/share/Steam/logs
|
||||
state=${XDG_RUNTIME_DIR:-/tmp}/droiddeck-steamvr
|
||||
umask 077
|
||||
mkdir -p "$state"
|
||||
chmod 700 "$state"
|
||||
|
||||
PKG_URL=https://steamdeck-packages.steamos.cloud/archlinux-deckard-hotfixes/release/0.3.x/deckard-steamvr-rel-r25358740%2B28b72a4f-1-aarch64.pkg.tar.zst
|
||||
PKG_SHA256=90a600775eb3e8856dc475f83166a93a290a0b1f3b4e82a69c8e2a8f06b9ddc9
|
||||
@@ -75,6 +77,13 @@ JSON
|
||||
export VK_LAYER_PATH="$layer${VK_LAYER_PATH:+:$VK_LAYER_PATH}"
|
||||
export VK_INSTANCE_LAYERS="VK_LAYER_DROIDDECK_vr${VK_INSTANCE_LAYERS:+:$VK_INSTANCE_LAYERS}"
|
||||
export VK_LOADER_LAYERS_DISABLE="${VK_LOADER_LAYERS_DISABLE:-~implicit~}"
|
||||
if [ -S /run/droiddeck/droiddeck-xr-frames ] && [ -z "${DROIDDECK_VR_PRESENT:-}" ]; then
|
||||
export DROIDDECK_VR_PRESENT=questframes
|
||||
fi
|
||||
if [ -z "${DROIDDECK_VR_DISPLAY_MODE:-}" ] && [ -x "$driver/bin/linuxarm64/xr-display-mode" ]; then
|
||||
mode=$("$driver/bin/linuxarm64/xr-display-mode") || mode=
|
||||
if [ -n "$mode" ]; then export DROIDDECK_VR_DISPLAY_MODE="$mode"; fi
|
||||
fi
|
||||
|
||||
shims=$state/shims
|
||||
mkdir -p "$shims"
|
||||
@@ -86,7 +95,9 @@ JSON
|
||||
|
||||
: > "$state/active"
|
||||
cd "$steamvr/bin/linuxarm64" || exit 1
|
||||
setsid ./vrstartup > "$state/vrstartup.log" 2>&1 < /dev/null &
|
||||
startup_log=$logs/droiddeck-vrstartup.log
|
||||
ln -sf "$startup_log" "$state/vrstartup.log"
|
||||
setsid ./vrstartup > "$startup_log" 2>&1 < /dev/null &
|
||||
echo "SteamVR starting; logs in $logs and $state/vrstartup.log"
|
||||
;;
|
||||
*)
|
||||
|
||||
@@ -98,23 +98,36 @@ long syscall(long number, ...) {
|
||||
args[i] = va_arg(ap, long);
|
||||
}
|
||||
va_end(ap);
|
||||
int saved_errno = errno;
|
||||
if (!real_syscall) {
|
||||
real_syscall = (long (*)(long, ...)) dlsym(RTLD_NEXT, "syscall");
|
||||
}
|
||||
if (number == SYS_get_robust_list && (args[0] == 0 || args[0] == gettid())) {
|
||||
errno = saved_errno;
|
||||
long result = real_syscall(number, args[0], args[1], args[2]);
|
||||
if (result != -1 || errno != ENOSYS) {
|
||||
return result;
|
||||
}
|
||||
struct robust_list_head *head = thread_robust_head();
|
||||
if (!head) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
*(struct robust_list_head **)args[1] = head;
|
||||
*(size_t *)args[2] = sizeof(*head);
|
||||
size_t length = sizeof(*head);
|
||||
struct iovec local[2] = {{&head, sizeof(head)}, {&length, sizeof(length)}};
|
||||
struct iovec remote[2] = {{(void *)args[1], sizeof(head)}, {(void *)args[2], sizeof(length)}};
|
||||
ssize_t written = process_vm_writev(getpid(), local, 2, remote, 2, 0);
|
||||
if (written != (ssize_t)(sizeof(head) + sizeof(length))) {
|
||||
if (written >= 0) errno = EFAULT;
|
||||
return -1;
|
||||
}
|
||||
errno = saved_errno;
|
||||
return 0;
|
||||
}
|
||||
long handled;
|
||||
if (bl_fsync_syscall(number, args, &handled)) {
|
||||
return handled;
|
||||
}
|
||||
if (!real_syscall) {
|
||||
real_syscall = (long (*)(long, ...)) dlsym(RTLD_NEXT, "syscall");
|
||||
}
|
||||
if (number == SYS_openat2) {
|
||||
if (!__atomic_load_n(&openat2_missing, __ATOMIC_RELAXED)) {
|
||||
long fd = real_syscall(number, args[0], args[1], args[2], args[3]);
|
||||
|
||||
@@ -8,7 +8,7 @@ from zipfile import ZipFile
|
||||
|
||||
|
||||
SYSTEM = set('libc.so libm.so libdl.so liblog.so libandroid.so libz.so libvulkan.so libstdc++.so '
|
||||
'libGLESv2.so libEGL.so libnativewindow.so libjnigraphics.so libaaudio.so '
|
||||
'libGLESv2.so libGLESv3.so libEGL.so libnativewindow.so libjnigraphics.so libaaudio.so '
|
||||
'libOpenSLES.so libmediandk.so libcamera2ndk.so libsync.so libneuralnetworks.so'.split())
|
||||
EXPORTS = ['Java_com_droiddeck_launcher_stores_StoresNative_nativeVersion',
|
||||
'Java_com_droiddeck_launcher_stores_gog_GogNative_nativeStart',
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
from adb_device import resolve_serial
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='Validate private owned-frame transport and GLES upload, not SteamVR gameplay')
|
||||
parser.add_argument('--adb', default=os.environ.get('ADB', 'adb'))
|
||||
parser.add_argument('--serial')
|
||||
parser.add_argument('--native-tests', type=Path, required=True)
|
||||
parser.add_argument('--output', type=Path, default=Path('questxr-validation/frames'))
|
||||
parser.add_argument('--require-rendering', action='store_true')
|
||||
args = parser.parse_args()
|
||||
serial = resolve_serial(args.adb, args.serial)
|
||||
package = 'com.droiddeck.questxr'
|
||||
health_path = f'/sdcard/Android/data/{package}/files/xr-health.json'
|
||||
args.output.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def adb(*command):
|
||||
return subprocess.run([args.adb, '-s', serial, *command], check=True,
|
||||
capture_output=True, text=True, timeout=30).stdout
|
||||
|
||||
for name in ('test_xr_tracking', 'test_xr_frames', 'send_xr_frame'):
|
||||
binary = args.native_tests / name
|
||||
if not binary.is_file():
|
||||
parser.error(f'Missing Android executable: {binary}')
|
||||
remote = f'/data/local/tmp/droiddeck-{name}'
|
||||
adb('push', str(binary), remote)
|
||||
adb('shell', 'run-as', package, 'cp', remote, f'./{name}')
|
||||
adb('shell', 'run-as', package, 'chmod', '700', f'./{name}')
|
||||
unit_output = adb('shell', 'run-as', package, './test_xr_tracking', './files')
|
||||
unit_output += adb('shell', 'run-as', package, './test_xr_frames', './files')
|
||||
adb('shell', 'am', 'force-stop', package)
|
||||
adb('shell', 'rm', '-f', health_path)
|
||||
failure = None
|
||||
samples = []
|
||||
sent = {}
|
||||
try:
|
||||
adb('shell', 'input', 'keyevent', 'KEYCODE_WAKEUP')
|
||||
adb('shell', 'am', 'broadcast', '-a', 'com.oculus.vrpowermanager.prox_close')
|
||||
adb('shell', 'am', 'start', '-n', f'{package}/android.app.NativeActivity',
|
||||
'--ez', 'frame_input', 'true', '--ez', 'frame_verify', 'true')
|
||||
sent = json.loads(adb('shell', 'run-as', package, './send_xr_frame',
|
||||
'./files/.wayland-rt/droiddeck-xr-frames'))
|
||||
deadline = time.monotonic() + 5
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
sample = json.loads(adb('shell', 'cat', health_path))
|
||||
samples.append(sample)
|
||||
if sample.get('error'):
|
||||
failure = sample['error']
|
||||
break
|
||||
if sample.get('gpuVerifiedFrameId') == sent['lastFrameId']:
|
||||
break
|
||||
except (subprocess.CalledProcessError, json.JSONDecodeError):
|
||||
pass
|
||||
time.sleep(0.2)
|
||||
except (subprocess.CalledProcessError, subprocess.TimeoutExpired, json.JSONDecodeError) as error:
|
||||
failure = f'Frame channel operation failed: {error}'
|
||||
if isinstance(error, subprocess.CalledProcessError) and error.stderr:
|
||||
failure += f' {error.stderr.strip()}'
|
||||
finally:
|
||||
try:
|
||||
sample = json.loads(adb('shell', 'cat', health_path))
|
||||
if not samples or sample != samples[-1]:
|
||||
samples.append(sample)
|
||||
except (subprocess.CalledProcessError, subprocess.TimeoutExpired, json.JSONDecodeError):
|
||||
pass
|
||||
try:
|
||||
adb('shell', 'am', 'broadcast', '-a', 'com.oculus.vrpowermanager.prox_far')
|
||||
adb('shell', 'am', 'force-stop', package)
|
||||
except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
|
||||
failure = failure or 'Could not restore headset proximity after testing'
|
||||
try:
|
||||
log = adb('logcat', '-d', '-s', 'DroidDeckXR:I', 'AndroidRuntime:E')
|
||||
(args.output / 'logcat.txt').write_text(log, encoding='utf-8')
|
||||
except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
|
||||
pass
|
||||
latest = samples[-1] if samples else {}
|
||||
if not failure:
|
||||
if latest.get('source') != 'frames' or latest.get('receivedFrames') != sent.get('sent'):
|
||||
failure = 'Not all complete frames arrived in the private receiver'
|
||||
elif latest.get('gpuVerifiedFrameId') != sent.get('lastFrameId') or latest.get('gpuChecksum') != sent.get('checksum'):
|
||||
failure = 'GPU readback does not prove the final image and frame ID match'
|
||||
elif latest.get('rejectedFrames') != 0 or latest.get('uploadedFrames', 0) < 1:
|
||||
failure = 'Malformed frames or no actual GPU upload'
|
||||
elif args.require_rendering and (latest.get('rendered', 0) < 72 or latest.get('headFlags', 0) & 15 != 15):
|
||||
failure = 'Owned upload passed but sustained tracked immersive projection did not'
|
||||
report = {'ok': failure is None, 'scope': 'owned-frame GLES upload', 'error': failure,
|
||||
'unitTests': unit_output, 'sender': sent, 'samples': samples}
|
||||
(args.output / 'report.json').write_text(json.dumps(report, indent=2), encoding='utf-8')
|
||||
print(json.dumps(report, indent=2))
|
||||
return 0 if report['ok'] else 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
@@ -0,0 +1,152 @@
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
from adb_device import resolve_serial
|
||||
|
||||
|
||||
def validate_samples(samples, require_controllers=False, guest=False, steamvr=False, surface=False):
|
||||
if len(samples) < 2:
|
||||
return 'No advancing telemetry; check runtime initialization and headset wake/proximity'
|
||||
if samples[-1]['rendered'] - samples[0]['rendered'] < 72:
|
||||
return 'Fewer than 72 additional stereo frames were submitted'
|
||||
if not any(sample['sessionState'] == 5 and sample['headFlags'] & 15 == 15 for sample in samples):
|
||||
return 'No focused session with tracked 6DoF position and orientation'
|
||||
if samples[-1]['sessionState'] != 5 or samples[-1]['headFlags'] & 15 != 15:
|
||||
return 'Session did not remain focused with tracked 6DoF through the final sample'
|
||||
if require_controllers and not any(all(sample['controllersActive']) for sample in samples):
|
||||
return 'Both controllers did not become active'
|
||||
if steamvr and samples[-1].get('uploadedFrames', 0) - samples[0].get('uploadedFrames', 0) < 72:
|
||||
return 'No advancing owned SteamVR images; XR submissions alone are insufficient'
|
||||
if steamvr and samples[-1].get('sourceFrameId', 0) <= samples[0].get('sourceFrameId', 0):
|
||||
return 'SteamVR source frame IDs did not advance'
|
||||
if guest and not steamvr and samples[-1].get('guestFrames', 0) - samples[0].get('guestFrames', 0) < 72:
|
||||
return 'No advancing guest GPU presentation; submitting an XR layer alone is insufficient'
|
||||
if steamvr and samples[-1].get('steamvrState') != 4:
|
||||
return 'SteamVR did not report RUNNING'
|
||||
expected = 'frames' if steamvr else 'guest' if guest else 'surface' if surface else 'native'
|
||||
if samples[-1].get('source') != expected:
|
||||
return 'Wrong projection source'
|
||||
return None
|
||||
|
||||
|
||||
def summarize_samples(samples):
|
||||
result = {'sampleCount': len(samples), 'focusedTrackedFraction': None, 'observedSeconds': None, 'ratesHz': {}}
|
||||
if not samples:
|
||||
return result
|
||||
result['focusedTrackedFraction'] = sum(sample.get('sessionState') == 5 and sample.get('headFlags', 0) & 15 == 15
|
||||
for sample in samples) / len(samples)
|
||||
if len(samples) < 2 or '_hostMonotonicSeconds' not in samples[0] or '_hostMonotonicSeconds' not in samples[-1]:
|
||||
return result
|
||||
duration = samples[-1]['_hostMonotonicSeconds'] - samples[0]['_hostMonotonicSeconds']
|
||||
if duration <= 0:
|
||||
return result
|
||||
result['observedSeconds'] = duration
|
||||
for counter in ('rendered', 'guestFrames', 'receivedFrames', 'rejectedFrames', 'uploadedFrames'):
|
||||
if counter in samples[0] and counter in samples[-1]:
|
||||
delta = samples[-1][counter] - samples[0][counter]
|
||||
result['ratesHz'][counter] = delta / duration if delta >= 0 else None
|
||||
period = samples[-1].get('periodNs', 0)
|
||||
result['runtimeRefreshHz'] = 1000000000 / period if period > 0 else None
|
||||
return result
|
||||
|
||||
|
||||
def collect_device_metrics(adb, package):
|
||||
result = {}
|
||||
for name, command in [('battery', ('shell', 'dumpsys', 'battery')),
|
||||
('thermal', ('shell', 'dumpsys', 'thermalservice')),
|
||||
('memory', ('shell', 'dumpsys', 'meminfo', package))]:
|
||||
try:
|
||||
result[name] = {'available': True, 'text': adb(*command)}
|
||||
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as error:
|
||||
result[name] = {'available': False, 'error': type(error).__name__}
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='Validate immersive DroidDeck XR on a connected Quest')
|
||||
parser.add_argument('--adb', default=os.environ.get('ADB', 'adb'))
|
||||
parser.add_argument('--serial')
|
||||
parser.add_argument('--seconds', type=int, default=20)
|
||||
parser.add_argument('--output', type=Path, default=Path('questxr-validation'))
|
||||
parser.add_argument('--require-controllers', action='store_true')
|
||||
parser.add_argument('--surface', action='store_true')
|
||||
parser.add_argument('--guest', action='store_true')
|
||||
parser.add_argument('--keep-awake', action='store_true')
|
||||
parser.add_argument('--steamvr', action='store_true')
|
||||
parser.add_argument('--metrics', action='store_true', help='Record public battery, thermal and package memory dumps before/after testing')
|
||||
args = parser.parse_args()
|
||||
if args.seconds < 5 or args.seconds > 600:
|
||||
parser.error('--seconds must be between 5 and 600')
|
||||
if args.steamvr and not args.guest:
|
||||
parser.error('--steamvr requires --guest')
|
||||
if args.steamvr and args.surface:
|
||||
parser.error('--steamvr uses owned frames and cannot use --surface')
|
||||
serial = resolve_serial(args.adb, args.serial)
|
||||
args.output.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def adb(*command):
|
||||
return subprocess.run([args.adb, '-s', serial, *command], check=True,
|
||||
capture_output=True, text=True, timeout=15).stdout
|
||||
|
||||
package = 'com.droiddeck.launcher' if args.guest else 'com.droiddeck.questxr'
|
||||
health_path = f'/sdcard/Android/data/{package}/files/xr-health.json'
|
||||
if not args.guest:
|
||||
adb('shell', 'am', 'force-stop', package)
|
||||
adb('shell', 'rm', '-f', health_path)
|
||||
adb('shell', 'input', 'keyevent', 'KEYCODE_WAKEUP')
|
||||
component = f'{package}/.QuestXrActivity' if args.guest else f'{package}/android.app.NativeActivity'
|
||||
launch = ['shell', 'am', 'start', '-n', component]
|
||||
if args.surface:
|
||||
launch += ['--ez', 'surface_test', 'true']
|
||||
if args.guest:
|
||||
launch += ['--ez', 'guest_frames' if args.steamvr else 'guest_surface', 'true']
|
||||
if args.steamvr:
|
||||
launch += ['--ez', 'start_steamvr', 'true']
|
||||
samples = []
|
||||
failure = None
|
||||
metrics = {'before': collect_device_metrics(adb, package)} if args.metrics else None
|
||||
try:
|
||||
adb('shell', 'am', 'broadcast', '-a', 'com.oculus.vrpowermanager.prox_close')
|
||||
adb(*launch)
|
||||
deadline = time.monotonic() + args.seconds
|
||||
while time.monotonic() < deadline:
|
||||
time.sleep(1)
|
||||
adb('shell', 'am', 'broadcast', '-a', 'com.oculus.vrpowermanager.prox_close')
|
||||
try:
|
||||
sample = json.loads(adb('shell', 'cat', health_path))
|
||||
sample['_hostMonotonicSeconds'] = time.monotonic()
|
||||
samples.append(sample)
|
||||
if sample.get('error'):
|
||||
failure = sample['error']
|
||||
break
|
||||
except (subprocess.CalledProcessError, json.JSONDecodeError):
|
||||
pass
|
||||
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as error:
|
||||
failure = f'ADB operation failed: {type(error).__name__}'
|
||||
finally:
|
||||
if not args.keep_awake:
|
||||
try:
|
||||
adb('shell', 'am', 'broadcast', '-a', 'com.oculus.vrpowermanager.prox_far')
|
||||
except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
|
||||
failure = failure or 'Could not restore headset proximity after testing'
|
||||
try:
|
||||
log = adb('logcat', '-d', '-s', 'DroidDeckXR:I', 'OpenXR-Loader:W', 'AndroidRuntime:E')
|
||||
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as error:
|
||||
log = str(error)
|
||||
(args.output / 'logcat.txt').write_text(log, encoding='utf-8')
|
||||
if metrics is not None:
|
||||
metrics['after'] = collect_device_metrics(adb, package)
|
||||
failure = failure or validate_samples(samples, args.require_controllers, args.guest, args.steamvr, args.surface)
|
||||
report = {'ok': failure is None, 'error': failure, 'samples': samples, 'summary': summarize_samples(samples), 'deviceMetrics': metrics}
|
||||
(args.output / 'report.json').write_text(json.dumps(report, indent=2), encoding='utf-8')
|
||||
print(json.dumps({'ok': report['ok'], 'error': failure, 'samples': len(samples),
|
||||
'last': samples[-1] if samples else None}, indent=2))
|
||||
return 0 if report['ok'] else 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
@@ -0,0 +1,91 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <linux/futex.h>
|
||||
#include <pthread.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int bl_fsync_syscall(long number, const long *args, long *result) {
|
||||
(void)number;
|
||||
(void)args;
|
||||
(void)result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include "../linuxfs/preload/robust.c"
|
||||
|
||||
static long (*kernel_syscall)(long, ...);
|
||||
static int missing_error;
|
||||
|
||||
static long missing_robust(long number, ...) {
|
||||
assert(number == SYS_get_robust_list);
|
||||
errno = missing_error;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static struct robust_list_head *check_head(void) {
|
||||
struct robust_list_head *expected = NULL;
|
||||
size_t expected_length = 0;
|
||||
assert(kernel_syscall(SYS_get_robust_list, 0, &expected, &expected_length) == 0);
|
||||
struct robust_list_head *head = NULL;
|
||||
size_t length = 0;
|
||||
errno = E2BIG;
|
||||
assert(syscall(SYS_get_robust_list, 0L, &head, &length, 0L, 0L, 0L) == 0);
|
||||
assert(errno == E2BIG);
|
||||
assert(head == expected && length == expected_length);
|
||||
return head;
|
||||
}
|
||||
|
||||
static void *check_thread(void *argument) {
|
||||
(void)argument;
|
||||
pthread_mutexattr_t attributes;
|
||||
pthread_mutex_t mutex;
|
||||
assert(pthread_mutexattr_init(&attributes) == 0);
|
||||
assert(pthread_mutexattr_setrobust(&attributes, PTHREAD_MUTEX_ROBUST) == 0);
|
||||
assert(pthread_mutex_init(&mutex, &attributes) == 0);
|
||||
assert(pthread_mutexattr_destroy(&attributes) == 0);
|
||||
struct robust_list_head *head = check_head();
|
||||
assert(pthread_mutex_lock(&mutex) == 0);
|
||||
struct robust_list *first = head->list.next;
|
||||
struct robust_list *pending = head->list_op_pending;
|
||||
assert(check_head() == head);
|
||||
assert(head->list.next == first && head->list_op_pending == pending);
|
||||
assert(pthread_mutex_unlock(&mutex) == 0);
|
||||
assert(pthread_mutex_destroy(&mutex) == 0);
|
||||
return head;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
kernel_syscall = dlsym(RTLD_NEXT, "syscall");
|
||||
assert(kernel_syscall);
|
||||
real_syscall = kernel_syscall;
|
||||
struct robust_list_head *main_head = check_thread(NULL);
|
||||
pthread_t thread;
|
||||
void *thread_head;
|
||||
assert(pthread_create(&thread, NULL, check_thread, NULL) == 0);
|
||||
assert(pthread_join(thread, &thread_head) == 0);
|
||||
assert(thread_head != main_head);
|
||||
real_syscall = missing_robust;
|
||||
missing_error = ENOSYS;
|
||||
check_thread(NULL);
|
||||
assert(pthread_create(&thread, NULL, check_thread, NULL) == 0);
|
||||
assert(pthread_join(thread, NULL) == 0);
|
||||
struct robust_list_head *head = NULL;
|
||||
size_t length = 0;
|
||||
assert(syscall(SYS_get_robust_list, 0L, NULL, &length, 0L, 0L, 0L) == -1);
|
||||
assert(errno == EFAULT);
|
||||
assert(syscall(SYS_get_robust_list, 0L, &head, NULL, 0L, 0L, 0L) == -1);
|
||||
assert(errno == EFAULT);
|
||||
head = NULL;
|
||||
missing_error = EPERM;
|
||||
assert(syscall(SYS_get_robust_list, 0L, &head, &length, 0L, 0L, 0L) == -1);
|
||||
assert(errno == EPERM && !head && !length);
|
||||
missing_error = EACCES;
|
||||
assert(syscall(SYS_get_robust_list, 0L, &head, &length, 0L, 0L, 0L) == -1);
|
||||
assert(errno == EACCES && !head && !length);
|
||||
puts("robust preload: native and ENOSYS fallback metadata, threads, errno and permission failures passed");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
from test_quest_xr import collect_device_metrics, summarize_samples, validate_samples
|
||||
import subprocess
|
||||
|
||||
|
||||
class QuestXrValidationTest(unittest.TestCase):
|
||||
def samples(self, source='native'):
|
||||
return [{'rendered': count, 'sessionState': 5, 'headFlags': 15, 'source': source,
|
||||
'controllersActive': [False, False], 'guestFrames': count,
|
||||
'uploadedFrames': count, 'sourceFrameId': count + 1, 'steamvrState': 4}
|
||||
for count in (0, 144)]
|
||||
|
||||
def test_native_and_surface_are_separate_diagnostics(self):
|
||||
self.assertIsNone(validate_samples(self.samples()))
|
||||
self.assertIsNone(validate_samples(self.samples('surface'), surface=True))
|
||||
self.assertEqual('Wrong projection source', validate_samples(self.samples('surface')))
|
||||
|
||||
def test_position_must_be_tracked_not_merely_valid(self):
|
||||
samples = self.samples()
|
||||
for sample in samples:
|
||||
sample['headFlags'] = 7
|
||||
self.assertIn('tracked 6DoF', validate_samples(samples))
|
||||
|
||||
def test_optional_controllers_cannot_be_claimed_as_tested(self):
|
||||
self.assertIn('controllers', validate_samples(self.samples(), require_controllers=True))
|
||||
|
||||
def test_one_early_tracked_sample_cannot_hide_final_tracking_loss(self):
|
||||
samples = self.samples()
|
||||
samples[-1]['headFlags'] = 7
|
||||
self.assertIn('final sample', validate_samples(samples))
|
||||
samples[-1]['headFlags'] = 15
|
||||
samples[-1]['sessionState'] = 4
|
||||
self.assertIn('final sample', validate_samples(samples))
|
||||
|
||||
def test_measured_rates_are_separate_from_runtime_refresh(self):
|
||||
samples = self.samples('frames')
|
||||
samples[0]['_hostMonotonicSeconds'] = 10
|
||||
samples[-1]['_hostMonotonicSeconds'] = 12
|
||||
samples[-1]['periodNs'] = 8333333
|
||||
summary = summarize_samples(samples)
|
||||
self.assertEqual(summary['observedSeconds'], 2)
|
||||
self.assertEqual(summary['ratesHz']['uploadedFrames'], 72)
|
||||
self.assertAlmostEqual(summary['runtimeRefreshHz'], 120, places=3)
|
||||
self.assertEqual(summary['focusedTrackedFraction'], 1)
|
||||
samples[-1]['uploadedFrames'] = -1
|
||||
self.assertIsNone(summarize_samples(samples)['ratesHz']['uploadedFrames'])
|
||||
self.assertIsNone(summarize_samples(self.samples())['observedSeconds'])
|
||||
self.assertIsNone(summarize_samples([])['focusedTrackedFraction'])
|
||||
|
||||
def test_metrics_failures_stay_unavailable_without_privileged_fallback(self):
|
||||
commands = []
|
||||
def adb(*command):
|
||||
commands.append(command)
|
||||
if command[-1] == 'battery':
|
||||
return 'temperature: 310'
|
||||
raise subprocess.CalledProcessError(1, command, stderr='Permission denied')
|
||||
metrics = collect_device_metrics(adb, 'com.droiddeck.launcher')
|
||||
self.assertEqual(len(commands), 3)
|
||||
self.assertTrue(metrics['battery']['available'])
|
||||
self.assertFalse(metrics['thermal']['available'])
|
||||
self.assertFalse(metrics['memory']['available'])
|
||||
|
||||
def test_steamvr_requires_owned_images_and_source_frame_ids(self):
|
||||
samples = self.samples('frames')
|
||||
self.assertIsNone(validate_samples(samples, guest=True, steamvr=True))
|
||||
samples[-1]['uploadedFrames'] = 0
|
||||
self.assertIn('owned SteamVR images', validate_samples(samples, guest=True, steamvr=True))
|
||||
samples[-1]['uploadedFrames'] = 144
|
||||
samples[-1]['sourceFrameId'] = samples[0]['sourceFrameId']
|
||||
self.assertIn('frame IDs', validate_samples(samples, guest=True, steamvr=True))
|
||||
|
||||
def test_running_process_alone_is_not_success(self):
|
||||
samples = self.samples('frames')
|
||||
samples[-1]['rendered'] = 0
|
||||
self.assertIn('stereo frames', validate_samples(samples, guest=True, steamvr=True))
|
||||
samples[-1]['rendered'] = 144
|
||||
samples[-1]['steamvrState'] = 3
|
||||
self.assertIn('RUNNING', validate_samples(samples, guest=True, steamvr=True))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,18 @@
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
|
||||
@unittest.skipUnless(shutil.which("gcc"), "needs gcc on a Linux host")
|
||||
class RobustPreloadTest(unittest.TestCase):
|
||||
def test_native_and_missing_syscall_contract(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
executable = Path(directory) / "robust-preload-test"
|
||||
harness = Path(__file__).with_name("robust_preload_harness.c")
|
||||
subprocess.run(["gcc", "-O2", "-Wall", "-Wextra", "-Werror", "-pthread",
|
||||
str(harness), "-ldl", "-o", str(executable)], check=True)
|
||||
result = subprocess.run([str(executable)], capture_output=True, text=True, timeout=20)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
self.assertIn("permission failures passed", result.stdout)
|
||||
@@ -0,0 +1,80 @@
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
SOURCE = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin/droiddeck-steamvr'
|
||||
|
||||
|
||||
class SteamVrHelperTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temporary = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.temporary.cleanup)
|
||||
self.root = Path(self.temporary.name)
|
||||
self.home = self.root / 'home'
|
||||
self.runtime = self.root / 'runtime'
|
||||
self.commands = self.root / 'commands'
|
||||
self.driver = self.root / 'driver/droiddeck/bin/linuxarm64'
|
||||
self.steamvr = self.root / 'steamvr'
|
||||
for directory in (self.commands, self.driver, self.steamvr / 'bin/linuxarm64'):
|
||||
directory.mkdir(parents=True)
|
||||
self.helper = self.root / 'droiddeck-steamvr'
|
||||
self.helper.write_text(SOURCE.read_text(), encoding='utf-8')
|
||||
self.executable(self.commands / 'pgrep', 'exit 1')
|
||||
self.executable(self.commands / 'sleep', 'exit 0')
|
||||
self.executable(self.commands / 'xset', 'exit 0')
|
||||
self.executable(self.commands / 'setsid', 'exec "$@"')
|
||||
self.executable(self.driver / 'xr-display-mode', 'echo 3360x1760@120')
|
||||
self.executable(self.steamvr / 'bin/linuxarm64/vrstartup',
|
||||
'echo "display=${DROIDDECK_VR_DISPLAY_MODE:-unset}"')
|
||||
(self.driver / 'driver_droiddeck.so').touch()
|
||||
self.environment = dict(os.environ)
|
||||
self.environment.pop('DROIDDECK_VR_DISPLAY_MODE', None)
|
||||
self.environment.pop('DROIDDECK_VR_PRESENT', None)
|
||||
self.environment.update(HOME=str(self.home), XDG_RUNTIME_DIR=str(self.runtime),
|
||||
STEAMVR=str(self.steamvr), DROIDDECK_VR_DIR=str(self.root / 'driver'),
|
||||
PATH=str(self.commands) + os.pathsep + os.environ['PATH'])
|
||||
|
||||
def executable(self, destination, body):
|
||||
destination.write_text('#!/bin/sh\n' + body + '\n', encoding='utf-8')
|
||||
destination.chmod(0o755)
|
||||
|
||||
def start(self):
|
||||
result = subprocess.run(['sh', str(self.helper), 'start'], env=self.environment,
|
||||
capture_output=True, text=True, timeout=5)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
log = self.home / '.local/share/Steam/logs/droiddeck-vrstartup.log'
|
||||
deadline = time.monotonic() + 2
|
||||
while time.monotonic() < deadline:
|
||||
if log.is_file() and 'display=' in log.read_text():
|
||||
return log
|
||||
time.sleep(0.01)
|
||||
self.fail('Startup log did not arrive')
|
||||
|
||||
def test_fresh_runtime_mode_is_selected_and_log_survives_runtime_cleanup(self):
|
||||
log = self.start()
|
||||
self.assertEqual((self.runtime / 'droiddeck-steamvr').stat().st_mode & 0o777, 0o700)
|
||||
self.assertEqual(log.read_text().strip(), 'display=3360x1760@120')
|
||||
self.assertEqual((self.runtime / 'droiddeck-steamvr/vrstartup.log').resolve(), log)
|
||||
shutil.rmtree(self.runtime)
|
||||
self.assertTrue(log.is_file())
|
||||
|
||||
def test_explicit_display_override_is_preserved(self):
|
||||
self.environment['DROIDDECK_VR_DISPLAY_MODE'] = '1920x1080@90'
|
||||
self.assertEqual(self.start().read_text().strip(), 'display=1920x1080@90')
|
||||
|
||||
def test_stale_or_missing_tracking_keeps_the_default(self):
|
||||
self.executable(self.driver / 'xr-display-mode', 'exit 1')
|
||||
self.assertEqual(self.start().read_text().strip(), 'display=unset')
|
||||
|
||||
def test_older_runtime_without_the_reader_still_starts(self):
|
||||
(self.driver / 'xr-display-mode').unlink()
|
||||
self.assertEqual(self.start().read_text().strip(), 'display=unset')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,45 @@
|
||||
import ctypes.util
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
|
||||
@unittest.skipUnless(shutil.which("gcc"), "needs gcc on a Linux host")
|
||||
class VrProducerLeaseTest(unittest.TestCase):
|
||||
def compile_and_run(self, source, headers=None, gpu=False):
|
||||
repository = Path(__file__).resolve().parents[2]
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
executable = Path(directory) / "producer-test"
|
||||
command = ["gcc", "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror", "-Wno-unused-parameter", "-pthread"]
|
||||
if headers:
|
||||
command += ["-I" + str(headers)]
|
||||
command += [str(repository / "tools/vr/tests" / source), "-ldl"]
|
||||
if gpu:
|
||||
command += ["-l:libvulkan.so.1"]
|
||||
subprocess.run(command + ["-o", str(executable)], check=True)
|
||||
environment = dict(os.environ)
|
||||
if gpu:
|
||||
manifests = sorted(Path("/usr/share/vulkan/icd.d").glob("lvp_icd*.json"))
|
||||
environment["VK_DRIVER_FILES"] = str(manifests[0])
|
||||
environment["VK_ICD_FILENAMES"] = str(manifests[0])
|
||||
result = subprocess.run([str(executable)], env=environment, capture_output=True, text=True, timeout=20)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
return result.stdout
|
||||
|
||||
def test_protocol_and_retained_ownership(self):
|
||||
self.assertIn("disconnect quarantine passed", self.compile_and_run("test_producer_lease.c"))
|
||||
|
||||
def test_real_gpu_completion_and_frozen_overwrite(self):
|
||||
if not ctypes.util.find_library("vulkan") or not list(Path("/usr/share/vulkan/icd.d").glob("lvp_icd*.json")):
|
||||
self.skipTest("needs Vulkan loader and Lavapipe")
|
||||
repository = Path(__file__).resolve().parents[2]
|
||||
candidates = [os.environ.get("VK_INCLUDE", ""), "/usr/include", repository / "build/vr/include"]
|
||||
candidates += [path for root in (os.environ.get("ANDROID_HOME"), os.environ.get("ANDROID_SDK_ROOT")) if root
|
||||
for path in Path(root).glob("ndk/*/toolchains/llvm/prebuilt/*/sysroot/usr/include")]
|
||||
headers = next((Path(path) for path in candidates if path and (Path(path) / "vulkan/vk_layer.h").is_file()), None)
|
||||
if headers is None:
|
||||
self.skipTest("needs Vulkan headers")
|
||||
self.assertIn("1024 buffer values passed", self.compile_and_run("test_producer_gpu.c", headers=headers, gpu=True))
|
||||
@@ -0,0 +1,26 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
|
||||
@unittest.skipUnless(shutil.which("gcc"), "needs gcc on a Linux host")
|
||||
class VrSemaphoreLayerTest(unittest.TestCase):
|
||||
def test_waits_are_never_fabricated(self):
|
||||
repository = Path(__file__).resolve().parents[2]
|
||||
candidates = [os.environ.get("VK_INCLUDE", ""), "/usr/include", repository / "build/vr/include"]
|
||||
candidates += [path for root in (os.environ.get("ANDROID_HOME"), os.environ.get("ANDROID_SDK_ROOT")) if root
|
||||
for path in Path(root).glob("ndk/*/toolchains/llvm/prebuilt/*/sysroot/usr/include")]
|
||||
headers = next((Path(path) for path in candidates if path and (Path(path) / "vulkan/vk_layer.h").is_file()), None)
|
||||
if headers is None:
|
||||
self.skipTest("needs Vulkan headers")
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
executable = Path(directory) / "semaphore-layer-test"
|
||||
subprocess.run(["gcc", "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror", "-Wno-unused-parameter",
|
||||
"-pthread", "-I" + str(headers), str(repository / "tools/vr/tests/test_semaphore_layer.c"),
|
||||
"-ldl", "-o", str(executable)], check=True)
|
||||
result = subprocess.run([str(executable)], capture_output=True, text=True, timeout=20)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
self.assertIn("fd ownership passed", result.stdout)
|
||||
+5
-1
@@ -29,6 +29,10 @@ compile() {
|
||||
local cmd=(sh -c '
|
||||
set -e
|
||||
mkdir -p build/vr/stub
|
||||
aarch64-linux-gnu-g++ -std=c++17 -O2 -Wall -Wextra -Werror -static-libstdc++ -static-libgcc -pthread \
|
||||
-o build/vr/out/droiddeck/bin/linuxarm64/xr-display-mode tools/vr/src/xr_display_mode.cpp
|
||||
aarch64-linux-gnu-g++ -std=c++17 -O2 -Wall -Wextra -Werror -Wno-missing-field-initializers \
|
||||
-Ibuild/vr/include -o build/vr/test_vulkan_sync_fd tools/vr/tests/test_vulkan_sync_fd.cpp -ldl
|
||||
aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wl,-soname,libwayland-client.so.0 -Itools/vr/third_party/wayland \
|
||||
-o build/vr/stub/libwayland-client.so tools/vr/src/wayland_client_stub.c
|
||||
aarch64-linux-gnu-gcc -c -fPIC -O2 -Itools/vr/third_party/wayland -o build/vr/xdg-shell-protocol.o \
|
||||
@@ -39,7 +43,7 @@ compile() {
|
||||
-o build/vr/out/droiddeck/bin/linuxarm64/driver_droiddeck.so tools/vr/src/driver_droiddeck.cpp tools/vr/src/presenter.cpp \
|
||||
build/vr/xdg-shell-protocol.o -Lbuild/vr/stub -lwayland-client -ldl
|
||||
aarch64-linux-gnu-gcc -std=c11 -shared -fPIC -O2 -Wall -Wextra -Wno-unused-parameter -fvisibility=hidden -pthread \
|
||||
-Wl,-z,defs -Ibuild/vr/include -o build/vr/out/layer/libVkLayer_droiddeck_vr.so tools/vr/src/vk_layer_droiddeck_vr.c -ldl
|
||||
-Wl,-z,defs -Wl,-z,nodelete -Ibuild/vr/include -o build/vr/out/layer/libVkLayer_droiddeck_vr.so tools/vr/src/vk_layer_droiddeck_vr.c -ldl
|
||||
aarch64-linux-gnu-strip --strip-unneeded build/vr/out/droiddeck/bin/linuxarm64/driver_droiddeck.so \
|
||||
build/vr/out/layer/libVkLayer_droiddeck_vr.so')
|
||||
if [ "$native" = 1 ]; then
|
||||
|
||||
@@ -17,6 +17,10 @@
|
||||
|
||||
#include "../../../app/src/main/cpp/waylandcomp/src/headpose_shm.h"
|
||||
#include "presenter.h"
|
||||
#include "xr_tracking_file.h"
|
||||
#include "xr_frame_math.h"
|
||||
#include "xr_vsync_timing.h"
|
||||
#include "xr_touch_input.h"
|
||||
|
||||
#define DD_EXPORT extern "C" __attribute__((visibility("default")))
|
||||
|
||||
@@ -87,6 +91,27 @@ HmdQuaternion_t QuatFromMatrix(const float m[9]) {
|
||||
return q;
|
||||
}
|
||||
|
||||
HmdMatrix34_t XrPoseMatrix(const DroidDeckXrPose &pose) {
|
||||
const float qx = pose.orientation[0], qy = pose.orientation[1], qz = pose.orientation[2], qw = pose.orientation[3];
|
||||
HmdMatrix34_t matrix{};
|
||||
matrix.m[0][0] = 1 - 2 * (qy * qy + qz * qz); matrix.m[0][1] = 2 * (qx * qy - qw * qz); matrix.m[0][2] = 2 * (qx * qz + qw * qy);
|
||||
matrix.m[1][0] = 2 * (qx * qy + qw * qz); matrix.m[1][1] = 1 - 2 * (qx * qx + qz * qz); matrix.m[1][2] = 2 * (qy * qz - qw * qx);
|
||||
matrix.m[2][0] = 2 * (qx * qz - qw * qy); matrix.m[2][1] = 2 * (qy * qz + qw * qx); matrix.m[2][2] = 1 - 2 * (qx * qx + qy * qy);
|
||||
for (int axis = 0; axis < 3; axis++) matrix.m[axis][3] = pose.position[axis];
|
||||
return matrix;
|
||||
}
|
||||
|
||||
HmdMatrix34_t XrEyeToHead(const DroidDeckXrPose &head, const DroidDeckXrPose &eye) {
|
||||
HmdMatrix34_t headMatrix = XrPoseMatrix(head), eyeMatrix = XrPoseMatrix(eye), result{};
|
||||
for (int row = 0; row < 3; row++) {
|
||||
for (int column = 0; column < 3; column++)
|
||||
for (int inner = 0; inner < 3; inner++) result.m[row][column] += headMatrix.m[inner][row] * eyeMatrix.m[inner][column];
|
||||
for (int inner = 0; inner < 3; inner++)
|
||||
result.m[row][3] += headMatrix.m[inner][row] * (eyeMatrix.m[inner][3] - headMatrix.m[inner][3]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
class VirtualDisplay : public IVRVirtualDisplay {
|
||||
public:
|
||||
void Present(const PresentInfo_t *info, uint32_t size) override {
|
||||
@@ -114,26 +139,32 @@ private:
|
||||
|
||||
class Vsync {
|
||||
public:
|
||||
static constexpr std::chrono::nanoseconds kInterval{16666667};
|
||||
static constexpr auto kInterval = XrVsyncTiming::kDefaultPeriod;
|
||||
static constexpr std::chrono::microseconds kLead{2000};
|
||||
|
||||
static void Start() {
|
||||
if (running_.exchange(true)) return;
|
||||
timing_.Reset();
|
||||
thread_ = std::thread([] {
|
||||
XrTrackingFile xrFile;
|
||||
auto next = std::chrono::steady_clock::now() + kInterval;
|
||||
while (running_) {
|
||||
auto interval = kInterval;
|
||||
DroidDeckXrTracking xr;
|
||||
if (xrFile.Read(&xr) && XrTrackingFile::Fresh(xr)) {
|
||||
interval = std::chrono::nanoseconds(xr.period_ns);
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
next = timing_.Target(now, next, xr.predicted_ns, interval);
|
||||
}
|
||||
auto announce = next - kLead;
|
||||
std::this_thread::sleep_until(announce);
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
double offset = std::chrono::duration<double>(announce - now).count();
|
||||
double offset = std::chrono::duration<double>(next - now).count();
|
||||
timing_.Record(next, interval);
|
||||
VRServerDriverHost()->VsyncEvent(offset);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
last_ = next;
|
||||
}
|
||||
next += kInterval;
|
||||
next += interval;
|
||||
now = std::chrono::steady_clock::now();
|
||||
while (next <= now) next += kInterval;
|
||||
while (next <= now) next += interval;
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -141,20 +172,22 @@ public:
|
||||
running_ = false;
|
||||
if (thread_.joinable()) thread_.join();
|
||||
}
|
||||
static std::chrono::steady_clock::time_point LastTick() {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return last_;
|
||||
static std::chrono::steady_clock::time_point Deadline(uint32_t framesToThrottle) {
|
||||
return timing_.Deadline(framesToThrottle);
|
||||
}
|
||||
|
||||
private:
|
||||
static inline std::atomic<bool> running_{false};
|
||||
static inline std::thread thread_;
|
||||
static inline std::mutex mutex_;
|
||||
static inline std::chrono::steady_clock::time_point last_{};
|
||||
static inline XrVsyncTiming timing_;
|
||||
};
|
||||
|
||||
class DirectMode : public IVRDriverDirectModeComponent {
|
||||
public:
|
||||
void ConfigureXr(const DroidDeckXrTracking &tracking) {
|
||||
xrWidth_ = tracking.eyes[0].width * 2; xrHeight_ = tracking.eyes[0].height;
|
||||
for (int eye = 0; eye < 2; eye++) eyeToHead_[eye] = XrEyeToHead(tracking.head, tracking.eyes[eye].pose);
|
||||
}
|
||||
struct Set {
|
||||
SharedTextureHandle_t handles[3] = {};
|
||||
int fds[3] = {-1, -1, -1};
|
||||
@@ -187,7 +220,7 @@ public:
|
||||
out->unTextureFlags = 0;
|
||||
Log(" -> handles 0x%llx 0x%llx 0x%llx, fds %d %d %d", (unsigned long long)set.handles[0],
|
||||
(unsigned long long)set.handles[1], (unsigned long long)set.handles[2], set.fds[0], set.fds[1], set.fds[2]);
|
||||
droiddeck::Presenter::AddTextures(set.handles, set.fds, d->nFormat, d->nWidth, d->nHeight);
|
||||
droiddeck::Presenter::AddTextures(set.handles, set.fds, d->nFormat, d->nWidth, d->nHeight, pid);
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
sets_.push_back(set);
|
||||
}
|
||||
@@ -223,10 +256,18 @@ public:
|
||||
void SubmitLayer(const SubmitLayerPerEye_t (&eyes)[2]) override {
|
||||
layers_++;
|
||||
if (layerInFrame_++ == 0) {
|
||||
frame_.render = {};
|
||||
if (xrWidth_) {
|
||||
frame_.render.magic = DROIDDECK_XR_FRAME_MAGIC; frame_.render.version = DROIDDECK_XR_FRAME_VERSION;
|
||||
frame_.render.width = xrWidth_; frame_.render.height = xrHeight_;
|
||||
frame_.render.bytes = xrWidth_ * xrHeight_ * 4; frame_.render.color_space = 1;
|
||||
}
|
||||
for (int e = 0; e < 2; e++) {
|
||||
frame_.texture[e] = eyes[e].hTexture;
|
||||
frame_.bounds[e][0] = eyes[e].bounds.uMin; frame_.bounds[e][1] = eyes[e].bounds.vMin;
|
||||
frame_.bounds[e][2] = eyes[e].bounds.uMax; frame_.bounds[e][3] = eyes[e].bounds.vMax;
|
||||
std::memcpy(frame_.render.projection[e], eyes[e].mProjection.m, sizeof(eyes[e].mProjection.m));
|
||||
xr_frame_eye_pose(frame_.render.eye_pose[e], eyes[e].mHmdPose.m, eyeToHead_[e].m);
|
||||
}
|
||||
}
|
||||
if (frames_ < 3)
|
||||
@@ -237,6 +278,8 @@ public:
|
||||
|
||||
void Present(SharedTextureHandle_t sync) override {
|
||||
frames_++;
|
||||
frame_.render.frame_id = frames_; frame_.render.captured_ns = XrTrackingFile::Now();
|
||||
frame_.layers = layerInFrame_;
|
||||
if (layerInFrame_) droiddeck::Presenter::Submit(frame_);
|
||||
if (layerInFrame_ != lastLayerCount_) {
|
||||
static int said;
|
||||
@@ -250,8 +293,7 @@ public:
|
||||
}
|
||||
|
||||
void PostPresent(const Throttling_t *t) override {
|
||||
auto last = Vsync::LastTick();
|
||||
auto until = last + Vsync::kInterval * (1 + (t ? t->nFramesToThrottle : 0));
|
||||
auto until = Vsync::Deadline(t ? t->nFramesToThrottle : 0);
|
||||
if (until > std::chrono::steady_clock::now()) std::this_thread::sleep_until(until);
|
||||
}
|
||||
|
||||
@@ -277,6 +319,8 @@ private:
|
||||
uint64_t frames_ = 0, layers_ = 0;
|
||||
int layerInFrame_ = 0, lastLayerCount_ = -1;
|
||||
droiddeck::EyeFrame frame_{};
|
||||
uint32_t xrWidth_ = 0, xrHeight_ = 0;
|
||||
HmdMatrix34_t eyeToHead_[2]{};
|
||||
};
|
||||
|
||||
struct PadState {
|
||||
@@ -342,12 +386,17 @@ public:
|
||||
EVRInitError Activate(uint32_t id) override {
|
||||
id_ = id;
|
||||
PropertyContainerHandle_t c = VRProperties()->TrackedDeviceToPropertyContainer(id_);
|
||||
VRProperties()->SetStringProperty(c, Prop_ModelNumber_String, "DroidDeck Pad Hand");
|
||||
VRProperties()->SetStringProperty(c, Prop_ManufacturerName_String, "DroidDeck");
|
||||
XrTrackingFile tracking;
|
||||
DroidDeckXrTracking packet{};
|
||||
xrMode_ = tracking.Read(&packet) && XrTrackingFile::Fresh(packet);
|
||||
VRProperties()->SetStringProperty(c, Prop_ModelNumber_String, xrMode_ ? "DroidDeck Quest Touch" : "DroidDeck Pad Hand");
|
||||
VRProperties()->SetStringProperty(c, Prop_ManufacturerName_String, xrMode_ ? "Meta" : "DroidDeck");
|
||||
VRProperties()->SetStringProperty(c, Prop_TrackingSystemName_String, "droiddeck");
|
||||
VRProperties()->SetStringProperty(c, Prop_RenderModelName_String, "generic_controller");
|
||||
VRProperties()->SetStringProperty(c, Prop_ControllerType_String, "vive_controller");
|
||||
VRProperties()->SetStringProperty(c, Prop_InputProfilePath_String, "{htc}/input/vive_controller_profile.json");
|
||||
VRProperties()->SetStringProperty(c, Prop_RenderModelName_String, xrMode_ ?
|
||||
(right_ ? "oculus_quest_plus_controller_right" : "oculus_quest_plus_controller_left") : "generic_controller");
|
||||
VRProperties()->SetStringProperty(c, Prop_ControllerType_String, xrMode_ ? "oculus_touch" : "vive_controller");
|
||||
VRProperties()->SetStringProperty(c, Prop_InputProfilePath_String, xrMode_ ?
|
||||
"{oculus}/input/touch_profile.json" : "{htc}/input/vive_controller_profile.json");
|
||||
VRProperties()->SetInt32Property(c, Prop_ControllerRoleHint_Int32,
|
||||
right_ ? TrackedControllerRole_RightHand : TrackedControllerRole_LeftHand);
|
||||
VRProperties()->SetInt32Property(c, Prop_DeviceClass_Int32, TrackedDeviceClass_Controller);
|
||||
@@ -357,12 +406,21 @@ public:
|
||||
in->CreateBooleanComponent(c, "/input/grip/click", &grip_);
|
||||
in->CreateBooleanComponent(c, "/input/trigger/click", &triggerClick_);
|
||||
in->CreateScalarComponent(c, "/input/trigger/value", &trigger_, VRScalarType_Absolute, VRScalarUnits_NormalizedOneSided);
|
||||
in->CreateScalarComponent(c, "/input/trackpad/x", &padX_, VRScalarType_Absolute, VRScalarUnits_NormalizedTwoSided);
|
||||
in->CreateScalarComponent(c, "/input/trackpad/y", &padY_, VRScalarType_Absolute, VRScalarUnits_NormalizedTwoSided);
|
||||
in->CreateBooleanComponent(c, "/input/trackpad/click", &padClick_);
|
||||
in->CreateBooleanComponent(c, "/input/trackpad/touch", &padTouch_);
|
||||
in->CreateScalarComponent(c, xrMode_ ? "/input/joystick/x" : "/input/trackpad/x", &padX_, VRScalarType_Absolute, VRScalarUnits_NormalizedTwoSided);
|
||||
in->CreateScalarComponent(c, xrMode_ ? "/input/joystick/y" : "/input/trackpad/y", &padY_, VRScalarType_Absolute, VRScalarUnits_NormalizedTwoSided);
|
||||
in->CreateBooleanComponent(c, xrMode_ ? "/input/joystick/click" : "/input/trackpad/click", &padClick_);
|
||||
in->CreateBooleanComponent(c, xrMode_ ? "/input/joystick/touch" : "/input/trackpad/touch", &padTouch_);
|
||||
if (xrMode_) {
|
||||
in->CreateScalarComponent(c, "/input/grip/value", &gripValue_, VRScalarType_Absolute, VRScalarUnits_NormalizedOneSided);
|
||||
in->CreateBooleanComponent(c, "/input/trigger/touch", &triggerTouch_);
|
||||
in->CreateBooleanComponent(c, right_ ? "/input/a/click" : "/input/x/click", &primaryClick_);
|
||||
in->CreateBooleanComponent(c, right_ ? "/input/b/click" : "/input/y/click", &secondaryClick_);
|
||||
in->CreateBooleanComponent(c, right_ ? "/input/a/touch" : "/input/x/touch", &primaryTouch_);
|
||||
in->CreateBooleanComponent(c, right_ ? "/input/b/touch" : "/input/y/touch", &secondaryTouch_);
|
||||
in->CreateBooleanComponent(c, "/input/thumbrest/touch", &thumbrestTouch_);
|
||||
}
|
||||
in->CreateHapticComponent(c, "/output/haptic", &haptic_);
|
||||
Log("%s hand activated (game pad)", right_ ? "right" : "left");
|
||||
Log("%s hand activated (%s)", right_ ? "right" : "left", xrMode_ ? "Quest Touch" : "game pad");
|
||||
return VRInitError_None;
|
||||
}
|
||||
void Deactivate() override { id_ = k_unTrackedDeviceIndexInvalid; }
|
||||
@@ -371,6 +429,44 @@ public:
|
||||
void DebugRequest(const char *, char *resp, uint32_t size) override { if (size) resp[0] = 0; }
|
||||
|
||||
DriverPose_t GetPose() override { return pose_; }
|
||||
VRInputComponentHandle_t HapticHandle() const { return haptic_; }
|
||||
|
||||
void UpdateXr(const DroidDeckXrHand &hand, int64_t predicted) {
|
||||
if (id_ == k_unTrackedDeviceIndexInvalid) return;
|
||||
DriverPose_t pose{};
|
||||
pose.qWorldFromDriverRotation.w = pose.qDriverFromHeadRotation.w = 1;
|
||||
pose.qRotation.w = 1;
|
||||
pose.deviceIsConnected = hand.active;
|
||||
pose.poseIsValid = hand.active && XrTrackingFile::Tracked(hand.pose);
|
||||
if (pose.poseIsValid) {
|
||||
pose.qRotation = {hand.pose.orientation[3], hand.pose.orientation[0], hand.pose.orientation[1], hand.pose.orientation[2]};
|
||||
for (int axis = 0; axis < 3; axis++) pose.vecPosition[axis] = hand.pose.position[axis];
|
||||
pose.poseTimeOffset = XrTrackingFile::PoseTimeOffset(predicted, XrTrackingFile::Now());
|
||||
}
|
||||
pose.result = pose.poseIsValid ? TrackingResult_Running_OK : TrackingResult_Running_OutOfRange;
|
||||
pose_ = pose;
|
||||
VRServerDriverHost()->TrackedDevicePoseUpdated(id_, pose, sizeof(pose));
|
||||
XrTouchInput input = xr_touch_input(hand, right_);
|
||||
auto driverInput = VRDriverInput();
|
||||
driverInput->UpdateScalarComponent(trigger_, input.trigger, 0);
|
||||
driverInput->UpdateBooleanComponent(triggerClick_, input.triggerClick, 0);
|
||||
driverInput->UpdateBooleanComponent(grip_, input.gripClick, 0);
|
||||
driverInput->UpdateScalarComponent(padX_, input.stickX, 0);
|
||||
driverInput->UpdateScalarComponent(padY_, input.stickY, 0);
|
||||
driverInput->UpdateBooleanComponent(padClick_, input.stickClick, 0);
|
||||
driverInput->UpdateBooleanComponent(padTouch_, input.stickTouch, 0);
|
||||
driverInput->UpdateBooleanComponent(menu_, input.menuClick, 0);
|
||||
driverInput->UpdateBooleanComponent(system_, false, 0);
|
||||
if (xrMode_) {
|
||||
driverInput->UpdateScalarComponent(gripValue_, input.grip, 0);
|
||||
driverInput->UpdateBooleanComponent(triggerTouch_, input.triggerTouch, 0);
|
||||
driverInput->UpdateBooleanComponent(primaryClick_, input.primaryClick, 0);
|
||||
driverInput->UpdateBooleanComponent(secondaryClick_, input.secondaryClick, 0);
|
||||
driverInput->UpdateBooleanComponent(primaryTouch_, input.primaryTouch, 0);
|
||||
driverInput->UpdateBooleanComponent(secondaryTouch_, input.secondaryTouch, 0);
|
||||
driverInput->UpdateBooleanComponent(thumbrestTouch_, input.thumbrestTouch, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void Update(const HmdQuaternion_t &head, const PadState *pad) {
|
||||
if (id_ == k_unTrackedDeviceIndexInvalid) return;
|
||||
@@ -409,10 +505,13 @@ public:
|
||||
|
||||
private:
|
||||
bool right_;
|
||||
bool xrMode_ = false;
|
||||
uint32_t id_ = k_unTrackedDeviceIndexInvalid;
|
||||
DriverPose_t pose_ = {};
|
||||
VRInputComponentHandle_t system_ = 0, menu_ = 0, grip_ = 0, triggerClick_ = 0, trigger_ = 0, padX_ = 0, padY_ = 0,
|
||||
padClick_ = 0, padTouch_ = 0, haptic_ = 0;
|
||||
VRInputComponentHandle_t gripValue_ = 0, triggerTouch_ = 0, primaryClick_ = 0, secondaryClick_ = 0,
|
||||
primaryTouch_ = 0, secondaryTouch_ = 0, thumbrestTouch_ = 0;
|
||||
};
|
||||
|
||||
class CardboardHmd : public ITrackedDeviceServerDriver, public IVRDisplayComponent {
|
||||
@@ -426,6 +525,12 @@ public:
|
||||
if (height_ <= 0) height_ = 1080;
|
||||
float hz = VRSettings()->GetFloat(kSettingsSection, "displayFrequency");
|
||||
if (hz <= 0.0f) hz = 60.0f;
|
||||
DroidDeckXrTracking xr;
|
||||
xrMode_ = xr_.Read(&xr) && XrTrackingFile::Fresh(xr);
|
||||
if (xrMode_) {
|
||||
width_ = xr.eyes[0].width * 2; height_ = xr.eyes[0].height;
|
||||
hz = 1000000000.f / xr.period_ns;
|
||||
}
|
||||
tanHalf_ = 1.0f;
|
||||
eyeAspect_ = (width_ * 0.5f) / height_;
|
||||
droiddeck_headpose hp;
|
||||
@@ -433,10 +538,20 @@ public:
|
||||
tanHalf_ = hp.tan_half_fov;
|
||||
if (hp.eye_aspect > 0.0f) eyeAspect_ = hp.eye_aspect;
|
||||
}
|
||||
VRProperties()->SetStringProperty(c, Prop_ModelNumber_String, "DroidDeck Cardboard");
|
||||
VRProperties()->SetStringProperty(c, Prop_ModelNumber_String, xrMode_ ? "DroidDeck Quest OpenXR" : "DroidDeck Cardboard");
|
||||
VRProperties()->SetStringProperty(c, Prop_ManufacturerName_String, "DroidDeck");
|
||||
VRProperties()->SetStringProperty(c, Prop_TrackingSystemName_String, "droiddeck");
|
||||
VRProperties()->SetFloatProperty(c, Prop_UserIpdMeters_Float, 0.064f);
|
||||
if (xrMode_) {
|
||||
VRServerDriverHost()->SetDisplayEyeToHead(id_, XrEyeToHead(xr.head, xr.eyes[0].pose), XrEyeToHead(xr.head, xr.eyes[1].pose));
|
||||
dm_.ConfigureXr(xr);
|
||||
float distanceSquared = 0;
|
||||
for (int axis = 0; axis < 3; axis++) {
|
||||
float difference = xr.eyes[0].pose.position[axis] - xr.eyes[1].pose.position[axis];
|
||||
distanceSquared += difference * difference;
|
||||
}
|
||||
VRProperties()->SetFloatProperty(c, Prop_UserIpdMeters_Float, std::sqrt(distanceSquared));
|
||||
}
|
||||
VRProperties()->SetFloatProperty(c, Prop_UserHeadToEyeDepthMeters_Float, 0.0f);
|
||||
VRProperties()->SetFloatProperty(c, Prop_DisplayFrequency_Float, hz);
|
||||
VRProperties()->SetFloatProperty(c, Prop_SecondsFromVsyncToPhotons_Float, 0.011f);
|
||||
@@ -488,6 +603,19 @@ public:
|
||||
p.qWorldFromDriverRotation.w = 1;
|
||||
p.qDriverFromHeadRotation.w = 1;
|
||||
p.qRotation.w = 1;
|
||||
DroidDeckXrTracking xr;
|
||||
bool freshXr = xr_.Read(&xr) && XrTrackingFile::Fresh(xr);
|
||||
if (freshXr || xrMode_) {
|
||||
p.deviceIsConnected = true;
|
||||
p.poseIsValid = freshXr && XrTrackingFile::Tracked(xr.head);
|
||||
p.result = p.poseIsValid ? TrackingResult_Running_OK : TrackingResult_Running_OutOfRange;
|
||||
if (p.poseIsValid) {
|
||||
p.qRotation = {xr.head.orientation[3], xr.head.orientation[0], xr.head.orientation[1], xr.head.orientation[2]};
|
||||
for (int axis = 0; axis < 3; axis++) p.vecPosition[axis] = xr.head.position[axis];
|
||||
p.poseTimeOffset = XrTrackingFile::PoseTimeOffset(xr.predicted_ns, XrTrackingFile::Now());
|
||||
}
|
||||
return p;
|
||||
}
|
||||
p.vecPosition[1] = 1.6;
|
||||
droiddeck_headpose hp;
|
||||
bool ok = pose_.Read(&hp);
|
||||
@@ -509,6 +637,7 @@ public:
|
||||
bool IsDisplayOnDesktop() override { return !directMode_; }
|
||||
bool IsDisplayRealDisplay() override { return false; }
|
||||
void GetRecommendedRenderTargetSize(uint32_t *w, uint32_t *h) override {
|
||||
if (xrMode_) { *w = width_ / 2; *h = height_; return; }
|
||||
*w = (uint32_t)(width_ / 2 * 1.4f);
|
||||
*h = (uint32_t)(height_ * 1.4f);
|
||||
}
|
||||
@@ -516,7 +645,14 @@ public:
|
||||
*y = 0; *w = (uint32_t)width_ / 2; *h = (uint32_t)height_;
|
||||
*x = eye == Eye_Left ? 0 : (uint32_t)width_ / 2;
|
||||
}
|
||||
void GetProjectionRaw(EVREye, float *left, float *right, float *top, float *bottom) override {
|
||||
void GetProjectionRaw(EVREye eye, float *left, float *right, float *top, float *bottom) override {
|
||||
DroidDeckXrTracking xr;
|
||||
if (xrMode_ && xr_.Read(&xr) && XrTrackingFile::Fresh(xr)) {
|
||||
const float *fov = xr.eyes[eye == Eye_Right ? 1 : 0].fov;
|
||||
*left = std::tan(fov[0]); *right = std::tan(fov[1]);
|
||||
*top = -std::tan(fov[2]); *bottom = -std::tan(fov[3]);
|
||||
return;
|
||||
}
|
||||
*left = -tanHalf_ * eyeAspect_; *right = tanHalf_ * eyeAspect_;
|
||||
*top = -tanHalf_; *bottom = tanHalf_;
|
||||
}
|
||||
@@ -536,6 +672,19 @@ private:
|
||||
uint32_t lastSeq = 0;
|
||||
auto lastSent = std::chrono::steady_clock::now();
|
||||
while (running_) {
|
||||
DroidDeckXrTracking xr;
|
||||
if (xrMode_) {
|
||||
bool fresh = xr_.Read(&xr) && XrTrackingFile::Fresh(xr);
|
||||
DriverPose_t head = GetPose();
|
||||
VRServerDriverHost()->TrackedDevicePoseUpdated(id_, head, sizeof(head));
|
||||
for (int hand = 0; hand < 2; hand++) if (hands_[hand]) {
|
||||
DroidDeckXrHand state = fresh ? xr.hands[hand] : DroidDeckXrHand{};
|
||||
hands_[hand]->UpdateXr(state, fresh ? xr.predicted_ns : 0);
|
||||
}
|
||||
VRDriverInput()->UpdateBooleanComponent(proximity_, fresh, 0);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(2));
|
||||
continue;
|
||||
}
|
||||
droiddeck_headpose hp;
|
||||
bool fresh = pose_.Read(&hp) && hp.seq != lastSeq;
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
@@ -570,6 +719,8 @@ public:
|
||||
|
||||
private:
|
||||
PoseFile pose_;
|
||||
XrTrackingFile xr_;
|
||||
bool xrMode_ = false;
|
||||
PadRing pad_;
|
||||
PadState lastPad_;
|
||||
VRInputComponentHandle_t proximity_ = 0;
|
||||
@@ -596,17 +747,49 @@ public:
|
||||
}
|
||||
return VRInitError_None;
|
||||
}
|
||||
void Cleanup() override { VR_CLEANUP_SERVER_DRIVER_CONTEXT(); }
|
||||
void Cleanup() override {
|
||||
if (haptics_) { munmap(haptics_, sizeof(*haptics_)); haptics_ = nullptr; }
|
||||
VR_CLEANUP_SERVER_DRIVER_CONTEXT();
|
||||
}
|
||||
const char *const *GetInterfaceVersions() override { return k_InterfaceVersions; }
|
||||
void RunFrame() override {
|
||||
VREvent_t e;
|
||||
while (VRServerDriverHost()->PollNextEvent(&e, sizeof(e))) {}
|
||||
while (VRServerDriverHost()->PollNextEvent(&e, sizeof(e))) {
|
||||
if (e.eventType == VREvent_Input_HapticVibration) {
|
||||
const auto &pulse = e.data.hapticVibration;
|
||||
int hand = pulse.componentHandle == left_.HapticHandle() ? 0 : pulse.componentHandle == right_.HapticHandle() ? 1 : -1;
|
||||
if (hand >= 0) SendHaptic(hand, pulse);
|
||||
}
|
||||
}
|
||||
}
|
||||
bool ShouldBlockStandbyMode() override { return false; }
|
||||
void EnterStandby() override {}
|
||||
void LeaveStandby() override {}
|
||||
|
||||
private:
|
||||
DroidDeckXrHaptics *haptics_ = nullptr;
|
||||
void SendHaptic(int hand, const VREvent_HapticVibration_t &pulse) {
|
||||
if (!std::isfinite(pulse.fAmplitude) || !std::isfinite(pulse.fFrequency) || !std::isfinite(pulse.fDurationSeconds)) return;
|
||||
if (!haptics_) {
|
||||
int descriptor = open("/run/droiddeck/" DROIDDECK_XR_HAPTIC_FILE, O_RDWR | O_CLOEXEC);
|
||||
if (descriptor < 0) return;
|
||||
struct stat status{};
|
||||
if (fstat(descriptor, &status) != 0 || status.st_size != sizeof(*haptics_)) { close(descriptor); return; }
|
||||
void *mapping = mmap(nullptr, sizeof(*haptics_), PROT_READ | PROT_WRITE, MAP_SHARED, descriptor, 0);
|
||||
close(descriptor);
|
||||
if (mapping == MAP_FAILED) return;
|
||||
haptics_ = static_cast<DroidDeckXrHaptics *>(mapping);
|
||||
}
|
||||
uint32_t sequence = __atomic_load_n(&haptics_->seq, __ATOMIC_RELAXED);
|
||||
__atomic_store_n(&haptics_->seq, sequence | 1u, __ATOMIC_SEQ_CST);
|
||||
__atomic_thread_fence(__ATOMIC_SEQ_CST);
|
||||
auto &target = haptics_->hands[hand];
|
||||
target.id++;
|
||||
target.duration_ns = (int64_t)(std::clamp(pulse.fDurationSeconds, 0.001f, 1.f) * 1000000000.0);
|
||||
target.amplitude = std::clamp(pulse.fAmplitude, 0.f, 1.f);
|
||||
target.frequency = std::max(pulse.fFrequency, 0.f);
|
||||
__atomic_store_n(&haptics_->seq, (sequence | 1u) + 1u, __ATOMIC_RELEASE);
|
||||
}
|
||||
CardboardHmd hmd_;
|
||||
CardboardController left_{false}, right_{true};
|
||||
};
|
||||
|
||||
+252
-30
@@ -9,6 +9,9 @@ typedef unsigned long VisualID;
|
||||
#include "xdg-shell-client-protocol.h"
|
||||
|
||||
#include "presenter.h"
|
||||
#include "xr_tracking_file.h"
|
||||
#include "xr_frame_channel.h"
|
||||
#include "producer_lease.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
@@ -36,7 +39,7 @@ void Log(const char *fmt, ...) {
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(buf, sizeof(buf), fmt, ap);
|
||||
va_end(ap);
|
||||
if (vr::VRDriverLog()) vr::VRDriverLog()->Log(buf);
|
||||
if (vr::VRDriverContext() && vr::VRDriverLog()) vr::VRDriverLog()->Log(buf);
|
||||
fprintf(stderr, "[droiddeck] %s\n", buf);
|
||||
}
|
||||
|
||||
@@ -73,13 +76,15 @@ struct X11 {
|
||||
X(GetPhysicalDeviceMemoryProperties) X(CreateDevice) X(GetDeviceProcAddr) X(CreateXlibSurfaceKHR) \
|
||||
X(CreateWaylandSurfaceKHR) \
|
||||
X(GetPhysicalDeviceSurfaceSupportKHR) X(GetPhysicalDeviceSurfaceCapabilitiesKHR) \
|
||||
X(GetPhysicalDeviceSurfaceFormatsKHR) X(GetPhysicalDeviceSurfacePresentModesKHR) X(GetPhysicalDeviceProperties)
|
||||
X(GetPhysicalDeviceSurfaceFormatsKHR) X(GetPhysicalDeviceSurfacePresentModesKHR) X(GetPhysicalDeviceProperties) X(DestroySurfaceKHR)
|
||||
#define VK_DEVICE(X) \
|
||||
X(GetDeviceQueue) X(CreateSwapchainKHR) X(DestroySwapchainKHR) X(GetSwapchainImagesKHR) X(AcquireNextImageKHR) \
|
||||
X(QueuePresentKHR) X(CreateCommandPool) X(AllocateCommandBuffers) X(BeginCommandBuffer) X(EndCommandBuffer) \
|
||||
X(ResetCommandBuffer) X(CmdPipelineBarrier) X(CmdBlitImage) X(CmdClearColorImage) X(QueueSubmit) X(CreateFence) \
|
||||
X(WaitForFences) X(ResetFences) X(CreateSemaphore) X(CreateImage) X(DestroyImage) X(GetImageMemoryRequirements) \
|
||||
X(AllocateMemory) X(FreeMemory) X(BindImageMemory) X(DeviceWaitIdle)
|
||||
X(AllocateMemory) X(FreeMemory) X(BindImageMemory) X(DeviceWaitIdle) X(DestroyDevice) X(DestroyFence) X(DestroySemaphore) \
|
||||
X(DestroyCommandPool) X(CreateBuffer) X(DestroyBuffer) X(GetBufferMemoryRequirements) X(BindBufferMemory) \
|
||||
X(MapMemory) X(UnmapMemory) X(InvalidateMappedMemoryRanges) X(CmdCopyImageToBuffer)
|
||||
|
||||
struct Vk {
|
||||
void *lib = nullptr;
|
||||
@@ -94,12 +99,31 @@ struct Imported {
|
||||
VkImage image = VK_NULL_HANDLE;
|
||||
VkDeviceMemory memory = VK_NULL_HANDLE;
|
||||
uint32_t width = 0, height = 0;
|
||||
uint32_t producerPid = 0;
|
||||
};
|
||||
|
||||
struct PendingImport {
|
||||
uint64_t handle;
|
||||
int fd;
|
||||
uint32_t format, width, height;
|
||||
uint32_t format, width, height, producerPid;
|
||||
};
|
||||
|
||||
class CaptureLease {
|
||||
public:
|
||||
explicit CaptureLease(uint32_t pid) : acquired_(droiddeck_producer_acquire(pid, &lease_)) {}
|
||||
~CaptureLease() {
|
||||
if (lease_.descriptor < 0) return;
|
||||
if (gpuComplete_) droiddeck_producer_release(&lease_);
|
||||
else close(lease_.descriptor);
|
||||
}
|
||||
bool Acquired() const { return acquired_; }
|
||||
void BeginGpu() { gpuComplete_ = false; }
|
||||
void EndGpu() { gpuComplete_ = true; }
|
||||
bool Release() { return gpuComplete_ && droiddeck_producer_release(&lease_); }
|
||||
private:
|
||||
droiddeck_producer_lease lease_{-1, 0};
|
||||
bool acquired_;
|
||||
bool gpuComplete_ = true;
|
||||
};
|
||||
|
||||
VkFormat UnormOf(uint32_t f) {
|
||||
@@ -117,65 +141,90 @@ public:
|
||||
return impl;
|
||||
}
|
||||
|
||||
void Add(const uint64_t handles[3], const int fds[3], uint32_t format, uint32_t w, uint32_t h) {
|
||||
void Add(const uint64_t handles[3], const int fds[3], uint32_t format, uint32_t w, uint32_t h, uint32_t producerPid) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (failed_ || exit_) return;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (fds[i] < 0) continue;
|
||||
int fd = dup(fds[i]);
|
||||
if (fd >= 0) pending_.push_back({handles[i], fd, format, w, h});
|
||||
if (fd >= 0) pending_.push_back({handles[i], fd, format, w, h, producerPid});
|
||||
}
|
||||
EnsureThread();
|
||||
cv_.notify_one();
|
||||
cv_.notify_all();
|
||||
}
|
||||
|
||||
void Remove(uint64_t handle) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
removals_.push_back(handle);
|
||||
cv_.notify_one();
|
||||
cv_.notify_all();
|
||||
}
|
||||
|
||||
void Submit(const EyeFrame &f) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
if (failed_ || exit_) return;
|
||||
bool blocking = FrameChannelRequested();
|
||||
if (blocking) cv_.wait(lock, [this] { return failed_ || exit_ || (!haveFrame_ && !inFlight_); });
|
||||
if (failed_ || exit_) return;
|
||||
EnsureThread();
|
||||
frame_ = f;
|
||||
frameAt_ = std::chrono::steady_clock::now();
|
||||
haveFrame_ = true;
|
||||
submitted_++;
|
||||
cv_.notify_one();
|
||||
uint64_t ticket = ++ticket_;
|
||||
cv_.notify_all();
|
||||
if (blocking) cv_.wait(lock, [this, ticket] { return failed_ || completed_ >= ticket; });
|
||||
}
|
||||
|
||||
void Shutdown() {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
exit_ = true;
|
||||
cv_.notify_one();
|
||||
cv_.notify_all();
|
||||
}
|
||||
if (thread_.joinable()) thread_.join();
|
||||
}
|
||||
|
||||
private:
|
||||
static bool FrameChannelRequested() {
|
||||
const char *mode = getenv("DROIDDECK_VR_PRESENT");
|
||||
return mode && !strcmp(mode, "questframes");
|
||||
}
|
||||
|
||||
void Fail() {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
failed_ = true;
|
||||
for (const auto &pending : pending_) close(pending.fd);
|
||||
pending_.clear(); cv_.notify_all();
|
||||
}
|
||||
void EnsureThread() {
|
||||
if (!started_) {
|
||||
started_ = true;
|
||||
thread_ = std::thread([this] { Run(); });
|
||||
thread_ = std::thread([this] {
|
||||
try { Run(); }
|
||||
catch (const std::exception &error) { Log("presenter: %s", error.what()); }
|
||||
Cleanup(); Fail();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Run() {
|
||||
const char *mode = getenv("DROIDDECK_VR_PRESENT");
|
||||
wayland_ = !(mode && !strcmp(mode, "x11")) && InitWayland();
|
||||
if (!wayland_ && !InitWindow()) {
|
||||
frameChannel_ = FrameChannelRequested();
|
||||
if (!frameChannel_) wayland_ = !(mode && !strcmp(mode, "x11")) && InitWayland();
|
||||
if (!frameChannel_ && !wayland_ && !InitWindow()) {
|
||||
Log("presenter: unavailable, frames are not shown");
|
||||
return;
|
||||
Fail(); return;
|
||||
}
|
||||
if (!InitVulkan()) {
|
||||
Log("presenter: unavailable, frames are not shown");
|
||||
return;
|
||||
Fail(); return;
|
||||
}
|
||||
uint64_t shown = 0;
|
||||
auto lastLog = std::chrono::steady_clock::now();
|
||||
while (true) {
|
||||
EyeFrame f;
|
||||
uint64_t ticket = 0;
|
||||
std::chrono::steady_clock::time_point at;
|
||||
std::vector<PendingImport> imports;
|
||||
std::vector<uint64_t> removals;
|
||||
@@ -189,13 +238,19 @@ private:
|
||||
at = frameAt_;
|
||||
bool draw = haveFrame_;
|
||||
haveFrame_ = false;
|
||||
inFlight_ = draw; ticket = ticket_;
|
||||
if (!draw) f.texture[0] = f.texture[1] = 0;
|
||||
}
|
||||
if (wl_) { wl_display_dispatch_pending(wl_); wl_display_flush(wl_); }
|
||||
for (auto &p : imports) Import(p);
|
||||
for (auto h : removals) Forget(h);
|
||||
if (f.texture[0]) std::this_thread::sleep_until(at + guard_);
|
||||
if (f.texture[0] && Draw(f)) shown++;
|
||||
if (f.texture[0] && !frameChannel_) std::this_thread::sleep_until(at + guard_);
|
||||
if (f.texture[0] && (frameChannel_ ? Capture(f) : Draw(f))) shown++;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
inFlight_ = false; completed_ = ticket; cv_.notify_all();
|
||||
if (failed_) break;
|
||||
}
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
if (now - lastLog > std::chrono::seconds(10)) {
|
||||
Log("presenter: %llu frames shown, %llu submitted (last 10 s)", (unsigned long long)shown,
|
||||
@@ -204,7 +259,23 @@ private:
|
||||
lastLog = now;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Cleanup() {
|
||||
if (dev_) vk_.DeviceWaitIdle(dev_);
|
||||
DestroyCapture();
|
||||
while (!textures_.empty()) Forget(textures_.back().handle);
|
||||
if (dev_) {
|
||||
if (swapchain_) vk_.DestroySwapchainKHR(dev_, swapchain_, nullptr);
|
||||
if (acquired_) vk_.DestroySemaphore(dev_, acquired_, nullptr);
|
||||
if (rendered_) vk_.DestroySemaphore(dev_, rendered_, nullptr);
|
||||
if (fence_) vk_.DestroyFence(dev_, fence_, nullptr);
|
||||
if (pool_) vk_.DestroyCommandPool(dev_, pool_, nullptr);
|
||||
vk_.DestroyDevice(dev_, nullptr); dev_ = VK_NULL_HANDLE;
|
||||
}
|
||||
if (surface_) vk_.DestroySurfaceKHR(inst_, surface_, nullptr);
|
||||
if (inst_) vk_.DestroyInstance(inst_, nullptr);
|
||||
surface_ = VK_NULL_HANDLE; inst_ = VK_NULL_HANDLE;
|
||||
}
|
||||
|
||||
std::chrono::microseconds guard_{4000};
|
||||
@@ -268,6 +339,11 @@ private:
|
||||
if (pread(fd, eye, sizeof(eye), 72) == (ssize_t)sizeof(eye) && eye[0] && eye[1]) { width_ = eye[0] * 2; height_ = eye[1]; }
|
||||
close(fd);
|
||||
}
|
||||
XrTrackingFile xrFile;
|
||||
DroidDeckXrTracking tracking;
|
||||
if (xrFile.Read(&tracking) && XrTrackingFile::Fresh(tracking)) {
|
||||
width_ = tracking.eyes[0].width * 2; height_ = tracking.eyes[0].height;
|
||||
}
|
||||
if (!width_ && cfgW_ > 0 && cfgH_ > 0) { width_ = (uint32_t)cfgW_; height_ = (uint32_t)cfgH_; }
|
||||
if (!width_) { width_ = 1920; height_ = 1080; }
|
||||
Log("presenter: %ux%u fullscreen window on Wayland display %s (configured %s)", width_, height_, name, configured_ ? "yes" : "no");
|
||||
@@ -306,8 +382,9 @@ private:
|
||||
VK_GLOBAL(X)
|
||||
#undef X
|
||||
const char *iexts[] = {"VK_KHR_surface", wayland_ ? "VK_KHR_wayland_surface" : "VK_KHR_xlib_surface"};
|
||||
VkApplicationInfo app = {VK_STRUCTURE_TYPE_APPLICATION_INFO, nullptr, "droiddeck-vr-presenter", 1, nullptr, 0, VK_API_VERSION_1_1};
|
||||
VkInstanceCreateInfo ici = {VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, nullptr, 0, &app, 0, nullptr, 2, iexts};
|
||||
VkApplicationInfo app = {VK_STRUCTURE_TYPE_APPLICATION_INFO, nullptr, "droiddeck-vr-presenter", 1, nullptr, 0, VK_API_VERSION_1_2};
|
||||
VkInstanceCreateInfo ici = {VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, nullptr, 0, &app, 0, nullptr,
|
||||
frameChannel_ ? 0u : 2u, frameChannel_ ? nullptr : iexts};
|
||||
VkResult r = vk_.CreateInstance(&ici, nullptr, &inst_);
|
||||
if (r != VK_SUCCESS) { Log("presenter: vkCreateInstance %d", r); return false; }
|
||||
#define X(n) vk_.n = (PFN_vk##n)vk_.GetInstanceProcAddr(inst_, "vk" #n);
|
||||
@@ -322,10 +399,10 @@ private:
|
||||
vk_.GetPhysicalDeviceProperties(pd_, &props);
|
||||
vk_.GetPhysicalDeviceMemoryProperties(pd_, &mem_);
|
||||
|
||||
if (wayland_) {
|
||||
if (!frameChannel_ && wayland_) {
|
||||
VkWaylandSurfaceCreateInfoKHR wci = {VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR, nullptr, 0, wl_, wlSurf_};
|
||||
if ((r = vk_.CreateWaylandSurfaceKHR(inst_, &wci, nullptr, &surface_)) != VK_SUCCESS) { Log("presenter: wayland surface %d", r); return false; }
|
||||
} else {
|
||||
} else if (!frameChannel_) {
|
||||
VkXlibSurfaceCreateInfoKHR sci = {VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR, nullptr, 0, dpy_, win_};
|
||||
if ((r = vk_.CreateXlibSurfaceKHR(inst_, &sci, nullptr, &surface_)) != VK_SUCCESS) { Log("presenter: xlib surface %d", r); return false; }
|
||||
}
|
||||
@@ -337,14 +414,15 @@ private:
|
||||
qfam_ = UINT32_MAX;
|
||||
for (uint32_t i = 0; i < nq; i++) {
|
||||
VkBool32 present = VK_FALSE;
|
||||
vk_.GetPhysicalDeviceSurfaceSupportKHR(pd_, i, surface_, &present);
|
||||
if (frameChannel_) present = VK_TRUE;
|
||||
else vk_.GetPhysicalDeviceSurfaceSupportKHR(pd_, i, surface_, &present);
|
||||
if ((qf[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) && present) { qfam_ = i; break; }
|
||||
}
|
||||
if (qfam_ == UINT32_MAX) { Log("presenter: no queue can present"); return false; }
|
||||
float prio = 1.0f;
|
||||
VkDeviceQueueCreateInfo qci = {VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, nullptr, 0, qfam_, 1, &prio};
|
||||
const char *dexts[] = {"VK_KHR_swapchain", "VK_KHR_external_memory_fd"};
|
||||
VkDeviceCreateInfo dci = {VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, nullptr, 0, 1, &qci, 0, nullptr, 2, dexts, nullptr};
|
||||
const char *dexts[] = {"VK_KHR_external_memory_fd", "VK_KHR_swapchain"};
|
||||
VkDeviceCreateInfo dci = {VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, nullptr, 0, 1, &qci, 0, nullptr, frameChannel_ ? 1u : 2u, dexts, nullptr};
|
||||
if ((r = vk_.CreateDevice(pd_, &dci, nullptr, &dev_)) != VK_SUCCESS) { Log("presenter: vkCreateDevice %d", r); return false; }
|
||||
#define X(n) vk_.n = (PFN_vk##n)vk_.GetDeviceProcAddr(dev_, "vk" #n);
|
||||
VK_DEVICE(X)
|
||||
@@ -361,7 +439,7 @@ private:
|
||||
VkSemaphoreCreateInfo semci = {VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, nullptr, 0};
|
||||
vk_.CreateSemaphore(dev_, &semci, nullptr, &acquired_);
|
||||
vk_.CreateSemaphore(dev_, &semci, nullptr, &rendered_);
|
||||
if (!CreateSwapchain()) return false;
|
||||
if (!frameChannel_ && !CreateSwapchain()) return false;
|
||||
Log("presenter: Vulkan on %s, queue family %u", props.deviceName, qfam_);
|
||||
return true;
|
||||
}
|
||||
@@ -423,6 +501,7 @@ private:
|
||||
Imported im{p.handle};
|
||||
im.width = p.width;
|
||||
im.height = p.height;
|
||||
im.producerPid = p.producerPid;
|
||||
const char *fl = getenv("DROIDDECK_VR_IMPORT_FORMAT_LIST");
|
||||
const bool formatList = !fl || strcmp(fl, "0") != 0;
|
||||
VkFormat formats[2] = {(VkFormat)p.format, UnormOf(p.format)};
|
||||
@@ -439,7 +518,9 @@ private:
|
||||
vk_.GetImageMemoryRequirements(dev_, im.image, &req);
|
||||
VkImportMemoryFdInfoKHR imp = {VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR, nullptr, VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT, p.fd};
|
||||
VkMemoryDedicatedAllocateInfo ded = {VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO, &imp, im.image, VK_NULL_HANDLE};
|
||||
VkMemoryAllocateInfo mai = {VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, &ded, req.size, (uint32_t)MemoryType(req.memoryTypeBits)};
|
||||
int memoryType = MemoryType(req.memoryTypeBits);
|
||||
if (memoryType < 0) { vk_.DestroyImage(dev_, im.image, nullptr); close(p.fd); return; }
|
||||
VkMemoryAllocateInfo mai = {VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, &ded, req.size, (uint32_t)memoryType};
|
||||
r = vk_.AllocateMemory(dev_, &mai, nullptr, &im.memory);
|
||||
if (r != VK_SUCCESS) {
|
||||
Log("presenter: import memory (fd %d, %llu bytes) %d", p.fd, (unsigned long long)req.size, r);
|
||||
@@ -447,7 +528,10 @@ private:
|
||||
vk_.DestroyImage(dev_, im.image, nullptr);
|
||||
return;
|
||||
}
|
||||
vk_.BindImageMemory(dev_, im.image, im.memory, 0);
|
||||
if (vk_.BindImageMemory(dev_, im.image, im.memory, 0) != VK_SUCCESS) {
|
||||
vk_.DestroyImage(dev_, im.image, nullptr); vk_.FreeMemory(dev_, im.memory, nullptr);
|
||||
return;
|
||||
}
|
||||
textures_.push_back(im);
|
||||
Log("presenter: imported texture 0x%llx (%ux%u, format %u)", (unsigned long long)p.handle, p.width, p.height, p.format);
|
||||
}
|
||||
@@ -476,6 +560,137 @@ private:
|
||||
return b;
|
||||
}
|
||||
|
||||
void DestroyCapture() {
|
||||
if (!dev_) return;
|
||||
if (captureImage_) vk_.DestroyImage(dev_, captureImage_, nullptr);
|
||||
if (captureMemory_) vk_.FreeMemory(dev_, captureMemory_, nullptr);
|
||||
if (readbackBuffer_) vk_.DestroyBuffer(dev_, readbackBuffer_, nullptr);
|
||||
if (readbackMemory_) vk_.FreeMemory(dev_, readbackMemory_, nullptr);
|
||||
captureImage_ = VK_NULL_HANDLE; captureMemory_ = readbackMemory_ = VK_NULL_HANDLE;
|
||||
readbackBuffer_ = VK_NULL_HANDLE; captureWidth_ = captureHeight_ = 0;
|
||||
}
|
||||
|
||||
bool PrepareCapture(const DroidDeckXrFrameHeader &header) {
|
||||
if (captureWidth_ == header.width && captureHeight_ == header.height) return true;
|
||||
DestroyCapture();
|
||||
auto fail = [this](VkResult result) { Log("presenter: capture allocation failed %d", result); DestroyCapture(); return false; };
|
||||
VkImageCreateInfo image{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
|
||||
image.imageType = VK_IMAGE_TYPE_2D; image.format = VK_FORMAT_R8G8B8A8_UNORM;
|
||||
image.extent = {header.width, header.height, 1}; image.mipLevels = image.arrayLayers = 1;
|
||||
image.samples = VK_SAMPLE_COUNT_1_BIT; image.tiling = VK_IMAGE_TILING_OPTIMAL;
|
||||
image.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
|
||||
VkResult result = vk_.CreateImage(dev_, &image, nullptr, &captureImage_);
|
||||
if (result != VK_SUCCESS) return fail(result);
|
||||
VkMemoryRequirements requirements{}; vk_.GetImageMemoryRequirements(dev_, captureImage_, &requirements);
|
||||
int type = MemoryType(requirements.memoryTypeBits);
|
||||
if (type < 0) return fail(VK_ERROR_FEATURE_NOT_PRESENT);
|
||||
VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
|
||||
allocation.allocationSize = requirements.size; allocation.memoryTypeIndex = type;
|
||||
result = vk_.AllocateMemory(dev_, &allocation, nullptr, &captureMemory_);
|
||||
if (result != VK_SUCCESS) return fail(result);
|
||||
result = vk_.BindImageMemory(dev_, captureImage_, captureMemory_, 0);
|
||||
if (result != VK_SUCCESS) return fail(result);
|
||||
VkBufferCreateInfo buffer{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; buffer.size = header.bytes;
|
||||
buffer.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
||||
result = vk_.CreateBuffer(dev_, &buffer, nullptr, &readbackBuffer_);
|
||||
if (result != VK_SUCCESS) return fail(result);
|
||||
vk_.GetBufferMemoryRequirements(dev_, readbackBuffer_, &requirements);
|
||||
type = -1;
|
||||
for (uint32_t index = 0; index < mem_.memoryTypeCount; index++) {
|
||||
if ((requirements.memoryTypeBits & (1u << index)) && (mem_.memoryTypes[index].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)) {
|
||||
type = index;
|
||||
if (mem_.memoryTypes[index].propertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) break;
|
||||
}
|
||||
}
|
||||
if (type < 0) return fail(VK_ERROR_FEATURE_NOT_PRESENT);
|
||||
allocation.allocationSize = requirements.size; allocation.memoryTypeIndex = type;
|
||||
result = vk_.AllocateMemory(dev_, &allocation, nullptr, &readbackMemory_);
|
||||
if (result != VK_SUCCESS) return fail(result);
|
||||
result = vk_.BindBufferMemory(dev_, readbackBuffer_, readbackMemory_, 0);
|
||||
if (result != VK_SUCCESS) return fail(result);
|
||||
captureWidth_ = header.width; captureHeight_ = header.height;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Capture(const EyeFrame &frame) {
|
||||
if (frame.layers != 1 || !xr_frame_valid(frame.render)) {
|
||||
static unsigned said = 0;
|
||||
if (said++ < 3) Log("presenter: refusing Quest frame with unsupported layer count or render geometry (%u layers)", frame.layers);
|
||||
return false;
|
||||
}
|
||||
const Imported *eye[2] = {Find(frame.texture[0]), Find(frame.texture[1])};
|
||||
if (!eye[0] || !eye[1]) return false;
|
||||
for (int index = 0; index < 2; index++) {
|
||||
for (float value : frame.bounds[index]) if (!std::isfinite(value) || value < 0 || value > 1) return false;
|
||||
if (frame.bounds[index][0] == frame.bounds[index][2] || frame.bounds[index][1] == frame.bounds[index][3]) return false;
|
||||
}
|
||||
if (!eye[0]->producerPid || eye[0]->producerPid != eye[1]->producerPid) return false;
|
||||
CaptureLease lease(eye[0]->producerPid);
|
||||
if (!lease.Acquired()) {
|
||||
static unsigned said = 0;
|
||||
if (said++ < 3) Log("presenter: refusing Quest frame without a completed, retained producer lease (pid %u)", eye[0]->producerPid);
|
||||
return false;
|
||||
}
|
||||
VkResult result = vk_.WaitForFences(dev_, 1, &fence_, VK_TRUE, UINT64_MAX);
|
||||
if (result != VK_SUCCESS) { Fail(); return false; }
|
||||
if (!PrepareCapture(frame.render)) return false;
|
||||
if (vk_.ResetCommandBuffer(cmd_, 0) != VK_SUCCESS) { Fail(); return false; }
|
||||
VkCommandBufferBeginInfo begin{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; begin.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
|
||||
if (vk_.BeginCommandBuffer(cmd_, &begin) != VK_SUCCESS) { Fail(); return false; }
|
||||
VkImageMemoryBarrier before[] = {
|
||||
Barrier(captureImage_, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_ACCESS_TRANSFER_WRITE_BIT),
|
||||
Barrier(eye[0]->image, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL, 0, VK_ACCESS_TRANSFER_READ_BIT, VK_QUEUE_FAMILY_EXTERNAL, qfam_),
|
||||
Barrier(eye[1]->image, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL, 0, VK_ACCESS_TRANSFER_READ_BIT, VK_QUEUE_FAMILY_EXTERNAL, qfam_)};
|
||||
vk_.CmdPipelineBarrier(cmd_, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr,
|
||||
eye[0] == eye[1] ? 2 : 3, before);
|
||||
int32_t half = frame.render.width / 2;
|
||||
for (int index = 0; index < 2; index++) {
|
||||
const float *bounds = frame.bounds[index];
|
||||
VkImageBlit blit{}; blit.srcSubresource = blit.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
|
||||
blit.srcOffsets[0] = {static_cast<int32_t>(bounds[0] * eye[index]->width), static_cast<int32_t>(bounds[1] * eye[index]->height), 0};
|
||||
blit.srcOffsets[1] = {static_cast<int32_t>(bounds[2] * eye[index]->width), static_cast<int32_t>(bounds[3] * eye[index]->height), 1};
|
||||
blit.dstOffsets[0] = {index * half, 0, 0}; blit.dstOffsets[1] = {(index + 1) * half, static_cast<int32_t>(frame.render.height), 1};
|
||||
vk_.CmdBlitImage(cmd_, eye[index]->image, VK_IMAGE_LAYOUT_GENERAL, captureImage_, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit, VK_FILTER_LINEAR);
|
||||
}
|
||||
VkImageMemoryBarrier after[] = {
|
||||
Barrier(captureImage_, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT),
|
||||
Barrier(eye[0]->image, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL, VK_ACCESS_TRANSFER_READ_BIT, 0, qfam_, VK_QUEUE_FAMILY_EXTERNAL),
|
||||
Barrier(eye[1]->image, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL, VK_ACCESS_TRANSFER_READ_BIT, 0, qfam_, VK_QUEUE_FAMILY_EXTERNAL)};
|
||||
vk_.CmdPipelineBarrier(cmd_, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr,
|
||||
eye[0] == eye[1] ? 2 : 3, after);
|
||||
VkBufferImageCopy copy{}; copy.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
|
||||
copy.imageExtent = {frame.render.width, frame.render.height, 1};
|
||||
vk_.CmdCopyImageToBuffer(cmd_, captureImage_, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, readbackBuffer_, 1, ©);
|
||||
VkBufferMemoryBarrier host{VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER};
|
||||
host.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; host.dstAccessMask = VK_ACCESS_HOST_READ_BIT;
|
||||
host.srcQueueFamilyIndex = host.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||||
host.buffer = readbackBuffer_; host.size = VK_WHOLE_SIZE;
|
||||
vk_.CmdPipelineBarrier(cmd_, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 0, nullptr, 1, &host, 0, nullptr);
|
||||
if (vk_.EndCommandBuffer(cmd_) != VK_SUCCESS || vk_.ResetFences(dev_, 1, &fence_) != VK_SUCCESS) { Fail(); return false; }
|
||||
VkSubmitInfo submit{VK_STRUCTURE_TYPE_SUBMIT_INFO}; submit.commandBufferCount = 1; submit.pCommandBuffers = &cmd_;
|
||||
lease.BeginGpu();
|
||||
result = vk_.QueueSubmit(queue_, 1, &submit, fence_);
|
||||
if (result != VK_SUCCESS) {
|
||||
if (vk_.DeviceWaitIdle(dev_) == VK_SUCCESS) lease.EndGpu();
|
||||
Fail(); return false;
|
||||
}
|
||||
result = vk_.WaitForFences(dev_, 1, &fence_, VK_TRUE, UINT64_MAX);
|
||||
if (result != VK_SUCCESS) {
|
||||
if (vk_.DeviceWaitIdle(dev_) == VK_SUCCESS) lease.EndGpu();
|
||||
Fail(); return false;
|
||||
}
|
||||
lease.EndGpu();
|
||||
if (!lease.Release()) return false;
|
||||
void *mapping = nullptr;
|
||||
if (vk_.MapMemory(dev_, readbackMemory_, 0, VK_WHOLE_SIZE, 0, &mapping) != VK_SUCCESS) return false;
|
||||
VkMappedMemoryRange range{VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE}; range.memory = readbackMemory_; range.size = VK_WHOLE_SIZE;
|
||||
result = vk_.InvalidateMappedMemoryRanges(dev_, 1, &range);
|
||||
bool sent = result == VK_SUCCESS && sender_.Send("/run/droiddeck/" DROIDDECK_XR_FRAME_SOCKET,
|
||||
frame.render, static_cast<unsigned char *>(mapping));
|
||||
vk_.UnmapMemory(dev_, readbackMemory_);
|
||||
return sent;
|
||||
}
|
||||
|
||||
bool Draw(const EyeFrame &f) {
|
||||
const Imported *eye[2] = {Find(f.texture[0]), Find(f.texture[1])};
|
||||
if (!eye[0] || !eye[1]) return false;
|
||||
@@ -545,7 +760,14 @@ private:
|
||||
std::vector<uint64_t> removals_;
|
||||
EyeFrame frame_{};
|
||||
std::chrono::steady_clock::time_point frameAt_{};
|
||||
bool haveFrame_ = false, exit_ = false, started_ = false;
|
||||
bool haveFrame_ = false, exit_ = false, started_ = false, inFlight_ = false, failed_ = false;
|
||||
uint64_t ticket_ = 0, completed_ = 0;
|
||||
bool frameChannel_ = false;
|
||||
XrFrameSender sender_;
|
||||
VkImage captureImage_ = VK_NULL_HANDLE;
|
||||
VkDeviceMemory captureMemory_ = VK_NULL_HANDLE, readbackMemory_ = VK_NULL_HANDLE;
|
||||
VkBuffer readbackBuffer_ = VK_NULL_HANDLE;
|
||||
uint32_t captureWidth_ = 0, captureHeight_ = 0;
|
||||
std::atomic<uint64_t> submitted_{0};
|
||||
std::thread thread_;
|
||||
|
||||
@@ -573,7 +795,7 @@ private:
|
||||
|
||||
}
|
||||
|
||||
void Presenter::AddTextures(const uint64_t h[3], const int fds[3], uint32_t f, uint32_t w, uint32_t hh) { Impl::Get().Add(h, fds, f, w, hh); }
|
||||
void Presenter::AddTextures(const uint64_t h[3], const int fds[3], uint32_t f, uint32_t w, uint32_t hh, uint32_t pid) { Impl::Get().Add(h, fds, f, w, hh, pid); }
|
||||
void Presenter::RemoveTexture(uint64_t h) { Impl::Get().Remove(h); }
|
||||
void Presenter::Submit(const EyeFrame &f) { Impl::Get().Submit(f); }
|
||||
void Presenter::Shutdown() { Impl::Get().Shutdown(); }
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include "xr_frame.h"
|
||||
|
||||
namespace droiddeck {
|
||||
|
||||
struct EyeFrame {
|
||||
uint64_t texture[2];
|
||||
float bounds[2][4];
|
||||
DroidDeckXrFrameHeader render;
|
||||
uint32_t layers = 0;
|
||||
};
|
||||
|
||||
class Presenter {
|
||||
public:
|
||||
static void AddTextures(const uint64_t handles[3], const int fds[3], uint32_t vkFormat, uint32_t width, uint32_t height);
|
||||
static void AddTextures(const uint64_t handles[3], const int fds[3], uint32_t vkFormat, uint32_t width, uint32_t height,
|
||||
uint32_t producerPid);
|
||||
static void RemoveTexture(uint64_t handle);
|
||||
static void Submit(const EyeFrame &frame);
|
||||
static void Shutdown();
|
||||
|
||||
@@ -0,0 +1,192 @@
|
||||
#pragma once
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <poll.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/un.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define DROIDDECK_PRODUCER_MAGIC 0x44504c31u
|
||||
#define DROIDDECK_PRODUCER_ACQUIRE 1u
|
||||
#define DROIDDECK_PRODUCER_READY 2u
|
||||
#define DROIDDECK_PRODUCER_RELEASE 3u
|
||||
#define DROIDDECK_PRODUCER_RELEASED 4u
|
||||
|
||||
struct droiddeck_producer_message {
|
||||
uint32_t magic;
|
||||
uint32_t command;
|
||||
uint64_t ticket;
|
||||
};
|
||||
|
||||
static inline int64_t droiddeck_producer_now(void) {
|
||||
struct timespec current;
|
||||
clock_gettime(CLOCK_MONOTONIC, ¤t);
|
||||
return (int64_t)current.tv_sec * 1000000000LL + current.tv_nsec;
|
||||
}
|
||||
|
||||
static inline int droiddeck_producer_path(char *path, size_t capacity, uint32_t pid) {
|
||||
const char *runtime = getenv("XDG_RUNTIME_DIR");
|
||||
int length = snprintf(path, capacity, "%s/droiddeck-steamvr/producer-%u.sock",
|
||||
runtime && *runtime ? runtime : "/run/droiddeck", pid);
|
||||
return pid && length > 0 && (size_t)length < capacity;
|
||||
}
|
||||
|
||||
static inline int droiddeck_producer_private_parent(const char *path) {
|
||||
char parent[sizeof(((struct sockaddr_un *)0)->sun_path)];
|
||||
if (strlen(path) >= sizeof(parent)) return 0;
|
||||
strcpy(parent, path);
|
||||
char *separator = strrchr(parent, '/');
|
||||
if (!separator) return 0;
|
||||
*separator = 0;
|
||||
struct stat metadata;
|
||||
return lstat(parent, &metadata) == 0 && S_ISDIR(metadata.st_mode) &&
|
||||
metadata.st_uid == getuid() && !(metadata.st_mode & 0077);
|
||||
}
|
||||
|
||||
static inline int droiddeck_producer_peer(int descriptor, uint32_t expected_pid) {
|
||||
struct ucred peer;
|
||||
socklen_t length = sizeof(peer);
|
||||
return getsockopt(descriptor, SOL_SOCKET, SO_PEERCRED, &peer, &length) == 0 &&
|
||||
peer.uid == getuid() && (!expected_pid || (uint32_t)peer.pid == expected_pid);
|
||||
}
|
||||
|
||||
static inline int droiddeck_producer_io(int descriptor, struct droiddeck_producer_message *message,
|
||||
int writing, int64_t deadline) {
|
||||
for (;;) {
|
||||
int64_t remaining = deadline - droiddeck_producer_now();
|
||||
if (remaining <= 0) return 0;
|
||||
struct pollfd ready = {descriptor, (short)(writing ? POLLOUT : POLLIN), 0};
|
||||
int result = poll(&ready, 1, (int)((remaining + 999999) / 1000000));
|
||||
if (result < 0 && errno == EINTR) continue;
|
||||
if (result <= 0 || !(ready.revents & ready.events)) return 0;
|
||||
ssize_t transferred = writing ? send(descriptor, message, sizeof(*message), MSG_NOSIGNAL | MSG_DONTWAIT)
|
||||
: recv(descriptor, message, sizeof(*message), MSG_TRUNC | MSG_DONTWAIT);
|
||||
if (transferred < 0 && (errno == EAGAIN || errno == EINTR)) continue;
|
||||
return transferred == (ssize_t)sizeof(*message) && message->magic == DROIDDECK_PRODUCER_MAGIC;
|
||||
}
|
||||
}
|
||||
|
||||
struct droiddeck_producer_lease {
|
||||
int descriptor;
|
||||
uint64_t ticket;
|
||||
};
|
||||
|
||||
static inline int droiddeck_producer_acquire(uint32_t pid, struct droiddeck_producer_lease *lease) {
|
||||
lease->descriptor = -1;
|
||||
lease->ticket = 0;
|
||||
struct sockaddr_un address;
|
||||
memset(&address, 0, sizeof(address));
|
||||
address.sun_family = AF_UNIX;
|
||||
if (!droiddeck_producer_path(address.sun_path, sizeof(address.sun_path), pid) ||
|
||||
!droiddeck_producer_private_parent(address.sun_path)) return 0;
|
||||
int descriptor = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
|
||||
if (descriptor < 0) return 0;
|
||||
int64_t deadline = droiddeck_producer_now() + 250000000LL;
|
||||
struct droiddeck_producer_message message = {DROIDDECK_PRODUCER_MAGIC, DROIDDECK_PRODUCER_ACQUIRE, 0};
|
||||
if (connect(descriptor, (struct sockaddr *)&address, sizeof(address)) != 0 ||
|
||||
!droiddeck_producer_peer(descriptor, pid) ||
|
||||
!droiddeck_producer_io(descriptor, &message, 1, deadline) ||
|
||||
!droiddeck_producer_io(descriptor, &message, 0, deadline) ||
|
||||
message.command != DROIDDECK_PRODUCER_READY || !message.ticket) {
|
||||
close(descriptor);
|
||||
return 0;
|
||||
}
|
||||
lease->descriptor = descriptor;
|
||||
lease->ticket = message.ticket;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static inline int droiddeck_producer_release(struct droiddeck_producer_lease *lease) {
|
||||
if (lease->descriptor < 0) return 0;
|
||||
int64_t deadline = droiddeck_producer_now() + 250000000LL;
|
||||
struct droiddeck_producer_message message = {DROIDDECK_PRODUCER_MAGIC, DROIDDECK_PRODUCER_RELEASE, lease->ticket};
|
||||
int released = droiddeck_producer_io(lease->descriptor, &message, 1, deadline) &&
|
||||
droiddeck_producer_io(lease->descriptor, &message, 0, deadline) &&
|
||||
message.command == DROIDDECK_PRODUCER_RELEASED && message.ticket == lease->ticket;
|
||||
close(lease->descriptor);
|
||||
lease->descriptor = -1;
|
||||
return released;
|
||||
}
|
||||
|
||||
#ifdef DROIDDECK_PRODUCER_SERVER
|
||||
#include <pthread.h>
|
||||
|
||||
struct droiddeck_producer_server {
|
||||
int listener;
|
||||
pthread_t thread;
|
||||
int (*acquire)(int64_t deadline);
|
||||
void (*release)(void);
|
||||
void (*quarantine)(void);
|
||||
char path[sizeof(((struct sockaddr_un *)0)->sun_path)];
|
||||
};
|
||||
|
||||
static void *droiddeck_producer_serve(void *argument) {
|
||||
struct droiddeck_producer_server *server = argument;
|
||||
uint64_t ticket = 0;
|
||||
for (;;) {
|
||||
int connection = accept4(server->listener, NULL, NULL, SOCK_CLOEXEC | SOCK_NONBLOCK);
|
||||
if (connection < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
break;
|
||||
}
|
||||
struct droiddeck_producer_message message;
|
||||
int64_t deadline = droiddeck_producer_now() + 150000000LL;
|
||||
if (!droiddeck_producer_peer(connection, 0) ||
|
||||
!droiddeck_producer_io(connection, &message, 0, deadline) ||
|
||||
message.command != DROIDDECK_PRODUCER_ACQUIRE || message.ticket || !server->acquire(deadline)) {
|
||||
close(connection);
|
||||
continue;
|
||||
}
|
||||
uint64_t granted = ++ticket;
|
||||
message = (struct droiddeck_producer_message){DROIDDECK_PRODUCER_MAGIC, DROIDDECK_PRODUCER_READY, granted};
|
||||
int valid = droiddeck_producer_io(connection, &message, 1, deadline);
|
||||
while (valid) {
|
||||
struct pollfd request = {connection, POLLIN, 0};
|
||||
int ready = poll(&request, 1, -1);
|
||||
if (ready < 0 && errno == EINTR) continue;
|
||||
if (ready <= 0 || !(request.revents & POLLIN)) { valid = 0; break; }
|
||||
valid = droiddeck_producer_io(connection, &message, 0, droiddeck_producer_now() + 100000000LL) &&
|
||||
message.command == DROIDDECK_PRODUCER_RELEASE && message.ticket == granted;
|
||||
break;
|
||||
}
|
||||
if (!valid) {
|
||||
server->quarantine();
|
||||
close(connection);
|
||||
break;
|
||||
}
|
||||
server->release();
|
||||
message = (struct droiddeck_producer_message){DROIDDECK_PRODUCER_MAGIC, DROIDDECK_PRODUCER_RELEASED, granted};
|
||||
droiddeck_producer_io(connection, &message, 1, droiddeck_producer_now() + 100000000LL);
|
||||
close(connection);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int droiddeck_producer_start(struct droiddeck_producer_server *server) {
|
||||
struct sockaddr_un address;
|
||||
memset(&address, 0, sizeof(address));
|
||||
address.sun_family = AF_UNIX;
|
||||
if (!droiddeck_producer_path(address.sun_path, sizeof(address.sun_path), getpid()) ||
|
||||
!droiddeck_producer_private_parent(address.sun_path)) return 0;
|
||||
server->listener = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0);
|
||||
if (server->listener < 0) return 0;
|
||||
if (bind(server->listener, (struct sockaddr *)&address, sizeof(address)) != 0) {
|
||||
close(server->listener);
|
||||
return 0;
|
||||
}
|
||||
if (chmod(address.sun_path, 0600) == 0 && listen(server->listener, 4) == 0 &&
|
||||
pthread_create(&server->thread, NULL, droiddeck_producer_serve, server) == 0) {
|
||||
strcpy(server->path, address.sun_path);
|
||||
return 1;
|
||||
}
|
||||
close(server->listener);
|
||||
unlink(address.sun_path);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
@@ -11,6 +11,8 @@
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#define DROIDDECK_PRODUCER_SERVER
|
||||
#include "producer_lease.h"
|
||||
|
||||
#define LAYER_NAME "VK_LAYER_DROIDDECK_vr"
|
||||
#define EXPORT __attribute__((visibility("default")))
|
||||
@@ -30,13 +32,20 @@ static void say(const char *fmt, const char *arg) {
|
||||
|
||||
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, (void *)uc->uc_mcontext.pc);
|
||||
si->si_addr, program_counter);
|
||||
if (write(2, buf, n)) {}
|
||||
void *frames[48];
|
||||
int count = backtrace(frames, 48);
|
||||
if (count > 1) frames[1] = (void *)uc->uc_mcontext.pc;
|
||||
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)
|
||||
@@ -80,6 +89,7 @@ struct inst {
|
||||
PFN_vkDestroyInstance destroy;
|
||||
int stripped;
|
||||
int active;
|
||||
int producer;
|
||||
};
|
||||
struct dev {
|
||||
VkDevice handle;
|
||||
@@ -87,6 +97,7 @@ struct dev {
|
||||
PFN_vkDestroyDevice destroy;
|
||||
int active;
|
||||
int display;
|
||||
int producer;
|
||||
};
|
||||
|
||||
static int steamvr_running(void) {
|
||||
@@ -187,8 +198,12 @@ static VkResult VKAPI_CALL CreateInstance(const VkInstanceCreateInfo *ci, const
|
||||
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};
|
||||
(PFN_vkDestroyInstance)next_gipa(*out, "vkDestroyInstance"), stripped, active, producer};
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -237,7 +252,8 @@ static VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice pd, const VkDeviceCreat
|
||||
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};
|
||||
g_dev[i] = (struct dev){*out, next_gdpa, (PFN_vkDestroyDevice)next_gdpa(*out, "vkDestroyDevice"), active, display,
|
||||
in && in->producer};
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -246,10 +262,12 @@ static VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice pd, const VkDeviceCreat
|
||||
}
|
||||
|
||||
static void forget_device_semaphores(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;
|
||||
if (d->producer) producer_destroy_device(d, alloc);
|
||||
forget_device_semaphores(device);
|
||||
PFN_vkDestroyDevice f = d->destroy;
|
||||
pthread_mutex_lock(&g_lock);
|
||||
@@ -470,6 +488,109 @@ static PFN_vkVoidFunction display_device_hook(const char *name) {
|
||||
#define SHARED_NAME "droiddeck-shared-sem"
|
||||
#define POLL_US 300
|
||||
|
||||
static pthread_mutex_t g_submission_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
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 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) 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) {
|
||||
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;
|
||||
@@ -477,7 +598,6 @@ struct bridged {
|
||||
int fd;
|
||||
int timeline;
|
||||
unsigned long ino;
|
||||
uint64_t submitted;
|
||||
};
|
||||
static pthread_mutex_t g_br_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static struct bridged g_br[MAX_SEMS];
|
||||
@@ -597,7 +717,10 @@ static VkResult VKAPI_CALL CreateSemaphore(VkDevice d, const VkSemaphoreCreateIn
|
||||
if (timeline) timeline_note(*out, 1);
|
||||
return r;
|
||||
}
|
||||
if (!export_info) 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;
|
||||
@@ -612,13 +735,26 @@ static VkResult VKAPI_CALL CreateSemaphore(VkDevice d, const VkSemaphoreCreateIn
|
||||
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); return r; }
|
||||
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};
|
||||
if (slot) *slot = (struct bridged){*out, d, counter, fd, timeline, fd_ino(fd)};
|
||||
pthread_mutex_unlock(&g_br_lock);
|
||||
if (!slot) { munmap((void *)counter, 4096); close(fd); return r; }
|
||||
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;
|
||||
}
|
||||
@@ -646,6 +782,11 @@ static VkResult VKAPI_CALL GetSemaphoreFdKHR(VkDevice d, const VkSemaphoreGetFdI
|
||||
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;
|
||||
@@ -654,113 +795,29 @@ static VkResult VKAPI_CALL GetSemaphoreFdKHR(VkDevice d, const VkSemaphoreGetFdI
|
||||
|
||||
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);
|
||||
_Atomic uint64_t *counter = map_counter(info->fd);
|
||||
const unsigned long ino = fd_ino(info->fd);
|
||||
close(info->fd);
|
||||
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 && counter) *b = (struct bridged){info->semaphore, d, counter, -1, timeline, ino, 0};
|
||||
else if (counter) munmap((void *)counter, 4096);
|
||||
if (b) *b = (struct bridged){info->semaphore, d, counter, info->fd, timeline, ino};
|
||||
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(timeline ? "imported a shared timeline semaphore (%s): bridged" : "imported a shared binary semaphore (%s): its waits are dropped",
|
||||
program_invocation_short_name);
|
||||
if (said++ < 4) say("imported a shared timeline semaphore (%s): waits retained", program_invocation_short_name);
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
static int drop_wait(VkSemaphore s) {
|
||||
pthread_mutex_lock(&g_br_lock);
|
||||
struct bridged *b = bridged_find(s);
|
||||
int r = b && !b->timeline && b->fd < 0;
|
||||
pthread_mutex_unlock(&g_br_lock);
|
||||
return r;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#define SUBMIT_GRACE_US 3000
|
||||
|
||||
static int submit_drop_wait(VkSemaphore s, uint64_t value) {
|
||||
pthread_mutex_lock(&g_br_lock);
|
||||
struct bridged *b = bridged_find(s);
|
||||
if (!b) { pthread_mutex_unlock(&g_br_lock); return 0; }
|
||||
if (!b->timeline) { int r = b->fd < 0; pthread_mutex_unlock(&g_br_lock); return r; }
|
||||
const int imported = b->fd < 0;
|
||||
const uint64_t queued = b->submitted;
|
||||
VkDevice dev = b->dev;
|
||||
_Atomic uint64_t *counter = b->counter;
|
||||
pthread_mutex_unlock(&g_br_lock);
|
||||
if (!imported || !value || value <= queued) return 0;
|
||||
struct dev *d = dev_for(dev);
|
||||
if (!d) return 0;
|
||||
PFN_vkGetSemaphoreCounterValue get = (PFN_vkGetSemaphoreCounterValue)d->gdpa(dev, "vkGetSemaphoreCounterValue");
|
||||
if (!get) get = (PFN_vkGetSemaphoreCounterValue)d->gdpa(dev, "vkGetSemaphoreCounterValueKHR");
|
||||
PFN_vkSignalSemaphore sig = (PFN_vkSignalSemaphore)d->gdpa(dev, "vkSignalSemaphore");
|
||||
if (!sig) sig = (PFN_vkSignalSemaphore)d->gdpa(dev, "vkSignalSemaphoreKHR");
|
||||
if (!get) return 0;
|
||||
const uint64_t until = now_us() + SUBMIT_GRACE_US;
|
||||
for (;;) {
|
||||
uint64_t local = 0;
|
||||
if (get(dev, s, &local) == VK_SUCCESS && local >= value) return 0;
|
||||
uint64_t shared = atomic_load(counter);
|
||||
if (shared >= value && sig) {
|
||||
VkSemaphoreSignalInfo si = {VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO, NULL, s, shared};
|
||||
if (sig(dev, &si) == VK_SUCCESS) return 0;
|
||||
}
|
||||
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);
|
||||
@@ -786,55 +843,11 @@ static VkResult VKAPI_CALL QueueSubmit(VkQueue q, uint32_t count, const VkSubmit
|
||||
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; j < submits[i].waitSemaphoreCount && !any; j++)
|
||||
any = submit_drop_wait(submits[i].pWaitSemaphores[j], t && j < t->waitSemaphoreValueCount ? t->pWaitSemaphoreValues[j] : 0);
|
||||
}
|
||||
if (!any) {
|
||||
VkResult r = next(q, count, submits, fence);
|
||||
note_submit_signals(count, submits);
|
||||
return r;
|
||||
}
|
||||
VkSubmitInfo *copy = calloc(count, sizeof(*copy));
|
||||
VkTimelineSemaphoreSubmitInfo *tl = calloc(count, sizeof(*tl));
|
||||
void **bufs = calloc(count * 3, sizeof(void *));
|
||||
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;
|
||||
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;
|
||||
const void *rest = NULL;
|
||||
const VkBaseInStructure *prev = NULL;
|
||||
for (const VkBaseInStructure *p = submits[i].pNext; p; prev = p, p = p->pNext) if (p == (const void *)t) { rest = p->pNext; break; }
|
||||
(void)prev;
|
||||
tl[i].pNext = rest;
|
||||
copy[i].pNext = &tl[i];
|
||||
}
|
||||
}
|
||||
VkResult r = next(q, count, copy, fence);
|
||||
note_submit_signals(count, submits);
|
||||
for (uint32_t i = 0; i < count * 3; i++) free(bufs[i]);
|
||||
free(bufs); free(tl); free(copy);
|
||||
return r;
|
||||
struct dev *device = dev_for(q);
|
||||
if (device->producer) { pthread_mutex_lock(&g_submission_lock); producer_record(device, q); }
|
||||
VkResult result = next(q, count, submits, fence);
|
||||
if (device->producer) pthread_mutex_unlock(&g_submission_lock);
|
||||
return result;
|
||||
}
|
||||
|
||||
static VkResult VKAPI_CALL QueueSubmit2(VkQueue q, uint32_t count, const VkSubmitInfo2 *submits, VkFence fence) {
|
||||
@@ -847,47 +860,35 @@ static VkResult VKAPI_CALL QueueSubmit2(VkQueue q, uint32_t count, const VkSubmi
|
||||
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; j < submits[i].waitSemaphoreInfoCount && !any; j++)
|
||||
any = submit_drop_wait(submits[i].pWaitSemaphoreInfos[j].semaphore, submits[i].pWaitSemaphoreInfos[j].value);
|
||||
if (!any) {
|
||||
VkResult r = next(q, count, submits, fence);
|
||||
note_submit2_signals(count, submits);
|
||||
return r;
|
||||
}
|
||||
VkSubmitInfo2 *copy = calloc(count, sizeof(*copy));
|
||||
VkSemaphoreSubmitInfo **waits = calloc(count, sizeof(*waits));
|
||||
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));
|
||||
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];
|
||||
}
|
||||
VkResult r = next(q, count, copy, fence);
|
||||
note_submit2_signals(count, submits);
|
||||
for (uint32_t i = 0; i < count; i++) free(waits[i]);
|
||||
free(waits); free(copy);
|
||||
return r;
|
||||
struct dev *device = dev_for(q);
|
||||
if (device->producer) { pthread_mutex_lock(&g_submission_lock); producer_record(device, q); }
|
||||
VkResult result = next(q, count, submits, fence);
|
||||
if (device->producer) pthread_mutex_unlock(&g_submission_lock);
|
||||
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); }
|
||||
VkResult result = next(queue, info);
|
||||
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 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");
|
||||
VkSemaphore sems[64]; uint64_t vals[64]; uint32_t k = 0;
|
||||
for (uint32_t i = 0; i < info->semaphoreCount && k < 64; i++)
|
||||
if (!drop_wait(info->pSemaphores[i])) { sems[k] = info->pSemaphores[i]; vals[k] = info->pValues[i]; k++; }
|
||||
if (k == info->semaphoreCount) {
|
||||
return next(d, info, timeout);
|
||||
}
|
||||
if (!k) return VK_SUCCESS;
|
||||
VkSemaphoreWaitInfo mine = *info;
|
||||
mine.semaphoreCount = k; mine.pSemaphores = sems; mine.pValues = vals;
|
||||
return next(d, &mine, timeout);
|
||||
return next(d, info, timeout);
|
||||
}
|
||||
|
||||
static VkResult VKAPI_CALL GetSemaphoreCounterValue(VkDevice d, VkSemaphore s, uint64_t *value) {
|
||||
@@ -925,6 +926,8 @@ static PFN_vkVoidFunction semaphore_hook(const char *name) {
|
||||
HOOK_AS("vkQueueSubmit", QueueSubmit)
|
||||
HOOK_AS("vkQueueSubmit2", QueueSubmit2)
|
||||
HOOK_AS("vkQueueSubmit2KHR", QueueSubmit2)
|
||||
HOOK_AS("vkQueuePresentKHR", QueuePresentKHR)
|
||||
HOOK_AS("vkQueueBindSparse", QueueBindSparse)
|
||||
HOOK_AS("vkWaitSemaphores", WaitSemaphores)
|
||||
HOOK_AS("vkWaitSemaphoresKHR", WaitSemaphores)
|
||||
HOOK_AS("vkGetSemaphoreCounterValue", GetSemaphoreCounterValue)
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
#include "xr_display_mode.h"
|
||||
#include <cstdio>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
XrTrackingFile reader(argc > 1 ? argv[1] : "/run/droiddeck/" DROIDDECK_XR_FILE);
|
||||
DroidDeckXrTracking tracking{};
|
||||
if (!reader.Read(&tracking)) return 1;
|
||||
auto mode = XrDisplayMode(tracking);
|
||||
if (mode.empty()) return 1;
|
||||
std::puts(mode.c_str());
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
#include "xr_tracking_file.h"
|
||||
|
||||
inline std::string XrDisplayMode(const DroidDeckXrTracking &tracking) {
|
||||
if (!XrTrackingFile::Fresh(tracking)) return {};
|
||||
auto refresh = static_cast<uint32_t>(std::llround(1000000000.0 / tracking.period_ns));
|
||||
return std::to_string(tracking.eyes[0].width * 2) + "x" + std::to_string(tracking.eyes[0].height)
|
||||
+ "@" + std::to_string(refresh);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <cmath>
|
||||
|
||||
#define DROIDDECK_XR_FRAME_MAGIC 0x46584444u
|
||||
#define DROIDDECK_XR_FRAME_VERSION 1u
|
||||
#define DROIDDECK_XR_FRAME_SOCKET "droiddeck-xr-frames"
|
||||
#define DROIDDECK_XR_FRAME_MAX_BYTES (64u * 1024u * 1024u)
|
||||
|
||||
struct DroidDeckXrFrameHeader {
|
||||
uint32_t magic, version, width, height;
|
||||
uint64_t frame_id;
|
||||
int64_t captured_ns;
|
||||
uint32_t bytes, color_space;
|
||||
float eye_pose[2][3][4];
|
||||
float projection[2][4][4];
|
||||
};
|
||||
|
||||
static_assert(sizeof(DroidDeckXrFrameHeader) == 264, "XR frame protocol layout");
|
||||
|
||||
inline bool xr_frame_valid(const DroidDeckXrFrameHeader &header) {
|
||||
uint64_t bytes = (uint64_t)header.width * header.height * 4;
|
||||
if (header.magic != DROIDDECK_XR_FRAME_MAGIC || header.version != DROIDDECK_XR_FRAME_VERSION
|
||||
|| !header.frame_id || header.width < 2 || (header.width & 1u) || !header.height
|
||||
|| header.width > 8192 || header.height > 4096 || bytes > DROIDDECK_XR_FRAME_MAX_BYTES
|
||||
|| header.bytes != bytes || header.color_space != 1 || header.captured_ns <= 0) return false;
|
||||
for (int eye = 0; eye < 2; eye++) {
|
||||
for (const auto &row : header.eye_pose[eye]) for (float value : row) if (!std::isfinite(value)) return false;
|
||||
for (const auto &row : header.projection[eye]) for (float value : row) if (!std::isfinite(value)) return false;
|
||||
const auto &projection = header.projection[eye];
|
||||
if (projection[0][0] < 0.0001f || projection[1][1] < 0.0001f
|
||||
|| std::abs(projection[3][2] + 1.f) > 0.0001f || std::abs(projection[3][3]) > 0.0001f
|
||||
|| std::abs(projection[3][0]) > 0.0001f || std::abs(projection[3][1]) > 0.0001f
|
||||
|| std::abs(projection[0][1]) > 0.0001f || std::abs(projection[0][3]) > 0.0001f
|
||||
|| std::abs(projection[1][0]) > 0.0001f || std::abs(projection[1][3]) > 0.0001f) return false;
|
||||
for (int row = 0; row < 3; row++) for (int other = 0; other < 3; other++) {
|
||||
float product = 0;
|
||||
for (int column = 0; column < 3; column++) product += header.eye_pose[eye][row][column] * header.eye_pose[eye][other][column];
|
||||
if (std::abs(product - (row == other ? 1.f : 0.f)) > 0.01f) return false;
|
||||
}
|
||||
const auto &rotation = header.eye_pose[eye];
|
||||
float determinant = rotation[0][0] * (rotation[1][1] * rotation[2][2] - rotation[1][2] * rotation[2][1])
|
||||
- rotation[0][1] * (rotation[1][0] * rotation[2][2] - rotation[1][2] * rotation[2][0])
|
||||
+ rotation[0][2] * (rotation[1][0] * rotation[2][1] - rotation[1][1] * rotation[2][0]);
|
||||
if (std::abs(determinant - 1.f) > 0.01f) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
#pragma once
|
||||
#include "xr_frame.h"
|
||||
#include <atomic>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <poll.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/un.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
inline int64_t xr_frame_now() {
|
||||
timespec now{}; clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
return (int64_t)now.tv_sec * 1000000000LL + now.tv_nsec;
|
||||
}
|
||||
|
||||
inline bool xr_frame_io(int descriptor, void *data, size_t size, bool write, int64_t deadline) {
|
||||
auto *bytes = static_cast<unsigned char *>(data);
|
||||
while (size) {
|
||||
int64_t remaining = deadline - xr_frame_now();
|
||||
if (remaining <= 0) return false;
|
||||
pollfd ready{descriptor, (short)(write ? POLLOUT : POLLIN), 0};
|
||||
int result = poll(&ready, 1, (int)((remaining + 999999) / 1000000));
|
||||
if (result < 0 && errno == EINTR) continue;
|
||||
if (result <= 0 || !(ready.revents & ready.events)) return false;
|
||||
ssize_t done = write ? send(descriptor, bytes, size, MSG_NOSIGNAL | MSG_DONTWAIT)
|
||||
: recv(descriptor, bytes, size, MSG_DONTWAIT);
|
||||
if (done < 0 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)) continue;
|
||||
if (done <= 0) return false;
|
||||
bytes += done; size -= done;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool xr_frame_peer(int descriptor) {
|
||||
struct { pid_t pid; uid_t uid; gid_t gid; } peer{};
|
||||
socklen_t size = sizeof(peer);
|
||||
return getsockopt(descriptor, SOL_SOCKET, SO_PEERCRED, &peer, &size) == 0
|
||||
&& size == sizeof(peer) && peer.uid == getuid();
|
||||
}
|
||||
|
||||
struct DroidDeckXrPixels {
|
||||
DroidDeckXrFrameHeader header{};
|
||||
std::vector<unsigned char> pixels;
|
||||
};
|
||||
|
||||
class XrFrameReceiver {
|
||||
public:
|
||||
~XrFrameReceiver() { Stop(); }
|
||||
bool Start(const std::string &filename) {
|
||||
if (listener_ >= 0 || filename.empty() || filename.size() >= sizeof(sockaddr_un::sun_path)) return false;
|
||||
lock_ = open((filename + ".lock").c_str(), O_CREAT | O_RDWR | O_CLOEXEC | O_NOFOLLOW, 0600);
|
||||
struct stat status{};
|
||||
if (lock_ < 0) return false;
|
||||
if (fstat(lock_, &status) != 0 || !S_ISREG(status.st_mode) || status.st_uid != getuid()
|
||||
|| flock(lock_, LOCK_EX | LOCK_NB) != 0) { close(lock_); lock_ = -1; return false; }
|
||||
if (lstat(filename.c_str(), &status) == 0) {
|
||||
if (!S_ISSOCK(status.st_mode) || status.st_uid != getuid() || unlink(filename.c_str()) != 0) {
|
||||
close(lock_); lock_ = -1; return false;
|
||||
}
|
||||
} else if (errno != ENOENT) { close(lock_); lock_ = -1; return false; }
|
||||
sockaddr_un address{}; address.sun_family = AF_UNIX;
|
||||
std::strcpy(address.sun_path, filename.c_str());
|
||||
listener_ = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
|
||||
if (listener_ < 0) { Stop(); return false; }
|
||||
if (bind(listener_, reinterpret_cast<sockaddr *>(&address), sizeof(address)) != 0) { Stop(); return false; }
|
||||
filename_ = filename;
|
||||
if (chmod(filename.c_str(), 0600) != 0 || listen(listener_, 2) != 0) { Stop(); return false; }
|
||||
{ std::lock_guard<std::mutex> lock(mutex_); pending_ = {}; }
|
||||
received_ = rejected_ = 0;
|
||||
stopping_ = false;
|
||||
thread_ = std::thread([this] { Loop(); });
|
||||
return true;
|
||||
}
|
||||
void Stop() {
|
||||
stopping_ = true;
|
||||
if (thread_.joinable()) thread_.join();
|
||||
if (listener_ >= 0) close(listener_);
|
||||
listener_ = -1;
|
||||
if (!filename_.empty()) unlink(filename_.c_str());
|
||||
filename_.clear();
|
||||
if (lock_ >= 0) close(lock_);
|
||||
lock_ = -1;
|
||||
}
|
||||
bool Take(DroidDeckXrPixels *out) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (pending_.pixels.empty()) return false;
|
||||
*out = std::move(pending_);
|
||||
pending_ = {};
|
||||
return true;
|
||||
}
|
||||
uint64_t Rejected() const { return rejected_; }
|
||||
uint64_t Received() const { return received_; }
|
||||
private:
|
||||
int listener_ = -1;
|
||||
int lock_ = -1;
|
||||
std::string filename_;
|
||||
std::atomic<bool> stopping_{false};
|
||||
std::atomic<uint64_t> rejected_{0}, received_{0};
|
||||
std::thread thread_;
|
||||
std::mutex mutex_;
|
||||
DroidDeckXrPixels pending_;
|
||||
void Loop() {
|
||||
while (!stopping_) {
|
||||
pollfd ready{listener_, POLLIN, 0};
|
||||
if (poll(&ready, 1, 50) <= 0) continue;
|
||||
int client = accept4(listener_, nullptr, nullptr, SOCK_CLOEXEC | SOCK_NONBLOCK);
|
||||
if (client < 0) continue;
|
||||
if (!xr_frame_peer(client)) { rejected_++; close(client); continue; }
|
||||
uint64_t lastFrame = 0;
|
||||
while (!stopping_) {
|
||||
DroidDeckXrPixels frame;
|
||||
int64_t deadline = xr_frame_now() + 200000000LL;
|
||||
if (!xr_frame_io(client, &frame.header, sizeof(frame.header), false, deadline)) break;
|
||||
int64_t now = xr_frame_now();
|
||||
if (!xr_frame_valid(frame.header) || frame.header.captured_ns > now
|
||||
|| now - frame.header.captured_ns > 500000000LL || frame.header.frame_id <= lastFrame) { rejected_++; break; }
|
||||
try { frame.pixels.resize(frame.header.bytes); }
|
||||
catch (...) { rejected_++; break; }
|
||||
if (!xr_frame_io(client, frame.pixels.data(), frame.pixels.size(), false, deadline)) { rejected_++; break; }
|
||||
if (xr_frame_now() - frame.header.captured_ns > 500000000LL) { rejected_++; break; }
|
||||
uint64_t acknowledgement = frame.header.frame_id;
|
||||
lastFrame = acknowledgement;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
pending_ = std::move(frame);
|
||||
}
|
||||
received_++;
|
||||
if (!xr_frame_io(client, &acknowledgement, sizeof(acknowledgement), true, deadline)) break;
|
||||
}
|
||||
close(client);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class XrFrameSender {
|
||||
public:
|
||||
XrFrameSender() = default;
|
||||
XrFrameSender(const XrFrameSender &) = delete;
|
||||
XrFrameSender &operator=(const XrFrameSender &) = delete;
|
||||
~XrFrameSender() { Disconnect(); }
|
||||
bool Send(const std::string &filename, const DroidDeckXrFrameHeader &header, const unsigned char *pixels) {
|
||||
if (!pixels || !xr_frame_valid(header) || filename.empty() || filename.size() >= sizeof(sockaddr_un::sun_path)) return false;
|
||||
if (filename_ != filename) Disconnect();
|
||||
int64_t deadline = xr_frame_now() + 200000000LL;
|
||||
if (descriptor_ < 0) {
|
||||
sockaddr_un address{}; address.sun_family = AF_UNIX;
|
||||
std::strcpy(address.sun_path, filename.c_str());
|
||||
descriptor_ = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
|
||||
if (descriptor_ < 0) return false;
|
||||
int result = connect(descriptor_, reinterpret_cast<sockaddr *>(&address), sizeof(address));
|
||||
if (result != 0 && errno == EINPROGRESS) {
|
||||
pollfd ready{descriptor_, POLLOUT, 0};
|
||||
int error = 0; socklen_t size = sizeof(error);
|
||||
result = poll(&ready, 1, 200) > 0 && getsockopt(descriptor_, SOL_SOCKET, SO_ERROR, &error, &size) == 0
|
||||
&& error == 0 ? 0 : -1;
|
||||
}
|
||||
if (result != 0 || !xr_frame_peer(descriptor_)) {
|
||||
Disconnect(); return false;
|
||||
}
|
||||
filename_ = filename;
|
||||
}
|
||||
uint64_t acknowledgement = 0;
|
||||
if (!xr_frame_io(descriptor_, const_cast<DroidDeckXrFrameHeader *>(&header), sizeof(header), true, deadline)
|
||||
|| !xr_frame_io(descriptor_, const_cast<unsigned char *>(pixels), header.bytes, true, deadline)
|
||||
|| !xr_frame_io(descriptor_, &acknowledgement, sizeof(acknowledgement), false, deadline)
|
||||
|| acknowledgement != header.frame_id) { Disconnect(); return false; }
|
||||
return true;
|
||||
}
|
||||
private:
|
||||
int descriptor_ = -1;
|
||||
std::string filename_;
|
||||
void Disconnect() { if (descriptor_ >= 0) close(descriptor_); descriptor_ = -1; filename_.clear(); }
|
||||
};
|
||||
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
#include "xr_frame.h"
|
||||
|
||||
struct DroidDeckXrRenderView {
|
||||
float orientation[4];
|
||||
float position[3];
|
||||
float fov[4];
|
||||
};
|
||||
|
||||
inline void xr_frame_eye_pose(float result[3][4], const float head[3][4], const float eyeToHead[3][4]) {
|
||||
for (int row = 0; row < 3; row++) {
|
||||
for (int column = 0; column < 3; column++) {
|
||||
result[row][column] = 0;
|
||||
for (int inner = 0; inner < 3; inner++) result[row][column] += head[row][inner] * eyeToHead[inner][column];
|
||||
}
|
||||
result[row][3] = head[row][3];
|
||||
for (int inner = 0; inner < 3; inner++) result[row][3] += head[row][inner] * eyeToHead[inner][3];
|
||||
}
|
||||
}
|
||||
|
||||
inline DroidDeckXrRenderView xr_frame_view(const DroidDeckXrFrameHeader &header, int eye) {
|
||||
const auto &matrix = header.eye_pose[eye];
|
||||
DroidDeckXrRenderView view{};
|
||||
float trace = matrix[0][0] + matrix[1][1] + matrix[2][2];
|
||||
if (trace > 0) {
|
||||
float scale = std::sqrt(trace + 1.f) * 2.f;
|
||||
view.orientation[3] = scale * 0.25f;
|
||||
view.orientation[0] = (matrix[2][1] - matrix[1][2]) / scale;
|
||||
view.orientation[1] = (matrix[0][2] - matrix[2][0]) / scale;
|
||||
view.orientation[2] = (matrix[1][0] - matrix[0][1]) / scale;
|
||||
} else {
|
||||
int axis = matrix[1][1] > matrix[0][0] ? 1 : 0;
|
||||
if (matrix[2][2] > matrix[axis][axis]) axis = 2;
|
||||
int next = (axis + 1) % 3, last = (axis + 2) % 3;
|
||||
float scale = std::sqrt(1.f + matrix[axis][axis] - matrix[next][next] - matrix[last][last]) * 2.f;
|
||||
view.orientation[axis] = scale * 0.25f;
|
||||
view.orientation[next] = (matrix[axis][next] + matrix[next][axis]) / scale;
|
||||
view.orientation[last] = (matrix[axis][last] + matrix[last][axis]) / scale;
|
||||
view.orientation[3] = (matrix[last][next] - matrix[next][last]) / scale;
|
||||
}
|
||||
float lengthSquared = 0;
|
||||
for (float value : view.orientation) lengthSquared += value * value;
|
||||
for (float &value : view.orientation) value /= std::sqrt(lengthSquared);
|
||||
for (int axis = 0; axis < 3; axis++) view.position[axis] = matrix[axis][3];
|
||||
const auto &projection = header.projection[eye];
|
||||
view.fov[0] = std::atan((projection[0][2] - 1.f) / projection[0][0]);
|
||||
view.fov[1] = std::atan((projection[0][2] + 1.f) / projection[0][0]);
|
||||
view.fov[2] = std::atan((projection[1][2] + 1.f) / projection[1][1]);
|
||||
view.fov[3] = std::atan((projection[1][2] - 1.f) / projection[1][1]);
|
||||
return view;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
#include "xr_tracking.h"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
struct XrTouchInput {
|
||||
float trigger = 0, grip = 0, stickX = 0, stickY = 0;
|
||||
bool triggerClick = false, gripClick = false, stickClick = false;
|
||||
bool primaryClick = false, secondaryClick = false, menuClick = false;
|
||||
bool triggerTouch = false, stickTouch = false, primaryTouch = false, secondaryTouch = false, thumbrestTouch = false;
|
||||
};
|
||||
|
||||
inline XrTouchInput xr_touch_input(const DroidDeckXrHand &hand, bool right) {
|
||||
XrTouchInput input;
|
||||
if (!hand.active) return input;
|
||||
auto scalar = [](float value, float minimum) { return std::isfinite(value) ? std::clamp(value, minimum, 1.f) : 0.f; };
|
||||
input.trigger = scalar(hand.trigger, 0);
|
||||
input.grip = scalar(hand.squeeze, 0);
|
||||
input.stickX = scalar(hand.stick[0], -1);
|
||||
input.stickY = scalar(hand.stick[1], -1);
|
||||
input.triggerClick = input.trigger > 0.6f;
|
||||
input.gripClick = input.grip > 0.6f;
|
||||
input.stickClick = hand.buttons & DROIDDECK_XR_STICK_CLICK;
|
||||
input.primaryClick = hand.buttons & DROIDDECK_XR_PRIMARY_CLICK;
|
||||
input.secondaryClick = hand.buttons & DROIDDECK_XR_SECONDARY_CLICK;
|
||||
input.menuClick = !right && (hand.buttons & DROIDDECK_XR_MENU_CLICK);
|
||||
input.triggerTouch = hand.buttons & DROIDDECK_XR_TRIGGER_TOUCH;
|
||||
input.stickTouch = hand.buttons & DROIDDECK_XR_STICK_TOUCH;
|
||||
input.primaryTouch = hand.buttons & DROIDDECK_XR_PRIMARY_TOUCH;
|
||||
input.secondaryTouch = hand.buttons & DROIDDECK_XR_SECONDARY_TOUCH;
|
||||
input.thumbrestTouch = hand.buttons & DROIDDECK_XR_THUMBREST_TOUCH;
|
||||
return input;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
#define DROIDDECK_XR_MAGIC 0x52584444u
|
||||
#define DROIDDECK_XR_VERSION 1u
|
||||
#define DROIDDECK_XR_FILE "droiddeck-xr-tracking"
|
||||
#define DROIDDECK_XR_STICK_CLICK (1u << 0)
|
||||
#define DROIDDECK_XR_PRIMARY_CLICK (1u << 1)
|
||||
#define DROIDDECK_XR_SECONDARY_CLICK (1u << 2)
|
||||
#define DROIDDECK_XR_MENU_CLICK (1u << 3)
|
||||
#define DROIDDECK_XR_TRIGGER_TOUCH (1u << 4)
|
||||
#define DROIDDECK_XR_STICK_TOUCH (1u << 5)
|
||||
#define DROIDDECK_XR_PRIMARY_TOUCH (1u << 6)
|
||||
#define DROIDDECK_XR_SECONDARY_TOUCH (1u << 7)
|
||||
#define DROIDDECK_XR_THUMBREST_TOUCH (1u << 8)
|
||||
|
||||
struct DroidDeckXrPose {
|
||||
float orientation[4];
|
||||
float position[3];
|
||||
uint32_t flags;
|
||||
};
|
||||
|
||||
struct DroidDeckXrEye {
|
||||
DroidDeckXrPose pose;
|
||||
float fov[4];
|
||||
uint32_t width, height;
|
||||
};
|
||||
|
||||
struct DroidDeckXrHand {
|
||||
DroidDeckXrPose pose;
|
||||
float trigger, squeeze, stick[2];
|
||||
uint32_t buttons, active;
|
||||
};
|
||||
|
||||
struct DroidDeckXrTracking {
|
||||
uint32_t magic, version, seq, active;
|
||||
int64_t sampled_ns, predicted_ns, period_ns;
|
||||
DroidDeckXrPose head;
|
||||
DroidDeckXrEye eyes[2];
|
||||
DroidDeckXrHand hands[2];
|
||||
};
|
||||
|
||||
static_assert(sizeof(DroidDeckXrTracking) == 296, "XR tracking IPC layout");
|
||||
|
||||
#define DROIDDECK_XR_HAPTIC_MAGIC 0x48584444u
|
||||
#define DROIDDECK_XR_HAPTIC_FILE "droiddeck-xr-haptics"
|
||||
|
||||
struct DroidDeckXrPulse {
|
||||
uint64_t id;
|
||||
int64_t duration_ns;
|
||||
float amplitude, frequency;
|
||||
};
|
||||
|
||||
struct DroidDeckXrHaptics {
|
||||
uint32_t magic, version, seq, reserved;
|
||||
DroidDeckXrPulse hands[2];
|
||||
};
|
||||
|
||||
static_assert(sizeof(DroidDeckXrHaptics) == 64, "XR haptics IPC layout");
|
||||
@@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
#include "xr_tracking.h"
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
class XrTrackingFile {
|
||||
public:
|
||||
explicit XrTrackingFile(const char *path = "/run/droiddeck/" DROIDDECK_XR_FILE) : path_(path) {}
|
||||
~XrTrackingFile() { if (mapping_) munmap(mapping_, sizeof(*mapping_)); }
|
||||
bool Read(DroidDeckXrTracking *packet) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (!mapping_) {
|
||||
int descriptor = open(path_.c_str(), O_RDONLY | O_CLOEXEC);
|
||||
if (descriptor < 0) return false;
|
||||
struct stat status{};
|
||||
if (fstat(descriptor, &status) != 0 || status.st_size != sizeof(*mapping_)) {
|
||||
close(descriptor); return false;
|
||||
}
|
||||
void *mapping = mmap(nullptr, sizeof(*mapping_), PROT_READ, MAP_SHARED, descriptor, 0);
|
||||
close(descriptor);
|
||||
if (mapping == MAP_FAILED) return false;
|
||||
mapping_ = static_cast<DroidDeckXrTracking *>(mapping);
|
||||
}
|
||||
for (int attempt = 0; attempt < 8; attempt++) {
|
||||
uint32_t sequence = __atomic_load_n(&mapping_->seq, __ATOMIC_ACQUIRE);
|
||||
if (sequence & 1u) continue;
|
||||
std::memcpy(packet, mapping_, sizeof(*packet));
|
||||
__atomic_thread_fence(__ATOMIC_ACQUIRE);
|
||||
if (__atomic_load_n(&mapping_->seq, __ATOMIC_RELAXED) == sequence)
|
||||
return packet->magic == DROIDDECK_XR_MAGIC && packet->version == DROIDDECK_XR_VERSION;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
static int64_t Now() {
|
||||
timespec now{}; clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
return (int64_t)now.tv_sec * 1000000000LL + now.tv_nsec;
|
||||
}
|
||||
static double PoseTimeOffset(int64_t predicted, int64_t now) {
|
||||
return predicted > 0 ? (static_cast<double>(predicted) - static_cast<double>(now)) / 1000000000.0 : 0;
|
||||
}
|
||||
static bool Fresh(const DroidDeckXrTracking &packet) {
|
||||
int64_t age = Now() - packet.sampled_ns;
|
||||
return packet.active && age >= 0 && age < 500000000LL && packet.period_ns >= 1000000LL
|
||||
&& packet.period_ns <= 50000000LL && packet.eyes[0].width > 0 && packet.eyes[0].width <= 8192
|
||||
&& packet.eyes[0].height > 0 && packet.eyes[0].height <= 8192;
|
||||
}
|
||||
static bool Tracked(const DroidDeckXrPose &pose) {
|
||||
if ((pose.flags & 15u) != 15u) return false;
|
||||
float lengthSquared = 0;
|
||||
for (float value : pose.orientation) {
|
||||
if (!std::isfinite(value)) return false;
|
||||
lengthSquared += value * value;
|
||||
}
|
||||
for (float value : pose.position) if (!std::isfinite(value)) return false;
|
||||
return std::abs(lengthSquared - 1.f) < 0.01f;
|
||||
}
|
||||
private:
|
||||
DroidDeckXrTracking *mapping_ = nullptr;
|
||||
std::mutex mutex_;
|
||||
std::string path_;
|
||||
};
|
||||
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <mutex>
|
||||
|
||||
class XrVsyncTiming {
|
||||
public:
|
||||
using Clock = std::chrono::steady_clock;
|
||||
static constexpr std::chrono::nanoseconds kDefaultPeriod{16666667};
|
||||
|
||||
void Reset() { Record({}, kDefaultPeriod); }
|
||||
|
||||
Clock::time_point Target(Clock::time_point now, Clock::time_point scheduled,
|
||||
int64_t predictedNs, std::chrono::nanoseconds period) const {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
auto predicted = Clock::time_point(std::chrono::nanoseconds(predictedNs));
|
||||
return predictedNs > 0 && predicted > now && predicted > last_ && predicted < now + period * 3
|
||||
? predicted : scheduled;
|
||||
}
|
||||
|
||||
void Record(Clock::time_point target, std::chrono::nanoseconds period) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
last_ = target;
|
||||
period_ = period;
|
||||
}
|
||||
|
||||
Clock::time_point Deadline(uint32_t framesToThrottle) const {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return last_ + period_ * (1 + static_cast<int64_t>(framesToThrottle));
|
||||
}
|
||||
|
||||
private:
|
||||
mutable std::mutex mutex_;
|
||||
Clock::time_point last_{};
|
||||
std::chrono::nanoseconds period_{kDefaultPeriod};
|
||||
};
|
||||
@@ -0,0 +1,42 @@
|
||||
#include "../src/xr_frame_channel.h"
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc != 2) { std::fprintf(stderr, "usage: send_xr_frame <socket>\n"); return 2; }
|
||||
DroidDeckXrFrameHeader header{};
|
||||
header.magic = DROIDDECK_XR_FRAME_MAGIC; header.version = DROIDDECK_XR_FRAME_VERSION;
|
||||
header.width = 64; header.height = 32; header.bytes = header.width * header.height * 4; header.color_space = 1;
|
||||
for (int eye = 0; eye < 2; eye++) {
|
||||
for (int axis = 0; axis < 3; axis++) header.eye_pose[eye][axis][axis] = 1;
|
||||
header.eye_pose[eye][0][3] = eye == 0 ? -0.032f : 0.032f; header.eye_pose[eye][1][3] = 1.6f;
|
||||
header.projection[eye][0][0] = header.projection[eye][1][1] = 1;
|
||||
header.projection[eye][2][2] = -1.002f; header.projection[eye][2][3] = -0.1001f;
|
||||
header.projection[eye][3][2] = -1;
|
||||
}
|
||||
std::vector<unsigned char> pixels(header.bytes);
|
||||
for (uint32_t row = 0; row < header.height; row++) for (uint32_t column = 0; column < header.width; column++) {
|
||||
size_t offset = (row * header.width + column) * 4;
|
||||
pixels[offset] = column < header.width / 2 ? 211 : 19;
|
||||
pixels[offset + 1] = row * 7;
|
||||
pixels[offset + 2] = column < header.width / 2 ? 31 : 239;
|
||||
pixels[offset + 3] = 255;
|
||||
}
|
||||
uint32_t checksum = 2166136261u;
|
||||
for (unsigned char value : pixels) checksum = (checksum ^ value) * 16777619u;
|
||||
XrFrameSender sender;
|
||||
int64_t deadline = xr_frame_now() + 10000000000LL;
|
||||
uint64_t sent = 0;
|
||||
for (uint64_t id = 1; id <= 144; id++) {
|
||||
header.frame_id = id; header.captured_ns = xr_frame_now();
|
||||
bool delivered = sender.Send(argv[1], header, pixels.data());
|
||||
while (!delivered && sent == 0 && xr_frame_now() < deadline) {
|
||||
usleep(20000); header.captured_ns = xr_frame_now();
|
||||
delivered = sender.Send(argv[1], header, pixels.data());
|
||||
}
|
||||
if (!delivered) { std::fprintf(stderr, "frame %llu was not acknowledged\n", (unsigned long long)id); return 1; }
|
||||
sent++; usleep(13889);
|
||||
}
|
||||
std::printf("{\"sent\":%llu,\"lastFrameId\":%llu,\"checksum\":%u}\n",
|
||||
(unsigned long long)sent, (unsigned long long)header.frame_id, checksum);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#include "../src/presenter.h"
|
||||
#include <cassert>
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
int main() {
|
||||
setenv("DROIDDECK_VR_PRESENT", "questframes", 1);
|
||||
setenv("VK_ICD_FILENAMES", "/droiddeck-test-no-icd.json", 1);
|
||||
setenv("VK_DRIVER_FILES", "/droiddeck-test-no-icd.json", 1);
|
||||
auto started = std::chrono::steady_clock::now();
|
||||
std::vector<std::thread> callers;
|
||||
for (int index = 0; index < 8; index++) callers.emplace_back([] {
|
||||
droiddeck::EyeFrame frame{};
|
||||
for (int count = 0; count < 1000; count++) droiddeck::Presenter::Submit(frame);
|
||||
});
|
||||
for (auto &caller : callers) caller.join();
|
||||
droiddeck::Presenter::Shutdown();
|
||||
assert(std::chrono::steady_clock::now() - started < std::chrono::seconds(5));
|
||||
std::puts("Presenter validation passed: unavailable GPU releases concurrent blocking callers and shutdown");
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <assert.h>
|
||||
#include <stdatomic.h>
|
||||
#include "../src/vk_layer_droiddeck_vr.c"
|
||||
|
||||
static VkDevice test_device;
|
||||
static VkQueue test_queue;
|
||||
static VkSemaphore gate;
|
||||
static VkCommandBuffer commands[2];
|
||||
static atomic_int second_returned;
|
||||
static atomic_int second_entered;
|
||||
|
||||
static void *signal_gate(void *argument) {
|
||||
(void)argument;
|
||||
usleep(30000);
|
||||
VkSemaphoreSignalInfo signal_info = {VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO, NULL, gate, 1};
|
||||
assert(vkSignalSemaphore(test_device, &signal_info) == VK_SUCCESS);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void *second_submit(void *argument) {
|
||||
(void)argument;
|
||||
VkSubmitInfo submit = {VK_STRUCTURE_TYPE_SUBMIT_INFO, NULL, 0, NULL, NULL, 1, &commands[1], 0, NULL};
|
||||
atomic_store(&second_entered, 1);
|
||||
assert(QueueSubmit(test_queue, 1, &submit, VK_NULL_HANDLE) == VK_SUCCESS);
|
||||
atomic_store(&second_returned, 1);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void check_pixels(const uint32_t *pixels, uint32_t expected) {
|
||||
for (unsigned index = 0; index < 1024; index++) assert(pixels[index] == expected);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
setenv("DROIDDECK_VR_LAYER_QUIET", "1", 1);
|
||||
VkApplicationInfo application = {VK_STRUCTURE_TYPE_APPLICATION_INFO, NULL, "DroidDeck producer GPU test", 1, NULL, 0, VK_API_VERSION_1_2};
|
||||
VkInstanceCreateInfo create_instance = {VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, NULL, 0, &application, 0, NULL, 0, NULL};
|
||||
VkInstance instance;
|
||||
assert(vkCreateInstance(&create_instance, NULL, &instance) == VK_SUCCESS);
|
||||
uint32_t count = 1;
|
||||
VkPhysicalDevice physical;
|
||||
VkResult enumeration = vkEnumeratePhysicalDevices(instance, &count, &physical);
|
||||
assert((enumeration == VK_SUCCESS || enumeration == VK_INCOMPLETE) && count);
|
||||
VkQueueFamilyProperties families[32];
|
||||
count = 32;
|
||||
vkGetPhysicalDeviceQueueFamilyProperties(physical, &count, families);
|
||||
uint32_t family = UINT32_MAX;
|
||||
for (uint32_t index = 0; index < count; index++)
|
||||
if (families[index].queueFlags & VK_QUEUE_GRAPHICS_BIT) { family = index; break; }
|
||||
assert(family != UINT32_MAX);
|
||||
float priority = 1;
|
||||
VkDeviceQueueCreateInfo queue_info = {VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, NULL, 0, family, 1, &priority};
|
||||
VkPhysicalDeviceTimelineSemaphoreFeatures timeline_feature = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TIMELINE_SEMAPHORE_FEATURES, NULL, VK_TRUE};
|
||||
VkDeviceCreateInfo create_device = {VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, &timeline_feature, 0, 1, &queue_info, 0, NULL, 0, NULL, NULL};
|
||||
assert(vkCreateDevice(physical, &create_device, NULL, &test_device) == VK_SUCCESS);
|
||||
vkGetDeviceQueue(test_device, family, 0, &test_queue);
|
||||
g_dev[0] = (struct dev){test_device, vkGetDeviceProcAddr, NULL, 1, 0, 1};
|
||||
g_producer_started = 1;
|
||||
VkBufferCreateInfo create_buffer = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, NULL, 0, 4096, VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
||||
VK_SHARING_MODE_EXCLUSIVE, 0, NULL};
|
||||
VkBuffer buffer;
|
||||
assert(vkCreateBuffer(test_device, &create_buffer, NULL, &buffer) == VK_SUCCESS);
|
||||
VkMemoryRequirements requirements;
|
||||
vkGetBufferMemoryRequirements(test_device, buffer, &requirements);
|
||||
VkPhysicalDeviceMemoryProperties properties;
|
||||
vkGetPhysicalDeviceMemoryProperties(physical, &properties);
|
||||
uint32_t memory_type = UINT32_MAX;
|
||||
VkMemoryPropertyFlags required = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
|
||||
for (uint32_t index = 0; index < properties.memoryTypeCount; index++)
|
||||
if ((requirements.memoryTypeBits & (1u << index)) && (properties.memoryTypes[index].propertyFlags & required) == required) {
|
||||
memory_type = index;
|
||||
break;
|
||||
}
|
||||
assert(memory_type != UINT32_MAX);
|
||||
VkMemoryAllocateInfo allocate = {VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, NULL, requirements.size, memory_type};
|
||||
VkDeviceMemory memory;
|
||||
assert(vkAllocateMemory(test_device, &allocate, NULL, &memory) == VK_SUCCESS);
|
||||
assert(vkBindBufferMemory(test_device, buffer, memory, 0) == VK_SUCCESS);
|
||||
uint32_t *pixels;
|
||||
assert(vkMapMemory(test_device, memory, 0, VK_WHOLE_SIZE, 0, (void **)&pixels) == VK_SUCCESS);
|
||||
VkCommandPoolCreateInfo create_pool = {VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, NULL, 0, family};
|
||||
VkCommandPool pool;
|
||||
assert(vkCreateCommandPool(test_device, &create_pool, NULL, &pool) == VK_SUCCESS);
|
||||
VkCommandBufferAllocateInfo allocate_commands = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, NULL, pool, VK_COMMAND_BUFFER_LEVEL_PRIMARY, 2};
|
||||
assert(vkAllocateCommandBuffers(test_device, &allocate_commands, commands) == VK_SUCCESS);
|
||||
const uint32_t patterns[2] = {0x13579bdf, 0x2468ace0};
|
||||
for (unsigned index = 0; index < 2; index++) {
|
||||
VkCommandBufferBeginInfo begin = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, NULL, 0, NULL};
|
||||
assert(vkBeginCommandBuffer(commands[index], &begin) == VK_SUCCESS);
|
||||
vkCmdFillBuffer(commands[index], buffer, 0, 4096, patterns[index]);
|
||||
VkMemoryBarrier barrier = {VK_STRUCTURE_TYPE_MEMORY_BARRIER, NULL, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_HOST_READ_BIT};
|
||||
vkCmdPipelineBarrier(commands[index], VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &barrier, 0, NULL, 0, NULL);
|
||||
assert(vkEndCommandBuffer(commands[index]) == VK_SUCCESS);
|
||||
}
|
||||
VkSemaphoreTypeCreateInfo type = {VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO, NULL, VK_SEMAPHORE_TYPE_TIMELINE, 0};
|
||||
VkSemaphoreCreateInfo create_gate = {VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, &type, 0};
|
||||
assert(vkCreateSemaphore(test_device, &create_gate, NULL, &gate) == VK_SUCCESS);
|
||||
uint64_t value = 1;
|
||||
VkTimelineSemaphoreSubmitInfo timeline = {VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO, NULL, 1, &value, 0, NULL};
|
||||
VkPipelineStageFlags stage = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
||||
VkSubmitInfo first = {VK_STRUCTURE_TYPE_SUBMIT_INFO, &timeline, 1, &gate, &stage, 1, &commands[0], 0, NULL};
|
||||
assert(QueueSubmit(test_queue, 1, &first, VK_NULL_HANDLE) == VK_SUCCESS);
|
||||
pthread_t signal_thread;
|
||||
int64_t began = droiddeck_producer_now();
|
||||
assert(pthread_create(&signal_thread, NULL, signal_gate, NULL) == 0);
|
||||
assert(producer_acquire(began + 1000000000LL));
|
||||
assert(droiddeck_producer_now() - began >= 20000000LL);
|
||||
assert(pthread_join(signal_thread, NULL) == 0);
|
||||
check_pixels(pixels, patterns[0]);
|
||||
pthread_t writer;
|
||||
assert(pthread_create(&writer, NULL, second_submit, NULL) == 0);
|
||||
while (!atomic_load(&second_entered)) usleep(100);
|
||||
usleep(50000);
|
||||
assert(!atomic_load(&second_returned));
|
||||
check_pixels(pixels, patterns[0]);
|
||||
producer_release();
|
||||
assert(pthread_join(writer, NULL) == 0);
|
||||
assert(vkQueueWaitIdle(test_queue) == VK_SUCCESS);
|
||||
check_pixels(pixels, patterns[1]);
|
||||
value = 2;
|
||||
assert(QueueSubmit(test_queue, 1, &first, VK_NULL_HANDLE) == VK_SUCCESS);
|
||||
assert(!producer_acquire(droiddeck_producer_now() + 20000000LL));
|
||||
VkFence pending = g_producer_queues[0].fence;
|
||||
assert(pending && vkGetFenceStatus(test_device, pending) == VK_NOT_READY);
|
||||
assert(!producer_acquire(droiddeck_producer_now() + 20000000LL));
|
||||
assert(g_producer_queues[0].fence == pending);
|
||||
VkSemaphoreSignalInfo release_gate = {VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO, NULL, gate, 2};
|
||||
assert(vkSignalSemaphore(test_device, &release_gate) == VK_SUCCESS);
|
||||
assert(vkQueueWaitIdle(test_queue) == VK_SUCCESS);
|
||||
assert(producer_acquire(droiddeck_producer_now() + 1000000000LL));
|
||||
producer_release();
|
||||
check_pixels(pixels, patterns[0]);
|
||||
producer_destroy_device(&g_dev[0], NULL);
|
||||
vkDestroySemaphore(test_device, gate, NULL);
|
||||
vkDestroyCommandPool(test_device, pool, NULL);
|
||||
vkUnmapMemory(test_device, memory);
|
||||
vkDestroyBuffer(test_device, buffer, NULL);
|
||||
vkFreeMemory(test_device, memory, NULL);
|
||||
vkDestroyDevice(test_device, NULL);
|
||||
vkDestroyInstance(instance, NULL);
|
||||
puts("producer GPU: actual completion, frozen overwrite, timeout retention and 1024 buffer values passed");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
#define _GNU_SOURCE
|
||||
#define DROIDDECK_PRODUCER_SERVER
|
||||
#include "../src/producer_lease.h"
|
||||
#include <assert.h>
|
||||
#include <stdatomic.h>
|
||||
|
||||
static pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static atomic_int completed;
|
||||
static atomic_int released;
|
||||
static atomic_int quarantined;
|
||||
static atomic_int available;
|
||||
|
||||
static int acquire(int64_t deadline) {
|
||||
assert(deadline > droiddeck_producer_now());
|
||||
if (!atomic_load(&available)) return 0;
|
||||
assert(pthread_mutex_lock(&queue_lock) == 0);
|
||||
atomic_fetch_add(&completed, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void release(void) {
|
||||
atomic_fetch_add(&released, 1);
|
||||
assert(pthread_mutex_unlock(&queue_lock) == 0);
|
||||
}
|
||||
|
||||
static void quarantine(void) {
|
||||
atomic_store(&quarantined, 1);
|
||||
}
|
||||
|
||||
static void wait_count(atomic_int *counter, int expected) {
|
||||
int64_t deadline = droiddeck_producer_now() + 1000000000LL;
|
||||
while (atomic_load(counter) != expected && droiddeck_producer_now() < deadline) usleep(1000);
|
||||
assert(atomic_load(counter) == expected);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
char directory[] = "/tmp/droiddeck-producer-XXXXXX";
|
||||
assert(mkdtemp(directory));
|
||||
setenv("XDG_RUNTIME_DIR", directory, 1);
|
||||
char parent[128];
|
||||
assert(snprintf(parent, sizeof(parent), "%s/droiddeck-steamvr", directory) > 0);
|
||||
assert(mkdir(parent, 0755) == 0);
|
||||
struct droiddeck_producer_server server = {.acquire = acquire, .release = release, .quarantine = quarantine};
|
||||
assert(!droiddeck_producer_start(&server));
|
||||
assert(chmod(parent, 0700) == 0);
|
||||
assert(droiddeck_producer_start(&server));
|
||||
struct stat metadata;
|
||||
assert(stat(server.path, &metadata) == 0 && (metadata.st_mode & 0777) == 0600);
|
||||
struct droiddeck_producer_server duplicate = server;
|
||||
assert(!droiddeck_producer_start(&duplicate));
|
||||
struct droiddeck_producer_lease lease;
|
||||
assert(!droiddeck_producer_acquire(getpid(), &lease));
|
||||
assert(!atomic_load(&completed));
|
||||
atomic_store(&available, 1);
|
||||
assert(droiddeck_producer_acquire(getpid(), &lease));
|
||||
assert(fcntl(lease.descriptor, F_GETFD) & FD_CLOEXEC);
|
||||
assert(pthread_mutex_trylock(&queue_lock) == EBUSY);
|
||||
uint64_t first_ticket = lease.ticket;
|
||||
usleep(300000);
|
||||
assert(pthread_mutex_trylock(&queue_lock) == EBUSY && !atomic_load(&released));
|
||||
assert(droiddeck_producer_release(&lease));
|
||||
assert(lease.descriptor == -1 && atomic_load(&released) == 1);
|
||||
assert(pthread_mutex_trylock(&queue_lock) == 0);
|
||||
assert(pthread_mutex_unlock(&queue_lock) == 0);
|
||||
assert(droiddeck_producer_acquire(getpid(), &lease));
|
||||
assert(lease.ticket > first_ticket);
|
||||
assert(droiddeck_producer_release(&lease));
|
||||
char wrong_path[128];
|
||||
assert(droiddeck_producer_path(wrong_path, sizeof(wrong_path), (uint32_t)getpid() + 1));
|
||||
assert(link(server.path, wrong_path) == 0);
|
||||
assert(!droiddeck_producer_acquire((uint32_t)getpid() + 1, &lease));
|
||||
assert(unlink(wrong_path) == 0);
|
||||
assert(chmod(parent, 0755) == 0);
|
||||
assert(!droiddeck_producer_acquire(getpid(), &lease));
|
||||
assert(chmod(parent, 0700) == 0);
|
||||
assert(droiddeck_producer_acquire(getpid(), &lease));
|
||||
close(lease.descriptor);
|
||||
wait_count(&quarantined, 1);
|
||||
assert(atomic_load(&released) == 2);
|
||||
assert(pthread_mutex_trylock(&queue_lock) == EBUSY);
|
||||
assert(pthread_join(server.thread, NULL) == 0);
|
||||
close(server.listener);
|
||||
assert(unlink(server.path) == 0);
|
||||
assert(rmdir(parent) == 0 && rmdir(directory) == 0);
|
||||
puts("producer lease: peer PID, private socket, completion gate, retained ownership and disconnect quarantine passed");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include "../src/vk_layer_droiddeck_vr.c"
|
||||
|
||||
static const VkSubmitInfo *expected_submit;
|
||||
static const VkSubmitInfo2 *expected_submit2;
|
||||
static const VkSemaphoreWaitInfo *expected_wait;
|
||||
static unsigned create_calls;
|
||||
static unsigned destroyed;
|
||||
static VkResult submit_result = VK_SUCCESS;
|
||||
|
||||
static VkResult VKAPI_CALL mock_submit(VkQueue queue, uint32_t count, const VkSubmitInfo *submits, VkFence fence) {
|
||||
assert(queue && count == 1 && fence == VK_NULL_HANDLE);
|
||||
assert(submits == expected_submit);
|
||||
assert(submits->waitSemaphoreCount == 1);
|
||||
return submit_result;
|
||||
}
|
||||
|
||||
static VkResult VKAPI_CALL mock_submit2(VkQueue queue, uint32_t count, const VkSubmitInfo2 *submits, VkFence fence) {
|
||||
assert(queue && count == 1 && fence == VK_NULL_HANDLE);
|
||||
assert(submits == expected_submit2);
|
||||
assert(submits->waitSemaphoreInfoCount == 1);
|
||||
return submit_result;
|
||||
}
|
||||
|
||||
static VkResult VKAPI_CALL mock_wait(VkDevice device, const VkSemaphoreWaitInfo *info, uint64_t timeout) {
|
||||
assert(device && info == expected_wait && timeout == 7654321);
|
||||
assert(info->semaphoreCount == 65);
|
||||
return VK_TIMEOUT;
|
||||
}
|
||||
|
||||
static VkResult VKAPI_CALL mock_create(VkDevice device, const VkSemaphoreCreateInfo *info,
|
||||
const VkAllocationCallbacks *allocator, VkSemaphore *output) {
|
||||
(void)device;
|
||||
(void)allocator;
|
||||
create_calls++;
|
||||
for (const VkBaseInStructure *item = info->pNext; item; item = item->pNext)
|
||||
if (item->sType == VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO) return VK_ERROR_INVALID_EXTERNAL_HANDLE;
|
||||
*output = (VkSemaphore)(uintptr_t)(create_calls + 100);
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
static void VKAPI_CALL mock_destroy(VkDevice device, VkSemaphore semaphore, const VkAllocationCallbacks *allocator) {
|
||||
(void)device;
|
||||
(void)semaphore;
|
||||
(void)allocator;
|
||||
destroyed++;
|
||||
}
|
||||
|
||||
static PFN_vkVoidFunction VKAPI_CALL mock_proc(VkDevice device, const char *name) {
|
||||
(void)device;
|
||||
if (!strcmp(name, "vkQueueSubmit")) return (PFN_vkVoidFunction)mock_submit;
|
||||
if (!strcmp(name, "vkQueueSubmit2")) return (PFN_vkVoidFunction)mock_submit2;
|
||||
if (!strcmp(name, "vkWaitSemaphores")) return (PFN_vkVoidFunction)mock_wait;
|
||||
if (!strcmp(name, "vkCreateSemaphore")) return (PFN_vkVoidFunction)mock_create;
|
||||
if (!strcmp(name, "vkDestroySemaphore")) return (PFN_vkVoidFunction)mock_destroy;
|
||||
assert(0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
setenv("DROIDDECK_VR_LAYER_QUIET", "1", 1);
|
||||
void *dispatch = &dispatch;
|
||||
VkDevice device = (VkDevice)&dispatch;
|
||||
VkQueue queue = (VkQueue)&dispatch;
|
||||
g_dev[0] = (struct dev){device, mock_proc, NULL, 1, 0, 0};
|
||||
g_poller_started = 1;
|
||||
VkExportSemaphoreCreateInfo export_info = {VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO, NULL,
|
||||
VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT};
|
||||
VkSemaphoreCreateInfo create = {VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, &export_info, 0};
|
||||
VkSemaphore semaphore = VK_NULL_HANDLE;
|
||||
assert(CreateSemaphore(device, &create, NULL, &semaphore) == VK_ERROR_INVALID_EXTERNAL_HANDLE);
|
||||
assert(create_calls == 1 && !semaphore);
|
||||
VkSemaphoreTypeCreateInfo type = {VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO, &export_info,
|
||||
VK_SEMAPHORE_TYPE_TIMELINE, 0};
|
||||
create.pNext = &type;
|
||||
assert(CreateSemaphore(device, &create, NULL, &semaphore) == VK_SUCCESS);
|
||||
struct bridged *bridge = bridged_find(semaphore);
|
||||
assert(bridge && atomic_load(bridge->counter) == 0);
|
||||
VkPipelineStageFlags stage = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
|
||||
uint64_t value = 9;
|
||||
VkTimelineSemaphoreSubmitInfo timeline = {VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO, NULL, 1, &value, 0, NULL};
|
||||
VkSubmitInfo submit = {VK_STRUCTURE_TYPE_SUBMIT_INFO, &timeline, 1, &semaphore, &stage, 0, NULL, 0, NULL};
|
||||
expected_submit = &submit;
|
||||
assert(QueueSubmit(queue, 1, &submit, VK_NULL_HANDLE) == VK_SUCCESS);
|
||||
assert(atomic_load(bridge->counter) == 0);
|
||||
VkSemaphoreSubmitInfo wait2 = {VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO, NULL, semaphore, value,
|
||||
VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, 0};
|
||||
VkSubmitInfo2 submit2 = {VK_STRUCTURE_TYPE_SUBMIT_INFO_2, NULL, 0, 1, &wait2, 0, NULL, 0, NULL};
|
||||
expected_submit2 = &submit2;
|
||||
assert(QueueSubmit2(queue, 1, &submit2, VK_NULL_HANDLE) == VK_SUCCESS);
|
||||
submit_result = VK_ERROR_DEVICE_LOST;
|
||||
assert(QueueSubmit(queue, 1, &submit, VK_NULL_HANDLE) == VK_ERROR_DEVICE_LOST);
|
||||
assert(QueueSubmit2(queue, 1, &submit2, VK_NULL_HANDLE) == VK_ERROR_DEVICE_LOST);
|
||||
VkSemaphore semaphores[65];
|
||||
uint64_t values[65];
|
||||
for (unsigned index = 0; index < 65; index++) { semaphores[index] = semaphore; values[index] = value; }
|
||||
VkSemaphoreWaitInfo wait = {VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO, NULL, VK_SEMAPHORE_WAIT_ANY_BIT, 65, semaphores, values};
|
||||
expected_wait = &wait;
|
||||
assert(WaitSemaphores(device, &wait, 7654321) == VK_TIMEOUT);
|
||||
int descriptor = dup(bridge->fd);
|
||||
VkSemaphore binary = (VkSemaphore)(uintptr_t)999;
|
||||
VkImportSemaphoreFdInfoKHR import_info = {VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR, NULL, binary, 0,
|
||||
VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT, descriptor};
|
||||
assert(ImportSemaphoreFdKHR(device, &import_info) == VK_ERROR_INVALID_EXTERNAL_HANDLE);
|
||||
assert(fcntl(descriptor, F_GETFD) >= 0);
|
||||
timeline_note(binary, 1);
|
||||
assert(ftruncate(descriptor, 32) == 0);
|
||||
assert(ImportSemaphoreFdKHR(device, &import_info) == VK_ERROR_INVALID_EXTERNAL_HANDLE);
|
||||
assert(ftruncate(descriptor, 4096) == 0);
|
||||
import_info.flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
|
||||
assert(ImportSemaphoreFdKHR(device, &import_info) == VK_ERROR_INVALID_EXTERNAL_HANDLE);
|
||||
import_info.flags = 0;
|
||||
assert(ImportSemaphoreFdKHR(device, &import_info) == VK_SUCCESS);
|
||||
VkSemaphoreGetFdInfoKHR get_info = {VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR, NULL, binary,
|
||||
VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT};
|
||||
int reexported = -1;
|
||||
assert(GetSemaphoreFdKHR(device, &get_info, &reexported) == VK_SUCCESS && reexported >= 0);
|
||||
assert(fd_ino(reexported) == fd_ino(descriptor));
|
||||
close(reexported);
|
||||
get_info.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
|
||||
assert(GetSemaphoreFdKHR(device, &get_info, &reexported) == VK_ERROR_INVALID_EXTERNAL_HANDLE);
|
||||
DestroySemaphore(device, binary, NULL);
|
||||
assert(fcntl(descriptor, F_GETFD) == -1 && errno == EBADF);
|
||||
DestroySemaphore(device, semaphore, NULL);
|
||||
assert(destroyed == 2 && !bridged_find(semaphore));
|
||||
puts("semaphore layer: pending waits retained, errors propagated, unsupported binary imports refused, fd ownership passed");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
#define VK_NO_PROTOTYPES
|
||||
#include <vulkan/vulkan.h>
|
||||
#include <cstdio>
|
||||
#include <dlfcn.h>
|
||||
#include <vector>
|
||||
#include <linux/sync_file.h>
|
||||
#include <poll.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main() {
|
||||
void *library = dlopen("libvulkan.so.1", RTLD_NOW);
|
||||
if (!library) { std::puts(dlerror()); return 1; }
|
||||
auto get = reinterpret_cast<PFN_vkGetInstanceProcAddr>(dlsym(library, "vkGetInstanceProcAddr"));
|
||||
auto create = reinterpret_cast<PFN_vkCreateInstance>(get(VK_NULL_HANDLE, "vkCreateInstance"));
|
||||
VkApplicationInfo application{VK_STRUCTURE_TYPE_APPLICATION_INFO};
|
||||
application.pApplicationName = "DroidDeck sync capability probe";
|
||||
application.apiVersion = VK_API_VERSION_1_2;
|
||||
VkInstanceCreateInfo info{VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO}; info.pApplicationInfo = &application;
|
||||
VkInstance instance = VK_NULL_HANDLE;
|
||||
VkResult result = create(&info, nullptr, &instance);
|
||||
std::printf("create_instance=%d\n", result);
|
||||
if (result != VK_SUCCESS) return 1;
|
||||
auto enumerate = reinterpret_cast<PFN_vkEnumeratePhysicalDevices>(get(instance, "vkEnumeratePhysicalDevices"));
|
||||
uint32_t count = 0;
|
||||
result = enumerate(instance, &count, nullptr);
|
||||
std::printf("enumerate=%d devices=%u\n", result, count);
|
||||
if (result != VK_SUCCESS || !count) return 1;
|
||||
std::vector<VkPhysicalDevice> devices(count);
|
||||
if (enumerate(instance, &count, devices.data()) != VK_SUCCESS) return 1;
|
||||
auto fenceProperties = reinterpret_cast<PFN_vkGetPhysicalDeviceExternalFenceProperties>(get(instance, "vkGetPhysicalDeviceExternalFenceProperties"));
|
||||
auto semaphoreProperties = reinterpret_cast<PFN_vkGetPhysicalDeviceExternalSemaphoreProperties>(get(instance, "vkGetPhysicalDeviceExternalSemaphoreProperties"));
|
||||
for (auto handleType : {VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT, VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT}) {
|
||||
VkPhysicalDeviceExternalFenceInfo query{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_FENCE_INFO};
|
||||
query.handleType = handleType;
|
||||
VkExternalFenceProperties properties{VK_STRUCTURE_TYPE_EXTERNAL_FENCE_PROPERTIES};
|
||||
fenceProperties(devices[0], &query, &properties);
|
||||
std::printf("fence type=%u features=%u compatible=%u\n", handleType, properties.externalFenceFeatures, properties.compatibleHandleTypes);
|
||||
}
|
||||
for (auto handleType : {VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT, VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT}) {
|
||||
VkPhysicalDeviceExternalSemaphoreInfo query{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_SEMAPHORE_INFO};
|
||||
query.handleType = handleType;
|
||||
VkExternalSemaphoreProperties properties{VK_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_PROPERTIES};
|
||||
semaphoreProperties(devices[0], &query, &properties);
|
||||
std::printf("semaphore type=%u features=%u compatible=%u\n", handleType, properties.externalSemaphoreFeatures, properties.compatibleHandleTypes);
|
||||
}
|
||||
auto check = [](VkResult status, const char *step) {
|
||||
if (status != VK_SUCCESS) std::printf("%s=%d\n", step, status);
|
||||
return status == VK_SUCCESS;
|
||||
};
|
||||
auto createDevice = reinterpret_cast<PFN_vkCreateDevice>(get(instance, "vkCreateDevice"));
|
||||
float priority = 1;
|
||||
VkDeviceQueueCreateInfo queueInfo{VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO};
|
||||
queueInfo.queueFamilyIndex = 0; queueInfo.queueCount = 1; queueInfo.pQueuePriorities = &priority;
|
||||
const char *extensions[] = {VK_KHR_EXTERNAL_FENCE_FD_EXTENSION_NAME};
|
||||
VkDeviceCreateInfo deviceInfo{VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO};
|
||||
deviceInfo.queueCreateInfoCount = 1; deviceInfo.pQueueCreateInfos = &queueInfo;
|
||||
deviceInfo.enabledExtensionCount = 1; deviceInfo.ppEnabledExtensionNames = extensions;
|
||||
VkDevice device = VK_NULL_HANDLE;
|
||||
if (!check(createDevice(devices[0], &deviceInfo, nullptr, &device), "create_device")) return 1;
|
||||
auto getDeviceProcAddr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(get(instance, "vkGetDeviceProcAddr"));
|
||||
#define DEVICE_PROC(name) auto name = reinterpret_cast<PFN_vk##name>(getDeviceProcAddr(device, "vk" #name))
|
||||
DEVICE_PROC(GetDeviceQueue); DEVICE_PROC(CreateBuffer); DEVICE_PROC(GetBufferMemoryRequirements);
|
||||
DEVICE_PROC(AllocateMemory); DEVICE_PROC(BindBufferMemory); DEVICE_PROC(MapMemory);
|
||||
DEVICE_PROC(CreateCommandPool); DEVICE_PROC(AllocateCommandBuffers); DEVICE_PROC(BeginCommandBuffer);
|
||||
DEVICE_PROC(CmdFillBuffer); DEVICE_PROC(CmdPipelineBarrier); DEVICE_PROC(EndCommandBuffer);
|
||||
DEVICE_PROC(CreateFence); DEVICE_PROC(QueueSubmit); DEVICE_PROC(GetFenceFdKHR); DEVICE_PROC(QueueWaitIdle);
|
||||
DEVICE_PROC(InvalidateMappedMemoryRanges); DEVICE_PROC(UnmapMemory); DEVICE_PROC(DestroyFence);
|
||||
DEVICE_PROC(DestroyCommandPool); DEVICE_PROC(DestroyBuffer); DEVICE_PROC(FreeMemory); DEVICE_PROC(DestroyDevice);
|
||||
VkQueue queue = VK_NULL_HANDLE; GetDeviceQueue(device, 0, 0, &queue);
|
||||
VkBufferCreateInfo bufferInfo{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
|
||||
bufferInfo.size = 4096; bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
||||
VkBuffer buffer = VK_NULL_HANDLE;
|
||||
if (!check(CreateBuffer(device, &bufferInfo, nullptr, &buffer), "create_buffer")) return 1;
|
||||
VkMemoryRequirements requirements{}; GetBufferMemoryRequirements(device, buffer, &requirements);
|
||||
auto getMemoryProperties = reinterpret_cast<PFN_vkGetPhysicalDeviceMemoryProperties>(get(instance, "vkGetPhysicalDeviceMemoryProperties"));
|
||||
VkPhysicalDeviceMemoryProperties memoryProperties{}; getMemoryProperties(devices[0], &memoryProperties);
|
||||
uint32_t memoryType = UINT32_MAX;
|
||||
for (uint32_t index = 0; index < memoryProperties.memoryTypeCount; index++) {
|
||||
if ((requirements.memoryTypeBits & (1u << index)) &&
|
||||
(memoryProperties.memoryTypes[index].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)) {
|
||||
memoryType = index; break;
|
||||
}
|
||||
}
|
||||
if (memoryType == UINT32_MAX) return 1;
|
||||
VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
|
||||
allocation.allocationSize = requirements.size; allocation.memoryTypeIndex = memoryType;
|
||||
VkDeviceMemory memory = VK_NULL_HANDLE;
|
||||
if (!check(AllocateMemory(device, &allocation, nullptr, &memory), "allocate_memory")) return 1;
|
||||
if (!check(BindBufferMemory(device, buffer, memory, 0), "bind_buffer")) return 1;
|
||||
VkCommandPoolCreateInfo poolInfo{VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO}; poolInfo.queueFamilyIndex = 0;
|
||||
VkCommandPool pool = VK_NULL_HANDLE;
|
||||
if (!check(CreateCommandPool(device, &poolInfo, nullptr, &pool), "create_pool")) return 1;
|
||||
VkCommandBufferAllocateInfo commandInfo{VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO};
|
||||
commandInfo.commandPool = pool; commandInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; commandInfo.commandBufferCount = 1;
|
||||
VkCommandBuffer command = VK_NULL_HANDLE;
|
||||
if (!check(AllocateCommandBuffers(device, &commandInfo, &command), "allocate_command")) return 1;
|
||||
VkCommandBufferBeginInfo begin{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO};
|
||||
if (!check(BeginCommandBuffer(command, &begin), "begin_command")) return 1;
|
||||
CmdFillBuffer(command, buffer, 0, 4096, 0x13579bdf);
|
||||
VkMemoryBarrier barrier{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
|
||||
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; barrier.dstAccessMask = VK_ACCESS_HOST_READ_BIT;
|
||||
CmdPipelineBarrier(command, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &barrier, 0, nullptr, 0, nullptr);
|
||||
if (!check(EndCommandBuffer(command), "end_command")) return 1;
|
||||
VkExportFenceCreateInfo exportInfo{VK_STRUCTURE_TYPE_EXPORT_FENCE_CREATE_INFO};
|
||||
exportInfo.handleTypes = VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
|
||||
VkFenceCreateInfo fenceInfo{VK_STRUCTURE_TYPE_FENCE_CREATE_INFO}; fenceInfo.pNext = &exportInfo;
|
||||
VkFence fence = VK_NULL_HANDLE;
|
||||
if (!check(CreateFence(device, &fenceInfo, nullptr, &fence), "create_fence")) return 1;
|
||||
VkSubmitInfo submission{VK_STRUCTURE_TYPE_SUBMIT_INFO}; submission.commandBufferCount = 1; submission.pCommandBuffers = &command;
|
||||
if (!check(QueueSubmit(queue, 1, &submission, fence), "submit")) return 1;
|
||||
VkFenceGetFdInfoKHR exportFd{VK_STRUCTURE_TYPE_FENCE_GET_FD_INFO_KHR};
|
||||
exportFd.fence = fence; exportFd.handleType = VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
|
||||
int descriptor = -2;
|
||||
if (!check(GetFenceFdKHR(device, &exportFd, &descriptor), "export_sync_fd")) return 1;
|
||||
std::printf("export_sync_fd=%d\n", descriptor);
|
||||
if (descriptor < -1) return 1;
|
||||
if (descriptor >= 0) {
|
||||
sync_file_info syncInfo{};
|
||||
int infoResult = ioctl(descriptor, SYNC_IOC_FILE_INFO, &syncInfo);
|
||||
std::printf("sync_file_info=%d fences=%u status=%d\n", infoResult, syncInfo.num_fences, syncInfo.status);
|
||||
if (infoResult != 0 || syncInfo.num_fences == 0) { close(descriptor); return 1; }
|
||||
pollfd signal{descriptor, POLLIN, 0};
|
||||
int waited = poll(&signal, 1, 1000);
|
||||
std::printf("poll=%d events=%u\n", waited, signal.revents);
|
||||
close(descriptor);
|
||||
if (waited != 1 || !(signal.revents & POLLIN) || (signal.revents & POLLERR)) return 1;
|
||||
}
|
||||
if (!check(QueueWaitIdle(queue), "queue_idle")) return 1;
|
||||
VkMappedMemoryRange mappedRange{VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE};
|
||||
mappedRange.memory = memory; mappedRange.size = VK_WHOLE_SIZE;
|
||||
void *mapping = nullptr;
|
||||
if (!check(MapMemory(device, memory, 0, VK_WHOLE_SIZE, 0, &mapping), "map_memory")) return 1;
|
||||
if (!check(InvalidateMappedMemoryRanges(device, 1, &mappedRange), "invalidate_memory")) return 1;
|
||||
uint32_t matches = 0;
|
||||
for (uint32_t index = 0; index < 1024; index++) matches += static_cast<uint32_t *>(mapping)[index] == 0x13579bdf;
|
||||
std::printf("gpu_pattern_matches=%u/1024\n", matches);
|
||||
UnmapMemory(device, memory); DestroyFence(device, fence, nullptr);
|
||||
DestroyCommandPool(device, pool, nullptr); DestroyBuffer(device, buffer, nullptr);
|
||||
FreeMemory(device, memory, nullptr); DestroyDevice(device, nullptr);
|
||||
auto destroy = reinterpret_cast<PFN_vkDestroyInstance>(get(instance, "vkDestroyInstance"));
|
||||
destroy(instance, nullptr);
|
||||
return matches == 1024 ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
#include "../src/xr_frame_channel.h"
|
||||
#include "../src/xr_frame_math.h"
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <limits>
|
||||
|
||||
static DroidDeckXrFrameHeader Header(uint64_t id = 1) {
|
||||
DroidDeckXrFrameHeader header{};
|
||||
header.magic = DROIDDECK_XR_FRAME_MAGIC; header.version = DROIDDECK_XR_FRAME_VERSION;
|
||||
header.width = 8; header.height = 4; header.bytes = 128; header.color_space = 1;
|
||||
header.frame_id = id; header.captured_ns = xr_frame_now();
|
||||
for (int eye = 0; eye < 2; eye++) {
|
||||
for (int axis = 0; axis < 3; axis++) header.eye_pose[eye][axis][axis] = 1;
|
||||
header.eye_pose[eye][0][3] = eye == 0 ? -0.032f : 0.032f;
|
||||
header.eye_pose[eye][1][3] = 1.6f;
|
||||
header.projection[eye][0][0] = header.projection[eye][1][1] = 1;
|
||||
header.projection[eye][2][2] = -1.002f; header.projection[eye][2][3] = -0.1001f;
|
||||
header.projection[eye][3][2] = -1;
|
||||
}
|
||||
return header;
|
||||
}
|
||||
|
||||
static int Connect(const std::string &path) {
|
||||
int descriptor = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
||||
assert(descriptor >= 0);
|
||||
sockaddr_un address{}; address.sun_family = AF_UNIX;
|
||||
std::strcpy(address.sun_path, path.c_str());
|
||||
assert(connect(descriptor, reinterpret_cast<sockaddr *>(&address), sizeof(address)) == 0);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
static void WaitRejected(XrFrameReceiver &receiver, uint64_t count) {
|
||||
int64_t deadline = xr_frame_now() + 1000000000LL;
|
||||
while (receiver.Rejected() < count && xr_frame_now() < deadline) usleep(1000);
|
||||
assert(receiver.Rejected() == count);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
char directory[256];
|
||||
std::snprintf(directory, sizeof(directory), "%s/droiddeck-frames-XXXXXX", argc > 1 ? argv[1] : "/tmp");
|
||||
assert(mkdtemp(directory));
|
||||
std::string socketPath = std::string(directory) + "/frames";
|
||||
auto valid = Header(); assert(xr_frame_valid(valid));
|
||||
auto invalid = valid; invalid.version++; assert(!xr_frame_valid(invalid));
|
||||
invalid = valid; invalid.width = 3; assert(!xr_frame_valid(invalid));
|
||||
invalid = valid; invalid.bytes--; assert(!xr_frame_valid(invalid));
|
||||
invalid = valid; invalid.width = 8192; invalid.height = 4096; assert(!xr_frame_valid(invalid));
|
||||
invalid = valid; invalid.eye_pose[0][0][0] = -1; assert(!xr_frame_valid(invalid));
|
||||
invalid = valid; invalid.eye_pose[0][1][1] = 2; assert(!xr_frame_valid(invalid));
|
||||
invalid = valid; invalid.projection[0][0][0] = std::numeric_limits<float>::quiet_NaN(); assert(!xr_frame_valid(invalid));
|
||||
invalid = valid; invalid.projection[0][3][2] = 0; assert(!xr_frame_valid(invalid));
|
||||
invalid = valid; invalid.projection[0][1][1] = -1; assert(!xr_frame_valid(invalid));
|
||||
auto view = xr_frame_view(valid, 0);
|
||||
assert(view.orientation[3] == 1 && view.position[0] == -0.032f && view.position[1] == 1.6f);
|
||||
assert(std::abs(view.fov[0] + 0.785398f) < 0.00001f && std::abs(view.fov[2] - 0.785398f) < 0.00001f);
|
||||
auto rotated = valid;
|
||||
rotated.eye_pose[0][0][0] = rotated.eye_pose[0][2][2] = 0;
|
||||
rotated.eye_pose[0][0][2] = 1; rotated.eye_pose[0][2][0] = -1;
|
||||
assert(xr_frame_valid(rotated)); view = xr_frame_view(rotated, 0);
|
||||
assert(std::abs(view.orientation[1] - 0.70710678f) < 0.00001f);
|
||||
float eyeToHead[3][4]{{1, 0, 0, 0.032f}, {0, 1, 0, 0}, {0, 0, 1, 0}};
|
||||
auto paired = rotated;
|
||||
xr_frame_eye_pose(paired.eye_pose[0], rotated.eye_pose[0], eyeToHead);
|
||||
rotated.eye_pose[0][1][3] = 9;
|
||||
assert(paired.eye_pose[0][1][3] == 1.6f && paired.eye_pose[0][2][3] == -0.032f);
|
||||
assert(xr_frame_valid(paired));
|
||||
for (int axis = 0; axis < 3; axis++) {
|
||||
rotated = valid;
|
||||
for (int other = 0; other < 3; other++) if (other != axis) rotated.eye_pose[0][other][other] = -1;
|
||||
assert(xr_frame_valid(rotated)); view = xr_frame_view(rotated, 0);
|
||||
assert(view.orientation[axis] == 1 && view.orientation[3] == 0);
|
||||
}
|
||||
valid.projection[0][0][2] = 0.2f; valid.projection[0][1][2] = -0.1f;
|
||||
view = xr_frame_view(valid, 0);
|
||||
assert(std::abs(std::tan(view.fov[0]) + 0.8f) < 0.00001f && std::abs(std::tan(view.fov[2]) - 0.9f) < 0.00001f);
|
||||
XrFrameReceiver receiver; assert(receiver.Start(socketPath));
|
||||
struct stat status{}; assert(stat(socketPath.c_str(), &status) == 0 && (status.st_mode & 0777) == 0600);
|
||||
XrFrameReceiver other; assert(!other.Start(socketPath));
|
||||
XrFrameSender sender;
|
||||
std::vector<unsigned char> pixels(valid.bytes, 17);
|
||||
assert(sender.Send(socketPath, valid, pixels.data()));
|
||||
pixels.assign(valid.bytes, 99);
|
||||
DroidDeckXrPixels taken; assert(receiver.Take(&taken));
|
||||
assert(taken.header.frame_id == 1 && taken.pixels[0] == 17 && taken.header.eye_pose[0][0][3] == -0.032f);
|
||||
assert(!receiver.Take(&taken));
|
||||
for (uint64_t id = 2; id <= 100; id++) {
|
||||
auto header = Header(id); header.eye_pose[0][0][3] = static_cast<float>(id);
|
||||
pixels.assign(header.bytes, static_cast<unsigned char>(id));
|
||||
assert(sender.Send(socketPath, header, pixels.data()));
|
||||
}
|
||||
assert(receiver.Take(&taken));
|
||||
assert(taken.header.frame_id == 100 && taken.header.eye_pose[0][0][3] == 100 && taken.pixels[0] == 100);
|
||||
assert(receiver.Received() == 100);
|
||||
assert(!sender.Send(socketPath, Header(100), pixels.data())); WaitRejected(receiver, 1);
|
||||
assert(sender.Send(socketPath, Header(101), pixels.data())); assert(receiver.Take(&taken));
|
||||
receiver.Stop(); assert(!sender.Send(socketPath, Header(102), pixels.data()));
|
||||
assert(receiver.Start(socketPath)); assert(sender.Send(socketPath, Header(1), pixels.data()));
|
||||
assert(receiver.Take(&taken));
|
||||
receiver.Stop(); assert(receiver.Start(socketPath));
|
||||
int descriptor = Connect(socketPath); auto header = Header(); header.version++;
|
||||
assert(xr_frame_io(descriptor, &header, sizeof(header), true, xr_frame_now() + 200000000LL));
|
||||
close(descriptor); WaitRejected(receiver, 1); assert(!receiver.Take(&taken));
|
||||
descriptor = Connect(socketPath); header = Header(); header.captured_ns += 1000000000LL;
|
||||
assert(xr_frame_io(descriptor, &header, sizeof(header), true, xr_frame_now() + 200000000LL));
|
||||
close(descriptor); WaitRejected(receiver, 2);
|
||||
descriptor = Connect(socketPath); header = Header(); header.captured_ns -= 1000000000LL;
|
||||
assert(xr_frame_io(descriptor, &header, sizeof(header), true, xr_frame_now() + 200000000LL));
|
||||
close(descriptor); WaitRejected(receiver, 3);
|
||||
descriptor = Connect(socketPath); header = Header();
|
||||
assert(xr_frame_io(descriptor, &header, sizeof(header), true, xr_frame_now() + 200000000LL));
|
||||
assert(send(descriptor, pixels.data(), 1, MSG_NOSIGNAL) == 1);
|
||||
close(descriptor); WaitRejected(receiver, 4); assert(!receiver.Take(&taken));
|
||||
descriptor = Connect(socketPath); header = Header();
|
||||
assert(xr_frame_io(descriptor, &header, sizeof(header), true, xr_frame_now() + 200000000LL));
|
||||
WaitRejected(receiver, 5); close(descriptor);
|
||||
assert(!sender.Send(socketPath, Header(200), pixels.data()));
|
||||
assert(sender.Send(socketPath, Header(201), pixels.data()));
|
||||
receiver.Stop();
|
||||
int stale = socket(AF_UNIX, SOCK_STREAM, 0); sockaddr_un address{}; address.sun_family = AF_UNIX;
|
||||
std::strcpy(address.sun_path, socketPath.c_str());
|
||||
assert(bind(stale, reinterpret_cast<sockaddr *>(&address), sizeof(address)) == 0); close(stale);
|
||||
assert(receiver.Start(socketPath)); receiver.Stop();
|
||||
int regular = open(socketPath.c_str(), O_CREAT | O_RDWR, 0600); assert(regular >= 0); close(regular);
|
||||
assert(!receiver.Start(socketPath)); assert(stat(socketPath.c_str(), &status) == 0 && S_ISREG(status.st_mode));
|
||||
unlink(socketPath.c_str()); unlink((socketPath + ".lock").c_str()); rmdir(directory);
|
||||
std::puts("XR frame validation passed: owned pixels, paired render pose, latest mailbox, bounds, projection, restart, stale socket, replay, malformed and timeout rejection");
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#include "../src/xr_touch_input.h"
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <limits>
|
||||
|
||||
int main() {
|
||||
DroidDeckXrHand hand{};
|
||||
hand.active = 1;
|
||||
hand.trigger = 0.7f; hand.squeeze = 0.8f;
|
||||
hand.stick[0] = -0.4f; hand.stick[1] = 0.9f;
|
||||
auto input = xr_touch_input(hand, false);
|
||||
assert(input.trigger == hand.trigger && input.grip == hand.squeeze && input.triggerClick && input.gripClick);
|
||||
assert(input.stickX == -0.4f && input.stickY == 0.9f);
|
||||
assert(!input.stickTouch && !input.stickClick && !input.primaryClick && !input.secondaryClick && !input.menuClick);
|
||||
hand.buttons = DROIDDECK_XR_PRIMARY_CLICK;
|
||||
input = xr_touch_input(hand, false);
|
||||
assert(input.primaryClick && !input.secondaryClick && !input.stickClick && !input.menuClick && !input.primaryTouch);
|
||||
hand.buttons = DROIDDECK_XR_SECONDARY_CLICK;
|
||||
input = xr_touch_input(hand, true);
|
||||
assert(input.secondaryClick && !input.primaryClick && !input.menuClick && !input.stickClick);
|
||||
hand.buttons = DROIDDECK_XR_STICK_CLICK;
|
||||
input = xr_touch_input(hand, true);
|
||||
assert(input.stickClick && !input.primaryClick && !input.stickTouch);
|
||||
hand.buttons = DROIDDECK_XR_MENU_CLICK;
|
||||
assert(xr_touch_input(hand, false).menuClick);
|
||||
assert(!xr_touch_input(hand, true).menuClick);
|
||||
hand.buttons = DROIDDECK_XR_TRIGGER_TOUCH | DROIDDECK_XR_STICK_TOUCH | DROIDDECK_XR_PRIMARY_TOUCH |
|
||||
DROIDDECK_XR_SECONDARY_TOUCH | DROIDDECK_XR_THUMBREST_TOUCH;
|
||||
hand.trigger = hand.squeeze = 0;
|
||||
input = xr_touch_input(hand, false);
|
||||
assert(input.triggerTouch && input.stickTouch && input.primaryTouch && input.secondaryTouch && input.thumbrestTouch);
|
||||
assert(!input.triggerClick && !input.gripClick && !input.primaryClick && !input.secondaryClick && !input.stickClick);
|
||||
hand.trigger = 2; hand.squeeze = -2; hand.stick[0] = -3; hand.stick[1] = 3;
|
||||
input = xr_touch_input(hand, false);
|
||||
assert(input.trigger == 1 && input.grip == 0 && input.stickX == -1 && input.stickY == 1);
|
||||
hand.trigger = std::numeric_limits<float>::quiet_NaN();
|
||||
hand.squeeze = std::numeric_limits<float>::infinity();
|
||||
hand.stick[0] = hand.trigger; hand.stick[1] = -hand.squeeze;
|
||||
input = xr_touch_input(hand, false);
|
||||
assert(input.trigger == 0 && input.grip == 0 && input.stickX == 0 && input.stickY == 0);
|
||||
hand.active = 0;
|
||||
hand.buttons = UINT32_MAX;
|
||||
hand.trigger = hand.squeeze = hand.stick[0] = hand.stick[1] = 1;
|
||||
input = xr_touch_input(hand, false);
|
||||
assert(input.trigger == 0 && input.grip == 0 && input.stickX == 0 && input.stickY == 0);
|
||||
assert(!input.triggerClick && !input.gripClick && !input.stickClick && !input.primaryClick && !input.secondaryClick && !input.menuClick);
|
||||
assert(!input.triggerTouch && !input.stickTouch && !input.primaryTouch && !input.secondaryTouch && !input.thumbrestTouch);
|
||||
std::puts("Touch input: independent face/stick/menu, real capacitive touch, analog grip, clamping and inactive reset passed");
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
#include "../src/xr_tracking_file.h"
|
||||
#include "../src/xr_display_mode.h"
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <limits>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
assert(XrTrackingFile::PoseTimeOffset(1010000000LL, 1000000000LL) == 0.01);
|
||||
assert(XrTrackingFile::PoseTimeOffset(990000000LL, 1000000000LL) == -0.01);
|
||||
assert(XrTrackingFile::PoseTimeOffset(0, 1000000000LL) == 0);
|
||||
assert(XrTrackingFile::PoseTimeOffset(-1, 1000000000LL) == 0);
|
||||
char filename[256];
|
||||
std::snprintf(filename, sizeof(filename), "%s/droiddeck-xr-test-XXXXXX", argc > 1 ? argv[1] : "/tmp");
|
||||
int descriptor = mkstemp(filename);
|
||||
assert(descriptor >= 0);
|
||||
assert(ftruncate(descriptor, sizeof(DroidDeckXrTracking)) == 0);
|
||||
auto *writer = static_cast<DroidDeckXrTracking *>(mmap(nullptr, sizeof(DroidDeckXrTracking),
|
||||
PROT_READ | PROT_WRITE, MAP_SHARED, descriptor, 0));
|
||||
assert(writer != MAP_FAILED);
|
||||
{
|
||||
XrTrackingFile reader(filename);
|
||||
DroidDeckXrTracking packet{};
|
||||
assert(!reader.Read(&packet));
|
||||
writer->magic = DROIDDECK_XR_MAGIC; writer->version = DROIDDECK_XR_VERSION;
|
||||
writer->active = 1; writer->sampled_ns = XrTrackingFile::Now(); writer->period_ns = 13888889;
|
||||
writer->eyes[0].width = 1680; writer->eyes[0].height = 1760;
|
||||
writer->head.orientation[3] = 1; writer->head.position[1] = 1.6f; writer->head.flags = 15;
|
||||
assert(reader.Read(&packet)); assert(XrTrackingFile::Fresh(packet));
|
||||
assert(packet.head.position[1] == 1.6f);
|
||||
assert(XrTrackingFile::Tracked(packet.head));
|
||||
assert(XrDisplayMode(packet) == "3360x1760@72");
|
||||
auto displayTracking = packet;
|
||||
displayTracking.period_ns = 11111111;
|
||||
assert(XrDisplayMode(displayTracking) == "3360x1760@90");
|
||||
displayTracking.period_ns = 8333547;
|
||||
assert(XrDisplayMode(displayTracking) == "3360x1760@120");
|
||||
displayTracking.active = 0;
|
||||
assert(XrDisplayMode(displayTracking).empty());
|
||||
displayTracking = packet; displayTracking.sampled_ns -= 1000000000LL;
|
||||
assert(XrDisplayMode(displayTracking).empty());
|
||||
displayTracking = packet; displayTracking.period_ns = 0;
|
||||
assert(XrDisplayMode(displayTracking).empty());
|
||||
auto invalidPose = packet.head; invalidPose.flags = 7;
|
||||
assert(!XrTrackingFile::Tracked(invalidPose));
|
||||
invalidPose = packet.head; invalidPose.orientation[3] = 0;
|
||||
assert(!XrTrackingFile::Tracked(invalidPose));
|
||||
invalidPose = packet.head; invalidPose.position[0] = std::numeric_limits<float>::quiet_NaN();
|
||||
assert(!XrTrackingFile::Tracked(invalidPose));
|
||||
__atomic_store_n(&writer->seq, 1u, __ATOMIC_RELEASE);
|
||||
assert(!reader.Read(&packet));
|
||||
__atomic_store_n(&writer->seq, 2u, __ATOMIC_RELEASE);
|
||||
writer->version++;
|
||||
assert(!reader.Read(&packet));
|
||||
writer->version = DROIDDECK_XR_VERSION;
|
||||
writer->sampled_ns = XrTrackingFile::Now() - 1000000000LL;
|
||||
assert(reader.Read(&packet)); assert(!XrTrackingFile::Fresh(packet));
|
||||
writer->sampled_ns = XrTrackingFile::Now() + 1000000000LL;
|
||||
assert(reader.Read(&packet)); assert(!XrTrackingFile::Fresh(packet));
|
||||
writer->sampled_ns = XrTrackingFile::Now(); writer->active = 0;
|
||||
assert(reader.Read(&packet)); assert(!XrTrackingFile::Fresh(packet));
|
||||
writer->active = 1; writer->eyes[0].width = 9000;
|
||||
assert(reader.Read(&packet)); assert(!XrTrackingFile::Fresh(packet));
|
||||
}
|
||||
munmap(writer, sizeof(DroidDeckXrTracking));
|
||||
assert(ftruncate(descriptor, 16) == 0);
|
||||
{
|
||||
XrTrackingFile reader(filename);
|
||||
DroidDeckXrTracking packet{};
|
||||
assert(!reader.Read(&packet));
|
||||
}
|
||||
close(descriptor); unlink(filename);
|
||||
std::puts("XR IPC validation passed: valid, seqlock, version, stale, future, inactive, dimensions, truncated");
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
#include "../src/xr_vsync_timing.h"
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <initializer_list>
|
||||
#include <limits>
|
||||
#include <thread>
|
||||
|
||||
int main() {
|
||||
using namespace std::chrono;
|
||||
using Clock = XrVsyncTiming::Clock;
|
||||
XrVsyncTiming timing;
|
||||
assert(timing.Deadline(0) == Clock::time_point(XrVsyncTiming::kDefaultPeriod));
|
||||
auto target = Clock::time_point(seconds(1));
|
||||
for (int64_t periodNs : {13888889LL, 11111111LL, 8333333LL}) {
|
||||
auto period = nanoseconds(periodNs);
|
||||
timing.Record(target, period);
|
||||
assert(timing.Deadline(0) == target + period);
|
||||
assert(timing.Deadline(2) == target + period * 3);
|
||||
assert(timing.Deadline(std::numeric_limits<uint32_t>::max()) == target + period * 4294967296LL);
|
||||
auto now = target - milliseconds(2);
|
||||
auto scheduled = target + period;
|
||||
assert(timing.Target(now, scheduled, 1000000000LL, period) == scheduled);
|
||||
assert(timing.Target(now, scheduled, 0, period) == scheduled);
|
||||
assert(timing.Target(now, scheduled, 900000000LL, period) == scheduled);
|
||||
assert(timing.Target(now, scheduled, 2000000000LL, period) == scheduled);
|
||||
auto prediction = target + period * 2;
|
||||
auto predictedNs = duration_cast<nanoseconds>(prediction.time_since_epoch()).count();
|
||||
assert(timing.Target(now, scheduled, predictedNs, period) == prediction);
|
||||
}
|
||||
auto first = Clock::time_point(seconds(1));
|
||||
auto second = Clock::time_point(seconds(2));
|
||||
auto firstPeriod = nanoseconds(13888889);
|
||||
auto secondPeriod = nanoseconds(8333333);
|
||||
timing.Record(first, firstPeriod);
|
||||
std::atomic<bool> finished{false};
|
||||
std::thread writer([&] {
|
||||
for (int count = 0; count < 100000; count++) {
|
||||
timing.Record(second, secondPeriod);
|
||||
timing.Record(first, firstPeriod);
|
||||
}
|
||||
finished = true;
|
||||
});
|
||||
do {
|
||||
auto deadline = timing.Deadline(0);
|
||||
assert(deadline == first + firstPeriod || deadline == second + secondPeriod);
|
||||
} while (!finished);
|
||||
writer.join();
|
||||
timing.Reset();
|
||||
assert(timing.Deadline(0) == Clock::time_point(XrVsyncTiming::kDefaultPeriod));
|
||||
std::puts("XR pacing validation passed: refresh rates, throttling, repeated predictions, atomic tick/period, restart");
|
||||
}
|
||||
Reference in new issue
Block a user