strata-render 0.1.4

High-performance Display List, Stacking Context Compositor, and GPU rasterization pipeline
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Zero-Copy Shared Memory Frame Transfer and Compositing Engine.
//!
//! Provides lock-free ring-buffered display list and pixel frame exchange
//! between sandboxed worker/tab processes and the main browser UI thread compositor,
//! achieving 120 FPS frame consumption with zero IPC copy overhead.

use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, RwLock};
use std::time::Instant;

use crate::compositor::{DisplayList, DrawCommand};
use strata_layout::Rect;

/// State of an individual shared memory frame slot.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SlotState {
    /// Slot is uninitialized or reclaimed for writing.
    Available,
    /// Producer process is actively writing to this slot.
    Writing,
    /// Producer finished writing; slot is ready for consumption.
    Committed,
    /// Consumer is actively sampling or compositing this slot.
    Acquired,
}

/// An individual frame slot inside the shared memory ring buffer.
#[derive(Debug, Clone)]
pub struct ShmFrameSlot {
    /// Monotonically increasing frame sequence identifier.
    pub sequence: u64,
    /// Monotonic creation timestamp.
    pub timestamp: Instant,
    /// Viewport width in device pixels.
    pub width: u32,
    /// Viewport height in device pixels.
    pub height: u32,
    /// Horizontal scanline stride in bytes (e.g. width * 4).
    pub stride: u32,
    /// Dirty damage rectangles updated in this frame.
    pub damage_rects: Vec<Rect>,
    /// Vector display list commands for compositor ingestion.
    pub display_list: DisplayList,
    /// Optional rasterized pixel backing buffer (RGBA8/BGRA8).
    pub pixel_buffer: Vec<u8>,
}

impl Default for ShmFrameSlot {
    fn default() -> Self {
        Self {
            sequence: 0,
            timestamp: Instant::now(),
            width: 0,
            height: 0,
            stride: 0,
            damage_rects: Vec::new(),
            display_list: DisplayList::default(),
            pixel_buffer: Vec::new(),
        }
    }
}

/// Thread-safe and process-safe circular ring buffer for zero-copy frame exchange.
pub struct ShmRingBuffer {
    /// Circular capacity (typically 2 for double-buffering, 3 for triple-buffering).
    capacity: usize,
    /// Slots storage.
    slots: Vec<RwLock<ShmFrameSlot>>,
    /// Per-slot atomic state markers.
    slot_states: Vec<RwLock<SlotState>>,
    /// Monotonic write sequence counter.
    head_sequence: AtomicU64,
    /// Monotonic read sequence counter.
    tail_sequence: AtomicU64,
    /// Index of the most recently committed slot.
    latest_committed_index: AtomicUsize,
    /// Dropped frames counter (when producer commits faster than consumer drains).
    dropped_frames: AtomicU64,
}

impl ShmRingBuffer {
    /// Creates a new circular frame buffer with the specified slot capacity (minimum 2).
    pub fn new(capacity: usize) -> Arc<Self> {
        let cap = capacity.max(2);
        let mut slots = Vec::with_capacity(cap);
        let mut slot_states = Vec::with_capacity(cap);

        for _ in 0..cap {
            slots.push(RwLock::new(ShmFrameSlot::default()));
            slot_states.push(RwLock::new(SlotState::Available));
        }

        Arc::new(Self {
            capacity: cap,
            slots,
            slot_states,
            head_sequence: AtomicU64::new(0),
            tail_sequence: AtomicU64::new(0),
            latest_committed_index: AtomicUsize::new(usize::MAX),
            dropped_frames: AtomicU64::new(0),
        })
    }

    /// Returns the circular buffer capacity.
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Number of dropped frames due to consumer backpressure.
    pub fn dropped_frames(&self) -> u64 {
        self.dropped_frames.load(Ordering::Relaxed)
    }

    /// Acquires a slot for the producer to write a new frame.
    /// Returns the target slot index.
    pub fn begin_write(&self) -> usize {
        let seq = self.head_sequence.fetch_add(1, Ordering::SeqCst);
        let slot_idx = (seq as usize) % self.capacity;

        if let Ok(mut state) = self.slot_states[slot_idx].write() {
            *state = SlotState::Writing;
        }

        slot_idx
    }

    /// Commits a written frame slot, updating its metadata and making it available to the consumer.
    pub fn commit_write(
        &self,
        slot_idx: usize,
        width: u32,
        height: u32,
        damage_rects: Vec<Rect>,
        display_list: DisplayList,
        pixel_buffer: Option<Vec<u8>>,
    ) {
        if slot_idx >= self.capacity {
            return;
        }

        let seq = self.head_sequence.load(Ordering::SeqCst);
        {
            if let Ok(mut slot) = self.slots[slot_idx].write() {
                slot.sequence = seq;
                slot.timestamp = Instant::now();
                slot.width = width;
                slot.height = height;
                slot.stride = width * 4;
                slot.damage_rects = damage_rects;
                slot.display_list = display_list;
                if let Some(pixels) = pixel_buffer {
                    slot.pixel_buffer = pixels;
                }
            }
        }

        if let Ok(mut state) = self.slot_states[slot_idx].write() {
            *state = SlotState::Committed;
        }

        let prev = self.latest_committed_index.swap(slot_idx, Ordering::SeqCst);
        if prev != usize::MAX && prev != slot_idx {
            let tail_seq = self.tail_sequence.load(Ordering::SeqCst);
            if let Ok(prev_slot) = self.slots[prev].read() {
                if prev_slot.sequence > tail_seq {
                    self.dropped_frames.fetch_add(1, Ordering::Relaxed);
                }
            }
        }
    }

    /// Acquires the most recent committed frame for zero-copy read access by the compositor.
    /// Returns a guard or cloned snapshot of the frame if a newer frame exists than `last_sequence`.
    pub fn acquire_latest(&self, last_sequence: u64) -> Option<(u64, ShmFrameSlot)> {
        let committed_idx = self.latest_committed_index.load(Ordering::SeqCst);
        if committed_idx == usize::MAX || committed_idx >= self.capacity {
            return None;
        }

        if let Ok(slot) = self.slots[committed_idx].read() {
            if slot.sequence > last_sequence {
                self.tail_sequence.store(slot.sequence, Ordering::SeqCst);
                return Some((slot.sequence, slot.clone()));
            }
        }

        None
    }

    /// Checks if a newer frame is available than `last_sequence` without reading the full slot.
    pub fn has_new_frame(&self, last_sequence: u64) -> bool {
        let committed_idx = self.latest_committed_index.load(Ordering::Relaxed);
        if committed_idx == usize::MAX || committed_idx >= self.capacity {
            return false;
        }

        if let Ok(slot) = self.slots[committed_idx].read() {
            slot.sequence > last_sequence
        } else {
            false
        }
    }
}

/// Telemetry metrics for the zero-copy compositor.
#[derive(Debug, Default, Clone)]
pub struct CompositorTelemetry {
    pub total_frames_composited: u64,
    pub total_frames_dropped: u64,
    pub last_frame_time_us: u64,
    pub p95_frame_time_us: u64,
    recent_latencies: Vec<u64>,
}

impl CompositorTelemetry {
    pub fn record_frame(&mut self, latency_us: u64) {
        self.total_frames_composited += 1;
        self.last_frame_time_us = latency_us;
        self.recent_latencies.push(latency_us);
        if self.recent_latencies.len() > 100 {
            self.recent_latencies.remove(0);
        }

        let mut sorted = self.recent_latencies.clone();
        sorted.sort_unstable();
        let idx = (sorted.len() as f32 * 0.95) as usize;
        if idx < sorted.len() {
            self.p95_frame_time_us = sorted[idx];
        }
    }
}

/// Registration handle for an attached shared memory surface.
struct AttachedSurface {
    surface_id: usize,
    ring_buffer: Arc<ShmRingBuffer>,
    last_sequence: u64,
    viewport_offset: (f32, f32),
    visible: bool,
}

/// Zero-copy compositor that ingests frames from multiple sandboxed process ring buffers.
pub struct ZeroCopyCompositor {
    surfaces: Vec<AttachedSurface>,
    telemetry: CompositorTelemetry,
}

impl Default for ZeroCopyCompositor {
    fn default() -> Self {
        Self::new()
    }
}

impl ZeroCopyCompositor {
    /// Creates a new zero-copy compositor.
    pub fn new() -> Self {
        Self {
            surfaces: Vec::new(),
            telemetry: CompositorTelemetry::default(),
        }
    }

    /// Attaches an isolated tab or worker surface ring buffer.
    pub fn attach_surface(&mut self, surface_id: usize, ring_buffer: Arc<ShmRingBuffer>) {
        self.surfaces.retain(|s| s.surface_id != surface_id);
        self.surfaces.push(AttachedSurface {
            surface_id,
            ring_buffer,
            last_sequence: 0,
            viewport_offset: (0.0, 0.0),
            visible: true,
        });
    }

    /// Detaches an isolated surface by its identifier.
    pub fn detach_surface(&mut self, surface_id: usize) {
        self.surfaces.retain(|s| s.surface_id != surface_id);
    }

    /// Updates the spatial viewport offset and visibility for a specific attached surface.
    pub fn set_surface_layout(&mut self, surface_id: usize, offset: (f32, f32), visible: bool) {
        if let Some(surf) = self
            .surfaces
            .iter_mut()
            .find(|s| s.surface_id == surface_id)
        {
            surf.viewport_offset = offset;
            surf.visible = visible;
        }
    }

    /// Composites the latest available frames from all attached surfaces into the master display list.
    /// Returns the number of new frames ingested and composited.
    pub fn composite_into(&mut self, master_display_list: &mut DisplayList) -> usize {
        let t_start = Instant::now();
        let mut new_frames_count = 0;

        for surf in &mut self.surfaces {
            if !surf.visible {
                continue;
            }

            if let Some((seq, slot)) = surf.ring_buffer.acquire_latest(surf.last_sequence) {
                surf.last_sequence = seq;
                new_frames_count += 1;

                let (dx, dy) = surf.viewport_offset;
                if dx == 0.0 && dy == 0.0 {
                    // Direct zero-copy command append
                    for cmd in slot.display_list.commands {
                        master_display_list.push(cmd);
                    }
                } else {
                    // Spatial translation
                    for cmd in slot.display_list.commands {
                        master_display_list.push(translate_cmd(&cmd, (dx, dy)));
                    }
                }
            }
        }

        let elapsed_us = t_start.elapsed().as_micros() as u64;
        self.telemetry.record_frame(elapsed_us);

        new_frames_count
    }

    /// Retrieves telemetry statistics for frame consumption and latency.
    pub fn telemetry(&self) -> &CompositorTelemetry {
        &self.telemetry
    }
}

/// Helper function to spatially translate a `DrawCommand`.
fn translate_cmd(cmd: &DrawCommand, offset: (f32, f32)) -> DrawCommand {
    let (dx, dy) = offset;
    match cmd {
        DrawCommand::DrawRect { bounds, color } => DrawCommand::DrawRect {
            bounds: Rect::new(bounds.x + dx, bounds.y + dy, bounds.width, bounds.height),
            color: *color,
        },
        DrawCommand::DrawRoundedRect {
            bounds,
            color,
            border_radius,
            border_width,
            border_color,
        } => DrawCommand::DrawRoundedRect {
            bounds: Rect::new(bounds.x + dx, bounds.y + dy, bounds.width, bounds.height),
            color: *color,
            border_radius: *border_radius,
            border_width: *border_width,
            border_color: *border_color,
        },
        DrawCommand::DrawShadow {
            bounds,
            shadow,
            border_radius,
        } => DrawCommand::DrawShadow {
            bounds: Rect::new(bounds.x + dx, bounds.y + dy, bounds.width, bounds.height),
            shadow: shadow.clone(),
            border_radius: *border_radius,
        },
        DrawCommand::DrawInsetShadow {
            bounds,
            shadow,
            border_radius,
        } => DrawCommand::DrawInsetShadow {
            bounds: Rect::new(bounds.x + dx, bounds.y + dy, bounds.width, bounds.height),
            shadow: shadow.clone(),
            border_radius: *border_radius,
        },
        DrawCommand::DrawLinearGradient {
            bounds,
            angle_deg,
            stops,
            border_radius,
        } => DrawCommand::DrawLinearGradient {
            bounds: Rect::new(bounds.x + dx, bounds.y + dy, bounds.width, bounds.height),
            angle_deg: *angle_deg,
            stops: stops.clone(),
            border_radius: *border_radius,
        },
        DrawCommand::DrawRadialGradient {
            bounds,
            center,
            stops,
            border_radius,
        } => DrawCommand::DrawRadialGradient {
            bounds: Rect::new(bounds.x + dx, bounds.y + dy, bounds.width, bounds.height),
            center: *center,
            stops: stops.clone(),
            border_radius: *border_radius,
        },
        DrawCommand::PushBackdropFilter {
            bounds,
            radius,
            filter,
        } => DrawCommand::PushBackdropFilter {
            bounds: Rect::new(bounds.x + dx, bounds.y + dy, bounds.width, bounds.height),
            radius: *radius,
            filter: filter.clone(),
        },
        DrawCommand::PopBackdropFilter => DrawCommand::PopBackdropFilter,
        DrawCommand::DrawBorder {
            bounds,
            widths,
            color,
            border_radius,
            style,
        } => DrawCommand::DrawBorder {
            bounds: Rect::new(bounds.x + dx, bounds.y + dy, bounds.width, bounds.height),
            widths: *widths,
            color: *color,
            border_radius: *border_radius,
            style: *style,
        },
        DrawCommand::DrawText {
            origin,
            bounds_width,
            bounds_height,
            text_align,
            text,
            font_size,
            line_height,
            font_weight,
            color,
            is_monospace,
            font_family,
        } => DrawCommand::DrawText {
            origin: (origin.0 + dx, origin.1 + dy),
            bounds_width: *bounds_width,
            bounds_height: *bounds_height,
            text_align: *text_align,
            text: text.clone(),
            font_size: *font_size,
            line_height: *line_height,
            font_weight: *font_weight,
            color: *color,
            is_monospace: *is_monospace,
            font_family: *font_family,
        },
        DrawCommand::PushClip { bounds, radius } => DrawCommand::PushClip {
            bounds: Rect::new(bounds.x + dx, bounds.y + dy, bounds.width, bounds.height),
            radius: *radius,
        },
        DrawCommand::PopClip => DrawCommand::PopClip,
        other => other.clone(),
    }
}