| 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 |
#![crate_name = "io_surface"] |
| 11 |
#![crate_type = "rlib"] |
| 12 |
|
| 13 |
extern crate libc; |
| 14 |
extern crate core_foundation; |
| 15 |
extern crate cgl; |
| 16 |
extern crate leaky_cow; |
| 17 |
|
| 18 |
// Rust bindings to the IOSurface framework on macOS. |
| 19 |
|
| 20 |
use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, CFType, TCFType}; |
| 21 |
use core_foundation::dictionary::{CFDictionary, CFDictionaryRef}; |
| 22 |
use core_foundation::string::{CFString, CFStringRef}; |
| 23 |
use cgl::{kCGLNoError, CGLGetCurrentContext, CGLTexImageIOSurface2D, CGLErrorString, GLenum}; |
| 24 |
use libc::{c_int, size_t}; |
| 25 |
use std::os::raw::c_void; |
| 26 |
use leaky_cow::LeakyCow; |
| 27 |
use std::slice; |
| 28 |
use std::ffi::CStr; |
| 29 |
|
| 30 |
const BGRA: GLenum = 0x80E1; |
| 31 |
const RGBA: GLenum = 0x1908; |
| 32 |
const RGB: GLenum = 0x1907; |
| 33 |
const TEXTURE_RECTANGLE_ARB: GLenum = 0x84F5; |
| 34 |
const UNSIGNED_INT_8_8_8_8_REV: GLenum = 0x8367; |
| 35 |
|
| 36 |
//static kIOSurfaceLockReadOnly: u32 = 0x1; |
| 37 |
//static kIOSurfaceLockAvoidSync: u32 = 0x2; |
| 38 |
|
| 39 |
type IOReturn = c_int; |
| 40 |
|
| 41 |
#[repr(C)] |
| 42 |
pub struct __IOSurface(c_void); |
| 43 |
|
| 44 |
pub type IOSurfaceRef = *const __IOSurface; |
| 45 |
|
| 46 |
pub struct IOSurface { |
| 47 |
pub obj: IOSurfaceRef, |
| 48 |
} |
| 49 |
|
| 50 |
impl Drop for IOSurface { |
| 51 |
fn drop(&mut self) { |
| 52 |
unsafe { |
| 53 |
CFRelease(self.as_CFTypeRef()) |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
pub type IOSurfaceID = u32; |
| 59 |
|
| 60 |
impl Clone for IOSurface { |
| 61 |
#[inline] |
| 62 |
fn clone(&self) -> IOSurface { |
| 63 |
unsafe { |
| 64 |
TCFType::wrap_under_get_rule(self.obj) |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
impl TCFType for IOSurface { |
| 70 |
type Ref = IOSurfaceRef; |
| 71 |
|
| 72 |
#[inline] |
| 73 |
fn as_concrete_TypeRef(&self) -> IOSurfaceRef { |
| 74 |
self.obj |
| 75 |
} |
| 76 |
|
| 77 |
#[inline] |
| 78 |
unsafe fn wrap_under_create_rule(obj: IOSurfaceRef) -> IOSurface { |
| 79 |
assert!(!obj.is_null(), "Attempted to create a NULL object."); |
| 80 |
IOSurface { |
| 81 |
obj: obj, |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
#[inline] |
| 86 |
fn type_id() -> CFTypeID { |
| 87 |
unsafe { |
| 88 |
IOSurfaceGetTypeID() |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
#[inline] |
| 93 |
fn as_CFTypeRef(&self) -> CFTypeRef { |
| 94 |
self.as_concrete_TypeRef() as CFTypeRef |
| 95 |
} |
| 96 |
|
| 97 |
#[inline] |
| 98 |
unsafe fn wrap_under_get_rule(reference: IOSurfaceRef) -> IOSurface { |
| 99 |
assert!(!reference.is_null(), "Attempted to create a NULL object."); |
| 100 |
let reference = CFRetain(reference as *const c_void) as IOSurfaceRef; |
| 101 |
TCFType::wrap_under_create_rule(reference) |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
pub fn new(properties: &CFDictionary<CFString, CFType>) -> IOSurface { |
| 106 |
unsafe { |
| 107 |
TCFType::wrap_under_create_rule(IOSurfaceCreate(properties.as_concrete_TypeRef())) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/// Looks up an `IOSurface` by its global ID. |
| 112 |
/// |
| 113 |
/// FIXME(pcwalton): This should return an `Option`. |
| 114 |
pub fn lookup(csid: IOSurfaceID) -> IOSurface { |
| 115 |
unsafe { |
| 116 |
TCFType::wrap_under_create_rule(IOSurfaceLookup(csid)) |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
impl IOSurface { |
| 121 |
pub fn get_id(&self) -> IOSurfaceID { |
| 122 |
unsafe { |
| 123 |
IOSurfaceGetID(self.as_concrete_TypeRef()) |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/// Binds to the current GL texture. |
| 128 |
pub fn bind_to_gl_texture(&self, width: i32, height: i32, has_alpha: bool) { |
| 129 |
unsafe { |
| 130 |
let context = CGLGetCurrentContext(); |
| 131 |
let gl_error = CGLTexImageIOSurface2D(context, |
| 132 |
TEXTURE_RECTANGLE_ARB, |
| 133 |
if has_alpha { RGBA as GLenum } else { RGB as GLenum }, |
| 134 |
width, |
| 135 |
height, |
| 136 |
BGRA as GLenum, |
| 137 |
UNSIGNED_INT_8_8_8_8_REV, |
| 138 |
self.as_concrete_TypeRef() as *mut libc::c_void, |
| 139 |
0); |
| 140 |
|
| 141 |
if gl_error != kCGLNoError { |
| 142 |
let error_msg = CStr::from_ptr(CGLErrorString(gl_error)); |
| 143 |
let error_msg = error_msg.to_string_lossy(); |
| 144 |
// This will only actually leak memory if error_msg is a `Cow::Owned`, which |
| 145 |
// will only happen if the platform gives us invalid unicode. |
| 146 |
panic!(error_msg.leak()); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
pub fn upload(&self, data: &[u8]) { |
| 152 |
unsafe { |
| 153 |
let surface = self.as_concrete_TypeRef(); |
| 154 |
let mut seed = 0; |
| 155 |
|
| 156 |
IOSurfaceLock(surface, 0, &mut seed); |
| 157 |
|
| 158 |
let height = IOSurfaceGetHeight(surface); |
| 159 |
let stride = IOSurfaceGetBytesPerRow(surface); |
| 160 |
let size = (height * stride) as usize; |
| 161 |
let address = IOSurfaceGetBaseAddress(surface) as *mut u8; |
| 162 |
let dest: &mut [u8] = slice::from_raw_parts_mut(address, size); |
| 163 |
dest.clone_from_slice(data); |
| 164 |
|
| 165 |
// FIXME(pcwalton): RAII |
| 166 |
IOSurfaceUnlock(surface, 0, &mut seed); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
#[link(name = "IOSurface", kind = "framework")] |
| 172 |
extern { |
| 173 |
pub static kIOSurfaceAllocSize: CFStringRef; |
| 174 |
pub static kIOSurfaceWidth: CFStringRef; |
| 175 |
pub static kIOSurfaceHeight: CFStringRef; |
| 176 |
pub static kIOSurfaceBytesPerRow: CFStringRef; |
| 177 |
pub static kIOSurfaceBytesPerElement: CFStringRef; |
| 178 |
pub static kIOSurfaceElementWidth: CFStringRef; |
| 179 |
pub static kIOSurfaceElementHeight: CFStringRef; |
| 180 |
pub static kIOSurfaceOffset: CFStringRef; |
| 181 |
|
| 182 |
pub static kIOSurfacePlaneInfo: CFStringRef; |
| 183 |
pub static kIOSurfacePlaneWidth: CFStringRef; |
| 184 |
pub static kIOSurfacePlaneHeight: CFStringRef; |
| 185 |
pub static kIOSurfacePlaneBytesPerRow: CFStringRef; |
| 186 |
pub static kIOSurfacePlaneOffset: CFStringRef; |
| 187 |
pub static kIOSurfacePlaneSize: CFStringRef; |
| 188 |
|
| 189 |
pub static kIOSurfacePlaneBase: CFStringRef; |
| 190 |
pub static kIOSurfacePlaneBytesPerElement: CFStringRef; |
| 191 |
pub static kIOSurfacePlaneElementWidth: CFStringRef; |
| 192 |
pub static kIOSurfacePlaneElementHeight: CFStringRef; |
| 193 |
|
| 194 |
pub static kIOSurfaceCacheMode: CFStringRef; |
| 195 |
pub static kIOSurfaceIsGlobal: CFStringRef; |
| 196 |
pub static kIOSurfacePixelFormat: CFStringRef; |
| 197 |
|
| 198 |
fn IOSurfaceCreate(properties: CFDictionaryRef) -> IOSurfaceRef; |
| 199 |
fn IOSurfaceLookup(csid: IOSurfaceID) -> IOSurfaceRef; |
| 200 |
fn IOSurfaceGetID(buffer: IOSurfaceRef) -> IOSurfaceID; |
| 201 |
|
| 202 |
fn IOSurfaceGetTypeID() -> CFTypeID; |
| 203 |
|
| 204 |
fn IOSurfaceLock(buffer: IOSurfaceRef, options: u32, seed: *mut u32) -> IOReturn; |
| 205 |
fn IOSurfaceUnlock(buffer: IOSurfaceRef, options: u32, seed: *mut u32) -> IOReturn; |
| 206 |
|
| 207 |
fn IOSurfaceGetHeight(buffer: IOSurfaceRef) -> size_t; |
| 208 |
fn IOSurfaceGetBytesPerRow(buffer: IOSurfaceRef) -> size_t; |
| 209 |
fn IOSurfaceGetBaseAddress(buffer: IOSurfaceRef) -> *mut c_void; |
| 210 |
} |