Monado OpenXR Runtime
xrt_space.h
Go to the documentation of this file.
1 // Copyright 2019-2023, Collabora, Ltd.
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4  * @file
5  * @brief Header defining xrt space and space overseer.
6  * @author Jakob Bornecrantz <jakob@collabora.com>
7  * @ingroup xrt_iface
8  */
9 
10 #pragma once
11 
12 #include "xrt/xrt_defines.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 
19 struct xrt_device;
20 
21 /*!
22  * A space very similar to a OpenXR XrSpace but not a full one-to-one mapping,
23  * but used to power XrSpace.
24  *
25  * @see @ref xrt_space_overseer
26  * @see @ref design-spaces
27  * @ingroup xrt_iface
28  */
29 struct xrt_space
30 {
31  /*!
32  * Reference helper.
33  */
34  struct xrt_reference reference;
35 
36  /*!
37  * Destroy function.
38  */
39  void (*destroy)(struct xrt_space *xs);
40 };
41 
42 /*!
43  * Update the reference counts on space(s).
44  *
45  * @param[in,out] dst Pointer to a object reference: if the object reference is
46  * non-null will decrement its counter. The reference that
47  * @p dst points to will be set to @p src.
48  * @param[in] src New object for @p dst to refer to (may be null).
49  * If non-null, will have its refcount increased.
50  * @ingroup xrt_iface
51  * @relates xrt_space
52  */
53 static inline void
54 xrt_space_reference(struct xrt_space **dst, struct xrt_space *src)
55 {
56  struct xrt_space *old_dst = *dst;
57 
58  if (old_dst == src) {
59  return;
60  }
61 
62  if (src) {
64  }
65 
66  *dst = src;
67 
68  if (old_dst) {
70  old_dst->destroy(old_dst);
71  }
72  }
73 }
74 
75 /*!
76  * Object that oversees and manages spaces, one created for each XR system.
77  *
78  * The space overseer is used by the state tracker to query the poses of spaces
79  * and devices in that space system. While the default implementation
80  * @ref u_space_overseer implements the spaces as a graph of relatable spaces,
81  * that is a implementation detail (the interface also lends itself to that
82  * since bases have parents). As such the graph is not exposed in this interface
83  * and spaces are technically free floating.
84  *
85  * One advantage of the free floating nature is that an overseer implementation
86  * has much greater flexibility in configuring the graph to fit the current XR
87  * system the best, it also have freedom to reconfigure the graph at runtime
88  * should that be needed. Since any potential graph isn't exposed there is no
89  * need to synchronise it across the app process and the service process.
90  *
91  * @see @ref design-spaces
92  * @ingroup xrt_iface
93  */
95 {
96  struct
97  {
98  struct xrt_space *root; //!< Root space, always available
99  struct xrt_space *view; //!< View space, may be null (in very rare cases).
100  struct xrt_space *local; //!< Local space, may be null (in very rare cases).
101  struct xrt_space *local_floor; //!< Local floor space, may be null.
102  struct xrt_space *stage; //!< Stage space, may be null.
103  struct xrt_space *unbounded; //!< Unbounded space, only here for slam trackers.
104 
105  /*!
106  * Semantic spaces to be mapped to OpenXR spaces.
107  */
108  } semantic;
109 
110  /*!
111  * Create a space with a fixed offset to the parent space.
112  *
113  * @param[in] xso Owning space overseer.
114  * @param[in] parent The parent space for the new space.
115  * @param[in] offset Offset to the space.
116  * @param[out] out_space The newly created space.
117  */
119  struct xrt_space *parent,
120  const struct xrt_pose *offset,
121  struct xrt_space **out_space);
122 
123  /*!
124  * Create a space that wraps the @p xdev input pose described by input
125  * @p name, implicitly make the device's tracking space the parent of
126  * the created space. The name pose_space was chosen because while most
127  * input poses are part of the device, they may also be things tracked
128  * by the device. The important part is that the space is following the
129  * pose, that it happens to be attached to device is coincidental.
130  *
131  * @param[in] xso Owning space overseer.
132  * @param[in] xdev Device to get the pose from.
133  * @param[in] name Name of the pose input.
134  * @param[out] out_space The newly created space.
135  */
137  struct xrt_device *xdev,
138  enum xrt_input_name name,
139  struct xrt_space **out_space);
140 
141  /*!
142  * Locate a space in the base space.
143  *
144  * @see xrt_device::get_tracked_pose.
145  *
146  * @param[in] xso Owning space overseer.
147  * @param[in] base_space The space that we want the pose in.
148  * @param[in] base_offset Offset if any to the base space.
149  * @param[in] at_timestamp_ns At which time.
150  * @param[in] space The space to be located.
151  * @param[in] offset Offset if any to the located space.
152  * @param[out] out_relation Resulting pose.
153  */
155  struct xrt_space *base_space,
156  const struct xrt_pose *base_offset,
157  uint64_t at_timestamp_ns,
158  struct xrt_space *space,
159  const struct xrt_pose *offset,
160  struct xrt_space_relation *out_relation);
161 
162  /*!
163  * Locate a the origin of the tracking space of a device, this is not
164  * the same as the device position. In other words, what is the position
165  * of the space that the device is in, and which it returns its poses
166  * in. Needed to use @ref xrt_device::get_view_poses and
167  * @ref xrt_device::get_hand_tracking.
168  *
169  * @see xrt_device::get_tracked_pose.
170  *
171  * @param[in] xso Owning space overseer.
172  * @param[in] base_space The space that we want the pose in.
173  * @param[in] base_offset Offset if any to the base space.
174  * @param[in] at_timestamp_ns At which time.
175  * @param[in] xdev Device to get the pose from.
176  * @param[out] out_relation Resulting pose.
177  */
179  struct xrt_space *base_space,
180  const struct xrt_pose *base_offset,
181  uint64_t at_timestamp_ns,
182  struct xrt_device *xdev,
183  struct xrt_space_relation *out_relation);
184 
185  /*!
186  * Increment the usage count of a reference space (aka semantic space).
187  * This lets the overseer know when different spaces are being used,
188  * allowing it to triggering a recenter if the local space is used for
189  * the first time, or stopping calculating where the stage space is if
190  * it is not used by the current application.
191  *
192  * @param[in] xso Owning space overseer.
193  * @param[in] type Which reference space is being referenced.
194  */
196 
197  /*!
198  * Decrement the usage count of a reference space (aka semantic space).
199  * See @ref xrt_space_overseer::ref_space_inc.
200  *
201  * @param[in] xso Owning space overseer.
202  * @param[in] type Which reference space is being referenced.
203  */
205 
206  /*!
207  * Trigger a re-center of the local and local_floor spaces, not all
208  * implementations of @ref xrt_space_overseer may support recenter. The
209  * recenter operation will normally mean that the local and local_floor
210  * will move to the where the view space currently is.
211  *
212  * @param[in] xso The space overseer.
213  */
215 
216  /*!
217  * Destroy function.
218  *
219  * @param xso The space overseer.
220  */
221  void (*destroy)(struct xrt_space_overseer *xs);
222 };
223 
224 /*!
225  * @copydoc xrt_space_overseer::create_offset_space
226  *
227  * Helper for calling through the function pointer.
228  *
229  * @public @memberof xrt_space_overseer
230  */
231 static inline xrt_result_t
233  struct xrt_space *parent,
234  const struct xrt_pose *offset,
235  struct xrt_space **out_space)
236 {
237  return xso->create_offset_space(xso, parent, offset, out_space);
238 }
239 
240 /*!
241  * @copydoc xrt_space_overseer::create_pose_space
242  *
243  * Helper for calling through the function pointer.
244  *
245  * @public @memberof xrt_space_overseer
246  */
247 static inline xrt_result_t
249  struct xrt_device *xdev,
250  enum xrt_input_name name,
251  struct xrt_space **out_space)
252 {
253  return xso->create_pose_space(xso, xdev, name, out_space);
254 }
255 
256 /*!
257  * @copydoc xrt_space_overseer::locate_space
258  *
259  * Helper for calling through the function pointer.
260  *
261  * @public @memberof xrt_space_overseer
262  */
263 static inline xrt_result_t
265  struct xrt_space *base_space,
266  const struct xrt_pose *base_offset,
267  uint64_t at_timestamp_ns,
268  struct xrt_space *space,
269  const struct xrt_pose *offset,
270  struct xrt_space_relation *out_relation)
271 {
272  return xso->locate_space(xso, base_space, base_offset, at_timestamp_ns, space, offset, out_relation);
273 }
274 
275 /*!
276  * @copydoc xrt_space_overseer::locate_device
277  *
278  * Helper for calling through the function pointer.
279  *
280  * @public @memberof xrt_space_overseer
281  */
282 static inline xrt_result_t
284  struct xrt_space *base_space,
285  const struct xrt_pose *base_offset,
286  uint64_t at_timestamp_ns,
287  struct xrt_device *xdev,
288  struct xrt_space_relation *out_relation)
289 {
290  return xso->locate_device(xso, base_space, base_offset, at_timestamp_ns, xdev, out_relation);
291 }
292 
293 /*!
294  * @copydoc xrt_space_overseer::ref_space_inc
295  *
296  * Helper for calling through the function pointer.
297  *
298  * @public @memberof xrt_space_overseer
299  */
300 static inline xrt_result_t
302 {
303  return xso->ref_space_inc(xso, type);
304 }
305 
306 /*!
307  * @copydoc xrt_space_overseer::ref_space_dec
308  *
309  * Helper for calling through the function pointer.
310  *
311  * @public @memberof xrt_space_overseer
312  */
313 static inline xrt_result_t
315 {
316  return xso->ref_space_dec(xso, type);
317 }
318 
319 /*!
320  * @copydoc xrt_space_overseer::recenter_local_spaces
321  *
322  * Helper for calling through the function pointer.
323  *
324  * @public @memberof xrt_space_overseer
325  */
326 static inline xrt_result_t
328 {
329  return xso->recenter_local_spaces(xso);
330 }
331 
332 /*!
333  * Helper for calling through the function pointer: does a null check and sets
334  * xc_ptr to null if freed.
335  *
336  * @see xrt_space_overseer::destroy
337  * @public @memberof xrt_space_overseer
338  */
339 static inline void
341 {
342  struct xrt_space_overseer *xso = *xso_ptr;
343  if (xso == NULL) {
344  return;
345  }
346 
347  xso->destroy(xso);
348  *xso_ptr = NULL;
349 }
350 
351 
352 #ifdef __cplusplus
353 }
354 #endif
static XRT_CHECK_RESULT bool xrt_reference_dec_and_is_zero(struct xrt_reference *xref)
Decrement the reference and return true if the value is now zero.
Definition: xrt_defines.h:1598
xrt_input_name
Every internal input source known to monado with a baked in type.
Definition: xrt_defines.h:1134
enum xrt_result xrt_result_t
Result type used across Monado.
static void xrt_reference_inc(struct xrt_reference *xref)
Increment the reference, probably want xrt_reference_inc_and_was_zero.
Definition: xrt_defines.h:1561
xrt_reference_space_type
Type of a OpenXR mapped reference space, maps to the semantic spaces on the xrt_space_overseer struct...
Definition: xrt_defines.h:599
static void xrt_space_reference(struct xrt_space **dst, struct xrt_space *src)
Update the reference counts on space(s).
Definition: xrt_space.h:54
A single HMD or input device.
Definition: xrt_device.h:230
enum xrt_device_name name
Enum identifier of the device.
Definition: xrt_device.h:232
A pose composed of a position and orientation.
Definition: xrt_defines.h:465
A base class for reference counted objects.
Definition: xrt_defines.h:96
Object that oversees and manages spaces, one created for each XR system.
Definition: xrt_space.h:95
xrt_result_t(* locate_space)(struct xrt_space_overseer *xso, struct xrt_space *base_space, const struct xrt_pose *base_offset, uint64_t at_timestamp_ns, struct xrt_space *space, const struct xrt_pose *offset, struct xrt_space_relation *out_relation)
Locate a space in the base space.
Definition: xrt_space.h:154
struct xrt_space * unbounded
Unbounded space, only here for slam trackers.
Definition: xrt_space.h:103
void(* destroy)(struct xrt_space_overseer *xs)
Destroy function.
Definition: xrt_space.h:221
xrt_result_t(* ref_space_dec)(struct xrt_space_overseer *xso, enum xrt_reference_space_type type)
Decrement the usage count of a reference space (aka semantic space).
Definition: xrt_space.h:204
xrt_result_t(* locate_device)(struct xrt_space_overseer *xso, struct xrt_space *base_space, const struct xrt_pose *base_offset, uint64_t at_timestamp_ns, struct xrt_device *xdev, struct xrt_space_relation *out_relation)
Locate a the origin of the tracking space of a device, this is not the same as the device position.
Definition: xrt_space.h:178
struct xrt_space * local
Local space, may be null (in very rare cases).
Definition: xrt_space.h:100
struct xrt_space * local_floor
Local floor space, may be null.
Definition: xrt_space.h:101
static xrt_result_t xrt_space_overseer_ref_space_dec(struct xrt_space_overseer *xso, enum xrt_reference_space_type type)
Decrement the usage count of a reference space (aka semantic space).
Definition: xrt_space.h:314
xrt_result_t(* create_offset_space)(struct xrt_space_overseer *xso, struct xrt_space *parent, const struct xrt_pose *offset, struct xrt_space **out_space)
Create a space with a fixed offset to the parent space.
Definition: xrt_space.h:118
struct xrt_space * view
View space, may be null (in very rare cases).
Definition: xrt_space.h:99
static xrt_result_t xrt_space_overseer_locate_device(struct xrt_space_overseer *xso, struct xrt_space *base_space, const struct xrt_pose *base_offset, uint64_t at_timestamp_ns, struct xrt_device *xdev, struct xrt_space_relation *out_relation)
Locate a the origin of the tracking space of a device, this is not the same as the device position.
Definition: xrt_space.h:283
struct xrt_space * stage
Stage space, may be null.
Definition: xrt_space.h:102
static void xrt_space_overseer_destroy(struct xrt_space_overseer **xso_ptr)
Helper for calling through the function pointer: does a null check and sets xc_ptr to null if freed.
Definition: xrt_space.h:340
static xrt_result_t xrt_space_overseer_ref_space_inc(struct xrt_space_overseer *xso, enum xrt_reference_space_type type)
Increment the usage count of a reference space (aka semantic space).
Definition: xrt_space.h:301
static xrt_result_t xrt_space_overseer_recenter_local_spaces(struct xrt_space_overseer *xso)
Trigger a re-center of the local and local_floor spaces, not all implementations of xrt_space_oversee...
Definition: xrt_space.h:327
static xrt_result_t xrt_space_overseer_create_offset_space(struct xrt_space_overseer *xso, struct xrt_space *parent, const struct xrt_pose *offset, struct xrt_space **out_space)
Create a space with a fixed offset to the parent space.
Definition: xrt_space.h:232
struct xrt_space * root
Root space, always available.
Definition: xrt_space.h:98
xrt_result_t(* create_pose_space)(struct xrt_space_overseer *xso, struct xrt_device *xdev, enum xrt_input_name name, struct xrt_space **out_space)
Create a space that wraps the xdev input pose described by input name, implicitly make the device's t...
Definition: xrt_space.h:136
static xrt_result_t xrt_space_overseer_create_pose_space(struct xrt_space_overseer *xso, struct xrt_device *xdev, enum xrt_input_name name, struct xrt_space **out_space)
Create a space that wraps the xdev input pose described by input name, implicitly make the device's t...
Definition: xrt_space.h:248
xrt_result_t(* recenter_local_spaces)(struct xrt_space_overseer *xso)
Trigger a re-center of the local and local_floor spaces, not all implementations of xrt_space_oversee...
Definition: xrt_space.h:214
xrt_result_t(* ref_space_inc)(struct xrt_space_overseer *xso, enum xrt_reference_space_type type)
Increment the usage count of a reference space (aka semantic space).
Definition: xrt_space.h:195
static xrt_result_t xrt_space_overseer_locate_space(struct xrt_space_overseer *xso, struct xrt_space *base_space, const struct xrt_pose *base_offset, uint64_t at_timestamp_ns, struct xrt_space *space, const struct xrt_pose *offset, struct xrt_space_relation *out_relation)
Locate a space in the base space.
Definition: xrt_space.h:264
A relation with two spaces, includes velocity and acceleration.
Definition: xrt_defines.h:657
A space very similar to a OpenXR XrSpace but not a full one-to-one mapping, but used to power XrSpace...
Definition: xrt_space.h:30
struct xrt_reference reference
Reference helper.
Definition: xrt_space.h:34
void(* destroy)(struct xrt_space *xs)
Destroy function.
Definition: xrt_space.h:39
Common defines and enums for XRT.