Monado OpenXR Runtime
ipc_client.h
Go to the documentation of this file.
1 // Copyright 2020-2023, Collabora, Ltd.
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4  * @file
5  * @brief Common client side code.
6  * @author Pete Black <pblack@collabora.com>
7  * @author Jakob Bornecrantz <jakob@collabora.com>
8  * @author Korcan Hussein <korcan.hussein@collabora.com>
9  * @ingroup ipc_client
10  */
11 
12 #pragma once
13 
14 #include "xrt/xrt_compiler.h"
15 #include "xrt/xrt_config_os.h"
16 
17 #include "util/u_threading.h"
18 #include "util/u_logging.h"
19 
20 #include "shared/ipc_utils.h"
21 #include "shared/ipc_protocol.h"
23 
24 #include <stdio.h>
25 
26 
27 /*
28  *
29  * Logging
30  *
31  */
32 
33 #define IPC_TRACE(IPC_C, ...) U_LOG_IFL_T((IPC_C)->imc.log_level, __VA_ARGS__)
34 #define IPC_DEBUG(IPC_C, ...) U_LOG_IFL_D((IPC_C)->imc.log_level, __VA_ARGS__)
35 #define IPC_INFO(IPC_C, ...) U_LOG_IFL_I((IPC_C)->imc.log_level, __VA_ARGS__)
36 #define IPC_WARN(IPC_C, ...) U_LOG_IFL_W((IPC_C)->imc.log_level, __VA_ARGS__)
37 #define IPC_ERROR(IPC_C, ...) U_LOG_IFL_E((IPC_C)->imc.log_level, __VA_ARGS__)
38 
39 /*!
40  * This define will error if `XRET` is not `XRT_SUCCESS`, printing out that the
41  * @p FUNC_STR string has failed, then returns @p XRET. The argument @p IPC_C
42  * will be used to look up the @p cond_level for the @ref ipc_print_result call.
43  *
44  * @param IPC_C Client connection, used to look up @p cond_level.
45  * @param XRET The @p xrt_result_t to check.
46  * @param FUNC_STR String literal with the function name, used for logging.
47  *
48  * @ingroup ipc_client
49  */
50 #define IPC_CHK_AND_RET(IPC_C, XRET, FUNC_STR) \
51  do { \
52  xrt_result_t _ret = XRET; \
53  if (_ret != XRT_SUCCESS) { \
54  ipc_print_result((IPC_C)->imc.log_level, __FILE__, __LINE__, __func__, _ret, FUNC_STR); \
55  return _ret; \
56  } \
57  } while (false)
58 
59 /*!
60  * This define will error if `XRET` is not `XRT_SUCCESS`, printing out that the
61  * @p FUNC_STR string has failed, then gotos @p GOTO. The argument @p IPC_C
62  * will be used to look up the @p cond_level for the @ref ipc_print_result call.
63  *
64  * @param IPC_C Client connection, used to look up @p cond_level.
65  * @param XRET The @p xrt_result_t to check.
66  * @param FUNC_STR String literal with the function name, used for logging.
67  * @param GOTO Goto label to jump to on error.
68  *
69  * @ingroup ipc_client
70  */
71 #define IPC_CHK_WITH_GOTO(IPC_C, XRET, FUNC_STR, GOTO) \
72  do { \
73  xrt_result_t _ret = XRET; \
74  if (_ret != XRT_SUCCESS) { \
75  ipc_print_result((IPC_C)->imc.log_level, __FILE__, __LINE__, __func__, _ret, FUNC_STR); \
76  goto GOTO; \
77  } \
78  } while (false)
79 
80 /*!
81  * This define will error if `XRET` is not `XRT_SUCCESS`, printing out that the
82  * @p FUNC_STR string has failed, then returns @p RET. The argument @p IPC_C
83  * will be used to look up the @p cond_level for the @ref ipc_print_result call.
84  *
85  * @param IPC_C Client connection, used to look up @p cond_level.
86  * @param XRET The @p xrt_result_t to check.
87  * @param FUNC_STR String literal with the function name, used for logging.
88  * @param RET The value that is returned on error.
89  *
90  * @ingroup ipc_client
91  */
92 #define IPC_CHK_WITH_RET(IPC_C, XRET, FUNC_STR, RET) \
93  do { \
94  xrt_result_t _ret = XRET; \
95  if (_ret != XRT_SUCCESS) { \
96  ipc_print_result((IPC_C)->imc.log_level, __FILE__, __LINE__, __func__, _ret, FUNC_STR); \
97  return RET; \
98  } \
99  } while (false)
100 
101 /*!
102  * This define will error if `XRET` is not `XRT_SUCCESS`, printing out that the
103  * @p FUNC_STR string has failed, it only prints and does nothing else. The
104  * argument @p IPC_C will be used to look up the @p cond_level for the
105  * @ref ipc_print_result call.
106  *
107  * @param IPC_C Client connection, used to look up @p cond_level.
108  * @param XRET The @p xrt_result_t to check.
109  * @param FUNC_STR String literal with the function name, used for logging.
110  *
111  * @ingroup ipc_client
112  */
113 #define IPC_CHK_ONLY_PRINT(IPC_C, XRET, FUNC_STR) \
114  do { \
115  xrt_result_t _ret = XRET; \
116  if (_ret != XRT_SUCCESS) { \
117  ipc_print_result((IPC_C)->imc.log_level, __FILE__, __LINE__, __func__, _ret, FUNC_STR); \
118  } \
119  } while (false)
120 
121 /*!
122  * This define will error if `XRET` is not `XRT_SUCCESS`, printing out that the
123  * @p FUNC_STR string has failed, then it will always return the value. The
124  * argument @p IPC_C will be used to look up the @p cond_level for the
125  * @ref ipc_print_result call.
126  *
127  * @param IPC_C Client connection, used to look up @p cond_level.
128  * @param XRET The @p xrt_result_t to check and always return.
129  * @param FUNC_STR String literal with the function name, used for logging.
130  *
131  * @ingroup ipc_client
132  */
133 #define IPC_CHK_ALWAYS_RET(IPC_C, XRET, FUNC_STR) \
134  do { \
135  xrt_result_t _ret = XRET; \
136  if (_ret != XRT_SUCCESS) { \
137  ipc_print_result((IPC_C)->imc.log_level, __FILE__, __LINE__, __func__, _ret, FUNC_STR); \
138  } \
139  return _ret; \
140  } while (false)
141 
142 
143 /*
144  *
145  * Structs
146  *
147  */
148 
149 struct xrt_compositor_native;
150 
151 
152 /*!
153  * Connection.
154  */
156 {
157  struct ipc_message_channel imc;
158 
159  struct ipc_shared_memory *ism;
160  xrt_shmem_handle_t ism_handle;
161 
162  struct os_mutex mutex;
163 
164 #ifdef XRT_OS_ANDROID
165  struct ipc_client_android *ica;
166 #endif // XRT_OS_ANDROID
167 };
168 
169 /*!
170  * An IPC client proxy for an @ref xrt_device.
171  *
172  * @implements xrt_device
173  * @ingroup ipc_client
174  */
176 {
177  struct xrt_device base;
178 
179  struct ipc_connection *ipc_c;
180 
181  uint32_t device_id;
182 };
183 
184 
185 /*
186  *
187  * Internal functions.
188  *
189  */
190 
191 /*!
192  * Convenience helper to go from a xdev to @ref ipc_client_xdev.
193  *
194  * @ingroup ipc_client
195  */
196 static inline struct ipc_client_xdev *
198 {
199  return (struct ipc_client_xdev *)xdev;
200 }
201 
202 /*!
203  * Create an IPC client system compositor.
204  *
205  * It owns a special implementation of the @ref xrt_system_compositor interface.
206  *
207  * This actually creates an IPC client "native" compositor with deferred
208  * initialization. The @ref ipc_client_create_native_compositor function
209  * actually completes the deferred initialization of the compositor, effectively
210  * finishing creation of a compositor IPC proxy.
211  *
212  * @param ipc_c IPC connection
213  * @param xina Optional native image allocator for client-side allocation. Takes
214  * ownership if one is supplied.
215  * @param xdev Taken in but not used currently @todo remove this param?
216  * @param[out] out_xcs Pointer to receive the created xrt_system_compositor.
217  */
220  struct xrt_image_native_allocator *xina,
221  struct xrt_device *xdev,
222  struct xrt_system_compositor **out_xcs);
223 
224 /*!
225  * Create a native compositor from a system compositor, this is used instead
226  * of the normal xrt_system_compositor::create_native_compositor function
227  * because it doesn't support events being generated on the app side. This will
228  * also create the session on the service side.
229  *
230  * @param xsysc IPC created system compositor.
231  * @param xsi Session information struct.
232  * @param[out] out_xcn Pointer to receive the created xrt_compositor_native.
233  */
236  const struct xrt_session_info *xsi,
237  struct xrt_compositor_native **out_xcn);
238 
239 struct xrt_device *
240 ipc_client_hmd_create(struct ipc_connection *ipc_c, struct xrt_tracking_origin *xtrack, uint32_t device_id);
241 
242 struct xrt_device *
243 ipc_client_device_create(struct ipc_connection *ipc_c, struct xrt_tracking_origin *xtrack, uint32_t device_id);
244 
245 struct xrt_system *
246 ipc_client_system_create(struct ipc_connection *ipc_c, struct xrt_system_compositor *xsysc);
247 
248 struct xrt_space_overseer *
249 ipc_client_space_overseer_create(struct ipc_connection *ipc_c);
250 
251 struct xrt_system_devices *
252 ipc_client_system_devices_create(struct ipc_connection *ipc_c);
253 
254 struct xrt_session *
255 ipc_client_session_create(struct ipc_connection *ipc_c);
Generic typedef for platform-specific shared memory handle.
static struct ipc_client_xdev * ipc_client_xdev(struct xrt_device *xdev)
Convenience helper to go from a xdev to ipc_client_xdev.
Definition: ipc_client.h:197
enum xrt_result xrt_result_t
Result type used across Monado.
xrt_result_t ipc_client_create_native_compositor(struct xrt_system_compositor *xsysc, const struct xrt_session_info *xsi, struct xrt_compositor_native **out_xcn)
Create a native compositor from a system compositor, this is used instead of the normal xrt_system_co...
Definition: ipc_client_compositor.c:1061
xrt_result_t ipc_client_create_system_compositor(struct ipc_connection *ipc_c, struct xrt_image_native_allocator *xina, struct xrt_device *xdev, struct xrt_system_compositor **out_xcs)
Create an IPC client system compositor.
Definition: ipc_client_compositor.c:1092
IPC message channel functions.
Common protocol definition.
IPC util helpers, for internal use only.
Definition: ipc_client_android.cpp:26
An IPC client proxy for an xrt_device.
Definition: ipc_client.h:176
Connection.
Definition: ipc_client.h:156
Wrapper for a socket and flags.
Definition: ipc_message_channel.h:30
A big struct that contains all data that is shared to a client, no pointers allowed in this.
Definition: ipc_protocol.h:195
A wrapper around a native mutex.
Definition: os_threading.h:55
Main compositor server interface.
Definition: xrt_compositor.h:2196
A single HMD or input device.
Definition: xrt_device.h:230
Allocator for system native images, in general you do not need to free the images as they will be con...
Definition: xrt_compositor.h:2598
Session information, mostly overlay extension data.
Definition: xrt_compositor.h:930
The XRT representation of XrSession, this object does not have all of the functionality of a session,...
Definition: xrt_session.h:233
Object that oversees and manages spaces, one created for each XR system.
Definition: xrt_space.h:95
The system compositor handles composition for a system.
Definition: xrt_compositor.h:2386
A collection of xrt_device, and an interface for identifying the roles they have been assigned.
Definition: xrt_system.h:218
A system is a collection of devices, policies and optionally a compositor that is organised into a ch...
Definition: xrt_system.h:61
A tracking system or device origin.
Definition: xrt_tracking.h:71
Basic logging functionality.
Slightly higher level thread safe helpers.
Header holding common defines.
Auto detect OS and certain features.