tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

lib.rs (5038B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 use std::os::raw::{c_void, c_char};
      6 
      7 /*
      8 
      9  This is a very simple (and unsafe!) rust wrapper for the DirectComposite / D3D11 / ANGLE
     10  implementation in lib.cpp.
     11 
     12  It just proxies the calls from the Compositor impl to the C99 code. This is very
     13  hacky and not suitable for production!
     14 
     15 */
     16 
     17 // Opaque wrapper for the Window type in lib.cpp
     18 #[repr(C)]
     19 pub struct Window {
     20    _unused: [u8; 0]
     21 }
     22 
     23 // C99 functions that do the compositor work
     24 extern "C" {
     25    fn com_dc_create_window(
     26        width: i32,
     27        height: i32,
     28        enable_compositor: bool,
     29        sync_mode: i32,
     30    ) -> *mut Window;
     31    fn com_dc_destroy_window(window: *mut Window);
     32    fn com_dc_tick(window: *mut Window) -> bool;
     33    fn com_dc_get_proc_address(name: *const c_char) -> *const c_void;
     34    fn com_dc_swap_buffers(window: *mut Window);
     35 
     36    fn com_dc_create_surface(
     37        window: *mut Window,
     38        id: u64,
     39        tile_width: i32,
     40        tile_height: i32,
     41        is_opaque: bool,
     42    );
     43 
     44    fn com_dc_create_tile(
     45        window: *mut Window,
     46        id: u64,
     47        x: i32,
     48        y: i32,
     49    );
     50 
     51    fn com_dc_destroy_tile(
     52        window: *mut Window,
     53        id: u64,
     54        x: i32,
     55        y: i32,
     56    );
     57 
     58    fn com_dc_destroy_surface(
     59        window: *mut Window,
     60        id: u64,
     61    );
     62 
     63    fn com_dc_bind_surface(
     64        window: *mut Window,
     65        surface_id: u64,
     66        tile_x: i32,
     67        tile_y: i32,
     68        x_offset: &mut i32,
     69        y_offset: &mut i32,
     70        dirty_x0: i32,
     71        dirty_y0: i32,
     72        dirty_width: i32,
     73        dirty_height: i32,
     74    ) -> u32;
     75    fn com_dc_unbind_surface(window: *mut Window);
     76 
     77    fn com_dc_begin_transaction(window: *mut Window);
     78 
     79    fn com_dc_add_surface(
     80        window: *mut Window,
     81        id: u64,
     82        x: i32,
     83        y: i32,
     84        clip_x: i32,
     85        clip_y: i32,
     86        clip_w: i32,
     87        clip_h: i32,
     88    );
     89 
     90    fn com_dc_end_transaction(window: *mut Window);
     91 }
     92 
     93 pub fn create_window(
     94    width: i32,
     95    height: i32,
     96    enable_compositor: bool,
     97    sync_mode: i32,
     98 ) -> *mut Window {
     99    unsafe {
    100        com_dc_create_window(width, height, enable_compositor, sync_mode)
    101    }
    102 }
    103 
    104 pub fn destroy_window(window: *mut Window) {
    105    unsafe {
    106        com_dc_destroy_window(window);
    107    }
    108 }
    109 
    110 pub fn tick(window: *mut Window) -> bool {
    111    unsafe {
    112        com_dc_tick(window)
    113    }
    114 }
    115 
    116 pub fn get_proc_address(name: *const c_char) -> *const c_void {
    117    unsafe {
    118        com_dc_get_proc_address(name)
    119    }
    120 }
    121 
    122 pub fn create_surface(
    123    window: *mut Window,
    124    id: u64,
    125    tile_width: i32,
    126    tile_height: i32,
    127    is_opaque: bool,
    128 ) {
    129    unsafe {
    130        com_dc_create_surface(
    131            window,
    132            id,
    133            tile_width,
    134            tile_height,
    135            is_opaque,
    136        )
    137    }
    138 }
    139 
    140 pub fn create_tile(
    141    window: *mut Window,
    142    id: u64,
    143    x: i32,
    144    y: i32,
    145 ) {
    146    unsafe {
    147        com_dc_create_tile(
    148            window,
    149            id,
    150            x,
    151            y,
    152        )
    153    }
    154 }
    155 
    156 pub fn destroy_tile(
    157    window: *mut Window,
    158    id: u64,
    159    x: i32,
    160    y: i32,
    161 ) {
    162    unsafe {
    163        com_dc_destroy_tile(
    164            window,
    165            id,
    166            x,
    167            y,
    168        )
    169    }
    170 }
    171 
    172 pub fn destroy_surface(
    173    window: *mut Window,
    174    id: u64,
    175 ) {
    176    unsafe {
    177        com_dc_destroy_surface(
    178            window,
    179            id,
    180        )
    181    }
    182 }
    183 
    184 pub fn bind_surface(
    185    window: *mut Window,
    186    surface_id: u64,
    187    tile_x: i32,
    188    tile_y: i32,
    189    dirty_x0: i32,
    190    dirty_y0: i32,
    191    dirty_width: i32,
    192    dirty_height: i32,
    193 ) -> (u32, i32, i32) {
    194    unsafe {
    195        let mut x_offset = 0;
    196        let mut y_offset = 0;
    197 
    198        let fbo_id = com_dc_bind_surface(
    199            window,
    200            surface_id,
    201            tile_x,
    202            tile_y,
    203            &mut x_offset,
    204            &mut y_offset,
    205            dirty_x0,
    206            dirty_y0,
    207            dirty_width,
    208            dirty_height,
    209        );
    210 
    211        (fbo_id, x_offset, y_offset)
    212    }
    213 }
    214 
    215 pub fn add_surface(
    216    window: *mut Window,
    217    id: u64,
    218    x: i32,
    219    y: i32,
    220    clip_x: i32,
    221    clip_y: i32,
    222    clip_w: i32,
    223    clip_h: i32,
    224 ) {
    225    unsafe {
    226        com_dc_add_surface(
    227            window,
    228            id,
    229            x,
    230            y,
    231            clip_x,
    232            clip_y,
    233            clip_w,
    234            clip_h,
    235        )
    236    }
    237 }
    238 
    239 pub fn begin_transaction(window: *mut Window) {
    240    unsafe {
    241        com_dc_begin_transaction(window)
    242    }
    243 }
    244 
    245 pub fn unbind_surface(window: *mut Window) {
    246    unsafe {
    247        com_dc_unbind_surface(window)
    248    }
    249 }
    250 
    251 pub fn end_transaction(window: *mut Window) {
    252    unsafe {
    253        com_dc_end_transaction(window)
    254    }
    255 }
    256 
    257 pub fn swap_buffers(window: *mut Window) {
    258    unsafe {
    259        com_dc_swap_buffers(window);
    260    }
    261 }
    262 
    263 pub fn deinit(_window: *mut Window) {
    264    todo!()
    265 }