1 // Copyright 2020 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 //! renderer_utils: Utility functions and structs used by virgl_renderer and gfxstream.
6
7 use std::cell::RefCell;
8 use std::os::raw::c_void;
9 use std::rc::Rc;
10
11 use crate::generated::virgl_renderer_bindings::__va_list_tag;
12 use crate::rutabaga_utils::{RutabagaError, RutabagaResult};
13
14 #[repr(C)]
15 #[derive(Debug, Copy, Clone)]
16 pub struct VirglBox {
17 pub x: u32,
18 pub y: u32,
19 pub z: u32,
20 pub w: u32,
21 pub h: u32,
22 pub d: u32,
23 }
24
25 /*
26 * automatically generated by rust-bindgen
27 * $ bindgen /usr/include/stdio.h \
28 * --no-layout-tests \
29 * --whitelist-function vsnprintf \
30 * -o vsnprintf.rs
31 */
32
33 #[allow(non_snake_case, non_camel_case_types)]
34 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
35 extern "C" {
vsnprintf( __s: *mut ::std::os::raw::c_char, __maxlen: ::std::os::raw::c_ulong, __format: *const ::std::os::raw::c_char, __arg: *mut __va_list_tag, ) -> ::std::os::raw::c_int36 pub fn vsnprintf(
37 __s: *mut ::std::os::raw::c_char,
38 __maxlen: ::std::os::raw::c_ulong,
39 __format: *const ::std::os::raw::c_char,
40 __arg: *mut __va_list_tag,
41 ) -> ::std::os::raw::c_int;
42 }
43
ret_to_res(ret: i32) -> RutabagaResult<()>44 pub fn ret_to_res(ret: i32) -> RutabagaResult<()> {
45 match ret {
46 0 => Ok(()),
47 _ => Err(RutabagaError::ComponentError(ret)),
48 }
49 }
50
51 pub struct FenceState {
52 pub latest_fence: u32,
53 }
54
55 impl FenceState {
write(&mut self, latest_fence: u32)56 pub fn write(&mut self, latest_fence: u32) {
57 if latest_fence > self.latest_fence {
58 self.latest_fence = latest_fence;
59 }
60 }
61 }
62
63 pub struct VirglCookie {
64 pub fence_state: Rc<RefCell<FenceState>>,
65 }
66
write_fence(cookie: *mut c_void, fence: u32)67 pub extern "C" fn write_fence(cookie: *mut c_void, fence: u32) {
68 assert!(!cookie.is_null());
69 let cookie = unsafe { &*(cookie as *mut VirglCookie) };
70
71 // Track the most recent fence.
72 let mut fence_state = cookie.fence_state.borrow_mut();
73 fence_state.write(fence);
74 }
75