Skip to main content

rlvgl_platform/
display.rs

1//! Traits and helpers for display drivers.
2use alloc::vec;
3use alloc::vec::Vec;
4use rlvgl_core::widget::{Color, Rect};
5
6/// Trait implemented by display drivers.
7pub trait DisplayDriver {
8    /// Flush a rectangular region of pixels to the display.
9    fn flush(&mut self, area: Rect, colors: &[Color]);
10
11    /// Optional vertical sync hook.
12    fn vsync(&mut self) {}
13}
14
15/// Dummy headless driver used for tests.
16pub struct DummyDisplay;
17
18impl DisplayDriver for DummyDisplay {
19    fn flush(&mut self, _area: Rect, _colors: &[Color]) {}
20}
21
22/// In-memory framebuffer driver for tests and headless rendering.
23pub struct BufferDisplay {
24    /// Width of the framebuffer in pixels.
25    pub width: usize,
26    /// Height of the framebuffer in pixels.
27    pub height: usize,
28    /// Pixel buffer stored in row-major order.
29    pub buffer: Vec<Color>,
30}
31
32impl BufferDisplay {
33    /// Create a framebuffer with the specified dimensions.
34    pub fn new(width: usize, height: usize) -> Self {
35        Self {
36            width,
37            height,
38            buffer: vec![Color(0, 0, 0, 255); width * height],
39        }
40    }
41}
42
43impl DisplayDriver for BufferDisplay {
44    fn flush(&mut self, area: Rect, colors: &[Color]) {
45        for y in 0..area.height as usize {
46            for x in 0..area.width as usize {
47                let idx = (area.y as usize + y) * self.width + (area.x as usize + x);
48                self.buffer[idx] = colors[y * area.width as usize + x];
49            }
50        }
51    }
52}