use std::vec;
use std::vec::Vec;
use crate::display::{PixelFormat, Xrgb8888};
use crate::engine::VeloxEngine;
use crate::pod::{KeyCode, KeyModifiers};
use crate::solver::Region;
use super::DesktopRenderBackend;
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct WlBuffer {
pub id: u32,
}
#[derive(Clone, Debug, Default)]
pub struct WlSurface {
pub attached_buffer_id: Option<u32>,
pub committed_damage: Vec<Region>,
pub commit_count: usize,
}
impl WlSurface {
pub fn new() -> Self {
Self::default()
}
pub fn attach(&mut self, buffer: Option<&WlBuffer>, _x: i32, _y: i32) {
self.attached_buffer_id = buffer.map(|b| b.id);
self.committed_damage.clear();
}
pub fn damage_buffer(&mut self, x: i32, y: i32, w: i32, h: i32) {
self.committed_damage
.push(Region::new(x as i16, y as i16, w as i16, h as i16));
}
pub fn commit(&mut self) {
self.commit_count += 1;
}
}
pub struct ShmSlot {
pub wl_buffer: WlBuffer,
pub pixels: Vec<Xrgb8888>,
pub in_use: bool,
pub age: u8,
}
impl ShmSlot {
pub fn new(id: u32, _width: usize, _height: usize) -> Self {
Self {
wl_buffer: WlBuffer { id },
pixels: Vec::new(),
in_use: false,
age: 0,
}
}
}
pub struct WaylandSurfaceDriver {
pub slots: [ShmSlot; 2],
pub damage_history: [Vec<Region>; 2],
pub last_raster_regions: Vec<Region>,
pub(crate) active_slot_idx: usize,
width: usize,
height: usize,
backend: DesktopRenderBackend,
#[cfg(feature = "gpu")]
gpu_scene: Option<crate::renderers::wgpu::WgpuScene>,
}
pub type ShmSurfaceDriver = WaylandSurfaceDriver;
pub type SoftwareSurfaceDriver = WaylandSurfaceDriver;
impl WaylandSurfaceDriver {
pub fn new(width: usize, height: usize) -> Self {
let w = width.max(1);
let h = height.max(1);
Self {
slots: [ShmSlot::new(1, w, h), ShmSlot::new(2, w, h)],
damage_history: [Vec::new(), Vec::new()],
last_raster_regions: Vec::new(),
active_slot_idx: 0,
width: w,
height: h,
backend: DesktopRenderBackend::default_desktop(),
#[cfg(feature = "gpu")]
gpu_scene: None,
}
}
#[inline]
pub fn with_backend(mut self, backend: DesktopRenderBackend) -> Self {
self.backend = backend;
self
}
#[inline]
pub fn set_backend(&mut self, backend: DesktopRenderBackend) {
self.backend = backend;
}
#[inline]
pub const fn backend(&self) -> DesktopRenderBackend {
self.backend
}
#[cfg(feature = "gpu")]
#[inline]
pub fn gpu_scene(&self) -> Option<&crate::renderers::wgpu::WgpuScene> {
self.gpu_scene.as_ref()
}
pub fn release_buffer(&mut self, buffer_id: u32) {
for slot in &mut self.slots {
if slot.wl_buffer.id == buffer_id {
slot.in_use = false;
}
}
}
pub fn front_pixels(&self) -> &[Xrgb8888] {
&self.slots[self.active_slot_idx].pixels
}
#[inline(always)]
pub const fn width(&self) -> usize {
self.width
}
#[inline(always)]
pub const fn height(&self) -> usize {
self.height
}
pub fn resize(&mut self, width: usize, height: usize) {
let w = width.max(1);
let h = height.max(1);
self.width = w;
self.height = h;
self.slots = [ShmSlot::new(1, w, h), ShmSlot::new(2, w, h)];
self.damage_history = [Vec::new(), Vec::new()];
self.last_raster_regions.clear();
self.active_slot_idx = 0;
}
pub fn flush_frame(&mut self, core: &mut VeloxEngine, wl_surface: &mut WlSurface) -> bool {
self.flush_frame_dt(core, wl_surface, 16)
}
pub fn flush_frame_dt(
&mut self,
core: &mut VeloxEngine,
wl_surface: &mut WlSurface,
dt_ms: u16,
) -> bool {
let curr_damage: Vec<Region> = core.step_frame_dt(dt_ms).to_vec();
if curr_damage.is_empty() {
return false;
}
let slot_idx = self
.slots
.iter()
.position(|s| !s.in_use)
.unwrap_or((self.active_slot_idx + 1) & 1);
let slot_age = self.slots[slot_idx].age;
let required_len = self.width.saturating_mul(self.height);
if self.slots[slot_idx].pixels.len() != required_len {
self.slots[slot_idx]
.pixels
.resize(required_len, Xrgb8888(0xFF00_0000));
}
let mut raster_regions = curr_damage.clone();
if slot_age == 0 || slot_age > 2 {
raster_regions = vec![core.viewport_region()];
} else if slot_age == 2 {
raster_regions.extend_from_slice(&self.damage_history[0]);
}
#[cfg(feature = "gpu")]
let s = core.dpi_scale().max(1);
let sf = core.scale_factor();
let dst_stride = self.width;
match self.backend {
DesktopRenderBackend::Wgpu => {
#[cfg(feature = "gpu")]
{
let scene = self
.gpu_scene
.get_or_insert_with(crate::renderers::wgpu::WgpuScene::new);
let _ = scene.record_draw_ops(core.cached_draw_ops(), s, &raster_regions);
}
let pixels = &mut self.slots[slot_idx].pixels;
for reg in &raster_regions {
clear_transparent_region(pixels, dst_stride, self.height, sf, *reg);
core.rasterize_strip(*reg, 0, 0, dst_stride, pixels);
}
}
DesktopRenderBackend::Software => {
let pixels = &mut self.slots[slot_idx].pixels;
for reg in &raster_regions {
clear_transparent_region(pixels, dst_stride, self.height, sf, *reg);
core.rasterize_strip(*reg, 0, 0, dst_stride, pixels);
}
}
}
let wl_buf = self.slots[slot_idx].wl_buffer;
wl_surface.attach(Some(&wl_buf), 0, 0);
for reg in &curr_damage {
wl_surface.damage_buffer(reg.x as i32, reg.y as i32, reg.w as i32, reg.h as i32);
}
wl_surface.commit();
self.slots[slot_idx].in_use = true;
self.slots[slot_idx].age = 1;
for (idx, other) in self.slots.iter_mut().enumerate() {
if idx != slot_idx && other.age > 0 {
other.age = other.age.saturating_add(1);
}
}
self.damage_history.rotate_right(1);
self.damage_history[0] = curr_damage;
self.last_raster_regions = raster_regions;
self.active_slot_idx = slot_idx;
true
}
}
#[inline]
fn clear_transparent_region(
pixels: &mut [Xrgb8888],
stride: usize,
height: usize,
scale_factor: f32,
reg: Region,
) {
let sf = scale_factor.clamp(0.5, 4.0);
let x0 = (((reg.x.max(0) as f32) * sf).floor() as usize).min(stride);
let y0 = (((reg.y.max(0) as f32) * sf).floor() as usize).min(height);
let x1 = (((reg.right().max(0) as f32) * sf).ceil() as usize).min(stride);
let y1 = (((reg.bottom().max(0) as f32) * sf).ceil() as usize).min(height);
if x0 >= x1 || y0 >= y1 {
return;
}
for y in y0..y1 {
let row = y * stride;
if row + x1 <= pixels.len() {
pixels[row + x0..row + x1].fill(Xrgb8888(0));
}
}
}
pub fn decode_linux_keycode(code: u16) -> Option<KeyCode> {
match code {
1 => Some(KeyCode::Escape),
2 => Some(KeyCode::Char('1')),
3 => Some(KeyCode::Char('2')),
4 => Some(KeyCode::Char('3')),
5 => Some(KeyCode::Char('4')),
6 => Some(KeyCode::Char('5')),
7 => Some(KeyCode::Char('6')),
8 => Some(KeyCode::Char('7')),
9 => Some(KeyCode::Char('8')),
10 => Some(KeyCode::Char('9')),
11 => Some(KeyCode::Char('0')),
12 => Some(KeyCode::Char('-')),
13 => Some(KeyCode::Char('=')),
14 => Some(KeyCode::Backspace),
15 => Some(KeyCode::Tab),
16 => Some(KeyCode::Char('q')),
17 => Some(KeyCode::Char('w')),
18 => Some(KeyCode::Char('e')),
19 => Some(KeyCode::Char('r')),
20 => Some(KeyCode::Char('t')),
21 => Some(KeyCode::Char('y')),
22 => Some(KeyCode::Char('u')),
23 => Some(KeyCode::Char('i')),
24 => Some(KeyCode::Char('o')),
25 => Some(KeyCode::Char('p')),
28 | 96 => Some(KeyCode::Enter),
30 => Some(KeyCode::Char('a')),
31 => Some(KeyCode::Char('s')),
32 => Some(KeyCode::Char('d')),
33 => Some(KeyCode::Char('f')),
34 => Some(KeyCode::Char('g')),
35 => Some(KeyCode::Char('h')),
36 => Some(KeyCode::Char('j')),
37 => Some(KeyCode::Char('k')),
38 => Some(KeyCode::Char('l')),
44 => Some(KeyCode::Char('z')),
45 => Some(KeyCode::Char('x')),
46 => Some(KeyCode::Char('c')),
47 => Some(KeyCode::Char('v')),
48 => Some(KeyCode::Char('b')),
49 => Some(KeyCode::Char('n')),
50 => Some(KeyCode::Char('m')),
51 => Some(KeyCode::Char(',')),
52 => Some(KeyCode::Char('.')),
53 => Some(KeyCode::Char('/')),
57 => Some(KeyCode::Space),
102 => Some(KeyCode::Home),
103 => Some(KeyCode::Up),
104 => Some(KeyCode::PageUp),
105 => Some(KeyCode::Left),
106 => Some(KeyCode::Right),
107 => Some(KeyCode::End),
108 => Some(KeyCode::Down),
109 => Some(KeyCode::PageDown),
111 => Some(KeyCode::Delete),
_ => None,
}
}
pub fn decode_linux_modifier(code: u16) -> Option<KeyModifiers> {
match code {
42 | 54 => Some(KeyModifiers::SHIFT),
29 | 97 => Some(KeyModifiers::CTRL),
56 | 100 => Some(KeyModifiers::ALT),
125 | 126 => Some(KeyModifiers::LOGO),
_ => None,
}
}
pub fn write_uncompressed_png(
path: &str,
width: usize,
height: usize,
pixels: &[Xrgb8888],
) -> std::io::Result<()> {
fn crc32(data: &[u8]) -> u32 {
let mut crc = 0xFFFF_FFFFu32;
for &b in data {
crc ^= b as u32;
for _ in 0..8 {
if (crc & 1) != 0 {
crc = (crc >> 1) ^ 0xEDB8_8320;
} else {
crc >>= 1;
}
}
}
!crc
}
fn write_chunk(out: &mut Vec<u8>, tag: &[u8; 4], data: &[u8]) {
out.extend_from_slice(&(data.len() as u32).to_be_bytes());
let start = out.len();
out.extend_from_slice(tag);
out.extend_from_slice(data);
let c = crc32(&out[start..]);
out.extend_from_slice(&c.to_be_bytes());
}
let mut raw_scanlines = Vec::with_capacity(height * (1 + width * 3));
for y in 0..height {
raw_scanlines.push(0u8);
for x in 0..width {
let c = pixels[y * width + x].to_color();
raw_scanlines.push(c.r());
raw_scanlines.push(c.g());
raw_scanlines.push(c.b());
}
}
let mut zlib_stream = Vec::with_capacity(raw_scanlines.len() + 64);
zlib_stream.push(0x78);
zlib_stream.push(0x01);
let mut s1: u32 = 1;
let mut s2: u32 = 0;
for &b in &raw_scanlines {
s1 = (s1 + b as u32) % 65521;
s2 = (s2 + s1) % 65521;
}
let adler = (s2 << 16) | s1;
let total_len = raw_scanlines.len();
let mut offset = 0usize;
while offset < total_len {
let rem = total_len - offset;
let block_len = rem.min(65535);
let is_final = u8::from(offset + block_len >= total_len);
zlib_stream.push(is_final);
let len_u16 = block_len as u16;
let nlen_u16 = !len_u16;
zlib_stream.extend_from_slice(&len_u16.to_le_bytes());
zlib_stream.extend_from_slice(&nlen_u16.to_le_bytes());
zlib_stream.extend_from_slice(&raw_scanlines[offset..offset + block_len]);
offset += block_len;
}
zlib_stream.extend_from_slice(&adler.to_be_bytes());
let mut png = Vec::with_capacity(zlib_stream.len() + 128);
png.extend_from_slice(&[137, 80, 78, 71, 13, 10, 26, 10]);
let mut ihdr = Vec::with_capacity(13);
ihdr.extend_from_slice(&(width as u32).to_be_bytes());
ihdr.extend_from_slice(&(height as u32).to_be_bytes());
ihdr.extend_from_slice(&[8, 2, 0, 0, 0]);
write_chunk(&mut png, b"IHDR", &ihdr);
write_chunk(&mut png, b"IDAT", &zlib_stream);
write_chunk(&mut png, b"IEND", &[]);
std::fs::write(path, png)
}