From 819b16fbe45f1ada369521b4baa60d2725fcdea3 Mon Sep 17 00:00:00 2001 From: Sven Peter Date: Fri, 12 Feb 2021 20:09:49 +0100 Subject: [PATCH] fb.c: minor cleanup Signed-off-by: Sven Peter --- src/fb.c | 75 ++++++++++++++++++++++++++++++----------------------- src/fb.h | 21 ++++++++++++--- src/utils.c | 2 +- 3 files changed, 60 insertions(+), 38 deletions(-) diff --git a/src/fb.c b/src/fb.c index b2c79068..e8dfe88c 100644 --- a/src/fb.c +++ b/src/fb.c @@ -1,66 +1,75 @@ /* SPDX-License-Identifier: MIT */ +#include "assert.h" #include "fb.h" +#include "string.h" #include "utils.h" #include "xnuboot.h" -u32 *fb; -int fb_s, fb_w, fb_h, fb_d; -int scale = 1; +#define FB_DEPTH_FLAG_RETINA 0x10000 +#define FB_DEPTH_MASK 0xff -static u32 *logo; -static int logo_w, logo_h; +fb_t fb; -extern char _binary_build_bootlogo_128_bin_start[0]; -extern char _binary_build_bootlogo_256_bin_start[0]; +static struct { + u32 *ptr; + u32 width; + u32 height; +} logo; + +extern u8 _binary_build_bootlogo_128_bin_start[]; +extern u8 _binary_build_bootlogo_256_bin_start[]; extern u8 _binary_build_font_bin_start[]; extern u8 _binary_build_font_retina_bin_start[]; void fb_init(void) { - fb = (void *)cur_boot_args.video.base; - fb_s = cur_boot_args.video.stride / 4; - fb_w = cur_boot_args.video.width; - fb_h = cur_boot_args.video.height; - fb_d = cur_boot_args.video.depth & 0xff; - printf("fb init: %dx%d (%d) [s=%d] @%p\n", fb_w, fb_h, fb_d, fb_s, fb); + fb.ptr = (void *)cur_boot_args.video.base; + fb.stride = cur_boot_args.video.stride / 4; + fb.width = cur_boot_args.video.width; + fb.height = cur_boot_args.video.height; + fb.depth = cur_boot_args.video.depth & FB_DEPTH_MASK; + printf("fb init: %dx%d (%d) [s=%d] @%p\n", fb.width, fb.height, fb.depth, fb.stride, fb.ptr); - if (cur_boot_args.video.depth & 0x10000) { - logo = (void *)_binary_build_bootlogo_256_bin_start; - logo_w = logo_h = 256; + if (cur_boot_args.video.depth & FB_DEPTH_FLAG_RETINA) { + logo.ptr = (void *)_binary_build_bootlogo_256_bin_start; + logo.width = logo.height = 256; } else { - logo = (void *)_binary_build_bootlogo_128_bin_start; - logo_w = logo_h = 128; + logo.ptr = (void *)_binary_build_bootlogo_128_bin_start; + logo.width = logo.height = 128; } } -static inline uint32_t rgbx_to_rgb30(u32 c) +static void fb_set_pixel(u32 x, u32 y, rgb_t c) { - u8 r = c; - u8 g = c >> 8; - u8 b = c >> 16; - return (b << 2) | (g << 12) | (r << 22); + fb.ptr[x + y * fb.stride] = (c.b << 2) | (c.g << 12) | (c.r << 22); } -void fb_blit(int x, int y, int w, int h, void *data, int stride) +void fb_blit(u32 x, u32 y, u32 w, u32 h, void *data, u32 stride) { - uint32_t *p = data; + u8 *p = data; - for (int i = 0; i < h; i++) - for (int j = 0; j < w; j++) - fb[x + j + (y + i) * fb_s] = rgbx_to_rgb30(p[j + i * stride]); + for (u32 i = 0; i < h; i++) { + for (u32 j = 0; j < w; j++) { + rgb_t color = {.r = p[(j + i * stride) * 4], + .g = p[(j + i * stride) * 4 + 1], + .b = p[(j + i * stride) * 4 + 2]}; + fb_set_pixel(x + j, y + i, color); + } + } } -void fb_fill(int x, int y, int w, int h, u32 color) +void fb_fill(u32 x, u32 y, u32 w, u32 h, rgb_t color) { - for (int i = 0; i < h; i++) - for (int j = 0; j < w; j++) - fb[x + j + (y + i) * fb_s] = rgbx_to_rgb30(color); + for (u32 i = 0; i < h; i++) + for (u32 j = 0; j < w; j++) + fb_set_pixel(x + j, y + i, color); } void fb_display_logo(void) { printf("fb: display logo\n"); - fb_blit((fb_w - logo_w) / 2, (fb_h - logo_h) / 2, logo_w, logo_h, logo, logo_w); + fb_blit((fb.width - logo.width) / 2, (fb.height - logo.height) / 2, logo.width, logo.height, + logo.ptr, logo.width); } diff --git a/src/fb.h b/src/fb.h index 4ea137c4..cbd690e8 100644 --- a/src/fb.h +++ b/src/fb.h @@ -5,12 +5,25 @@ #include "types.h" -extern u32 *fb; -extern int fb_s, fb_w, fb_h; +typedef struct { + u32 *ptr; /* pointer to the start of the framebuffer */ + u32 stride; /* framebuffer stride divided by four (i.e. stride in pixels) */ + u32 depth; /* framebuffer depth (i.e. bits per pixel) */ + u32 width; /* width of the framebuffer in pixels */ + u32 height; /* height of the framebuffer in pixels */ +} fb_t; + +typedef struct { + u8 r; + u8 g; + u8 b; +} rgb_t; + +extern fb_t fb; void fb_init(void); -void fb_blit(int x, int y, int w, int h, void *data, int stride); -void fb_fill(int x, int y, int w, int h, u32 color); +void fb_blit(u32 x, u32 y, u32 w, u32 h, void *data, u32 stride); +void fb_fill(u32 x, u32 y, u32 w, u32 h, rgb_t color); void fb_display_logo(void); diff --git a/src/utils.c b/src/utils.c index 00270d77..66a089b8 100644 --- a/src/utils.c +++ b/src/utils.c @@ -2,9 +2,9 @@ #include -#include "utils.h" #include "types.h" #include "uart.h" +#include "utils.h" #include "vsprintf.h" static char ascii(char s)