Skip to content

Commit

Permalink
Merge pull request #3 from lunixbochs/calayer
Browse files Browse the repository at this point in the history
use CALayer for macOS backend
  • Loading branch information
john01dav authored May 17, 2022
2 parents 7521606 + 112e777 commit dac3671
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ x11-dl = "2.19.1"
winapi = "0.3.9"

[target.'cfg(target_os = "macos")'.dependencies]
cocoa = "0.24.0"
core-graphics = "0.22.3"
foreign-types = "0.3.0"
objc = "0.2.7"

[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand All @@ -49,4 +51,4 @@ features = ["jpeg"]
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
# Turn rayon back on everywhere else; creating the separate entry resets the features to default.
image = "0.23.14"
rayon = "1.5.1"
rayon = "1.5.1"
54 changes: 23 additions & 31 deletions src/cg.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,44 @@
use crate::{GraphicsContextImpl, SoftBufferError};
use raw_window_handle::{HasRawWindowHandle, AppKitHandle};
use objc::runtime::Object;
use core_graphics::base::{kCGBitmapByteOrder32Little, kCGImageAlphaNoneSkipFirst, kCGRenderingIntentDefault};
use core_graphics::color_space::CGColorSpace;
use core_graphics::context::CGContext;
use core_graphics::data_provider::CGDataProvider;
use core_graphics::geometry::{CGPoint, CGSize, CGRect};
use core_graphics::image::CGImage;
use core_graphics::sys;

use cocoa::base::{id, nil};
use cocoa::appkit::{NSView, NSViewWidthSizable, NSViewHeightSizable};
use cocoa::quartzcore::{CALayer, ContentsGravity};
use foreign_types::ForeignType;

use std::sync::Arc;

pub struct CGImpl {
view: *mut Object,
layer: CALayer,
}

impl CGImpl {
pub unsafe fn new<W: HasRawWindowHandle>(handle: AppKitHandle) -> Result<Self, SoftBufferError<W>> {
let window = handle.ns_window as *mut Object;
let view = handle.ns_view as *mut Object;
let cls = class!(NSGraphicsContext);
let graphics_context: *mut Object = msg_send![cls, graphicsContextWithWindow:window];
if graphics_context.is_null() {
return Err(SoftBufferError::PlatformError(Some("Graphics context is null".into()), None));
}
let _: () = msg_send![cls, setCurrentContext:graphics_context];
Ok(
Self {
view,
}
)
let view = handle.ns_view as id;
let layer = CALayer::new();
let subview: id = NSView::alloc(nil).initWithFrame_(view.frame());
layer.set_contents_gravity(ContentsGravity::TopLeft);
layer.set_needs_display_on_bounds_change(false);
subview.setLayer(layer.id());
subview.setAutoresizingMask_(NSViewWidthSizable | NSViewHeightSizable);

view.addSubview_(subview); // retains subview (+1) = 2
let _: () = msg_send![subview, release]; // releases subview (-1) = 1
Ok(Self{layer})
}
}

impl GraphicsContextImpl for CGImpl {
unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) {
let cls = class!(NSGraphicsContext);
let graphics_context: *mut Object = msg_send![cls, currentContext];
let context_ptr: *mut sys::CGContext = msg_send![graphics_context, CGContext];
let context = CGContext::from_existing_context_ptr(context_ptr);
let color_space = CGColorSpace::create_device_rgb();
let slice = std::slice::from_raw_parts(
let data = std::slice::from_raw_parts(
buffer.as_ptr() as *const u8,
buffer.len() * 4);
let data_provider = CGDataProvider::from_slice(slice);
buffer.len() * 4).to_vec();
let data_provider = CGDataProvider::from_buffer(Arc::new(data));
let image = CGImage::new(
width as usize,
height as usize,
Expand All @@ -54,11 +51,6 @@ impl GraphicsContextImpl for CGImpl {
false,
kCGRenderingIntentDefault,
);
let frame: CGRect = msg_send![self.view, frame];
// In Core Graphics, (0, 0) is bottom left, not top left
let origin = CGPoint { x: 0f64, y: frame.size.height };
let size = CGSize { width: width as f64, height: -(height as f64) };
let rect = CGRect { origin, size };
context.draw_image(rect, &image);
self.layer.set_contents(image.as_ptr() as id);
}
}

0 comments on commit dac3671

Please sign in to comment.