@@ -174,7 +174,7 @@ impl MetalRenderer {
unit_vertices,
instances,
sprite_atlas,
- core_video_texture_cache: CVMetalTextureCache::new(device.as_ptr()).unwrap(),
+ core_video_texture_cache: unsafe { CVMetalTextureCache::new(device.as_ptr()).unwrap() },
}
}
@@ -849,28 +849,30 @@ impl MetalRenderer {
media::core_video::kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
);
- let y_texture = self
- .core_video_texture_cache
- .create_texture_from_image(
- surface.image_buffer.as_concrete_TypeRef(),
- ptr::null(),
- MTLPixelFormat::R8Unorm,
- surface.image_buffer.plane_width(0),
- surface.image_buffer.plane_height(0),
- 0,
- )
- .unwrap();
- let cb_cr_texture = self
- .core_video_texture_cache
- .create_texture_from_image(
- surface.image_buffer.as_concrete_TypeRef(),
- ptr::null(),
- MTLPixelFormat::RG8Unorm,
- surface.image_buffer.plane_width(1),
- surface.image_buffer.plane_height(1),
- 1,
- )
- .unwrap();
+ let y_texture = unsafe {
+ self.core_video_texture_cache
+ .create_texture_from_image(
+ surface.image_buffer.as_concrete_TypeRef(),
+ ptr::null(),
+ MTLPixelFormat::R8Unorm,
+ surface.image_buffer.plane_width(0),
+ surface.image_buffer.plane_height(0),
+ 0,
+ )
+ .unwrap()
+ };
+ let cb_cr_texture = unsafe {
+ self.core_video_texture_cache
+ .create_texture_from_image(
+ surface.image_buffer.as_concrete_TypeRef(),
+ ptr::null(),
+ MTLPixelFormat::RG8Unorm,
+ surface.image_buffer.plane_width(1),
+ surface.image_buffer.plane_height(1),
+ 1,
+ )
+ .unwrap()
+ };
align_offset(offset);
let next_offset = *offset + mem::size_of::<Surface>();
@@ -108,25 +108,23 @@ pub mod core_video {
impl_CFTypeDescription!(CVMetalTextureCache);
impl CVMetalTextureCache {
- pub fn new(metal_device: *mut MTLDevice) -> Result<Self> {
- unsafe {
- let mut this = ptr::null();
- let result = CVMetalTextureCacheCreate(
- kCFAllocatorDefault,
- ptr::null(),
- metal_device,
- ptr::null(),
- &mut this,
- );
- if result == kCVReturnSuccess {
- Ok(CVMetalTextureCache::wrap_under_create_rule(this))
- } else {
- Err(anyhow!("could not create texture cache, code: {}", result))
- }
+ pub unsafe fn new(metal_device: *mut MTLDevice) -> Result<Self> {
+ let mut this = ptr::null();
+ let result = CVMetalTextureCacheCreate(
+ kCFAllocatorDefault,
+ ptr::null(),
+ metal_device,
+ ptr::null(),
+ &mut this,
+ );
+ if result == kCVReturnSuccess {
+ Ok(CVMetalTextureCache::wrap_under_create_rule(this))
+ } else {
+ Err(anyhow!("could not create texture cache, code: {}", result))
}
}
- pub fn create_texture_from_image(
+ pub unsafe fn create_texture_from_image(
&self,
source: CVImageBufferRef,
texture_attributes: CFDictionaryRef,
@@ -135,24 +133,22 @@ pub mod core_video {
height: usize,
plane_index: usize,
) -> Result<CVMetalTexture> {
- unsafe {
- let mut this = ptr::null();
- let result = CVMetalTextureCacheCreateTextureFromImage(
- kCFAllocatorDefault,
- self.as_concrete_TypeRef(),
- source,
- texture_attributes,
- pixel_format,
- width,
- height,
- plane_index,
- &mut this,
- );
- if result == kCVReturnSuccess {
- Ok(CVMetalTexture::wrap_under_create_rule(this))
- } else {
- Err(anyhow!("could not create texture, code: {}", result))
- }
+ let mut this = ptr::null();
+ let result = CVMetalTextureCacheCreateTextureFromImage(
+ kCFAllocatorDefault,
+ self.as_concrete_TypeRef(),
+ source,
+ texture_attributes,
+ pixel_format,
+ width,
+ height,
+ plane_index,
+ &mut this,
+ );
+ if result == kCVReturnSuccess {
+ Ok(CVMetalTexture::wrap_under_create_rule(this))
+ } else {
+ Err(anyhow!("could not create texture, code: {}", result))
}
}
}
@@ -438,60 +434,56 @@ pub mod video_toolbox {
impl_CFTypeDescription!(VTCompressionSession);
impl VTCompressionSession {
- pub fn new(
+ pub unsafe fn new(
width: usize,
height: usize,
codec: CMVideoCodecType,
callback: VTCompressionOutputCallback,
callback_data: *const c_void,
) -> Result<Self> {
- unsafe {
- let mut this = ptr::null();
- let result = VTCompressionSessionCreate(
- ptr::null(),
- width as i32,
- height as i32,
- codec,
- ptr::null(),
- ptr::null(),
- ptr::null(),
- callback,
- callback_data,
- &mut this,
- );
-
- if result == 0 {
- Ok(Self::wrap_under_create_rule(this))
- } else {
- Err(anyhow!(
- "error creating compression session, code {}",
- result
- ))
- }
+ let mut this = ptr::null();
+ let result = VTCompressionSessionCreate(
+ ptr::null(),
+ width as i32,
+ height as i32,
+ codec,
+ ptr::null(),
+ ptr::null(),
+ ptr::null(),
+ callback,
+ callback_data,
+ &mut this,
+ );
+
+ if result == 0 {
+ Ok(Self::wrap_under_create_rule(this))
+ } else {
+ Err(anyhow!(
+ "error creating compression session, code {}",
+ result
+ ))
}
}
- pub fn encode_frame(
+ pub unsafe fn encode_frame(
&self,
buffer: CVImageBufferRef,
presentation_timestamp: CMTime,
duration: CMTime,
) -> Result<()> {
- unsafe {
- let result = VTCompressionSessionEncodeFrame(
- self.as_concrete_TypeRef(),
- buffer,
- presentation_timestamp,
- duration,
- ptr::null(),
- ptr::null(),
- ptr::null_mut(),
- );
- if result == 0 {
- Ok(())
- } else {
- Err(anyhow!("error encoding frame, code {}", result))
- }
+ let result = VTCompressionSessionEncodeFrame(
+ self.as_concrete_TypeRef(),
+ buffer,
+ presentation_timestamp,
+ duration,
+ ptr::null(),
+ ptr::null(),
+ ptr::null_mut(),
+ );
+ if result == 0 {
+ Ok(())
+ } else {
+ Err(anyhow!("error encoding frame, code {}", result))
}
}
}