Skip to main content

Canvas

Struct Canvas 

Source
pub struct Canvas<'a, O: PixelOps = ScalarOps> {
    base: *mut u8,
    pitch: usize,
    dims: Dims,
    format: Format,
    wc: bool,
    track_dirty: bool,
    dirty: RectSet<DEFAULT_SET_CAPACITY>,
    ops: O,
    _borrow: PhantomData<&'a mut [u8]>,
}
Expand description

A contiguous pixel surface with a validated geometry and format.

The default ops backend is ScalarOps, which keeps the type usable from a host test. The kernel instantiates Canvas<'_, KernelOps> so the SSE2 paths are inlined with no dispatch.

Fields§

§base: *mut u8§pitch: usize§dims: Dims§format: Format§wc: bool§track_dirty: bool§dirty: RectSet<DEFAULT_SET_CAPACITY>§ops: O§_borrow: PhantomData<&'a mut [u8]>

Implementations§

Source§

impl<'a> Canvas<'a, ScalarOps>

Source

pub fn new( buf: &'a mut [u8], dims: Dims, pitch: usize, format: Format, ) -> Result<Self, Error>

Build a scalar-backend canvas over a borrowed buffer.

pitch is a byte count, not a pixel count.

Source§

impl<'a, O: PixelOps> Canvas<'a, O>

Source

pub fn with_ops( buf: &'a mut [u8], dims: Dims, pitch: usize, format: Format, ops: O, ) -> Result<Self, Error>

Build a canvas over a borrowed buffer with an explicit ops backend.

Source

pub unsafe fn direct( base: NonNull<u8>, dims: Dims, pitch: usize, format: Format, write_combining: bool, ops: O, ) -> Result<Self, Error>

Build a canvas over physical framebuffer memory.

base is the HHDM-mapped virtual address, already validated by the caller (page-aligned and mapped for pitch * height bytes).

§Safety

base must stay mapped and exclusively owned by this canvas for the whole lifetime of the value, and pitch * height must not wrap.

Source

unsafe fn from_raw( base: *mut u8, dims: Dims, pitch: usize, format: Format, wc: bool, ops: O, ) -> Self

Source

pub fn dims(&self) -> Dims

Source

pub fn format(&self) -> Format

Source

pub fn pitch(&self) -> usize

Bytes per row.

Source

pub fn bytes_per_pixel(&self) -> usize

Bytes from the start of one row to the start of the same column in the next row.

Source

pub fn is_write_combining(&self) -> bool

Source

pub fn set_write_combining(&mut self, wc: bool)

Mark the surface as write-combining (MMIO framebuffer). Drawing then uses the non-temporal stores and fences once per call.

Source

pub fn bounds(&self) -> Rect

The whole surface, as a rectangle.

Source

pub fn ops(&self) -> &O

Source

pub fn ops_mut(&mut self) -> &mut O

Source

pub fn clip(&self, rect: Rect) -> Option<Rect>

Clip a rectangle to this surface. None means nothing to draw.

This is the only clipping rule in the crate.

Source

pub fn set_tracking(&mut self, on: bool)

Enable or disable dirty-rectangle tracking. Enabled by default: the cost is one bounded insertion per drawing call.

Source

pub fn is_tracking(&self) -> bool

Source

pub fn dirty(&self) -> &RectSet<DEFAULT_SET_CAPACITY>

Source

pub fn take_dirty(&mut self) -> RectSet<DEFAULT_SET_CAPACITY>

Take the accumulated damage, leaving the set empty.

Source

pub fn clear_dirty(&mut self)

Source

fn mark(&mut self, rect: Rect)

Source

pub fn fence(&self)

Fence the stores of a write-combining surface.

Called once per public drawing call; callers that emit their own stores must call it before the pixels are read.

Source

pub(crate) fn row_ptr(&self, y: u32) -> *mut u8

Source

fn read_raw(&self, x: u32, y: u32) -> u32

Read a pixel. Coordinates must be inside the surface.

Volatile on a write-combining (MMIO) surface: the value may have been changed by the device since the last cached load.

Source

fn write_raw(&mut self, x: u32, y: u32, value: u32)

Write a pixel. Coordinates must be inside the surface. Volatile on a write-combining (MMIO) surface.

Source

pub fn read_pixel(&self, x: u32, y: u32) -> Option<Packed>

Read one pixel. Volatile, because the surface may be MMIO.

Source

pub fn write_pixel(&mut self, x: u32, y: u32, pixel: Packed) -> bool

Write one pixel. Volatile, because the surface may be MMIO.

Source

pub fn fill_rect(&mut self, rect: Rect, color: Packed) -> Option<Rect>

Fill rect (clipped) with a packed colour. Returns the rectangle actually written, or None when entirely outside.

Source

pub fn fill(&mut self, color: Packed) -> Rect

Fill the whole surface. Convenience over Self::fill_rect.

Source

pub fn blit<S: Source + ?Sized>( &mut self, dst: Rect, src: &S, src_rect: Rect, ) -> Result<Option<Rect>, Error>

Copy src_rect from src into dst on this surface.

Both formats must be identical: a bulk copy must not silently reinterpret channels. Clipping is applied to the destination and to the source, and src_rect’s origin is shifted accordingly.

Returns the destination rectangle written, or None when nothing was visible.

Source

pub fn blend_rect<S: Source + ?Sized>( &mut self, dst: Rect, src: &S, src_rect: Rect, alpha: u8, ) -> Result<Option<Rect>, Error>

Blend an optional constant alpha.

With alpha == 255 this is a plain copy; with alpha == 0 the destination is unchanged, so the call returns None without touching the surface or recording damage. The blend is per byte of the native word, matching the kernel’s FramebufferOps::blend.

Source

pub fn convert_from<S: Source + ?Sized>( &mut self, dst: Rect, src: &S, src_rect: Rect, ) -> Result<Option<Rect>, Error>

Copy from a surface that may have a different format.

This is the crate’s only per-pixel conversion path: each source pixel is decoded with Format::unpack and re-encoded with Format::pack. Prefer Self::blit when the formats match.

Source

pub fn copy_to<O2: PixelOps>( &self, dst: &mut Canvas<'_, O2>, origin: (u32, u32), ) -> Result<Option<Rect>, Error>

Copy the whole surface into dst at origin, with the same format rules as Self::blit.

Source

fn is_contiguous<S: Source + ?Sized>(&self, d: Rect, sx: u32, src: &S) -> bool

True when d spans a full row of both surfaces and both pitches have no padding, so the whole block is contiguous in memory.

Source

fn map_rect( &self, dst: Rect, src_dims: Dims, src_rect: Rect, ) -> Option<(Rect, u32, u32)>

Map a requested copy onto the destination and source surfaces.

Returns the clipped destination rectangle plus the source origin to start from. This is what keeps a partially off-screen blit correct: clipping the destination shifts the source window by the same amount, it never stretches or re-anchors the image.

Source§

impl<'a, O: PixelOps> Canvas<'a, O>

Source

pub fn view(&self) -> View<'_>

Borrow this canvas as an immutable view.

Useful to keep a source alive while the destination is drawn through a different alias, or to pass a canvas to code that only reads.

Trait Implementations§

Source§

impl<O: PixelOps> Debug for Canvas<'_, O>

Source§

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

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

impl<O: PixelOps + Send> Send for Canvas<'_, O>

Source§

impl<O: PixelOps> Source for Canvas<'_, O>

Source§

fn dims(&self) -> Dims

Source§

fn format(&self) -> Format

Source§

fn pitch(&self) -> usize

Bytes per row. Read more
Source§

unsafe fn row_ptr(&self, y: u32) -> *const u8

Pointer to the first pixel of row y. Read more
Source§

unsafe fn read_raw(&self, x: u32, y: u32) -> u32

Read one pixel with no volatile semantics. Read more

Auto Trait Implementations§

§

impl<'a, O = ScalarOps> !Sync for Canvas<'a, O>

§

impl<'a, O = ScalarOps> !UnwindSafe for Canvas<'a, O>

§

impl<'a, O> Forget for Canvas<'a, O>

§

impl<'a, O> Freeze for Canvas<'a, O>
where O: Freeze,

§

impl<'a, O> Move for Canvas<'a, O>

§

impl<'a, O> RefUnwindSafe for Canvas<'a, O>
where O: RefUnwindSafe,

§

impl<'a, O> Unpin for Canvas<'a, O>
where O: Unpin,

§

impl<'a, O> UnsafeUnpin for Canvas<'a, O>
where O: UnsafeUnpin,

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.