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;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SlotState {
Available,
Writing,
Committed,
Acquired,
}
#[derive(Debug, Clone)]
pub struct ShmFrameSlot {
pub sequence: u64,
pub timestamp: Instant,
pub width: u32,
pub height: u32,
pub stride: u32,
pub damage_rects: Vec<Rect>,
pub display_list: DisplayList,
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(),
}
}
}
pub struct ShmRingBuffer {
capacity: usize,
slots: Vec<RwLock<ShmFrameSlot>>,
slot_states: Vec<RwLock<SlotState>>,
head_sequence: AtomicU64,
tail_sequence: AtomicU64,
latest_committed_index: AtomicUsize,
dropped_frames: AtomicU64,
}
impl ShmRingBuffer {
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),
})
}
pub fn capacity(&self) -> usize {
self.capacity
}
pub fn dropped_frames(&self) -> u64 {
self.dropped_frames.load(Ordering::Relaxed)
}
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
}
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);
}
}
}
}
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
}
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
}
}
}
#[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];
}
}
}
struct AttachedSurface {
surface_id: usize,
ring_buffer: Arc<ShmRingBuffer>,
last_sequence: u64,
viewport_offset: (f32, f32),
visible: bool,
}
pub struct ZeroCopyCompositor {
surfaces: Vec<AttachedSurface>,
telemetry: CompositorTelemetry,
}
impl Default for ZeroCopyCompositor {
fn default() -> Self {
Self::new()
}
}
impl ZeroCopyCompositor {
pub fn new() -> Self {
Self {
surfaces: Vec::new(),
telemetry: CompositorTelemetry::default(),
}
}
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,
});
}
pub fn detach_surface(&mut self, surface_id: usize) {
self.surfaces.retain(|s| s.surface_id != surface_id);
}
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;
}
}
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 {
for cmd in slot.display_list.commands {
master_display_list.push(cmd);
}
} else {
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
}
pub fn telemetry(&self) -> &CompositorTelemetry {
&self.telemetry
}
}
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(),
}
}