From f3c160e241f476d08daf2b3da7c31a8a63cf8255 Mon Sep 17 00:00:00 2001 From: David Stevens Date: Wed, 9 Oct 2019 19:12:30 +0900 Subject: gpu_buffer: remove dead code This code was left behind when BackedBuffer was removed. BUG=none TEST=compile Change-Id: I450a98af8a2cbea1f86e4fd825f054f7853f7a18 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1847538 Reviewed-by: Zach Reizner Tested-by: David Stevens Tested-by: kokoro Commit-Queue: David Stevens --- gpu_buffer/src/lib.rs | 320 +------------------------------------------------- 1 file changed, 2 insertions(+), 318 deletions(-) diff --git a/gpu_buffer/src/lib.rs b/gpu_buffer/src/lib.rs index 08174dc..3bc4b24 100644 --- a/gpu_buffer/src/lib.rs +++ b/gpu_buffer/src/lib.rs @@ -34,24 +34,19 @@ mod drm_formats; mod raw; pub mod rendernode; -use std::cmp::min; use std::ffi::CStr; use std::fmt::{self, Display}; use std::fs::File; -use std::isize; -use std::os::raw::{c_char, c_void}; +use std::os::raw::c_char; use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; -use std::ptr::null_mut; use std::rc::Rc; use std::result::Result; -use data_model::{VolatileMemory, VolatileMemoryError, VolatileSlice}; +use data_model::VolatileMemoryError; use crate::drm_formats::*; use crate::raw::*; -const MAP_FAILED: *mut c_void = (-1isize as *mut _); - #[derive(Debug)] pub enum Error { GbmFailed, @@ -103,42 +98,6 @@ impl Display for Error { } } -macro_rules! checked_arithmetic { - ($x:ident $op:ident $y:ident $op_name:expr) => { - $x.$op($y).ok_or_else(|| Error::CheckedArithmetic { - field1: (stringify!($x), $x as usize), - field2: (stringify!($y), $y as usize), - op: $op_name, - }) - }; - ($x:ident + $y:ident) => { - checked_arithmetic!($x checked_add $y "+") - }; - ($x:ident - $y:ident) => { - checked_arithmetic!($x checked_sub $y "-") - }; - ($x:ident * $y:ident) => { - checked_arithmetic!($x checked_mul $y "*") - }; -} - -macro_rules! checked_range { - ($x:expr; <= $y:expr) => { - if $x <= $y { - Ok(()) - } else { - Err(Error::InvalidPrecondition { - field1: (stringify!($x), $x as usize), - field2: (stringify!($y), $y as usize), - op: "<=", - }) - } - }; - ($x:ident <= $y:ident) => { - check_range!($x; <= $y) - }; -} - /// A [fourcc](https://en.wikipedia.org/wiki/FourCC) format identifier. #[derive(Copy, Clone, Eq, PartialEq)] pub struct Format(u32); @@ -489,213 +448,6 @@ impl Buffer { ret => Err(ret), } } - - fn map( - &self, - x: u32, - y: u32, - width: u32, - height: u32, - plane: usize, - flags: u32, - ) -> Result { - checked_range!(checked_arithmetic!(x + width)?; <= self.width())?; - checked_range!(checked_arithmetic!(y + height)?; <= self.height())?; - checked_range!(plane; <= self.num_planes())?; - - let bytes_per_pixel = self - .format() - .bytes_per_pixel(plane) - .ok_or(Error::UnknownFormat(self.format()))? as u32; - - let mut stride = 0; - let mut map_data = null_mut(); - // Safe because only a valid gbm_bo object is used and the return value is checked. Only - // pointers coerced from stack references are used for returned values, and we trust gbm to - // only write as many bytes as the size of the pointed to values. - let mapping = unsafe { - gbm_bo_map( - self.0, - x, - y, - width, - height, - flags, - &mut stride, - &mut map_data, - plane, - ) - }; - if mapping == MAP_FAILED { - return Err(Error::MapFailed); - } - - // The size of returned slice is equal the size of a row in bytes multiplied by the height - // of the mapped region, subtracted by the offset into the first mapped row. The 'x' and - // 'y's in the below diagram of a 2D buffer are bytes in the mapping. The first 'y' is what - // the mapping points to in memory, and the '-'s are unmapped bytes of the buffer. - // |----------| - // |--stride--| - // |-----yyyyx| h - // |xxxxxyyyyx| e - // |xxxxxyyyyx| i - // |xxxxxyyyyx| g - // |xxxxxyyyyx| h - // |xxxxxyyyyx| t - // |----------| - let size = checked_arithmetic!(stride * height)?; - let x_offset_bytes = checked_arithmetic!(x * bytes_per_pixel)?; - let slice_size = checked_arithmetic!(size - x_offset_bytes)? as u64; - - Ok(BufferMapping { - // Safe because the chunk of memory starting at mapping with size `slice_size` is valid - // and tied to the lifetime of `buffer_mapping`. - slice: unsafe { VolatileSlice::new(mapping as *mut u8, slice_size) }, - stride, - map_data, - buffer: self, - }) - } - - /// Reads the given subsection of the buffer to `dst`. - pub fn read_to_volatile( - &self, - x: u32, - y: u32, - width: u32, - height: u32, - plane: usize, - dst: VolatileSlice, - dst_stride: u32, - ) -> Result<(), Error> { - if width == 0 || height == 0 { - return Ok(()); - } - - let mapping = self.map(x, y, width, height, plane, GBM_BO_TRANSFER_READ)?; - let src_stride = mapping.stride() as u64; - let dst_stride = dst_stride as u64; - - if x == 0 && width == self.width() && src_stride == dst_stride { - mapping.as_volatile_slice().copy_to_volatile_slice(dst); - } else { - // This path is more complicated because there are gaps in the data between lines. - let width = width as u64; - let bytes_per_pixel = match self.format().bytes_per_pixel(plane) { - Some(bpp) => bpp as u64, - None => return Err(Error::UnknownFormat(self.format())), - }; - let line_copy_size = checked_arithmetic!(width * bytes_per_pixel)?; - let src = mapping.as_volatile_slice(); - for yy in 0..(height as u64) { - let src_line_offset = checked_arithmetic!(yy * src_stride)?; - let dst_line_offset = checked_arithmetic!(yy * dst_stride)?; - let src_line = src - .get_slice(src_line_offset, line_copy_size) - .map_err(Error::Memcopy)?; - let dst_line = dst - .get_slice(dst_line_offset, line_copy_size) - .map_err(Error::Memcopy)?; - src_line.copy_to_volatile_slice(dst_line); - } - } - - Ok(()) - } - - /// Writes to the given subsection of the buffer from `sgs`. - pub fn write_from_sg<'a, S: Iterator>>( - &self, - x: u32, - y: u32, - width: u32, - height: u32, - plane: usize, - src_offset: usize, - mut sgs: S, - ) -> Result<(), Error> { - if width == 0 || height == 0 { - return Ok(()); - } - - checked_range!(src_offset; <= isize::MAX as usize)?; - - let mapping = self.map(x, y, width, height, plane, GBM_BO_TRANSFER_WRITE)?; - let mut dst_slice = mapping.as_volatile_slice(); - let stride = mapping.stride() as u64; - let mut height = height as u64; - let mut src_offset = src_offset as u64; - - if x == 0 && width == self.width() { - // This path is a simple copy from the scatter gather iterator to the buffer objection, - // with no gaps in the data. - let mut copy_size = checked_arithmetic!(stride * height)?; - for sg in sgs { - // Skip src_offset into this scatter gather item, or the entire thing if offset is - // larger. - let sg_size = match sg.size().checked_sub(src_offset) { - Some(sg_remaining_size) => sg_remaining_size, - None => { - src_offset -= sg.size(); - continue; - } - }; - let copy_sg_size = min(sg_size, copy_size); - let src_slice = sg - .get_slice(src_offset, copy_sg_size) - .map_err(Error::Memcopy)?; - src_slice.copy_to_volatile_slice(dst_slice); - - src_offset = 0; - dst_slice = dst_slice.offset(copy_sg_size).map_err(Error::Memcopy)?; - copy_size -= copy_sg_size; - if copy_size == 0 { - break; - } - } - } else { - let width = width as u64; - // This path is more complicated because there are gaps in the data between lines. - let bytes_per_pixel = self.format().bytes_per_pixel(plane).unwrap_or(0) as u64; - let line_copy_size = checked_arithmetic!(width * bytes_per_pixel)?; - let line_end_skip = checked_arithmetic!(stride - line_copy_size)?; - let mut remaining_line_copy_size = line_copy_size; - let mut sg_opt = sgs.next(); - while let Some(sg) = sg_opt { - // Skip src_offset into this scatter gather item, or the entire thing if offset is - // larger. - let sg_size = match sg.size().checked_sub(src_offset) { - None | Some(0) => { - src_offset -= sg.size(); - sg_opt = sgs.next(); - continue; - } - Some(sg_remaining_size) => sg_remaining_size, - }; - let copy_sg_size = min(sg_size, remaining_line_copy_size); - let src_slice = sg - .get_slice(src_offset, copy_sg_size) - .map_err(Error::Memcopy)?; - src_slice.copy_to_volatile_slice(dst_slice); - - src_offset += copy_sg_size; - dst_slice = dst_slice.offset(copy_sg_size).map_err(Error::Memcopy)?; - remaining_line_copy_size -= copy_sg_size; - if remaining_line_copy_size == 0 { - remaining_line_copy_size = line_copy_size; - height -= 1; - if height == 0 { - break; - } - - src_offset += line_end_skip; - dst_slice = dst_slice.offset(line_end_skip).map_err(Error::Memcopy)?; - } - } - } - - Ok(()) - } } impl Drop for Buffer { @@ -712,33 +464,6 @@ impl AsRawFd for Buffer { } } -struct BufferMapping<'a> { - slice: VolatileSlice<'a>, - stride: u32, - map_data: *mut c_void, - buffer: &'a Buffer, -} - -impl<'a> BufferMapping<'a> { - fn as_volatile_slice(&self) -> VolatileSlice { - self.slice - } - - fn stride(&self) -> u32 { - self.stride - } -} - -impl<'a> Drop for BufferMapping<'a> { - fn drop(&mut self) { - // safe because the gbm_bo is assumed to be valid and the map_data is the same one given by - // gbm_bo_map. - unsafe { - gbm_bo_unmap(self.buffer.0, self.map_data); - } - } -} - #[cfg(test)] mod tests { use super::*; @@ -822,45 +547,4 @@ mod tests { .expect("failed to create buffer"); bo.export_plane_fd(0).expect("failed to export plane"); } - - #[test] - #[ignore] // no access to /dev/dri - fn buffer_transfer() { - let drm_card = File::open("/dev/dri/card0").expect("failed to open card"); - let device = Device::new(drm_card).expect("failed to create device with card"); - let bo = device - .create_buffer( - 1024, - 1024, - Format::new(b'X', b'R', b'2', b'4'), - Flags::empty().use_scanout(true).use_linear(true), - ) - .expect("failed to create buffer"); - let mut dst: Vec = Vec::new(); - dst.resize((bo.stride() * bo.height()) as usize, 0x4A); - let dst_len = dst.len() as u64; - bo.write_from_sg( - 0, - 0, - 1024, - 1024, - 0, - 0, - [dst.as_mut_slice().get_slice(0, dst_len).unwrap()] - .iter() - .cloned(), - ) - .expect("failed to read bo"); - bo.read_to_volatile( - 0, - 0, - 1024, - 1024, - 0, - dst.as_mut_slice().get_slice(0, dst_len).unwrap(), - bo.stride(), - ) - .expect("failed to read bo"); - assert!(dst.iter().all(|&x| x == 0x4A)); - } } -- cgit 1.4.1