diff --git a/server/driver/view_list.h b/server/driver/view_list.h index 4e35e584..82c5ee22 100644 --- a/server/driver/view_list.h +++ b/server/driver/view_list.h @@ -20,7 +20,7 @@ #pragma once #include "pose_list.h" -#include "wivrn_session.h" +#include "wivrn_connection.h" struct tracked_views { diff --git a/server/driver/wivrn_controller.cpp b/server/driver/wivrn_controller.cpp index da5efb40..24e0996b 100644 --- a/server/driver/wivrn_controller.cpp +++ b/server/driver/wivrn_controller.cpp @@ -18,6 +18,7 @@ */ #include "wivrn_controller.h" +#include "wivrn_session.h" #include "util/u_logging.h" #include @@ -150,7 +151,7 @@ static void wivrn_controller_update_inputs(xrt_device * xdev); wivrn_controller::wivrn_controller(int hand_id, xrt_device * hmd, - std::shared_ptr cnx) : + xrt::drivers::wivrn::wivrn_session * cnx) : xrt_device{}, grip(hand_id == 0 ? device_id::LEFT_GRIP : device_id::RIGHT_GRIP), aim(hand_id == 0 ? device_id::LEFT_AIM : device_id::RIGHT_AIM), @@ -388,7 +389,7 @@ void wivrn_controller::set_output(xrt_output_name name, const xrt_output_value * static void wivrn_controller_destroy(xrt_device * xdev) { - delete (wivrn_controller *)xdev; + static_cast(xdev)->unregister(); } static void wivrn_controller_update_inputs(xrt_device * xdev) diff --git a/server/driver/wivrn_controller.h b/server/driver/wivrn_controller.h index 76301612..40c4c221 100644 --- a/server/driver/wivrn_controller.h +++ b/server/driver/wivrn_controller.h @@ -23,12 +23,17 @@ #include "hand_joints_list.h" #include "pose_list.h" -#include "wivrn_session.h" +#include "wivrn_connection.h" #include #include #include +namespace xrt::drivers::wivrn +{ +class wivrn_session; +} + class wivrn_controller : public xrt_device { std::mutex mutex; @@ -41,10 +46,10 @@ class wivrn_controller : public xrt_device std::vector inputs_array; xrt_output haptic_output; - std::shared_ptr cnx; + xrt::drivers::wivrn::wivrn_session * cnx; public: - wivrn_controller(int hand_id, xrt_device * hmd, std::shared_ptr cnx); + wivrn_controller(int hand_id, xrt_device * hmd, xrt::drivers::wivrn::wivrn_session * cnx); void unregister() { diff --git a/server/driver/wivrn_hmd.cpp b/server/driver/wivrn_hmd.cpp index e3cd351a..0fdee45e 100644 --- a/server/driver/wivrn_hmd.cpp +++ b/server/driver/wivrn_hmd.cpp @@ -18,6 +18,7 @@ */ #include "wivrn_hmd.h" +#include "wivrn_session.h" #include "xrt/xrt_defines.h" #include "xrt/xrt_device.h" @@ -196,7 +197,7 @@ bool wivrn_hmd::wivrn_hmd_compute_distortion(xrt_device * xdev, uint32_t view_in return true; } -wivrn_hmd::wivrn_hmd(std::shared_ptr cnx, +wivrn_hmd::wivrn_hmd(xrt::drivers::wivrn::wivrn_session * cnx, const from_headset::headset_info_packet & info) : xrt_device{}, cnx(cnx) { diff --git a/server/driver/wivrn_hmd.h b/server/driver/wivrn_hmd.h index 0f05d14b..58f62386 100644 --- a/server/driver/wivrn_hmd.h +++ b/server/driver/wivrn_hmd.h @@ -23,7 +23,7 @@ #include "xrt/xrt_tracking.h" #include "view_list.h" -#include "wivrn_session.h" +#include "wivrn_connection.h" #include #include @@ -32,6 +32,11 @@ struct comp_target; +namespace xrt::drivers::wivrn +{ +class wivrn_session; +} + class wivrn_hmd : public xrt_device { std::mutex mutex; @@ -50,12 +55,12 @@ class wivrn_hmd : public xrt_device std::array foveation_parameters{}; from_headset::battery battery{}; - std::shared_ptr cnx; + xrt::drivers::wivrn::wivrn_session * cnx; static bool wivrn_hmd_compute_distortion(xrt_device * xdev, uint32_t view_index, float u, float v, xrt_uv_triplet * result); public: - wivrn_hmd(std::shared_ptr cnx, + wivrn_hmd(xrt::drivers::wivrn::wivrn_session * cnx, const from_headset::headset_info_packet & info); void unregister() { diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index 0cd29e72..5924378a 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -34,11 +34,9 @@ #include "audio/audio_setup.h" #include "wivrn_comp_target.h" #include "wivrn_config.h" -#include "wivrn_controller.h" #include "wivrn_eye_tracker.h" #include "wivrn_fb_face2_tracker.h" #include "wivrn_foveation.h" -#include "wivrn_hmd.h" #include "wivrn_ipc.h" #include "xrt/xrt_session.h" @@ -113,7 +111,19 @@ void xrt::drivers::wivrn::tracking_control_t::set_enabled(to_headset::tracking_c } xrt::drivers::wivrn::wivrn_session::wivrn_session(xrt::drivers::wivrn::TCP && tcp, u_system & system) : - connection(std::move(tcp)), xrt_system(system) + connection(std::move(tcp)), + info([](wivrn_connection & connection) { + std::optional control; + while (not(control = connection.poll_control(-1))) + { + // FIXME: timeout + } + return std::get(*control); + }(connection)), + xrt_system(system), + hmd(this, info), + left_hand(0, &hmd, this), + right_hand(1, &hmd, this) { } @@ -124,14 +134,9 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi xrt_system_compositor ** out_xsysc) { std::shared_ptr self; - std::optional control; try { self = std::shared_ptr(new wivrn_session(std::move(tcp), system)); - while (not(control = self->connection.poll_control(-1))) - { - // FIXME: timeout - } } catch (std::exception & e) { @@ -139,7 +144,6 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi return XRT_ERROR_DEVICE_CREATION_FAILED; } - self->info = std::get(*control); send_to_main(self->info); try @@ -159,35 +163,24 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi U_LOG_E("Failed to register audio device: %s", e.what()); return XRT_ERROR_DEVICE_CREATION_FAILED; } - self->hmd = std::make_unique(self, self->info); - self->left_hand = std::make_unique(0, self->hmd.get(), self); - self->right_hand = std::make_unique(1, self->hmd.get(), self); auto * usysds = u_system_devices_static_allocate(); *out_xsysd = &usysds->base.base; auto & devices = *out_xsysd; int n = 0; - if (self->hmd) - usysds->base.base.static_roles.head = devices->xdevs[n++] = self->hmd.get(); - if (self->hmd->face_tracking_supported) - usysds->base.base.static_roles.face = self->hmd.get(); + usysds->base.base.static_roles.head = devices->xdevs[n++] = &self->hmd; - xrt_device * active_left_hand = nullptr; - xrt_device * active_right_hand = nullptr; + if (self->hmd.face_tracking_supported) + usysds->base.base.static_roles.face = &self->hmd; - if (self->left_hand) - { - devices->xdevs[n++] = self->left_hand.get(); - active_left_hand = self->left_hand.get(); - usysds->base.base.static_roles.hand_tracking.left = self->left_hand.get(); - } - if (self->right_hand) - { - devices->xdevs[n++] = self->right_hand.get(); - active_right_hand = self->right_hand.get(); - usysds->base.base.static_roles.hand_tracking.right = self->right_hand.get(); - } + devices->xdevs[n++] = &self->left_hand; + xrt_device * active_left_hand = &self->left_hand; + usysds->base.base.static_roles.hand_tracking.left = &self->left_hand; + + devices->xdevs[n++] = &self->right_hand; + xrt_device * active_right_hand = &self->right_hand; + usysds->base.base.static_roles.hand_tracking.right = &self->right_hand; #if WIVRN_FEATURE_STEAMVR_LIGHTHOUSE auto use_steamvr_lh = std::getenv("WIVRN_USE_STEAMVR_LH"); @@ -218,14 +211,14 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi if (self->info.eye_gaze) { - self->eye_tracker = std::make_unique(self->hmd.get(), self); + self->eye_tracker = std::make_unique(&self->hmd, self); self->foveation = std::make_unique(); usysds->base.base.static_roles.eyes = self->eye_tracker.get(); devices->xdevs[n++] = self->eye_tracker.get(); } if (self->info.face_tracking2_fb) { - self->fb_face2_tracker = std::make_unique(self->hmd.get(), self); + self->fb_face2_tracker = std::make_unique(&self->hmd, self); usysds->base.base.static_roles.face = self->fb_face2_tracker.get(); devices->xdevs[n++] = self->fb_face2_tracker.get(); } @@ -233,7 +226,7 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi #if WIVRN_FEATURE_SOLARXR xrt_device * solar_devs[XRT_SYSTEM_MAX_DEVICES]; uint32_t solar_devs_cap = XRT_SYSTEM_MAX_DEVICES - devices->xdev_count; - uint32_t num_devs = solarxr_device_create_xdevs(self->hmd.get(), solar_devs, XRT_SYSTEM_MAX_DEVICES - devices->xdev_count); + uint32_t num_devs = solarxr_device_create_xdevs(&self->hmd, solar_devs, XRT_SYSTEM_MAX_DEVICES - devices->xdev_count); for (int i = 0; i < num_devs; i++) { devices->xdevs[n++] = solar_devs[i]; @@ -247,7 +240,7 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi u_system_devices_static_finalize(usysds, active_left_hand, active_right_hand); wivrn_comp_target_factory ctf(self, self->info.preferred_refresh_rate); - auto xret = comp_main_create_system_compositor(self->hmd.get(), &ctf, out_xsysc); + auto xret = comp_main_create_system_compositor(&self->hmd, &ctf, out_xsysc); if (xret != XRT_SUCCESS) { U_LOG_E("Failed to create system compositor"); @@ -256,9 +249,9 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi u_builder_create_space_overseer_legacy( &self->xrt_system.broadcast, - self->hmd.get(), - self->left_hand.get(), - self->right_hand.get(), + &self->hmd, + &self->left_hand, + &self->right_hand, devices->xdevs, devices->xdev_count, false, @@ -308,7 +301,7 @@ void wivrn_session::operator()(const from_headset::tracking & tracking) continue; xrt_pose offset; - auto tracking_origin = ((xrt_device *)hmd.get())->tracking_origin; + auto tracking_origin = static_cast(hmd).tracking_origin; space_overseer->get_reference_space_offset(space_overseer, xrt_reference_space_type::XRT_SPACE_REFERENCE_TYPE_STAGE, &offset); xrt_vec3 hmd_pos = xrt_cast(pose.pose.position); @@ -337,9 +330,9 @@ void wivrn_session::operator()(const from_headset::tracking & tracking) auto offset = offset_est.get_offset(); - hmd->update_tracking(tracking, offset); - left_hand->update_tracking(tracking, offset); - right_hand->update_tracking(tracking, offset); + hmd.update_tracking(tracking, offset); + left_hand.update_tracking(tracking, offset); + right_hand.update_tracking(tracking, offset); if (eye_tracker) eye_tracker->update_tracking(tracking, offset); if (foveation) @@ -352,15 +345,15 @@ void wivrn_session::operator()(from_headset::hand_tracking && hand_tracking) { auto offset = offset_est.get_offset(); - left_hand->update_hand_tracking(hand_tracking, offset); - right_hand->update_hand_tracking(hand_tracking, offset); + left_hand.update_hand_tracking(hand_tracking, offset); + right_hand.update_hand_tracking(hand_tracking, offset); } void wivrn_session::operator()(from_headset::inputs && inputs) { auto offset = get_offset(); - left_hand->set_inputs(inputs, offset); - right_hand->set_inputs(inputs, offset); + left_hand.set_inputs(inputs, offset); + right_hand.set_inputs(inputs, offset); } void wivrn_session::operator()(from_headset::timesync_response && timesync) @@ -416,7 +409,7 @@ void wivrn_session::operator()(from_headset::feedback && feedback) void wivrn_session::operator()(from_headset::battery && battery) { - hmd->update_battery(battery); + hmd.update_battery(battery); } void wivrn_session::operator()(audio_data && data) @@ -458,7 +451,7 @@ void wivrn_session::run(std::weak_ptr weak_self) std::array wivrn_session::set_foveated_size(uint32_t width, uint32_t height) { - auto p = hmd->set_foveated_size(width, height); + auto p = hmd.set_foveated_size(width, height); if (foveation) foveation->set_initial_parameters(p); @@ -471,14 +464,14 @@ bool wivrn_session::apply_dynamic_foveation() if (!foveation) return false; - hmd->set_foveation_center(foveation->get_center()); - comp_target->render_dynamic_foveation(hmd->get_foveation_parameters()); + hmd.set_foveation_center(foveation->get_center()); + comp_target->render_dynamic_foveation(hmd.get_foveation_parameters()); return true; } std::array wivrn_session::get_foveation_parameters() { - return hmd->get_foveation_parameters(); + return hmd.get_foveation_parameters(); } void wivrn_session::dump_time(const std::string & event, uint64_t frame, int64_t time, uint8_t stream, const char * extra) diff --git a/server/driver/wivrn_session.h b/server/driver/wivrn_session.h index dbfaed9f..c3fd1d20 100644 --- a/server/driver/wivrn_session.h +++ b/server/driver/wivrn_session.h @@ -21,6 +21,8 @@ #include "clock_offset.h" #include "wivrn_connection.h" +#include "wivrn_controller.h" +#include "wivrn_hmd.h" #include "wivrn_ipc.h" #include "wivrn_packets.h" #include "xrt/xrt_defines.h" @@ -31,8 +33,6 @@ #include #include -class wivrn_hmd; -class wivrn_controller; class wivrn_eye_tracker; class wivrn_fb_face2_tracker; class wivrn_foveation; @@ -88,9 +88,9 @@ class wivrn_session : public std::enable_shared_from_this std::atomic quit = false; std::thread thread; - std::unique_ptr hmd; - std::unique_ptr left_hand; - std::unique_ptr right_hand; + wivrn_hmd hmd; + wivrn_controller left_hand; + wivrn_controller right_hand; std::unique_ptr eye_tracker; std::unique_ptr fb_face2_tracker; std::unique_ptr foveation;