1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Common video device-related definitions.

#![forbid(unsafe_code)]
#![warn(missing_docs)]

use inspect::Inspect;
use mesh::payload::Protobuf;
use mesh::MeshPayload;
use vm_resource::kind::FramebufferHandleKind;
use vm_resource::CanResolveTo;
use vm_resource::ResourceId;

impl CanResolveTo<ResolvedFramebuffer> for FramebufferHandleKind {
    type Input<'a> = ();
}

/// A resolved framebuffer.
pub struct ResolvedFramebuffer(pub Box<dyn FramebufferControl>);

impl<T: 'static + FramebufferControl> From<T> for ResolvedFramebuffer {
    fn from(value: T) -> Self {
        Self(Box::new(value))
    }
}

/// A handle to the global shared framebuffer that has been mapped with the VM.
#[derive(MeshPayload)]
pub struct SharedFramebufferHandle;

impl ResourceId<FramebufferHandleKind> for SharedFramebufferHandle {
    const ID: &'static str = "shared";
}

/// The framebuffer memory format.
#[derive(Debug, Copy, Clone, Protobuf, PartialEq, Eq, Inspect)]
#[mesh(package = "framebuffer")]
pub struct FramebufferFormat {
    /// Width in pixels.
    #[mesh(1)]
    pub width: usize,
    /// Height in pixels.
    #[mesh(2)]
    pub height: usize,
    /// Bytes per scanline.
    #[mesh(3)]
    pub bytes_per_line: usize,
    /// Starting offset.
    #[mesh(4)]
    pub offset: usize,
}

/// Functions necessary to control the framebuffer from a video device.
///
/// This trait needs to be async so that an implementation of these functions can be async.
///
/// For example, the GET request needed to map the framebuffer from Underhill is async since
/// the video device needs to wait for a response from the host to send an ack to the guest.
#[async_trait::async_trait]
pub trait FramebufferControl: Send {
    /// Maps the framebuffer to the guest at the specified GPA.
    async fn map(&mut self, gpa: u64);
    /// Unmaps the framebuffer from the guest.
    async fn unmap(&mut self);
    /// Updates the framebuffer format.
    async fn set_format(&mut self, format: FramebufferFormat);
}