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
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>
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)
pub fn forget(&mut self)
Drop the copy of the last frame, so the next frame is written whole.
pub fn writer(&self) -> &LiveImageWriter
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
pub fn into_writer(self) -> LiveImageWriter
The writer back, and the copy freed.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for LiveImageDiffWriter
impl !UnwindSafe for LiveImageDiffWriter
impl Freeze for LiveImageDiffWriter
impl Send for LiveImageDiffWriter
impl Sync for LiveImageDiffWriter
impl Unpin for LiveImageDiffWriter
impl UnsafeUnpin for LiveImageDiffWriter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
impl<T> ErasedDestructor for Twhere
T: 'static,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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