c8ui: damage and drawing #
Part of the toolkit's notes (CLAUDE.md beside this file).
- Damage: a frame draws and submits only what changed. Widgets say
what (
c8ui_widget_queue_draw(w): w's rect and 16 px round it, cut to every ancestor thatclips, plus the entrance slide;c8ui_widget_queue_draw_rectfor a piece of a widget that draws many things, nothing added round it), the root keeps it as a region (damage_boxin core.c; over 32 rects becomes everything) and the host takes it withc8ui_root_take_damage(NULL = everything), soc8ui_host_implis as it was and a host that never asks draws it all. Everything is still the answer for a layout (queue_layout, anything added, removed, hidden or resized), overlays coming and going, the theme, tooltips, keyboard focus of the window. After a tick the root damages the ticking widget's rect, unless its impl saystick_damages(it calledqueue_draw_rectitself: Start). A widget whose draw fn moves or scales its children setsw->moves_childrenwhile it does (pivot and flip view mid-slide, zoom moving, viewbox always): a child's damage is then the whole of it. c8ui-wayland (render): the root's region to buffer pixels rounded outwards (fractional scales), per buffer what it has yet to catch up on (stale: the two take turns, so each missed the other's frames;blankfor a new one, as afterrest, unmap, suspend or a new size), the frame clipped to both, the background painted with SOURCE inside the clip (see-through windows are cleared, not painted over), all of it when over 70 % is damaged, andwl_surface.damage_bufferonly for what changed this frame.C8UI_FULL_REDRAW=1draws everything every frame (to compare screenshots against),C8UI_FRAME_LOG=1says on stderr what each frame drew and how long it took. What a widget author must do: a change that shows only inside the widget needs nothing new (queue_draw(w)); one that shows elsewhere needs aqueue_drawon that widget too, orqueue_layout; never draw further than 16 px outside the rect; state that isn't the widget's own (a global the draw fn reads) needs aqueue_drawwhen it changes, as nothing else redraws the window any more. In a draw fnc8ui_clip_shows(cr, x, y, w, h)says whether a piece is among what is being drawn (the clip can be several rects apart), to skip the slow ones. A list damages single items for hover andc8ui_list_redraw_item(an item's draw fn must keep to its rect). Start with live tiles ticking, 1920x1080 headless pixman: 7.5 ms a frame to about 2 (a tile is 2-4 % of the window; what's left is drawing its face), the shell from 1.3-1.5 s of CPU a minute to 0.4. - Drawing fast (
kept.c): a scroll still draws its whole rect again, in software, for every frame, so nothing slow may happen per frame. Pictures at another size go throughc8ui_draw_surface(_cover,_fit), which keeps a copy at the drawn size with the surface and copies it after (the image widget, PNG icons, Files' thumbnails, the shell's live tile and toast pictures, News); nevercairo_scale+CAIRO_FILTER_GOODin a draw fn (about 4 ms a tile).c8ui_kept_drawkeeps any drawing as its pixels in a slot (state + size say when to draw again; fn returning false = not final, shown but not kept; all of them held to 64 MB, least recently drawn first), andc8ui_list_set_checksgives a list's items check boxes (a row's firstC8UI_LIST_CHECK_WIDTHpx, a cell's top right corner; a click there toggles one, the rest staying), and anymultiplelist selects by a band dragged from off the items.c8ui_label_set_style. Presses and releases carrymodstoo (c8ui_root_set_mods, which c8ui-wayland calls before each button): before, a list never saw Ctrl or Shift with a click, so neither added to a selection.c8ui_list_open_point(list, &x, &y)in an open fn says whether a click or tap opened the item and where, for items with parts of their own to press (Cirrus' like button). A scroll that only goes up and down leaves fingers going mostly sideways (|dx| > 2|dy|, touchpad) to what it is in, so a hub of scrolling columns still pans; untested on a real touchpad.c8ui_list_set_keepdoes that per list item: the draw fn then runs only for a new item or a changed state, gets its rect at 0, 0, and the app callsc8ui_list_redraw/_redraw_itemwhen content changes andc8ui_list_not_finalfor a stand-in. Kept drawings land on whole pixels. Decode pictures off the main loop (News'image.c, a GTask). Untested: fractional scales, a zoom animating over kept items. - Drawing:
c8ui_widget_drawskips a widget without children whose rect (plusCULL_MARGIN, 16 px) is outside the cairo clip, so a leaf must not draw further than that outside its rect.c8ui_text_sizekeeps its answers (keyed by font, text and limits). Helpers several widget files share (c8ui_activates,c8ui_stroke_rect,c8ui_icon_draw_tinted,c8ui_first_focusable,c8ui_now_ms,c8ui_ease_out,c8ui_dup_or_null,c8ui_header_height/_drawfor the line over a control,C8UI_MULTI_CLICK_MS,c8ui_push_eventfor anything pressed like a button,c8ui_button_face,c8ui_hover_item,c8ui_dotted_rectfor the focus ring) arestatic inlineinc8ui-private.h. The root redraws a widget after ENTER/LEAVE and on PRESS/RELEASE (update_hover,pointer_button), so event fns needn't.c8ui_text_drawkeeps the layouts of what it drew (512 per thread, texts up to 256 bytes,pango_cairo_update_layoutfor the next context), and an SVG icon its last two raster sizes.