modes/
modes.rs

1// Copyright (c) 2025 nytpu <alex [at] nytpu.com>
2// SPDX-License-Identifier: MPL-2.0
3// For more license details, see LICENSE or <https://www.mozilla.org/en-US/MPL/2.0/>.
4
5//! Example showing usage of an `FbDrawTarget`, by enumerating all supported graphics modes and
6//! printing them using embedded-graphics' text primitives.
7
8#![no_main]
9#![no_std]
10
11extern crate alloc;
12
13use alloc::vec::Vec;
14use core::time::Duration;
15use embedded_graphics::{
16	mono_font::{MonoTextStyle, ascii::FONT_10X20},
17	pixelcolor::Rgb888,
18	prelude::*,
19	text::Text,
20};
21use embedded_graphics_gop::fb::FbDrawTarget;
22use uefi::{helpers, prelude::*, proto::console::gop::GraphicsOutput, runtime::ResetType};
23
24#[entry]
25fn main() -> Status {
26	helpers::init().unwrap();
27
28	let handle =
29		boot::get_handle_for_protocol::<GraphicsOutput>().expect("Unable to get GOP handle");
30	let mut gop =
31		boot::open_protocol_exclusive::<GraphicsOutput>(handle).expect("Unable to open GOP handle");
32
33	let mode_800x600 = gop
34		.modes()
35		// one of the standard modes, although some systems may only provide 640×480
36		.find(|m| m.info().resolution() == (800, 600))
37		.expect("Unable to find 800x600 video mode");
38	gop.set_mode(&mode_800x600)
39		.expect("Couldn't switch to 800x600 video mode");
40
41	let mut formats = Vec::new();
42	for (idx, mode) in gop.modes().enumerate() {
43		let mode = mode.info();
44		let (width, height) = mode.resolution();
45		formats.push(alloc::format!(
46			"Mode {:02}: resolution {}x{}, pixel format {:?}, stride {}",
47			idx,
48			width,
49			height,
50			mode.pixel_format(),
51			mode.stride(),
52		));
53	}
54
55	let mut target = FbDrawTarget::new(&mut gop);
56
57	let font = MonoTextStyle::new(&FONT_10X20, Rgb888::WHITE);
58
59	let mut pos = Point::new(90, 20);
60	for format in &formats {
61		Text::new(format, pos, font)
62			.draw(&mut target)
63			.expect("unable to draw text");
64		pos.y += 15;
65	}
66
67	boot::stall(Duration::from_secs(10));
68	runtime::reset(ResetType::SHUTDOWN, Status::SUCCESS, None);
69}