Monado OpenXR Runtime
Loading...
Searching...
No Matches
g_frameserver.hpp
Go to the documentation of this file.
1// Copyright 2026, Beyley Cardellio
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Header for glue classes to wrap xrt frameserver interfaces.
6 * @author Beyley Cardellio <ep1cm1n10n123@gmail.com>
7 * @ingroup aux_util
8 */
9
10#pragma once
11
12#include "xrt/xrt_frameserver.h"
13
14#include "util/u_frameserver.h"
15
16#include "g_catch_guard.hpp"
17#include "g_traits.hpp"
18
19#include <type_traits>
20
21
22namespace xrt::util {
23
24/*!
25 * Selects which optional functions that the @ref FrameServerBase wrapper
26 * should set.
27 */
29{
30 //! @ref xrt_fs::slam_stream_start
31 bool slam{false};
32};
33
34/*!
35 * CRTP glue wrapper for @ref xrt_fs. Relies on standard layout to recover the
36 * derived object from the C struct, and has some requirements and limitations
37 * because of that. See @ref cpp-glue-wrappers for the guide and conventions
38 * for these wrappers.
39 *
40 * @ref xrt_fs has no destroy function, a frameserver is instead destroyed
41 * through a @ref xrt_frame_node added to a @ref xrt_frame_context, see
42 * @ref FrameNodeBase.
43 *
44 * The identifying fields (name, product, manufacturer, serial and source_id)
45 * are zeroed and are to be filled in by the derived class via @ref getXFS.
46 */
47template <class T, FrameServerFunctions functions> class FrameServerBase
48{
49public: // Methods
50 FrameServerBase() noexcept
51 {
52 static_assert(std::is_standard_layout_v<FrameServerBase>,
53 "glue base must be standard layout for pointer recovery");
54 static_assert(is_non_virtual_base_v<FrameServerBase, T>,
55 "glue base must be a non-virtual base of T for pointer recovery");
56
57 auto &xfs = *getXFS();
58
60
61 xfs.enumerate_modes = enumerateModesWrap;
62 xfs.configure_capture = configureCaptureWrap;
63 xfs.stream_start = streamStartWrap;
64 xfs.stream_stop = streamStopWrap;
65 xfs.is_running = isRunningWrap;
66
67 if constexpr (functions.slam) {
68 xfs.slam_stream_start = slamStreamStartWrap;
69 }
70 }
71
72 ~FrameServerBase() noexcept = default;
73
74 const T &
75 derived() const noexcept
76 {
77 return static_cast<const T &>(*this);
78 }
79
80 T &
81 derived() noexcept
82 {
83 return static_cast<T &>(*this);
84 }
85
86 static const T *
87 fromXFS(const xrt_fs *xfs) noexcept
88 {
89 return &(reinterpret_cast<const FrameServerBase *>(xfs)->derived());
90 }
91
92 static T *
93 fromXFS(xrt_fs *xfs) noexcept
94 {
95 return &(reinterpret_cast<FrameServerBase *>(xfs)->derived());
96 }
97
98 const xrt_fs *
99 getXFS() const noexcept
100 {
101 return &mFrameServer;
102 }
103
104 xrt_fs *
105 getXFS() noexcept
106 {
107 return &mFrameServer;
108 }
109
110
111private: // Members
112 /*!
113 * Wrapped @ref xrt_fs. Must be the first data member: a pointer to it is
114 * then interconvertible with a pointer to this standard-layout base, which
115 * lets the glue cast a C pointer back to the derived C++ class. See
116 * @ref cpp-glue-wrappers.
117 */
118 xrt_fs mFrameServer = {};
119
120
121private: // Functions
122// Not `->derived()`, that is ambiguous when T has more than one glue base.
123#define GET(xfs) (*fromXFS(xfs))
124
125 static xrt_result_t
126 enumerateModesWrap(struct xrt_fs *xfs, struct xrt_fs_mode **out_modes, uint32_t *out_count) noexcept
127 try {
128 return GET(xfs).enumerateModes(out_modes, out_count);
129 }
130 G_CATCH_GUARDS_WITH_RETURN(XRT_ERROR_UNCAUGHT_EXCEPTION)
131
132 static xrt_result_t
133 configureCaptureWrap(struct xrt_fs *xfs, struct xrt_fs_capture_parameters *cp) noexcept
134 try {
135 return GET(xfs).configureCapture(cp);
136 }
137 G_CATCH_GUARDS_WITH_RETURN(XRT_ERROR_UNCAUGHT_EXCEPTION)
138
139 static xrt_result_t
140 streamStartWrap(struct xrt_fs *xfs,
141 struct xrt_frame_sink *xs,
142 enum xrt_fs_capture_type capture_type,
143 uint32_t descriptor_index) noexcept
144 try {
145 return GET(xfs).streamStart(xs, capture_type, descriptor_index);
146 }
147 G_CATCH_GUARDS_WITH_RETURN(XRT_ERROR_UNCAUGHT_EXCEPTION)
148
149 static xrt_result_t
150 slamStreamStartWrap(struct xrt_fs *xfs, struct xrt_slam_sinks *sinks) noexcept
151 try {
152 return GET(xfs).slamStreamStart(sinks);
153 }
154 G_CATCH_GUARDS_WITH_RETURN(XRT_ERROR_UNCAUGHT_EXCEPTION)
155
156 static xrt_result_t
157 streamStopWrap(struct xrt_fs *xfs) noexcept
158 try {
159 return GET(xfs).streamStop();
160 }
161 G_CATCH_GUARDS_WITH_RETURN(XRT_ERROR_UNCAUGHT_EXCEPTION)
162
163 static xrt_result_t
164 isRunningWrap(struct xrt_fs *xfs, bool *out_is_running) noexcept
165 try {
166 return GET(xfs).isRunning(*out_is_running);
167 }
168 G_CATCH_GUARDS_WITH_RETURN(XRT_ERROR_UNCAUGHT_EXCEPTION)
169
170#undef GET
171};
172
173} // namespace xrt::util
CRTP glue wrapper for xrt_fs.
Definition g_frameserver.hpp:48
Catch guards for glue classes.
Shared type traits for glue classes.
void u_frameserver_populate_function_pointers(struct xrt_fs *xfs)
Populate the frameserver's function pointers with default implementations.
Definition u_frameserver.c:17
enum xrt_result xrt_result_t
Result type used across Monado.
xrt_fs_capture_type
Enum describing which type of capture we are doing.
Definition xrt_frameserver.h:59
@ XRT_ERROR_UNCAUGHT_EXCEPTION
C++ code threw an exception that wasn't caught.
Definition xrt_results.h:264
Selects which optional functions that the FrameServerBase wrapper should set.
Definition g_frameserver.hpp:29
bool slam
xrt_fs::slam_stream_start
Definition g_frameserver.hpp:31
A object that is sent frames.
Definition xrt_frame.h:58
Controlling the camera capture parameters.
Definition xrt_frameserver.h:36
Definition xrt_frameserver.h:46
Frameserver that generates frames.
Definition xrt_frameserver.h:72
Container of pointers to sinks that could be used for a SLAM system.
Definition xrt_tracking.h:214
Misc helpers for xrt_fs implementations.
Frameserver interface for video drivers.