virtio/devices/virtio_gpu/messages/
mod.rs1use kernel::ErrorCode;
6
7pub(crate) mod ctrl_header;
8pub(crate) mod resource_attach_backing;
9pub(crate) mod resource_create_2d;
10pub(crate) mod resource_detach_backing;
11pub(crate) mod resource_flush;
12pub(crate) mod set_scanout;
13pub(crate) mod transfer_to_host_2d;
14
15use super::helpers::copy_to_iter;
16use ctrl_header::{CtrlHeader, CtrlType};
17
18pub trait VirtIOGPUReq {
19 const ENCODED_SIZE: usize;
20 const CTRL_TYPE: CtrlType;
21 type ExpectedResponse;
22
23 fn write_to_byte_iter<'a>(
24 &self,
25 dst: &mut impl Iterator<Item = &'a mut u8>,
26 ) -> Result<(), ErrorCode>;
27}
28
29pub trait VirtIOGPUResp {
30 const ENCODED_SIZE: usize;
31 const EXPECTED_CTRL_TYPE: CtrlType;
32
33 fn from_byte_iter_post_checked_ctrl_header(
34 ctrl_header: CtrlHeader,
35 src: &mut impl Iterator<Item = u8>,
36 ) -> Result<Self, ErrorCode>
37 where
38 Self: Sized;
39
40 fn from_byte_iter_post_ctrl_header(
41 ctrl_header: CtrlHeader,
42 src: &mut impl Iterator<Item = u8>,
43 ) -> Result<Self, ErrorCode>
44 where
45 Self: Sized,
46 {
47 if ctrl_header.ctrl_type == Self::EXPECTED_CTRL_TYPE {
48 Self::from_byte_iter_post_checked_ctrl_header(ctrl_header, src)
49 } else {
50 Err(ErrorCode::INVAL)
51 }
52 }
53
54 #[allow(dead_code)]
55 fn from_byte_iter(src: &mut impl Iterator<Item = u8>) -> Result<Self, ErrorCode>
56 where
57 Self: Sized,
58 {
59 let ctrl_header = CtrlHeader::from_byte_iter(src)?;
60 Self::from_byte_iter_post_ctrl_header(ctrl_header, src)
61 }
62}
63
64#[derive(Debug, Copy, Clone)]
65#[repr(C)]
66pub struct Rect {
67 pub x: u32,
68 pub y: u32,
69 pub width: u32,
70 pub height: u32,
71}
72
73impl Rect {
74 pub const fn empty() -> Self {
75 Rect {
76 x: 0,
77 y: 0,
78 width: 0,
79 height: 0,
80 }
81 }
82
83 pub fn is_empty(&self) -> bool {
84 self.width == 0 && self.height == 0
85 }
86
87 fn write_to_byte_iter<'a>(
88 &self,
89 dst: &mut impl Iterator<Item = &'a mut u8>,
90 ) -> Result<(), ErrorCode> {
91 copy_to_iter(dst, u32::to_le_bytes(self.x).into_iter())?;
95 copy_to_iter(dst, u32::to_le_bytes(self.y).into_iter())?;
96 copy_to_iter(dst, u32::to_le_bytes(self.width).into_iter())?;
97 copy_to_iter(dst, u32::to_le_bytes(self.height).into_iter())?;
98
99 Ok(())
100 }
101}