JFLib

gpu/vk_dmabuf.rs#

Source: lib/gpu/vk_dmabuf.rs

vk_dmabuf -- the swapchain of a Linux program: images without a VkSurface (programs against glibc;
docs/GPU.md "Ausgabe unter Linux")

Vulkan's Wayland surface wants a pointer from libwayland-client, the X11 one from libxcb.  JFLib
speaks both protocols itself and has neither.  So the GPU draws into images of its own, exported as
dma-bufs, and plat hands them to the window (lib/plat/linux_gpuout.rs).  For the caller this is the
same as vk_swap.rs: Surface, create_swapchain, try_acquire, view, present, rebuild.

  Surface::native(&instance, plat::win_gpu_child(w), w)   what the display of window w accepts
  surface.track_size()              (the window loop's thread) the window's size for a rebuild on a
                                    worker: only that thread may ask plat
  device.create_swapchain(&surface, &SwapchainDesc)       any thread
       min_images images (0: 3), XRGB8888 (else XBGR8888, ARGB8888, ABGR8888) as B8G8R8A8 /
       R8G8B8A8, single-plane modifiers the device can export and the display accepts: the driver
       picks (VkImageDrmFormatModifierListCreateInfoEXT), a dedicated allocation, dma-buf
  sc.try_acquire()                  never waits, never rebuilds; registers the images with plat at
       its first call; looks at the frame before (flush); then NotReady while one is on its way or
       the display has not answered the last commit (the frame stays in the caller's texture and
       its newest state is drawn then: nothing queues up) or no image is free
  sc.present(frame, [cmd, ..])      submits; the image goes to the window when the GPU has finished
  sc.flush() -> still on its way    that look at the timeline: the frame goes to the window (the
       window loop calls it, a frame is rarely done before present returns)
  sc.busy()                         a frame is on its way: flush soon
  sc.next_look(retry)               when a frame that found NotReady should try again
  sc.damage(rects)                  what the next presented image changes against the last one (None:
       all; without any call: all).  What the compositor has to draw again, nothing else
  sc.acquire()                      waits (pumping the display, events stay queued) and rebuilds
  sc.rebuild()                      any thread, without waiting: the old images die with the frames
       that use them

Why the image can never be wrong on the screen: linux_gpuout.rs.  Here: an image goes to plat only
after its submission reached the timeline; an image is drawn into only when plat says FREE and a
poll of its dma-buf for POLLOUT says no fence of another reader is left.  Queue ownership goes to
VK_QUEUE_FAMILY_FOREIGN_EXT at the end of every frame and comes back at the start of the next.
Compression (modifiers with more than one plane) is left out: the window system does not gain
enough from it for the checks each driver wants at the hand-over.

Threads: plat's display calls only on the thread of the window loop (linux_disp.rs).  So a worker's
rebuild and any drop only note the buffers that go (RETIRED), try_acquire and flush free them.
  • struct Surface(pub(crate) Arc<SurfShared>) a window as a target for swapchains

impl Surface

  • fn new(instance: &Instance, window: i32) -> Result<Surface> the surface of plat window window

  • fn native(_instance: &Instance, nw: plat::NativeWindow, window: i32) -> Result<Surface> the surface from what plat::win_gpu_child said about plat window window (any thread)

  • fn track_size(&self) (the window loop's thread) the window's size now: what a rebuild on a worker makes

  • struct Frame an acquired image (present it with Swapchain::present)

  • enum TryAcquire what Swapchain::try_acquire found

    • Frame(Frame)
    • NotReady -- no image is free yet, or the display has not answered the last frame: try again later
    • Stale -- the swapchain does not fit the window any more: Swapchain::rebuild first
    • NoArea -- the window is minimised or has no area

impl Frame

  • fn index(&self) -> u32 the image's index in the swapchain

  • fn size(&self) -> (u32, u32)

  • struct Swapchain the images a window shows, presented in turn

impl Device

  • fn create_swapchain(&self, surface: &Surface, desc: &SwapchainDesc) -> Result<Swapchain>

impl Swapchain

  • fn rebuild(&mut self) -> Result<()> the images for the size the window has now, without waiting (any thread)
  • fn flush(&self) -> Result<bool> the frame on its way: finished by the GPU -> it goes to the window; -> still on its way
  • fn busy(&self) -> bool a frame is on its way to the window: flush soon
  • fn next_look(&self, retry: u64) -> u64 when a frame that found NotReady should try again: soon when the GPU is drawing; when the display's answer is overdue if it has not given it (the answer itself ends the wait earlier); else retry, the caller's time
  • fn prepare(&mut self) -> Result<bool> the images to the display, its answer there (the window loop's thread)
  • fn damage(&self, rects: Option<&[[i32; 4]]>) what the next presented image changes against the last one: rectangles x, y, w, h (None: all)
  • fn try_acquire(&mut self) -> Result<TryAcquire> the next image to draw into, without waiting and without rebuilding (see TryAcquire)
  • fn acquire(&mut self) -> Result<Option<Frame>> the next image to draw into, rebuilt when stale; waits (the display is pumped, events stay queued); None while the window is minimised or has no area
  • fn view(&self, frame: &Frame) -> &TextureView the frame's image as a render target (Undefined at the start, Present at the end)
  • fn texture(&self, frame: &Frame) -> &Texture the frame's image (copies: SwapchainDesc::copy_src)
  • fn present<'a, I: IntoIterator<Item = CommandBuffer<'a>>>(&self, frame: Frame, cmds: I) -> Result<SubmitId> submit the frame's commands (they leave the image in Layout::Present); it goes to the window when the GPU has finished it (flush)
  • fn resize(&mut self) rebuild at the next acquire (the window's size is also checked at every acquire)
  • fn set_present_mode(&mut self, m: PresentMode) another present mode from the next acquire on
  • fn present_mode(&self) -> PresentMode
  • fn format(&self) -> Format
  • fn size(&self) -> (u32, u32) the images' size (0, 0 while the window has no area)
  • fn image_count(&self) -> usize
  • fn drm_format(&self) -> (u32, u64) the images' DRM format and modifier (tests, traces)

Source: docs/API.md in the JFLib repository.