Struct linuxfb::double::Buffer

source ·
pub struct Buffer {
    pub width: u32,
    pub height: u32,
    /* private fields */
}
Expand description

Double-buffered interface to a framebuffer

let mut fb = linuxfb::Framebuffer::new("/dev/fb0").unwrap();
// Do any custom setup on the framebuffer here, such as
// setting bytes_per_pixel.

// The double::Buffer will take ownership of the framebuffer, and
// configure it's virtual_size to be twice the actual size.
let mut buffer = linuxfb::double::Buffer::new(fb).unwrap();

// These values represent the size of a single buffer,
// which is equivalent to the screen resolution:
let width = buffer.width as usize;
let height = buffer.height as usize;

// Retrieve a slice for the current backbuffer:
let frame: &mut[u8] = buffer.as_mut_slice();

// Write pixel data:
for i in 0..frame.len() {
  frame[i] = 0;
}

// Flip the display, to make the data appear on screen:
buffer.flip().unwrap();

// The `frame` reference acquired above is no longer valid
// (it now points to the front buffer), so we need to get
// a new one:

let frame: &mut[u8] = buffer.as_mut_slice();

// Writing byte-wise is neither very efficient, nor convenient.
// To write whole pixels, we can cast our buffer to the right
// format (u32 in this case):
let (prefix, pixels, suffix) = unsafe { frame.align_to_mut::<u32>() };

// Since we are using a type that can hold a whole pixel, it should
// always align nicely.
// Thus there is no prefix or suffix here:
assert_eq!(prefix.len(), 0);
assert_eq!(suffix.len(), 0);

// Now we can start filling the pixels:
for y in 0..height {
  for x in 0..width {
    pixels[x + y * width] = 0xFF00FFFF; // magenta, assuming 32-bit RGBA
  }
}

// Finally flip the buffer again, to make the changes visible on screen:
buffer.flip().unwrap();

Fields§

§width: u32§height: u32

Implementations§

source§

impl Buffer

source

pub fn new(fb: Framebuffer) -> Result<Self, Error>

Create a new Buffer object, backed by the given framebuffer.

Initializes the virtual size and the offset of the buffer.

Takes ownership of the framebuffer, so any other modifications to the framebuffer’s state need to be done beforehand.

Usually, after initialization the offset will be set to (0, 0), and the first frame will be drawn into the backbuffer at (0, height). However, when the offset of the framebuffer is already set to (0, height), it is left like that and the initial backbuffer is at (0, 0). This behavior prevents the display from showing an old, retained image between the call to new and the first call to [flip].

source

pub fn as_mut_slice(&mut self) -> &mut [u8]

Returns a mutable slice to the current backbuffer.

Changes to this slice will not end up on screen, until [flip] is called.

The slice has a length of width * height * bytes_per_pixel, where width and height are equal to the screen resolution, and bytes_per_pixel is equal to the value returned from Framebuffer::get_bytes_per_pixel

source

pub fn flip(&mut self) -> Result<(), Error>

Flips the display, by exchanging

source

pub fn blank(&self, level: BlankingLevel) -> Result<(), Error>

Calls blank on the underlying Framebuffer

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.