JFLib

plat/linux_gpuout.rs (Linux)#

Source: lib/plat/linux_gpuout.rs

linux_gpuout -- what lib/gpu's images need from the window system: dmabuf buffers on a window,
through JFLib's own Wayland and X11 code (programs against glibc only; docs/GPU.md "Ausgabe unter
Linux").  No libwayland, no libxcb: Vulkan's surfaces would want a pointer from them, so the GPU
draws into images of its own and this code hands them to the window.

  win_gpu_child(w) -> NativeWindow   what the display accepts for window w (format + modifier
                                     pairs, its render device) and the window's size: None = the
                                     CPU path.  The name is the Windows presenter's, whose child
                                     window this stands for; a Linux window needs no child: one
                                     wl_surface or X window takes shared memory and dmabufs in turn
  win_gpu_child_show, win_release_kept, win_in_sizemove, win_display_adapter, win_remote_session
                                     nothing to do or know here
  gpu_buf_new(w, &GpuBufDesc) -> handle     a buffer of window w from its planes (takes the fds);
                                     0: refused.  On Wayland its answer comes later: PENDING first
  gpu_buf_state(w, handle) -> GPU_BUF_*     FREE: nobody reads it, it is not the one on the window
  gpu_buf_free(w, handle)            the buffer goes (also while it is on the window: what it shows
                                     stays, as long as nobody writes the memory)
  gpu_can_show(w) -> bool            a new frame is welcome now: the last commit has been answered
                                     (the frame callback, PresentCompleteNotify; 1 s at most)
  gpu_show(w, handle, rects)         the buffer, finished by the GPU, goes on the window (at once, or
                                     as soon as the window can take it); rects: what changed against
                                     the last one, None = all.  From now on window w shows through
                                     buffers, until the next win_present.
  gpu_next_look(w) -> time_us        when a frame that cannot go on yet should look again; the wait
                                     returns earlier when the answer comes
  gpu_wait(deadline_us)              pump the display until the deadline, also with events queued
                                     (they stay queued; a plain wait would not read the socket)

Only the thread of the window loop calls these (the backends are single-threaded: linux_disp.rs).

No wrong frame can show:
  * a buffer goes to the window only after the GPU has finished it (lib/gpu: Swapchain::flush
    looks at the timeline without waiting).  So nothing depends on implicit sync: at the attach no
    fence is pending on the buffer, on any compositor, kernel or driver.
  * a buffer is drawn into only after the compositor gave it back (wl_buffer.release,
    PresentIdleNotify) and no read of it is pending on its dma-buf (poll POLLOUT: the compositor's
    GPU may still be reading it when the release arrives), and never while it is the newest.
  * win_present ends the buffers' turn: a frame still on its way is dropped, shared memory shows
    the whole frame.
Test aids: win_test_image(w) reads what the window system holds where it can (X11: GetImage).
/// a buffer's state (gpu_buf_state)
GPU_BUF_PENDING: u32 = 0
GPU_BUF_FREE: u32 = 1
GPU_BUF_BUSY: u32 = 2
GPU_BUF_FAILED: u32 = 3
  • struct GpuCaps what a display accepts as buffers

    • device: Option<u64> -- the device it renders with (dev_t of a node of the DRM device), when it says so
    • formats: Vec<(u32, u64)> -- DRM fourcc and modifier
  • struct GpuPlane a plane of a buffer: a dma-buf (owned: closed once sent), its offset and row pitch

    • fd: i32
    • offset: u32
    • stride: u32
  • struct GpuBufDesc a buffer of memory the GPU drew into

    • w: i32
    • h: i32
    • fourcc: u32
    • modifier: u64
    • planes: Vec<GpuPlane>
  • struct NativeWindow the Linux form of the handles lib/gpu gets for a window (Windows: HWND, HINSTANCE)

    • caps: GpuCaps
    • size: (i32, i32)
  • fn win_gpu_child(w: i32) -> Option<NativeWindow>

  • fn win_gpu_child_show(_w: i32, _show: bool)

  • fn win_release_kept(_w: i32)

  • fn win_gpu_child_later(_w: i32) -> bool no child now, but it may come (the host or the next frame wakes the loop): only Android

  • fn win_gpu_child_alone(_w: i32) -> bool the window still shows on its child alone: here always

  • fn gpu_out_state(_state: u32) how the output came up (GPU_STATE_*): Android keeps it across runs

  • fn gpu_thread_spawn(stack: usize, f: alloc::boxed::Box<dyn FnOnce() + Send>) -> Option<crate::Thread> a thread for the output's jobs

  • fn win_in_sizemove() -> bool

  • fn win_display_adapter(_w: i32) -> Option<(u32, u32)> the GPU that drives the window's monitor: not known here, the display's device decides (GpuCaps)

  • fn win_remote_session() -> bool

  • fn gpu_buf_new(w: i32, d: &GpuBufDesc) -> u32

  • fn gpu_buf_state(w: i32, h: u32) -> u32

  • fn gpu_buf_free(w: i32, h: u32)

  • fn gpu_can_show(w: i32) -> bool

  • fn gpu_show(w: i32, h: u32, rects: Option<&[[i32; 4]]>)

  • fn gpu_next_look(w: i32) -> u64

  • fn gpu_wait(deadline_us: u64)

test aids, as on Windows

WT_PIXEL: u32 = 1
WT_REDRAW: u32 = 2
WT_MOVE: u32 = 3
WT_DPI: u32 = 4
WT_POPUP_RECT: u32 = 7
WT_NOANIM: u32 = 16
WT_FROM_POINT: u32 = 17
WT_SHOW: u32 = 18
  • fn win_test(op: u32, w: i32, a: [i32; 4]) -> i64 (tests)

    WT_PIXEL      the colour the window system holds at client pixel a[0], a[1] of window w: X11
                  GetImage, Wayland the last shared memory buffer; -1: no way to know (Wayland on
                  GPU buffers: a client cannot read a window back)
    WT_MOVE       a[0], a[1], a[2], a[3]: the window's rectangle; win_set_rect reports nothing back
                  here, the event a size change has on Windows comes from this aid
    WT_DPI        a[0]: the dpi as if the compositor preferred that scale (Wayland with a viewport,
                  else -1: X11 has one dpi for the display and no second size)
    WT_SHOW       a[0] = 7 minimised, 4 back: X11 through the window manager; Wayland can minimise, but
                  no client can undo that: 4 does what the compositor's next activation does here
    WT_POPUP_RECT popup slot w is shown: 1, else -1
    WT_NOANIM, WT_FROM_POINT  nothing to turn off; nobody tells a client what lies on top of its
                  window: it counts as ours
  • fn win_test_image(w: i32) -> Option<(i32, i32, Vec<u8>)> (tests) what the window system holds for window w: (w, h, rows of w * 4 bytes): X11 reads it back (GetImage; also a buffer on the window), Wayland has no way to: None

  • fn win_test_composed(_w: i32) -> Option<(i32, i32, Vec<u8>)> (tests) window w as the system composes it: Windows only (PrintWindow): None

  • fn win_test_screen(_x: i32, _y: i32, _w: i32, _h: i32) -> Option<Vec<u8>> (tests) a part of the screen: no client on Linux can read it without asking the user

  • fn win_test_send(_w: i32, _msg: u32, _wp: usize, _lp: isize) -> isize (tests) nothing to send to: Windows' messages

  • fn win_test_usage() -> (u64, u64, u64) (tests) (CPU time of this process so far in us, its resident bytes, its anonymous resident bytes)

  • fn gpu_test_presented() -> Vec<(u64, u64)> (bench: jfgui_WL_FEEDBACK=1 on Wayland) the commits since the last call and when each reached the screen (wp_presentation): (time of the commit, time on the screen; 0: discarded), time_us

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