From 3e0777503d2e9abda611e9940f5d791d1a7a1b17 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Fri, 13 Nov 2026 21:48:08 -0800 Subject: [PATCH] Draw composed TileGrid cells in batches The compose pass queued one LVGL draw task per changed cell and drew them all at the end. LVGL walks its whole task list to add or dispatch each task, so a full redraw took time with the square of the cell count. A 1600 cell grid took 4.8 s, and stopping code.py while a big grid was redrawing held the LVGL lock long enough to hang USB. Drawing every 64 queued cells makes it linear and caps the queue at 64 tasks instead of one per cell. A tail pointer in lv_draw_add_task() would fix the root cause upstream. Co-Authored-By: Claude Opus 5.5 --- shared-module/displayio/lvgl/TileGrid.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/shared-module/displayio/lvgl/TileGrid.c b/shared-module/displayio/lvgl/TileGrid.c index 94023ea09d6..ca87691ae41 100644 --- a/shared-module/displayio/lvgl/TileGrid.c +++ b/shared-module/displayio/lvgl/TileGrid.c @@ -803,6 +803,10 @@ static void _cell_display_area(displayio_tilegrid_t *self, uint32_t x, uint32_t area->y2 = area->y1 + self->tile_height * self->lv_scale - 1; } +// The most cells a compose pass queues before drawing them. LVGL walks the whole task list to add +// or dispatch each task, so drawing a long queue at once takes time with the square of its length. +#define DISPLAYIO_LVGL_COMPOSE_BATCH 64 + // Compose every cell that changed into the buffer. Returns false when nothing was drawn so the // caller can skip the invalidation. `dirty` comes back as the union of the composed cells, in // container coordinates. @@ -837,6 +841,7 @@ static bool _compose(displayio_tilegrid_t *self, lv_area_t *dirty) { lv_layer_t layer; lv_canvas_init_layer(self->lv_canvas, &layer); lv_area_t layer_clip = layer._clip_area; + uint32_t queued = 0; uint32_t cells = _cell_count(self); uint32_t tiles_w = self->width_in_tiles; @@ -887,6 +892,12 @@ static bool _compose(displayio_tilegrid_t *self, lv_area_t *dirty) { }; lv_draw_image(&layer, &dsc, &coords); layer._clip_area = layer_clip; + queued++; + if (queued == DISPLAYIO_LVGL_COMPOSE_BATCH) { + lv_canvas_finish_layer(self->lv_canvas, &layer); + lv_canvas_init_layer(self->lv_canvas, &layer); + queued = 0; + } } } _clear_cell(self, cell);