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::os::raw::{c_int, c_void};
8 use std::panic::catch_unwind;
9 use std::process::abort;
10
11 use base::{IntoRawDescriptor, SafeDescriptor};
12
13 use crate::rutabaga_utils::{
14 RutabagaError, RutabagaFence, RutabagaFenceHandler, RutabagaResult, RUTABAGA_FLAG_FENCE,
15 };
16
17 #[cfg(feature = "gfxstream")]
18 use crate::rutabaga_utils::RUTABAGA_FLAG_INFO_RING_IDX;
19
20 #[repr(C)]
21 #[derive(Debug, Copy, Clone)]
22 pub struct VirglBox {
23 pub x: u32,
24 pub y: u32,
25 pub z: u32,
26 pub w: u32,
27 pub h: u32,
28 pub d: u32,
29 }
30
ret_to_res(ret: i32) -> RutabagaResult<()>31 pub fn ret_to_res(ret: i32) -> RutabagaResult<()> {
32 match ret {
33 0 => Ok(()),
34 _ => Err(RutabagaError::ComponentError(ret)),
35 }
36 }
37
38 pub struct VirglCookie {
39 pub render_server_fd: Option<SafeDescriptor>,
40 pub fence_handler: Option<RutabagaFenceHandler>,
41 }
42
write_fence(cookie: *mut c_void, fence: u32)43 pub unsafe extern "C" fn write_fence(cookie: *mut c_void, fence: u32) {
44 catch_unwind(|| {
45 assert!(!cookie.is_null());
46 let cookie = &*(cookie as *mut VirglCookie);
47
48 // Call fence completion callback
49 if let Some(handler) = &cookie.fence_handler {
50 handler.call(RutabagaFence {
51 flags: RUTABAGA_FLAG_FENCE,
52 fence_id: fence as u64,
53 ctx_id: 0,
54 ring_idx: 0,
55 });
56 }
57 })
58 .unwrap_or_else(|_| abort())
59 }
60
61 #[cfg(feature = "gfxstream")]
write_context_fence( cookie: *mut c_void, fence_id: u64, ctx_id: u32, ring_idx: u8, )62 pub extern "C" fn write_context_fence(
63 cookie: *mut c_void,
64 fence_id: u64,
65 ctx_id: u32,
66 ring_idx: u8,
67 ) {
68 catch_unwind(|| {
69 assert!(!cookie.is_null());
70 let cookie = unsafe { &*(cookie as *mut VirglCookie) };
71
72 // Call fence completion callback
73 if let Some(handler) = &cookie.fence_handler {
74 handler.call(RutabagaFence {
75 flags: RUTABAGA_FLAG_FENCE | RUTABAGA_FLAG_INFO_RING_IDX,
76 fence_id,
77 ctx_id,
78 ring_idx,
79 });
80 }
81 })
82 .unwrap_or_else(|_| abort())
83 }
84
85 #[allow(dead_code)]
get_server_fd(cookie: *mut c_void, version: u32) -> c_int86 pub unsafe extern "C" fn get_server_fd(cookie: *mut c_void, version: u32) -> c_int {
87 catch_unwind(|| {
88 assert!(!cookie.is_null());
89 let cookie = &mut *(cookie as *mut VirglCookie);
90
91 if version != 0 {
92 return -1;
93 }
94
95 // Transfer the fd ownership to virglrenderer.
96 cookie
97 .render_server_fd
98 .take()
99 .map(SafeDescriptor::into_raw_descriptor)
100 .unwrap_or(-1)
101 })
102 .unwrap_or_else(|_| abort())
103 }
104