Monado OpenXR Runtime
mock_compositor.h
Go to the documentation of this file.
1// Copyright 2020-2022, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief A mock native compositor to use when testing client compositors.
6 * @author Rylie Pavlik <rylie.pavlik@collabora.com>
7 * @author Jakob Bornecrantz <jakob@collabora.com>
8 */
9
10#pragma once
11#include "xrt/xrt_compositor.h"
12#include "xrt/xrt_defines.h"
13#include "xrt/xrt_handles.h"
14
15#ifdef __cplusplus
16
17#include <type_traits>
18
19extern "C" {
20#endif
21
23/*!
24 * Mock implementation of a native compositor
25 * @implements xrt_compositor_native
26 */
28{
30
31 //! ID for next swapchain
32 uint32_t next_id;
33
34 //! Mock users can populate this pointer to use data from hooks
35 void *userdata;
36
37 /*!
38 * Optional function pointers you can populate to hook into the behavior of the mock compositor implementation.
39 *
40 * Providing a function pointer will disable any built-in functionality in the mock for most of these fields.
41 * While you can populate these with a lambda, because they're plain function pointers, you can't have any
42 * captures, so use @ref mock_compositor::userdata to read or write any data from the outside world.
43 */
44 struct
45 {
46 /*!
47 * Optional function pointer for mock compositor, called during
48 * @ref xrt_comp_get_swapchain_create_properties
49 */
51 const struct xrt_swapchain_create_info *info,
53
54 /*!
55 * Optional function pointer for mock compositor, called during @ref xrt_comp_create_swapchain
56 *
57 * Takes the extra parameter of the typed pointer to the in-progress swapchain @p mcsc , which is
58 * allocated and has basic values populated for it, even if this function pointer is set.
59 */
61 struct mock_compositor_swapchain *mcsc,
62 const struct xrt_swapchain_create_info *info,
63 struct xrt_swapchain **out_xsc);
64
65 /*!
66 * Optional function pointer for mock compositor, called during @ref xrt_comp_import_swapchain
67 *
68 * Takes the extra parameter of the typed pointer to the in-progress swapchain @p mcsc , which is
69 * allocated and has basic values populated for it, even if this function pointer is set. Does **not**
70 * release the native images passed in if this function pointer is set, so you will have to do that
71 * yourself.
72 */
74 struct mock_compositor_swapchain *mcsc,
75 const struct xrt_swapchain_create_info *info,
76 struct xrt_image_native *native_images,
77 uint32_t image_count,
78 struct xrt_swapchain **out_xsc);
79
80 // Mocks for the following not yet implemented
81#if 0
82 /*!
83 * Optional function pointer for mock compositor, called during @ref xrt_comp_import_fence
84 */
87 struct xrt_compositor_fence **out_xcf);
88
89 /*!
90 * Optional function pointer for mock compositor, called during @ref xrt_comp_create_semaphore
91 */
94 struct xrt_compositor_semaphore **out_xcsem);
95
96 /*!
97 * Optional function pointer for mock compositor, called during @ref xrt_comp_poll_events
98 */
99 xrt_result_t (*poll_events)(struct mock_compositor *mc, union xrt_compositor_event *out_xce);
100
101 /*!
102 * Optional function pointer for mock compositor, called during @ref xrt_comp_begin_session
103 */
104 xrt_result_t (*begin_session)(struct mock_compositor *mc, enum xrt_view_type view_type);
105
106 /*!
107 * Optional function pointer for mock compositor, called during @ref xrt_comp_end_session
108 */
110
111 /*!
112 * Optional function pointer for mock compositor, called during @ref xrt_comp_predict_frame
113 */
115 int64_t *out_frame_id,
116 uint64_t *out_wake_time_ns,
117 uint64_t *out_predicted_gpu_time_ns,
118 uint64_t *out_predicted_display_time_ns,
119 uint64_t *out_predicted_display_period_ns);
120
121 /*!
122 *
123 * Optional function pointer for mock compositor, called during @ref xrt_comp_mark_frame
124 */
126 int64_t frame_id,
128 uint64_t when_ns);
129
130 /*!
131 * Optional function pointer for mock compositor, called during @ref xrt_comp_wait_frame
132 */
134 int64_t *out_frame_id,
135 uint64_t *out_predicted_display_time,
136 uint64_t *out_predicted_display_period);
137
138 /*!
139 * Optional function pointer for mock compositor, called during @ref xrt_comp_begin_frame
140 */
141 xrt_result_t (*begin_frame)(struct mock_compositor *mc, int64_t frame_id);
142
143 /*!
144 * Optional function pointer for mock compositor, called during @ref xrt_comp_discard_frame
145 */
146 xrt_result_t (*discard_frame)(struct mock_compositor *mc, int64_t frame_id);
147
148 /*!
149 * Optional function pointer for mock compositor, called during @ref xrt_comp_layer_begin
150 */
152 int64_t frame_id,
153 uint64_t display_time_ns,
154 enum xrt_blend_mode env_blend_mode);
155
156 /*!
157 * Optional function pointer for mock compositor, called during @ref xrt_comp_layer_projection
158 */
160 struct xrt_device *xdev,
161 struct xrt_swapchain *l_xsc,
162 struct xrt_swapchain *r_xsc,
163 const struct xrt_layer_data *data);
164
165 /*!
166 * Optional function pointer for mock compositor, called during @ref
167 * xrt_comp_layer_projection_depth
168 */
170 struct xrt_device *xdev,
171 struct xrt_swapchain *l_xsc,
172 struct xrt_swapchain *r_xsc,
173 struct xrt_swapchain *l_d_xsc,
174 struct xrt_swapchain *r_d_xsc,
175 const struct xrt_layer_data *data);
176
177 /*!
178 * Optional function pointer for mock compositor, called during @ref xrt_comp_layer_quad
179 */
181 struct xrt_device *xdev,
182 struct xrt_swapchain *xsc,
183 const struct xrt_layer_data *data);
184
185 /*!
186 * Optional function pointer for mock compositor, called during @ref xrt_comp_layer_cube
187 */
189 struct xrt_device *xdev,
190 struct xrt_swapchain *xsc,
191 const struct xrt_layer_data *data);
192
193 /*!
194 * Optional function pointer for mock compositor, called during @ref xrt_comp_layer_cylinder
195 */
197 struct xrt_device *xdev,
198 struct xrt_swapchain *xsc,
199 const struct xrt_layer_data *data);
200
201 /*!
202 * Optional function pointer for mock compositor, called during @ref xrt_comp_layer_equirect1
203 */
205 struct xrt_device *xdev,
206 struct xrt_swapchain *xsc,
207 const struct xrt_layer_data *data);
208
209
210 /*!
211 * Optional function pointer for mock compositor, called during @ref xrt_comp_layer_equirect2
212 */
214 struct xrt_device *xdev,
215 struct xrt_swapchain *xsc,
216 const struct xrt_layer_data *data);
217
218 /*!
219 * Optional function pointer for mock compositor, called during @ref xrt_comp_layer_commit
220 */
222 int64_t frame_id,
223 xrt_graphics_sync_handle_t sync_handle);
224
225 /*!
226 * Optional function pointer for mock compositor, called during @ref
227 * xrt_comp_layer_commit_with_semaphore
228 */
230 int64_t frame_id,
231 struct xrt_compositor_semaphore *xcsem,
232 uint64_t value);
233#endif
234
235 /*!
236 * Optional function pointer for mock compositor, called during @ref xrt_comp_destroy (before actual
237 * destruction)
238 *
239 * The actual destruction is done by the mock implementation whether or not you populate this field.
240 */
241 void (*destroy)(struct mock_compositor *mc);
243
244 /*!
245 * Optional function pointers you can populate to hook into the behavior of the mock swapchain implementation.
246 *
247 * Providing a function pointer will disable any built-in functionality in the mock for most of these fields.
248 * While you can populate these with a lambda, because they're plain function pointers, you can't have any
249 * captures, so use @ref mock_compositor::userdata to read or write any data from the outside world.
250 */
251 struct
252 {
253 /*!
254 * Optional function pointer, called during @ref xrt_swapchain::destroy (before actual
255 * destruction)
256 *
257 * The actual destruction is done by the mock implementation whether or not you populate this field.
258 */
259 void (*destroy)(struct mock_compositor *mc, struct mock_compositor_swapchain *mcsc);
260
261 /*!
262 * Optional function pointer, called during @ref xrt_swapchain::acquire_image
263 */
265 struct mock_compositor_swapchain *mcsc,
266 uint32_t *out_index);
267
268 /*!
269 * Optional function pointer, called during @ref xrt_swapchain::wait_image
270 */
272 struct mock_compositor_swapchain *mcsc,
273 uint64_t timeout_ns,
274 uint32_t index);
275
276 /*!
277 * Optional function pointer, called during @ref xrt_swapchain::release_image
278 */
280 struct mock_compositor_swapchain *mcsc,
281 uint32_t index);
283};
284
285/*!
286 * @brief Cast a generic @ref xrt_compositor pointer (that you know externally is a @ref mock_compositor) to a @p
287 * mock_compositor pointer.
288 */
289static inline struct mock_compositor *
291{
292 return (struct mock_compositor *)(xc);
293}
294
295/*!
296 * Mock implementation of @ref xrt_swapchain_native
297 */
299{
301
302 //! A swapchain ID, assigned by create_swapchain/import_swapchain
303 uint32_t id;
304
305 //! Set if this swapchain was created by import_swapchain
307
308 //! Populated by copying the create info passed to create_swapchain/import_swapchain
310 /**
311 * Native handles for images.
312 * Populated by the import_swapchain mock if not hooked.
313 * Will be released/unreferenced at destruction by default.
314 */
316
317 //! Modified by the default mock implementations of acquire_image and release_image
319
320 //! Modified by the default mock implementations of wait_image and release_image
322
323 /**
324 * The image ID that will next be acquired.
325 *
326 * The default minimal mock implementation just increments this, modulo image count, regardless of
327 * acquire/wait/release status.
328 */
330
331 //! non-owning pointer to parent
333};
334
335/*!
336 * Cast a generic @ref xrt_swapchain pointer (that you know externally is a @ref mock_compositor_swapchain) to a @p
337 * mock_compositor_swapchain pointer.
338 */
339static inline struct mock_compositor_swapchain *
341{
342 return (struct mock_compositor_swapchain *)(xsc);
343}
344
345/*!
346 * Create a mock implementation of @ref xrt_compositor_native.
347 *
348 * The returned value can be passed to @ref mock_compositor() to use the internals of the mock, e.g. to populate
349 * hooks to override mock behavior.
350 */
353
354#ifdef __cplusplus
355
356static_assert(std::is_standard_layout<struct mock_compositor>::value);
357static_assert(std::is_standard_layout<struct mock_compositor_swapchain>::value);
358
359} // extern "C"
360#endif
xrt_blend_mode
Blend mode that the device supports, exact mirror of XrEnvironmentBlendMode.
Definition: xrt_defines.h:109
#define XRT_MAX_SWAPCHAIN_IMAGES
Max swapchain images, artificial limit.
Definition: xrt_limits.h:34
xrt_view_type
View type to be rendered to by the compositor.
Definition: xrt_compositor.h:862
enum xrt_result xrt_result_t
Result type used across Monado.
xrt_compositor_frame_point
Definition: xrt_compositor.h:868
struct xrt_compositor_native * mock_create_native_compositor()
Create a mock implementation of xrt_compositor_native.
Definition: mock_compositor.cpp:178
static struct mock_compositor * mock_compositor(xrt_compositor *xc)
Cast a generic xrt_compositor pointer (that you know externally is a mock_compositor) to a mock_compo...
Definition: mock_compositor.h:290
static struct mock_compositor_swapchain * mock_compositor_swapchain(xrt_swapchain *xsc)
Cast a generic xrt_swapchain pointer (that you know externally is a mock_compositor_swapchain) to a m...
Definition: mock_compositor.h:340
Mock implementation of xrt_swapchain_native.
Definition: mock_compositor.h:299
bool imported
Set if this swapchain was created by import_swapchain.
Definition: mock_compositor.h:306
uint32_t next_to_acquire
The image ID that will next be acquired.
Definition: mock_compositor.h:329
xrt_swapchain_create_info info
Populated by copying the create info passed to create_swapchain/import_swapchain.
Definition: mock_compositor.h:309
struct mock_compositor * mc
non-owning pointer to parent
Definition: mock_compositor.h:332
bool waited[XRT_MAX_SWAPCHAIN_IMAGES]
Modified by the default mock implementations of wait_image and release_image.
Definition: mock_compositor.h:321
xrt_graphics_buffer_handle_t handles[XRT_MAX_SWAPCHAIN_IMAGES]
Native handles for images.
Definition: mock_compositor.h:315
uint32_t id
A swapchain ID, assigned by create_swapchain/import_swapchain.
Definition: mock_compositor.h:303
bool acquired[XRT_MAX_SWAPCHAIN_IMAGES]
Modified by the default mock implementations of acquire_image and release_image.
Definition: mock_compositor.h:318
Mock implementation of a native compositor.
Definition: mock_compositor.h:28
xrt_result_t(* create_swapchain)(struct mock_compositor *mc, struct mock_compositor_swapchain *mcsc, const struct xrt_swapchain_create_info *info, struct xrt_swapchain **out_xsc)
Optional function pointer for mock compositor, called during xrt_comp_create_swapchain.
Definition: mock_compositor.h:60
xrt_result_t(* release_image)(struct mock_compositor *mc, struct mock_compositor_swapchain *mcsc, uint32_t index)
Optional function pointer, called during xrt_swapchain::release_image.
Definition: mock_compositor.h:279
xrt_result_t(* get_swapchain_create_properties)(struct mock_compositor *mc, const struct xrt_swapchain_create_info *info, struct xrt_swapchain_create_properties *xsccp)
Optional function pointer for mock compositor, called during xrt_comp_get_swapchain_create_properties...
Definition: mock_compositor.h:50
struct mock_compositor::@78 compositor_hooks
Optional function pointers you can populate to hook into the behavior of the mock compositor implemen...
xrt_result_t(* wait_image)(struct mock_compositor *mc, struct mock_compositor_swapchain *mcsc, uint64_t timeout_ns, uint32_t index)
Optional function pointer, called during xrt_swapchain::wait_image.
Definition: mock_compositor.h:271
struct mock_compositor::@79 swapchain_hooks
Optional function pointers you can populate to hook into the behavior of the mock swapchain implement...
void * userdata
Mock users can populate this pointer to use data from hooks.
Definition: mock_compositor.h:35
void(* destroy)(struct mock_compositor *mc)
Optional function pointer for mock compositor, called during xrt_comp_destroy (before actual destruct...
Definition: mock_compositor.h:241
xrt_result_t(* import_swapchain)(struct mock_compositor *mc, struct mock_compositor_swapchain *mcsc, const struct xrt_swapchain_create_info *info, struct xrt_image_native *native_images, uint32_t image_count, struct xrt_swapchain **out_xsc)
Optional function pointer for mock compositor, called during xrt_comp_import_swapchain.
Definition: mock_compositor.h:73
uint32_t next_id
ID for next swapchain.
Definition: mock_compositor.h:32
xrt_result_t(* acquire_image)(struct mock_compositor *mc, struct mock_compositor_swapchain *mcsc, uint32_t *out_index)
Optional function pointer, called during xrt_swapchain::acquire_image.
Definition: mock_compositor.h:264
Compositor fence used for syncornization.
Definition: xrt_compositor.h:731
Main compositor server interface.
Definition: xrt_compositor.h:2225
struct xrt_compositor base
Base.
Definition: xrt_compositor.h:2227
Compositor semaphore used for synchronization, needs to be as capable as a Vulkan pipeline semaphore.
Definition: xrt_compositor.h:788
Common compositor client interface/base.
Definition: xrt_compositor.h:987
xrt_result_t(* layer_projection_depth)(struct xrt_compositor *xc, struct xrt_device *xdev, struct xrt_swapchain *xsc[XRT_MAX_VIEWS], struct xrt_swapchain *d_xsc[XRT_MAX_VIEWS], const struct xrt_layer_data *data)
Adds a projection layer for submission, has depth information.
Definition: xrt_compositor.h:1238
xrt_result_t(* layer_commit)(struct xrt_compositor *xc, xrt_graphics_sync_handle_t sync_handle)
Commits all of the submitted layers.
Definition: xrt_compositor.h:1339
xrt_result_t(* layer_cylinder)(struct xrt_compositor *xc, struct xrt_device *xdev, struct xrt_swapchain *xsc, const struct xrt_layer_data *data)
Adds a cylinder layer for submission.
Definition: xrt_compositor.h:1285
xrt_result_t(* end_session)(struct xrt_compositor *xc)
See xrEndSession, unlike the OpenXR one the state tracker is responsible to call discard frame before...
Definition: xrt_compositor.h:1074
xrt_result_t(* discard_frame)(struct xrt_compositor *xc, int64_t frame_id)
Explicitly discard a frame.
Definition: xrt_compositor.h:1180
xrt_result_t(* begin_frame)(struct xrt_compositor *xc, int64_t frame_id)
See xrBeginFrame.
Definition: xrt_compositor.h:1162
xrt_result_t(* predict_frame)(struct xrt_compositor *xc, int64_t *out_frame_id, uint64_t *out_wake_time_ns, uint64_t *out_predicted_gpu_time_ns, uint64_t *out_predicted_display_time_ns, uint64_t *out_predicted_display_period_ns)
This function and mark_frame function calls are a alternative to wait_frame.
Definition: xrt_compositor.h:1104
xrt_result_t(* layer_quad)(struct xrt_compositor *xc, struct xrt_device *xdev, struct xrt_swapchain *xsc, const struct xrt_layer_data *data)
Adds a quad layer for submission, the center of the quad is specified by the pose and extends outward...
Definition: xrt_compositor.h:1255
xrt_result_t(* wait_frame)(struct xrt_compositor *xc, int64_t *out_frame_id, uint64_t *out_predicted_display_time, uint64_t *out_predicted_display_period)
See xrWaitFrame.
Definition: xrt_compositor.h:1147
xrt_result_t(* mark_frame)(struct xrt_compositor *xc, int64_t frame_id, enum xrt_compositor_frame_point point, uint64_t when_ns)
This function and predict_frame function calls are a alternative to wait_frame.
Definition: xrt_compositor.h:1123
xrt_result_t(* layer_projection)(struct xrt_compositor *xc, struct xrt_device *xdev, struct xrt_swapchain *xsc[XRT_MAX_VIEWS], const struct xrt_layer_data *data)
Adds a projection layer for submissions.
Definition: xrt_compositor.h:1214
xrt_result_t(* layer_equirect1)(struct xrt_compositor *xc, struct xrt_device *xdev, struct xrt_swapchain *xsc, const struct xrt_layer_data *data)
Adds a equirect1 layer for submission.
Definition: xrt_compositor.h:1300
xrt_result_t(* import_fence)(struct xrt_compositor *xc, xrt_graphics_sync_handle_t handle, struct xrt_compositor_fence **out_xcf)
Create a compositor fence from a native sync handle.
Definition: xrt_compositor.h:1032
xrt_result_t(* layer_begin)(struct xrt_compositor *xc, const struct xrt_layer_frame_data *data)
Begins layer submission.
Definition: xrt_compositor.h:1198
struct xrt_compositor_info info
Capabilities and recommended values information.
Definition: xrt_compositor.h:991
xrt_result_t(* begin_session)(struct xrt_compositor *xc, const struct xrt_begin_session_info *info)
See xrBeginSession.
Definition: xrt_compositor.h:1067
xrt_result_t(* layer_equirect2)(struct xrt_compositor *xc, struct xrt_device *xdev, struct xrt_swapchain *xsc, const struct xrt_layer_data *data)
Adds a equirect2 layer for submission.
Definition: xrt_compositor.h:1316
xrt_result_t(* create_semaphore)(struct xrt_compositor *xc, xrt_graphics_sync_handle_t *out_handle, struct xrt_compositor_semaphore **out_xcsem)
Create a compositor semaphore, also returns a native handle.
Definition: xrt_compositor.h:1039
xrt_result_t(* layer_cube)(struct xrt_compositor *xc, struct xrt_device *xdev, struct xrt_swapchain *xsc, const struct xrt_layer_data *data)
Adds a cube layer for submission.
Definition: xrt_compositor.h:1270
xrt_result_t(* layer_commit_with_semaphore)(struct xrt_compositor *xc, struct xrt_compositor_semaphore *xcsem, uint64_t value)
Commits all of the submitted layers, with a semaphore.
Definition: xrt_compositor.h:1351
A single HMD or input device.
Definition: xrt_device.h:230
A single image of a swapchain based on native buffer handles.
Definition: xrt_compositor.h:2157
All the pure data values associated with a composition layer.
Definition: xrt_compositor.h:394
Swapchain creation info.
Definition: xrt_compositor.h:876
Struct used to negotiate properties of a swapchain that is created outside of the compositor.
Definition: xrt_compositor.h:918
Base class for a swapchain that exposes a native buffer handle to be imported into a client API.
Definition: xrt_compositor.h:2192
struct xrt_swapchain base
Base.
Definition: xrt_compositor.h:2194
Common swapchain interface/base.
Definition: xrt_compositor.h:536
Header declaring XRT graphics interfaces.
Common defines and enums for XRT.
Native handle types.
int xrt_graphics_buffer_handle_t
The type underlying buffers shared between compositor clients and the main compositor.
Definition: xrt_handles.h:246
int xrt_graphics_sync_handle_t
The type underlying synchronization primitives (semaphores, etc) shared between compositor clients an...
Definition: xrt_handles.h:348