microsoft/openvmm

Public

mirrored fromhttps://github.com/microsoft/openvmmAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
de3e9e6393c9fef181505015739d81320d340d11

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

guest_test_uefi/src/uefi/splash.rs

81lines · modecode

1// Copyright (C) Microsoft Corporation. All rights reserved.
2
3// NOTE: adapted from uefi-rs sierpinski example code:
4// https://github.com/rust-osdev/uefi-rs/blob/main/uefi-test-runner/examples/sierpinski.rs
5
6use alloc::vec::Vec;
7use core::num::NonZeroU8;
8use uefi::boot;
9use uefi::proto::console::gop::BltOp;
10use uefi::proto::console::gop::BltPixel;
11use uefi::proto::console::gop::BltRegion;
12use uefi::proto::console::gop::GraphicsOutput;
13use uefi::Result;
14
15struct Buffer {
16 width: usize,
17 height: usize,
18 pixels: Vec<BltPixel>,
19}
20
21impl Buffer {
22 /// Create a new `Buffer`.
23 fn new(width: usize, height: usize) -> Self {
24 Buffer {
25 width,
26 height,
27 pixels: vec![BltPixel::new(0, 0, 0); width * height],
28 }
29 }
30
31 /// Get a single pixel.
32 fn pixel(&mut self, x: usize, y: usize) -> Option<&mut BltPixel> {
33 self.pixels.get_mut(y * self.width + x)
34 }
35
36 /// Blit the buffer to the framebuffer.
37 fn blit(&self, gop: &mut GraphicsOutput) -> Result {
38 gop.blt(BltOp::BufferToVideo {
39 buffer: &self.pixels,
40 src: BltRegion::Full,
41 dest: (0, 0),
42 dims: (self.width, self.height),
43 })
44 }
45}
46
47pub struct Splashes(pub NonZeroU8);
48
49pub fn draw_splash(splashes: Splashes) {
50 // The graphic output is not always available.
51 let gop_handle = if let Ok(handle) = boot::get_handle_for_protocol::<GraphicsOutput>() {
52 handle
53 } else {
54 return;
55 };
56 let mut gop = boot::open_protocol_exclusive::<GraphicsOutput>(gop_handle).expect("can get GOP");
57
58 // Create a buffer to draw into.
59 let (resolution_width, resolution_height) = gop.current_mode_info().resolution();
60 let mut buffer = Buffer::new(resolution_width, resolution_height);
61
62 let splashes = splashes.0.get() as usize;
63 let height = resolution_height / splashes;
64 let width = resolution_width / splashes;
65 for s in 0..splashes {
66 // Initialize the buffer with a simple gradient background.
67 for y in s * height..(s + 1) * height {
68 let r = ((y as f32) / ((resolution_height - 1) as f32)) * 255.0;
69 for x in s * width..(s + 1) * width {
70 let g = ((x as f32) / ((resolution_width - 1) as f32)) * 255.0;
71 let pixel = buffer.pixel(x, y).expect("Can draw a pixel");
72 pixel.red = r as u8;
73 pixel.green = g as u8;
74 pixel.blue = 255;
75 }
76 }
77 }
78
79 // Draw the buffer to the screen.
80 buffer.blit(&mut gop).expect("can draw the image");
81}
82