video_core/
lib.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Common video device-related definitions.
5
6#![forbid(unsafe_code)]
7
8use inspect::Inspect;
9use mesh::MeshPayload;
10use mesh::payload::Protobuf;
11use vm_resource::CanResolveTo;
12use vm_resource::ResourceId;
13use vm_resource::kind::FramebufferHandleKind;
14
15impl CanResolveTo<ResolvedFramebuffer> for FramebufferHandleKind {
16    type Input<'a> = ();
17}
18
19/// A resolved framebuffer.
20pub struct ResolvedFramebuffer(pub Box<dyn FramebufferControl>);
21
22impl<T: 'static + FramebufferControl> From<T> for ResolvedFramebuffer {
23    fn from(value: T) -> Self {
24        Self(Box::new(value))
25    }
26}
27
28/// A handle to the global shared framebuffer that has been mapped with the VM.
29#[derive(MeshPayload)]
30pub struct SharedFramebufferHandle;
31
32impl ResourceId<FramebufferHandleKind> for SharedFramebufferHandle {
33    const ID: &'static str = "shared";
34}
35
36/// The framebuffer memory format.
37#[derive(Debug, Copy, Clone, Protobuf, PartialEq, Eq, Inspect)]
38#[mesh(package = "framebuffer")]
39pub struct FramebufferFormat {
40    /// Width in pixels.
41    #[mesh(1)]
42    pub width: usize,
43    /// Height in pixels.
44    #[mesh(2)]
45    pub height: usize,
46    /// Bytes per scanline.
47    #[mesh(3)]
48    pub bytes_per_line: usize,
49    /// Starting offset.
50    #[mesh(4)]
51    pub offset: usize,
52}
53
54/// Functions necessary to control the framebuffer from a video device.
55///
56/// This trait needs to be async so that an implementation of these functions can be async.
57///
58/// For example, the GET request needed to map the framebuffer from Underhill is async since
59/// the video device needs to wait for a response from the host to send an ack to the guest.
60#[async_trait::async_trait]
61pub trait FramebufferControl: Send {
62    /// Maps the framebuffer to the guest at the specified GPA.
63    async fn map(&mut self, gpa: u64);
64    /// Unmaps the framebuffer from the guest.
65    async fn unmap(&mut self);
66    /// Updates the framebuffer format.
67    async fn set_format(&mut self, format: FramebufferFormat);
68}