···11+Illustrate various control flows for swapchain image allocation, including interaction with IPC.
+2-1
doc/ipc-design.md
···222222a @ref xrt_image_native_allocator (aka XINA) and shares it to the server. When
223223using D3D11 or D3D12 on Windows, buffers are allocated by the client compositor
224224and imported into the native compositor, because Vulkan can import buffers from
225225-D3D, but D3D cannot import buffers allocated by Vulkan.
225225+D3D, but D3D cannot import buffers allocated by Vulkan. See @ref swapchains-ipc
226226+for details.
226227227228[SCM_RIGHTS]: https://man7.org/linux/man-pages/man3/cmsg.3.html
228229[win32handles]: https://lackingrhoticity.blogspot.com/2015/05/passing-fds-handles-between-processes.html
+16
doc/mermaid/swapchain_allocation_inproc.mmd
···11+%% Copyright 2024, Collabora, Ltd. and the Monado contributors
22+%% SPDX-License-Identifier: BSL-1.0
33+44+%% Simple in-process case
55+sequenceDiagram
66+ participant app
77+ participant cc as client compositor
88+ participant native_comp as xrt_compositor_native
99+1010+1111+ app->>+cc: xrCreateSwapchain
1212+ cc->>+native_comp: xrt_comp_create_swapchain
1313+ native_comp->>-cc: xrt_swapchain impl
1414+ Note over cc: Keep reference to inner xrt_swapchain in<br>the object we create
1515+ Note over cc: Import handles from<br/>inner xrt_swapchain into client API
1616+ cc->>-app: return swapchain
+25
doc/mermaid/swapchain_allocation_ipc.mmd
···11+%% Copyright 2024, Collabora, Ltd. and the Monado contributors
22+%% SPDX-License-Identifier: BSL-1.0
33+44+%% Out of process, typical behavior (server-side allocation)
55+sequenceDiagram
66+ box Client Process
77+ participant app
88+ participant cc as client compositor
99+ participant client_native as client process<br/>native compositor<br/>(IPC Stub)
1010+ end
1111+ box Server Process
1212+ participant server_ipc_handler as Server IPC handler
1313+ participant server_comp as Server xrt_compositor_native
1414+ end
1515+1616+ app->>+cc: xrCreateSwapchain
1717+ cc->>+client_native: xrt_comp_create_swapchain
1818+ client_native->>+server_ipc_handler: xrt_comp_create_swapchain<br/>(IPC call)
1919+ server_ipc_handler->>+server_comp: xrt_comp_create_swapchain
2020+ server_comp->>-server_ipc_handler: xrt_swapchain impl
2121+ server_ipc_handler->>-client_native: swapchain ID and<br/>image handles<br/>(over IPC)
2222+ client_native->>-cc: return ipc_swapchain<br/>as inner xrt_swapchain
2323+ Note over cc: Keep reference to inner xrt_swapchain in<br>the object we create
2424+ Note over cc: Import handles from<br/>inner xrt_swapchain into client API
2525+ cc->>-app: return swapchain
+27
doc/mermaid/swapchain_allocation_ipc_d3d.mmd
···11+%% Copyright 2024, Collabora, Ltd. and the Monado contributors
22+%% SPDX-License-Identifier: BSL-1.0
33+44+%% Out of process, D3D client API
55+sequenceDiagram
66+ box Client Process
77+ participant app
88+ participant cc as D3D client compositor
99+ participant client_native as client process<br/>native compositor<br/>(IPC Stub)
1010+ end
1111+ box Server Process
1212+ participant server_ipc_handler as Server IPC handler
1313+ participant server_comp as Server xrt_compositor_native
1414+ end
1515+1616+ app->>+cc: xrCreateSwapchain
1717+ Note over cc: create images<br/>(because d3d cannot reliably import from Vulkan)
1818+ Note over cc: export images to handles
1919+ Note over cc: import handles to images for app
2020+ cc->>+client_native: xrt_comp_import_swapchain(xrt_image_native[])<br/>(import handles into xrt_swapchain_native)
2121+ client_native->>+server_ipc_handler: swapchain_import<br/>(IPC call, copies handles)
2222+ server_ipc_handler->>+server_comp: xrt_comp_import_swapchain(xrt_image_native[])
2323+ server_comp->>-server_ipc_handler: xrt_swapchain impl
2424+ server_ipc_handler->>-client_native: swapchain ID (over IPC)
2525+ client_native->>-cc: return ipc_swapchain<br/>as inner xrt_swapchain
2626+ Note over cc: Keep reference to inner xrt_swapchain in<br>the object we create
2727+ cc->>-app: return swapchain
+28
doc/mermaid/swapchain_allocation_ipc_xina.mmd
···11+%% Copyright 2024, Collabora, Ltd. and the Monado contributors
22+%% SPDX-License-Identifier: BSL-1.0
33+44+%% Out of process, with a "xina" in the IPC client (currently only android with AHB)
55+sequenceDiagram
66+ box Client Process
77+ participant app
88+ participant cc as client compositor
99+ participant client_native as client process<br/>native compositor<br>(IPC Stub)
1010+ participant xina as xrt_image_native_allocator
1111+ end
1212+ box Server Process
1313+ participant server_ipc_handler as Server IPC handler
1414+ participant server_comp as Server xrt_compositor_native
1515+ end
1616+1717+ app->>+cc: xrCreateSwapchain
1818+ cc->>+client_native: xrt_comp_create_swapchain
1919+ client_native->>+xina: create native images
2020+ xina->>-client_native: collection of<br/>native image handles
2121+ client_native->>+server_ipc_handler: swapchain_import<br/>(IPC call, passing handles)
2222+ server_ipc_handler->>+server_comp: xrt_comp_import_swapchain
2323+ server_comp->>-server_ipc_handler: xrt_swapchain impl
2424+ server_ipc_handler->>-client_native: swapchain ID<br/>(over IPC)
2525+ client_native->>-cc: return ipc_swapchain<br>(handles allocated in client process)<br>as inner xrt_swapchain
2626+ Note over cc: Keep reference to inner xrt_swapchain in<br>the object we create
2727+ Note over cc: Import handles from<br/>inner xrt_swapchain into client API
2828+ cc->>-app: return swapchain
+61
doc/swapchains-ipc.md
···11+# Swapchain Allocation and IPC {#swapchains-ipc}
22+33+<!--
44+Copyright 2024, Collabora, Ltd. and the Monado contributors
55+SPDX-License-Identifier: BSL-1.0
66+-->
77+88+The control flow in Monado for allocating the images/buffers used for a given
99+@ref XrSwapchain can vary widely based on a number of factors.
1010+1111+## Simple in-process
1212+1313+In the minimal case, which is not common for widespread deployment but which is
1414+useful for debugging, there is only a single process, with the entire runtime
1515+loaded into the "client" application. Despite the absence of IPC in this
1616+scenario, the same basic flow and interaction applies: images are conveyed using
1717+system ("native") handles.
1818+1919+@mermaid{swapchain_allocation_inproc}
2020+2121+The control flow makes its way to the main compositor, which, in default
2222+implementations, uses Vulkan to allocate exportable images.
2323+2424+## Simple out-of-process
2525+2626+The usual model on desktop is that the images for a given @ref XrSwapchain are
2727+allocated in the service, as shown below.
2828+2929+@mermaid{swapchain_allocation_ipc}
3030+3131+However, there are two additional paths possible.
3232+3333+## Client-side Allocation with XINA (Out-of-process)
3434+3535+In some builds, a "XINA" (like the warrior princess) is used: @ref
3636+xrt_image_native_allocator. This allocates the "native" (system handle) images
3737+within the client process, in a central location. Currently only Android builds
3838+using AHardwareBuffer use this model, but ideally we would probably move toward
3939+this model for all systems. The main advantage is that the swapchain images are
4040+allocated in the application process and thus counted against application
4141+quotas, rather than the service/runtime quotas, avoiding security, privacy, and
4242+denial-of-service risks.
4343+4444+@mermaid{swapchain_allocation_ipc_xina}
4545+4646+Generally a XINA used in this way is intended for client-process allocation.
4747+However, there is an implementation (disabled by default) of a "loopback XINA"
4848+in the IPC client, which implements the XINA API using the IPC interface. So
4949+using a XINA in that case would not result in client-process image allocation.
5050+5151+## Allocation for D3D client apps (Out-of-process)
5252+5353+Direct3D cannot reliably and portably import images allocated in Vulkan. So, if
5454+an application uses Direct3D, the D3D client compositor does the allocation, on
5555+the client side, as shown below. The example shown is out-of-process.
5656+5757+@mermaid{swapchain_allocation_ipc_d3d}
5858+5959+Note that this pattern is used even when running in-process with D3D, the result
6060+is a hybrid of this diagram and the first (in-process) diagram, dropping the
6161+client process native compositor and server IPC handler from the diagram.