Pass CVImageBuffers into GPUI instead of IOSurfaces

This commit is contained in:
Nathan Sobo 2022-08-30 20:34:59 -06:00 committed by Antonio Scandurra
parent 0430bbf7d9
commit 531ffc01c9
8 changed files with 95 additions and 96 deletions

22
Cargo.lock generated
View file

@ -763,8 +763,8 @@ dependencies = [
"foreign-types", "foreign-types",
"futures", "futures",
"gpui", "gpui",
"io_surface",
"log", "log",
"media",
"objc", "objc",
"parking_lot 0.11.2", "parking_lot 0.11.2",
"postage", "postage",
@ -2232,9 +2232,9 @@ dependencies = [
"futures", "futures",
"gpui_macros", "gpui_macros",
"image", "image",
"io_surface",
"lazy_static", "lazy_static",
"log", "log",
"media",
"metal", "metal",
"num_cpus", "num_cpus",
"objc", "objc",
@ -2601,15 +2601,6 @@ dependencies = [
"winapi 0.3.9", "winapi 0.3.9",
] ]
[[package]]
name = "io_surface"
version = "0.1.0"
dependencies = [
"block",
"core-foundation",
"objc",
]
[[package]] [[package]]
name = "iovec" name = "iovec"
version = "0.1.4" version = "0.1.4"
@ -3039,6 +3030,15 @@ dependencies = [
"digest 0.10.3", "digest 0.10.3",
] ]
[[package]]
name = "media"
version = "0.1.0"
dependencies = [
"block",
"core-foundation",
"objc",
]
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.5.0" version = "2.5.0"

View file

@ -10,7 +10,7 @@ identifier = "dev.zed.Capture"
[dependencies] [dependencies]
gpui = { path = "../gpui" } gpui = { path = "../gpui" }
io_surface = { path = "../io_surface" } media = { path = "../media" }
block = "0.1" block = "0.1"
cocoa = "0.24" cocoa = "0.24"

View file

@ -16,8 +16,8 @@ use gpui::{
platform::current::Surface, platform::current::Surface,
Menu, MenuItem, ViewContext, Menu, MenuItem, ViewContext,
}; };
use io_surface::IOSurface;
use log::LevelFilter; use log::LevelFilter;
use media::core_video::{self, CVImageBuffer};
use objc::{ use objc::{
class, class,
declare::ClassDecl, declare::ClassDecl,
@ -55,7 +55,7 @@ fn main() {
} }
struct ScreenCaptureView { struct ScreenCaptureView {
surface: Option<io_surface::IOSurface>, image_buffer: Option<core_video::CVImageBuffer>,
} }
impl gpui::Entity for ScreenCaptureView { impl gpui::Entity for ScreenCaptureView {
@ -64,8 +64,9 @@ impl gpui::Entity for ScreenCaptureView {
impl ScreenCaptureView { impl ScreenCaptureView {
pub fn new(cx: &mut ViewContext<Self>) -> Self { pub fn new(cx: &mut ViewContext<Self>) -> Self {
let (surface_tx, mut surface_rx) = postage::watch::channel::<Option<IOSurface>>(); let (image_buffer_tx, mut image_buffer_rx) =
let surface_tx = Arc::new(Mutex::new(surface_tx)); postage::watch::channel::<Option<CVImageBuffer>>();
let image_buffer_tx = Arc::new(Mutex::new(image_buffer_tx));
unsafe { unsafe {
let block = ConcreteBlock::new(move |content: id, error: id| { let block = ConcreteBlock::new(move |content: id, error: id| {
@ -93,7 +94,7 @@ impl ScreenCaptureView {
let output: id = msg_send![capture_output_class, alloc]; let output: id = msg_send![capture_output_class, alloc];
let output: id = msg_send![output, init]; let output: id = msg_send![output, init];
let surface_tx = surface_tx.clone(); let surface_tx = image_buffer_tx.clone();
let callback = Box::new(move |buffer: CMSampleBufferRef| { let callback = Box::new(move |buffer: CMSampleBufferRef| {
let buffer = CMSampleBuffer::wrap_under_get_rule(buffer); let buffer = CMSampleBuffer::wrap_under_get_rule(buffer);
@ -112,8 +113,7 @@ impl ScreenCaptureView {
} }
let image_buffer = buffer.image_buffer(); let image_buffer = buffer.image_buffer();
let io_surface = image_buffer.io_surface(); *surface_tx.lock().borrow_mut() = Some(image_buffer);
*surface_tx.lock().borrow_mut() = Some(io_surface);
}) as Box<dyn FnMut(CMSampleBufferRef)>; }) as Box<dyn FnMut(CMSampleBufferRef)>;
let callback = Box::into_raw(Box::new(callback)); let callback = Box::into_raw(Box::new(callback));
(*output).set_ivar("callback", callback as *mut c_void); (*output).set_ivar("callback", callback as *mut c_void);
@ -169,10 +169,10 @@ impl ScreenCaptureView {
} }
cx.spawn_weak(|this, mut cx| async move { cx.spawn_weak(|this, mut cx| async move {
while let Some(surface) = surface_rx.next().await { while let Some(image_buffer) = image_buffer_rx.next().await {
if let Some(this) = this.upgrade(&cx) { if let Some(this) = this.upgrade(&cx) {
this.update(&mut cx, |this, cx| { this.update(&mut cx, |this, cx| {
this.surface = surface; this.image_buffer = image_buffer;
println!("NEW SURFACE!"); println!("NEW SURFACE!");
cx.notify(); cx.notify();
}) })
@ -183,7 +183,7 @@ impl ScreenCaptureView {
}) })
.detach(); .detach();
Self { surface: None } Self { image_buffer: None }
} }
} }
@ -193,12 +193,12 @@ impl gpui::View for ScreenCaptureView {
} }
fn render(&mut self, _: &mut gpui::RenderContext<Self>) -> gpui::ElementBox { fn render(&mut self, _: &mut gpui::RenderContext<Self>) -> gpui::ElementBox {
let surface = self.surface.clone(); let image_buffer = self.image_buffer.clone();
Canvas::new(move |bounds, _, cx| { Canvas::new(move |bounds, _, cx| {
if let Some(native_surface) = surface.clone() { if let Some(image_buffer) = image_buffer.clone() {
cx.scene.push_surface(Surface { cx.scene.push_surface(Surface {
bounds, bounds,
native_surface, image_buffer,
}); });
} }
}) })
@ -291,48 +291,3 @@ mod core_media {
fn CMSampleBufferGetImageBuffer(buffer: CMSampleBufferRef) -> CVImageBufferRef; fn CMSampleBufferGetImageBuffer(buffer: CMSampleBufferRef) -> CVImageBufferRef;
} }
} }
mod core_video {
#![allow(non_snake_case)]
use core_foundation::{
base::{CFTypeID, TCFType},
declare_TCFType, impl_CFTypeDescription, impl_TCFType,
};
use io_surface::{IOSurface, IOSurfaceRef};
use std::ffi::c_void;
#[repr(C)]
pub struct __CVImageBuffer(c_void);
// The ref type must be a pointer to the underlying struct.
pub type CVImageBufferRef = *const __CVImageBuffer;
declare_TCFType!(CVImageBuffer, CVImageBufferRef);
impl_TCFType!(CVImageBuffer, CVImageBufferRef, CVImageBufferGetTypeID);
impl_CFTypeDescription!(CVImageBuffer);
impl CVImageBuffer {
pub fn io_surface(&self) -> IOSurface {
unsafe {
IOSurface::wrap_under_get_rule(CVPixelBufferGetIOSurface(
self.as_concrete_TypeRef(),
))
}
}
pub fn width(&self) -> usize {
unsafe { CVPixelBufferGetWidth(self.as_concrete_TypeRef()) }
}
pub fn height(&self) -> usize {
unsafe { CVPixelBufferGetHeight(self.as_concrete_TypeRef()) }
}
}
extern "C" {
fn CVImageBufferGetTypeID() -> CFTypeID;
fn CVPixelBufferGetIOSurface(buffer: CVImageBufferRef) -> IOSurfaceRef;
fn CVPixelBufferGetWidth(buffer: CVImageBufferRef) -> usize;
fn CVPixelBufferGetHeight(buffer: CVImageBufferRef) -> usize;
}
}

View file

@ -60,7 +60,7 @@ png = "0.16"
simplelog = "0.9" simplelog = "0.9"
[target.'cfg(target_os = "macos")'.dependencies] [target.'cfg(target_os = "macos")'.dependencies]
io_surface = { path = "../io_surface" } media = { path = "../media" }
anyhow = "1" anyhow = "1"
block = "0.1" block = "0.1"
cocoa = "0.24" cocoa = "0.24"

View file

@ -39,7 +39,7 @@ struct PathSprite {
pub struct Surface { pub struct Surface {
pub bounds: RectF, pub bounds: RectF,
pub native_surface: io_surface::IOSurface, pub image_buffer: media::core_video::CVImageBuffer,
} }
impl Renderer { impl Renderer {
@ -790,7 +790,6 @@ impl Renderer {
let target_size = surface.bounds.size() * scale_factor; let target_size = surface.bounds.size() * scale_factor;
// let corner_radius = surface.corner_radius * scale_factor; // let corner_radius = surface.corner_radius * scale_factor;
// let border_width = surface.border.width * scale_factor; // let border_width = surface.border.width * scale_factor;
// let (alloc_id, atlas_bounds) = self.image_cache.render(&surface.native_surface);
} }
// command_encoder.set_render_pipeline_state(&self.image_pipeline_state); // command_encoder.set_render_pipeline_state(&self.image_pipeline_state);

View file

@ -1,21 +0,0 @@
#![allow(non_snake_case)]
use core_foundation::{
base::{CFTypeID, TCFType},
declare_TCFType, impl_CFTypeDescription, impl_TCFType,
};
use std::ffi::c_void;
#[repr(C)]
pub struct __IOSurface(c_void);
// The ref type must be a pointer to the underlying struct.
pub type IOSurfaceRef = *const __IOSurface;
declare_TCFType!(IOSurface, IOSurfaceRef);
impl_TCFType!(IOSurface, IOSurfaceRef, IOSurfaceGetTypeID);
impl_CFTypeDescription!(IOSurface);
#[link(name = "IOSurface", kind = "framework")]
extern "C" {
fn IOSurfaceGetTypeID() -> CFTypeID;
}

View file

@ -1,10 +1,10 @@
[package] [package]
name = "io_surface" name = "media"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
[lib] [lib]
path = "src/io_surface.rs" path = "src/media.rs"
doctest = false doctest = false
[dependencies] [dependencies]

66
crates/media/src/media.rs Normal file
View file

@ -0,0 +1,66 @@
#![allow(non_snake_case)]
use core_foundation::{
base::{CFTypeID, TCFType},
declare_TCFType, impl_CFTypeDescription, impl_TCFType,
};
use std::ffi::c_void;
pub mod io_surface {
use super::*;
#[repr(C)]
pub struct __IOSurface(c_void);
// The ref type must be a pointer to the underlying struct.
pub type IOSurfaceRef = *const __IOSurface;
declare_TCFType!(IOSurface, IOSurfaceRef);
impl_TCFType!(IOSurface, IOSurfaceRef, IOSurfaceGetTypeID);
impl_CFTypeDescription!(IOSurface);
#[link(name = "IOSurface", kind = "framework")]
extern "C" {
fn IOSurfaceGetTypeID() -> CFTypeID;
}
}
pub mod core_video {
#![allow(non_snake_case)]
use super::*;
use io_surface::{IOSurface, IOSurfaceRef};
#[repr(C)]
pub struct __CVImageBuffer(c_void);
// The ref type must be a pointer to the underlying struct.
pub type CVImageBufferRef = *const __CVImageBuffer;
declare_TCFType!(CVImageBuffer, CVImageBufferRef);
impl_TCFType!(CVImageBuffer, CVImageBufferRef, CVImageBufferGetTypeID);
impl_CFTypeDescription!(CVImageBuffer);
impl CVImageBuffer {
pub fn io_surface(&self) -> IOSurface {
unsafe {
IOSurface::wrap_under_get_rule(CVPixelBufferGetIOSurface(
self.as_concrete_TypeRef(),
))
}
}
pub fn width(&self) -> usize {
unsafe { CVPixelBufferGetWidth(self.as_concrete_TypeRef()) }
}
pub fn height(&self) -> usize {
unsafe { CVPixelBufferGetHeight(self.as_concrete_TypeRef()) }
}
}
extern "C" {
fn CVImageBufferGetTypeID() -> CFTypeID;
fn CVPixelBufferGetIOSurface(buffer: CVImageBufferRef) -> IOSurfaceRef;
fn CVPixelBufferGetWidth(buffer: CVImageBufferRef) -> usize;
fn CVPixelBufferGetHeight(buffer: CVImageBufferRef) -> usize;
}
}