diff options
author | Zach Reizner <zachr@google.com> | 2018-10-03 10:22:32 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2018-10-09 21:14:05 -0700 |
commit | 55a9e504beef368bd97e51ffd5a7fa6c034eb8ad (patch) | |
tree | 894d8685e2fdfa105ea35d1cb6cfceee06502c7a /gpu_buffer | |
parent | 046df60760f3b0691f23c27a7f24a96c9afe8c05 (diff) | |
download | crosvm-55a9e504beef368bd97e51ffd5a7fa6c034eb8ad.tar crosvm-55a9e504beef368bd97e51ffd5a7fa6c034eb8ad.tar.gz crosvm-55a9e504beef368bd97e51ffd5a7fa6c034eb8ad.tar.bz2 crosvm-55a9e504beef368bd97e51ffd5a7fa6c034eb8ad.tar.lz crosvm-55a9e504beef368bd97e51ffd5a7fa6c034eb8ad.tar.xz crosvm-55a9e504beef368bd97e51ffd5a7fa6c034eb8ad.tar.zst crosvm-55a9e504beef368bd97e51ffd5a7fa6c034eb8ad.zip |
cargo fmt all source code
Now that cargo fmt has landed, run it over everything at once to bring rust source to the standard formatting. TEST=cargo test BUG=None Change-Id: Ic95a48725e5a40dcbd33ba6d5aef2bd01e91865b Reviewed-on: https://chromium-review.googlesource.com/1259287 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
Diffstat (limited to 'gpu_buffer')
-rw-r--r-- | gpu_buffer/src/lib.rs | 264 | ||||
-rw-r--r-- | gpu_buffer/src/raw.rs | 88 | ||||
-rw-r--r-- | gpu_buffer/src/rendernode.rs | 8 |
3 files changed, 190 insertions, 170 deletions
diff --git a/gpu_buffer/src/lib.rs b/gpu_buffer/src/lib.rs index aa57ea9..e57053d 100644 --- a/gpu_buffer/src/lib.rs +++ b/gpu_buffer/src/lib.rs @@ -34,9 +34,9 @@ extern crate data_model; #[macro_use] extern crate sys_util; -pub mod rendernode; -mod raw; mod drm_formats; +mod raw; +pub mod rendernode; use std::cmp::min; use std::fmt; @@ -48,10 +48,10 @@ use std::ptr::null_mut; use std::rc::Rc; use std::result::Result; -use data_model::{VolatileSlice, VolatileMemory, VolatileMemoryError}; +use data_model::{VolatileMemory, VolatileMemoryError, VolatileSlice}; -use raw::*; use drm_formats::*; +use raw::*; const MAP_FAILED: *mut c_void = (-1isize as *mut _); @@ -84,28 +84,20 @@ impl fmt::Display for Error { field1: (label1, value1), field2: (label2, value2), op, - } => { - write!(f, - "arithmetic failed: {}({}) {} {}({})", - label1, - value1, - op, - label2, - value2) - } + } => write!( + f, + "arithmetic failed: {}({}) {} {}({})", + label1, value1, op, label2, value2 + ), Error::InvalidPrecondition { field1: (label1, value1), field2: (label2, value2), op, - } => { - write!(f, - "invalid precondition: {}({}) {} {}({})", - label1, - value1, - op, - label2, - value2) - } + } => write!( + f, + "invalid precondition: {}({}) {} {}({})", + label1, value1, op, label2, value2 + ), Error::UnknownFormat(format) => write!(f, "unknown format {:?}", format), Error::Memcopy(ref e) => write!(f, "error copying memory: {}", e), } @@ -113,29 +105,39 @@ impl fmt::Display for Error { } macro_rules! checked_arithmetic { - ($x:ident $op:ident $y:ident $op_name:expr) => ($x.$op($y).ok_or_else(|| - Error::CheckedArithmetic { + ($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 "*")); + }) + }; + ($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 { + ($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: "<=" + op: "<=", }) - }); - ($x:ident <= $y:ident) => (check_range!($x; <= $y)); + } + }; + ($x:ident <= $y:ident) => { + check_range!($x; <= $y) + }; } /// A [fourcc](https://en.wikipedia.org/wiki/FourCC) format identifier. @@ -254,19 +256,17 @@ impl fmt::Debug for Format { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let b = self.to_bytes(); if b.iter().all(u8::is_ascii_graphic) { - write!(f, - "fourcc({}{}{}{})", - b[0] as char, - b[1] as char, - b[2] as char, - b[3] as char) + write!( + f, + "fourcc({}{}{}{})", + b[0] as char, b[1] as char, b[2] as char, b[3] as char + ) } else { - write!(f, - "fourcc(0x{:02x}{:02x}{:02x}{:02x})", - b[0], - b[1], - b[2], - b[3]) + write!( + f, + "fourcc(0x{:02x}{:02x}{:02x}{:02x})", + b[0], b[1], b[2], b[3] + ) } } } @@ -349,7 +349,6 @@ impl Flags { } } - struct DeviceInner { _fd: File, gbm: *mut gbm_device, @@ -383,12 +382,13 @@ impl Device { } /// Creates a new buffer with the given metadata. - pub fn create_buffer(&self, - width: u32, - height: u32, - format: Format, - usage: Flags) - -> Result<Buffer, Error> { + pub fn create_buffer( + &self, + width: u32, + height: u32, + format: Format, + usage: Flags, + ) -> Result<Buffer, Error> { // This is safe because only a valid gbm_device is used and the return value is checked. let bo = unsafe { gbm_bo_create(self.0.gbm, width, height, format.0, usage.0) }; if bo.is_null() { @@ -477,19 +477,21 @@ impl Buffer { } } - fn map(&self, - x: u32, - y: u32, - width: u32, - height: u32, - plane: usize, - flags: u32) - -> Result<BufferMapping, Error> { + fn map( + &self, + x: u32, + y: u32, + width: u32, + height: u32, + plane: usize, + flags: u32, + ) -> Result<BufferMapping, Error> { 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() + let bytes_per_pixel = self + .format() .bytes_per_pixel(plane) .ok_or(Error::UnknownFormat(self.format()))? as u32; @@ -499,15 +501,17 @@ impl Buffer { // 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) + gbm_bo_map( + self.0, + x, + y, + width, + height, + flags, + &mut stride, + &mut map_data, + plane, + ) }; if mapping == MAP_FAILED { return Err(Error::MapFailed); @@ -541,14 +545,15 @@ impl Buffer { } /// 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) - -> Result<(), Error> { + pub fn read_to_volatile( + &self, + x: u32, + y: u32, + width: u32, + height: u32, + plane: usize, + dst: VolatileSlice, + ) -> Result<(), Error> { if width == 0 || height == 0 { return Ok(()); } @@ -569,9 +574,11 @@ impl Buffer { let src = mapping.as_volatile_slice(); for yy in 0..(height as u64) { let line_offset = checked_arithmetic!(yy * stride)?; - let src_line = src.get_slice(line_offset, line_copy_size) + let src_line = src + .get_slice(line_offset, line_copy_size) .map_err(|e| Error::Memcopy(e))?; - let dst_line = dst.get_slice(line_offset, line_copy_size) + let dst_line = dst + .get_slice(line_offset, line_copy_size) .map_err(|e| Error::Memcopy(e))?; src_line.copy_to_volatile_slice(dst_line); } @@ -581,15 +588,16 @@ impl Buffer { } /// Writes to the given subsection of the buffer from `sgs`. - pub fn write_from_sg<'a, S: Iterator<Item = VolatileSlice<'a>>>(&self, - x: u32, - y: u32, - width: u32, - height: u32, - plane: usize, - src_offset: usize, - mut sgs: S) - -> Result<(), Error> { + pub fn write_from_sg<'a, S: Iterator<Item = VolatileSlice<'a>>>( + &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(()); } @@ -617,7 +625,8 @@ impl Buffer { } }; let copy_sg_size = min(sg_size, copy_size); - let src_slice = sg.get_slice(src_offset, copy_sg_size) + let src_slice = sg + .get_slice(src_offset, copy_sg_size) .map_err(|e| Error::Memcopy(e))?; src_slice.copy_to_volatile_slice(dst_slice); @@ -651,7 +660,8 @@ impl Buffer { 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) + let src_slice = sg + .get_slice(src_offset, copy_sg_size) .map_err(|e| Error::Memcopy(e))?; src_slice.copy_to_volatile_slice(dst_slice); @@ -723,8 +733,8 @@ impl<'a> Drop for BufferMapping<'a> { #[cfg(test)] mod tests { use super::*; - use std::fmt::Write; use data_model::VolatileMemory; + use std::fmt::Write; #[test] fn format_debug() { @@ -774,11 +784,12 @@ mod tests { 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, - 512, - Format::new(b'X', b'R', b'2', b'4'), - Flags::empty().use_scanout(true)) - .expect("failed to create buffer"); + .create_buffer( + 1024, + 512, + Format::new(b'X', b'R', b'2', b'4'), + Flags::empty().use_scanout(true), + ).expect("failed to create buffer"); assert_eq!(bo.width(), 1024); assert_eq!(bo.height(), 512); @@ -792,46 +803,49 @@ mod tests { 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)) - .expect("failed to create buffer"); + .create_buffer( + 1024, + 1024, + Format::new(b'X', b'R', b'2', b'4'), + Flags::empty().use_scanout(true), + ).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"); + .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<u8> = 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()) - .expect("failed to read bo"); + 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(), + ).expect("failed to read bo"); assert!(dst.iter().all(|&x| x == 0x4A)); } } diff --git a/gpu_buffer/src/raw.rs b/gpu_buffer/src/raw.rs index e5e258a..7fc5a9f 100644 --- a/gpu_buffer/src/raw.rs +++ b/gpu_buffer/src/raw.rs @@ -7,7 +7,7 @@ #![allow(dead_code)] -use std::os::raw::{c_int, c_char, c_void}; +use std::os::raw::{c_char, c_int, c_void}; /// \file gbm.h /// \brief Generic Buffer Manager @@ -96,23 +96,25 @@ extern "C" { pub fn gbm_device_is_format_supported(gbm: *mut gbm_device, format: u32, usage: u32) -> c_int; pub fn gbm_device_destroy(gbm: *mut gbm_device); pub fn gbm_create_device(fd: c_int) -> *mut gbm_device; - pub fn gbm_bo_create(gbm: *mut gbm_device, - width: u32, - height: u32, - format: u32, - flags: u32) - -> *mut gbm_bo; - pub fn gbm_bo_create_with_modifiers(gbm: *mut gbm_device, - width: u32, - height: u32, - format: u32, - modifiers: *const u64, - count: u32) - -> *mut gbm_bo; + pub fn gbm_bo_create( + gbm: *mut gbm_device, + width: u32, + height: u32, + format: u32, + flags: u32, + ) -> *mut gbm_bo; + pub fn gbm_bo_create_with_modifiers( + gbm: *mut gbm_device, + width: u32, + height: u32, + format: u32, + modifiers: *const u64, + count: u32, + ) -> *mut gbm_bo; } #[repr(C)] -#[derive(Debug, Copy, Clone )] +#[derive(Debug, Copy, Clone)] pub struct gbm_import_fd_data { pub fd: c_int, pub width: u32, @@ -122,7 +124,7 @@ pub struct gbm_import_fd_data { } #[repr(C)] -#[derive(Debug, Copy, Clone )] +#[derive(Debug, Copy, Clone)] pub struct gbm_import_fd_planar_data { pub fds: [c_int; 4usize], pub width: u32, @@ -134,11 +136,12 @@ pub struct gbm_import_fd_planar_data { } extern "C" { - pub fn gbm_bo_import(gbm: *mut gbm_device, - type_: u32, - buffer: *mut c_void, - usage: u32) - -> *mut gbm_bo; + pub fn gbm_bo_import( + gbm: *mut gbm_device, + type_: u32, + buffer: *mut c_void, + usage: u32, + ) -> *mut gbm_bo; } /// Buffer contents read back (or accessed directly) at transfer @@ -162,16 +165,17 @@ pub const GBM_BO_TRANSFER_READ_WRITE: gbm_bo_transfer_flags = 3; pub type gbm_bo_transfer_flags = u32; extern "C" { - pub fn gbm_bo_map(bo: *mut gbm_bo, - x: u32, - y: u32, - width: u32, - height: u32, - flags: u32, - stride: *mut u32, - map_data: *mut *mut c_void, - plane: usize) - -> *mut c_void; + pub fn gbm_bo_map( + bo: *mut gbm_bo, + x: u32, + y: u32, + width: u32, + height: u32, + flags: u32, + stride: *mut u32, + map_data: *mut *mut c_void, + plane: usize, + ) -> *mut c_void; pub fn gbm_bo_unmap(bo: *mut gbm_bo, map_data: *mut c_void); pub fn gbm_bo_get_width(bo: *mut gbm_bo) -> u32; pub fn gbm_bo_get_height(bo: *mut gbm_bo) -> u32; @@ -190,18 +194,20 @@ extern "C" { pub fn gbm_bo_get_plane_stride(bo: *mut gbm_bo, plane: usize) -> u32; pub fn gbm_bo_get_plane_format_modifier(bo: *mut gbm_bo, plane: usize) -> u64; // Did not generate cleanly by bindgen. Redone manually by zachr. - pub fn gbm_bo_set_user_data(bo: *mut gbm_bo, - data: *mut c_void, - destroy_user_data: extern "C" fn(bo: *mut gbm_bo, - data: *mut c_void)); + pub fn gbm_bo_set_user_data( + bo: *mut gbm_bo, + data: *mut c_void, + destroy_user_data: extern "C" fn(bo: *mut gbm_bo, data: *mut c_void), + ); pub fn gbm_bo_get_user_data(bo: *mut gbm_bo) -> *mut c_void; pub fn gbm_bo_destroy(bo: *mut gbm_bo); - pub fn gbm_surface_create(gbm: *mut gbm_device, - width: u32, - height: u32, - format: u32, - flags: u32) - -> *mut gbm_surface; + pub fn gbm_surface_create( + gbm: *mut gbm_device, + width: u32, + height: u32, + format: u32, + flags: u32, + ) -> *mut gbm_surface; pub fn gbm_surface_lock_front_buffer(surface: *mut gbm_surface) -> *mut gbm_bo; pub fn gbm_surface_release_buffer(surface: *mut gbm_surface, bo: *mut gbm_bo); pub fn gbm_surface_has_free_buffers(surface: *mut gbm_surface) -> c_int; diff --git a/gpu_buffer/src/rendernode.rs b/gpu_buffer/src/rendernode.rs index f8b0201..2c4c51b 100644 --- a/gpu_buffer/src/rendernode.rs +++ b/gpu_buffer/src/rendernode.rs @@ -4,9 +4,9 @@ use std::ffi::CString; use std::fs::{File, OpenOptions}; -use std::os::raw::{c_char, c_int, c_uint}; #[cfg(target_pointer_width = "64")] use std::os::raw::c_ulong; +use std::os::raw::{c_char, c_int, c_uint}; use std::path::Path; use std::ptr::null_mut; @@ -76,11 +76,11 @@ fn get_drm_device_name(fd: &File) -> Result<String, ()> { } Ok(CString::new(&name_bytes[..(version.name_len as usize)]) - .map_err(|_| ())? - .into_string().map_err(|_| ())?) + .map_err(|_| ())? + .into_string() + .map_err(|_| ())?) } - /// Returns a `fd` for an opened rendernode device, while filtering out specified /// undesired drivers. pub fn open_device(undesired: &[&str]) -> Result<File, ()> { |