Skip to content

Commit 0edd977

Browse files
author
bors-servo
authored
Auto merge of #268 - pcwalton:additions-0.17.3, r=jdm
CFArray, Core Animation, and Quartz Window Services additions Here's your regularly scheduled set of additions to the Core Foundation bindings. I needed these for the testing infrastructure I'm developing for https://github.com/pcwalton/planeshift. r? @jdm <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/268) <!-- Reviewable:end -->
2 parents b937267 + fd4736b commit 0edd977

File tree

5 files changed

+425
-4
lines changed

5 files changed

+425
-4
lines changed

cocoa/src/quartzcore.rs

Lines changed: 261 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,35 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10+
#![allow(non_upper_case_globals)]
11+
1012
use core_foundation::array::{CFArray, CFArrayRef};
11-
use core_foundation::base::{CFTypeRef, TCFType};
13+
use core_foundation::base::{CFType, CFTypeRef, TCFType};
1214
use core_foundation::date::CFTimeInterval;
1315
use core_foundation::dictionary::{CFDictionary, CFDictionaryRef};
1416
use core_foundation::string::{CFString, CFStringRef};
1517
use core_graphics::base::CGFloat;
1618
use core_graphics::color::{CGColor, SysCGColorRef};
19+
use core_graphics::color_space::CGColorSpace;
1720
use core_graphics::context::CGContext;
1821
use core_graphics::geometry::{CGAffineTransform, CGPoint, CGRect, CGSize};
1922
use core_graphics::path::{CGPath, SysCGPathRef};
2023
use foreign_types::ForeignType;
2124
use std::ops::Mul;
2225
use std::ptr;
2326

27+
use appkit::CGLContextObj;
2428
use base::{BOOL, id, nil, YES};
2529
use foundation::NSUInteger;
2630

31+
// CABase.h
32+
33+
pub fn current_media_time() -> CFTimeInterval {
34+
unsafe {
35+
CACurrentMediaTime()
36+
}
37+
}
38+
2739
// CALayer.h
2840

2941
pub struct CALayer(id);
@@ -1356,11 +1368,169 @@ bitflags! {
13561368
}
13571369
}
13581370

1371+
// CARenderer.h
1372+
1373+
pub struct CARenderer(id);
1374+
1375+
unsafe impl Send for CARenderer {}
1376+
unsafe impl Sync for CARenderer {}
1377+
1378+
impl Clone for CARenderer {
1379+
#[inline]
1380+
fn clone(&self) -> CARenderer {
1381+
unsafe {
1382+
CARenderer(msg_send![self.id(), retain])
1383+
}
1384+
}
1385+
}
1386+
1387+
impl Drop for CARenderer {
1388+
#[inline]
1389+
fn drop(&mut self) {
1390+
unsafe {
1391+
msg_send![self.id(), release]
1392+
}
1393+
}
1394+
}
1395+
1396+
impl CARenderer {
1397+
#[inline]
1398+
pub fn id(&self) -> id {
1399+
self.0
1400+
}
1401+
1402+
#[inline]
1403+
pub unsafe fn from_cgl_context(context: CGLContextObj, color_space: Option<CGColorSpace>)
1404+
-> CARenderer {
1405+
let mut pairs: Vec<(CFString, CFType)> = vec![];
1406+
if let Some(color_space) = color_space {
1407+
pairs.push((CFString::wrap_under_get_rule(kCARendererColorSpace),
1408+
CFType::wrap_under_get_rule(color_space.as_ptr() as *const _ as *const _)))
1409+
}
1410+
1411+
let options: CFDictionary<CFString, CFType> = CFDictionary::from_CFType_pairs(&pairs);
1412+
1413+
let renderer: id =
1414+
msg_send![class!(CARenderer), rendererWithCGLContext:context
1415+
options:options.as_CFTypeRef()];
1416+
debug_assert!(renderer != nil);
1417+
CARenderer(renderer)
1418+
}
1419+
1420+
#[inline]
1421+
pub unsafe fn from_metal_texture(metal_texture: id,
1422+
metal_command_queue: id,
1423+
color_space: Option<CGColorSpace>)
1424+
-> CARenderer {
1425+
let mut pairs: Vec<(CFString, CFType)> = vec![
1426+
(CFString::wrap_under_get_rule(kCARendererMetalCommandQueue),
1427+
CFType::wrap_under_get_rule(metal_command_queue as *const _ as *const _)),
1428+
];
1429+
if let Some(color_space) = color_space {
1430+
pairs.push((CFString::wrap_under_get_rule(kCARendererColorSpace),
1431+
CFType::wrap_under_get_rule(color_space.as_ptr() as *const _ as *const _)))
1432+
}
1433+
1434+
let options: CFDictionary<CFString, CFType> = CFDictionary::from_CFType_pairs(&pairs);
1435+
1436+
let renderer: id =
1437+
msg_send![class!(CARenderer), rendererWithMTLTexture:metal_texture
1438+
options:options.as_CFTypeRef()];
1439+
debug_assert!(renderer != nil);
1440+
CARenderer(renderer)
1441+
}
1442+
1443+
#[inline]
1444+
pub fn layer(&self) -> Option<CALayer> {
1445+
unsafe {
1446+
let layer: id = msg_send![self.id(), layer];
1447+
if layer.is_null() {
1448+
None
1449+
} else {
1450+
Some(CALayer(layer))
1451+
}
1452+
}
1453+
}
1454+
1455+
#[inline]
1456+
pub fn set_layer(&self, layer: Option<CALayer>) {
1457+
unsafe {
1458+
let layer = match layer {
1459+
Some(ref layer) => layer.id(),
1460+
None => nil,
1461+
};
1462+
msg_send![self.id(), setLayer:layer];
1463+
}
1464+
}
1465+
1466+
#[inline]
1467+
pub fn bounds(&self) -> CGRect {
1468+
unsafe {
1469+
msg_send![self.id(), bounds]
1470+
}
1471+
}
1472+
1473+
#[inline]
1474+
pub fn set_bounds(&self, bounds: CGRect) {
1475+
unsafe {
1476+
msg_send![self.id(), setBounds:bounds]
1477+
}
1478+
}
1479+
1480+
#[inline]
1481+
pub fn begin_frame_at(&self, time: CFTimeInterval, timestamp: Option<&CVTimeStamp>) {
1482+
unsafe {
1483+
msg_send![self.id(), beginFrameAtTime:time timeStamp:timestamp]
1484+
}
1485+
}
1486+
1487+
#[inline]
1488+
pub fn update_bounds(&self) -> CGRect {
1489+
unsafe {
1490+
msg_send![self.id(), updateBounds]
1491+
}
1492+
}
1493+
1494+
#[inline]
1495+
pub fn add_update_rect(&self, rect: CGRect) {
1496+
unsafe {
1497+
msg_send![self.id(), addUpdateRect:rect]
1498+
}
1499+
}
1500+
1501+
#[inline]
1502+
pub fn render(&self) {
1503+
unsafe {
1504+
msg_send![self.id(), render]
1505+
}
1506+
}
1507+
1508+
#[inline]
1509+
pub fn next_frame_time(&self) -> CFTimeInterval {
1510+
unsafe {
1511+
msg_send![self.id(), nextFrameTime]
1512+
}
1513+
}
1514+
1515+
#[inline]
1516+
pub fn end_frame(&self) {
1517+
unsafe {
1518+
msg_send![self.id(), endFrame]
1519+
}
1520+
}
1521+
1522+
#[inline]
1523+
pub unsafe fn set_destination(&self, metal_texture: id) {
1524+
msg_send![self.id(), setDestination:metal_texture]
1525+
}
1526+
}
1527+
13591528
// CATransaction.h
13601529

13611530
// You can't actually construct any `CATransaction` objects, so that class is
13621531
// really just a module.
13631532
pub mod transaction {
1533+
use block::{Block, ConcreteBlock, IntoConcreteBlock, RcBlock};
13641534
use core_foundation::date::CFTimeInterval;
13651535
use core_foundation::string::CFString;
13661536

@@ -1442,8 +1612,27 @@ pub mod transaction {
14421612
}
14431613
}
14441614

1445-
// Omitted: `completionBlock`; depends on blocks.
1446-
// Omitted: `setCompletionBlock:`; depends on blocks.
1615+
#[inline]
1616+
pub fn completion_block() -> Option<RcBlock<(), ()>> {
1617+
unsafe {
1618+
let completion_block: *mut Block<(), ()> =
1619+
msg_send![class!(CATransaction), completionBlock];
1620+
if completion_block.is_null() {
1621+
None
1622+
} else {
1623+
Some(RcBlock::new(completion_block))
1624+
}
1625+
}
1626+
}
1627+
1628+
#[inline]
1629+
pub fn set_completion_block<F>(block: ConcreteBlock<(), (), F>)
1630+
where F: 'static + IntoConcreteBlock<(), Ret = ()> {
1631+
unsafe {
1632+
let block = block.copy();
1633+
msg_send![class!(CATransaction), setCompletionBlock:&*block]
1634+
}
1635+
}
14471636

14481637
#[inline]
14491638
pub fn value_for_key(key: &str) -> id {
@@ -1581,6 +1770,11 @@ impl CATransform3D {
15811770

15821771
#[link(name = "QuartzCore", kind = "framework")]
15831772
extern {
1773+
static kCARendererColorSpace: CFStringRef;
1774+
static kCARendererMetalCommandQueue: CFStringRef;
1775+
1776+
fn CACurrentMediaTime() -> CFTimeInterval;
1777+
15841778
fn CATransform3DIsIdentity(t: CATransform3D) -> bool;
15851779
fn CATransform3DEqualToTransform(a: CATransform3D, b: CATransform3D) -> bool;
15861780
fn CATransform3DMakeTranslation(tx: CGFloat, ty: CGFloat, tz: CGFloat) -> CATransform3D;
@@ -1599,3 +1793,67 @@ extern {
15991793
fn CATransform3DIsAffine(t: CATransform3D) -> bool;
16001794
fn CATransform3DGetAffineTransform(t: CATransform3D) -> CGAffineTransform;
16011795
}
1796+
1797+
// Miscellaneous structures in other frameworks.
1798+
//
1799+
// These should be moved elsewhere eventually.
1800+
1801+
// CoreVideo/CVBase.h
1802+
1803+
#[repr(C)]
1804+
#[derive(Clone, Copy)]
1805+
pub struct CVTimeStamp {
1806+
pub version: u32,
1807+
pub videoTimeScale: i32,
1808+
pub videoTime: i64,
1809+
pub hostTime: u64,
1810+
pub rateScalar: f64,
1811+
pub videoRefreshPeriod: i64,
1812+
pub smpteTime: CVSMPTETime,
1813+
pub flags: u64,
1814+
pub reserved: u64,
1815+
}
1816+
1817+
pub type CVTimeStampFlags = u64;
1818+
1819+
pub const kCVTimeStampVideoTimeValid: CVTimeStampFlags = 1 << 0;
1820+
pub const kCVTimeStampHostTimeValid: CVTimeStampFlags = 1 << 1;
1821+
pub const kCVTimeStampSMPTETimeValid: CVTimeStampFlags = 1 << 2;
1822+
pub const kCVTimeStampVideoRefreshPeriodValid: CVTimeStampFlags = 1 << 3;
1823+
pub const kCVTimeStampRateScalarValid: CVTimeStampFlags = 1 << 4;
1824+
pub const kCVTimeStampTopField: CVTimeStampFlags = 1 << 16;
1825+
pub const kCVTimeStampBottomField: CVTimeStampFlags = 1 << 17;
1826+
pub const kCVTimeStampVideoHostTimeValid: CVTimeStampFlags =
1827+
kCVTimeStampVideoTimeValid | kCVTimeStampHostTimeValid;
1828+
pub const kCVTimeStampIsInterlaced: CVTimeStampFlags =
1829+
kCVTimeStampTopField | kCVTimeStampBottomField;
1830+
1831+
#[repr(C)]
1832+
#[derive(Clone, Copy)]
1833+
pub struct CVSMPTETime {
1834+
pub subframes: i16,
1835+
pub subframeDivisor: i16,
1836+
pub counter: u32,
1837+
pub time_type: u32,
1838+
pub flags: u32,
1839+
pub hours: i16,
1840+
pub minutes: i16,
1841+
pub seconds: i16,
1842+
pub frames: i16,
1843+
}
1844+
1845+
pub type CVSMPTETimeType = u32;
1846+
1847+
pub const kCVSMPTETimeType24: CVSMPTETimeType = 0;
1848+
pub const kCVSMPTETimeType25: CVSMPTETimeType = 1;
1849+
pub const kCVSMPTETimeType30Drop: CVSMPTETimeType = 2;
1850+
pub const kCVSMPTETimeType30: CVSMPTETimeType = 3;
1851+
pub const kCVSMPTETimeType2997: CVSMPTETimeType = 4;
1852+
pub const kCVSMPTETimeType2997Drop: CVSMPTETimeType = 5;
1853+
pub const kCVSMPTETimeType60: CVSMPTETimeType = 6;
1854+
pub const kCVSMPTETimeType5994: CVSMPTETimeType = 7;
1855+
1856+
pub type CVSMPTETimeFlags = u32;
1857+
1858+
pub const kCVSMPTETimeValid: CVSMPTETimeFlags = 1 << 0;
1859+
pub const kCVSMPTETimeRunning: CVSMPTETimeFlags = 1 << 1;

core-foundation/src/array.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use core_foundation_sys::base::{CFTypeRef, CFRelease, kCFAllocatorDefault};
1515
use std::mem;
1616
use std::marker::PhantomData;
1717
use std::os::raw::c_void;
18+
use std::ptr;
1819
use ConcreteCFType;
1920

2021
use base::{CFIndexConvertible, TCFType, CFRange};
@@ -61,6 +62,17 @@ impl_CFTypeDescription!(CFArray);
6162
unsafe impl ConcreteCFType for CFArray<*const c_void> {}
6263

6364
impl<T> CFArray<T> {
65+
/// Creates a new `CFArray` with the given elements, which must implement `Copy`.
66+
pub fn from_copyable(elems: &[T]) -> CFArray<T> where T: Copy {
67+
unsafe {
68+
let array_ref = CFArrayCreate(kCFAllocatorDefault,
69+
mem::transmute(elems.as_ptr()),
70+
elems.len().to_CFIndex(),
71+
ptr::null());
72+
TCFType::wrap_under_create_rule(array_ref)
73+
}
74+
}
75+
6476
/// Creates a new `CFArray` with the given elements, which must be `CFType` objects.
6577
pub fn from_CFTypes(elems: &[T]) -> CFArray<T> where T: TCFType {
6678
unsafe {

core-graphics/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "core-graphics"
33
description = "Bindings to Core Graphics for OS X"
44
homepage = "https://github.com/servo/core-graphics-rs"
55
repository = "https://github.com/servo/core-foundation-rs"
6-
version = "0.17.2"
6+
version = "0.17.3"
77
authors = ["The Servo Project Developers"]
88
license = "MIT / Apache-2.0"
99

core-graphics/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub mod event_source;
3333
pub mod font;
3434
pub mod geometry;
3535
#[cfg(target_os = "macos")]
36+
pub mod window;
37+
#[cfg(target_os = "macos")]
3638
pub mod private;
3739
pub mod image;
3840
pub mod path;

0 commit comments

Comments
 (0)