mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 21:06:09 +00:00
feat: add immersive Quest SteamVR to the standard app (#910)
This commit is contained in:
1 parent
d21466be6b
commit
2847910f22
73 files changed
+5916
-289
No files matched your search
@@ -7,6 +7,7 @@
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
@@ -17,6 +18,11 @@
|
||||
|
||||
#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"
|
||||
#include "xr_chaperone.h"
|
||||
|
||||
#define DD_EXPORT extern "C" __attribute__((visibility("default")))
|
||||
|
||||
@@ -87,6 +93,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 +141,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 +174,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 +222,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 +258,19 @@ 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 = DROIDDECK_XR_COLOR_LINEAR_RGBA8;
|
||||
}
|
||||
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 +281,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 +296,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 +322,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 +389,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 +409,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 +432,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_, false, 0);
|
||||
driverInput->UpdateBooleanComponent(system_, input.systemClick, 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 +508,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 +528,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,17 +541,31 @@ 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);
|
||||
VRProperties()->SetUint64Property(c, Prop_CurrentUniverseId_Uint64, 2);
|
||||
chaperone_.Reset();
|
||||
if (xrMode_) PublishChaperone(xr);
|
||||
VRProperties()->SetBoolProperty(c, Prop_IsOnDesktop_Bool, false);
|
||||
VRProperties()->SetBoolProperty(c, Prop_DisplayDebugMode_Bool, false);
|
||||
directMode_ = VRSettings()->GetBool(kSettingsSection, "directMode");
|
||||
const char *presentation = std::getenv("DROIDDECK_VR_PRESENT");
|
||||
if (xrMode_ && presentation && !std::strcmp(presentation, "questwsi")) directMode_ = false;
|
||||
VRProperties()->SetBoolProperty(c, Prop_HasDriverDirectModeComponent_Bool, directMode_);
|
||||
if (directMode_) {
|
||||
VRProperties()->SetBoolProperty(c, Prop_DriverDirectModeSendsVsyncEvents_Bool, true);
|
||||
@@ -488,6 +610,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 +644,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 +652,13 @@ 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;
|
||||
xr_frame_projection_raw(fov, left, right, top, bottom);
|
||||
return;
|
||||
}
|
||||
*left = -tanHalf_ * eyeAspect_; *right = tanHalf_ * eyeAspect_;
|
||||
*top = -tanHalf_; *bottom = tanHalf_;
|
||||
}
|
||||
@@ -532,10 +674,34 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
void PublishChaperone(const DroidDeckXrTracking &tracking) {
|
||||
chaperone_.Update(tracking, [this](const DroidDeckXrPose &head) {
|
||||
char timestamp[32];
|
||||
std::time_t now = std::time(nullptr); std::tm utc{}; gmtime_r(&now, &utc);
|
||||
std::strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%SZ", &utc);
|
||||
const auto json = xr_chaperone_json(head, timestamp);
|
||||
const auto container = VRProperties()->TrackedDeviceToPropertyContainer(id_);
|
||||
return VRProperties()->SetStringProperty(container, Prop_DriverProvidedChaperoneJson_String, json.c_str()) == TrackedProp_Success;
|
||||
});
|
||||
}
|
||||
void PoseLoop() {
|
||||
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);
|
||||
if (fresh) PublishChaperone(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 +736,9 @@ public:
|
||||
|
||||
private:
|
||||
PoseFile pose_;
|
||||
XrTrackingFile xr_;
|
||||
XrChaperoneOrigin chaperone_;
|
||||
bool xrMode_ = false;
|
||||
PadRing pad_;
|
||||
PadState lastPad_;
|
||||
VRInputComponentHandle_t proximity_ = 0;
|
||||
@@ -586,6 +755,7 @@ class Provider : public IServerTrackedDeviceProvider {
|
||||
public:
|
||||
EVRInitError Init(IVRDriverContext *ctx) override {
|
||||
VR_INIT_SERVER_DRIVER_CONTEXT(ctx);
|
||||
focusEnabled_ = access("/run/droiddeck/droiddeck-xr-frames", F_OK) == 0;
|
||||
Log("DroidDeck Cardboard driver loaded");
|
||||
if (!VRServerDriverHost()->TrackedDeviceAdded("droiddeck-cardboard-hmd", TrackedDeviceClass_HMD, &hmd_))
|
||||
Log("TrackedDeviceAdded failed");
|
||||
@@ -596,17 +766,72 @@ public:
|
||||
}
|
||||
return VRInitError_None;
|
||||
}
|
||||
void Cleanup() override { VR_CLEANUP_SERVER_DRIVER_CONTEXT(); }
|
||||
void Cleanup() override {
|
||||
if (focusEnabled_) unlink(kFocusPath);
|
||||
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 (focusEnabled_) {
|
||||
bool changed = false;
|
||||
if (e.eventType == VREvent_SceneApplicationChanged) { scenePid_ = e.data.process.pid; changed = true; }
|
||||
if (e.eventType == VREvent_DashboardActivated) { dashboard_ = true; changed = true; }
|
||||
if (e.eventType == VREvent_DashboardDeactivated) { dashboard_ = false; changed = true; }
|
||||
if (changed) PublishFocus();
|
||||
}
|
||||
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:
|
||||
static constexpr const char *kFocusPath = "/run/droiddeck/droiddeck-steamvr/scene-focus.json";
|
||||
bool focusEnabled_ = false, dashboard_ = true;
|
||||
uint32_t scenePid_ = 0;
|
||||
void PublishFocus() {
|
||||
const std::string temporary = std::string(kFocusPath) + ".tmp";
|
||||
int fd = open(temporary.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, 0600);
|
||||
if (fd < 0) return;
|
||||
char body[128];
|
||||
int length = snprintf(body, sizeof(body), "{\"serverPid\":%d,\"scenePid\":%u,\"dashboard\":%s}\n",
|
||||
getpid(), scenePid_, dashboard_ ? "true" : "false");
|
||||
bool written = write(fd, body, length) == length;
|
||||
close(fd);
|
||||
if (written) rename(temporary.c_str(), kFocusPath);
|
||||
else unlink(temporary.c_str());
|
||||
}
|
||||
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,207 @@
|
||||
#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};
|
||||
if (!droiddeck_producer_io(connection, &message, 1, droiddeck_producer_now() + 100000000LL)) {
|
||||
server->release();
|
||||
close(connection);
|
||||
continue;
|
||||
}
|
||||
int valid = 1;
|
||||
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;
|
||||
struct stat existing;
|
||||
if (lstat(address.sun_path, &existing) == 0) {
|
||||
if (!S_ISSOCK(existing.st_mode) || existing.st_uid != getuid()) return 0;
|
||||
int probe = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
|
||||
if (probe < 0) return 0;
|
||||
int connected = connect(probe, (struct sockaddr *)&address, sizeof(address));
|
||||
int error = errno;
|
||||
close(probe);
|
||||
if (connected == 0 || error != ECONNREFUSED || unlink(address.sun_path) != 0) return 0;
|
||||
} else if (errno != ENOENT) 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,11 @@
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#define DROIDDECK_PRODUCER_SERVER
|
||||
#include "producer_lease.h"
|
||||
#ifdef DROIDDECK_WSI_CAPTURE
|
||||
#include "wsi_capture.h"
|
||||
#endif
|
||||
|
||||
#define LAYER_NAME "VK_LAYER_DROIDDECK_vr"
|
||||
#define EXPORT __attribute__((visibility("default")))
|
||||
@@ -30,13 +35,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 +92,7 @@ struct inst {
|
||||
PFN_vkDestroyInstance destroy;
|
||||
int stripped;
|
||||
int active;
|
||||
int producer;
|
||||
};
|
||||
struct dev {
|
||||
VkDevice handle;
|
||||
@@ -87,6 +100,8 @@ struct dev {
|
||||
PFN_vkDestroyDevice destroy;
|
||||
int active;
|
||||
int display;
|
||||
int producer;
|
||||
int quest_images;
|
||||
};
|
||||
|
||||
static int steamvr_running(void) {
|
||||
@@ -187,8 +202,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;
|
||||
}
|
||||
}
|
||||
@@ -223,33 +242,64 @@ static VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice pd, const VkDeviceCreat
|
||||
const int display = in && in->stripped;
|
||||
|
||||
VkDeviceCreateInfo mine = *ci;
|
||||
const char **names = calloc(ci->enabledExtensionCount + 1, sizeof(*names));
|
||||
const char **names = calloc(ci->enabledExtensionCount + 4, sizeof(*names));
|
||||
if (!names) return VK_ERROR_OUT_OF_HOST_MEMORY;
|
||||
uint32_t n = 0;
|
||||
for (uint32_t i = 0; i < ci->enabledExtensionCount; i++) {
|
||||
if (display && !strcmp(ci->ppEnabledExtensionNames[i], "VK_EXT_display_control")) { say("taking %s off the device", ci->ppEnabledExtensionNames[i]); continue; }
|
||||
names[n++] = ci->ppEnabledExtensionNames[i];
|
||||
}
|
||||
#ifdef DROIDDECK_WSI_CAPTURE
|
||||
const char *present_mode = getenv("DROIDDECK_VR_PRESENT");
|
||||
if (present_mode && !strcmp(present_mode, "questwsi") && !strcmp(program_invocation_short_name, "vrcompositor")) {
|
||||
PFN_vkEnumerateDeviceExtensionProperties enumerate = (PFN_vkEnumerateDeviceExtensionProperties)
|
||||
next_gipa(in ? in->handle : VK_NULL_HANDLE, "vkEnumerateDeviceExtensionProperties");
|
||||
uint32_t count = 0;
|
||||
if (enumerate && enumerate(pd, NULL, &count, NULL) == VK_SUCCESS && count) {
|
||||
VkExtensionProperties *supported = calloc(count, sizeof(*supported));
|
||||
if (supported && enumerate(pd, NULL, &count, supported) == VK_SUCCESS) {
|
||||
const char *required[] = {"VK_KHR_external_memory_fd", "VK_EXT_external_memory_dma_buf", "VK_EXT_queue_family_foreign"};
|
||||
for (unsigned j = 0; j < 3; j++) {
|
||||
int enabled = 0, available = 0;
|
||||
for (uint32_t k = 0; k < n; k++) if (!strcmp(names[k], required[j])) enabled = 1;
|
||||
for (uint32_t k = 0; k < count; k++) if (!strcmp(supported[k].extensionName, required[j])) available = 1;
|
||||
if (!enabled && available) names[n++] = required[j];
|
||||
}
|
||||
}
|
||||
free(supported);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
mine.enabledExtensionCount = n;
|
||||
mine.ppEnabledExtensionNames = names;
|
||||
VkResult r = next_create(pd, &mine, alloc, out);
|
||||
free(names);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
if (r != VK_SUCCESS) { free(names); return r; }
|
||||
pthread_mutex_lock(&g_lock);
|
||||
for (int i = 0; i < MAX_OBJS; i++) {
|
||||
if (!g_dev[i].handle) {
|
||||
g_dev[i] = (struct dev){*out, next_gdpa, (PFN_vkDestroyDevice)next_gdpa(*out, "vkDestroyDevice"), active, display};
|
||||
g_dev[i] = (struct dev){*out, next_gdpa, (PFN_vkDestroyDevice)next_gdpa(*out, "vkDestroyDevice"), active, display,
|
||||
in && in->producer, active && access("/run/droiddeck/droiddeck-xr-frames", F_OK) == 0};
|
||||
break;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&g_lock);
|
||||
#ifdef DROIDDECK_WSI_CAPTURE
|
||||
if (in) droiddeck_wsi_device(*out, pd, in->handle, in->gipa, next_gdpa, &mine);
|
||||
#endif
|
||||
free(names);
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
static void forget_device_semaphores(VkDevice dev);
|
||||
static void producer_destroy_device(struct dev *device, const VkAllocationCallbacks *allocator);
|
||||
|
||||
static void VKAPI_CALL DestroyDevice(VkDevice device, const VkAllocationCallbacks *alloc) {
|
||||
struct dev *d = dev_for(device);
|
||||
if (!d) return;
|
||||
#ifdef DROIDDECK_WSI_CAPTURE
|
||||
droiddeck_wsi_destroy(device);
|
||||
#endif
|
||||
if (d->producer) producer_destroy_device(d, alloc);
|
||||
forget_device_semaphores(device);
|
||||
PFN_vkDestroyDevice f = d->destroy;
|
||||
pthread_mutex_lock(&g_lock);
|
||||
@@ -263,6 +313,7 @@ static void VKAPI_CALL DestroyDevice(VkDevice device, const VkAllocationCallback
|
||||
|
||||
#define FAKE_DISPLAY ((VkDisplayKHR)(uintptr_t)0xdd0001)
|
||||
#define FAKE_MODE ((VkDisplayModeKHR)(uintptr_t)0xdd0002)
|
||||
static VkSurfaceKHR g_hmd_surface;
|
||||
static uint32_t g_mode_w = 1920, g_mode_h = 1080, g_mode_mhz = 60000;
|
||||
|
||||
static void mode_init(void) {
|
||||
@@ -365,6 +416,7 @@ static VkResult VKAPI_CALL CreateDisplayPlaneSurfaceKHR(VkInstance i, const VkDi
|
||||
if (!headless) { say("%s: no headless surface to stand in", "vkCreateDisplayPlaneSurfaceKHR"); return VK_ERROR_INITIALIZATION_FAILED; }
|
||||
VkHeadlessSurfaceCreateInfoEXT hci = {VK_STRUCTURE_TYPE_HEADLESS_SURFACE_CREATE_INFO_EXT, NULL, 0};
|
||||
VkResult r = headless(i, &hci, a, s);
|
||||
if (r == VK_SUCCESS) g_hmd_surface = *s;
|
||||
say("the display's surface is a headless one (%s)", r == VK_SUCCESS ? "ok" : "failed");
|
||||
return r;
|
||||
}
|
||||
@@ -423,12 +475,24 @@ static VkResult VKAPI_CALL CreateSwapchainKHR(VkDevice d, const VkSwapchainCreat
|
||||
VkSwapchainKHR *out) {
|
||||
PFN_vkCreateSwapchainKHR next = NEXT(d, CreateSwapchainKHR);
|
||||
VkSwapchainCreateInfoKHR sized = *ci;
|
||||
#ifdef DROIDDECK_WSI_CAPTURE
|
||||
const char *capture_mode = getenv("DROIDDECK_VR_PRESENT");
|
||||
const int capture = capture_mode && !strcmp(capture_mode, "questwsi")
|
||||
&& !strcmp(program_invocation_short_name, "vrcompositor") && ci->surface == g_hmd_surface;
|
||||
if (capture)
|
||||
sized.imageUsage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
|
||||
#endif
|
||||
mode_init();
|
||||
if (sized.imageExtent.width == UINT32_MAX || sized.imageExtent.height == UINT32_MAX)
|
||||
sized.imageExtent = (VkExtent2D){g_mode_w, g_mode_h};
|
||||
ci = &sized;
|
||||
VkResult r = next(d, ci, a, out);
|
||||
if (r == VK_SUCCESS) return r;
|
||||
if (r == VK_SUCCESS) {
|
||||
#ifdef DROIDDECK_WSI_CAPTURE
|
||||
if (capture) droiddeck_wsi_swapchain(d, *out, ci);
|
||||
#endif
|
||||
return r;
|
||||
}
|
||||
char line[256];
|
||||
int n = snprintf(line, sizeof(line), "format %d space %d mode %d images %u %ux%u usage 0x%x chain:", ci->imageFormat,
|
||||
ci->imageColorSpace, ci->presentMode, ci->minImageCount, ci->imageExtent.width, ci->imageExtent.height,
|
||||
@@ -444,16 +508,30 @@ static VkResult VKAPI_CALL CreateSwapchainKHR(VkDevice d, const VkSwapchainCreat
|
||||
if (r != VK_SUCCESS) {
|
||||
mine.imageFormat = VK_FORMAT_B8G8R8A8_UNORM;
|
||||
mine.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
|
||||
#ifdef DROIDDECK_WSI_CAPTURE
|
||||
if (capture) mine.imageUsage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
|
||||
#endif
|
||||
mine.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
||||
mine.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
|
||||
r = next(d, &mine, a, out);
|
||||
}
|
||||
say("headless swapchain: %s", r == VK_SUCCESS ? "ok" : "still refused");
|
||||
#ifdef DROIDDECK_WSI_CAPTURE
|
||||
if (r == VK_SUCCESS && capture) droiddeck_wsi_swapchain(d, *out, &mine);
|
||||
#endif
|
||||
return r;
|
||||
}
|
||||
|
||||
static void VKAPI_CALL DestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks *allocator) {
|
||||
#ifdef DROIDDECK_WSI_CAPTURE
|
||||
droiddeck_wsi_forget(device, swapchain);
|
||||
#endif
|
||||
NEXT(device, DestroySwapchainKHR)(device, swapchain, allocator);
|
||||
}
|
||||
|
||||
static PFN_vkVoidFunction display_device_hook(const char *name) {
|
||||
HOOK_AS("vkCreateSwapchainKHR", CreateSwapchainKHR)
|
||||
HOOK_AS("vkDestroySwapchainKHR", DestroySwapchainKHR)
|
||||
HOOK_AS("vkDisplayPowerControlEXT", DisplayPowerControlEXT)
|
||||
HOOK_AS("vkRegisterDeviceEventEXT", RegisterDeviceEventEXT)
|
||||
HOOK_AS("vkRegisterDisplayEventEXT", RegisterDisplayEventEXT)
|
||||
@@ -470,6 +548,112 @@ 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;
|
||||
static unsigned g_producer_idle_calls;
|
||||
struct producer_queue {
|
||||
VkQueue queue;
|
||||
VkDevice device;
|
||||
VkFence fence;
|
||||
};
|
||||
static struct producer_queue g_producer_queues[MAX_OBJS];
|
||||
static int g_producer_overflow;
|
||||
static int g_producer_started;
|
||||
static int64_t g_producer_retry_at;
|
||||
static struct droiddeck_producer_server g_producer_server;
|
||||
|
||||
__attribute__((destructor)) static void producer_process_exit(void) {
|
||||
if (g_producer_started && g_producer_server.path[0]) unlink(g_producer_server.path);
|
||||
}
|
||||
|
||||
static int producer_acquire(int64_t deadline) {
|
||||
while (pthread_mutex_trylock(&g_submission_lock) != 0) {
|
||||
if (droiddeck_producer_now() >= deadline) return 0;
|
||||
usleep(100);
|
||||
}
|
||||
int queues = 0;
|
||||
if (g_producer_overflow || g_producer_idle_calls) goto fail;
|
||||
for (unsigned index = 0; index < MAX_OBJS; index++) {
|
||||
struct producer_queue *entry = &g_producer_queues[index];
|
||||
if (!entry->queue) continue;
|
||||
queues++;
|
||||
struct dev *device = dev_for(entry->device);
|
||||
if (!device) goto fail;
|
||||
PFN_vkGetFenceStatus status = (PFN_vkGetFenceStatus)device->gdpa(device->handle, "vkGetFenceStatus");
|
||||
PFN_vkDestroyFence destroy = (PFN_vkDestroyFence)device->gdpa(device->handle, "vkDestroyFence");
|
||||
if (entry->fence) {
|
||||
if (status(device->handle, entry->fence) != VK_SUCCESS) goto fail;
|
||||
destroy(device->handle, entry->fence, NULL);
|
||||
entry->fence = VK_NULL_HANDLE;
|
||||
}
|
||||
VkFenceCreateInfo create = {VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, NULL, 0};
|
||||
PFN_vkCreateFence create_fence = (PFN_vkCreateFence)device->gdpa(device->handle, "vkCreateFence");
|
||||
if (create_fence(device->handle, &create, NULL, &entry->fence) != VK_SUCCESS) goto fail;
|
||||
PFN_vkQueueSubmit submit = (PFN_vkQueueSubmit)device->gdpa(device->handle, "vkQueueSubmit");
|
||||
if (submit(entry->queue, 0, NULL, entry->fence) != VK_SUCCESS) {
|
||||
destroy(device->handle, entry->fence, NULL);
|
||||
entry->fence = VK_NULL_HANDLE;
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
if (!queues) goto fail;
|
||||
for (unsigned index = 0; index < MAX_OBJS; index++) {
|
||||
struct producer_queue *entry = &g_producer_queues[index];
|
||||
if (!entry->queue) continue;
|
||||
int64_t remaining = deadline - droiddeck_producer_now();
|
||||
if (remaining <= 0) goto fail;
|
||||
struct dev *device = dev_for(entry->device);
|
||||
PFN_vkWaitForFences wait = (PFN_vkWaitForFences)device->gdpa(device->handle, "vkWaitForFences");
|
||||
if (wait(device->handle, 1, &entry->fence, VK_TRUE, (uint64_t)remaining) != VK_SUCCESS) goto fail;
|
||||
}
|
||||
return 1;
|
||||
fail:
|
||||
pthread_mutex_unlock(&g_submission_lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void producer_release(void) {
|
||||
pthread_mutex_unlock(&g_submission_lock);
|
||||
}
|
||||
|
||||
static void producer_quarantine(void) {
|
||||
say("%s", "producer lease lost before capture completion: submissions quarantined; restart SteamVR");
|
||||
}
|
||||
|
||||
static void producer_record(struct dev *device, VkQueue queue) {
|
||||
if (!g_producer_started && droiddeck_producer_now() >= g_producer_retry_at) {
|
||||
g_producer_retry_at = droiddeck_producer_now() + 1000000000LL;
|
||||
g_producer_server.acquire = producer_acquire;
|
||||
g_producer_server.release = producer_release;
|
||||
g_producer_server.quarantine = producer_quarantine;
|
||||
if (droiddeck_producer_start(&g_producer_server)) {
|
||||
pthread_detach(g_producer_server.thread);
|
||||
g_producer_started = 1;
|
||||
}
|
||||
}
|
||||
for (unsigned index = 0; index < MAX_OBJS; index++) {
|
||||
struct producer_queue *entry = &g_producer_queues[index];
|
||||
if (entry->queue == queue) return;
|
||||
if (!entry->queue) {
|
||||
*entry = (struct producer_queue){queue, device->handle, VK_NULL_HANDLE};
|
||||
return;
|
||||
}
|
||||
}
|
||||
g_producer_overflow = 1;
|
||||
}
|
||||
|
||||
static void producer_destroy_device(struct dev *device, const VkAllocationCallbacks *allocator) {
|
||||
(void)allocator;
|
||||
pthread_mutex_lock(&g_submission_lock);
|
||||
PFN_vkDestroyFence destroy = (PFN_vkDestroyFence)device->gdpa(device->handle, "vkDestroyFence");
|
||||
for (unsigned index = 0; index < MAX_OBJS; index++) {
|
||||
struct producer_queue *entry = &g_producer_queues[index];
|
||||
if (entry->device != device->handle) continue;
|
||||
if (entry->fence) destroy(device->handle, entry->fence, NULL);
|
||||
memset(entry, 0, sizeof(*entry));
|
||||
}
|
||||
pthread_mutex_unlock(&g_submission_lock);
|
||||
}
|
||||
|
||||
struct bridged {
|
||||
VkSemaphore sem;
|
||||
VkDevice dev;
|
||||
@@ -477,7 +661,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 +780,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 +798,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 +845,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 +858,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 +906,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 +923,73 @@ 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;
|
||||
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); }
|
||||
#ifdef DROIDDECK_WSI_CAPTURE
|
||||
VkResult result = droiddeck_wsi_present(device->handle, queue, info, next);
|
||||
#else
|
||||
VkResult result = next(queue, info);
|
||||
#endif
|
||||
if (device->producer) pthread_mutex_unlock(&g_submission_lock);
|
||||
return result;
|
||||
}
|
||||
|
||||
static VkResult VKAPI_CALL QueueBindSparse(VkQueue queue, uint32_t count, const VkBindSparseInfo *binds, VkFence fence) {
|
||||
struct dev *device = dev_for(queue);
|
||||
PFN_vkQueueBindSparse next = (PFN_vkQueueBindSparse)device->gdpa(device->handle, "vkQueueBindSparse");
|
||||
if (device->producer) { pthread_mutex_lock(&g_submission_lock); producer_record(device, queue); }
|
||||
VkResult result = next(queue, count, binds, fence);
|
||||
if (device->producer) pthread_mutex_unlock(&g_submission_lock);
|
||||
return result;
|
||||
}
|
||||
|
||||
static VkResult VKAPI_CALL QueueWaitIdle(VkQueue queue) {
|
||||
struct dev *device = dev_for(queue);
|
||||
PFN_vkQueueWaitIdle next = (PFN_vkQueueWaitIdle)device->gdpa(device->handle, "vkQueueWaitIdle");
|
||||
if (device->producer) {
|
||||
pthread_mutex_lock(&g_submission_lock);
|
||||
g_producer_idle_calls++;
|
||||
pthread_mutex_unlock(&g_submission_lock);
|
||||
}
|
||||
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 result = next(queue);
|
||||
if (device->producer) {
|
||||
pthread_mutex_lock(&g_submission_lock);
|
||||
g_producer_idle_calls--;
|
||||
pthread_mutex_unlock(&g_submission_lock);
|
||||
}
|
||||
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;
|
||||
return result;
|
||||
}
|
||||
|
||||
static VkResult VKAPI_CALL DeviceWaitIdle(VkDevice handle) {
|
||||
struct dev *device = dev_for(handle);
|
||||
PFN_vkDeviceWaitIdle next = (PFN_vkDeviceWaitIdle)device->gdpa(handle, "vkDeviceWaitIdle");
|
||||
if (device->producer) {
|
||||
pthread_mutex_lock(&g_submission_lock);
|
||||
g_producer_idle_calls++;
|
||||
pthread_mutex_unlock(&g_submission_lock);
|
||||
}
|
||||
VkResult result = next(handle);
|
||||
if (device->producer) {
|
||||
pthread_mutex_lock(&g_submission_lock);
|
||||
g_producer_idle_calls--;
|
||||
pthread_mutex_unlock(&g_submission_lock);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static VkResult VKAPI_CALL WaitSemaphores(VkDevice d, const VkSemaphoreWaitInfo *info, uint64_t timeout) {
|
||||
PFN_vkWaitSemaphores next = (PFN_vkWaitSemaphores)dev_for(d)->gdpa(d, "vkWaitSemaphores");
|
||||
if (!next) next = (PFN_vkWaitSemaphores)dev_for(d)->gdpa(d, "vkWaitSemaphoresKHR");
|
||||
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) {
|
||||
@@ -897,6 +999,30 @@ static VkResult VKAPI_CALL GetSemaphoreCounterValue(VkDevice d, VkSemaphore s, u
|
||||
}
|
||||
|
||||
static VkResult VKAPI_CALL CreateImage(VkDevice d, const VkImageCreateInfo *ci, const VkAllocationCallbacks *a, VkImage *out) {
|
||||
VkImageCreateInfo compatible = *ci;
|
||||
VkExternalMemoryImageCreateInfo shared;
|
||||
const VkBaseInStructure *first = ci->pNext;
|
||||
// SteamVR's OpenXR stereo arrays omit a format list, while DXVK adds one on import.
|
||||
// Leave single-layer OpenVR images alone: the application owns their export layout.
|
||||
if (dev_for(d)->quest_images && ci->arrayLayers > 1 && (ci->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) && ci->tiling == VK_IMAGE_TILING_OPTIMAL
|
||||
&& first && first->sType == VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO) {
|
||||
const VkBaseInStructure *second = first->pNext;
|
||||
if (second && second->sType == VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO
|
||||
&& ((const VkExternalMemoryImageCreateInfo *)second)->handleTypes == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT) {
|
||||
compatible.pNext = second; ci = &compatible;
|
||||
say("%s", "matching opaque shared image layout to SteamVR's unrestricted format list");
|
||||
}
|
||||
}
|
||||
if (dev_for(d)->quest_images && ci->arrayLayers > 1 && (ci->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) && ci->tiling == VK_IMAGE_TILING_OPTIMAL
|
||||
&& first && first->sType == VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO) {
|
||||
const VkExternalMemoryImageCreateInfo *external = (const void *)first;
|
||||
const VkBaseInStructure *second = external->pNext;
|
||||
if (external->handleTypes == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT && second
|
||||
&& second->sType == VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO) {
|
||||
shared = *external; shared.pNext = second->pNext; compatible.pNext = &shared; ci = &compatible;
|
||||
say("%s", "matching opaque shared image layout to SteamVR's unrestricted format list");
|
||||
}
|
||||
}
|
||||
VkResult r = NEXT(d, CreateImage)(d, ci, a, out);
|
||||
if (!sem_trace_on()) return r;
|
||||
const VkExternalMemoryImageCreateInfo *ext = NULL;
|
||||
@@ -907,11 +1033,13 @@ static VkResult VKAPI_CALL CreateImage(VkDevice d, const VkImageCreateInfo *ci,
|
||||
}
|
||||
if (!ext) return r;
|
||||
char line[256];
|
||||
int n = snprintf(line, sizeof(line), "%s image %ux%u format %d flags 0x%x usage 0x%x tiling %d handles 0x%x list",
|
||||
int n = snprintf(line, sizeof(line), "%s image %ux%u format %d flags 0x%x usage 0x%x tiling %d layers %u mips %u samples %u handles 0x%x list",
|
||||
program_invocation_short_name, ci->extent.width, ci->extent.height, ci->format, ci->flags, ci->usage,
|
||||
ci->tiling, ext->handleTypes);
|
||||
ci->tiling, ci->arrayLayers, ci->mipLevels, ci->samples, ext->handleTypes);
|
||||
for (uint32_t i = 0; list && i < list->viewFormatCount && n < (int)sizeof(line) - 8; i++)
|
||||
n += snprintf(line + n, sizeof(line) - n, " %d", list->pViewFormats[i]);
|
||||
for (const VkBaseInStructure *p = ci->pNext; p && n < (int)sizeof(line) - 16; p = p->pNext)
|
||||
n += snprintf(line + n, sizeof(line) - n, " chain:%d", p->sType);
|
||||
say("%s", line);
|
||||
return r;
|
||||
}
|
||||
@@ -925,6 +1053,10 @@ 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("vkQueueWaitIdle", QueueWaitIdle)
|
||||
HOOK_AS("vkDeviceWaitIdle", DeviceWaitIdle)
|
||||
HOOK_AS("vkWaitSemaphores", WaitSemaphores)
|
||||
HOOK_AS("vkWaitSemaphoresKHR", WaitSemaphores)
|
||||
HOOK_AS("vkGetSemaphoreCounterValue", GetSemaphoreCounterValue)
|
||||
|
||||
@@ -0,0 +1,404 @@
|
||||
#define VK_NO_PROTOTYPES
|
||||
#include "wsi_capture.h"
|
||||
#include "xr_tracking_file.h"
|
||||
#include "xr_frame_channel.h"
|
||||
#include "xr_gpu_channel.h"
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <condition_variable>
|
||||
#include <array>
|
||||
|
||||
namespace {
|
||||
#define FUNCTIONS(X) \
|
||||
X(GetSwapchainImagesKHR) X(CreateCommandPool) X(DestroyCommandPool) X(AllocateCommandBuffers) \
|
||||
X(ResetCommandBuffer) X(BeginCommandBuffer) X(EndCommandBuffer) X(CmdPipelineBarrier) \
|
||||
X(CmdCopyImageToBuffer) X(CreateBuffer) X(DestroyBuffer) X(GetBufferMemoryRequirements) \
|
||||
X(AllocateMemory) X(FreeMemory) X(BindBufferMemory) X(MapMemory) X(UnmapMemory) \
|
||||
X(InvalidateMappedMemoryRanges) X(CreateFence) X(DestroyFence) X(ResetFences) X(WaitForFences) X(QueueSubmit) \
|
||||
X(CreateSemaphore) X(DestroySemaphore) X(GetMemoryFdKHR) X(CreateImage) X(DestroyImage) \
|
||||
X(GetImageMemoryRequirements) X(BindImageMemory) X(CmdBlitImage)
|
||||
struct Device {
|
||||
VkDevice handle{};
|
||||
VkPhysicalDeviceMemoryProperties memory{};
|
||||
bool foreign{};
|
||||
PFN_vkGetMemoryFdPropertiesKHR GetMemoryFdPropertiesKHR{};
|
||||
std::unordered_map<VkQueue, uint32_t> queues;
|
||||
#define MEMBER(name) PFN_vk##name name{};
|
||||
FUNCTIONS(MEMBER)
|
||||
#undef MEMBER
|
||||
};
|
||||
struct CaptureSlot {
|
||||
Device *device{};
|
||||
VkCommandPool pool{};
|
||||
VkCommandBuffer command{};
|
||||
VkBuffer buffer{};
|
||||
VkDeviceMemory memory{};
|
||||
VkImage rgbaImage{};
|
||||
VkDeviceMemory rgbaMemory{};
|
||||
int sharedFd = -1;
|
||||
bool rgbaInitialized{};
|
||||
void *mapping{};
|
||||
VkFence fence{};
|
||||
bool busy{}, queued{};
|
||||
DroidDeckXrFrameHeader header{};
|
||||
~CaptureSlot() { Clear(); }
|
||||
void Clear() {
|
||||
if (!device) return;
|
||||
if (busy && fence) device->WaitForFences(device->handle, 1, &fence, VK_TRUE, UINT64_MAX);
|
||||
if (sharedFd >= 0) close(sharedFd);
|
||||
if (fence) device->DestroyFence(device->handle, fence, nullptr);
|
||||
if (pool) device->DestroyCommandPool(device->handle, pool, nullptr);
|
||||
if (buffer) device->DestroyBuffer(device->handle, buffer, nullptr);
|
||||
if (rgbaImage) device->DestroyImage(device->handle, rgbaImage, nullptr);
|
||||
if (rgbaMemory) device->FreeMemory(device->handle, rgbaMemory, nullptr);
|
||||
if (mapping) device->UnmapMemory(device->handle, memory);
|
||||
if (memory) device->FreeMemory(device->handle, memory, nullptr);
|
||||
device = nullptr; pool = VK_NULL_HANDLE; command = VK_NULL_HANDLE; buffer = VK_NULL_HANDLE;
|
||||
memory = VK_NULL_HANDLE; rgbaImage = VK_NULL_HANDLE; rgbaMemory = VK_NULL_HANDLE;
|
||||
sharedFd = -1; mapping = nullptr; fence = VK_NULL_HANDLE; busy = queued = rgbaInitialized = false;
|
||||
}
|
||||
bool Initialize(Device &d, VkExtent2D extent, VkFormat format, uint32_t family, int imported = -1, uint64_t size = 0) {
|
||||
device = &d;
|
||||
VkCommandPoolCreateInfo pc{VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO};
|
||||
pc.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; pc.queueFamilyIndex = family;
|
||||
if (d.CreateCommandPool(d.handle, &pc, nullptr, &pool)) return false;
|
||||
VkCommandBufferAllocateInfo ca{VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO};
|
||||
ca.commandPool = pool; ca.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; ca.commandBufferCount = 1;
|
||||
if (d.AllocateCommandBuffers(d.handle, &ca, &command)) return false;
|
||||
VkBufferCreateInfo bc{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
|
||||
VkExternalMemoryBufferCreateInfo external{VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO};
|
||||
external.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT; bc.pNext = &external;
|
||||
bc.size = uint64_t(extent.width) * extent.height * 4; bc.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
||||
if (d.CreateBuffer(d.handle, &bc, nullptr, &buffer)) return false;
|
||||
VkMemoryRequirements requirements{}; d.GetBufferMemoryRequirements(d.handle, buffer, &requirements);
|
||||
VkImportMemoryFdInfoKHR import{VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR};
|
||||
VkMemoryFdPropertiesKHR fdProperties{VK_STRUCTURE_TYPE_MEMORY_FD_PROPERTIES_KHR};
|
||||
if (imported >= 0) {
|
||||
if (!d.GetMemoryFdPropertiesKHR || size < requirements.size
|
||||
|| d.GetMemoryFdPropertiesKHR(d.handle, VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, imported, &fdProperties)) return false;
|
||||
requirements.memoryTypeBits &= fdProperties.memoryTypeBits;
|
||||
}
|
||||
uint32_t type = d.memory.memoryTypeCount;
|
||||
for (uint32_t i = 0; i < d.memory.memoryTypeCount; i++) {
|
||||
auto flags = d.memory.memoryTypes[i].propertyFlags;
|
||||
if (!(requirements.memoryTypeBits & (1u << i)) || (imported < 0 && !(flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT))) continue;
|
||||
type = i;
|
||||
if (flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) break;
|
||||
}
|
||||
if (type == d.memory.memoryTypeCount) return false;
|
||||
VkMemoryAllocateInfo alloc{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
|
||||
VkExportMemoryAllocateInfo exported{VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO};
|
||||
exported.handleTypes = external.handleTypes; alloc.pNext = &exported;
|
||||
alloc.allocationSize = requirements.size; alloc.memoryTypeIndex = type;
|
||||
if (imported >= 0) {
|
||||
import.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT; import.fd = dup(imported);
|
||||
if (import.fd < 0) return false;
|
||||
alloc.pNext = &import; alloc.allocationSize = size;
|
||||
}
|
||||
VkResult allocated = d.AllocateMemory(d.handle, &alloc, nullptr, &memory);
|
||||
if (allocated && import.fd >= 0 && imported >= 0) close(import.fd);
|
||||
if (allocated || d.BindBufferMemory(d.handle, buffer, memory, 0)) return false;
|
||||
if (imported < 0) {
|
||||
if (d.MapMemory(d.handle, memory, 0, VK_WHOLE_SIZE, 0, &mapping)) return false;
|
||||
VkMemoryGetFdInfoKHR getFd{VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR};
|
||||
getFd.memory = memory; getFd.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
|
||||
if (d.GetMemoryFdKHR(d.handle, &getFd, &sharedFd)) return false;
|
||||
}
|
||||
if (imported < 0) {
|
||||
VkImageCreateInfo image{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
|
||||
image.imageType = VK_IMAGE_TYPE_2D; image.extent = {extent.width, extent.height, 1};
|
||||
image.mipLevels = image.arrayLayers = 1; image.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||
image.format = (format == VK_FORMAT_R8G8B8A8_SRGB || format == VK_FORMAT_B8G8R8A8_SRGB)
|
||||
? VK_FORMAT_R8G8B8A8_SRGB : VK_FORMAT_R8G8B8A8_UNORM;
|
||||
image.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
|
||||
if (d.CreateImage(d.handle, &image, nullptr, &rgbaImage)) return false;
|
||||
d.GetImageMemoryRequirements(d.handle, rgbaImage, &requirements);
|
||||
type = 0;
|
||||
while (type < d.memory.memoryTypeCount && !(requirements.memoryTypeBits & (1u << type))) type++;
|
||||
if (type == d.memory.memoryTypeCount) return false;
|
||||
alloc.pNext = nullptr; alloc.allocationSize = requirements.size; alloc.memoryTypeIndex = type;
|
||||
if (d.AllocateMemory(d.handle, &alloc, nullptr, &rgbaMemory)
|
||||
|| d.BindImageMemory(d.handle, rgbaImage, rgbaMemory, 0)) return false;
|
||||
}
|
||||
VkFenceCreateInfo fc{VK_STRUCTURE_TYPE_FENCE_CREATE_INFO};
|
||||
if (d.CreateFence(d.handle, &fc, nullptr, &fence)) return false;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
struct Swapchain {
|
||||
explicit Swapchain(const char *trackingPath = "/run/droiddeck/" DROIDDECK_XR_FILE) : tracking(trackingPath) {}
|
||||
Device *device{};
|
||||
VkExtent2D extent{};
|
||||
VkFormat format{};
|
||||
std::vector<VkImage> images;
|
||||
std::vector<VkSemaphore> ready;
|
||||
std::array<CaptureSlot, 3> slots;
|
||||
uint64_t frame{};
|
||||
XrTrackingFile tracking;
|
||||
XrFrameSender sender;
|
||||
XrGpuSender gpuSender;
|
||||
bool gpu{};
|
||||
bool initialized{}, attempted{};
|
||||
uint32_t family{};
|
||||
std::mutex sendMutex;
|
||||
std::condition_variable sendReady;
|
||||
bool stopping{}, failed{};
|
||||
std::thread sendThread;
|
||||
~Swapchain() {
|
||||
{ std::lock_guard<std::mutex> lock(sendMutex); stopping = true; sendReady.notify_all(); }
|
||||
if (sendThread.joinable()) sendThread.join();
|
||||
for (auto semaphore : ready) if (semaphore) device->DestroySemaphore(device->handle, semaphore, nullptr);
|
||||
}
|
||||
void Run() noexcept {
|
||||
try { Loop(); }
|
||||
catch (...) {
|
||||
std::lock_guard<std::mutex> lock(sendMutex); failed = true;
|
||||
std::fprintf(stderr, "[droiddeck-vr-layer] WSI worker failed; restart SteamVR\n");
|
||||
}
|
||||
}
|
||||
void Loop() {
|
||||
for (;;) {
|
||||
CaptureSlot *slot;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(sendMutex);
|
||||
sendReady.wait(lock, [this] {
|
||||
return stopping || std::any_of(slots.begin(), slots.end(), [](const auto &item) { return item.queued; });
|
||||
});
|
||||
slot = nullptr;
|
||||
for (auto &item : slots) if (item.queued && (!slot || item.header.frame_id < slot->header.frame_id)) slot = &item;
|
||||
if (!slot) return;
|
||||
slot->queued = false;
|
||||
}
|
||||
auto &d = *device;
|
||||
const auto header = slot->header;
|
||||
auto &fence = slot->fence;
|
||||
auto &memory = slot->memory;
|
||||
auto sharedFd = slot->sharedFd;
|
||||
int64_t begin = XrTrackingFile::Now();
|
||||
VkResult result = d.WaitForFences(d.handle, 1, &fence, VK_TRUE, UINT64_MAX);
|
||||
int64_t waited = XrTrackingFile::Now();
|
||||
VkMappedMemoryRange range{VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE}; range.memory = memory; range.size = VK_WHOLE_SIZE;
|
||||
if (result == VK_SUCCESS && !gpu) result = d.InvalidateMappedMemoryRanges(d.handle, 1, &range);
|
||||
int64_t invalidated = XrTrackingFile::Now();
|
||||
if (result == VK_SUCCESS && XrTrackingFile::Now() - header.captured_ns < 200000000LL) {
|
||||
if (gpu) {
|
||||
uint32_t flags = (format == VK_FORMAT_B8G8R8A8_UNORM || format == VK_FORMAT_B8G8R8A8_SRGB) ? XR_GPU_BGRA : 0;
|
||||
if (!gpuSender.Send(header, static_cast<uint32_t>(slot - slots.data()), flags)) result = VK_ERROR_DEVICE_LOST;
|
||||
} else sender.SendShared("/run/droiddeck/" DROIDDECK_XR_FRAME_SOCKET, header, sharedFd);
|
||||
}
|
||||
if (header.frame_id % 120 == 0) std::fprintf(stderr, "[droiddeck-vr-layer] WSI ms wait=%.2f invalidate=%.2f shared=%.2f\n",
|
||||
(waited-begin)/1e6, (invalidated-waited)/1e6, (XrTrackingFile::Now()-invalidated)/1e6);
|
||||
std::lock_guard<std::mutex> lock(sendMutex);
|
||||
if (result != VK_SUCCESS || sender.SharedLeaseLost()) {
|
||||
std::fprintf(stderr, "[droiddeck-vr-layer] WSI buffer quarantined: result=%d leaseLost=%d; restart SteamVR\n", result, sender.SharedLeaseLost());
|
||||
failed = true; return;
|
||||
}
|
||||
slot->busy = false;
|
||||
}
|
||||
}
|
||||
bool Initialize(VkSwapchainKHR chain, VkQueue queue) {
|
||||
if (initialized) return true;
|
||||
if (attempted) return false;
|
||||
attempted = true;
|
||||
auto &d = *device;
|
||||
auto queueFamily = d.queues.find(queue);
|
||||
if (queueFamily == d.queues.end()) return false;
|
||||
family = queueFamily->second;
|
||||
uint32_t count = 0;
|
||||
if (d.GetSwapchainImagesKHR(d.handle, chain, &count, nullptr) || !count) return false;
|
||||
images.resize(count);
|
||||
if (d.GetSwapchainImagesKHR(d.handle, chain, &count, images.data())) return false;
|
||||
ready.resize(count);
|
||||
VkSemaphoreCreateInfo semaphoreInfo{VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO};
|
||||
for (auto &semaphore : ready) if (d.CreateSemaphore(d.handle, &semaphoreInfo, nullptr, &semaphore)) return false;
|
||||
XrGpuPoolInfo info;
|
||||
std::array<int, XR_GPU_SLOTS> fds{{-1, -1, -1}};
|
||||
gpu = d.foreign && gpuSender.Connect("/run/droiddeck/" DROIDDECK_XR_GPU_SOCKET, extent.width, extent.height, info, fds);
|
||||
if (gpu) {
|
||||
for (unsigned index = 0; index < slots.size(); index++)
|
||||
if (!slots[index].Initialize(d, extent, format, family, fds[index], info.sizes[index])) { gpu = false; break; }
|
||||
for (int fd : fds) if (fd >= 0) close(fd);
|
||||
}
|
||||
if (!gpu) {
|
||||
gpuSender.Close();
|
||||
for (auto &slot : slots) { slot.Clear(); if (!slot.Initialize(d, extent, format, family)) return false; }
|
||||
}
|
||||
sendThread = std::thread([this] { Run(); });
|
||||
initialized = true;
|
||||
std::fprintf(stderr, "[droiddeck-vr-layer] capturing compositor swapchain %ux%u format %d channel=%s\n", extent.width, extent.height, format, gpu ? "gpu" : "cpu");
|
||||
return true;
|
||||
}
|
||||
VkResult Fail(VkResult result, CaptureSlot &slot) {
|
||||
std::lock_guard<std::mutex> lock(sendMutex);
|
||||
failed = true; slot.busy = false;
|
||||
return result;
|
||||
}
|
||||
bool Header(DroidDeckXrFrameHeader &header) {
|
||||
DroidDeckXrTracking packet{};
|
||||
if (!tracking.Read(&packet) || !XrTrackingFile::Fresh(packet)) return false;
|
||||
header = {};
|
||||
header.magic = DROIDDECK_XR_FRAME_MAGIC; header.version = DROIDDECK_XR_FRAME_VERSION;
|
||||
header.width = extent.width; header.height = extent.height;
|
||||
header.bytes = extent.width * extent.height * 4;
|
||||
header.color_space = DROIDDECK_XR_COLOR_SRGB_RGBA8;
|
||||
header.frame_id = ++frame; header.captured_ns = XrTrackingFile::Now();
|
||||
for (int eye = 0; eye < 2; eye++) {
|
||||
const auto &pose = packet.eyes[eye].pose;
|
||||
const float x = pose.orientation[0], y = pose.orientation[1], z = pose.orientation[2], w = pose.orientation[3];
|
||||
float matrix[3][4] = {{1-2*(y*y+z*z),2*(x*y-w*z),2*(x*z+w*y),pose.position[0]},
|
||||
{2*(x*y+w*z),1-2*(x*x+z*z),2*(y*z-w*x),pose.position[1]},
|
||||
{2*(x*z-w*y),2*(y*z+w*x),1-2*(x*x+y*y),pose.position[2]}};
|
||||
std::memcpy(header.eye_pose[eye], matrix, sizeof(matrix));
|
||||
const auto *fov = packet.eyes[eye].fov;
|
||||
float left = std::tan(fov[0]), right = std::tan(fov[1]), up = std::tan(fov[2]), down = std::tan(fov[3]);
|
||||
auto &p = header.projection[eye];
|
||||
p[0][0] = 2/(right-left); p[0][2] = (right+left)/(right-left);
|
||||
p[1][1] = 2/(up-down); p[1][2] = (up+down)/(up-down);
|
||||
p[2][2] = -1; p[2][3] = -0.2f; p[3][2] = -1;
|
||||
}
|
||||
return xr_frame_valid(header);
|
||||
}
|
||||
VkResult Capture(VkQueue queue, uint32_t index, const VkPresentInfoKHR &present, VkSemaphore *completion) {
|
||||
auto &d = *device;
|
||||
if (index >= images.size()) return VK_ERROR_OUT_OF_DATE_KHR;
|
||||
CaptureSlot *selected = nullptr;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(sendMutex);
|
||||
if (failed) return VK_ERROR_DEVICE_LOST;
|
||||
for (auto &slot : slots) if (!slot.busy) { selected = &slot; slot.busy = true; break; }
|
||||
}
|
||||
if (!selected) return VK_SUCCESS;
|
||||
auto &slot = *selected;
|
||||
DroidDeckXrFrameHeader header{};
|
||||
if (!Header(header)) { std::lock_guard<std::mutex> lock(sendMutex); slot.busy = false; return VK_SUCCESS; }
|
||||
auto &command = slot.command;
|
||||
auto &buffer = slot.buffer;
|
||||
auto &fence = slot.fence;
|
||||
auto &rgbaImage = slot.rgbaImage;
|
||||
auto &rgbaInitialized = slot.rgbaInitialized;
|
||||
VkResult result = d.ResetCommandBuffer(command, 0);
|
||||
if (result) return Fail(result, slot);
|
||||
VkCommandBufferBeginInfo begin{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO};
|
||||
begin.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
|
||||
if ((result = d.BeginCommandBuffer(command, &begin))) return Fail(result, slot);
|
||||
VkImageMemoryBarrier image{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER};
|
||||
image.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT; image.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
|
||||
image.oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; image.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
|
||||
image.srcQueueFamilyIndex = image.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||||
image.image = images[index]; image.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
|
||||
d.CmdPipelineBarrier(command, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &image);
|
||||
VkBufferImageCopy copy{}; copy.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
|
||||
copy.imageExtent = {extent.width, extent.height, 1};
|
||||
if (!gpu) {
|
||||
VkImageMemoryBarrier rgba = image;
|
||||
rgba.image = rgbaImage; rgba.srcAccessMask = rgbaInitialized ? VK_ACCESS_TRANSFER_READ_BIT : 0;
|
||||
rgba.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
||||
rgba.oldLayout = rgbaInitialized ? VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL : VK_IMAGE_LAYOUT_UNDEFINED;
|
||||
rgba.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
||||
d.CmdPipelineBarrier(command, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &rgba);
|
||||
VkImageBlit blit{}; blit.srcSubresource = blit.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
|
||||
blit.srcOffsets[1] = blit.dstOffsets[1] = {static_cast<int32_t>(extent.width), static_cast<int32_t>(extent.height), 1};
|
||||
d.CmdBlitImage(command, images[index], VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, rgbaImage,
|
||||
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit, VK_FILTER_NEAREST);
|
||||
rgba.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; rgba.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
|
||||
rgba.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; rgba.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
|
||||
d.CmdPipelineBarrier(command, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &rgba);
|
||||
}
|
||||
VkBufferMemoryBarrier ownership{VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER};
|
||||
ownership.srcQueueFamilyIndex = VK_QUEUE_FAMILY_FOREIGN_EXT; ownership.dstQueueFamilyIndex = family;
|
||||
ownership.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; ownership.buffer = buffer; ownership.size = VK_WHOLE_SIZE;
|
||||
if (gpu) d.CmdPipelineBarrier(command, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
|
||||
0, 0, nullptr, 1, &ownership, 0, nullptr);
|
||||
d.CmdCopyImageToBuffer(command, gpu ? images[index] : rgbaImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, buffer, 1, ©);
|
||||
image.srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT; image.dstAccessMask = 0;
|
||||
image.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; image.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
||||
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 = buffer; host.size = VK_WHOLE_SIZE;
|
||||
if (gpu) {
|
||||
host.dstAccessMask = 0; host.srcQueueFamilyIndex = family; host.dstQueueFamilyIndex = VK_QUEUE_FAMILY_FOREIGN_EXT;
|
||||
}
|
||||
d.CmdPipelineBarrier(command, VK_PIPELINE_STAGE_TRANSFER_BIT, (gpu ? 0 : VK_PIPELINE_STAGE_HOST_BIT) | VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
|
||||
0, 0, nullptr, 1, &host, 1, &image);
|
||||
if ((result = d.EndCommandBuffer(command)) || (result = d.ResetFences(d.handle, 1, &fence))) return Fail(result, slot);
|
||||
std::vector<VkPipelineStageFlags> stages(present.waitSemaphoreCount, VK_PIPELINE_STAGE_TRANSFER_BIT);
|
||||
VkSubmitInfo submit{VK_STRUCTURE_TYPE_SUBMIT_INFO};
|
||||
submit.waitSemaphoreCount = present.waitSemaphoreCount; submit.pWaitSemaphores = present.pWaitSemaphores;
|
||||
submit.pWaitDstStageMask = stages.data(); submit.commandBufferCount = 1; submit.pCommandBuffers = &command;
|
||||
submit.signalSemaphoreCount = 1; submit.pSignalSemaphores = &ready[index];
|
||||
if ((result = d.QueueSubmit(queue, 1, &submit, fence))) return Fail(result, slot);
|
||||
rgbaInitialized = true;
|
||||
{ std::lock_guard<std::mutex> lock(sendMutex); slot.header = header; slot.queued = true; sendReady.notify_all(); }
|
||||
*completion = ready[index];
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
};
|
||||
std::mutex mutex;
|
||||
std::unordered_map<VkDevice, std::unique_ptr<Device>> devices;
|
||||
std::unordered_map<VkSwapchainKHR, std::unique_ptr<Swapchain>> swapchains;
|
||||
}
|
||||
|
||||
extern "C" void droiddeck_wsi_device(VkDevice device, VkPhysicalDevice physical, VkInstance instance,
|
||||
PFN_vkGetInstanceProcAddr gipa, PFN_vkGetDeviceProcAddr gdpa, const VkDeviceCreateInfo *create) {
|
||||
const char *mode = getenv("DROIDDECK_VR_PRESENT");
|
||||
if (!mode || strcmp(mode, "questwsi") || strcmp(program_invocation_short_name, "vrcompositor")) return;
|
||||
auto d = std::make_unique<Device>(); d->handle = device;
|
||||
d->GetMemoryFdPropertiesKHR = reinterpret_cast<PFN_vkGetMemoryFdPropertiesKHR>(gdpa(device, "vkGetMemoryFdPropertiesKHR"));
|
||||
for (uint32_t index = 0; index < create->enabledExtensionCount; index++)
|
||||
if (!strcmp(create->ppEnabledExtensionNames[index], "VK_EXT_queue_family_foreign")) d->foreign = true;
|
||||
#define LOAD(name) d->name = reinterpret_cast<PFN_vk##name>(gdpa(device, "vk" #name)); if (!d->name) return;
|
||||
FUNCTIONS(LOAD)
|
||||
#undef LOAD
|
||||
auto properties = reinterpret_cast<PFN_vkGetPhysicalDeviceMemoryProperties>(gipa(instance, "vkGetPhysicalDeviceMemoryProperties"));
|
||||
properties(physical, &d->memory);
|
||||
auto getQueue = reinterpret_cast<PFN_vkGetDeviceQueue>(gdpa(device, "vkGetDeviceQueue"));
|
||||
for (uint32_t index = 0; index < create->queueCreateInfoCount; index++) {
|
||||
const auto &info = create->pQueueCreateInfos[index];
|
||||
if (info.flags) continue;
|
||||
for (uint32_t number = 0; number < info.queueCount; number++) {
|
||||
VkQueue queue{}; getQueue(device, info.queueFamilyIndex, number, &queue);
|
||||
if (queue) d->queues[queue] = info.queueFamilyIndex;
|
||||
}
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(mutex); devices[device] = std::move(d);
|
||||
}
|
||||
extern "C" void droiddeck_wsi_destroy(VkDevice device) {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
for (auto it = swapchains.begin(); it != swapchains.end();)
|
||||
if (it->second->device->handle == device) it = swapchains.erase(it); else ++it;
|
||||
devices.erase(device);
|
||||
}
|
||||
extern "C" void droiddeck_wsi_swapchain(VkDevice device, VkSwapchainKHR chain, const VkSwapchainCreateInfoKHR *create) {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
auto it = devices.find(device); if (it == devices.end()) return;
|
||||
if (create->imageFormat != VK_FORMAT_R8G8B8A8_UNORM && create->imageFormat != VK_FORMAT_R8G8B8A8_SRGB &&
|
||||
create->imageFormat != VK_FORMAT_B8G8R8A8_UNORM && create->imageFormat != VK_FORMAT_B8G8R8A8_SRGB) return;
|
||||
auto chainState = std::make_unique<Swapchain>(); chainState->device = it->second.get();
|
||||
chainState->extent = create->imageExtent; chainState->format = create->imageFormat;
|
||||
swapchains[chain] = std::move(chainState);
|
||||
}
|
||||
extern "C" void droiddeck_wsi_forget(VkDevice device, VkSwapchainKHR chain) {
|
||||
(void)device; std::lock_guard<std::mutex> lock(mutex); swapchains.erase(chain);
|
||||
}
|
||||
extern "C" VkResult droiddeck_wsi_present(VkDevice device, VkQueue queue, const VkPresentInfoKHR *present, PFN_vkQueuePresentKHR next) try {
|
||||
(void)device; std::lock_guard<std::mutex> lock(mutex);
|
||||
if (present->swapchainCount != 1) return next(queue, present);
|
||||
auto it = swapchains.find(present->pSwapchains[0]);
|
||||
if (it == swapchains.end()) return next(queue, present);
|
||||
if (!it->second->Initialize(present->pSwapchains[0], queue)) return VK_ERROR_INITIALIZATION_FAILED;
|
||||
auto family = it->second->device->queues.find(queue);
|
||||
if (family == it->second->device->queues.end() || family->second != it->second->family)
|
||||
return VK_ERROR_INITIALIZATION_FAILED;
|
||||
VkSemaphore completion{};
|
||||
VkResult result = it->second->Capture(queue, present->pImageIndices[0], *present, &completion);
|
||||
if (result) return result;
|
||||
if (!completion) return next(queue, present);
|
||||
// Reacquiring this swapchain image also proves its previous present wait semaphore is reusable.
|
||||
VkPresentInfoKHR copy = *present; copy.waitSemaphoreCount = 1; copy.pWaitSemaphores = &completion;
|
||||
return next(queue, ©);
|
||||
} catch (...) {
|
||||
std::fprintf(stderr, "[droiddeck-vr-layer] WSI capture failed; restart SteamVR\n");
|
||||
return VK_ERROR_OUT_OF_HOST_MEMORY;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
#include <vulkan/vulkan.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
void droiddeck_wsi_device(VkDevice device, VkPhysicalDevice physical, VkInstance instance,
|
||||
PFN_vkGetInstanceProcAddr gipa, PFN_vkGetDeviceProcAddr gdpa,
|
||||
const VkDeviceCreateInfo *create);
|
||||
void droiddeck_wsi_destroy(VkDevice device);
|
||||
void droiddeck_wsi_swapchain(VkDevice device, VkSwapchainKHR swapchain, const VkSwapchainCreateInfoKHR *create);
|
||||
void droiddeck_wsi_forget(VkDevice device, VkSwapchainKHR swapchain);
|
||||
VkResult droiddeck_wsi_present(VkDevice device, VkQueue queue, const VkPresentInfoKHR *present,
|
||||
PFN_vkQueuePresentKHR next);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
#include "xr_tracking_file.h"
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
#include <locale>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
struct XrSeatedOrigin {
|
||||
double yaw, translation[3];
|
||||
};
|
||||
|
||||
class XrChaperoneOrigin {
|
||||
public:
|
||||
void Reset() { published_ = false; }
|
||||
template <typename Publish> void Update(const DroidDeckXrTracking &tracking, Publish publish) {
|
||||
if (!published_ && XrTrackingFile::Fresh(tracking) && XrTrackingFile::Tracked(tracking.head))
|
||||
published_ = publish(tracking.head);
|
||||
}
|
||||
private:
|
||||
bool published_ = false;
|
||||
};
|
||||
|
||||
inline XrSeatedOrigin xr_seated_origin(const DroidDeckXrPose &head) {
|
||||
const auto *q = head.orientation;
|
||||
double yaw = std::atan2(2 * (q[0] * q[2] + q[3] * q[1]), 1 - 2 * (q[0] * q[0] + q[1] * q[1]));
|
||||
double c = std::cos(yaw), s = std::sin(yaw);
|
||||
return {-yaw, {-c * head.position[0] + s * head.position[2], -head.position[1],
|
||||
-s * head.position[0] - c * head.position[2]}};
|
||||
}
|
||||
|
||||
inline std::string xr_chaperone_json(const DroidDeckXrPose &head, const char *timestamp) {
|
||||
auto seated = xr_seated_origin(head);
|
||||
std::ostringstream json; json.imbue(std::locale::classic()); json << std::setprecision(9);
|
||||
json << "{\"jsonid\":\"chaperone_info\",\"version\":5,\"time\":\"" << timestamp
|
||||
<< "\",\"universes\":[{\"universeID\":\"2\",\"standing\":{\"translation\":[0,0,0],\"yaw\":0},"
|
||||
<< "\"seated\":{\"translation\":[" << seated.translation[0] << ',' << seated.translation[1] << ',' << seated.translation[2]
|
||||
<< "],\"yaw\":" << seated.yaw << "},\"play_area\":[0,0],\"collision_bounds\":[]}]}";
|
||||
return json.str();
|
||||
}
|
||||
@@ -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,53 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <cmath>
|
||||
|
||||
#define DROIDDECK_XR_FRAME_MAGIC 0x46584444u
|
||||
#define DROIDDECK_XR_FRAME_VERSION 1u
|
||||
#define DROIDDECK_XR_FRAME_SHARED_VERSION 2u
|
||||
#define DROIDDECK_XR_FRAME_SOCKET "droiddeck-xr-frames"
|
||||
#define DROIDDECK_XR_FRAME_MAX_BYTES (64u * 1024u * 1024u)
|
||||
#define DROIDDECK_XR_COLOR_LINEAR_RGBA8 1u
|
||||
#define DROIDDECK_XR_COLOR_SRGB_RGBA8 2u
|
||||
|
||||
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.version != DROIDDECK_XR_FRAME_SHARED_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 != DROIDDECK_XR_COLOR_LINEAR_RGBA8
|
||||
&& header.color_space != DROIDDECK_XR_COLOR_SRGB_RGBA8) || 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,289 @@
|
||||
#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/mman.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <linux/dma-buf.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 XrFrameDescriptor {
|
||||
int fd = -1;
|
||||
~XrFrameDescriptor() { if (fd >= 0) close(fd); }
|
||||
XrFrameDescriptor() = default;
|
||||
XrFrameDescriptor(const XrFrameDescriptor &) = delete;
|
||||
XrFrameDescriptor &operator=(const XrFrameDescriptor &) = delete;
|
||||
};
|
||||
|
||||
inline bool xr_frame_header(int socket, DroidDeckXrFrameHeader &header, XrFrameDescriptor &shared, int64_t deadline) {
|
||||
for (;;) {
|
||||
int64_t remaining = deadline - xr_frame_now();
|
||||
if (remaining <= 0) return false;
|
||||
pollfd ready{socket, POLLIN, 0};
|
||||
int result = poll(&ready, 1, (int)((remaining + 999999) / 1000000));
|
||||
if (result < 0 && errno == EINTR) continue;
|
||||
if (result <= 0 || !(ready.revents & POLLIN)) return false;
|
||||
alignas(cmsghdr) unsigned char control[CMSG_SPACE(8 * sizeof(int))]{};
|
||||
iovec data{&header, sizeof(header)};
|
||||
msghdr message{}; message.msg_iov = &data; message.msg_iovlen = 1;
|
||||
message.msg_control = control; message.msg_controllen = sizeof(control);
|
||||
ssize_t bytes = recvmsg(socket, &message, MSG_DONTWAIT | MSG_CMSG_CLOEXEC);
|
||||
if (bytes < 0 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)) continue;
|
||||
if (bytes <= 0) return false;
|
||||
unsigned count = 0;
|
||||
for (auto *item = CMSG_FIRSTHDR(&message); item; item = CMSG_NXTHDR(&message, item)) {
|
||||
if (item->cmsg_level != SOL_SOCKET || item->cmsg_type != SCM_RIGHTS) continue;
|
||||
size_t descriptors = (item->cmsg_len - CMSG_LEN(0)) / sizeof(int);
|
||||
auto *fds = reinterpret_cast<int *>(CMSG_DATA(item));
|
||||
for (size_t index = 0; index < descriptors; index++) {
|
||||
if (++count == 1) shared.fd = fds[index]; else close(fds[index]);
|
||||
}
|
||||
}
|
||||
if ((message.msg_flags & (MSG_CTRUNC | MSG_TRUNC)) || count > 1) return false;
|
||||
return xr_frame_io(socket, reinterpret_cast<unsigned char *>(&header) + bytes, sizeof(header) - bytes, false, deadline);
|
||||
}
|
||||
}
|
||||
|
||||
inline bool xr_frame_shared_pixels(int descriptor, unsigned char *pixels, size_t bytes) {
|
||||
struct stat status{};
|
||||
if (fstat(descriptor, &status) != 0 || status.st_size < static_cast<int64_t>(bytes)) return false;
|
||||
dma_buf_sync sync{DMA_BUF_SYNC_START | DMA_BUF_SYNC_READ};
|
||||
if (ioctl(descriptor, DMA_BUF_IOCTL_SYNC, &sync) != 0) return false;
|
||||
void *mapping = mmap(nullptr, bytes, PROT_READ, MAP_SHARED, descriptor, 0);
|
||||
bool success = mapping != MAP_FAILED;
|
||||
if (success) { std::memcpy(pixels, mapping, bytes); munmap(mapping, bytes); }
|
||||
sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ;
|
||||
return ioctl(descriptor, DMA_BUF_IOCTL_SYNC, &sync) == 0 && success;
|
||||
}
|
||||
|
||||
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;
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
pending_ = {}; recycled_.clear();
|
||||
}
|
||||
bool Take(DroidDeckXrPixels *out) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (pending_.pixels.empty()) return false;
|
||||
*out = std::move(pending_);
|
||||
pending_ = {};
|
||||
return true;
|
||||
}
|
||||
void Recycle(std::vector<unsigned char> pixels) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (recycled_.size() < 3) recycled_.push_back(std::move(pixels));
|
||||
}
|
||||
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_;
|
||||
std::vector<std::vector<unsigned char>> recycled_;
|
||||
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_) {
|
||||
pollfd idle{client, POLLIN, 0};
|
||||
int available;
|
||||
do {
|
||||
available = poll(&idle, 1, 50);
|
||||
} while (!stopping_ && (available == 0 || (available < 0 && errno == EINTR)));
|
||||
if (stopping_ || available <= 0 || !(idle.revents & POLLIN)) break;
|
||||
DroidDeckXrPixels frame;
|
||||
XrFrameDescriptor shared;
|
||||
int64_t deadline = xr_frame_now() + 200000000LL;
|
||||
if (!xr_frame_header(client, frame.header, shared, 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
|
||||
|| (frame.header.version == DROIDDECK_XR_FRAME_SHARED_VERSION) != (shared.fd >= 0)) { rejected_++; break; }
|
||||
try {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (!recycled_.empty()) {
|
||||
frame.pixels = std::move(recycled_.back()); recycled_.pop_back();
|
||||
}
|
||||
}
|
||||
frame.pixels.resize(frame.header.bytes);
|
||||
}
|
||||
catch (...) { rejected_++; break; }
|
||||
deadline = xr_frame_now() + 200000000LL;
|
||||
bool copied = shared.fd >= 0 ? xr_frame_shared_pixels(shared.fd, frame.pixels.data(), frame.pixels.size())
|
||||
: xr_frame_io(client, frame.pixels.data(), frame.pixels.size(), false, deadline);
|
||||
if (!copied) { 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_);
|
||||
if (!pending_.pixels.empty() && recycled_.size() < 3) recycled_.push_back(std::move(pending_.pixels));
|
||||
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) {
|
||||
return SendFrame(filename, header, pixels, -1);
|
||||
}
|
||||
bool SendShared(const std::string &filename, DroidDeckXrFrameHeader header, int shared) {
|
||||
if (shared < 0 || sharedLeaseLost_) return false;
|
||||
header.version = DROIDDECK_XR_FRAME_SHARED_VERSION;
|
||||
return SendFrame(filename, header, nullptr, shared);
|
||||
}
|
||||
bool SharedLeaseLost() const { return sharedLeaseLost_; }
|
||||
private:
|
||||
int descriptor_ = -1;
|
||||
bool sharedLeaseLost_ = false;
|
||||
std::string filename_;
|
||||
bool SendFrame(const std::string &filename, const DroidDeckXrFrameHeader &header, const unsigned char *pixels, int shared) {
|
||||
if ((!pixels && shared < 0) || !xr_frame_valid(header) || filename.empty()
|
||||
|| filename.size() >= sizeof(sockaddr_un::sun_path)
|
||||
|| (header.version == DROIDDECK_XR_FRAME_SHARED_VERSION) != (shared >= 0)) 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;
|
||||
bool sent = false;
|
||||
if (shared >= 0) {
|
||||
iovec data{const_cast<DroidDeckXrFrameHeader *>(&header), sizeof(header)};
|
||||
alignas(cmsghdr) unsigned char control[CMSG_SPACE(sizeof(int))]{};
|
||||
msghdr message{}; message.msg_iov = &data; message.msg_iovlen = 1;
|
||||
message.msg_control = control; message.msg_controllen = sizeof(control);
|
||||
auto *item = CMSG_FIRSTHDR(&message); item->cmsg_level = SOL_SOCKET; item->cmsg_type = SCM_RIGHTS;
|
||||
item->cmsg_len = CMSG_LEN(sizeof(int)); std::memcpy(CMSG_DATA(item), &shared, sizeof(shared));
|
||||
ssize_t bytes;
|
||||
do { bytes = sendmsg(descriptor_, &message, MSG_NOSIGNAL | MSG_DONTWAIT); } while (bytes < 0 && errno == EINTR);
|
||||
if (bytes > 0) {
|
||||
sharedLeaseLost_ = true;
|
||||
sent = xr_frame_io(descriptor_, reinterpret_cast<unsigned char *>(data.iov_base) + bytes,
|
||||
sizeof(header) - bytes, true, deadline);
|
||||
}
|
||||
} else sent = 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);
|
||||
if (!sent
|
||||
|| !xr_frame_io(descriptor_, &acknowledgement, sizeof(acknowledgement), false, deadline)
|
||||
|| acknowledgement != header.frame_id) { Disconnect(); return false; }
|
||||
sharedLeaseLost_ = false;
|
||||
return true;
|
||||
}
|
||||
void Disconnect() { if (descriptor_ >= 0) close(descriptor_); descriptor_ = -1; filename_.clear(); }
|
||||
};
|
||||
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
#include "xr_frame.h"
|
||||
|
||||
struct DroidDeckXrRenderView {
|
||||
float orientation[4];
|
||||
float position[3];
|
||||
float fov[4];
|
||||
};
|
||||
|
||||
inline void xr_frame_projection_raw(const float fov[4], float *left, float *right, float *top, float *bottom) {
|
||||
*left = std::tan(fov[0]); *right = std::tan(fov[1]);
|
||||
// OpenVR's top/bottom parameters are the lower/upper Y bounds in its projection formula.
|
||||
*top = std::tan(fov[3]); *bottom = std::tan(fov[2]);
|
||||
}
|
||||
|
||||
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,118 @@
|
||||
#pragma once
|
||||
#include "xr_frame_channel.h"
|
||||
#include <array>
|
||||
|
||||
#define DROIDDECK_XR_GPU_SOCKET "droiddeck-xr-gpu"
|
||||
constexpr uint32_t XR_GPU_MAGIC = 0x47584444u;
|
||||
constexpr uint32_t XR_GPU_VERSION = 1;
|
||||
constexpr unsigned XR_GPU_SLOTS = 3;
|
||||
constexpr uint32_t XR_GPU_BGRA = 1;
|
||||
|
||||
struct XrGpuRequest { uint32_t magic = XR_GPU_MAGIC, version = XR_GPU_VERSION, width{}, height{}; };
|
||||
struct XrGpuPoolInfo {
|
||||
uint32_t magic = XR_GPU_MAGIC, version = XR_GPU_VERSION, width{}, height{};
|
||||
uint64_t sizes[XR_GPU_SLOTS]{};
|
||||
};
|
||||
struct XrGpuFrame { DroidDeckXrFrameHeader header{}; uint32_t slot{}, flags{}; };
|
||||
static_assert(sizeof(XrGpuRequest) == 16 && sizeof(XrGpuPoolInfo) == 40 && sizeof(XrGpuFrame) == 272,
|
||||
"XR GPU protocol layout");
|
||||
|
||||
inline bool xr_gpu_dimensions(uint32_t width, uint32_t height) {
|
||||
return width >= 2 && !(width & 1) && height && width <= 8192 && height <= 4096
|
||||
&& uint64_t(width) * height * 4 <= DROIDDECK_XR_FRAME_MAX_BYTES;
|
||||
}
|
||||
|
||||
enum class XrGpuFrameState { Invalid, Stale, Ready };
|
||||
inline XrGpuFrameState xr_gpu_frame_state(const XrGpuFrame &frame, uint32_t width, uint32_t height,
|
||||
uint64_t previous, int64_t now) {
|
||||
if (frame.slot >= XR_GPU_SLOTS || (frame.flags & ~XR_GPU_BGRA) || !xr_frame_valid(frame.header)
|
||||
|| frame.header.width != width || frame.header.height != height || frame.header.frame_id <= previous
|
||||
|| frame.header.captured_ns > now + 100000000LL) return XrGpuFrameState::Invalid;
|
||||
return now - frame.header.captured_ns > 200000000LL ? XrGpuFrameState::Stale : XrGpuFrameState::Ready;
|
||||
}
|
||||
|
||||
// SEQPACKET keeps descriptor transfer and its metadata in one indivisible message.
|
||||
inline bool xr_gpu_send(int socket, const void *data, size_t size, const int *fds = nullptr, unsigned count = 0) {
|
||||
if (count > XR_GPU_SLOTS) return false;
|
||||
iovec bytes{const_cast<void *>(data), size};
|
||||
alignas(cmsghdr) unsigned char control[CMSG_SPACE(XR_GPU_SLOTS * sizeof(int))]{};
|
||||
msghdr message{}; message.msg_iov = &bytes; message.msg_iovlen = 1;
|
||||
if (count) {
|
||||
message.msg_control = control; message.msg_controllen = CMSG_SPACE(count * sizeof(int));
|
||||
auto *item = CMSG_FIRSTHDR(&message); item->cmsg_level = SOL_SOCKET; item->cmsg_type = SCM_RIGHTS;
|
||||
item->cmsg_len = CMSG_LEN(count * sizeof(int)); std::memcpy(CMSG_DATA(item), fds, count * sizeof(int));
|
||||
}
|
||||
ssize_t result;
|
||||
do { result = sendmsg(socket, &message, MSG_NOSIGNAL | MSG_DONTWAIT); } while (result < 0 && errno == EINTR);
|
||||
return result == static_cast<ssize_t>(size);
|
||||
}
|
||||
|
||||
inline bool xr_gpu_receive(int socket, void *data, size_t size, int *fds = nullptr, unsigned count = 0) {
|
||||
if (count > XR_GPU_SLOTS) return false;
|
||||
iovec bytes{data, size};
|
||||
alignas(cmsghdr) unsigned char control[CMSG_SPACE(16 * sizeof(int))]{};
|
||||
msghdr message{}; message.msg_iov = &bytes; message.msg_iovlen = 1;
|
||||
message.msg_control = control; message.msg_controllen = sizeof(control);
|
||||
ssize_t result;
|
||||
do { result = recvmsg(socket, &message, MSG_DONTWAIT | MSG_CMSG_CLOEXEC); } while (result < 0 && errno == EINTR);
|
||||
unsigned received = 0;
|
||||
std::array<int, XR_GPU_SLOTS> owned{{-1, -1, -1}};
|
||||
for (auto *item = CMSG_FIRSTHDR(&message); result >= 0 && item; item = CMSG_NXTHDR(&message, item)) {
|
||||
if (item->cmsg_level != SOL_SOCKET || item->cmsg_type != SCM_RIGHTS) continue;
|
||||
auto *handles = reinterpret_cast<int *>(CMSG_DATA(item));
|
||||
for (unsigned index = 0; index < (item->cmsg_len - CMSG_LEN(0)) / sizeof(int); index++) {
|
||||
if (received < owned.size()) owned[received] = handles[index]; else close(handles[index]);
|
||||
received++;
|
||||
}
|
||||
}
|
||||
bool valid = result == static_cast<ssize_t>(size) && !(message.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) && received == count;
|
||||
for (unsigned index = 0; index < owned.size(); index++) {
|
||||
if (valid && index < count) fds[index] = owned[index];
|
||||
else if (owned[index] >= 0) close(owned[index]);
|
||||
}
|
||||
return valid;
|
||||
}
|
||||
|
||||
inline bool xr_gpu_wait(int socket, int timeout) {
|
||||
pollfd ready{socket, POLLIN, 0};
|
||||
int result;
|
||||
do { result = poll(&ready, 1, timeout); } while (result < 0 && errno == EINTR);
|
||||
return result > 0 && (ready.revents & POLLIN);
|
||||
}
|
||||
|
||||
class XrGpuSender {
|
||||
public:
|
||||
~XrGpuSender() { Close(); }
|
||||
bool Connect(const char *filename, uint32_t width, uint32_t height, XrGpuPoolInfo &pool,
|
||||
std::array<int, XR_GPU_SLOTS> &fds) {
|
||||
Close();
|
||||
if (!xr_gpu_dimensions(width, height) || !filename || strlen(filename) >= sizeof(sockaddr_un::sun_path)) return false;
|
||||
socket_ = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
|
||||
if (socket_ < 0) return false;
|
||||
sockaddr_un address{}; address.sun_family = AF_UNIX; std::strcpy(address.sun_path, filename);
|
||||
if (connect(socket_, reinterpret_cast<sockaddr *>(&address), sizeof(address)) || !xr_frame_peer(socket_)) { Close(); return false; }
|
||||
XrGpuRequest request; request.width = width; request.height = height;
|
||||
if (!xr_gpu_send(socket_, &request, sizeof(request)) || !xr_gpu_wait(socket_, 2000)
|
||||
|| !xr_gpu_receive(socket_, &pool, sizeof(pool), fds.data(), fds.size())) { Close(); return false; }
|
||||
bool valid = pool.magic == XR_GPU_MAGIC && pool.version == XR_GPU_VERSION && pool.width == width && pool.height == height;
|
||||
const uint64_t bytes = uint64_t(width) * height * 4;
|
||||
for (unsigned index = 0; index < fds.size(); index++) {
|
||||
struct stat status{};
|
||||
if (fstat(fds[index], &status) || pool.sizes[index] < bytes || pool.sizes[index] > bytes + 65536
|
||||
|| status.st_size != static_cast<int64_t>(pool.sizes[index])) valid = false;
|
||||
}
|
||||
if (!valid) { for (int &fd : fds) { close(fd); fd = -1; } Close(); }
|
||||
return valid;
|
||||
}
|
||||
bool Send(const DroidDeckXrFrameHeader &header, uint32_t slot, uint32_t flags = 0) {
|
||||
if (socket_ < 0 || slot >= XR_GPU_SLOTS || (flags & ~XR_GPU_BGRA) || !xr_frame_valid(header)) return false;
|
||||
XrGpuFrame frame; frame.header = header; frame.slot = slot; frame.flags = flags;
|
||||
uint64_t ack{};
|
||||
if (!xr_gpu_send(socket_, &frame, sizeof(frame)) || !xr_gpu_wait(socket_, 5000)
|
||||
|| !xr_gpu_receive(socket_, &ack, sizeof(ack)) || ack != header.frame_id) { Close(); return false; }
|
||||
return true;
|
||||
}
|
||||
void Close() { if (socket_ >= 0) close(socket_); socket_ = -1; }
|
||||
private:
|
||||
int socket_ = -1;
|
||||
};
|
||||
@@ -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, systemClick = 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.systemClick = !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};
|
||||
};
|
||||
Reference in new issue
Block a user