1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use std::sync::{Arc, Mutex};
use femtos::Instant;

use crate::traits::ClockedQueue;

pub const MASK_COLOUR: u32 = 0xFFFFFFFF;

#[derive(Copy, Clone, Default, Debug, PartialEq, Eq)]
pub enum PixelEncoding {
    #[default]
    RGBA,
    ARGB,
    ABGR,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Pixel {
    Rgb(u8, u8, u8),
    Rgba(u8, u8, u8, u8),
    Mask,
}

impl Pixel {
    #[inline]
    pub fn encode(self, encoding: PixelEncoding) -> u32 {
        let (r, g, b, a) = match self {
            Pixel::Rgb(r, g, b) => (r as u32, g as u32, b as u32, 255),
            Pixel::Rgba(r, g, b, a) => (r as u32, g as u32, b as u32, a as u32),
            Pixel::Mask => return MASK_COLOUR,
        };

        match encoding {
            PixelEncoding::RGBA => (r << 24) | (g << 16) | (b << 8) | a,
            PixelEncoding::ARGB => (a << 24) | (r << 16) | (g << 8) | b,
            PixelEncoding::ABGR => (a << 24) | (b << 16) | (g << 8) | r,
        }
    }
}

#[derive(Clone, Default)]
pub struct Frame {
    pub width: u32,
    pub height: u32,
    pub encoding: PixelEncoding,
    pub bitmap: Vec<u32>,
}

impl Frame {
    pub fn new(width: u32, height: u32, encoding: PixelEncoding) -> Self {
        Self {
            width,
            height,
            encoding,
            bitmap: vec![0; (width * height) as usize],
        }
    }

    pub fn new_shared(width: u32, height: u32, encoding: PixelEncoding) -> Arc<Mutex<Frame>> {
        Arc::new(Mutex::new(Frame::new(width, height, encoding)))
    }

    pub fn set_size(&mut self, width: u32, height: u32) {
        self.width = width;
        self.height = height;
        self.bitmap.resize((width * height) as usize, 0);
    }

    #[inline]
    pub fn set_pixel(&mut self, pos_x: u32, pos_y: u32, pixel: Pixel) {
        match pixel {
            Pixel::Mask => {},
            value if pos_x < self.width && pos_y < self.height => {
                self.bitmap[(pos_x + (pos_y * self.width)) as usize] = value.encode(self.encoding);
            },
            _ => {},
        }
    }

    #[inline]
    pub fn set_encoded_pixel(&mut self, pos_x: u32, pos_y: u32, pixel: u32) {
        match pixel {
            MASK_COLOUR => {},
            value if pos_x < self.width && pos_y < self.height => {
                self.bitmap[(pos_x + (pos_y * self.width)) as usize] = value;
            },
            _ => {},
        }
    }

    pub fn blit<B: Iterator<Item = Pixel>>(&mut self, pos_x: u32, pos_y: u32, mut bitmap: B, width: u32, height: u32) {
        for y in pos_y..(pos_y + height) {
            for x in pos_x..(pos_x + width) {
                match bitmap.next().unwrap() {
                    Pixel::Mask => {},
                    value if x < self.width && y < self.height => {
                        self.bitmap[(x + (y * self.width)) as usize] = value.encode(self.encoding);
                    },
                    _ => {},
                }
            }
        }
    }

    pub fn clear(&mut self, value: Pixel) {
        let value = value.encode(self.encoding);
        self.bitmap.iter_mut().for_each(|pixel| *pixel = value);
    }
}

pub fn frame_queue(width: u32, height: u32) -> (FrameSender, FrameReceiver) {
    let sender = FrameSender {
        encoding: Arc::new(Mutex::new(PixelEncoding::RGBA)),
        queue: ClockedQueue::new(10),
    };

    let receiver = FrameReceiver {
        max_size: (width, height),
        encoding: sender.encoding.clone(),
        queue: sender.queue.clone(),
    };

    (sender, receiver)
}

pub struct FrameSender {
    encoding: Arc<Mutex<PixelEncoding>>,
    queue: ClockedQueue<Frame>,
}

impl FrameSender {
    pub fn encoding(&self) -> PixelEncoding {
        *self.encoding.lock().unwrap()
    }

    pub fn add(&self, clock: Instant, frame: Frame) {
        self.queue.push(clock, frame);
    }
}

pub struct FrameReceiver {
    max_size: (u32, u32),
    encoding: Arc<Mutex<PixelEncoding>>,
    queue: ClockedQueue<Frame>,
}

impl FrameReceiver {
    pub fn max_size(&self) -> (u32, u32) {
        self.max_size
    }

    pub fn request_encoding(&self, encoding: PixelEncoding) {
        *self.encoding.lock().unwrap() = encoding;
    }

    pub fn latest(&self) -> Option<(Instant, Frame)> {
        self.queue.pop_latest()
    }
}