FbDrawTarget

Struct FbDrawTarget 

Source
pub struct FbDrawTarget<'proto> { /* private fields */ }
Expand description

A single-buffered framebuffer DrawTarget.

See the module-level documentation for more information on the framebuffer DrawTargets.

§Examples

use embedded_graphics_gop::fb::FbDrawTarget;
use uefi::{prelude::*, proto::console::gop::GraphicsOutput};

// Get the first available handle for the GOP.
let handle = boot::get_handle_for_protocol::<GraphicsOutput>().unwrap();
// Open the protocol in exclusive mode (exclusive mode is for applications, other modes are
// intended for drivers)
let mut protocol = boot::open_protocol_exclusive::<GraphicsOutput>(handle).unwrap();
// Configure protocol here if desired, for example
let mode = protocol.modes().find(|m| m.info().resolution() == (800, 600)).unwrap();
protocol.set_mode(&mode).unwrap();

// Create the draw target utilizing the configured protocol.
let mut target = FbDrawTarget::new(&mut protocol);

// ...draw on it...

Implementations§

Source§

impl<'proto> FbDrawTarget<'proto>

Source

pub fn new(protocol: &'proto mut ScopedProtocol<GraphicsOutput>) -> Self

Create a new FbDrawTarget given a GOP protocol handle.

Fills the framebuffer with black.

Note that one should configure the desired graphics mode for the protocol prior to creating the FbDrawTarget.

§Panics
  • If the pixel format for the current graphics mode is not PixelFormat::Rgb or PixelFormat::Bgr.
  • If the reported resolution of the current graphics mode is ≥ 2^31 - 1 in either dimension.
  • If the reported number of pixels addressed by the current graphics mode (width×height) exceeds isize::MAX / 4 (i.e. largest possible size for an array of u32s).
  • If the framebuffer pointer returned from UEFI is not aligned to align_of::<u32>() (typically four bytes).
Examples found in repository?
examples/modes.rs (line 55)
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}
Source§

impl FbDrawTarget<'static>

Source

pub unsafe fn new_free_lifetime( protocol: &mut ScopedProtocol<GraphicsOutput>, ) -> Self

Equivalent to FbDrawTarget::new, but allows the FbDrawTarget to outlive the input ScopedProtocol.

Allows preserving the framebuffer after closing the GOP handle or even after exiting UEFI boot services (with caveats). See the module-level documentation.

Other than breaking the input protocol lifetime, precisely equivalent to the new method in behavior.

§Safety

This technically violates the requirements specified in FrameBuffer, as the pointer outlives the FrameBuffer that created it (in fact, this immediately discards the Framebuffer so it violates the specified safety requirements even before the protocol gets dropped). However, per the implementation in uefi-rs 0.36.1, it directly passes through the pointer given by the UEFI implementation, so the rules for handling it correspond to how it could be handled in C. The implementation follows all the other safety requirements specified by FrameBuffer.

Re-opening a specific GOP handle may invalidate all previous framebuffer pointers, and changing the graphics mode will almost certainly invalidate them.

The ability to preserve the framebuffer pointer after exiting boot services is not specified at all in the UEFI Specification as of version 2.11. As such, it is implementation-specific. One must also ensure the system memory map remains set up properly to keep the pointer pointing to the same physical address.

Examples found in repository?
examples/framebuffer_free_lifetime.rs (line 37)
17fn main() -> Status {
18	helpers::init().unwrap();
19
20	let handle =
21		boot::get_handle_for_protocol::<GraphicsOutput>().expect("Unable to get GOP handle");
22	let mut gop =
23		boot::open_protocol_exclusive::<GraphicsOutput>(handle).expect("Unable to open GOP handle");
24
25	let mode_800x600 = gop
26		.modes()
27		// one of the standard modes, although some systems may only provide 640×480
28		.find(|m| m.info().resolution() == (800, 600))
29		.expect("Unable to find 800x600 video mode");
30	gop.set_mode(&mode_800x600)
31		.expect("Couldn't switch to 800x600 video mode");
32
33	// SAFETY: see rules on `FbDrawTarget::new_free_lifetime` for caveats about invalidation,
34	// implementation-specific considerations, and complications with exiting boot services.
35	//
36	// This example does work on QEMU and should work on most real desktop implementations.
37	let mut target = unsafe { FbDrawTarget::new_free_lifetime(&mut gop) };
38	drop(gop);
39	// SAFETY: this is completely disregarding almost everything one needs to do after exiting boot
40	// services, just for demonstration purposes.  The example still works after uncommenting this.
41	// unsafe { drop(uefi::boot::exit_boot_services(None)) };
42
43	target
44		.fill_solid(
45			&Rectangle::new(Point::zero(), Size::new(266, 600)),
46			Rgb888::RED,
47		)
48		.expect("infallible");
49	target
50		.fill_solid(
51			&Rectangle::new(Point::new(266, 0), Size::new(267, 600)),
52			Rgb888::GREEN,
53		)
54		.expect("infallible");
55	target
56		.fill_solid(
57			&Rectangle::new(Point::new(533, 0), Size::new(267, 600)),
58			Rgb888::BLUE,
59		)
60		.expect("infallible");
61
62	boot::stall(Duration::from_secs(10));
63	runtime::reset(ResetType::SHUTDOWN, Status::SUCCESS, None);
64}

Trait Implementations§

Source§

impl<'proto> Debug for FbDrawTarget<'proto>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl DrawTarget for FbDrawTarget<'_>

Source§

type Color = Rgb888

The pixel color type the targetted display supports.
Source§

type Error = Infallible

Error type to return when a drawing operation fails. Read more
Source§

fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where I: IntoIterator<Item = Pixel<Self::Color>>,

Draw individual pixels to the display without a defined order. Read more
Source§

fn fill_contiguous<I>( &mut self, area: &Rectangle, colors: I, ) -> Result<(), Self::Error>
where I: IntoIterator<Item = Self::Color>,

Fill a given area with an iterator providing a contiguous stream of pixel colors. Read more
Source§

fn fill_solid( &mut self, area: &Rectangle, color: Self::Color, ) -> Result<(), Self::Error>

Fill a given area with a solid color. Read more
Source§

fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error>

Fill the entire display with a solid color. Read more
Source§

impl OriginDimensions for FbDrawTarget<'_>

Source§

fn size(&self) -> Size

Returns the size of the bounding box.

Auto Trait Implementations§

§

impl<'proto> Freeze for FbDrawTarget<'proto>

§

impl<'proto> RefUnwindSafe for FbDrawTarget<'proto>

§

impl<'proto> !Send for FbDrawTarget<'proto>

§

impl<'proto> !Sync for FbDrawTarget<'proto>

§

impl<'proto> Unpin for FbDrawTarget<'proto>

§

impl<'proto> !UnwindSafe for FbDrawTarget<'proto>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> Dimensions for T

Source§

fn bounding_box(&self) -> Rectangle

Returns the bounding box.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.