double_buffered_framebuffer/
double_buffered_framebuffer.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 a double-buffered `FbDbDrawTarget`, with other features like
6//! `discard_changes`.
7
8#![no_main]
9#![no_std]
10
11use core::time::Duration;
12use embedded_graphics::{
13	pixelcolor::Rgb888,
14	prelude::*,
15	primitives::{Circle, PrimitiveStyle, PrimitiveStyleBuilder, Rectangle},
16};
17use embedded_graphics_gop::fb::FbDbDrawTarget;
18use uefi::{helpers, prelude::*, proto::console::gop::GraphicsOutput, runtime::ResetType};
19
20#[entry]
21fn main() -> Status {
22	helpers::init().unwrap();
23
24	let handle =
25		boot::get_handle_for_protocol::<GraphicsOutput>().expect("Unable to get GOP handle");
26	let mut gop =
27		boot::open_protocol_exclusive::<GraphicsOutput>(handle).expect("Unable to open GOP handle");
28
29	let mode_800x600 = gop
30		.modes()
31		// one of the standard modes, although some systems may only provide 640×480
32		.find(|m| m.info().resolution() == (800, 600))
33		.expect("Unable to find 800x600 video mode");
34	gop.set_mode(&mode_800x600)
35		.expect("Couldn't switch to 800x600 video mode");
36
37	let mut target = FbDbDrawTarget::new(&mut gop);
38
39	let center = Point::new(400, 300);
40
41	let white_fill = PrimitiveStyle::with_fill(Rgb888::WHITE);
42	let (red_outline, green_outline, blue_outline) = {
43		let base = PrimitiveStyleBuilder::new().stroke_width(10);
44		(
45			base.stroke_color(Rgb888::RED).build(),
46			base.stroke_color(Rgb888::GREEN).build(),
47			base.stroke_color(Rgb888::BLUE).build(),
48		)
49	};
50
51	Circle::with_center(center, 500)
52		.into_styled(red_outline)
53		.draw(&mut target)
54		.expect("infallible");
55
56	Circle::with_center(center, 400)
57		.into_styled(green_outline)
58		.draw(&mut target)
59		.expect("infallible");
60
61	target.commit();
62
63	// Would cover circles
64	Rectangle::with_center(Point::new(50, 50), Size::new(700, 500))
65		.into_styled(white_fill)
66		.draw(&mut target)
67		.expect("infallible");
68
69	target.discard_changes();
70
71	Circle::with_center(center, 300)
72		.into_styled(blue_outline)
73		.draw(&mut target)
74		.expect("infallible");
75
76	// should auto-commit
77	drop(target);
78
79	boot::stall(Duration::from_secs(10));
80	runtime::reset(ResetType::SHUTDOWN, Status::SUCCESS, None);
81}