gpui-cc Course handbook
CHAPTER 02 / STAGE 04COURSE

Present a CPU-produced frame

Put pixels you own into the native window.

present-frame
On this page

Learn: separate the image from its destination

At the end of Stage 3 the window is a real server resource, but its background is still the server’s work. Build your first frame by writing bytes in your process and transferring them to that destination. A useful mental picture is color → owned CPU buffer → native upload → window; the window ID is never a pointer to the buffer.

RGB channels describe a color. A byte layout describes how to send it. A depth-24 drawable often consumes 32 storage bits per pixel: 24 for RGB and eight spare bits. The order of bytes on the wire-facing client image depends on the server’s image format. Red and blue must differ in your first test, or a swapped byte order can go unnoticed.

Run it: inspect the bytes of one color

Compile core-lab.cpp once from the project root; later lessons reuse this binary.

Terminal
mkdir -p build/examples
c++ -std=c++20 course/examples/core-lab.cpp -o build/examples/core-lab
./build/examples/core-lab pixel-bytes
C++ · core-lab.cpp
void pixel_bytes() {
    const unsigned r = 0x24, g = 0x68, b = 0xb0;
    const unsigned pixel = (r << 16) | (g << 8) | b;
    for (unsigned shift : {0u, 8u, 16u, 24u}) {
        std::cout << std::hex << std::setw(2) << std::setfill('0')
                  << ((pixel >> shift) & 0xff) << ' ';
    }
    std::cout << '\n';
}

Expect b0 68 24 00 (whitespace after the last byte is immaterial). The value represents #2468b0 with blue in the low byte. Shifts choose the bytes explicitly; reading raw integer memory would make host byte order a hidden dependency. Try it: copy the example to the ignored build directory, reverse the shift list, and observe 00 24 68 b0.

Then find out what your server actually expects with inspect-visual.cpp. It walks two kinds of server-owned records: a pixmap format describes storage for a depth, while a visual describes how the root’s color bits are interpreted. Both are borrowed from the connection’s setup.

Terminal
c++ -std=c++20 course/examples/inspect-visual.cpp -o build/examples/inspect-visual \
  $(pkg-config --cflags --libs xcb)
./build/examples/inspect-visual

On the course’s private X11 profile, expect depth 24; bpp 32; scanline pad 32 and masks ff0000 ff00 ff. Byte order and request limit are also printed; treat them as negotiated data, not constants. On a machine without a usable desktop, run the example with xvfb-run -a if installed. The sample does not create a window or send pixels.

Build it: send a solid frame, not a window background

Your challenge: Keep --probe-display and the default window. Add --clear-rgb RRGGBB for the native check. Open the same 640 × 480 window titled gpui-cc, with a fixed black native background. Present the requested color by filling and uploading an owned CPU buffer.

  1. In the native code you built in Chapter 1, inspect the selected screen’s root_depth and root_visual. Walk permitted depths and visuals with xcb_screen_allowed_depths_iterator and xcb_depth_visuals_iterator; match the chosen visual ID. Walk xcb_setup_pixmap_formats_iterator for the matching depth’s bits per pixel and scanline pad. Read xcb_get_setup(connection)->image_byte_order. For this edition require 24-bit TrueColor, 32 bpp, RGB masks 00ff0000, 0000ff00, 000000ff, and scanline pad 32; report unsupported formats rather than guessing. The spare byte is not an alpha channel.
  2. Use a small value type for color and an owned byte buffer for the X11 representation. At the supported format, row bytes are width * 4. Allocate only after checking the dimensions and byte count. Parse exactly six hex digits for --clear-rgb; for example, use std::from_chars with base 16, and check its error code and that it consumed the whole argument.
  3. Create an X11 graphics context: an ID carrying drawing settings, separate from your window. Use xcb_generate_id, then xcb_create_gc_checked with the window as drawable. For each band of rows, call xcb_put_image_checked with XCB_IMAGE_FORMAT_Z_PIXMAP, window and GC, band width and height, destination x 0 and band y, left pad 0, depth, byte count, and a pointer to the band. Determine a band that fits the request limit before uploading; start small (for example 16 rows at the initial width) and still validate it. XCB reports the maximum request length in 4-byte units via xcb_get_maximum_request_length; leave room for protocol overhead. Check requests while debugging and flush. Release the GC before disconnecting.
  4. Wait until the window is ready to show content, such as its first Expose after selecting XCB_EVENT_MASK_EXPOSURE. Re-present the matching image on later exposes. Keep that work separate from the window’s close path. Chapter 2 does not add rendering callbacks or a GPU.

This fragment shows the argument groups for one band. It assumes your size checks and span selection have already succeeded:

C++
auto cookie = xcb_put_image_checked(
    connection, XCB_IMAGE_FORMAT_Z_PIXMAP, window, gc,
    band_width, band_height, 0, destination_y,
    0, drawable_depth, band_byte_count, band_data);

destination_y is measured in rows of the window; band_byte_count is measured in bytes of the source image. Don’t use the same offset for both. As with the map sample, the cookie is not a confirmation.

See the X11 PutImage definition for exact request constraints. The course’s CPU path is not how GPUI-CE’s native WGPU renderer works; the source notes explain the shared separation between preparing a frame and presenting it.

Check it

Terminal
python3 course/native_preview.py present-frame

It runs two selected colors and reads three actual X11 pixels for each. It must reject untouched black and a red/blue swap. As a separate review, trace the bytes back to your CPU buffer: an observer alone cannot prove how the color was produced. Keep the earlier close check green. Use --xvfb PATH if the server is not on PATH.

Hints and troubleshooting

Hint 1: the native window appears but stays black

Check whether your exposure branch is reached and whether you actually issue a pixel transfer. Inspect xcb_request_check before changing your pixel loop.

Hint 2: blue and red are swapped, or upload fails

Print one packed pixel and the first four storage bytes. Check server byte order, drawable depth, row bytes, and request units separately. The native background should stay black for both color cases.