#[cfg(target_os = "macos")]
use j2k::EncodedJ2k;
#[cfg(target_os = "macos")]
use j2k_core::BackendKind;
#[cfg(target_os = "macos")]
use metal::Buffer;
#[cfg(target_os = "macos")]
use std::time::Duration;
#[cfg(target_os = "macos")]
use std::time::Instant;
#[cfg(target_os = "macos")]
pub struct MetalEncodedJ2k {
pub codestream_buffer: Buffer,
pub byte_offset: usize,
pub byte_len: usize,
pub capacity: usize,
pub width: u32,
pub height: u32,
pub components: u8,
pub bit_depth: u8,
pub signed: bool,
}
#[cfg(target_os = "macos")]
impl MetalEncodedJ2k {
pub fn codestream_bytes(&self) -> Result<&[u8], crate::Error> {
let end = self.byte_offset.checked_add(self.byte_len).ok_or_else(|| {
crate::Error::MetalKernel {
message: "J2K Metal codestream byte range overflow".to_string(),
}
})?;
let buffer_len = usize::try_from(self.codestream_buffer.length()).map_err(|_| {
crate::Error::MetalKernel {
message: "J2K Metal codestream buffer length exceeds usize".to_string(),
}
})?;
if end > buffer_len {
return Err(crate::Error::MetalKernel {
message: "J2K Metal codestream byte range exceeds buffer length".to_string(),
});
}
let ptr = self.codestream_buffer.contents().cast::<u8>();
if ptr.is_null() {
return Err(crate::Error::MetalKernel {
message: "J2K Metal codestream buffer is not CPU-readable".to_string(),
});
}
Ok(unsafe { core::slice::from_raw_parts(ptr.add(self.byte_offset), self.byte_len) })
}
pub fn to_encoded_j2k(&self) -> Result<EncodedJ2k, crate::Error> {
let (encoded, _host_readback_duration) = self.to_encoded_j2k_with_readback_duration()?;
Ok(encoded)
}
pub(super) fn to_encoded_j2k_with_readback_duration(
&self,
) -> Result<(EncodedJ2k, Duration), crate::Error> {
let readback_started = Instant::now();
let codestream = self.codestream_bytes()?.to_vec();
let host_readback_duration = readback_started.elapsed();
Ok((
EncodedJ2k {
codestream,
backend: BackendKind::Metal,
dispatch_report: j2k::adapter::encode_stage::J2kEncodeDispatchReport::default(),
width: self.width,
height: self.height,
components: self.components,
bit_depth: self.bit_depth,
signed: self.signed,
},
host_readback_duration,
))
}
}
#[cfg(not(target_os = "macos"))]
pub struct MetalEncodedJ2k {
_private: (),
}