ferroui_base/platform/i_bitmap_impl.rs
1use super::{AlphaFormat, IDrawingContextImpl, ILockedFramebuffer, PixelFormat};
2use crate::media::imaging::BitmapEncoderOptions;
3use crate::{PixelSize, Vector};
4use std::any::Any;
5use std::io::Write;
6use std::rc::Rc;
7
8/// Defines the platform-specific interface for a bitmap.
9pub trait IBitmapImpl {
10 /// The dots per inch (DPI) of the image.
11 fn dpi(&self) -> Vector;
12
13 /// The size of the bitmap, in device pixels.
14 fn pixel_size(&self) -> PixelSize;
15
16 /// Version of the pixel data: incremented whenever the content changes.
17 fn version(&self) -> i32;
18
19 /// Saves the bitmap to a stream, encoded as selected by `options`.
20 fn save(&self, stream: &mut dyn Write, options: &BitmapEncoderOptions) -> std::io::Result<()>;
21
22 /// Releases the bitmap's resources.
23 fn dispose(&self);
24
25 /// Lets the backend recover its concrete type.
26 fn as_any(&self) -> &dyn Any;
27
28 /// The bitmap viewed as [`IReadableBitmapImpl`], when its pixels can be
29 /// read.
30 fn as_readable_bitmap(&self) -> Option<&dyn IReadableBitmapImpl> {
31 None
32 }
33}
34
35/// A bitmap shared between the UI thread and the render thread: what a
36/// bitmap object of the UI side holds and what a batch carries. (A layer of
37/// a drawing context is a bitmap too, but stays on the render thread.)
38pub type SharedBitmapImpl = dyn IBitmapImpl + Send + Sync;
39
40/// A bitmap whose pixels can be read.
41pub trait IReadableBitmapImpl: IBitmapImpl {
42 /// The pixel format, if known.
43 fn format(&self) -> Option<PixelFormat>;
44
45 /// The alpha format, if known.
46 fn alpha_format(&self) -> Option<AlphaFormat>;
47
48 /// Locks the pixels for reading (and writing, for writeable bitmaps).
49 fn lock(&self) -> Rc<dyn ILockedFramebuffer>;
50}
51
52/// Defines the platform-specific interface for a writeable bitmap.
53pub trait IWriteableBitmapImpl: IReadableBitmapImpl + Send + Sync {}
54
55/// Defines the platform-specific interface for a bitmap that can be drawn
56/// into.
57pub trait IRenderTargetBitmapImpl: IReadableBitmapImpl + Send + Sync {
58 /// Creates a drawing context that draws into the bitmap.
59 fn create_drawing_context(&self) -> Box<dyn IDrawingContextImpl>;
60}