Skip to main content

LiveImageDiffWriter

Struct LiveImageDiffWriter 

pub struct LiveImageDiffWriter { /* private fields */ }
Expand description

A LiveImageWriter for a producer that redraws, or copies, a whole frame every time, whether or not anything in it moved: a VM’s framebuffer copied at each vsync, a remote desktop’s screen, a renderer with no damage tracking of its own.

write_frame compares the frame with the last one it wrote and commits only the rows that changed, each cut to the span of pixels that changed in it. A frame identical to the last one commits nothing: it takes no lock, wakes no window, uploads nothing and draws nothing, so a still picture fed at 60 Hz costs the window nothing.

The comparison runs on the producer’s thread, against a copy of the last frame it keeps, never under the source’s lock: a renderer is kept waiting only while the changed rows are copied in. That copy costs one frame of memory, and the comparison a read of both frames, a fraction of a millisecond for 720 × 1280.

The result is the frame handed over. When the source no longer holds the last frame this writer wrote (another writer committed, cleared or resized it, or left a transaction abandoned), the next frame is written whole. The one change it cannot see is another writer’s write through pixels_mut left unmarked in a transaction that ended without a commit or a panic: such pixels reach no window either, until something marks them.

let source = LiveImageSource::new(LivePixelFormat::Bgrx8);
let mut writer = LiveImageDiffWriter::new(source.writer());
let mut frame = vec![0u8; 64 * 48 * 4];
assert!(writer.write_frame(64, 48, &frame, 64 * 4).unwrap().is_some());
// The same frame again: nothing is committed.
assert_eq!(writer.write_frame(64, 48, &frame, 64 * 4).unwrap(), None);
frame[0] = 255;
assert!(writer.write_frame(64, 48, &frame, 64 * 4).unwrap().is_some());

Implementations§

§

impl LiveImageDiffWriter

pub fn new(writer: LiveImageWriter) -> LiveImageDiffWriter

pub fn write_frame( &mut self, width: u32, height: u32, src: &[u8], src_stride: usize, ) -> Result<Option<u64>, LiveImageError>

Write src, a width × height frame whose rows are src_stride bytes apart, committing only what differs from the last frame written. Some(generation) when it committed, None when the frame was identical and the source still shows it.

§Errors

As LiveImageWriter::write_frame. A refused frame changes nothing, here or in the source.

pub fn forget(&mut self)

Drop the copy of the last frame, so the next frame is written whole.

pub fn writer(&self) -> &LiveImageWriter

The writer, for the source’s other operations: resize, clear, is_displayed. A frame it writes makes the next one handed to this writer go out whole.

pub fn into_writer(self) -> LiveImageWriter

The writer back, and the copy freed.

Trait Implementations§

§

impl Debug for LiveImageDiffWriter

§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync> ⓘ

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<T> WasmNotSend for T
where T: Send,

§

impl<T> WasmNotSendSync for T
where T: WasmNotSend + WasmNotSync,

§

impl<T> WasmNotSync for T
where T: Sync,

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more