use std::collections::BTreeMap as Map;
#[cfg(feature = "vulkano")]
use log::error;
use crate::rutabaga_gralloc::formats::*;
#[cfg(feature = "minigbm")]
use crate::rutabaga_gralloc::minigbm::MinigbmDevice;
use crate::rutabaga_gralloc::system_gralloc::SystemGralloc;
#[cfg(feature = "vulkano")]
use crate::rutabaga_gralloc::vulkano_gralloc::VulkanoGralloc;
use crate::rutabaga_os::round_up_to_page_size;
use crate::rutabaga_os::MappedRegion;
use crate::rutabaga_utils::*;
const RUTABAGA_GRALLOC_BACKEND_SYSTEM: u32 = 1 << 0;
const RUTABAGA_GRALLOC_BACKEND_GBM: u32 = 1 << 1;
const RUTABAGA_GRALLOC_BACKEND_VULKANO: u32 = 1 << 2;
#[derive(Copy, Clone, Eq, PartialEq, Default)]
pub struct RutabagaGrallocBackendFlags(pub u32);
impl RutabagaGrallocBackendFlags {
#[inline(always)]
pub fn new() -> RutabagaGrallocBackendFlags {
RutabagaGrallocBackendFlags(
RUTABAGA_GRALLOC_BACKEND_SYSTEM
| RUTABAGA_GRALLOC_BACKEND_GBM
| RUTABAGA_GRALLOC_BACKEND_VULKANO,
)
}
#[inline(always)]
pub fn disable_vulkano(self) -> RutabagaGrallocBackendFlags {
RutabagaGrallocBackendFlags(self.0 & !RUTABAGA_GRALLOC_BACKEND_VULKANO)
}
pub fn uses_system(&self) -> bool {
self.0 & RUTABAGA_GRALLOC_BACKEND_SYSTEM != 0
}
pub fn uses_gbm(&self) -> bool {
self.0 & RUTABAGA_GRALLOC_BACKEND_GBM != 0
}
pub fn uses_vulkano(&self) -> bool {
self.0 & RUTABAGA_GRALLOC_BACKEND_VULKANO != 0
}
}
const RUTABAGA_GRALLOC_USE_SCANOUT: u32 = 1 << 0;
const RUTABAGA_GRALLOC_USE_RENDERING: u32 = 1 << 2;
const RUTABAGA_GRALLOC_USE_LINEAR: u32 = 1 << 4;
const RUTABAGA_GRALLOC_USE_TEXTURING: u32 = 1 << 5;
const RUTABAGA_GRALLOC_USE_CAMERA_WRITE: u32 = 1 << 6;
const RUTABAGA_GRALLOC_USE_CAMERA_READ: u32 = 1 << 7;
#[allow(dead_code)]
const RUTABAGA_GRALLOC_USE_PROTECTED: u32 = 1 << 8;
const RUTABAGA_GRALLOC_USE_SW_READ_OFTEN: u32 = 1 << 9;
const RUTABAGA_GRALLOC_USE_SW_WRITE_OFTEN: u32 = 1 << 11;
#[allow(dead_code)]
const RUTABAGA_GRALLOC_VIDEO_DECODER: u32 = 1 << 13;
#[allow(dead_code)]
const RUTABAGA_GRALLOC_VIDEO_ENCODER: u32 = 1 << 14;
#[derive(Copy, Clone, Eq, PartialEq, Default)]
pub struct RutabagaGrallocFlags(pub u32);
impl RutabagaGrallocFlags {
#[inline(always)]
pub fn empty() -> RutabagaGrallocFlags {
RutabagaGrallocFlags(0)
}
#[inline(always)]
pub fn new(raw: u32) -> RutabagaGrallocFlags {
RutabagaGrallocFlags(raw)
}
#[inline(always)]
pub fn use_scanout(self, e: bool) -> RutabagaGrallocFlags {
if e {
RutabagaGrallocFlags(self.0 | RUTABAGA_GRALLOC_USE_SCANOUT)
} else {
RutabagaGrallocFlags(self.0 & !RUTABAGA_GRALLOC_USE_SCANOUT)
}
}
#[inline(always)]
pub fn use_rendering(self, e: bool) -> RutabagaGrallocFlags {
if e {
RutabagaGrallocFlags(self.0 | RUTABAGA_GRALLOC_USE_RENDERING)
} else {
RutabagaGrallocFlags(self.0 & !RUTABAGA_GRALLOC_USE_RENDERING)
}
}
#[inline(always)]
pub fn use_linear(self, e: bool) -> RutabagaGrallocFlags {
if e {
RutabagaGrallocFlags(self.0 | RUTABAGA_GRALLOC_USE_LINEAR)
} else {
RutabagaGrallocFlags(self.0 & !RUTABAGA_GRALLOC_USE_LINEAR)
}
}
#[inline(always)]
pub fn use_sw_write(self, e: bool) -> RutabagaGrallocFlags {
if e {
RutabagaGrallocFlags(self.0 | RUTABAGA_GRALLOC_USE_SW_WRITE_OFTEN)
} else {
RutabagaGrallocFlags(self.0 & !RUTABAGA_GRALLOC_USE_SW_WRITE_OFTEN)
}
}
#[inline(always)]
pub fn use_sw_read(self, e: bool) -> RutabagaGrallocFlags {
if e {
RutabagaGrallocFlags(self.0 | RUTABAGA_GRALLOC_USE_SW_READ_OFTEN)
} else {
RutabagaGrallocFlags(self.0 & !RUTABAGA_GRALLOC_USE_SW_READ_OFTEN)
}
}
#[inline(always)]
pub fn uses_texturing(self) -> bool {
self.0 & RUTABAGA_GRALLOC_USE_TEXTURING != 0
}
#[inline(always)]
pub fn uses_rendering(self) -> bool {
self.0 & RUTABAGA_GRALLOC_USE_RENDERING != 0
}
#[inline(always)]
pub fn host_visible(self) -> bool {
self.0 & RUTABAGA_GRALLOC_USE_SW_READ_OFTEN != 0
|| self.0 & RUTABAGA_GRALLOC_USE_SW_WRITE_OFTEN != 0
|| self.0 & RUTABAGA_GRALLOC_USE_CAMERA_WRITE != 0
|| self.0 & RUTABAGA_GRALLOC_USE_CAMERA_READ != 0
}
#[inline(always)]
pub fn host_cached(self) -> bool {
self.0 & RUTABAGA_GRALLOC_USE_CAMERA_READ != 0
|| self.0 & RUTABAGA_GRALLOC_USE_SW_READ_OFTEN != 0
}
}
#[derive(Copy, Clone, Default)]
pub struct ImageAllocationInfo {
pub width: u32,
pub height: u32,
pub drm_format: DrmFormat,
pub flags: RutabagaGrallocFlags,
}
#[derive(Copy, Clone, Default)]
pub struct ImageMemoryRequirements {
pub info: ImageAllocationInfo,
pub map_info: u32,
pub strides: [u32; 4],
pub offsets: [u32; 4],
pub modifier: u64,
pub size: u64,
pub vulkan_info: Option<VulkanInfo>,
}
pub trait Gralloc: Send {
fn supports_external_gpu_memory(&self) -> bool;
fn supports_dmabuf(&self) -> bool;
fn get_image_memory_requirements(
&mut self,
info: ImageAllocationInfo,
) -> RutabagaResult<ImageMemoryRequirements>;
fn allocate_memory(&mut self, reqs: ImageMemoryRequirements) -> RutabagaResult<RutabagaHandle>;
fn import_and_map(
&mut self,
_handle: RutabagaHandle,
_vulkan_info: VulkanInfo,
_size: u64,
) -> RutabagaResult<Box<dyn MappedRegion>> {
Err(RutabagaError::Unsupported)
}
}
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
pub enum GrallocBackend {
#[allow(dead_code)]
Vulkano,
#[allow(dead_code)]
Minigbm,
System,
}
pub struct RutabagaGralloc {
grallocs: Map<GrallocBackend, Box<dyn Gralloc>>,
}
impl RutabagaGralloc {
pub fn new(flags: RutabagaGrallocBackendFlags) -> RutabagaResult<RutabagaGralloc> {
let mut grallocs: Map<GrallocBackend, Box<dyn Gralloc>> = Default::default();
if flags.uses_system() {
let system = SystemGralloc::init()?;
grallocs.insert(GrallocBackend::System, system);
}
#[cfg(feature = "minigbm")]
if flags.uses_gbm() {
if let Ok(gbm_device) = MinigbmDevice::init() {
grallocs.insert(GrallocBackend::Minigbm, gbm_device);
}
}
#[cfg(feature = "vulkano")]
if flags.uses_vulkano() {
match VulkanoGralloc::init() {
Ok(vulkano) => {
grallocs.insert(GrallocBackend::Vulkano, vulkano);
}
Err(e) => {
error!("failed to init Vulkano gralloc: {:?}", e);
}
}
}
Ok(RutabagaGralloc { grallocs })
}
pub fn supports_external_gpu_memory(&self) -> bool {
for gralloc in self.grallocs.values() {
if gralloc.supports_external_gpu_memory() {
return true;
}
}
false
}
pub fn supports_dmabuf(&self) -> bool {
for gralloc in self.grallocs.values() {
if gralloc.supports_dmabuf() {
return true;
}
}
false
}
fn determine_optimal_backend(&self, _info: ImageAllocationInfo) -> GrallocBackend {
#[allow(clippy::let_and_return)]
let mut _backend = GrallocBackend::System;
#[cfg(feature = "minigbm")]
{
if self.grallocs.contains_key(&GrallocBackend::Minigbm) {
_backend = GrallocBackend::Minigbm;
}
}
#[cfg(feature = "vulkano")]
{
_backend = GrallocBackend::Vulkano;
}
_backend
}
pub fn get_image_memory_requirements(
&mut self,
info: ImageAllocationInfo,
) -> RutabagaResult<ImageMemoryRequirements> {
let backend = self.determine_optimal_backend(info);
let gralloc = self
.grallocs
.get_mut(&backend)
.ok_or(RutabagaError::InvalidGrallocBackend)?;
let mut reqs = gralloc.get_image_memory_requirements(info)?;
reqs.size = round_up_to_page_size(reqs.size)?;
Ok(reqs)
}
pub fn allocate_memory(
&mut self,
reqs: ImageMemoryRequirements,
) -> RutabagaResult<RutabagaHandle> {
let backend = self.determine_optimal_backend(reqs.info);
let gralloc = self
.grallocs
.get_mut(&backend)
.ok_or(RutabagaError::InvalidGrallocBackend)?;
gralloc.allocate_memory(reqs)
}
pub fn import_and_map(
&mut self,
handle: RutabagaHandle,
vulkan_info: VulkanInfo,
size: u64,
) -> RutabagaResult<Box<dyn MappedRegion>> {
let gralloc = self
.grallocs
.get_mut(&GrallocBackend::Vulkano)
.ok_or(RutabagaError::InvalidGrallocBackend)?;
gralloc.import_and_map(handle, vulkan_info, size)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[cfg_attr(target_os = "windows", ignore)]
fn create_render_target() {
let gralloc_result = RutabagaGralloc::new(RutabagaGrallocBackendFlags::new());
if gralloc_result.is_err() {
return;
}
let mut gralloc = gralloc_result.unwrap();
let info = ImageAllocationInfo {
width: 512,
height: 1024,
drm_format: DrmFormat::new(b'X', b'R', b'2', b'4'),
flags: RutabagaGrallocFlags::empty().use_scanout(true),
};
let reqs = gralloc.get_image_memory_requirements(info).unwrap();
let min_reqs = canonical_image_requirements(info).unwrap();
assert!(reqs.strides[0] >= min_reqs.strides[0]);
assert!(reqs.size >= min_reqs.size);
let _handle = gralloc.allocate_memory(reqs).unwrap();
let _handle2 = gralloc.allocate_memory(reqs).unwrap();
}
#[test]
#[cfg_attr(target_os = "windows", ignore)]
fn create_video_buffer() {
let gralloc_result = RutabagaGralloc::new(RutabagaGrallocBackendFlags::new());
if gralloc_result.is_err() {
return;
}
let mut gralloc = gralloc_result.unwrap();
let info = ImageAllocationInfo {
width: 512,
height: 1024,
drm_format: DrmFormat::new(b'N', b'V', b'1', b'2'),
flags: RutabagaGrallocFlags::empty().use_linear(true),
};
let reqs = gralloc.get_image_memory_requirements(info).unwrap();
let min_reqs = canonical_image_requirements(info).unwrap();
assert!(reqs.strides[0] >= min_reqs.strides[0]);
assert!(reqs.strides[1] >= min_reqs.strides[1]);
assert_eq!(reqs.strides[2], 0);
assert_eq!(reqs.strides[3], 0);
assert!(reqs.offsets[0] >= min_reqs.offsets[0]);
assert!(reqs.offsets[1] >= min_reqs.offsets[1]);
assert_eq!(reqs.offsets[2], 0);
assert_eq!(reqs.offsets[3], 0);
assert!(reqs.size >= min_reqs.size);
let _handle = gralloc.allocate_memory(reqs).unwrap();
let _handle2 = gralloc.allocate_memory(reqs).unwrap();
}
#[test]
#[cfg_attr(target_os = "windows", ignore)]
fn export_and_map() {
let gralloc_result = RutabagaGralloc::new(RutabagaGrallocBackendFlags::new());
if gralloc_result.is_err() {
return;
}
let mut gralloc = gralloc_result.unwrap();
let info = ImageAllocationInfo {
width: 512,
height: 1024,
drm_format: DrmFormat::new(b'X', b'R', b'2', b'4'),
flags: RutabagaGrallocFlags::empty()
.use_linear(true)
.use_sw_write(true)
.use_sw_read(true),
};
let mut reqs = gralloc.get_image_memory_requirements(info).unwrap();
if reqs.vulkan_info.is_none() {
return;
}
let handle = gralloc.allocate_memory(reqs).unwrap();
let vulkan_info = reqs.vulkan_info.take().unwrap();
let mapping = gralloc
.import_and_map(handle, vulkan_info, reqs.size)
.unwrap();
let addr = mapping.as_ptr();
let size = mapping.size();
assert_eq!(size as u64, reqs.size);
assert_ne!(addr as *const u8, std::ptr::null());
}
}