fb.c: minor cleanup

Signed-off-by: Sven Peter <sven@svenpeter.dev>
This commit is contained in:
Sven Peter 2021-02-12 20:09:49 +01:00 committed by Hector Martin
parent 05e7306bf9
commit 819b16fbe4
3 changed files with 60 additions and 38 deletions

View file

@ -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);
}

View file

@ -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);

View file

@ -2,9 +2,9 @@
#include <stdarg.h>
#include "utils.h"
#include "types.h"
#include "uart.h"
#include "utils.h"
#include "vsprintf.h"
static char ascii(char s)