Return results for fallible media APIs

This commit is contained in:
Antonio Scandurra 2022-08-31 11:09:14 +02:00
parent 79a7a0e0e7
commit fcf6aa15eb
6 changed files with 34 additions and 21 deletions

1
Cargo.lock generated
View file

@ -3033,6 +3033,7 @@ dependencies = [
name = "media" name = "media"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"bindgen", "bindgen",
"block", "block",
"core-foundation", "core-foundation",

View file

@ -132,7 +132,7 @@ impl Renderer {
"underline_fragment", "underline_fragment",
pixel_format, pixel_format,
); );
let cv_texture_cache = CVMetalTextureCache::new(device.as_ptr()); let cv_texture_cache = CVMetalTextureCache::new(device.as_ptr()).unwrap();
Self { Self {
sprite_cache, sprite_cache,
image_cache, image_cache,
@ -825,14 +825,17 @@ impl Renderer {
panic!("unsupported pixel format") panic!("unsupported pixel format")
}; };
let texture = self.cv_texture_cache.create_texture_from_image( let texture = self
surface.image_buffer.as_concrete_TypeRef(), .cv_texture_cache
ptr::null(), .create_texture_from_image(
pixel_format, surface.image_buffer.as_concrete_TypeRef(),
source_size.x() as usize, ptr::null(),
source_size.y() as usize, pixel_format,
0, source_size.x() as usize,
); source_size.y() as usize,
0,
)
.unwrap();
align_offset(offset); align_offset(offset);
let next_offset = *offset + mem::size_of::<shaders::GPUIImage>(); let next_offset = *offset + mem::size_of::<shaders::GPUIImage>();

View file

@ -8,6 +8,7 @@ path = "src/media.rs"
doctest = false doctest = false
[dependencies] [dependencies]
anyhow = "1.0"
block = "0.1" block = "0.1"
core-foundation = "0.9.3" core-foundation = "0.9.3"
foreign-types = "0.3" foreign-types = "0.3"

View file

@ -17,6 +17,7 @@ fn main() {
.clang_arg(format!("-isysroot{}", sdk_path)) .clang_arg(format!("-isysroot{}", sdk_path))
.clang_arg("-xobjective-c") .clang_arg("-xobjective-c")
.allowlist_var("kCVPixelFormatType_.*") .allowlist_var("kCVPixelFormatType_.*")
.allowlist_var("kCVReturn.*")
.parse_callbacks(Box::new(bindgen::CargoCallbacks)) .parse_callbacks(Box::new(bindgen::CargoCallbacks))
.layout_tests(false) .layout_tests(false)
.generate() .generate()

View file

@ -1 +1,2 @@
#import <CoreVideo/CVPixelFormatDescription.h> #import <CoreVideo/CVPixelFormatDescription.h>
#import <CoreVideo/CVReturn.h>

View file

@ -33,16 +33,16 @@ pub mod io_surface {
pub mod core_video { pub mod core_video {
#![allow(non_snake_case)] #![allow(non_snake_case)]
use std::ptr;
use super::*; use super::*;
pub use crate::bindings::*; pub use crate::bindings::*;
use anyhow::{anyhow, Result};
use core_foundation::{ use core_foundation::{
base::kCFAllocatorDefault, dictionary::CFDictionaryRef, mach_port::CFAllocatorRef, base::kCFAllocatorDefault, dictionary::CFDictionaryRef, mach_port::CFAllocatorRef,
}; };
use foreign_types::ForeignTypeRef; use foreign_types::ForeignTypeRef;
use io_surface::{IOSurface, IOSurfaceRef}; use io_surface::{IOSurface, IOSurfaceRef};
use metal::{MTLDevice, MTLPixelFormat, MTLTexture}; use metal::{MTLDevice, MTLPixelFormat};
use std::ptr;
#[repr(C)] #[repr(C)]
pub struct __CVImageBuffer(c_void); pub struct __CVImageBuffer(c_void);
@ -97,7 +97,7 @@ pub mod core_video {
impl_CFTypeDescription!(CVMetalTextureCache); impl_CFTypeDescription!(CVMetalTextureCache);
impl CVMetalTextureCache { impl CVMetalTextureCache {
pub fn new(metal_device: *mut MTLDevice) -> Self { pub fn new(metal_device: *mut MTLDevice) -> Result<Self> {
unsafe { unsafe {
let mut this = ptr::null(); let mut this = ptr::null();
let result = CVMetalTextureCacheCreate( let result = CVMetalTextureCacheCreate(
@ -107,8 +107,11 @@ pub mod core_video {
ptr::null_mut(), ptr::null_mut(),
&mut this, &mut this,
); );
// TODO: Check result if result == kCVReturnSuccess {
CVMetalTextureCache::wrap_under_create_rule(this) Ok(CVMetalTextureCache::wrap_under_create_rule(this))
} else {
Err(anyhow!("could not create texture cache, code: {}", result))
}
} }
} }
@ -120,7 +123,7 @@ pub mod core_video {
width: usize, width: usize,
height: usize, height: usize,
plane_index: usize, plane_index: usize,
) -> CVMetalTexture { ) -> Result<CVMetalTexture> {
unsafe { unsafe {
let mut this = ptr::null(); let mut this = ptr::null();
let result = CVMetalTextureCacheCreateTextureFromImage( let result = CVMetalTextureCacheCreateTextureFromImage(
@ -134,8 +137,11 @@ pub mod core_video {
plane_index, plane_index,
&mut this, &mut this,
); );
// TODO: Check result if result == kCVReturnSuccess {
CVMetalTexture::wrap_under_create_rule(this) Ok(CVMetalTexture::wrap_under_create_rule(this))
} else {
Err(anyhow!("could not create texture, code: {}", result))
}
} }
} }
} }
@ -149,7 +155,7 @@ pub mod core_video {
metal_device: *const MTLDevice, metal_device: *const MTLDevice,
texture_attributes: CFDictionaryRef, texture_attributes: CFDictionaryRef,
cache_out: *mut CVMetalTextureCacheRef, cache_out: *mut CVMetalTextureCacheRef,
) -> i32; // TODO: This should be a CVReturn enum ) -> CVReturn;
fn CVMetalTextureCacheCreateTextureFromImage( fn CVMetalTextureCacheCreateTextureFromImage(
allocator: CFAllocatorRef, allocator: CFAllocatorRef,
texture_cache: CVMetalTextureCacheRef, texture_cache: CVMetalTextureCacheRef,
@ -160,7 +166,7 @@ pub mod core_video {
height: usize, height: usize,
plane_index: usize, plane_index: usize,
texture_out: *mut CVMetalTextureRef, texture_out: *mut CVMetalTextureRef,
) -> i32; ) -> CVReturn;
} }
#[repr(C)] #[repr(C)]