Monado OpenXR Runtime
xrt_compositor.h
Go to the documentation of this file.
1 // Copyright 2019, Collabora, Ltd.
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4  * @file
5  * @brief Header defining a XRT graphics provider.
6  * @author Jakob Bornecrantz <jakob@collabora.com>
7  * @ingroup xrt_iface
8  */
9 
10 #pragma once
11 
12 #include "xrt/xrt_defines.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 
19 /*!
20  * Max swapchain images, artificial limit.
21  *
22  * @ingroup xrt_iface
23  */
24 #define XRT_MAX_SWAPCHAIN_IMAGES 8
25 
26 /*!
27  * Max formats supported by a compositor, artificial limit.
28  *
29  * @ingroup xrt_iface
30  */
31 #define XRT_MAX_SWAPCHAIN_FORMATS 8
32 
33 /*!
34  * Special flags for creating swapchain images.
35  *
36  * @ingroup xrt_iface
37  */
39 {
40  XRT_SWAPCHAIN_CREATE_STATIC_IMAGE = (1 << 0),
41 };
42 
43 /*!
44  * Usage of the swapchain images.
45  *
46  * @ingroup xrt_iface
47  */
49 {
50  XRT_SWAPCHAIN_USAGE_COLOR = 0x00000001,
51  XRT_SWAPCHAIN_USAGE_DEPTH_STENCIL = 0x00000002,
52  XRT_SWAPCHAIN_USAGE_UNORDERED_ACCESS = 0x00000004,
53  XRT_SWAPCHAIN_USAGE_TRANSFER_SRC = 0x00000008,
54  XRT_SWAPCHAIN_USAGE_TRANSFER_DST = 0x00000010,
55  XRT_SWAPCHAIN_USAGE_SAMPLED = 0x00000020,
56  XRT_SWAPCHAIN_USAGE_MUTABLE_FORMAT = 0x00000040
57 };
58 
59 /*!
60  * View type to be rendered to by the compositor.
61  *
62  * @ingroup xrt_iface
63  */
65 {
66  XRT_VIEW_TYPE_MONO = 1,
67  XRT_VIEW_TYPE_STEREO = 2,
68 };
69 
70 /*!
71  * Common swapchain base.
72  *
73  * @ingroup xrt_iface
74  */
76 {
77  /*!
78  * Number of images, the images themselves are on the subclasses.
79  */
80  uint32_t num_images;
81 
82  /*!
83  * Number of array layers per image.
84  */
85  uint32_t array_size;
86 
87  /*!
88  * Must have called release_image before calling this function.
89  */
90  void (*destroy)(struct xrt_swapchain *sc);
91 
92  /*!
93  * See xrWaitSwapchainImage, must make sure that no image is acquired
94  * before calling acquire_image.
95  */
96  bool (*acquire_image)(struct xrt_swapchain *xc, uint32_t *index);
97 
98  /*!
99  * See xrWaitSwapchainImage, state tracker needs to track index.
100  */
101  bool (*wait_image)(struct xrt_swapchain *xc,
102  uint64_t timeout,
103  uint32_t index);
104 
105  /*!
106  * See xrReleaseSwapchainImage, state tracker needs to track index.
107  */
108  bool (*release_image)(struct xrt_swapchain *xc, uint32_t index);
109 };
110 
111 /*!
112  * Common compositor base.
113  *
114  * @ingroup xrt_iface
115  */
117 {
118  /*!
119  * Number of formats.
120  */
121  uint32_t num_formats;
122 
123  /*!
124  * Supported formats.
125  */
126  int64_t formats[XRT_MAX_SWAPCHAIN_FORMATS];
127 
128  /*!
129  * Create a swapchain with a set of images.
130  */
131  struct xrt_swapchain *(*create_swapchain)(
132  struct xrt_compositor *xc,
133  enum xrt_swapchain_create_flags create,
134  enum xrt_swapchain_usage_bits bits,
135  int64_t format,
136  uint32_t sample_count,
137  uint32_t width,
138  uint32_t height,
139  uint32_t face_count,
140  uint32_t array_size,
141  uint32_t mip_count);
142 
143  /*!
144  * Poll events from this compositor.
145  *
146  * This function is very much WIP.
147  */
148  void (*poll_events)(struct xrt_compositor *xc, uint64_t *WIP);
149 
150  /*!
151  * This function is implicit in the OpenXR spec but made explicit here.
152  */
153  void (*prepare_session)(struct xrt_compositor *xc);
154 
155  /*!
156  * See xrBeginSession.
157  */
158  void (*begin_session)(struct xrt_compositor *xc,
159  enum xrt_view_type view_type);
160 
161  /*!
162  * See xrEndSession, unlike the OpenXR one the state tracker is
163  * responsible to call discard frame before calling this function. See
164  * discard_frame.
165  */
166  void (*end_session)(struct xrt_compositor *xc);
167 
168  /*!
169  * See xrWaitFrame.
170  */
171  void (*wait_frame)(struct xrt_compositor *xc,
172  uint64_t *predicted_display_time,
173  uint64_t *predicted_display_period);
174 
175  /*!
176  * See xrBeginFrame.
177  */
178  void (*begin_frame)(struct xrt_compositor *xc);
179 
180  /*!
181  * This isn't in the OpenXR API but is explicit in the XRT interfaces.
182  *
183  * Two calls to xrBeginFrame will cause the state tracker to call.
184  *
185  * ```c
186  * xc->begin_frame(xc)
187  * xc->discard_frame(xc)
188  * xc->begin_frame(xc)
189  * ```
190  */
191  void (*discard_frame)(struct xrt_compositor *xc);
192 
193  /*!
194  * See xrEndFrame.
195  */
196  void (*end_frame)(struct xrt_compositor *xc,
197  enum xrt_blend_mode blend_mode,
198  struct xrt_swapchain **xscs,
199  const uint32_t *image_index,
200  uint32_t *layers,
201  uint32_t num_swapchains);
202 
203  /*!
204  * Teardown the compositor.
205  *
206  * The state tracker must have made sure that no frames or sessions are
207  * currently pending. See discard_frame, end_frame, end_session.
208  */
209  void (*destroy)(struct xrt_compositor *xc);
210 };
211 
212 /*!
213  * Helper for xrt_compositor::create_swapchain
214  *
215  * @ingroup xrt_iface
216  */
217 static inline struct xrt_swapchain *
218 xrt_comp_create_swapchain(struct xrt_compositor *xc,
219  enum xrt_swapchain_create_flags create,
220  enum xrt_swapchain_usage_bits bits,
221  int64_t format,
222  uint32_t sample_count,
223  uint32_t width,
224  uint32_t height,
225  uint32_t face_count,
226  uint32_t array_size,
227  uint32_t mip_count)
228 {
229  return xc->create_swapchain(xc, create, bits, format, sample_count,
230  width, height, face_count, array_size,
231  mip_count);
232 }
233 
234 /*!
235  * Helper for xrt_compositor::poll_events
236  *
237  * @ingroup xrt_iface
238  */
239 static inline void
240 xrt_comp_poll_events(struct xrt_compositor *xc, uint64_t *WIP)
241 {
242  xc->poll_events(xc, WIP);
243 }
244 
245 /*!
246  * Helper for xrt_compositor::prepare_session
247  *
248  * @ingroup xrt_iface
249  */
250 static inline void
251 xrt_comp_prepare_session(struct xrt_compositor *xc)
252 {
253  xc->prepare_session(xc);
254 }
255 
256 /*!
257  * Helper for xrt_compositor::begin_session
258  *
259  * @ingroup xrt_iface
260  */
261 static inline void
262 xrt_comp_begin_session(struct xrt_compositor *xc, enum xrt_view_type view_type)
263 {
264  xc->begin_session(xc, view_type);
265 }
266 
267 /*!
268  * Helper for xrt_compositor::end_session
269  *
270  * @ingroup xrt_iface
271  */
272 static inline void
273 xrt_comp_end_session(struct xrt_compositor *xc)
274 {
275  xc->end_session(xc);
276 }
277 
278 /*!
279  * Helper for xrt_compositor::wait_frame
280  *
281  * @ingroup xrt_iface
282  */
283 static inline void
284 xrt_comp_wait_frame(struct xrt_compositor *xc,
285  uint64_t *predicted_display_time,
286  uint64_t *predicted_display_period)
287 {
288  xc->wait_frame(xc, predicted_display_time, predicted_display_period);
289 }
290 
291 /*!
292  * Helper for xrt_compositor::begin_frame
293  *
294  * @ingroup xrt_iface
295  */
296 static inline void
297 xrt_comp_begin_frame(struct xrt_compositor *xc)
298 {
299  xc->begin_frame(xc);
300 }
301 
302 /*!
303  * Helper for xrt_compositor::discard_frame
304  *
305  * @ingroup xrt_iface
306  */
307 static inline void
308 xrt_comp_discard_frame(struct xrt_compositor *xc)
309 {
310  xc->discard_frame(xc);
311 }
312 
313 /*!
314  * Helper for xrt_compositor::end_frame
315  *
316  * @ingroup xrt_iface
317  */
318 static inline void
319 xrt_comp_end_frame(struct xrt_compositor *xc,
320  enum xrt_blend_mode blend_mode,
321  struct xrt_swapchain **xscs,
322  const uint32_t *image_index,
323  uint32_t *layers,
324  uint32_t num_swapchains)
325 {
326  xc->end_frame(xc, blend_mode, xscs, image_index, layers,
327  num_swapchains);
328 }
329 
330 /*!
331  * Helper for xrt_compositor::destroy, does a null check and sets xc_ptr to
332  * null if freed.
333  *
334  * @ingroup xrt_iface
335  */
336 static inline void
337 xrt_comp_destroy(struct xrt_compositor **xc_ptr)
338 {
339  struct xrt_compositor *xc = *xc_ptr;
340  if (xc == NULL) {
341  return;
342  }
343 
344  xc->destroy(xc);
345  *xc_ptr = NULL;
346 }
347 
348 
349 /*
350  *
351  * OpenGL interface.
352  *
353  */
354 
355 /*!
356  * @ingroup xrt_iface comp_client
357  */
359 {
360  struct xrt_swapchain base;
361 
362  // GLuint
363  unsigned int images[XRT_MAX_SWAPCHAIN_IMAGES];
364  // GLuint
365  unsigned int memory[XRT_MAX_SWAPCHAIN_IMAGES];
366 };
367 
368 /*!
369  * @ingroup xrt_iface comp_client
370  */
372 {
373  struct xrt_compositor base;
374 };
375 
376 static inline struct xrt_swapchain_gl *
377 xrt_swapchain_gl(struct xrt_swapchain *xsc)
378 {
379  return (struct xrt_swapchain_gl *)xsc;
380 }
381 
382 static inline struct xrt_compositor_gl *
384 {
385  return (struct xrt_compositor_gl *)xc;
386 }
387 
388 
389 /*
390  *
391  * Vulkan interface.
392  *
393  */
394 
395 #ifdef XRT_64_BIT
396 typedef struct VkImage_T *VkImage;
397 typedef struct VkDeviceMemory_T *VkDeviceMemory;
398 #else
399 typedef uint64_t VkImage;
400 typedef uint64_t VkDeviceMemory;
401 #endif
402 
403 /*!
404  * Base class for a Vulkan client swapchain.
405  *
406  * @ingroup xrt_iface comp_client
407  */
409 {
410  struct xrt_swapchain base;
411 
412  VkImage images[XRT_MAX_SWAPCHAIN_IMAGES];
413  VkDeviceMemory mems[XRT_MAX_SWAPCHAIN_IMAGES];
414 };
415 
416 /*!
417  * Base class for a Vulkan client compositor.
418  *
419  * @ingroup xrt_iface comp_client
420  */
422 {
423  struct xrt_compositor base;
424 };
425 
426 static inline struct xrt_swapchain_vk *
427 xrt_swapchain_vk(struct xrt_swapchain *xsc)
428 {
429  return (struct xrt_swapchain_vk *)xsc;
430 }
431 
432 static inline struct xrt_compositor_vk *
434 {
435  return (struct xrt_compositor_vk *)xc;
436 }
437 
438 
439 /*
440  *
441  * FD interface, aka DMABUF.
442  *
443  */
444 
445 /*!
446  * A single image of a fd based swapchain.
447  *
448  * @ingroup xrt_iface comp
449  */
451 {
452  size_t size;
453  int fd;
454  int _pad;
455 };
456 
457 /*!
458  * A swapchain that exposes fd to be imported into a client API.
459  *
460  * @ingroup xrt_iface comp
461  */
463 {
464  struct xrt_swapchain base;
465 
466  struct xrt_image_fd images[XRT_MAX_SWAPCHAIN_IMAGES];
467 };
468 
469 /*!
470  * Main compositor.
471  *
472  * @ingroup xrt_iface comp
473  */
475 {
476  struct xrt_compositor base;
477 };
478 
479 static inline struct xrt_swapchain_fd *
480 xrt_swapchain_fd(struct xrt_swapchain *xsc)
481 {
482  return (struct xrt_swapchain_fd *)xsc;
483 }
484 
485 static inline struct xrt_compositor_fd *
487 {
488  return (struct xrt_compositor_fd *)xc;
489 }
490 
491 
492 #ifdef __cplusplus
493 }
494 #endif
void(* begin_session)(struct xrt_compositor *xc, enum xrt_view_type view_type)
See xrBeginSession.
Definition: xrt_compositor.h:158
void(* 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:166
Main compositor.
Definition: xrt_compositor.h:474
Base class for a Vulkan client compositor.
Definition: xrt_compositor.h:421
void(* poll_events)(struct xrt_compositor *xc, uint64_t *WIP)
Poll events from this compositor.
Definition: xrt_compositor.h:148
#define XRT_MAX_SWAPCHAIN_FORMATS
Max formats supported by a compositor, artificial limit.
Definition: xrt_compositor.h:31
A swapchain that exposes fd to be imported into a client API.
Definition: xrt_compositor.h:462
void(* discard_frame)(struct xrt_compositor *xc)
This isn&#39;t in the OpenXR API but is explicit in the XRT interfaces.
Definition: xrt_compositor.h:191
Definition: xrt_compositor.h:358
Base class for a Vulkan client swapchain.
Definition: xrt_compositor.h:408
xrt_swapchain_usage_bits
Usage of the swapchain images.
Definition: xrt_compositor.h:48
bool(* wait_image)(struct xrt_swapchain *xc, uint64_t timeout, uint32_t index)
See xrWaitSwapchainImage, state tracker needs to track index.
Definition: xrt_compositor.h:101
void(* end_frame)(struct xrt_compositor *xc, enum xrt_blend_mode blend_mode, struct xrt_swapchain **xscs, const uint32_t *image_index, uint32_t *layers, uint32_t num_swapchains)
See xrEndFrame.
Definition: xrt_compositor.h:196
Common defines and enums for XRT.
Common swapchain base.
Definition: xrt_compositor.h:75
Definition: xrt_compositor.h:371
xrt_blend_mode
Which blend mode does the device support, used as both a bitfield and value.
Definition: xrt_defines.h:34
void(* wait_frame)(struct xrt_compositor *xc, uint64_t *predicted_display_time, uint64_t *predicted_display_period)
See xrWaitFrame.
Definition: xrt_compositor.h:171
void(* prepare_session)(struct xrt_compositor *xc)
This function is implicit in the OpenXR spec but made explicit here.
Definition: xrt_compositor.h:153
uint32_t array_size
Number of array layers per image.
Definition: xrt_compositor.h:85
Common compositor base.
Definition: xrt_compositor.h:116
#define XRT_MAX_SWAPCHAIN_IMAGES
Max swapchain images, artificial limit.
Definition: xrt_compositor.h:24
void(* destroy)(struct xrt_compositor *xc)
Teardown the compositor.
Definition: xrt_compositor.h:209
xrt_view_type
View type to be rendered to by the compositor.
Definition: xrt_compositor.h:64
void(* destroy)(struct xrt_swapchain *sc)
Must have called release_image before calling this function.
Definition: xrt_compositor.h:90
bool(* release_image)(struct xrt_swapchain *xc, uint32_t index)
See xrReleaseSwapchainImage, state tracker needs to track index.
Definition: xrt_compositor.h:108
bool(* acquire_image)(struct xrt_swapchain *xc, uint32_t *index)
See xrWaitSwapchainImage, must make sure that no image is acquired before calling acquire_image...
Definition: xrt_compositor.h:96
struct xrt_swapchain *(* create_swapchain)(struct xrt_compositor *xc, enum xrt_swapchain_create_flags create, enum xrt_swapchain_usage_bits bits, int64_t format, uint32_t sample_count, uint32_t width, uint32_t height, uint32_t face_count, uint32_t array_size, uint32_t mip_count)
Create a swapchain with a set of images.
Definition: xrt_compositor.h:131
uint32_t num_formats
Number of formats.
Definition: xrt_compositor.h:121
xrt_swapchain_create_flags
Special flags for creating swapchain images.
Definition: xrt_compositor.h:38
uint32_t num_images
Number of images, the images themselves are on the subclasses.
Definition: xrt_compositor.h:80
void(* begin_frame)(struct xrt_compositor *xc)
See xrBeginFrame.
Definition: xrt_compositor.h:178
A single image of a fd based swapchain.
Definition: xrt_compositor.h:450