gpu_display/
gpu_display_android_stub.rs

1// Copyright 2024 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Stub implementation of the native interface of libcrosvm_android_display_client
6//!
7//! This implementation is used to enable the gpu display backend for Android to be compiled
8//! without libcrosvm_android_display_client available. It is only used for testing purposes and
9//! not functional at runtime. Buffer imports are emulated so that unit tests can check how the
10//! backend manages imported buffers.
11
12use std::cell::Cell;
13use std::cell::RefCell;
14use std::ffi::c_char;
15
16use crate::gpu_display_android::AHardwareBuffer;
17use crate::gpu_display_android::AHardwareBufferInfo;
18use crate::gpu_display_android::ANativeWindow_Buffer;
19use crate::gpu_display_android::AndroidDisplayContext;
20use crate::gpu_display_android::AndroidDisplaySurface;
21use crate::gpu_display_android::ErrorCallback;
22
23thread_local! {
24    static IMPORTED_BUFFERS: Cell<usize> = const { Cell::new(0) };
25    static LIVE_IMPORTED_BUFFERS: Cell<usize> = const { Cell::new(0) };
26    static FLIPPED_BUFFERS: RefCell<Vec<usize>> = const { RefCell::new(Vec::new()) };
27}
28
29/// Returns how many buffers were imported on the current thread.
30#[cfg(test)]
31pub(crate) fn imported_buffer_count() -> usize {
32    IMPORTED_BUFFERS.with(Cell::get)
33}
34
35/// Returns how many buffers imported on the current thread are not released yet.
36#[cfg(test)]
37pub(crate) fn live_imported_buffers() -> usize {
38    LIVE_IMPORTED_BUFFERS.with(Cell::get)
39}
40
41/// Returns the addresses of the buffers flipped to on the current thread, in order.
42#[cfg(test)]
43pub(crate) fn flipped_buffers() -> Vec<usize> {
44    FLIPPED_BUFFERS.with(|flipped| flipped.borrow().clone())
45}
46
47/// Resets the buffer tracking of the current thread, which tests may share (`--test-threads=1`).
48#[cfg(test)]
49pub(crate) fn reset_test_state() {
50    IMPORTED_BUFFERS.with(|count| count.set(0));
51    LIVE_IMPORTED_BUFFERS.with(|count| count.set(0));
52    FLIPPED_BUFFERS.with(|flipped| flipped.borrow_mut().clear());
53}
54
55#[no_mangle]
56extern "C" fn create_android_display_context(
57    _name: *const c_char,
58    _error_callback: ErrorCallback,
59) -> *mut AndroidDisplayContext {
60    unimplemented!();
61}
62
63#[no_mangle]
64extern "C" fn destroy_android_display_context(_ctx: *mut AndroidDisplayContext) {
65    unimplemented!();
66}
67
68#[no_mangle]
69extern "C" fn create_android_surface(
70    _ctx: *mut AndroidDisplayContext,
71    _width: u32,
72    _height: u32,
73    _for_cursor: bool,
74) -> *mut AndroidDisplaySurface {
75    unimplemented!();
76}
77
78#[no_mangle]
79extern "C" fn destroy_android_surface(
80    _ctx: *mut AndroidDisplayContext,
81    _surface: *mut AndroidDisplaySurface,
82) {
83    unimplemented!();
84}
85
86#[no_mangle]
87extern "C" fn set_android_surface_position(_ctx: *mut AndroidDisplayContext, _x: u32, _y: u32) {
88    unimplemented!();
89}
90
91#[no_mangle]
92extern "C" fn get_android_surface_buffer(
93    _ctx: *mut AndroidDisplayContext,
94    _surface: *mut AndroidDisplaySurface,
95    _out_buffer: *mut ANativeWindow_Buffer,
96) -> u32 {
97    unimplemented!();
98}
99
100#[no_mangle]
101extern "C" fn post_android_surface_buffer(
102    _ctx: *mut AndroidDisplayContext,
103    _surface: *mut AndroidDisplaySurface,
104) {
105    unimplemented!();
106}
107
108#[no_mangle]
109extern "C" fn android_display_flip_to(
110    _ctx: *mut AndroidDisplayContext,
111    _surface: *mut AndroidDisplaySurface,
112    _ahb_info: *const AHardwareBufferInfo,
113) {
114    unimplemented!();
115}
116
117#[no_mangle]
118extern "C" fn android_display_import_buffer(
119    _ctx: *mut AndroidDisplayContext,
120    _ahb_info: *const AHardwareBufferInfo,
121) -> *mut AHardwareBuffer {
122    IMPORTED_BUFFERS.with(|count| count.set(count.get() + 1));
123    LIVE_IMPORTED_BUFFERS.with(|count| count.set(count.get() + 1));
124    Box::into_raw(Box::new(0u8)).cast()
125}
126
127#[no_mangle]
128extern "C" fn android_display_flip_to_imported(
129    _ctx: *mut AndroidDisplayContext,
130    _surface: *mut AndroidDisplaySurface,
131    ahb: *mut AHardwareBuffer,
132) {
133    FLIPPED_BUFFERS.with(|flipped| flipped.borrow_mut().push(ahb as usize));
134}
135
136#[no_mangle]
137extern "C" fn android_display_release_buffer(ahb: *mut AHardwareBuffer) {
138    // SAFETY: `ahb` was allocated by `android_display_import_buffer` and is released once.
139    drop(unsafe { Box::from_raw(ahb.cast::<u8>()) });
140    LIVE_IMPORTED_BUFFERS.with(|count| count.set(count.get() - 1));
141}