gpui-cc Course handbook
CHAPTER 02 / STAGE 05COURSE

Resize without presenting stale pixels

Resize storage and repaint at the new extent.

resize-frame
On this page

Learn: dimensions belong to a generation of storage

A resize gives your buffer a new row length. A raw pointer, span, or row index derived before the resize cannot be reused afterward. ConfigureNotify reports the actual drawable dimensions; not every configure event changes them. Expose asks for pixels in the current window, and does not necessarily allocate a new buffer.

Run it: count allocations across a trace

Terminal
./build/examples/core-lab resize-replay
C++ · core-lab.cpp
void resize_replay() {
    int width = 0, height = 0, allocations = 0;
    for (const auto next : {std::array{2, 2}, std::array{2, 2}, std::array{3, 2}}) {
        if (next[0] != width || next[1] != height) {
            width = next[0];
            height = next[1];
            ++allocations;
        }
        std::cout << width << 'x' << height << " allocations: " << allocations << '\n';
    }
}

Expect widths/heights 2x2, 2x2, 3x2 and allocation counts 1, 1, 2. Try it: add the same 3x2 a second time. The count must not change. This is state tracking, not an allocator or an X11 resize handler.

Build it: validate sizes and rebuild only when they change

Your challenge: Rebuild an owned, current-size surface after an actual resize, then redraw. Repeated size and expose events must not reallocate an otherwise usable buffer. Reject invalid or over-budget sizes before allocation.

  • Normalize a native ConfigureNotify into a proposed width and height. Check nonzero dimensions, your explicit cap, and the X11 field ranges before converting to smaller or unsigned types. Use a memory cap (e.g. 64 MiB) and check the products without first overflowing them: for an already-validated positive divisor, test a > limit / b before multiplying.
  • On a real change, build a replacement surface and derive its view from the new owner. Don’t mark it uploadable if allocation failed. An expose reuses or regenerates valid pixels at the current dimensions; it must not replay stale storage at the old size.
  • Generalize bands: rows_per_band = min(height, safe_payload / row_bytes) and reject when it is zero. A conservative budget may cap the non-BIG-REQUESTS form at 65535 * 4 bytes and reserve extra header bytes (e.g. 64) before deriving payload; compare against the advertised server limit too. Clip the final band to rows left, and use the offset in both source bytes and destination y.

Check it

Register resize-frame in CTest using the verification setup. Test repeated extents, rejected zero/over-limit inputs, and this forced-small budget case: width=4, height=5, row_bytes=16, usable payload 32; expect row bands (y=0,h=2), (2,2), (4,1). Payload 15 must be rejected. Test an artificial cap even if the actual server advertises a much larger request.

Terminal
python3 course/check_core.py resize-frame
python3 course/native_preview.py resize-frame

The native probe resizes to 695 × 537, samples near the new lower-right corner, and clears the window to provoke an expose. It expects the submitted color to come back. The band test is independent evidence for request sizes.

Hints and troubleshooting

Hint: only the old rectangle lights up after resize

Print the configure size, allocated stride, band offset, and new bottom row from the same frame. Look for a view or width captured before resize. A band’s source starts at y * row_bytes, not its index in the list of bands.