<commit_before>// Copyright 2013 The Servo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Mac OS-specific implementation of cross-process surfaces. This uses `IOSurface`, introduced
//! in Mac OS X 10.6 Snow Leopard.

use core_foundation::boolean::CFBoolean;
use core_foundation::dictionary::CFDictionary;
use core_foundation::number::CFNumber;
use core_foundation::string::CFString;
use geom::size::Size2D;
use io_surface::{kIOSurfaceBytesPerElement, kIOSurfaceBytesPerRow, kIOSurfaceHeight};
use io_surface::{kIOSurfaceIsGlobal, kIOSurfaceWidth, IOSurface};
use io_surface;
use opengles::cgl::CGLPixelFormatObj;
use platform::surface::NativeSurfaceMethods;
use std::util;
use texturegl::Texture;

pub struct NativeGraphicsMetadata {
    pixel_format: CGLPixelFormatObj,
}

pub struct NativePaintingGraphicsContext {
    metadata: NativeGraphicsMetadata,
}

impl NativePaintingGraphicsContext {
    pub fn from_metadata(metadata: &NativeGraphicsMetadata) -> NativePaintingGraphicsContext {
        NativePaintingGraphicsContext {
            metadata: *metadata,
        }
    }
}

impl Drop for NativePaintingGraphicsContext {
    fn drop(&mut self) {}
}

pub struct NativeCompositingGraphicsContext {
    contents: (),
}

impl NativeCompositingGraphicsContext {
    pub fn new() -> NativeCompositingGraphicsContext {
        NativeCompositingGraphicsContext {
            contents: (),
        }
    }
}

pub struct NativeSurface {
    io_surface: Option<IOSurface>,
    will_leak: bool,
}

impl NativeSurface {
    #[fixed_stack_segment]
    pub fn from_io_surface(io_surface: IOSurface) -> NativeSurface {
        NativeSurface {
            io_surface: Some(io_surface),
            will_leak: true,
        }
    }
}

impl NativeSurfaceMethods for NativeSurface {
    fn new(_: &NativePaintingGraphicsContext, size: Size2D<i32>, stride: i32) -> NativeSurface {
        let width_key = CFString::wrap_shared(kIOSurfaceWidth);
        let width_value = CFNumber::new(size.width);

        let height_key = CFString::wrap_shared(kIOSurfaceHeight);
        let height_value = CFNumber::new(size.height);

        let bytes_per_row_key = CFString::wrap_shared(kIOSurfaceBytesPerRow);
        let bytes_per_row_value = CFNumber::new(stride);

        let bytes_per_elem_key = CFString::wrap_shared(kIOSurfaceBytesPerElement);
        let bytes_per_elem_value = CFNumber::new(4i32);

        let is_global_key = CFString::wrap_shared(kIOSurfaceIsGlobal);
        let is_global_value = CFBoolean::true_value();

        let surface = io_surface::new(&CFDictionary::new([
            (*width_key.contents.borrow_ref(), *width_value.contents.borrow_type_ref()),
            (*height_key.contents.borrow_ref(), *height_value.contents.borrow_type_ref()),
            (*bytes_per_row_key.contents.borrow_ref(),
             *bytes_per_row_value.contents.borrow_type_ref()),
            (*bytes_per_elem_key.contents.borrow_ref(),
             *bytes_per_elem_value.contents.borrow_type_ref()),
            (*is_global_key.contents.borrow_ref(), *is_global_value.contents.borrow_type_ref()),
        ]));

        NativeSurface {
            io_surface: Some(surface),
            will_leak: true,
        }
    }
<commit_msg>Update for Core Foundation bindings revamp<commit_after>// Copyright 2013 The Servo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Mac OS-specific implementation of cross-process surfaces. This uses `IOSurface`, introduced
//! in Mac OS X 10.6 Snow Leopard.

use core_foundation::base::TCFType;
use core_foundation::boolean::CFBoolean;
use core_foundation::dictionary::CFDictionary;
use core_foundation::number::CFNumber;
use core_foundation::string::CFString;
use geom::size::Size2D;
use io_surface::{kIOSurfaceBytesPerElement, kIOSurfaceBytesPerRow, kIOSurfaceHeight};
use io_surface::{kIOSurfaceIsGlobal, kIOSurfaceWidth, IOSurface};
use io_surface;
use opengles::cgl::CGLPixelFormatObj;
use platform::surface::NativeSurfaceMethods;
use std::util;
use texturegl::Texture;

pub struct NativeGraphicsMetadata {
    pixel_format: CGLPixelFormatObj,
}

pub struct NativePaintingGraphicsContext {
    metadata: NativeGraphicsMetadata,
}

impl NativePaintingGraphicsContext {
    pub fn from_metadata(metadata: &NativeGraphicsMetadata) -> NativePaintingGraphicsContext {
        NativePaintingGraphicsContext {
            metadata: *metadata,
        }
    }
}

impl Drop for NativePaintingGraphicsContext {
    fn drop(&mut self) {}
}

pub struct NativeCompositingGraphicsContext {
    contents: (),
}

impl NativeCompositingGraphicsContext {
    pub fn new() -> NativeCompositingGraphicsContext {
        NativeCompositingGraphicsContext {
            contents: (),
        }
    }
}

pub struct NativeSurface {
    io_surface: Option<IOSurface>,
    will_leak: bool,
}

impl NativeSurface {
    #[fixed_stack_segment]
    pub fn from_io_surface(io_surface: IOSurface) -> NativeSurface {
        NativeSurface {
            io_surface: Some(io_surface),
            will_leak: true,
        }
    }
}

impl NativeSurfaceMethods for NativeSurface {
    fn new(_: &NativePaintingGraphicsContext, size: Size2D<i32>, stride: i32) -> NativeSurface {
        unsafe {
            let width_key: CFString = TCFType::wrap_under_get_rule(kIOSurfaceWidth);
            let width_value: CFNumber = FromPrimitive::from_i32(size.width).unwrap();

            let height_key: CFString = TCFType::wrap_under_get_rule(kIOSurfaceHeight);
            let height_value: CFNumber = FromPrimitive::from_i32(size.height).unwrap();

            let bytes_per_row_key: CFString = TCFType::wrap_under_get_rule(kIOSurfaceBytesPerRow);
            let bytes_per_row_value: CFNumber = FromPrimitive::from_i32(stride).unwrap();

            let bytes_per_elem_key: CFString =
                TCFType::wrap_under_get_rule(kIOSurfaceBytesPerElement);
            let bytes_per_elem_value: CFNumber = FromPrimitive::from_i32(4).unwrap();

            let is_global_key: CFString = TCFType::wrap_under_get_rule(kIOSurfaceIsGlobal);
            let is_global_value = CFBoolean::true_value();

            let surface = io_surface::new(&CFDictionary::from_CFType_pairs([
                (width_key.as_CFType(), width_value.as_CFType()),
                (height_key.as_CFType(), height_value.as_CFType()),
                (bytes_per_row_key.as_CFType(), bytes_per_row_value.as_CFType()),
                (bytes_per_elem_key.as_CFType(), bytes_per_elem_value.as_CFType()),
                (is_global_key.as_CFType(), is_global_value.as_CFType()),
            ]));

            NativeSurface {
                io_surface: Some(surface),
                will_leak: true,
            }
        }
    }

{'fraction_non_alphanumeric': 0.10009468416069255, 'fraction_numerical': 0.006222101988367375, 'mean_word_length': 3.6444723618090453, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2455563', 'n_tokens_mistral': 2258, 'n_tokens_neox': 2175, 'n_words': 449}