Monado OpenXR Runtime
Loading...
Searching...
No Matches
sdl_internal.h
Go to the documentation of this file.
1// Copyright 2020-2023, Collabora, Ltd.
2// Copyright 2026, NVIDIA CORPORATION.
3// SPDX-License-Identifier: BSL-1.0
4/*!
5 * @file
6 * @brief Internal header for SDL XR system.
7 * @author Jakob Bornecrantz <jakob@collabora.com>
8 * @ingroup sdl_test
9 */
10
11#include "xrt/xrt_system.h"
12#include "xrt/xrt_device.h"
13#include "xrt/xrt_instance.h"
14#include "xrt/xrt_tracking.h"
15#include "xrt/xrt_compositor.h"
16
17#include "util/u_pacing.h"
18#include "util/u_logging.h"
19#include "util/comp_base.h"
20#include "util/comp_swapchain.h"
21
22#include "SDL2/SDL.h"
23
24#include "ogl/ogl_api.h"
25
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31struct sdl_program;
32
33/*!
34 * Sub-class of @ref comp_swapchain, used to do OpenGL rendering.
35 *
36 * @ingroup sdl_test
37 */
39{
40 struct comp_swapchain base;
41
42 //! Pointer back to main program.
43 struct sdl_program *sp;
44
45 //! Cached width and height.
46 int w, h;
47
48 //! Number of textures in base.base.base.image_count.
50
51 //! Same number of images as textures.
53};
54
55/*!
56 * Tracking frame state.
57 *
58 * @ingroup sdl_test
59 */
61{
62 int64_t id;
63 uint64_t predicted_display_time_ns;
64 uint64_t desired_present_time_ns;
65 uint64_t present_slop_ns;
66};
67
68/*!
69 * Split out for convenience.
70 *
71 * This ultimately implements @ref xrt_compositor_native but does so by
72 * extending @ref comp_base, similar to how @ref comp_compositor works.
73 *
74 * @extends comp_base
75 * @ingroup sdl_test
76 */
78{
79 //! Base native compositor.
81
82 //! The device being displayed to.
84
85 //! The supported view configurations
86 struct xrt_view_config view_configs[XRT_MAX_COMPOSITOR_VIEW_CONFIGS_COUNT];
87
88 //! Pacing helper to drive us forward.
90
91 struct
92 {
93 //! Frame interval that we are using.
95 } settings;
96
97 // Kept here for convenience.
98 struct xrt_system_compositor_info sys_info;
99
100
101 //! @todo Insert your own required members here
102
103 struct
104 {
105 struct sdl_comp_frame waited;
106 struct sdl_comp_frame rendering;
108};
109
110struct sdl_program_plus;
111
112/*!
113 * C base class for the SDL program.
114 *
115 * @implements xrt_instance
116 * @implements xrt_device
117 * @implements xrt_system_devices
118 * @extends sdl_compositor
119 * @ingroup sdl_test
120 */
122{
123 //! Base class for devices.
125
126 //! Instance base.
128
129 //! System, implemented using base code.
130 struct b_system *bsys;
131
132 //! System devices base.
134
135 //! Space overseer, implemented for now using helper code.
137
138 //! SDL compositor struct.
140
141 //! Created system compositor.
143
144 //! Inputs exposed by the SDL device.
145 struct xrt_input inputs[1];
146
147 //! HMD parts exposed by the SDL device to become a HMD.
149
150 //! Tracking origin that the device is located in.
152
153 //! The current log level.
155
156 struct
157 {
158 struct
159 {
160 //! The pose of the head, only used for view space.
162 } head;
163
164 struct
165 {
166 //! Pose of each individual eye.
167 struct xrt_pose pose;
168
169 //! Fov of each individual eye.
170 struct xrt_fov fov;
171 } left, right;
172 } state;
173
174 //! The main window.
175 SDL_Window *win;
176
177 //! Main OpenGL context.
178 SDL_GLContext ctx;
179
180 //! Protects the OpenGL context.
182
183 //! Pointer back to the C++ part of the program.
185};
186
187
188static inline struct sdl_program *
189from_xinst(struct xrt_instance *xinst)
190{
191 return container_of(xinst, struct sdl_program, xinst_base);
192}
193
194static inline struct sdl_program *
195from_xsysd(struct xrt_system_devices *xsysd)
196{
197 return container_of(xsysd, struct sdl_program, xsysd_base);
198}
199
200static inline struct sdl_program *
201from_xdev(struct xrt_device *xdev)
202{
203 return container_of(xdev, struct sdl_program, xdev_base);
204}
205
206static inline struct sdl_program *
207from_comp(struct xrt_compositor *xc)
208{
209 return container_of(xc, struct sdl_program, c.base.base);
210}
211
212
213/*!
214 * Spew level logging.
215 *
216 * @relates sdl_program
217 * @ingroup sdl_test
218 */
219#define ST_TRACE(sp, ...) U_LOG_IFL_T(sp->log_level, __VA_ARGS__);
220
221/*!
222 * Debug level logging.
223 *
224 * @relates sdl_program
225 */
226#define ST_DEBUG(sp, ...) U_LOG_IFL_D(sp->log_level, __VA_ARGS__);
227
228/*!
229 * Info level logging.
230 *
231 * @relates sdl_program
232 * @ingroup sdl_test
233 */
234#define ST_INFO(sp, ...) U_LOG_IFL_I(sp->log_level, __VA_ARGS__);
235
236/*!
237 * Warn level logging.
238 *
239 * @relates sdl_program
240 * @ingroup sdl_test
241 */
242#define ST_WARN(sp, ...) U_LOG_IFL_W(sp->log_level, __VA_ARGS__);
243
244/*!
245 * Error level logging.
246 *
247 * @relates sdl_program
248 * @ingroup sdl_test
249 */
250#define ST_ERROR(sp, ...) U_LOG_IFL_E(sp->log_level, __VA_ARGS__);
251
252/*!
253 * Check for OpenGL errors, context needs to be current.
254 *
255 * @ingroup sdl_test
256 */
257#define CHECK_GL() \
258 do { \
259 GLint err = glGetError(); \
260 if (err != 0) { \
261 U_LOG_RAW("%s:%u: error: 0x%04x", __func__, __LINE__, err); \
262 } \
263 } while (false)
264
265/*!
266 * Makes the OpenGL context current in this thread, takes lock.
267 *
268 * @ingroup sdl_test
269 */
270static inline void
272{
273 os_mutex_lock(&sp->current_mutex);
274 SDL_GL_MakeCurrent(sp->win, sp->ctx);
275}
276
277/*!
278 * Unmakes the any OpenGL context current in this thread, releases the lock.
279 *
280 * @ingroup sdl_test
281 */
282static inline void
284{
285 SDL_GL_MakeCurrent(NULL, NULL);
286 os_mutex_unlock(&sp->current_mutex);
287}
288
289
290/*
291 *
292 * sdl_device.c
293 *
294 */
295
296/*!
297 * Init the @ref xrt_device sub struct.
298 *
299 * In sdl_device.c
300 *
301 * @ingroup sdl_test
302 */
303void
304sdl_device_init(struct sdl_program *sp);
305
306
307/*
308 *
309 * sdl_swapchain.c
310 *
311 */
312
313/*!
314 * Implementation of @ref xrt_compositor::create_swapchain.
315 *
316 * @ingroup sdl_test
317 */
320 const struct xrt_swapchain_create_info *info,
321 struct xrt_swapchain **out_xsc);
322
323/*!
324 * Implementation of @ref xrt_compositor::import_swapchain.
325 *
326 * @ingroup sdl_test
327 */
330 const struct xrt_swapchain_create_info *info,
331 struct xrt_image_native *native_images,
332 uint32_t native_image_count,
333 struct xrt_swapchain **out_xsc);
334
335
336/*
337 *
338 * sdl_compositor.c
339 *
340 */
341
342/*!
343 * Initializes the compositor part of the SDL program.
344 *
345 * @ingroup sdl_test
346 */
347void
349
350/*!
351 * Creates the system compositor that wraps the native compositor.
352 *
353 * @ingroup sdl_test
354 */
357
358
359/*
360 *
361 * sdl_instance.c
362 *
363 */
364
365/*!
366 * Init the @ref xrt_system (and @ref u_system) struct.
367 *
368 * @ingroup sdl_test
369 */
370void
371sdl_system_init(struct sdl_program *sp);
372
373/*!
374 * Init the @ref xrt_system_devices sub struct.
375 *
376 * @ingroup sdl_test
377 */
378void
380
381/*!
382 * Init the @ref xrt_instance sub struct.
383 *
384 * @ingroup sdl_test
385 */
386void
388
389
390/*
391 *
392 * sdl_program.cpp
393 *
394 */
395
396/*!
397 * Create the SDL program.
398 *
399 * @ingroup sdl_test
400 */
401struct sdl_program *
403
404/*!
405 * Render a frame, called by the compositor when layers have been committed.
406 *
407 * @ingroup sdl_test
408 */
409void
411
412/*!
413 * Destroy the SDL program.
414 *
415 * @ingroup sdl_test
416 */
417void
419
420
421#ifdef __cplusplus
422}
423#endif
Helper implementation for native compositors.
Independent swapchain implementation.
u_logging_level
Logging level enum.
Definition u_logging.h:45
#define XRT_MAX_SWAPCHAIN_IMAGES
Max swapchain images, artificial limit.
Definition xrt_limits.h:53
enum xrt_result xrt_result_t
Result type used across Monado.
#define container_of(ptr, type, field)
Get the holder from a pointer to a field.
Definition xrt_compiler.h:298
OpenGL API wrapper header.
void sdl_compositor_init(struct sdl_program *sp)
Initializes the compositor part of the SDL program.
Definition sdl_compositor.c:565
xrt_result_t sdl_compositor_create_system(struct sdl_program *sp, struct xrt_system_compositor **out_xsysc)
Creates the system compositor that wraps the native compositor.
Definition sdl_compositor.c:616
void sdl_system_init(struct sdl_program *sp)
Init the xrt_system (and u_system) struct.
Definition sdl_instance.c:155
void sdl_program_plus_destroy(struct sdl_program_plus *spp)
Destroy the SDL program.
Definition sdl_program.cpp:175
void sdl_instance_init(struct sdl_program *sp)
Init the xrt_instance sub struct.
Definition sdl_instance.c:217
void sdl_device_init(struct sdl_program *sp)
Init the xrt_device sub struct.
Definition sdl_device.c:74
void sdl_program_plus_render(struct sdl_program_plus *spp)
Render a frame, called by the compositor when layers have been committed.
Definition sdl_program.cpp:103
xrt_result_t sdl_swapchain_create(struct xrt_compositor *xc, const struct xrt_swapchain_create_info *info, struct xrt_swapchain **out_xsc)
Implementation of xrt_compositor::create_swapchain.
Definition sdl_swapchain.c:105
static void sdl_make_current(struct sdl_program *sp)
Makes the OpenGL context current in this thread, takes lock.
Definition sdl_internal.h:271
xrt_result_t sdl_swapchain_import(struct xrt_compositor *xc, const struct xrt_swapchain_create_info *info, struct xrt_image_native *native_images, uint32_t native_image_count, struct xrt_swapchain **out_xsc)
Implementation of xrt_compositor::import_swapchain.
Definition sdl_swapchain.c:143
void sdl_system_devices_init(struct sdl_program *sp)
Init the xrt_system_devices sub struct.
Definition sdl_instance.c:164
static void sdl_make_uncurrent(struct sdl_program *sp)
Unmakes the any OpenGL context current in this thread, releases the lock.
Definition sdl_internal.h:283
struct sdl_program * sdl_program_plus_create(void)
Create the SDL program.
Definition sdl_program.cpp:78
A helper to implement a xrt_system, takes care of multiplexing events to sessions.
Definition b_system.h:46
A simple compositor base that handles a lot of things for you.
Definition comp_base.h:69
struct xrt_compositor_native base
Base native compositor.
Definition comp_base.h:71
A swapchain that is almost a one to one mapping to a OpenXR swapchain.
Definition comp_swapchain.h:98
A wrapper around a native mutex.
Definition os_threading.h:69
Tracking frame state.
Definition sdl_internal.h:61
Split out for convenience.
Definition sdl_internal.h:78
struct xrt_device * xdev
The device being displayed to.
Definition sdl_internal.h:83
uint64_t frame_interval_ns
Frame interval that we are using.
Definition sdl_internal.h:94
struct comp_base base
Base native compositor.
Definition sdl_internal.h:80
struct sdl_compositor::@322 frame
struct xrt_view_config view_configs[XRT_MAX_COMPOSITOR_VIEW_CONFIGS_COUNT]
The supported view configurations.
Definition sdl_internal.h:86
struct u_pacing_compositor * upc
Pacing helper to drive us forward.
Definition sdl_internal.h:89
C++ version of the sdl_program struct, where you place C++ only things.
Definition sdl_internal.hpp:25
C base class for the SDL program.
Definition sdl_internal.h:122
struct xrt_tracking_origin origin
Tracking origin that the device is located in.
Definition sdl_internal.h:151
struct xrt_system_compositor * xsysc
Created system compositor.
Definition sdl_internal.h:142
SDL_GLContext ctx
Main OpenGL context.
Definition sdl_internal.h:178
struct os_mutex current_mutex
Protects the OpenGL context.
Definition sdl_internal.h:181
struct b_system * bsys
System, implemented using base code.
Definition sdl_internal.h:130
struct xrt_device xdev_base
Base class for devices.
Definition sdl_internal.h:124
SDL_Window * win
The main window.
Definition sdl_internal.h:175
struct xrt_system_devices xsysd_base
System devices base.
Definition sdl_internal.h:133
struct xrt_fov fov
Fov of each individual eye.
Definition sdl_internal.h:170
struct xrt_input inputs[1]
Inputs exposed by the SDL device.
Definition sdl_internal.h:145
enum u_logging_level log_level
The current log level.
Definition sdl_internal.h:154
struct xrt_space_overseer * xso
Space overseer, implemented for now using helper code.
Definition sdl_internal.h:136
struct sdl_program_plus * spp
Pointer back to the C++ part of the program.
Definition sdl_internal.h:184
struct xrt_pose pose
The pose of the head, only used for view space.
Definition sdl_internal.h:161
struct xrt_hmd_parts hmd
HMD parts exposed by the SDL device to become a HMD.
Definition sdl_internal.h:148
struct sdl_compositor c
SDL compositor struct.
Definition sdl_internal.h:139
struct xrt_instance xinst_base
Instance base.
Definition sdl_internal.h:127
Sub-class of comp_swapchain, used to do OpenGL rendering.
Definition sdl_internal.h:39
int w
Cached width and height.
Definition sdl_internal.h:46
GLuint memory[XRT_MAX_SWAPCHAIN_IMAGES]
Same number of images as textures.
Definition sdl_internal.h:52
struct sdl_program * sp
Pointer back to main program.
Definition sdl_internal.h:43
GLuint textures[XRT_MAX_SWAPCHAIN_IMAGES]
Number of textures in base.base.base.image_count.
Definition sdl_internal.h:49
Compositor pacing helper interface.
Definition u_pacing.h:69
Common compositor client interface/base.
Definition xrt_compositor.h:1005
struct xrt_compositor_info info
Capabilities and recommended values information.
Definition xrt_compositor.h:1009
A single HMD or input device.
Definition xrt_device.h:345
Describes a projection matrix fov.
Definition xrt_defines.h:555
All of the device components that deals with interfacing to a users head.
Definition xrt_device.h:182
A single image of a swapchain based on native buffer handles.
Definition xrt_compositor.h:2103
A single named input, that sits on a xrt_device.
Definition xrt_device.h:253
This interface acts as a root object for Monado.
Definition xrt_instance.h:124
A pose composed of a position and orientation.
Definition xrt_defines.h:535
Object that oversees and manages spaces, one created for each XR system.
Definition xrt_space.h:97
Swapchain creation info.
Definition xrt_compositor.h:883
Common swapchain interface/base.
Definition xrt_compositor.h:552
Capabilities and information about the system compositor (and its wrapped native compositor,...
Definition xrt_compositor.h:2296
The system compositor handles composition for a system.
Definition xrt_compositor.h:2454
A collection of xrt_device, and an interface for identifying the roles they have been assigned.
Definition xrt_system.h:251
A tracking system or device origin.
Definition xrt_tracking.h:115
Definition xrt_compositor.h:2273
Basic logging functionality.
Shared pacing code.
Header declaring XRT graphics interfaces.
Header defining an xrt display or controller device.
Header for xrt_instance object.
Header for system objects.
Header defining the tracking system integration in Monado.