perf: optimize graphics rendering and implement caching

This commit is contained in:
boreddevnl 2026-05-09 17:27:39 +02:00
parent eb05d62891
commit ff9be87c5d
2 changed files with 104 additions and 29 deletions

View file

@ -415,6 +415,23 @@ static void decode_wallpapers_task(void *arg) {
wp->name[(nl-4 < 63) ? nl-4 : 63] = 0; wp->name[(nl-4 < 63) ? nl-4 : 63] = 0;
// Load and generate thumbnail // Load and generate thumbnail
char cache_path[256];
int cp = 0;
char *cpref = "/Library/Caches/Thumbnails/";
while (cpref[cp]) { cache_path[cp] = cpref[cp]; cp++; }
int cn = 0;
while (info[i].name[cn]) { cache_path[cp+cn] = info[i].name[cn]; cn++; }
char *csuf = ".bin";
int cs = 0;
while (csuf[cs]) { cache_path[cp+cn+cs] = csuf[cs]; cs++; }
cache_path[cp+cn+cs] = 0;
int cfd = sys_open(cache_path, "r");
if (cfd >= 0) {
sys_read(cfd, wp->thumb, WALLPAPER_THUMB_W * WALLPAPER_THUMB_H * 4);
sys_close(cfd);
wp->valid = 1;
} else {
int fd = sys_open(wp->path, "r"); int fd = sys_open(wp->path, "r");
if (fd >= 0) { if (fd >= 0) {
int size = sys_seek(fd, 0, 2); // SEEK_END int size = sys_seek(fd, 0, 2); // SEEK_END
@ -429,12 +446,22 @@ static void decode_wallpapers_task(void *arg) {
scale_rgba_to_argb(img, img_w, img_h, wp->thumb, WALLPAPER_THUMB_W, WALLPAPER_THUMB_H); scale_rgba_to_argb(img, img_w, img_h, wp->thumb, WALLPAPER_THUMB_W, WALLPAPER_THUMB_H);
wp->valid = 1; wp->valid = 1;
stbi_image_free(img); stbi_image_free(img);
// Save to cache
sys_mkdir("/Library/Caches");
sys_mkdir("/Library/Caches/Thumbnails");
int swfd = sys_open(cache_path, "w");
if (swfd >= 0) {
sys_write_fs(swfd, wp->thumb, WALLPAPER_THUMB_W * WALLPAPER_THUMB_H * 4);
sys_close(swfd);
} }
free(buf); // Release memory }
free(buf);
} }
} }
sys_close(fd); sys_close(fd);
} }
}
wallpaper_count++; wallpaper_count++;
} }
@ -624,11 +651,7 @@ static void control_panel_paint_wallpaper(ui_window_t win) {
widget_button_draw(&settings_ctx, &btn_wp_thumbs[i]); widget_button_draw(&settings_ctx, &btn_wp_thumbs[i]);
if (wallpapers[i].valid) { if (wallpapers[i].valid) {
for (int py = 0; py < WALLPAPER_THUMB_H; py++) { ui_draw_image(win, button_x + tx + 4, button_y + ty + 4, WALLPAPER_THUMB_W, WALLPAPER_THUMB_H, wallpapers[i].thumb);
for (int px = 0; px < WALLPAPER_THUMB_W; px++) {
ui_draw_rect(win, button_x + tx + 4 + px, button_y + ty + 4 + py, 1, 1, wallpapers[i].thumb[py * WALLPAPER_THUMB_W + px]);
}
}
} }
ui_draw_string(win, button_x + tx + 8, button_y + ty + WALLPAPER_THUMB_H + 8, wallpapers[i].name, 0xFFFFFFFF); ui_draw_string(win, button_x + tx + 8, button_y + ty + WALLPAPER_THUMB_H + 8, wallpapers[i].name, 0xFFFFFFFF);
} }

View file

@ -20,6 +20,53 @@ static volatile const char *pending_wallpaper_path = NULL;
static char pending_path_buf[256]; static char pending_path_buf[256];
#define WALLPAPER_CONF_PATH "/Library/BWM/Wallpaper/wallpaper" #define WALLPAPER_CONF_PATH "/Library/BWM/Wallpaper/wallpaper"
#define WALLPAPER_CACHE_PATH "/Library/Caches/Wallpaper.bin"
static void wallpaper_save_cache(void) {
if (!wp_pixels || wp_width <= 0 || wp_height <= 0) return;
fat32_mkdir("/Library/Caches");
FAT32_FileHandle *fh = fat32_open(WALLPAPER_CACHE_PATH, "w");
if (fh && fh->valid) {
uint32_t w = (uint32_t)wp_width;
uint32_t h = (uint32_t)wp_height;
fat32_write(fh, &w, 4);
fat32_write(fh, &h, 4);
fat32_write(fh, wp_pixels, w * h * 4);
fat32_close(fh);
}
}
static int wallpaper_load_cache(void) {
if (!fat32_exists(WALLPAPER_CACHE_PATH)) return 0;
FAT32_FileHandle *fh = fat32_open(WALLPAPER_CACHE_PATH, "r");
if (!fh || !fh->valid) return 0;
uint32_t w, h;
fat32_read(fh, &w, 4);
fat32_read(fh, &h, 4);
if (w != (uint32_t)get_screen_width() || h != (uint32_t)get_screen_height()) {
fat32_close(fh);
return 0;
}
if (!wp_pixels) {
wp_pixels = (uint32_t*)kmalloc(MAX_WP_WIDTH * MAX_WP_HEIGHT * sizeof(uint32_t));
if (!wp_pixels) { fat32_close(fh); return 0; }
}
uint32_t total_size = w * h * 4;
uint32_t bytes_read = fat32_read(fh, wp_pixels, total_size);
fat32_close(fh);
if (bytes_read == total_size) {
wp_width = (int)w;
wp_height = (int)h;
graphics_set_bg_image(wp_pixels, wp_width, wp_height);
return 1;
}
return 0;
}
// Simple nearest-neighbor scale from decoded RGBA to ARGB pixel buffer // Simple nearest-neighbor scale from decoded RGBA to ARGB pixel buffer
static void scale_rgba_to_argb(const unsigned char *rgba, int src_w, int src_h, static void scale_rgba_to_argb(const unsigned char *rgba, int src_w, int src_h,
@ -72,6 +119,7 @@ static int decode_and_set_wallpaper(const unsigned char *jpg_data, unsigned int
stbi_image_free(rgba); stbi_image_free(rgba);
graphics_set_bg_image(wp_pixels, wp_width, wp_height); graphics_set_bg_image(wp_pixels, wp_width, wp_height);
wallpaper_save_cache();
return 1; return 1;
} }
@ -190,6 +238,9 @@ int wallpaper_get_width(void) { return wp_width; }
int wallpaper_get_height(void) { return wp_height; } int wallpaper_get_height(void) { return wp_height; }
void wallpaper_init(void) { void wallpaper_init(void) {
if (wallpaper_load_cache()) {
serial_str("[WALLPAPER] Loaded from cache\n");
} else {
wallpaper_load_setting(); wallpaper_load_setting();
if (pending_wallpaper_path == NULL) { if (pending_wallpaper_path == NULL) {
@ -200,3 +251,4 @@ void wallpaper_init(void) {
} }
} }
} }
}