5.7 kB 86 lines
7e68a437
⋯
toolkit/c8ui/DRAWING.md
download raw history
at main Markdown

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 that clips, plus the entrance slide; c8ui_widget_queue_draw_rect for a piece of a widget that draws many things, nothing added round it), the root keeps it as a region (damage_box in core.c; over 32 rects becomes everything) and the host takes it with c8ui_root_take_damage (NULL = everything), so c8ui_host_impl is 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 says tick_damages (it called queue_draw_rect itself: Start). A widget whose draw fn moves or scales its children sets w->moves_children while 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; blank for a new one, as after rest, 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, and wl_surface.damage_buffer only for what changed this frame. C8UI_FULL_REDRAW=1 draws everything every frame (to compare screenshots against), C8UI_FRAME_LOG=1 says 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 a queue_draw on that widget too, or queue_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 a queue_draw when it changes, as nothing else redraws the window any more. In a draw fn c8ui_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 and c8ui_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 through c8ui_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); never cairo_scale + CAIRO_FILTER_GOOD in a draw fn (about 4 ms a tile). c8ui_kept_draw keeps 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), and c8ui_list_set_checks gives a list's items check boxes (a row's first C8UI_LIST_CHECK_WIDTH px, a cell's top right corner; a click there toggles one, the rest staying), and any multiple list selects by a band dragged from off the items. c8ui_label_set_style. Presses and releases carry mods too (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_keep does 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 calls c8ui_list_redraw/_redraw_item when content changes and c8ui_list_not_final for 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_draw skips a widget without children whose rect (plus CULL_MARGIN, 16 px) is outside the cairo clip, so a leaf must not draw further than that outside its rect. c8ui_text_size keeps 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/_draw for the line over a control, C8UI_MULTI_CLICK_MS, c8ui_push_event for anything pressed like a button, c8ui_button_face, c8ui_hover_item, c8ui_dotted_rect for the focus ring) are static inline in c8ui-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_draw keeps the layouts of what it drew (512 per thread, texts up to 256 bytes, pango_cairo_update_layout for the next context), and an SVG icon its last two raster sizes.