gpu_buffer: add bytes_per_pixel method to Format

TEST=cargo test -p gpu_buffer
BUG=None

Change-Id: I39f825065b6dd2904d81ae3baf76b3552c739691
Reviewed-on: https://chromium-review.googlesource.com/1102111
Commit-Ready: Zach Reizner <zachr@chromium.org>
Tested-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: David Reveman <reveman@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
This commit is contained in:
Zach Reizner 2018-06-14 18:06:11 -07:00 committed by chrome-bot
parent 3a8100adc7
commit 59caefa5ab
2 changed files with 168 additions and 0 deletions

View file

@ -0,0 +1,72 @@
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#![allow(dead_code)]
pub const DRM_FORMAT_C8: [u8; 4] = [b'C', b'8', b' ', b' '];
pub const DRM_FORMAT_R8: [u8; 4] = [b'R', b'8', b' ', b' '];
pub const DRM_FORMAT_R16: [u8; 4] = [b'R', b'1', b'6', b' '];
pub const DRM_FORMAT_RG88: [u8; 4] = [b'R', b'G', b'8', b'8'];
pub const DRM_FORMAT_GR88: [u8; 4] = [b'G', b'R', b'8', b'8'];
pub const DRM_FORMAT_RG1616: [u8; 4] = [b'R', b'G', b'3', b'2'];
pub const DRM_FORMAT_GR1616: [u8; 4] = [b'G', b'R', b'3', b'2'];
pub const DRM_FORMAT_RGB332: [u8; 4] = [b'R', b'G', b'B', b'8'];
pub const DRM_FORMAT_BGR233: [u8; 4] = [b'B', b'G', b'R', b'8'];
pub const DRM_FORMAT_XRGB4444: [u8; 4] = [b'X', b'R', b'1', b'2'];
pub const DRM_FORMAT_XBGR4444: [u8; 4] = [b'X', b'B', b'1', b'2'];
pub const DRM_FORMAT_RGBX4444: [u8; 4] = [b'R', b'X', b'1', b'2'];
pub const DRM_FORMAT_BGRX4444: [u8; 4] = [b'B', b'X', b'1', b'2'];
pub const DRM_FORMAT_ARGB4444: [u8; 4] = [b'A', b'R', b'1', b'2'];
pub const DRM_FORMAT_ABGR4444: [u8; 4] = [b'A', b'B', b'1', b'2'];
pub const DRM_FORMAT_RGBA4444: [u8; 4] = [b'R', b'A', b'1', b'2'];
pub const DRM_FORMAT_BGRA4444: [u8; 4] = [b'B', b'A', b'1', b'2'];
pub const DRM_FORMAT_XRGB1555: [u8; 4] = [b'X', b'R', b'1', b'5'];
pub const DRM_FORMAT_XBGR1555: [u8; 4] = [b'X', b'B', b'1', b'5'];
pub const DRM_FORMAT_RGBX5551: [u8; 4] = [b'R', b'X', b'1', b'5'];
pub const DRM_FORMAT_BGRX5551: [u8; 4] = [b'B', b'X', b'1', b'5'];
pub const DRM_FORMAT_ARGB1555: [u8; 4] = [b'A', b'R', b'1', b'5'];
pub const DRM_FORMAT_ABGR1555: [u8; 4] = [b'A', b'B', b'1', b'5'];
pub const DRM_FORMAT_RGBA5551: [u8; 4] = [b'R', b'A', b'1', b'5'];
pub const DRM_FORMAT_BGRA5551: [u8; 4] = [b'B', b'A', b'1', b'5'];
pub const DRM_FORMAT_RGB565: [u8; 4] = [b'R', b'G', b'1', b'6'];
pub const DRM_FORMAT_BGR565: [u8; 4] = [b'B', b'G', b'1', b'6'];
pub const DRM_FORMAT_RGB888: [u8; 4] = [b'R', b'G', b'2', b'4'];
pub const DRM_FORMAT_BGR888: [u8; 4] = [b'B', b'G', b'2', b'4'];
pub const DRM_FORMAT_XRGB8888: [u8; 4] = [b'X', b'R', b'2', b'4'];
pub const DRM_FORMAT_XBGR8888: [u8; 4] = [b'X', b'B', b'2', b'4'];
pub const DRM_FORMAT_RGBX8888: [u8; 4] = [b'R', b'X', b'2', b'4'];
pub const DRM_FORMAT_BGRX8888: [u8; 4] = [b'B', b'X', b'2', b'4'];
pub const DRM_FORMAT_ARGB8888: [u8; 4] = [b'A', b'R', b'2', b'4'];
pub const DRM_FORMAT_ABGR8888: [u8; 4] = [b'A', b'B', b'2', b'4'];
pub const DRM_FORMAT_RGBA8888: [u8; 4] = [b'R', b'A', b'2', b'4'];
pub const DRM_FORMAT_BGRA8888: [u8; 4] = [b'B', b'A', b'2', b'4'];
pub const DRM_FORMAT_XRGB2101010: [u8; 4] = [b'X', b'R', b'3', b'0'];
pub const DRM_FORMAT_XBGR2101010: [u8; 4] = [b'X', b'B', b'3', b'0'];
pub const DRM_FORMAT_RGBX1010102: [u8; 4] = [b'R', b'X', b'3', b'0'];
pub const DRM_FORMAT_BGRX1010102: [u8; 4] = [b'B', b'X', b'3', b'0'];
pub const DRM_FORMAT_ARGB2101010: [u8; 4] = [b'A', b'R', b'3', b'0'];
pub const DRM_FORMAT_ABGR2101010: [u8; 4] = [b'A', b'B', b'3', b'0'];
pub const DRM_FORMAT_RGBA1010102: [u8; 4] = [b'R', b'A', b'3', b'0'];
pub const DRM_FORMAT_BGRA1010102: [u8; 4] = [b'B', b'A', b'3', b'0'];
pub const DRM_FORMAT_YUYV: [u8; 4] = [b'Y', b'U', b'Y', b'V'];
pub const DRM_FORMAT_YVYU: [u8; 4] = [b'Y', b'V', b'Y', b'U'];
pub const DRM_FORMAT_UYVY: [u8; 4] = [b'U', b'Y', b'V', b'Y'];
pub const DRM_FORMAT_VYUY: [u8; 4] = [b'V', b'Y', b'U', b'Y'];
pub const DRM_FORMAT_AYUV: [u8; 4] = [b'A', b'Y', b'U', b'V'];
pub const DRM_FORMAT_NV12: [u8; 4] = [b'N', b'V', b'1', b'2'];
pub const DRM_FORMAT_NV21: [u8; 4] = [b'N', b'V', b'2', b'1'];
pub const DRM_FORMAT_NV16: [u8; 4] = [b'N', b'V', b'1', b'6'];
pub const DRM_FORMAT_NV61: [u8; 4] = [b'N', b'V', b'6', b'1'];
pub const DRM_FORMAT_NV24: [u8; 4] = [b'N', b'V', b'2', b'4'];
pub const DRM_FORMAT_NV42: [u8; 4] = [b'N', b'V', b'4', b'2'];
pub const DRM_FORMAT_YUV410: [u8; 4] = [b'Y', b'U', b'V', b'9'];
pub const DRM_FORMAT_YVU410: [u8; 4] = [b'Y', b'V', b'U', b'9'];
pub const DRM_FORMAT_YUV411: [u8; 4] = [b'Y', b'U', b'1', b'1'];
pub const DRM_FORMAT_YVU411: [u8; 4] = [b'Y', b'V', b'1', b'1'];
pub const DRM_FORMAT_YUV420: [u8; 4] = [b'Y', b'U', b'1', b'2'];
pub const DRM_FORMAT_YVU420: [u8; 4] = [b'Y', b'V', b'1', b'2'];
pub const DRM_FORMAT_YUV422: [u8; 4] = [b'Y', b'U', b'1', b'6'];
pub const DRM_FORMAT_YVU422: [u8; 4] = [b'Y', b'V', b'1', b'6'];
pub const DRM_FORMAT_YUV444: [u8; 4] = [b'Y', b'U', b'2', b'4'];
pub const DRM_FORMAT_YVU444: [u8; 4] = [b'Y', b'V', b'2', b'4'];

View file

@ -36,6 +36,7 @@ extern crate sys_util;
pub mod rendernode;
mod raw;
mod drm_formats;
use std::os::raw::c_void;
use std::fmt;
@ -49,6 +50,7 @@ use std::result::Result;
use data_model::VolatileSlice;
use raw::*;
use drm_formats::*;
const MAP_FAILED: *mut c_void = (-1isize as *mut _);
@ -78,6 +80,78 @@ impl Format {
let f = self.0;
[f as u8, (f >> 8) as u8, (f >> 16) as u8, (f >> 24) as u8]
}
/// Returns the number of bytes per pixel for the given plane, suitable for making copies
/// to/from the plane.
pub fn bytes_per_pixel(&self, plane: usize) -> Option<usize> {
let b = self.to_bytes();
// NV12 and NV21 have 2 planes with 1 byte per pixel.
if (b == DRM_FORMAT_NV12 || b == DRM_FORMAT_NV21) && plane < 2 {
return Some(1);
}
// YVU420 has 3 planes, all with the same 1 byte per pixel.
if b == DRM_FORMAT_YVU420 && plane < 3 {
return Some(1);
}
if plane != 0 {
return None;
}
let bpp = match self.to_bytes() {
DRM_FORMAT_BGR233 => 1,
DRM_FORMAT_C8 => 1,
DRM_FORMAT_R8 => 1,
DRM_FORMAT_RGB332 => 1,
DRM_FORMAT_ABGR1555 => 2,
DRM_FORMAT_ABGR4444 => 2,
DRM_FORMAT_ARGB1555 => 2,
DRM_FORMAT_ARGB4444 => 2,
DRM_FORMAT_BGR565 => 2,
DRM_FORMAT_BGRA4444 => 2,
DRM_FORMAT_BGRA5551 => 2,
DRM_FORMAT_BGRX4444 => 2,
DRM_FORMAT_BGRX5551 => 2,
DRM_FORMAT_GR88 => 2,
DRM_FORMAT_RG88 => 2,
DRM_FORMAT_RGB565 => 2,
DRM_FORMAT_RGBA4444 => 2,
DRM_FORMAT_RGBA5551 => 2,
DRM_FORMAT_RGBX4444 => 2,
DRM_FORMAT_RGBX5551 => 2,
DRM_FORMAT_UYVY => 2,
DRM_FORMAT_VYUY => 2,
DRM_FORMAT_XBGR1555 => 2,
DRM_FORMAT_XBGR4444 => 2,
DRM_FORMAT_XRGB1555 => 2,
DRM_FORMAT_XRGB4444 => 2,
DRM_FORMAT_YUYV => 2,
DRM_FORMAT_YVYU => 2,
DRM_FORMAT_BGR888 => 3,
DRM_FORMAT_RGB888 => 3,
DRM_FORMAT_ABGR2101010 => 4,
DRM_FORMAT_ABGR8888 => 4,
DRM_FORMAT_ARGB2101010 => 4,
DRM_FORMAT_ARGB8888 => 4,
DRM_FORMAT_AYUV => 4,
DRM_FORMAT_BGRA1010102 => 4,
DRM_FORMAT_BGRA8888 => 4,
DRM_FORMAT_BGRX1010102 => 4,
DRM_FORMAT_BGRX8888 => 4,
DRM_FORMAT_RGBA1010102 => 4,
DRM_FORMAT_RGBA8888 => 4,
DRM_FORMAT_RGBX1010102 => 4,
DRM_FORMAT_RGBX8888 => 4,
DRM_FORMAT_XBGR2101010 => 4,
DRM_FORMAT_XBGR8888 => 4,
DRM_FORMAT_XRGB2101010 => 4,
DRM_FORMAT_XRGB8888 => 4,
_ => return None,
};
return Some(bpp);
}
}
impl From<u32> for Format {
@ -506,6 +580,28 @@ mod tests {
assert_eq!(buf, "fourcc(0x00010210)");
}
#[test]
fn format_bytes_per_pixel() {
let f = Format::new(b'X', b'R', b'2', b'4');
assert_eq!(f.bytes_per_pixel(0), Some(4));
assert_eq!(f.bytes_per_pixel(1), None);
let f = Format::new(b'N', b'V', b'1', b'2');
assert_eq!(f.bytes_per_pixel(0), Some(1));
assert_eq!(f.bytes_per_pixel(1), Some(2));
assert_eq!(f.bytes_per_pixel(2), None);
let f = Format::new(b'R', b'8', b' ', b' ');
assert_eq!(f.bytes_per_pixel(0), Some(1));
assert_eq!(f.bytes_per_pixel(1), None);
let f = Format::new(b'B', b'G', b'2', b'4');
assert_eq!(f.bytes_per_pixel(0), Some(3));
assert_eq!(f.bytes_per_pixel(1), None);
let f = Format::new(b'G', b'R', b'8', b'8');
assert_eq!(f.bytes_per_pixel(0), Some(2));
assert_eq!(f.bytes_per_pixel(1), None);
let f = Format::new(b'Z', b'A', b'C', b'H');
assert_eq!(f.bytes_per_pixel(0), None);
}
#[test]
#[ignore] // no access to /dev/dri
fn open_device() {