gpui-cc Course handbook
CHAPTER 04 / STAGE 13COURSE

Coalesce redraws without dropping input

Present on demand and preserve event order.

redraw-coalesce
On this page

Learn: receiving two events need not mean presenting twice

An update may dirty layout/paint; an expose may need only the existing pixels. A runtime turn consumes a bounded batch, produces any required new frame, and presents at most once at the end. If an early event changes geometry, later input in the same batch still needs fresh hit data before dispatch. That does not require an intermediate native presentation.

Run it: compare one turn and two

Terminal
./build/examples/core-lab dirty-batch
./build/examples/core-lab dirty-batch separate
C++ · core-lab.cpp
void dirty_batch(bool separate) {
    int model = 0, presentations = 0;
    bool dirty = false;
    const auto change = [&] { ++model; dirty = true; };
    const auto end_turn = [&] {
        if (dirty) ++presentations;
        dirty = false;
    };
    change();
    if (separate) end_turn();
    change();
    end_turn();
    end_turn(); // idle
    std::cout << "model " << model << "; presentations " << presentations << '\n';
}

Expect model 2; presentations 1, then model 2; presentations 2. The idle call changes neither. Try it: remove the reset of dirty and see the idle call erroneously produce another presentation.

Build it: separate invalidation, preparation, and presentation

Your challenge: Use explicit pending work to rebuild/present on demand, not repeatedly while idle. Preserve event order; close wins even with dirty work pending.

Introduce a small runtime entry point taking a finite sequence of normalized events, with a recorded/headless presenter for tests. Initial startup and a changed viewport request new work. An action that does not change model or focus requests none. Before resolving a later input event, refresh obsolete layout/interaction records; commit the final scene and corresponding hit records together. At turn end present once if work remains. A matching-size expose may request presentation without rebuilding; don’t reuse stale-size pixels. A close stops the loop before any additional frame.

On the native side, block for the first event when clean, then drain only a bounded number of queued events (xcb_poll_for_event) before serving the turn. Handle XCB connection loss after a null event, and don’t block for more events with dirty work or buffered input unserved. In test code inject events directly; don’t sleep to create a batch.

Check it

Using a recording presenter: initial dirty → one present. Two effective changes in one turn → one more present and the result of both updates. A no-op and idle turn → zero. Expose with current pixels → one present and no extra rebuild. Resize plus click → uses updated target even if final presentation is later. Update followed by close in the same turn → no post-close presentation.

Terminal
python3 course/check_core.py redraw-coalesce

Hints and troubleshooting

Hint: either a click is dropped or every event redraws

Log event processing and actual presents on separate lines. Count semantic updates before debugging dirty flags. Check whether the code invalidates during a frame and accidentally clears deferred work.