mod alias;
mod conversion;
mod device;
mod frame;
mod vulkan;
pub use conversion::ConversionFormat;
pub use device::{DEVICE_EXTENSIONS, DeviceRequestError, DeviceRequirements, request_device};
pub use frame::{HardwareBufferFrame, HardwareBufferLease};
pub use ndk::hardware_buffer::{HardwareBuffer, HardwareBufferDesc, HardwareBufferUsage};
pub use ndk::hardware_buffer_format::HardwareBufferFormat;
use ash::vk;
use conversion::Converter;
use frame::BufferReference;
use vulkan::{BufferProperties, ImportedImage};
use crate::{YcbcrEncoding, YcbcrMatrix, YcbcrRange};
#[derive(Debug, thiserror::Error)]
pub enum HardwareBufferImportError {
#[error(
"the AHardwareBuffer format {format:?} has no Vulkan format on this device, only the \
external format {external_format:#x}, and only Y8Cb8Cr8_420 buffers of an external \
format are converted"
)]
ExternalFormat {
format: HardwareBufferFormat,
external_format: u64,
},
#[error(
"the Vulkan driver could not allocate the descriptor set that binds the AHardwareBuffer's \
{format} to its YCbCr sampler from a pool of {descriptor_count} combined image sampler \
descriptors"
)]
ConversionDescriptorPool {
format: ConversionFormat,
descriptor_count: u32,
},
#[error(
"the AHardwareBuffer format {format:?} imports as YCbCr, but the driver suggests the \
YCbCr model {}, which names no YCbCr matrix",
ycbcr_model_name(*.model)
)]
UnsupportedYcbcrModel {
format: HardwareBufferFormat,
model: i32,
},
#[error(
"the AHardwareBuffer format {format:?} imports as the Vulkan format {}, which this \
import does not support",
vk_format_name(*.vulkan_format)
)]
UnsupportedFormat {
format: HardwareBufferFormat,
vulkan_format: i32,
},
#[error("the AHardwareBuffer usage {0:?} lacks GPU_SAMPLED_IMAGE")]
NotGpuSampled(HardwareBufferUsage),
#[error("the AHardwareBuffer holds protected content")]
Protected,
#[error("the AHardwareBuffer has {0} layers; only single-layer buffers import as a 2D texture")]
Layered(u32),
}
fn vk_format_name(raw: i32) -> String {
format!("{:?}", vk::Format::from_raw(raw))
}
fn ycbcr_model_name(raw: i32) -> String {
format!("{:?}", vk::SamplerYcbcrModelConversion::from_raw(raw))
}
#[derive(Debug)]
pub enum ImportedHardwareBuffer {
Rgba(wgpu::Texture),
Ycbcr420(Ycbcr420Planes),
}
#[derive(Debug)]
pub struct Ycbcr420Planes {
pub luma: wgpu::TextureView,
pub chroma: wgpu::TextureView,
pub encoding: YcbcrEncoding,
}
#[derive(Debug)]
pub struct HardwareBufferImporter {
converter: Option<Converter>,
nv12: bool,
device: wgpu::Device,
queue: wgpu::Queue,
}
impl HardwareBufferImporter {
#[must_use]
pub fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Self {
let hal_device = unsafe {
device
.as_hal::<wgpu::hal::api::Vulkan>()
.expect("AHardwareBuffer import requires a Vulkan device")
};
vulkan::validate_device(&hal_device);
drop(hal_device);
Self {
converter: None,
nv12: device
.features()
.contains(wgpu::Features::TEXTURE_FORMAT_NV12),
device: device.clone(),
queue: queue.clone(),
}
}
pub fn import(
&mut self,
frame: HardwareBufferFrame,
) -> Result<ImportedHardwareBuffer, HardwareBufferImportError> {
let parts = frame.into_parts();
check_description(&parts.description)?;
let hal_device = unsafe {
self.device
.as_hal::<wgpu::hal::api::Vulkan>()
.expect("AHardwareBuffer import requires a Vulkan device")
};
let properties = vulkan::buffer_properties(&hal_device, parts.buffer.buffer());
match properties.format {
vk::Format::UNDEFINED => {
if parts.description.format != HardwareBufferFormat::Y8Cb8Cr8_420 {
return Err(HardwareBufferImportError::ExternalFormat {
format: parts.description.format,
external_format: properties.external_format,
});
}
let encoding = ycbcr_encoding(&parts.description, &properties)?;
let converter = self
.converter
.get_or_insert_with(|| Converter::new(&hal_device));
let planes = conversion::import(
&self.device,
&self.queue,
converter,
hal_device,
parts,
&properties,
encoding,
)?;
Ok(ImportedHardwareBuffer::Ycbcr420(planes))
}
vk::Format::R8G8B8A8_UNORM => {
let texture = alias::import(
&self.device,
&self.queue,
hal_device,
parts,
&properties,
wgpu::TextureFormat::Rgba8Unorm,
);
Ok(ImportedHardwareBuffer::Rgba(texture))
}
vk::Format::G8_B8R8_2PLANE_420_UNORM => {
let encoding = ycbcr_encoding(&parts.description, &properties)?;
if self.nv12 {
let texture = alias::import(
&self.device,
&self.queue,
hal_device,
parts,
&properties,
wgpu::TextureFormat::NV12,
);
let plane = |format, aspect| {
texture.create_view(&wgpu::TextureViewDescriptor {
format: Some(format),
aspect,
..wgpu::TextureViewDescriptor::default()
})
};
Ok(ImportedHardwareBuffer::Ycbcr420(Ycbcr420Planes {
luma: plane(wgpu::TextureFormat::R8Unorm, wgpu::TextureAspect::Plane0),
chroma: plane(wgpu::TextureFormat::Rg8Unorm, wgpu::TextureAspect::Plane1),
encoding,
}))
} else {
let converter = self
.converter
.get_or_insert_with(|| Converter::new(&hal_device));
let planes = conversion::import(
&self.device,
&self.queue,
converter,
hal_device,
parts,
&properties,
encoding,
)?;
Ok(ImportedHardwareBuffer::Ycbcr420(planes))
}
}
other => Err(HardwareBufferImportError::UnsupportedFormat {
format: parts.description.format,
vulkan_format: other.as_raw(),
}),
}
}
}
const fn check_description(
description: &HardwareBufferDesc,
) -> Result<(), HardwareBufferImportError> {
if !description
.usage
.contains(HardwareBufferUsage::GPU_SAMPLED_IMAGE)
{
return Err(HardwareBufferImportError::NotGpuSampled(description.usage));
}
if description
.usage
.contains(HardwareBufferUsage::PROTECTED_CONTENT)
{
return Err(HardwareBufferImportError::Protected);
}
if description.layers != 1 {
return Err(HardwareBufferImportError::Layered(description.layers));
}
Ok(())
}
fn ycbcr_encoding(
description: &HardwareBufferDesc,
properties: &BufferProperties,
) -> Result<YcbcrEncoding, HardwareBufferImportError> {
let matrix = match properties.suggested_model {
vk::SamplerYcbcrModelConversion::YCBCR_601 => YcbcrMatrix::Bt601,
vk::SamplerYcbcrModelConversion::YCBCR_709 => YcbcrMatrix::Bt709,
vk::SamplerYcbcrModelConversion::YCBCR_2020 => YcbcrMatrix::Bt2020,
other => {
return Err(HardwareBufferImportError::UnsupportedYcbcrModel {
format: description.format,
model: other.as_raw(),
});
}
};
let range = match properties.suggested_range {
vk::SamplerYcbcrRange::ITU_FULL => YcbcrRange::Full,
vk::SamplerYcbcrRange::ITU_NARROW => YcbcrRange::Video,
other => panic!(
"the Vulkan driver suggested {other:?} as the YCbCr range of an AHardwareBuffer, \
which is not a VkSamplerYcbcrRange value"
),
};
Ok(YcbcrEncoding { matrix, range })
}
fn record_raw(
device: &wgpu::Device,
label: &'static str,
record: impl FnOnce(&mut wgpu::hal::vulkan::CommandEncoder),
) -> wgpu::CommandEncoder {
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some(label) });
unsafe {
encoder.as_hal_mut::<wgpu::hal::api::Vulkan, _, _>(|encoder| {
record(encoder.expect("AHardwareBuffer command encoder is not Vulkan"));
});
}
encoder
}
fn submit(
device: &wgpu::Device,
queue: &wgpu::Queue,
raw: wgpu::CommandEncoder,
textures: &[&wgpu::Texture],
acquire_semaphore: Option<vk::Semaphore>,
) {
let mut track = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("wgpu_external_frame_ahardware_buffer_track"),
});
track.transition_resources(
core::iter::empty(),
textures.iter().map(|&texture| wgpu::TextureTransition {
texture,
selector: None,
state: wgpu::TextureUses::RESOURCE,
}),
);
let command_buffers = [raw.finish(), track.finish()];
if let Some(semaphore) = acquire_semaphore {
let hal_queue = unsafe {
queue
.as_hal::<wgpu::hal::api::Vulkan>()
.expect("AHardwareBuffer import requires a Vulkan queue")
};
hal_queue.add_wait_semaphore(semaphore, None, vk::PipelineStageFlags::ALL_COMMANDS);
}
queue.submit(command_buffers);
}
struct Retirement {
imported: ImportedImage,
buffer: BufferReference,
lease: Option<Box<dyn HardwareBufferLease>>,
}
impl Retirement {
fn retire(self) {
let Self {
imported,
buffer,
lease,
} = self;
drop(imported);
drop(buffer);
if let Some(lease) = lease {
lease.release();
}
}
}