Skip to main content

plat/
android_gpu.rs

1// android_gpu -- what the GPU layer and the UI's output through the GPU need of Android
2// (lib/gpu vk_load.rs, vk_swap.rs; lib/ui/gpu_out.rs; docs/GPU.md "Ausgabe unter Android")
3//
4//   dl_open(name) / dl_sym(handle, name) / dl_close(handle)   libvulkan.so at run time: no
5//                                      DT_NEEDED, the program starts on a phone without it
6//   gpu_thread_spawn(stack, f)         a real thread for the output's jobs.  thread_spawn stays
7//                                      None here: the pools and tasks of this target are cooperative
8//                                      and start nothing behind the UI's back; nothing but the
9//                                      output's jobs, which end by themselves, runs on these
10//   win_gpu_child(w)                   the surface of the SurfaceView (ANativeWindow), the analogue
11//                                      of Windows' child window; only for window 0 while nothing
12//                                      else shows on the canvas (tool windows and popups are
13//                                      composed on the CPU, below the surface)
14//   win_gpu_child_later(w)             no surface now, a frame or the host brings it: not a reason
15//                                      to give up, and nothing to poll for
16//   win_gpu_child_show(w, show)        show: the frame is on the surface, the canvas path rests;
17//                                      hide: the frame is on its way to the Bitmap, the host takes
18//                                      the SurfaceView away (the surface goes with it)
19//   win_release_kept(w)                the canvas's copy of the window goes
20//
21// The surface comes and goes with the system (GalleryActivity's SurfaceHolder.Callback, bridge.c):
22// jfgui_gpu_surface(window) on the UI thread.  bridge.c owns the ANativeWindow reference; the
23// VkSurfaceKHR takes its own, so a worker still building on a surface that went away gets errors,
24// not a dangling pointer.
25
26use alloc::boxed::Box;
27use core::{
28    ffi::c_void,
29    sync::atomic::{AtomicBool, AtomicUsize, Ordering},
30};
31
32#[link(name = "dl")]
33unsafe extern "C" {
34    fn dlopen(name: *const u8, flags: i32) -> *mut c_void;
35    fn dlsym(handle: *mut c_void, name: *const u8) -> *mut c_void;
36    fn dlclose(handle: *mut c_void) -> i32;
37}
38
39#[repr(C, align(8))]
40struct PthreadAttr([u64; 8]);
41
42#[link(name = "c")]
43unsafe extern "C" {
44    fn pthread_attr_init(attr: *mut PthreadAttr) -> i32;
45    fn pthread_attr_setstacksize(attr: *mut PthreadAttr, size: usize) -> i32;
46    fn pthread_attr_destroy(attr: *mut PthreadAttr) -> i32;
47    fn pthread_create(out: *mut usize, attr: *const PthreadAttr, start: extern "C" fn(*mut c_void) -> *mut c_void, arg: *mut c_void) -> i32;
48    fn pthread_join(thread: usize, out: *mut *mut c_void) -> i32;
49}
50
51/// open a shared library (a name as the linker searches it, or a path) -> its handle
52pub fn dl_open(name: &str) -> Option<usize> {
53    let mut z = alloc::vec::Vec::with_capacity(name.len() + 1);
54    z.extend_from_slice(name.as_bytes());
55    z.push(0);
56    // SAFETY: z is NUL-terminated and outlives the call; RTLD_NOW (2), local to this library.
57    let h = unsafe { dlopen(z.as_ptr(), 2) };
58    (!h.is_null()).then_some(h as usize)
59}
60
61/// a symbol of the library (name NUL-terminated) -> its address, 0 when missing
62///
63/// # Safety
64/// `handle` came from dl_open and has not been closed: the dynamic linker reads its structures
65/// before it could tell a wrong handle.
66pub unsafe fn dl_sym(handle: usize, name: &[u8]) -> usize {
67    if name.last() != Some(&0) {
68        return 0;
69    }
70    // SAFETY: a handle of dl_open (the caller's promise); name is NUL-terminated.
71    unsafe { dlsym(handle as *mut c_void, name.as_ptr()) as usize }
72}
73
74/// give the library back
75///
76/// # Safety
77/// `handle` came from dl_open and is closed once; nothing uses what dl_sym found in it any more
78/// (the library may be unloaded).
79pub unsafe fn dl_close(handle: usize) {
80    // SAFETY: the caller's promise.
81    unsafe { dlclose(handle as *mut c_void) };
82}
83
84type Start = Box<dyn FnOnce() + Send>;
85
86extern "C" fn thread_main(arg: *mut c_void) -> *mut c_void {
87    // SAFETY: gpu_thread_spawn hands over this box, once.
88    let f = unsafe { Box::from_raw(arg as *mut Start) };
89    rt::abort_on_unwind(f); // feature unwind: a panic that leaves the thread ends the program
90    core::ptr::null_mut()
91}
92
93/// a real thread with stack bytes of stack (the output's jobs) -> its handle
94pub fn gpu_thread_spawn(stack: usize, f: Start) -> Option<super::Thread> {
95    let arg = Box::into_raw(Box::new(f));
96    let mut attr = PthreadAttr([0; 8]);
97    let mut handle = 0usize;
98    // SAFETY: attr is at least as large as bionic's pthread_attr_t and lives across the calls;
99    // thread_main takes the box when the thread started, else it is taken back below.
100    let r = unsafe {
101        pthread_attr_init(&mut attr);
102        pthread_attr_setstacksize(&mut attr, stack);
103        let r = pthread_create(&mut handle, &attr, thread_main, arg as *mut c_void);
104        pthread_attr_destroy(&mut attr);
105        r
106    };
107    if r != 0 {
108        // SAFETY: no thread started, the box is still ours.
109        drop(unsafe { Box::from_raw(arg) });
110        return None;
111    }
112    Some(super::Thread { handle })
113}
114
115pub(super) fn thread_join(handle: usize) {
116    // SAFETY: a thread of gpu_thread_spawn, joined once (Thread::join takes self).
117    unsafe { pthread_join(handle, core::ptr::null_mut()) };
118}
119
120#[link(name = "android")]
121unsafe extern "C" {
122    fn ANativeWindow_acquire(window: usize);
123    fn ANativeWindow_release(window: usize);
124}
125
126/// a target for swapchains: the ANativeWindow of the SurfaceView, with a reference of its own (a
127/// worker may get to it after the surface went)
128#[derive(Debug)]
129pub struct NativeWindow {
130    pub window: usize,
131}
132
133impl Drop for NativeWindow {
134    fn drop(&mut self) {
135        // SAFETY: the reference win_gpu_child took.
136        unsafe { ANativeWindow_release(self.window) };
137    }
138}
139
140// the surface's ANativeWindow, 0 while there is none
141static SURFACE: AtomicUsize = AtomicUsize::new(0);
142
143/// (bridge.c) the surface came (an ANativeWindow) or went (0; nothing uses it when this returns)
144/// -> 1: the frame waits to go to the canvas, the host renders once more
145#[unsafe(no_mangle)]
146pub extern "C" fn jfgui_gpu_surface(window: usize) -> u32 {
147    SURFACE.store(window, Ordering::Release);
148    ASKED.store(false, Ordering::Release);
149    (window == 0 && super::surface_gone()) as u32
150}
151
152// GalleryHost.call -> GalleryActivity.gpuCall: 0 the SurfaceView away, 1 back, 2 .. 5 GPU_STATE_*
153const CALL_GPU: u32 = 60;
154
155fn ask(what: u8) {
156    let _ = super::android_host::call(CALL_GPU, &[what]);
157}
158
159// the SurfaceView was asked to come back, its surface is on the way
160static ASKED: AtomicBool = AtomicBool::new(false);
161
162/// how the output came up: the host keeps it across runs, the program's resolution follows
163pub fn gpu_out_state(state: u32) {
164    ask(state as u8);
165}
166
167/// the surface for window w; None: see win_gpu_child_later
168pub fn win_gpu_child(w: i32) -> Option<NativeWindow> {
169    if w != 0 || !super::windows::only_main() {
170        return None;
171    }
172    match SURFACE.load(Ordering::Acquire) {
173        0 => {
174            if !ASKED.swap(true, Ordering::AcqRel) {
175                ask(1); // (the host wakes the loop when it is there)
176            }
177            None
178        }
179        window => {
180            // SAFETY: bridge.c holds a reference while SURFACE has it, on this thread.
181            unsafe { ANativeWindow_acquire(window) };
182            Some(NativeWindow { window })
183        }
184    }
185}
186
187/// no surface for window w now, but it may come: the host or the next frame wakes the loop
188pub fn win_gpu_child_later(w: i32) -> bool {
189    w != 0 || !super::windows::only_main() || (SURFACE.load(Ordering::Acquire) == 0 && ASKED.load(Ordering::Acquire))
190}
191
192/// the window still shows on its surface alone: nothing else came on the canvas
193pub fn win_gpu_child_alone(w: i32) -> bool {
194    w == 0 && super::windows::only_main()
195}
196
197/// show: the window shows on the surface; hide: on the canvas, the surface goes
198pub fn win_gpu_child_show(w: i32, show: bool) {
199    if w == 0 {
200        super::windows::gpu_shows(show);
201        // (when the system took the surface the SurfaceView stays: the surface comes back with it)
202        if !show && SURFACE.load(Ordering::Acquire) != 0 {
203            ask(0);
204        }
205    }
206}
207
208/// the frame is on the surface: the canvas's copy of the window goes (it comes back whole)
209pub fn win_release_kept(w: i32) {
210    super::windows::release_pixels(w);
211}
212
213/// no modal size loop here
214pub fn win_in_sizemove() -> bool {
215    false
216}
217
218/// one GPU, which drives the display
219pub fn win_display_adapter(_w: i32) -> Option<(u32, u32)> {
220    None
221}
222
223/// no remote sessions here
224pub fn win_remote_session() -> bool {
225    false
226}
227
228// the script command `gpu` (a script's real windows) has nothing to work with here: scripts run on
229// the desktop, this target has its own test (apps/androidGallery/tests/android_gpu.py)
230pub const WT_NOANIM: u32 = 0;
231pub const WT_FROM_POINT: u32 = 0;
232pub fn win_test(_op: u32, _w: i32, _a: [i32; 4]) -> i64 {
233    0
234}
235pub fn win_test_image(_w: i32) -> Option<(i32, i32, alloc::vec::Vec<u8>)> {
236    None
237}
238pub fn win_test_screen(_x: i32, _y: i32, _w: i32, _h: i32) -> Option<alloc::vec::Vec<u8>> {
239    None
240}
241pub fn gpu_wait(_deadline_us: u64) {}