1
0
Fork 0
forked from wry/wry

cli: use map stride when taking screenshots

This commit is contained in:
Julian Orth 2024-06-05 18:34:07 +02:00
parent 53b7d3c805
commit 5e336e19b7
2 changed files with 32 additions and 8 deletions

View file

@ -3,7 +3,7 @@ use {
cli::{GlobalArgs, ScreenshotArgs, ScreenshotFormat}, cli::{GlobalArgs, ScreenshotArgs, ScreenshotFormat},
format::XRGB8888, format::XRGB8888,
tools::tool_client::{with_tool_client, Handle, ToolClient}, tools::tool_client::{with_tool_client, Handle, ToolClient},
utils::{errorfmt::ErrorFmt, queue::AsyncQueue}, utils::{errorfmt::ErrorFmt, queue::AsyncQueue, windows::WindowsExt},
video::{ video::{
dmabuf::{DmaBuf, DmaBufIds, DmaBufPlane, PlaneVec}, dmabuf::{DmaBuf, DmaBufIds, DmaBufPlane, PlaneVec},
drm::Drm, drm::Drm,
@ -107,7 +107,7 @@ pub fn buf_to_bytes(dma_buf_ids: &DmaBufIds, buf: &Dmabuf, format: ScreenshotFor
fatal!("Could not import screenshot dmabuf: {}", ErrorFmt(e)); fatal!("Could not import screenshot dmabuf: {}", ErrorFmt(e));
} }
}; };
let bo_map = match bo.map() { let bo_map = match bo.map_read() {
Ok(map) => map, Ok(map) => map,
Err(e) => { Err(e) => {
fatal!("Could not map dmabuf: {}", ErrorFmt(e)); fatal!("Could not map dmabuf: {}", ErrorFmt(e));
@ -115,14 +115,18 @@ pub fn buf_to_bytes(dma_buf_ids: &DmaBufIds, buf: &Dmabuf, format: ScreenshotFor
}; };
let data = unsafe { bo_map.data() }; let data = unsafe { bo_map.data() };
if format == ScreenshotFormat::Qoi { if format == ScreenshotFormat::Qoi {
return xrgb8888_encode_qoi(data, buf.width, buf.height, buf.stride); return xrgb8888_encode_qoi(data, buf.width, buf.height, bo_map.stride() as u32);
} }
let mut out = vec![]; let mut out = vec![];
{ {
let mut image_data = Vec::with_capacity(data.len()); let mut image_data = Vec::with_capacity((buf.width * buf.height * 4) as usize);
for i in 0..data.len() / 4 { let lines = data[..(buf.height as usize * bo_map.stride() as usize)]
image_data.extend_from_slice(&[data[4 * i + 2], data[4 * i + 1], data[4 * i + 0], 255]) .chunks_exact(bo_map.stride() as usize);
for line in lines {
for pixel in line[..(buf.width as usize * 4)].array_chunks_ext::<4>() {
image_data.extend_from_slice(&[pixel[2], pixel[1], pixel[0], 255])
}
} }
let mut encoder = Encoder::new(&mut out, buf.width, buf.height); let mut encoder = Encoder::new(&mut out, buf.width, buf.height);
encoder.set_color(ColorType::Rgba); encoder.set_color(ColorType::Rgba);

View file

@ -148,12 +148,22 @@ pub struct GbmBoMap {
bo: Rc<GbmBo>, bo: Rc<GbmBo>,
data: *mut [u8], data: *mut [u8],
opaque: *mut u8, opaque: *mut u8,
stride: i32,
} }
impl GbmBoMap { impl GbmBoMap {
pub unsafe fn data(&self) -> &[u8] { pub unsafe fn data(&self) -> &[u8] {
&*self.data &*self.data
} }
#[allow(dead_code)]
pub fn data_ptr(&self) -> *mut u8 {
self.data as _
}
pub fn stride(&self) -> i32 {
self.stride
}
} }
unsafe fn export_bo(dmabuf_ids: &DmaBufIds, bo: *mut Bo) -> Result<DmaBuf, GbmError> { unsafe fn export_bo(dmabuf_ids: &DmaBufIds, bo: *mut Bo) -> Result<DmaBuf, GbmError> {
@ -293,7 +303,16 @@ impl GbmBo {
&self.dmabuf &self.dmabuf
} }
pub fn map(self: &Rc<Self>) -> Result<GbmBoMap, GbmError> { pub fn map_read(self: &Rc<Self>) -> Result<GbmBoMap, GbmError> {
self.map2(GBM_BO_TRANSFER_READ)
}
#[allow(dead_code)]
pub fn map_write(self: &Rc<Self>) -> Result<GbmBoMap, GbmError> {
self.map2(GBM_BO_TRANSFER_READ_WRITE)
}
fn map2(self: &Rc<Self>, flags: u32) -> Result<GbmBoMap, GbmError> {
let mut stride = 0; let mut stride = 0;
let mut map_data = ptr::null_mut(); let mut map_data = ptr::null_mut();
unsafe { unsafe {
@ -303,7 +322,7 @@ impl GbmBo {
0, 0,
self.dmabuf.width as _, self.dmabuf.width as _,
self.dmabuf.height as _, self.dmabuf.height as _,
GBM_BO_TRANSFER_READ, flags,
&mut stride, &mut stride,
&mut map_data, &mut map_data,
); );
@ -315,6 +334,7 @@ impl GbmBo {
bo: self.clone(), bo: self.clone(),
data: map, data: map,
opaque: map_data, opaque: map_data,
stride: stride as i32,
}) })
} }
} }