core_graphics/
private.rs

1// Copyright 2016 The Servo Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10//! Evil private APIs.
11//!
12//! These are liable to change at any time. Use with caution!
13
14use crate::geometry::CGRect;
15use libc::{c_int, c_uint};
16use std::ptr;
17
18pub struct CGSRegion {
19    region: ffi::CGSRegionRef,
20}
21
22impl Drop for CGSRegion {
23    fn drop(&mut self) {
24        unsafe { ffi::CGSRegionRelease(self.region) }
25    }
26}
27
28impl CGSRegion {
29    #[inline]
30    pub fn from_rect(rect: &CGRect) -> CGSRegion {
31        unsafe {
32            let mut region = ptr::null_mut();
33            assert!(ffi::CGSNewRegionWithRect(rect, &mut region) == 0);
34            CGSRegion { region }
35        }
36    }
37
38    #[inline]
39    pub fn from_rects(rects: &[CGRect]) -> CGSRegion {
40        unsafe {
41            let mut region = ptr::null_mut();
42            assert!(
43                ffi::CGSNewRegionWithRectList(rects.as_ptr(), rects.len() as c_uint, &mut region)
44                    == 0
45            );
46            CGSRegion { region }
47        }
48    }
49}
50
51/// This should always be memory-safe; the window server rejects any invalid surface IDs.
52pub struct CGSSurface {
53    context_id: c_uint,
54    window_number: c_int,
55    surface_id: c_uint,
56}
57
58impl CGSSurface {
59    #[inline]
60    pub fn from_ids(context_id: c_uint, window_number: c_int, surface_id: c_uint) -> CGSSurface {
61        CGSSurface {
62            context_id,
63            window_number,
64            surface_id,
65        }
66    }
67
68    #[inline]
69    pub fn id(&self) -> c_uint {
70        self.surface_id
71    }
72
73    #[inline]
74    pub fn set_shape(&self, region: &CGSRegion) {
75        unsafe {
76            assert!(
77                ffi::CGSSetSurfaceShape(
78                    self.context_id,
79                    self.window_number,
80                    self.surface_id,
81                    region.region
82                ) == 0
83            )
84        }
85    }
86}
87
88mod ffi {
89    use crate::geometry::CGRect;
90    use libc::{c_int, c_uint};
91
92    // This is an enum so that we can't easily make instances of this opaque type.
93    pub enum CGSRegionObject {}
94
95    pub type CGError = OSStatus;
96    pub type CGSRegionRef = *mut CGSRegionObject;
97    pub type OSStatus = i32;
98
99    #[cfg_attr(feature = "link", link(name = "CoreGraphics", kind = "framework"))]
100    extern "C" {
101        pub fn CGSRegionRelease(region: CGSRegionRef);
102        pub fn CGSNewRegionWithRect(rect: *const CGRect, outRegion: *mut CGSRegionRef) -> CGError;
103        pub fn CGSNewRegionWithRectList(
104            rects: *const CGRect,
105            rectCount: c_uint,
106            outRegion: *mut CGSRegionRef,
107        ) -> CGError;
108
109        pub fn CGSSetSurfaceShape(
110            contextID: c_uint,
111            windowNumber: c_int,
112            surfaceID: c_uint,
113            region: CGSRegionRef,
114        ) -> CGError;
115    }
116}