use std::sync::{Arc, Once};
use frust_gpu::RenderContext;
use crate::texture_bus::{
ImportError, ImportedTexture, NativeHandle, NativeHandleKind, NativeImporter,
set_native_importer,
};
struct AndroidImporter;
impl NativeImporter for AndroidImporter {
fn import(
&self,
handle: &NativeHandle,
device: &wgpu::Device,
_queue: &wgpu::Queue,
) -> Result<ImportedTexture, ImportError> {
let imported = unsafe {
frust_gpu::import::android::import_hardware_buffer(device, handle.ptr().cast())
};
let texture = imported.map_err(|error| match error {
frust_gpu::import::android::HardwareBufferImportError::Unsupported(reason) => {
ImportError::Unsupported(reason)
}
other => ImportError::Failed(other.to_string()),
})?;
Ok(ImportedTexture {
view: texture.create_view(&wgpu::TextureViewDescriptor::default()),
size: (texture.width(), texture.height()),
})
}
}
pub fn install(context: &RenderContext) {
static INSTALLED: Once = Once::new();
if !context.caps().is_some_and(|caps| caps.hwbuffer_import) {
return;
}
INSTALLED.call_once(|| {
set_native_importer(NativeHandleKind::AHardwareBuffer, Arc::new(AndroidImporter));
});
}