| 1 |
// Copyright 2013 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 |
use core_foundation::base::{CFRelease, CFRetain, CFTypeID, TCFType}; |
| 11 |
use core_foundation::data::{CFData, CFDataRef}; |
| 12 |
|
| 13 |
use libc::{size_t, off_t}; |
| 14 |
use std::mem; |
| 15 |
use std::ptr; |
| 16 |
use std::sync::Arc; |
| 17 |
use std::os::raw::c_void; |
| 18 |
|
| 19 |
use foreign_types::{ForeignType, ForeignTypeRef}; |
| 20 |
|
| 21 |
pub type CGDataProviderGetBytesCallback = Option<unsafe extern fn (*mut c_void, *mut c_void, size_t) -> size_t>; |
| 22 |
pub type CGDataProviderReleaseInfoCallback = Option<unsafe extern fn (*mut c_void)>; |
| 23 |
pub type CGDataProviderRewindCallback = Option<unsafe extern fn (*mut c_void)>; |
| 24 |
pub type CGDataProviderSkipBytesCallback = Option<unsafe extern fn (*mut c_void, size_t)>; |
| 25 |
pub type CGDataProviderSkipForwardCallback = Option<unsafe extern fn (*mut c_void, off_t) -> off_t>; |
| 26 |
|
| 27 |
pub type CGDataProviderGetBytePointerCallback = Option<unsafe extern fn (*mut c_void) -> *mut c_void>; |
| 28 |
pub type CGDataProviderGetBytesAtOffsetCallback = Option<unsafe extern fn (*mut c_void, *mut c_void, size_t, size_t)>; |
| 29 |
pub type CGDataProviderReleaseBytePointerCallback = Option<unsafe extern fn (*mut c_void, *const c_void)>; |
| 30 |
pub type CGDataProviderReleaseDataCallback = Option<unsafe extern fn (*mut c_void, *const c_void, size_t)>; |
| 31 |
pub type CGDataProviderGetBytesAtPositionCallback = Option<unsafe extern fn (*mut c_void, *mut c_void, off_t, size_t)>; |
| 32 |
|
| 33 |
foreign_type! { |
| 34 |
#[doc(hidden)] |
| 35 |
pub unsafe type CGDataProvider { |
| 36 |
type CType = ::sys::CGDataProvider; |
| 37 |
fn drop = |cs| CFRelease(cs as *mut _); |
| 38 |
fn clone = |p| CFRetain(p as *const _) as *mut _; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
impl CGDataProvider { |
| 43 |
pub fn type_id() -> CFTypeID { |
| 44 |
unsafe { |
| 45 |
CGDataProviderGetTypeID() |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/// Creates a data provider from the given reference-counted buffer. |
| 50 |
/// |
| 51 |
/// The `CGDataProvider` object takes ownership of the reference. Once the data provider |
| 52 |
/// is destroyed, the reference count of the buffer is automatically decremented. |
| 53 |
pub fn from_buffer<T: AsRef<[u8]> + Sync + Send>(buffer: Arc<T>) -> Self { |
| 54 |
unsafe { |
| 55 |
let ptr = (*buffer).as_ref().as_ptr() as *const c_void; |
| 56 |
let len = (*buffer).as_ref().len() as size_t; |
| 57 |
let info = Arc::into_raw(buffer) as *mut c_void; |
| 58 |
let result = CGDataProviderCreateWithData(info, ptr, len, Some(release::<T>)); |
| 59 |
return CGDataProvider::from_ptr(result); |
| 60 |
} |
| 61 |
|
| 62 |
unsafe extern "C" fn release<T>(info: *mut c_void, _: *const c_void, _: size_t) { |
| 63 |
drop(Arc::from_raw(info as *mut T)) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/// Creates a data prvider from a given slice. The data provider does not own the slice in this |
| 68 |
/// case, so it's up to the user to ensure the memory safety here. |
| 69 |
pub unsafe fn from_slice(buffer: &[u8]) -> Self { |
| 70 |
let ptr = buffer.as_ptr() as *const c_void; |
| 71 |
let len = buffer.len() as size_t; |
| 72 |
let result = CGDataProviderCreateWithData(ptr::null_mut(), ptr, len, None); |
| 73 |
CGDataProvider::from_ptr(result) |
| 74 |
} |
| 75 |
|
| 76 |
/// Creates a data provider from the given raw pointer, length, and destructor function. |
| 77 |
/// |
| 78 |
/// This is double-boxed because the Core Text API requires that the userdata be a single |
| 79 |
/// pointer. |
| 80 |
pub unsafe fn from_custom_data(custom_data: Box<Box<dyn CustomData>>) -> Self { |
| 81 |
let (ptr, len) = (custom_data.ptr() as *const c_void, custom_data.len()); |
| 82 |
let userdata = mem::transmute::<Box<Box<dyn CustomData>>, &mut c_void>(custom_data); |
| 83 |
let data_provider = CGDataProviderCreateWithData(userdata, ptr, len, Some(release)); |
| 84 |
return CGDataProvider::from_ptr(data_provider); |
| 85 |
|
| 86 |
unsafe extern "C" fn release(info: *mut c_void, _: *const c_void, _: size_t) { |
| 87 |
drop(mem::transmute::<*mut c_void, Box<Box<dyn CustomData>>>(info)) |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
impl CGDataProviderRef { |
| 93 |
/// Creates a copy of the data from the underlying `CFDataProviderRef`. |
| 94 |
pub fn copy_data(&self) -> CFData { |
| 95 |
unsafe { CFData::wrap_under_create_rule(CGDataProviderCopyData(self.as_ptr())) } |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/// Encapsulates custom data that can be wrapped. |
| 100 |
pub trait CustomData { |
| 101 |
/// Returns a pointer to the start of the custom data. This pointer *must not change* during |
| 102 |
/// the lifespan of this CustomData. |
| 103 |
unsafe fn ptr(&self) -> *const u8; |
| 104 |
/// Returns the length of this custom data. This value must not change during the lifespan of |
| 105 |
/// this CustomData. |
| 106 |
unsafe fn len(&self) -> usize; |
| 107 |
} |
| 108 |
|
| 109 |
#[test] |
| 110 |
fn test_data_provider() { |
| 111 |
let l = vec![5]; |
| 112 |
CGDataProvider::from_buffer(Arc::new(l)); |
| 113 |
|
| 114 |
let l = vec![5]; |
| 115 |
CGDataProvider::from_buffer(Arc::new(l.into_boxed_slice())); |
| 116 |
|
| 117 |
// Make sure the buffer is actually dropped |
| 118 |
use std::sync::atomic::{AtomicBool, Ordering::SeqCst}; |
| 119 |
struct VecWrapper { |
| 120 |
inner: Vec<u8>, |
| 121 |
dropped: Arc<AtomicBool>, |
| 122 |
} |
| 123 |
|
| 124 |
impl Drop for VecWrapper { |
| 125 |
fn drop(&mut self) { |
| 126 |
self.dropped.store(true, SeqCst) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
impl std::convert::AsRef<[u8]> for VecWrapper { |
| 131 |
fn as_ref(&self) -> &[u8] { |
| 132 |
&self.inner |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
let dropped = Arc::new(AtomicBool::default()); |
| 137 |
let l = Arc::new(VecWrapper {inner: vec![5], dropped: dropped.clone() }); |
| 138 |
let m = l.clone(); |
| 139 |
let dp = CGDataProvider::from_buffer(l); |
| 140 |
drop(m); |
| 141 |
assert!(!dropped.load(SeqCst)); |
| 142 |
drop(dp); |
| 143 |
assert!(dropped.load(SeqCst)) |
| 144 |
} |
| 145 |
|
| 146 |
#[link(name = "CoreGraphics", kind = "framework")] |
| 147 |
extern { |
| 148 |
fn CGDataProviderCopyData(provider: ::sys::CGDataProviderRef) -> CFDataRef; |
| 149 |
//fn CGDataProviderCreateDirect |
| 150 |
//fn CGDataProviderCreateSequential |
| 151 |
//fn CGDataProviderCreateWithCFData |
| 152 |
fn CGDataProviderCreateWithData(info: *mut c_void, |
| 153 |
data: *const c_void, |
| 154 |
size: size_t, |
| 155 |
releaseData: CGDataProviderReleaseDataCallback |
| 156 |
) -> ::sys::CGDataProviderRef; |
| 157 |
//fn CGDataProviderCreateWithFilename(filename: *c_char) -> CGDataProviderRef; |
| 158 |
//fn CGDataProviderCreateWithURL |
| 159 |
fn CGDataProviderGetTypeID() -> CFTypeID; |
| 160 |
//fn CGDataProviderRelease(provider: CGDataProviderRef); |
| 161 |
//fn CGDataProviderRetain(provider: CGDataProviderRef) -> CGDataProviderRef; |
| 162 |
} |