ferroui_media_apple/
lib.rs1#![cfg(target_vendor = "apple")]
3#![deny(unsafe_code)]
4#[allow(unsafe_code)]
5mod native;
6#[cfg(all(target_os = "macos", feature = "opengl"))]
7mod cgl;
8#[cfg(all(target_os = "macos", feature = "opengl"))]
9pub use cgl::CglFrameView;
10use ferroui_base::{
11 platform::{
12 IPlatformHandle, KnownPlatformGraphicsExternalImageHandleTypes, LeasedPlatformHandle,
13 },
14 reactive::Subscription,
15 threading::{DispatcherPriority, DispatcherTimer},
16};
17use ferroui_media::{frames::*, *};
18pub use native::PixelBuffer;
19use std::{rc::Rc, task::Poll, time::Duration};
20pub fn poll_native<T: 'static, E: 'static>(
21 mut read: impl FnMut() -> Poll<Result<T, E>> + 'static,
22) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<T, E>>>> {
23 let mut timer: Option<Subscription> = None;
24 Box::pin(std::future::poll_fn(move |cx| {
25 timer.take();
26 match read() {
27 Poll::Ready(result) => Poll::Ready(result),
28 Poll::Pending => {
29 let waker = cx.waker().clone();
30 timer = Some(DispatcherTimer::run_once(
31 move || waker.wake_by_ref(),
32 Duration::from_millis(16),
33 DispatcherPriority::NORMAL,
34 ));
35 Poll::Pending
36 }
37 }
38 }))
39}
40pub fn frame_surface(frame: &FrameLease) -> Result<Rc<dyn IPlatformHandle>, MediaError> {
43 let surface = frame
44 .resource::<PixelBuffer>()
45 .and_then(|buffer| buffer.surface())
46 .ok_or_else(|| {
47 MediaError::new(MediaErrorKind::Unsupported, "Not a CoreVideo-backed frame")
48 })?;
49 Ok(Rc::new(LeasedPlatformHandle::new(
50 surface as isize,
51 Some(KnownPlatformGraphicsExternalImageHandleTypes::IO_SURFACE_REF),
52 frame.clone(),
53 )))
54}
55
56#[cfg(feature = "controls")]
57pub struct AppleTextureImporter;
58#[cfg(feature = "controls")]
59impl ferroui_media_controls::TextureFrameImporter for AppleTextureImporter {
60 fn import(
61 &self,
62 interop: &dyn ferroui_base::rendering::composition::ICompositionGpuInterop,
63 frame: &FrameLease,
64 ) -> Result<ferroui_media_controls::ImportedTextureFrame, MediaError> {
65 use ferroui_base::platform::{
66 PlatformGraphicsExternalImageFormat, PlatformGraphicsExternalImageProperties,
67 };
68 let m = frame.metadata();
69 #[cfg(all(target_os = "macos", feature = "opengl"))]
70 if frame.resource::<ferroui_opengl::SharedFrameTexture>().is_some() {
71 return ferroui_media_controls::TextureFrameImporter::import(
72 &ferroui_media_opengl::NativeGlTextureImporter, interop, frame);
73 }
74 if !interop.supported_image_handle_types().iter().any(|t| t == KnownPlatformGraphicsExternalImageHandleTypes::IO_SURFACE_REF) {
75 return Err(MediaError::new(MediaErrorKind::Unsupported, "Renderer cannot directly import CoreVideo IOSurfaces"));
76 }
77 if m.device_generation != interop.shared_context_generation() {
78 return Err(MediaError::new(
79 MediaErrorKind::Unsupported,
80 "Frame belongs to a retired renderer generation",
81 ));
82 }
83 if Some(DeviceIdentity::Luid(interop.device_luid().ok_or_else(
84 || {
85 MediaError::new(
86 MediaErrorKind::Unsupported,
87 "Renderer device identity is unavailable",
88 )
89 },
90 )?)) != m.device
91 {
92 return Err(MediaError::new(
93 MediaErrorKind::Unsupported,
94 "Frame and renderer devices differ",
95 ));
96 }
97 let image = interop.import_image(
98 frame_surface(frame)?,
99 PlatformGraphicsExternalImageProperties {
100 width: m.width as i32,
101 height: m.height as i32,
102 format: PlatformGraphicsExternalImageFormat::B8G8R8A8UNorm,
103 top_left_origin: true,
104 memory_size: m.retained_bytes,
105 ..Default::default()
106 },
107 );
108 let producer_ready = GpuCompletion::new();
111 producer_ready.complete();
112 Ok(ferroui_media_controls::ImportedTextureFrame {
113 image,
114 producer_ready,
115 })
116 }
117}