Files
subhobhai943andClaude Opus 5 2136b6fb4c perf(gui): Present only changed pixels and cache the wallpaper
The compositor recomposited and re-uploaded the entire screen on every pass of
its loop. At the 1280x720 the desktop actually boots into, gui_gfx_present()
memcpy'd 3.6 MB into the framebuffer per frame, and the background underneath
it was recomputed pixel by pixel every time.

Present only the damage
- Keep a RAM shadow of what the framebuffer currently holds and diff the back
  buffer against it, pushing just the changed span of each row. Reading the
  shadow costs a RAM access; the framebuffer write it avoids costs far more,
  and the framebuffer is never read back.
- Each row is narrowed to its first and last differing pixel rather than
  tracked as a set of runs: one memcpy per row keeps the loop tight, and a row
  that changed in two distant places is rare enough that bridging the gap is
  cheaper than the bookkeeping.
- gui_gfx_invalidate() forces a full push for when something outside the
  compositor owns the screen, which the desktop calls on entry and on exit
  around the framebuffer console.

Cache the wallpaper
- The vertical gradient, the blended grid and the branding text never change
  between frames, so they are painted once into a cached buffer and blitted
  back per frame. The cache is keyed on resolution and the grid setting, so
  toggling the grid from the context menu still repaints it.

No drawing code changed: the damage is derived from the frame itself, so every
widget, window and app keeps rendering exactly as before.

Measured in QEMU at 1280x720: an idle desktop with two windows open went from
921600 pixels per frame to 3609, about 0.4% of a full blit. A screendump of
the running desktop is unchanged -- wallpaper, grid, icons, both windows,
taskbar and cursor all render identically.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0161Jh7uCEMLYEMNPsjC1LKf
2026-08-25 07:06:08 +00:00

60 lines
2.5 KiB
C

#ifndef _GUI_GFX_H
#define _GUI_GFX_H
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <gui/gui_theme.h>
#define GUI_DEFAULT_WIDTH 800
#define GUI_DEFAULT_HEIGHT 600
typedef struct {
int x;
int y;
int width;
int height;
} gui_rect_t;
void gui_gfx_init(uint32_t* fb, int width, int height);
int gui_gfx_get_width(void);
int gui_gfx_get_height(void);
uint32_t* gui_gfx_get_backbuffer(void);
void gui_gfx_clear(uint32_t color);
void gui_gfx_draw_pixel(int x, int y, uint32_t color);
void gui_gfx_draw_pixel_blend(int x, int y, uint32_t color, uint8_t alpha);
void gui_gfx_draw_line(int x0, int y0, int x1, int y1, uint32_t color);
void gui_gfx_draw_rect(int x, int y, int w, int h, uint32_t color);
void gui_gfx_fill_rect(int x, int y, int w, int h, uint32_t color);
void gui_gfx_fill_rect_blend(int x, int y, int w, int h, uint32_t color, uint8_t alpha);
void gui_gfx_draw_rounded_rect(int x, int y, int w, int h, int r, uint32_t color, bool filled);
void gui_gfx_draw_gradient_v(int x, int y, int w, int h, uint32_t top_col, uint32_t bot_col);
void gui_gfx_draw_gradient_h(int x, int y, int w, int h, uint32_t left_col, uint32_t right_col);
// Sharp Typography & Font Rendering
void gui_gfx_draw_char(int x, int y, char c, uint32_t fg, uint32_t bg, bool transparent_bg);
void gui_gfx_draw_char_16(int x, int y, char c, uint32_t fg, uint32_t bg, bool transparent_bg);
void gui_gfx_draw_string(int x, int y, const char* str, uint32_t fg);
void gui_gfx_draw_string_16(int x, int y, const char* str, uint32_t fg);
void gui_gfx_draw_string_bg(int x, int y, const char* str, uint32_t fg, uint32_t bg);
void gui_gfx_draw_string_shadow(int x, int y, const char* str, uint32_t fg, uint32_t shadow);
void gui_gfx_draw_string_16_shadow(int x, int y, const char* str, uint32_t fg, uint32_t shadow);
void gui_gfx_draw_shadow(int x, int y, int w, int h, int blur);
void gui_gfx_blit(int dx, int dy, int w, int h, const uint32_t* src_buf);
// Push the composited frame to the display. Only the pixels that differ from
// the previous frame are written, so an idle desktop costs almost no
// framebuffer traffic.
void gui_gfx_present(void);
// Force the next present to push the whole screen. Needed whenever something
// outside the compositor has drawn over the framebuffer, such as the kernel
// console before the desktop takes the display back.
void gui_gfx_invalidate(void);
// Pixels written to the framebuffer by the last present. Diagnostic only.
uint64_t gui_gfx_last_present_pixels(void);
#endif // _GUI_GFX_H