|
1 | 1 | use uefi::table::boot::BootServices;
|
2 | | -use uefi::proto::console::gop::{GraphicsOutput, BltOp, BltPixel}; |
| 2 | +use uefi::proto::console::gop::{GraphicsOutput, BltOp, BltPixel,PixelFormat}; |
3 | 3 | use uefi_exts::BootServicesExt;
|
4 | 4 |
|
5 | 5 | pub fn test(bt: &BootServices) {
|
@@ -48,29 +48,38 @@ fn fill_color(gop: &mut GraphicsOutput) {
|
48 | 48 | fn draw_fb(gop: &mut GraphicsOutput) {
|
49 | 49 | let mi = gop.current_mode_info();
|
50 | 50 | let stride = mi.stride();
|
51 | | - // BUG: we should check we have enough space to draw. |
52 | | - // let (width, height) = mi.resolution(); |
| 51 | + let (width, height) = mi.resolution(); |
53 | 52 |
|
54 | 53 | let fb = unsafe { gop.frame_buffer() };
|
55 | 54 |
|
56 | | - let mut set_pixel = |(row, column), (r, g, b)| { |
57 | | - let index = (row * stride) + column; |
58 | | - |
59 | | - // BUG: we assume the pixel format is 32-bit BGR, as it often is on x86. |
60 | | - // For RGB the red / blue channels will be inverted. |
61 | | - let bi = 4 * index; |
62 | | - let gi = 4 * index + 1; |
63 | | - let ri = 4 * index + 2; |
64 | | - |
65 | | - fb[bi] = b; |
66 | | - fb[gi] = g; |
67 | | - fb[ri] = r; |
| 55 | + type PixelWriter<'a> = &'a Fn(&mut [u8], (u8, u8, u8)); |
| 56 | + let write_pixel_rgb = |pixel: &mut [u8], (r, g, b)| { |
| 57 | + pixel[0] = r; |
| 58 | + pixel[1] = g; |
| 59 | + pixel[2] = b; |
| 60 | + }; |
| 61 | + let write_pixel_bgr = |pixel: &mut [u8], (r, g, b)| { |
| 62 | + pixel[0] = b; |
| 63 | + pixel[1] = g; |
| 64 | + pixel[2] = r; |
| 65 | + }; |
| 66 | + let write_pixel: PixelWriter = match mi.pixel_format() { |
| 67 | + PixelFormat::RGB => &write_pixel_rgb, |
| 68 | + PixelFormat::BGR => &write_pixel_bgr, |
| 69 | + _ => { |
| 70 | + info!("This pixel format is not supported by the drawing demo"); |
| 71 | + return; |
| 72 | + } |
68 | 73 | };
|
69 | 74 |
|
70 | 75 | let mut fill_rectangle = |(x1, y1), (x2, y2), color| {
|
| 76 | + assert!((x1 < width) && (x2 < width), "Bad X coordinate"); |
| 77 | + assert!((y1 < height) && (y2 < height), "Bad Y coordinate"); |
71 | 78 | for row in y1..y2 {
|
72 | 79 | for column in x1..x2 {
|
73 | | - set_pixel((row, column), color); |
| 80 | + let index = (row * stride) + column; |
| 81 | + let pixel = &mut fb[4*index..4*index+3]; |
| 82 | + write_pixel(pixel, color); |
74 | 83 | }
|
75 | 84 | }
|
76 | 85 | };
|
|
0 commit comments