Task 17b: The render thread

Goal

Drawing is not on the thread that runs the script. The UI thread runs the interpreter, the style, the layout, and the paint, and makes a scene. The render thread draws the newest scene and shows it. A slow script does not stop a frame that is ready, and a slow draw or a wait for the display does not stop the script.

The plan has the reason in decision D23.

Files

Interfaces

// src/scene.zig
/// A region of the atlas page with its new bytes.
pub const AtlasPatch = struct { x: u32, y: u32, w: u32, h: u32, bytes: []const u8 };
/// All that the render thread needs for one frame. It has no pointer
/// into the widget tree or the interpreter.
pub const Frame = struct {
    scene: Scene,
    patches: std.ArrayListUnmanaged(AtlasPatch) = .empty,
    serial: u64 = 0,
    pub fn reset(f: *Frame) void;
};

// src/render/thread.zig
pub const Mode = enum { threaded, inline_draw };
pub const Target = struct {
    ctx: *anyopaque,
    /// Draws and shows one frame. It runs on the render thread.
    draw: *const fn (ctx: *anyopaque, frame: *const Frame) anyerror!void,
};
pub const RenderThread = struct {
    pub fn init(gpa: std.mem.Allocator, mode: Mode, target: Target) !*RenderThread;
    /// Stops the thread after its current frame and joins it.
    pub fn deinit(r: *RenderThread) void;
    /// The frame that the UI thread fills next. It is never the frame
    /// that the render thread reads.
    pub fn back(r: *RenderThread) *Frame;
    /// Hands the back frame to the render thread and returns at once.
    /// A frame that the render thread did not start is replaced.
    pub fn submit(r: *RenderThread) void;
    /// Blocks until the render thread showed all submitted frames.
    pub fn flush(r: *RenderThread) void;
    /// Counts, for the tests and the statistics.
    drawn: std.atomic.Value(u64),
    replaced: std.atomic.Value(u64),
};

Steps

  1. Three Frame values exist: one that the UI thread fills, one that waits, and one that the render thread reads. submit swaps the back frame with the waiting frame under a mutex and wakes the render thread. The render thread swaps the waiting frame with its own frame before a draw. No frame is copied and no thread waits for the other in submit.
  2. The render thread owns the renderer, its GPU objects, and the present step. For the Vulkan renderer, all Vulkan calls after init are on the render thread. For the CPU renderer, the image, changedRows, and the write to the display are on the render thread.
  3. The atlas. The UI thread makes glyph images during the paint and keeps the atlas page. For each change, it adds an AtlasPatch with a copy of the bytes to the frame. The render thread has its own copy of the page (the CPU renderer) or the texture (Vulkan) and applies the patches before the draw. When a waiting frame is replaced, its patches move to the new frame, in order. No patch is lost.
  4. Platform.draw is replaced by Platform.target(window) Target. The App fills back() in the paint stage and calls submit. The backend gives the frame event on the UI thread, as before.
  5. A resize. The UI thread sends the new size in the next frame (scene.width, scene.height). The render thread makes its target again when the size of a frame differs from its target.
  6. inline_draw runs draw inside submit on the calling thread. The golden tests, the oracle, and a platform with no threads (the web build of Stage 3) use it. Both modes give the same pixels.
  7. update and update idletasks call flush after submit, so that a script and a test see the frame on the display when update returns.
  8. Shutdown: deinit runs before the backend deletes the window and before Vulkan.deinit.
  9. The render thread touches no widget, no style, no interpreter value, and no allocator of the UI thread, except the frame it owns. Each Frame has its own arena.
  10. The frame statistics of task 18 measure the stages of the UI thread and the draw time of the render thread separately.
  11. Threads need no libc in Zig on Linux. The static build of task 20 keeps the render thread.

Tests

zig build test -Dtest-filter=renderthread

Acceptance

Out of scope