Skip to content

Commit 7ff0d65

Browse files
committed
Add macOS support using Core Graphics
1 parent a23b7f9 commit 7ff0d65

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ x11-dl = "2.19.1"
1616
[target.'cfg(target_os = "windows")'.dependencies]
1717
winapi = "0.3.9"
1818

19+
[target.'cfg(target_os = "macos")'.dependencies]
20+
core-graphics = "0.22.3"
21+
objc = "0.2.7"
22+
1923
[dev-dependencies]
2024
winit = "0.26.1"
2125
image = "0.23.14"

src/cg.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use crate::{GraphicsContextImpl, SoftBufferError};
2+
use raw_window_handle::{HasRawWindowHandle, AppKitHandle};
3+
use objc::runtime::Object;
4+
use core_graphics::base::{kCGBitmapByteOrder32Little, kCGImageAlphaNoneSkipFirst, kCGRenderingIntentDefault};
5+
use core_graphics::color_space::CGColorSpace;
6+
use core_graphics::context::CGContext;
7+
use core_graphics::data_provider::CGDataProvider;
8+
use core_graphics::geometry::{CGPoint, CGSize, CGRect};
9+
use core_graphics::image::CGImage;
10+
use core_graphics::sys;
11+
12+
pub struct CGImpl;
13+
14+
impl CGImpl {
15+
pub unsafe fn new<W: HasRawWindowHandle>(handle: AppKitHandle) -> Result<Self, SoftBufferError<W>> {
16+
let window = handle.ns_window as *mut Object;
17+
let cls = class!(NSGraphicsContext);
18+
let graphics_context: *mut Object = msg_send![cls, graphicsContextWithWindow:window];
19+
if graphics_context.is_null() {
20+
return Err(SoftBufferError::PlatformError(Some("Graphics context is null".into()), None));
21+
}
22+
let _: () = msg_send![cls, setCurrentContext:graphics_context];
23+
Ok(Self)
24+
}
25+
}
26+
27+
impl GraphicsContextImpl for CGImpl {
28+
unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) {
29+
let cls = class!(NSGraphicsContext);
30+
let graphics_context: *mut Object = msg_send![cls, currentContext];
31+
let context_ptr: *mut sys::CGContext = msg_send![graphics_context, CGContext];
32+
let context = CGContext::from_existing_context_ptr(context_ptr);
33+
let color_space = CGColorSpace::create_device_rgb();
34+
let slice = std::slice::from_raw_parts(
35+
buffer.as_ptr() as *const u8,
36+
buffer.len() * 4);
37+
let data_provider = CGDataProvider::from_slice(slice);
38+
let image = CGImage::new(
39+
width as usize,
40+
height as usize,
41+
8,
42+
32,
43+
(width * 4) as usize,
44+
&color_space,
45+
kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst,
46+
&data_provider,
47+
false,
48+
kCGRenderingIntentDefault,
49+
);
50+
let origin = CGPoint { x: 0f64, y: 0f64 };
51+
let size = CGSize { width: width as f64, height: height as f64 };
52+
let rect = CGRect { origin, size };
53+
context.draw_image(rect, &image);
54+
}
55+
}

src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
#![doc = include_str!("../README.md")]
22

3+
#[cfg(target_os = "macos")]
4+
#[macro_use]
5+
extern crate objc;
6+
37
#[cfg(target_os = "windows")]
48
mod win32;
9+
#[cfg(target_os = "macos")]
10+
mod cg;
511
#[cfg(target_os = "linux")]
612
mod x11;
713

@@ -32,6 +38,8 @@ impl<W: HasRawWindowHandle> GraphicsContext<W> {
3238
RawWindowHandle::Xlib(xlib_handle) => Box::new(x11::X11Impl::new(xlib_handle)?),
3339
#[cfg(target_os = "windows")]
3440
RawWindowHandle::Win32(win32_handle) => Box::new(win32::Win32Impl::new(&win32_handle)?),
41+
#[cfg(target_os = "macos")]
42+
RawWindowHandle::AppKit(appkit_handle) => Box::new(cg::CGImpl::new(appkit_handle)?),
3543
unimplemented_handle_type => return Err(SoftBufferError::UnsupportedPlatform {
3644
window,
3745
human_readable_platform_name: window_handle_type_name(&unimplemented_handle_type),

0 commit comments

Comments
 (0)