drm-gfx 0.1.2

3D graphics rendering for direct rendering manager with optional tokio thread support
Documentation
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use embedded_graphics_core::draw_target::DrawTarget;
use embedded_graphics_core::prelude::Point;

use crate::DrawPrimitive;

/// Fills a buffer with a solid color
#[inline]
pub fn fill_buffer<D: DrawTarget<Color = embedded_graphics_core::pixelcolor::Bgr888>>(
    fb: &mut D,
    color: embedded_graphics_core::pixelcolor::Bgr888,
) where
    <D as DrawTarget>::Error: std::fmt::Debug,
{
    fb.clear(color).unwrap();
}

#[inline]
pub fn draw<D: DrawTarget<Color = embedded_graphics_core::pixelcolor::Bgr888>>(
    primitive: DrawPrimitive,
    fb: &mut D,
) where
    <D as DrawTarget>::Error: std::fmt::Debug,
{
    match primitive {
        DrawPrimitive::Line([p1, p2], color) => {
            fb.draw_iter(
                line_drawing::Bresenham::new((p1.x, p1.y), (p2.x, p2.y))
                    .map(|(x, y)| embedded_graphics_core::Pixel(Point::new(x, y), color)),
            )
            .unwrap();
        }
        DrawPrimitive::ColoredPoint(p, c) => {
            let p = embedded_graphics_core::geometry::Point::new(p.x, p.y);

            fb.draw_iter([embedded_graphics_core::Pixel(p, c)]).unwrap();
        }
        DrawPrimitive::ColoredTriangle(mut vertices, color) => {
            //sort vertices by y
            vertices.sort_by(|a, b| a.y.cmp(&b.y));

            let [p1, p2, p3] = vertices
                .iter()
                .map(|p| embedded_graphics_core::geometry::Point::new(p.x, p.y))
                .collect::<Vec<embedded_graphics_core::geometry::Point>>()
                .try_into()
                .unwrap();

            if p2.y == p3.y {
                fill_bottom_flat_triangle(p1, p2, p3, color, fb);
            } else if p1.y == p2.y {
                fill_top_flat_triangle(p1, p2, p3, color, fb);
            } else {
                let p4 = Point::new(
                    (p1.x as f32
                        + ((p2.y - p1.y) as f32 / (p3.y - p1.y) as f32) * (p3.x - p1.x) as f32)
                        as i32,
                    p2.y,
                );

                fill_bottom_flat_triangle(p1, p2, p4, color, fb);
                fill_top_flat_triangle(p2, p4, p3, color, fb);
            }
        }
    }
}

fn fill_bottom_flat_triangle<D: DrawTarget<Color = embedded_graphics_core::pixelcolor::Bgr888>>(
    p1: Point,
    p2: Point,
    p3: Point,
    color: embedded_graphics_core::pixelcolor::Bgr888,
    fb: &mut D,
) where
    <D as DrawTarget>::Error: std::fmt::Debug,
{
    // Avoid accumulated floating point errors by recalculating for each scanline
    for scanline_y in p1.y..=p2.y {
        let t = (scanline_y - p1.y) as f32 / (p2.y - p1.y) as f32;

        // Calculate exact x-coordinates for this scanline
        let x1 = p1.x as f32 + t * (p2.x - p1.x) as f32;
        let x2 = p1.x as f32 + t * (p3.x - p1.x) as f32;

        draw_horizontal_line(
            Point::new(x1.round() as i32, scanline_y),
            Point::new(x2.round() as i32, scanline_y),
            color,
            fb,
        );
    }
}

fn fill_top_flat_triangle<D: DrawTarget<Color = embedded_graphics_core::pixelcolor::Bgr888>>(
    p1: Point,
    p2: Point,
    p3: Point,
    color: embedded_graphics_core::pixelcolor::Bgr888,
    fb: &mut D,
) where
    <D as DrawTarget>::Error: std::fmt::Debug,
{
    // Avoid accumulated floating point errors by recalculating for each scanline
    for scanline_y in p1.y..=p3.y {
        let t = (scanline_y - p1.y) as f32 / (p3.y - p1.y) as f32;

        // Calculate exact x-coordinates for this scanline
        let x1 = p1.x as f32 + t * (p3.x - p1.x) as f32;
        let x2 = p2.x as f32 + t * (p3.x - p2.x) as f32;

        draw_horizontal_line(
            Point::new(x1.round() as i32, scanline_y),
            Point::new(x2.round() as i32, scanline_y),
            color,
            fb,
        );
    }
}

fn draw_horizontal_line<D: DrawTarget<Color = embedded_graphics_core::pixelcolor::Bgr888>>(
    p1: Point,
    p2: Point,
    color: embedded_graphics_core::pixelcolor::Bgr888,
    fb: &mut D,
) where
    <D as DrawTarget>::Error: std::fmt::Debug,
{
    let start = p1.x.min(p2.x);
    let end = p1.x.max(p2.x);

    for x in start..=end {
        fb.draw_iter([embedded_graphics_core::Pixel(Point::new(x, p1.y), color)])
            .unwrap();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use embedded_graphics_core::{
        draw_target::DrawTarget,
        geometry::{OriginDimensions, Size},
        pixelcolor::Bgr888,
    };
    use nalgebra::Point2;

    // A simple mock framebuffer for testing drawing functions
    struct MockFrameBuffer {
        pixels: Vec<Vec<Option<Bgr888>>>,
        width: usize,
        height: usize,
        draw_calls: Vec<String>, // Track the drawing operations for verification
    }

    impl MockFrameBuffer {
        fn new(width: usize, height: usize) -> Self {
            let pixels = vec![vec![None; width]; height];
            Self {
                pixels,
                width,
                height,
                draw_calls: Vec::new(),
            }
        }

        fn get_pixel(&self, x: i32, y: i32) -> Option<Bgr888> {
            if x < 0 || y < 0 || x >= self.width as i32 || y >= self.height as i32 {
                return None;
            }
            self.pixels[y as usize][x as usize]
        }

        fn pixel_count(&self) -> usize {
            self.pixels.iter().flatten().filter(|p| p.is_some()).count()
        }

        fn contains_line(&self, p1: Point2<i32>, p2: Point2<i32>) -> bool {
            // Check if all pixels on the line are set
            for (x, y) in line_drawing::Bresenham::new((p1.x, p1.y), (p2.x, p2.y)) {
                if x < 0 || y < 0 || x >= self.width as i32 || y >= self.height as i32 {
                    continue;
                }
                if self.pixels[y as usize][x as usize].is_none() {
                    return false;
                }
            }
            true
        }
    }

    impl DrawTarget for MockFrameBuffer {
        type Color = Bgr888;
        type Error = std::convert::Infallible;

        fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
        where
            I: IntoIterator<Item = embedded_graphics_core::Pixel<Self::Color>>,
        {
            for embedded_graphics_core::Pixel(point, color) in pixels {
                if point.x >= 0
                    && point.y >= 0
                    && point.x < self.width as i32
                    && point.y < self.height as i32
                {
                    self.pixels[point.y as usize][point.x as usize] = Some(color);
                    self.draw_calls
                        .push(format!("Pixel at ({}, {}) = {:?}", point.x, point.y, color));
                }
            }
            Ok(())
        }

        fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error> {
            for y in 0..self.height {
                for x in 0..self.width {
                    self.pixels[y][x] = Some(color);
                }
            }
            self.draw_calls
                .push(format!("Clear with color {:?}", color));
            Ok(())
        }
    }

    impl OriginDimensions for MockFrameBuffer {
        fn size(&self) -> Size {
            Size::new(self.width as u32, self.height as u32)
        }
    }

    #[test]
    fn test_draw_colored_point() {
        let mut fb = MockFrameBuffer::new(100, 100);
        let point = Point2::new(50, 50);
        let color = Bgr888::new(255, 127, 63);

        draw(DrawPrimitive::ColoredPoint(point, color), &mut fb);

        assert_eq!(fb.get_pixel(50, 50), Some(color));
        assert_eq!(fb.pixel_count(), 1);
    }

    #[test]
    fn test_draw_line() {
        let mut fb = MockFrameBuffer::new(100, 100);
        let p1 = Point2::new(10, 10);
        let p2 = Point2::new(20, 20);
        let color = Bgr888::new(255, 127, 63);

        draw(DrawPrimitive::Line([p1, p2], color), &mut fb);

        // Check that the line is actually drawn
        assert!(fb.contains_line(p1, p2));

        // A diagonal line from (10,10) to (20,20) should have exactly 11 pixels
        let pixel_count = fb.pixel_count();
        assert_eq!(pixel_count, 11);
    }

    #[test]
    fn test_draw_horizontal_line() {
        let mut fb = MockFrameBuffer::new(100, 100);
        let p1 = embedded_graphics_core::geometry::Point::new(10, 50);
        let p2 = embedded_graphics_core::geometry::Point::new(20, 50);
        let color = Bgr888::new(255, 127, 63);

        draw_horizontal_line(p1, p2, color, &mut fb);

        // Check all pixels in the horizontal line
        for x in 10..=20 {
            assert_eq!(fb.get_pixel(x, 50), Some(color));
        }

        // A horizontal line from (10,50) to (20,50) should have exactly 11 pixels
        assert_eq!(fb.pixel_count(), 11);
    }

    #[test]
    fn test_fill_bottom_flat_triangle() {
        let mut fb = MockFrameBuffer::new(100, 100);
        let p1 = embedded_graphics_core::geometry::Point::new(50, 10); // Top point
        let p2 = embedded_graphics_core::geometry::Point::new(30, 50); // Bottom-left point
        let p3 = embedded_graphics_core::geometry::Point::new(70, 50); // Bottom-right point
        let color = Bgr888::new(255, 127, 63);

        fill_bottom_flat_triangle(p1, p2, p3, color, &mut fb);

        // Check some key points
        assert_eq!(fb.get_pixel(50, 10), Some(color)); // Top vertex
        assert_eq!(fb.get_pixel(30, 50), Some(color)); // Bottom-left vertex
        assert_eq!(fb.get_pixel(70, 50), Some(color)); // Bottom-right vertex
        assert_eq!(fb.get_pixel(50, 30), Some(color)); // Middle point

        // The triangle should have a non-zero number of pixels
        assert!(fb.pixel_count() > 0);
    }

    #[test]
    fn test_fill_top_flat_triangle() {
        let mut fb = MockFrameBuffer::new(100, 100);
        let p1 = embedded_graphics_core::geometry::Point::new(30, 10); // Top-left point
        let p2 = embedded_graphics_core::geometry::Point::new(70, 10); // Top-right point
        let p3 = embedded_graphics_core::geometry::Point::new(50, 50); // Bottom point
        let color = Bgr888::new(255, 127, 63);

        fill_top_flat_triangle(p1, p2, p3, color, &mut fb);

        // Check some key points
        assert_eq!(fb.get_pixel(30, 10), Some(color)); // Top-left vertex
        assert_eq!(fb.get_pixel(70, 10), Some(color)); // Top-right vertex
        assert_eq!(fb.get_pixel(50, 50), Some(color)); // Bottom vertex
        assert_eq!(fb.get_pixel(50, 30), Some(color)); // Middle point

        // The triangle should have a non-zero number of pixels
        assert!(fb.pixel_count() > 0);
    }

    #[test]
    fn test_draw_colored_triangle() {
        let mut fb = MockFrameBuffer::new(100, 100);
        let vertices = [
            Point2::new(50, 10), // Top point
            Point2::new(30, 70), // Bottom-left point
            Point2::new(70, 70), // Bottom-right point
        ];
        let color = Bgr888::new(255, 127, 63);

        draw(DrawPrimitive::ColoredTriangle(vertices, color), &mut fb);

        // Check the vertices
        assert_eq!(fb.get_pixel(50, 10), Some(color));
        assert_eq!(fb.get_pixel(30, 70), Some(color));
        assert_eq!(fb.get_pixel(70, 70), Some(color));

        // Check the center of the triangle
        assert_eq!(fb.get_pixel(50, 50), Some(color));

        // The triangle should have a non-zero number of pixels
        assert!(fb.pixel_count() > 0);
    }

    #[test]
    fn test_draw_colored_triangle_flat_top() {
        let mut fb = MockFrameBuffer::new(100, 100);
        let vertices = [
            Point2::new(30, 10), // Top-left point
            Point2::new(70, 10), // Top-right point
            Point2::new(50, 70), // Bottom point
        ];
        let color = Bgr888::new(255, 127, 63);

        draw(DrawPrimitive::ColoredTriangle(vertices, color), &mut fb);

        // Check the vertices
        assert_eq!(fb.get_pixel(30, 10), Some(color));
        assert_eq!(fb.get_pixel(70, 10), Some(color));
        assert_eq!(fb.get_pixel(50, 70), Some(color));

        // The triangle should have a non-zero number of pixels
        assert!(fb.pixel_count() > 0);
    }

    #[test]
    fn test_draw_colored_triangle_flat_bottom() {
        let mut fb = MockFrameBuffer::new(100, 100);
        let vertices = [
            Point2::new(50, 10), // Top point
            Point2::new(30, 70), // Bottom-left point
            Point2::new(70, 70), // Bottom-right point
        ];
        let color = Bgr888::new(255, 127, 63);

        draw(DrawPrimitive::ColoredTriangle(vertices, color), &mut fb);

        // Check the vertices
        assert_eq!(fb.get_pixel(50, 10), Some(color));
        assert_eq!(fb.get_pixel(30, 70), Some(color));
        assert_eq!(fb.get_pixel(70, 70), Some(color));

        // The triangle should have a non-zero number of pixels
        assert!(fb.pixel_count() > 0);
    }

    #[test]
    fn test_draw_colored_triangle_general_case() {
        let mut fb = MockFrameBuffer::new(100, 100);
        let vertices = [
            Point2::new(20, 20), // Top point
            Point2::new(40, 60), // Middle point
            Point2::new(80, 40), // Bottom point
        ];
        let color = Bgr888::new(255, 127, 63);

        draw(DrawPrimitive::ColoredTriangle(vertices, color), &mut fb);

        // Check the vertices
        assert_eq!(fb.get_pixel(20, 20), Some(color));
        assert_eq!(fb.get_pixel(40, 60), Some(color));
        assert_eq!(fb.get_pixel(80, 40), Some(color));

        // The triangle should have a non-zero number of pixels
        assert!(fb.pixel_count() > 0);
    }

    #[test]
    fn test_fill_buffer() {
        let mut fb = MockFrameBuffer::new(100, 100);
        let color = Bgr888::new(255, 127, 63);

        fill_buffer(&mut fb, color);

        // Check that all pixels are filled with the color
        for y in 0..100 {
            for x in 0..100 {
                assert_eq!(fb.get_pixel(x, y), Some(color));
            }
        }

        // The buffer should have all pixels filled
        assert_eq!(fb.pixel_count(), 100 * 100);
    }
}