The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

ipc: Add callbacks and unify main functions

Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/2467>

+121 -62
+44 -17
src/xrt/ipc/server/ipc_server_interface.h
··· 10 10 * @ingroup ipc_server 11 11 */ 12 12 13 - #include "xrt/xrt_compiler.h" 13 + #pragma once 14 + 14 15 #include "xrt/xrt_config_os.h" 16 + #include "xrt/xrt_results.h" 15 17 16 - #ifndef XRT_OS_ANDROID 17 18 #include "util/u_debug_gui.h" 18 - #endif 19 19 20 20 21 21 #ifdef __cplusplus ··· 23 23 #endif 24 24 25 25 26 - #ifndef XRT_OS_ANDROID 26 + struct xrt_instance; 27 + struct ipc_server; 27 28 28 29 /*! 29 30 * Information passed into the IPC server main function, used for customization ··· 38 39 }; 39 40 40 41 /*! 41 - * Main entrypoint to the compositor process. 42 + * 43 + * @ingroup ipc_server 44 + */ 45 + struct ipc_server_callbacks 46 + { 47 + /*! 48 + * The IPC server failed to init. 49 + * 50 + * @param[in] xret The error code generated during init. 51 + * @param[in] data User data given passed into the main function. 52 + */ 53 + void (*init_failed)(xrt_result_t xret, void *data); 54 + 55 + /*! 56 + * The service has completed init and is entering its mainloop. 57 + * 58 + * @param[in] s The IPC server. 59 + * @param[in] xinst Instance that was created by the IPC server. 60 + * @param[in] data User data given passed into the main function. 61 + */ 62 + void (*mainloop_entering)(struct ipc_server *s, struct xrt_instance *xinst, void *data); 63 + 64 + /*! 65 + * The service is leaving the mainloop, after this callback returns the 66 + * IPC server will destroy all resources created. 67 + * 68 + * @param[in] s The IPC server. 69 + * @param[in] xinst Instance that was created by the IPC server. 70 + * @param[in] data User data given passed into the main function. 71 + */ 72 + void (*mainloop_leaving)(struct ipc_server *s, struct xrt_instance *xinst, void *data); 73 + }; 74 + 75 + /*! 76 + * Common main function for starting the IPC service. 42 77 * 43 78 * @ingroup ipc_server 44 79 */ 45 80 int 46 - ipc_server_main(int argc, char **argv, const struct ipc_server_main_info *ismi); 47 - 48 - #endif 81 + ipc_server_main_common(const struct ipc_server_main_info *ismi, const struct ipc_server_callbacks *iscb, void *data); 49 82 50 83 51 - #ifdef XRT_OS_ANDROID 84 + #ifndef XRT_OS_ANDROID 52 85 53 86 /*! 54 - * Main entrypoint to the server process. 55 - * 56 - * @param ps Pointer to populate with the server struct. 57 - * @param startup_complete_callback Function to call upon completing startup 58 - * and populating *ps, but before entering 59 - * the mainloop. 60 - * @param data user data to pass to your callback. 87 + * Main entrypoint to the compositor process. 61 88 * 62 89 * @ingroup ipc_server 63 90 */ 64 91 int 65 - ipc_server_main_android(struct ipc_server **ps, void (*startup_complete_callback)(void *data), void *data); 92 + ipc_server_main(int argc, char **argv, const struct ipc_server_main_info *ismi); 66 93 67 94 #endif 68 95
+44 -40
src/xrt/ipc/server/ipc_server_process.c
··· 978 978 return XRT_SUCCESS; 979 979 } 980 980 981 - #ifndef XRT_OS_ANDROID 982 981 int 983 - ipc_server_main(int argc, char **argv, const struct ipc_server_main_info *ismi) 982 + ipc_server_main_common(const struct ipc_server_main_info *ismi, 983 + const struct ipc_server_callbacks *callbacks, 984 + void *data) 984 985 { 985 986 xrt_result_t xret = XRT_SUCCESS; 986 987 int ret = -1; ··· 1008 1009 xret = init_all(s, log_level); 1009 1010 U_LOG_CHK_ONLY_PRINT(log_level, xret, "init_all"); 1010 1011 if (xret != XRT_SUCCESS) { 1011 - #ifdef XRT_OS_LINUX 1012 - // Print information how to debug issues. 1013 - print_linux_end_user_failed_information(log_level); 1014 - #endif 1015 - 1012 + // Propegate the failure. 1013 + callbacks->init_failed(xret, data); 1016 1014 u_debug_gui_stop(&s->debug_gui); 1017 1015 free(s); 1018 1016 return -1; ··· 1021 1019 // Start the debug UI now (if enabled). 1022 1020 u_debug_gui_start(s->debug_gui, s->xinst, s->xsysd); 1023 1021 1024 - #ifdef XRT_OS_LINUX 1025 - // Print a very clear service started message. 1026 - print_linux_end_user_started_information(log_level); 1027 - #endif 1022 + // Tell the callbacks we are entering the main-loop. 1023 + callbacks->mainloop_entering(s, s->xinst, data); 1024 + 1028 1025 // Main loop. 1029 1026 ret = main_loop(s); 1030 1027 1028 + // Tell the callbacks we are leaving the main-loop. 1029 + callbacks->mainloop_leaving(s, s->xinst, data); 1030 + 1031 1031 // Stop the UI before tearing everything down. 1032 1032 u_debug_gui_stop(&s->debug_gui); 1033 1033 ··· 1044 1044 return ret; 1045 1045 } 1046 1046 1047 - #endif // !XRT_OS_ANDROID 1048 1047 1049 - #ifdef XRT_OS_ANDROID 1050 - int 1051 - ipc_server_main_android(struct ipc_server **ps, void (*startup_complete_callback)(void *data), void *data) 1052 - { 1053 - xrt_result_t xret = XRT_SUCCESS; 1054 - int ret = -1; 1055 - 1056 - // Get log level first. 1057 - enum u_logging_level log_level = debug_get_log_option_ipc_log(); 1058 - 1059 - struct ipc_server *s = U_TYPED_CALLOC(struct ipc_server); 1060 - U_LOG_D("Created IPC server!"); 1061 - 1062 - xret = init_all(s, log_level); 1063 - U_LOG_CHK_ONLY_PRINT(log_level, xret, "init_all"); 1064 - if (xret != XRT_SUCCESS) { 1065 - free(s); 1066 - startup_complete_callback(data); 1067 - return -1; 1068 - } 1048 + #ifndef XRT_OS_ANDROID 1069 1049 1070 - *ps = s; 1071 - startup_complete_callback(data); 1050 + static void 1051 + init_failed(xrt_result_t xret, void *data) 1052 + { 1053 + #ifdef XRT_OS_LINUX 1054 + // Print information how to debug issues. 1055 + print_linux_end_user_failed_information(debug_get_log_option_ipc_log()); 1056 + #endif 1057 + } 1072 1058 1073 - ret = main_loop(s); 1059 + static void 1060 + mainloop_entering(struct ipc_server *s, struct xrt_instance *xinst, void *data) 1061 + { 1062 + #ifdef XRT_OS_LINUX 1063 + // Print a very clear service started message. 1064 + print_linux_end_user_started_information(s->log_level); 1065 + #endif 1066 + } 1074 1067 1075 - teardown_all(s); 1076 - free(s); 1068 + static void 1069 + mainloop_leaving(struct ipc_server *s, struct xrt_instance *xinst, void *data) 1070 + { 1071 + // No-op 1072 + } 1077 1073 1078 - U_LOG_I("Server exiting '%i'!", ret); 1074 + int 1075 + ipc_server_main(int argc, char **argv, const struct ipc_server_main_info *ismi) 1076 + { 1077 + const struct ipc_server_callbacks callbacks = { 1078 + .init_failed = init_failed, 1079 + .mainloop_entering = mainloop_entering, 1080 + .mainloop_leaving = mainloop_leaving, 1081 + }; 1079 1082 1080 - return ret; 1083 + return ipc_server_main_common(ismi, &callbacks, NULL); 1081 1084 } 1082 - #endif // XRT_OS_ANDROID 1085 + 1086 + #endif // !XRT_OS_ANDROID
+33 -5
src/xrt/targets/service-lib/service_target.cpp
··· 1 1 // Copyright 2020, Collabora, Ltd. 2 + // Copyright 2024-2025, NVIDIA CORPORATION. 2 3 // SPDX-License-Identifier: BSL-1.0 3 4 /*! 4 5 * @file ··· 41 42 } 42 43 43 44 void 44 - signalStartupComplete() 45 + signalStartupComplete(ipc_server *s) 45 46 { 46 47 std::unique_lock<std::mutex> lock{server_mutex}; 47 48 startup_complete = true; 49 + server = s; 48 50 startup_cond.notify_all(); 49 51 } 50 52 ··· 53 55 { 54 56 std::unique_lock lock(server_mutex); 55 57 if (!server && !server_thread) { 56 - server_thread = std::make_unique<std::thread>( 57 - [&]() { ipc_server_main_android(&server, signalStartupCompleteTrampoline, this); }); 58 + server_thread = 59 + std::make_unique<std::thread>([&]() { ipc_server_main_common(&ismi, &callbacks, this); }); 58 60 } 59 61 } 60 62 61 63 static void 62 - signalStartupCompleteTrampoline(void *data) 64 + signalInitFailed(xrt_result_t xret, void *data) 63 65 { 64 - static_cast<IpcServerHelper *>(data)->signalStartupComplete(); 66 + static_cast<IpcServerHelper *>(data)->signalStartupComplete(nullptr); 67 + } 68 + 69 + static void 70 + signalStartupCompleteTrampoline(ipc_server *s, xrt_instance *xsint, void *data) 71 + { 72 + static_cast<IpcServerHelper *>(data)->signalStartupComplete(s); 73 + } 74 + 75 + static void 76 + signalShuttingDownTrampoline(ipc_server *s, xrt_instance *xsint, void *data) 77 + { 78 + // No-op 65 79 } 66 80 67 81 int32_t ··· 113 127 } 114 128 return server && completed; 115 129 } 130 + 131 + const struct ipc_server_main_info ismi = { 132 + .udgci = 133 + { 134 + .window_title = "Monado Android Service", 135 + .open = U_DEBUG_GUI_OPEN_NEVER, 136 + }, 137 + }; 138 + 139 + const struct ipc_server_callbacks callbacks = { 140 + .init_failed = signalInitFailed, 141 + .mainloop_entering = signalStartupCompleteTrampoline, 142 + .mainloop_leaving = signalShuttingDownTrampoline, 143 + }; 116 144 117 145 //! Reference to the ipc_server, managed by ipc_server_process 118 146 struct ipc_server *server = NULL;