From 142d8c0337437e119e831511213607d56ed7f530 Mon Sep 17 00:00:00 2001 From: "E. Dunham" Date: Fri, 8 Sep 2017 10:52:25 -0700 Subject: [PATCH] rustfmt --- src/layers.rs | 91 ++--- src/lib.rs | 20 +- src/platform/android/surface.rs | 56 +-- src/platform/egl/surface.rs | 51 +-- src/platform/linux/surface.rs | 170 ++++++---- src/platform/macos/surface.rs | 48 +-- src/platform/surface.rs | 93 +++-- src/platform/windows/surface.rs | 3 +- src/rendergl.rs | 585 +++++++++++++++++++------------- src/scene.rs | 53 +-- src/texturegl.rs | 51 ++- src/tiling.rs | 195 ++++++----- src/util.rs | 54 +-- 13 files changed, 850 insertions(+), 620 deletions(-) mode change 100755 => 100644 src/lib.rs mode change 100755 => 100644 src/rendergl.rs mode change 100755 => 100644 src/scene.rs diff --git a/src/layers.rs b/src/layers.rs index 76e7f4d..5ac9c5c 100644 --- a/src/layers.rs +++ b/src/layers.rs @@ -31,9 +31,7 @@ known_heap_size!(0, ContentAge); impl ContentAge { pub fn new() -> ContentAge { - ContentAge { - age: 0, - } + ContentAge { age: 0 } } pub fn next(&mut self) { @@ -103,15 +101,16 @@ pub struct Layer { } impl Layer { - pub fn new(bounds: TypedRect, - tile_size: usize, - background_color: Color, - opacity: f32, - establishes_3d_context: bool, - data: T) - -> Layer { + pub fn new( + bounds: TypedRect, + tile_size: usize, + background_color: Color, + opacity: f32, + establishes_3d_context: bool, + data: T, + ) -> Layer { Layer { - children: RefCell::new(vec!()), + children: RefCell::new(vec![]), transform: RefCell::new(Matrix4D::identity()), perspective: RefCell::new(Matrix4D::identity()), bounds: RefCell::new(bounds), @@ -142,19 +141,21 @@ impl Layer { /// Returns buffer requests inside the given dirty rect, and simultaneously throws out tiles /// outside the given viewport rect. - pub fn get_buffer_requests(&self, - rect_in_layer: TypedRect, - viewport_in_layer: TypedRect, - scale: ScaleFactor) - -> Vec { + pub fn get_buffer_requests( + &self, + rect_in_layer: TypedRect, + viewport_in_layer: TypedRect, + scale: ScaleFactor, + ) -> Vec { let mut tile_grid = self.tile_grid.borrow_mut(); - tile_grid.get_buffer_requests_in_rect(rect_in_layer * scale, - viewport_in_layer * scale, - self.bounds.borrow().size * scale, - &(self.transform_state.borrow().world_rect.origin * - scale.get()), - &self.transform_state.borrow().final_transform, - *self.content_age.borrow()) + tile_grid.get_buffer_requests_in_rect( + rect_in_layer * scale, + viewport_in_layer * scale, + self.bounds.borrow().size * scale, + &(self.transform_state.borrow().world_rect.origin * scale.get()), + &self.transform_state.borrow().final_transform, + *self.content_age.borrow(), + ) } pub fn resize(&self, new_size: TypedSize2D) { @@ -185,14 +186,14 @@ impl Layer { self.tile_grid.borrow().do_for_all_tiles(f); } - pub fn update_transform_state(&self, - parent_transform: &Matrix4D, - parent_perspective: &Matrix4D, - parent_origin: &Point2D) { + pub fn update_transform_state( + &self, + parent_transform: &Matrix4D, + parent_perspective: &Matrix4D, + parent_origin: &Point2D, + ) { let mut ts = self.transform_state.borrow_mut(); - let rect_without_scroll = self.bounds.borrow() - .to_untyped() - .translate(parent_origin); + let rect_without_scroll = self.bounds.borrow().to_untyped().translate(parent_origin); ts.world_rect = rect_without_scroll.translate(&self.content_offset.borrow().to_untyped()); @@ -205,9 +206,9 @@ impl Layer { .pre_mul(&*self.transform.borrow()) .pre_translated(-x0, -y0, 0.0); - ts.final_transform = parent_perspective - .pre_mul(&local_transform) - .pre_mul(&parent_transform); + ts.final_transform = parent_perspective.pre_mul(&local_transform).pre_mul( + &parent_transform, + ); ts.screen_rect = project_rect_to_screen(&ts.world_rect, &ts.final_transform); // TODO(gw): This is quite bogus. It's a hack to allow the paint task @@ -224,18 +225,21 @@ impl Layer { .pre_translated(-x0, -y0, 0.0); for child in self.children().iter() { - child.update_transform_state(&ts.final_transform, - &perspective_transform, - &rect_without_scroll.origin); + child.update_transform_state( + &ts.final_transform, + &perspective_transform, + &rect_without_scroll.origin, + ); } } /// Calculate the amount of memory used by this layer and all its children. /// The memory may be allocated on the heap or in GPU memory. pub fn get_memory_usage(&self) -> usize { - let size_of_children : usize = self.children().iter().map(|ref child| -> usize { - child.get_memory_usage() - }).sum(); + let size_of_children: usize = self.children() + .iter() + .map(|ref child| -> usize { child.get_memory_usage() }) + .sum(); size_of_children + self.tile_grid.borrow().get_memory_usage() } } @@ -256,8 +260,11 @@ pub struct BufferRequest { } impl BufferRequest { - pub fn new(screen_rect: Rect, page_rect: Rect, content_age: ContentAge) - -> BufferRequest { + pub fn new( + screen_rect: Rect, + page_rect: Rect, + content_age: ContentAge, + ) -> BufferRequest { BufferRequest { screen_rect: screen_rect, page_rect: page_rect, @@ -320,7 +327,7 @@ impl LayerBuffer { /// A set of layer buffers. This is an atomic unit used to switch between the front and back /// buffers. pub struct LayerBufferSet { - pub buffers: Vec> + pub buffers: Vec>, } impl LayerBufferSet { diff --git a/src/lib.rs b/src/lib.rs old mode 100755 new mode 100644 index 92e097a..9866218 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,16 +21,16 @@ extern crate rustc_serialize; extern crate gleam; extern crate skia; -#[cfg(target_os="macos")] +#[cfg(target_os = "macos")] extern crate core_foundation; -#[cfg(target_os="macos")] +#[cfg(target_os = "macos")] extern crate io_surface; -#[cfg(target_os="macos")] +#[cfg(target_os = "macos")] extern crate cgl; -#[cfg(target_os="linux")] +#[cfg(target_os = "linux")] extern crate x11; -#[cfg(target_os="linux")] +#[cfg(target_os = "linux")] extern crate glx; #[cfg(any(target_os = "linux", target_os = "android"))] @@ -46,23 +46,23 @@ pub mod tiling; pub mod util; pub mod platform { - #[cfg(target_os="linux")] + #[cfg(target_os = "linux")] pub mod linux { pub mod surface; } - #[cfg(target_os="macos")] + #[cfg(target_os = "macos")] pub mod macos { pub mod surface; } - #[cfg(target_os="android")] + #[cfg(target_os = "android")] pub mod android { pub mod surface; } - #[cfg(any(target_os="android",target_os="linux"))] + #[cfg(any(target_os = "android", target_os = "linux"))] pub mod egl { pub mod surface; } - #[cfg(target_os="windows")] + #[cfg(target_os = "windows")] pub mod windows { pub mod surface; } diff --git a/src/platform/android/surface.rs b/src/platform/android/surface.rs index 93090f2..fb554a6 100644 --- a/src/platform/android/surface.rs +++ b/src/platform/android/surface.rs @@ -27,7 +27,6 @@ use std::vec::Vec; /// buffer. EGLImageKHR is used to GPU rendering and vector is used to CPU rendering. EGL /// extension seems not provide simple way to accessing its bitmap directly. In the /// future, we need to find out the way to integrate them. - #[derive(Clone, Copy)] pub struct NativeDisplay { pub display: EGLDisplay, @@ -40,15 +39,11 @@ impl NativeDisplay { } pub fn new_with_display(display: EGLDisplay) -> NativeDisplay { - NativeDisplay { - display: display, - } + NativeDisplay { display: display } } pub fn platform_display_data(&self) -> PlatformDisplayData { - PlatformDisplayData { - display: self.display, - } + PlatformDisplayData { display: self.display } } } @@ -85,25 +80,29 @@ impl EGLImageNativeSurface { pub fn bind_to_texture(&self, _: &NativeDisplay, texture: &Texture) { let _bound = texture.bind(); match self.image { - None => match self.bitmap { - Some(ref bitmap) => { - let data = bitmap.as_ptr() as *const c_void; - unsafe { - TexImage2D(TEXTURE_2D, - 0, - BGRA_EXT as i32, - self.size.width as i32, - self.size.height as i32, - 0, - BGRA_EXT as u32, - UNSIGNED_BYTE, - data); + None => { + match self.bitmap { + Some(ref bitmap) => { + let data = bitmap.as_ptr() as *const c_void; + unsafe { + TexImage2D( + TEXTURE_2D, + 0, + BGRA_EXT as i32, + self.size.width as i32, + self.size.height as i32, + 0, + BGRA_EXT as u32, + UNSIGNED_BYTE, + data, + ); + } + } + None => { + debug!("Cannot bind the buffer(CPU rendering), there is no bitmap"); } } - None => { - debug!("Cannot bind the buffer(CPU rendering), there is no bitmap"); - } - }, + } Some(image_khr) => { egl_image_target_texture2d_oes(TEXTURE_2D, image_khr as *const c_void); } @@ -132,7 +131,7 @@ impl EGLImageNativeSurface { pub fn destroy(&mut self, graphics_context: &NativeDisplay) { match self.image { - None => {}, + None => {} Some(image_khr) => { DestroyImageKHR(graphics_context.display, image_khr); mem::replace(&mut self.image, None); @@ -149,9 +148,10 @@ impl EGLImageNativeSurface { self.will_leak = false } - pub fn gl_rasterization_context(&mut self, - gl_context: Arc) - -> Option { + pub fn gl_rasterization_context( + &mut self, + gl_context: Arc, + ) -> Option { // TODO: Eventually we should preserve the previous GLRasterizationContext, // so that we don't have to keep destroying and recreating the image. if let Some(egl_image) = self.image.take() { diff --git a/src/platform/egl/surface.rs b/src/platform/egl/surface.rs index a1a2206..584a1ff 100644 --- a/src/platform/egl/surface.rs +++ b/src/platform/egl/surface.rs @@ -29,10 +29,10 @@ const GL_FORMAT_BGRA: gl::GLuint = gl::BGRA; #[cfg(any(target_os = "android", target_os = "gonk"))] const GL_FORMAT_BGRA: gl::GLuint = gl::BGRA_EXT; -#[cfg(target_os="linux")] +#[cfg(target_os = "linux")] pub use platform::linux::surface::NativeDisplay; -#[cfg(target_os="android")] +#[cfg(target_os = "android")] pub use platform::android::surface::NativeDisplay; pub struct EGLImageNativeSurface { @@ -68,25 +68,29 @@ impl EGLImageNativeSurface { pub fn bind_to_texture(&self, _: &NativeDisplay, texture: &Texture) { let _bound = texture.bind(); match self.image { - None => match self.bitmap { - Some(ref bitmap) => { - let data = bitmap.as_ptr() as *const c_void; - unsafe { - TexImage2D(TEXTURE_2D, - 0, - GL_FORMAT_BGRA as i32, - self.size.width as i32, - self.size.height as i32, - 0, - GL_FORMAT_BGRA as u32, - UNSIGNED_BYTE, - data); - } - } - None => { - debug!("Cannot bind the buffer(CPU rendering), there is no bitmap"); + None => { + match self.bitmap { + Some(ref bitmap) => { + let data = bitmap.as_ptr() as *const c_void; + unsafe { + TexImage2D( + TEXTURE_2D, + 0, + GL_FORMAT_BGRA as i32, + self.size.width as i32, + self.size.height as i32, + 0, + GL_FORMAT_BGRA as u32, + UNSIGNED_BYTE, + data, + ); + } + } + None => { + debug!("Cannot bind the buffer(CPU rendering), there is no bitmap"); + } } - }, + } Some(_image_khr) => { panic!("TODO: Support GPU rasterizer path on EGL"); } @@ -128,9 +132,10 @@ impl EGLImageNativeSurface { self.will_leak = false } - pub fn gl_rasterization_context(&mut self, - _gl_context: Arc) - -> Option { + pub fn gl_rasterization_context( + &mut self, + _gl_context: Arc, + ) -> Option { panic!("TODO: Support GL context on EGL"); } } diff --git a/src/platform/linux/surface.rs b/src/platform/linux/surface.rs index d510e5d..3a7def0 100644 --- a/src/platform/linux/surface.rs +++ b/src/platform/linux/surface.rs @@ -13,7 +13,7 @@ //TODO: Linking EGL here is probably wrong - should it be done in gleam / glutin etc? #[link(name = "EGL")] -extern {} +extern "C" {} use texturegl::Texture; @@ -40,7 +40,6 @@ use egl::egl::{EGLDisplay, GetCurrentDisplay}; /// to fix because the Display is given to us by the native windowing system, but we should fix it /// someday. /// FIXME(pcwalton): Mark nonsendable. - #[derive(Copy, Clone)] pub struct GLXDisplayInfo { pub display: *mut xlib::Display, @@ -79,8 +78,9 @@ impl NativeDisplay { /// Chooses the compositor visual info using the same algorithm that the compositor uses. /// /// FIXME(pcwalton): It would be more robust to actually have the compositor pass the visual. - fn compositor_visual_info(display: *mut xlib::Display) - -> (*mut xlib::XVisualInfo, Option) { + fn compositor_visual_info( + display: *mut xlib::Display, + ) -> (*mut xlib::XVisualInfo, Option) { // If display is null, we'll assume we are going to be rendering // in headless mode without X running. if display == ptr::null_mut() { @@ -89,28 +89,36 @@ impl NativeDisplay { unsafe { let fbconfig_attributes = [ - glx::DOUBLEBUFFER as i32, 0, - glx::DRAWABLE_TYPE as i32, glx::PIXMAP_BIT as i32 | glx::WINDOW_BIT as i32, - glx::BIND_TO_TEXTURE_RGBA_EXT as i32, 1, - glx::RENDER_TYPE as i32, glx::RGBA_BIT as i32, - glx::ALPHA_SIZE as i32, 8, - 0 + glx::DOUBLEBUFFER as i32, + 0, + glx::DRAWABLE_TYPE as i32, + glx::PIXMAP_BIT as i32 | glx::WINDOW_BIT as i32, + glx::BIND_TO_TEXTURE_RGBA_EXT as i32, + 1, + glx::RENDER_TYPE as i32, + glx::RGBA_BIT as i32, + glx::ALPHA_SIZE as i32, + 8, + 0, ]; let screen = xlib::XDefaultScreen(display); let mut number_of_configs = 0; - let configs = glx::ChooseFBConfig(mem::transmute(display), - screen, - fbconfig_attributes.as_ptr(), - &mut number_of_configs); + let configs = glx::ChooseFBConfig( + mem::transmute(display), + screen, + fbconfig_attributes.as_ptr(), + &mut number_of_configs, + ); NativeDisplay::get_compatible_configuration(display, configs, number_of_configs) } } - fn get_compatible_configuration(display: *mut xlib::Display, - configs: *mut glx::types::GLXFBConfig, - number_of_configs: i32) - -> (*mut xlib::XVisualInfo, Option) { + fn get_compatible_configuration( + display: *mut xlib::Display, + configs: *mut glx::types::GLXFBConfig, + number_of_configs: i32, + ) -> (*mut xlib::XVisualInfo, Option) { unsafe { if number_of_configs == 0 { panic!("glx::ChooseFBConfig returned no configurations."); @@ -149,12 +157,11 @@ impl NativeDisplay { if glx_vendor == ptr::null() { panic!("Could not determine GLX vendor."); } - let glx_vendor = - str::from_utf8(CStr::from_ptr(glx_vendor).to_bytes()) - .ok() - .expect("GLX client vendor string not in UTF-8 format.") - .to_string() - .to_ascii_lowercase(); + let glx_vendor = str::from_utf8(CStr::from_ptr(glx_vendor).to_bytes()) + .ok() + .expect("GLX client vendor string not in UTF-8 format.") + .to_string() + .to_ascii_lowercase(); glx_vendor.contains("nvidia") || glx_vendor.contains("ati") } } @@ -172,9 +179,7 @@ impl NativeDisplay { } pub fn new_egl_display() -> NativeDisplay { - NativeDisplay::EGL(EGLDisplayInfo { - display: GetCurrentDisplay() - }) + NativeDisplay::EGL(EGLDisplayInfo { display: GetCurrentDisplay() }) } } @@ -193,8 +198,10 @@ pub struct PixmapNativeSurface { impl Drop for PixmapNativeSurface { fn drop(&mut self) { if self.will_leak { - panic!("You should have disposed of the pixmap properly with destroy()! This pixmap \ - will leak!"); + panic!( + "You should have disposed of the pixmap properly with destroy()! This pixmap \ + will leak!" + ); } } } @@ -208,11 +215,13 @@ impl PixmapNativeSurface { // The X server we use for testing on build machines always returns // visuals that report 24 bit depth. But creating a 32 bit pixmap does work, so // hard code the depth here. - let pixmap = xlib::XCreatePixmap(display.display, - window, - size.width as c_uint, - size.height as c_uint, - 32); + let pixmap = xlib::XCreatePixmap( + display.display, + window, + size.width as c_uint, + size.height as c_uint, + 32, + ); PixmapNativeSurface { pixmap: pixmap, will_leak: true, @@ -233,27 +242,39 @@ impl PixmapNativeSurface { }; let pixmap_attributes = [ - glx::TEXTURE_TARGET_EXT as i32, glx::TEXTURE_2D_EXT as i32, - glx::TEXTURE_FORMAT_EXT as i32, glx::TEXTURE_FORMAT_RGBA_EXT as i32, - 0 + glx::TEXTURE_TARGET_EXT as i32, + glx::TEXTURE_2D_EXT as i32, + glx::TEXTURE_FORMAT_EXT as i32, + glx::TEXTURE_FORMAT_RGBA_EXT as i32, + 0, ]; let glx_display = mem::transmute(display.display); - let glx_pixmap = glx::CreatePixmap(glx_display, - display.framebuffer_configuration.expect( - "GLX 1.3 should have a framebuffer_configuration"), - self.pixmap, - pixmap_attributes.as_ptr()); - - let glx_bind_tex_image: extern "C" fn(*mut xlib::Display, glx::types::GLXDrawable, c_int, *mut c_int) = - mem::transmute(glx::GetProcAddress(mem::transmute(&"glXBindTexImageEXT\x00".as_bytes()[0]))); + let glx_pixmap = glx::CreatePixmap( + glx_display, + display.framebuffer_configuration.expect( + "GLX 1.3 should have a framebuffer_configuration", + ), + self.pixmap, + pixmap_attributes.as_ptr(), + ); + + let glx_bind_tex_image: extern "C" fn(*mut xlib::Display, + glx::types::GLXDrawable, + c_int, + *mut c_int) = + mem::transmute(glx::GetProcAddress( + mem::transmute(&"glXBindTexImageEXT\x00".as_bytes()[0]), + )); assert!(glx_bind_tex_image as *mut c_void != ptr::null_mut()); let _bound = texture.bind(); - glx_bind_tex_image(display.display, - mem::transmute(glx_pixmap), - glx::FRONT_EXT as i32, - ptr::null_mut()); + glx_bind_tex_image( + display.display, + mem::transmute(glx_pixmap), + glx::FRONT_EXT as i32, + ptr::null_mut(), + ); // FIXME(pcwalton): Recycle these for speed? glx::DestroyPixmap(glx_display, glx_pixmap); @@ -268,28 +289,32 @@ impl PixmapNativeSurface { &NativeDisplay::EGL(_) => unreachable!(), }; - let image = xlib::XCreateImage(display.display, - (*display.visual_info).visual, - 32, - xlib::ZPixmap, - 0, - mem::transmute(&data[0]), - self.size.width as c_uint, - self.size.height as c_uint, - 32, - 0); + let image = xlib::XCreateImage( + display.display, + (*display.visual_info).visual, + 32, + xlib::ZPixmap, + 0, + mem::transmute(&data[0]), + self.size.width as c_uint, + self.size.height as c_uint, + 32, + 0, + ); let gc = xlib::XCreateGC(display.display, self.pixmap, 0, ptr::null_mut()); - let _ = xlib::XPutImage(display.display, - self.pixmap, - gc, - image, - 0, - 0, - 0, - 0, - self.size.width as c_uint, - self.size.height as c_uint); + let _ = xlib::XPutImage( + display.display, + self.pixmap, + gc, + image, + 0, + 0, + 0, + 0, + self.size.width as c_uint, + self.size.height as c_uint, + ); } } @@ -318,9 +343,10 @@ impl PixmapNativeSurface { self.will_leak = false; } - pub fn gl_rasterization_context(&mut self, - gl_context: Arc) - -> Option { + pub fn gl_rasterization_context( + &mut self, + gl_context: Arc, + ) -> Option { GLRasterizationContext::new(gl_context, self.pixmap, self.size) } } diff --git a/src/platform/macos/surface.rs b/src/platform/macos/surface.rs index 1dc2d59..c9ced59 100644 --- a/src/platform/macos/surface.rs +++ b/src/platform/macos/surface.rs @@ -34,16 +34,12 @@ unsafe impl Send for NativeDisplay {} impl NativeDisplay { pub fn new() -> NativeDisplay { unsafe { - NativeDisplay { - pixel_format: cgl::CGLGetPixelFormat(cgl::CGLGetCurrentContext()), - } + NativeDisplay { pixel_format: cgl::CGLGetPixelFormat(cgl::CGLGetCurrentContext()) } } } pub fn platform_display_data(&self) -> PlatformDisplayData { - PlatformDisplayData { - pixel_format: self.pixel_format, - } + PlatformDisplayData { pixel_format: self.pixel_format } } } @@ -68,7 +64,12 @@ impl Decodable for IOSurfaceNativeSurface { } impl Encodable for IOSurfaceNativeSurface { fn encode(&self, e: &mut E) -> Result<(), E::Error> { - try!(self.surface.as_ref().map(io_surface::IOSurface::get_id).encode(e)); + try!( + self.surface + .as_ref() + .map(io_surface::IOSurface::get_id) + .encode(e) + ); try!(self.will_leak.encode(e)); try!(self.size.encode(e)); Ok(()) @@ -96,13 +97,21 @@ impl IOSurfaceNativeSurface { TCFType::wrap_under_get_rule(io_surface::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()), - ])); + 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()), + ], + )); IOSurfaceNativeSurface { surface: Some(surface), @@ -143,11 +152,10 @@ impl IOSurfaceNativeSurface { self.will_leak = false } - pub fn gl_rasterization_context(&mut self, - gl_context: Arc) - -> Option { - GLRasterizationContext::new(gl_context, - self.surface.as_ref().unwrap().obj, - self.size) + pub fn gl_rasterization_context( + &mut self, + gl_context: Arc, + ) -> Option { + GLRasterizationContext::new(gl_context, self.surface.as_ref().unwrap().obj, self.size) } } diff --git a/src/platform/surface.rs b/src/platform/surface.rs index 04ebfa2..1e33820 100644 --- a/src/platform/surface.rs +++ b/src/platform/surface.rs @@ -17,39 +17,37 @@ use skia::gl_rasterization_context::GLRasterizationContext; use skia::gl_context::GLContext; use std::sync::Arc; -#[cfg(not(target_os="android"))] +#[cfg(not(target_os = "android"))] use gleam::gl; -#[cfg(target_os="macos")] -pub use platform::macos::surface::{NativeDisplay, - IOSurfaceNativeSurface}; +#[cfg(target_os = "macos")] +pub use platform::macos::surface::{NativeDisplay, IOSurfaceNativeSurface}; -#[cfg(target_os="linux")] -pub use platform::linux::surface::{NativeDisplay, - PixmapNativeSurface}; -#[cfg(target_os="linux")] +#[cfg(target_os = "linux")] +pub use platform::linux::surface::{NativeDisplay, PixmapNativeSurface}; +#[cfg(target_os = "linux")] use std::ptr; -#[cfg(any(target_os="android",target_os="linux"))] -pub use platform::egl::surface::{EGLImageNativeSurface}; +#[cfg(any(target_os = "android", target_os = "linux"))] +pub use platform::egl::surface::EGLImageNativeSurface; -#[cfg(target_os="android")] +#[cfg(target_os = "android")] pub use platform::android::surface::NativeDisplay; -#[cfg(target_os="windows")] +#[cfg(target_os = "windows")] pub use platform::windows::surface::NativeDisplay; pub enum NativeSurface { MemoryBuffer(MemoryBufferNativeSurface), -#[cfg(target_os="linux")] + #[cfg(target_os = "linux")] Pixmap(PixmapNativeSurface), -#[cfg(target_os="macos")] + #[cfg(target_os = "macos")] IOSurface(IOSurfaceNativeSurface), -#[cfg(any(target_os="android",target_os="linux"))] + #[cfg(any(target_os = "android", target_os = "linux"))] EGLImage(EGLImageNativeSurface), } -#[cfg(target_os="linux")] +#[cfg(target_os = "linux")] impl NativeSurface { /// Creates a new native surface with uninitialized data. pub fn new(display: &NativeDisplay, size: Size2D) -> NativeSurface { @@ -68,28 +66,28 @@ impl NativeSurface { } } -#[cfg(target_os="macos")] +#[cfg(target_os = "macos")] impl NativeSurface { /// Creates a new native surface with uninitialized data. pub fn new(display: &NativeDisplay, size: Size2D) -> NativeSurface { NativeSurface::IOSurface(IOSurfaceNativeSurface::new(display, size)) - } + } } -#[cfg(target_os="android")] +#[cfg(target_os = "android")] impl NativeSurface { /// Creates a new native surface with uninitialized data. pub fn new(display: &NativeDisplay, size: Size2D) -> NativeSurface { NativeSurface::EGLImage(EGLImageNativeSurface::new(display, size)) - } + } } -#[cfg(target_os="windows")] +#[cfg(target_os = "windows")] impl NativeSurface { /// Creates a new native surface with uninitialized data. pub fn new(display: &NativeDisplay, size: Size2D) -> NativeSurface { NativeSurface::MemoryBuffer(MemoryBufferNativeSurface::new(display, size)) - } + } } macro_rules! native_surface_method_with_mutability { @@ -193,9 +191,10 @@ impl NativeSurface { native_surface_method_mut!(self mark_wont_leak ()) } - pub fn gl_rasterization_context(&mut self, - gl_context: Arc) - -> Option> { + pub fn gl_rasterization_context( + &mut self, + gl_context: Arc, + ) -> Option> { match native_surface_method_mut!(self gl_rasterization_context (gl_context)) { Some(context) => Some(Arc::new(context)), None => None, @@ -225,28 +224,30 @@ pub struct MemoryBufferNativeSurface { impl MemoryBufferNativeSurface { pub fn new(_: &NativeDisplay, size: Size2D) -> MemoryBufferNativeSurface { - MemoryBufferNativeSurface{ - bytes: vec!(), + MemoryBufferNativeSurface { + bytes: vec![], size: size, } } /// This may only be called on the compositor side. - #[cfg(not(target_os="android"))] + #[cfg(not(target_os = "android"))] pub fn bind_to_texture(&self, _: &NativeDisplay, texture: &Texture) { let _bound = texture.bind(); - gl::tex_image_2d(gl::TEXTURE_2D, - 0, - gl::RGBA as i32, - self.size.width as i32, - self.size.height as i32, - 0, - gl::BGRA, - gl::UNSIGNED_BYTE, - Some(&self.bytes)); + gl::tex_image_2d( + gl::TEXTURE_2D, + 0, + gl::RGBA as i32, + self.size.width as i32, + self.size.height as i32, + 0, + gl::BGRA, + gl::UNSIGNED_BYTE, + Some(&self.bytes), + ); } - #[cfg(target_os="android")] + #[cfg(target_os = "android")] pub fn bind_to_texture(&self, _: &NativeDisplay, _: &Texture) { panic!("Binding a memory surface to a texture is not yet supported on Android."); } @@ -261,18 +262,16 @@ impl MemoryBufferNativeSurface { 0 } - pub fn destroy(&mut self, _: &NativeDisplay) { - } + pub fn destroy(&mut self, _: &NativeDisplay) {} - pub fn mark_will_leak(&mut self) { - } + pub fn mark_will_leak(&mut self) {} - pub fn mark_wont_leak(&mut self) { - } + pub fn mark_wont_leak(&mut self) {} - pub fn gl_rasterization_context(&mut self, - _: Arc) - -> Option { + pub fn gl_rasterization_context( + &mut self, + _: Arc, + ) -> Option { None } } diff --git a/src/platform/windows/surface.rs b/src/platform/windows/surface.rs index f63569d..9ed9f60 100644 --- a/src/platform/windows/surface.rs +++ b/src/platform/windows/surface.rs @@ -3,7 +3,7 @@ use skia::gl_context::PlatformDisplayData; #[derive(Copy, Clone)] pub struct NativeDisplay; -#[cfg(target_os="windows")] +#[cfg(target_os = "windows")] impl NativeDisplay { pub fn new() -> NativeDisplay { NativeDisplay @@ -13,4 +13,3 @@ impl NativeDisplay { PlatformDisplayData::new() } } - diff --git a/src/rendergl.rs b/src/rendergl.rs old mode 100755 new mode 100644 index 6037c82..b578625 --- a/src/rendergl.rs +++ b/src/rendergl.rs @@ -69,7 +69,14 @@ const ORTHO_NEAR_PLANE: f32 = -1000000.0; const ORTHO_FAR_PLANE: f32 = 1000000.0; fn create_ortho(scene_size: &Size2D) -> Matrix4D { - Matrix4D::ortho(0.0, scene_size.width, scene_size.height, 0.0, ORTHO_NEAR_PLANE, ORTHO_FAR_PLANE) + Matrix4D::ortho( + 0.0, + scene_size.width, + scene_size.height, + 0.0, + ORTHO_NEAR_PLANE, + ORTHO_FAR_PLANE, + ) } static TEXTURE_FRAGMENT_SHADER_SOURCE: &'static str = " @@ -125,11 +132,26 @@ static SOLID_COLOR_VERTEX_SHADER_SOURCE: &'static str = " } "; -static TILE_DEBUG_BORDER_COLOR: Color = Color { r: 0., g: 1., b: 1., a: 1.0 }; +static TILE_DEBUG_BORDER_COLOR: Color = Color { + r: 0., + g: 1., + b: 1., + a: 1.0, +}; static TILE_DEBUG_BORDER_THICKNESS: usize = 1; -static LAYER_DEBUG_BORDER_COLOR: Color = Color { r: 1., g: 0.5, b: 0., a: 1.0 }; +static LAYER_DEBUG_BORDER_COLOR: Color = Color { + r: 1., + g: 0.5, + b: 0., + a: 1.0, +}; static LAYER_DEBUG_BORDER_THICKNESS: usize = 2; -static LAYER_AABB_DEBUG_BORDER_COLOR: Color = Color { r: 1., g: 0.0, b: 0., a: 1.0 }; +static LAYER_AABB_DEBUG_BORDER_COLOR: Color = Color { + r: 1., + g: 0.0, + b: 0., + a: 1.0, +}; static LAYER_AABB_DEBUG_BORDER_THICKNESS: usize = 1; #[derive(Copy, Clone)] @@ -146,21 +168,28 @@ struct ShaderProgram { impl ShaderProgram { pub fn new(vertex_shader_source: &str, fragment_shader_source: &str) -> ShaderProgram { let id = gl::create_program(); - gl::attach_shader(id, ShaderProgram::compile_shader(fragment_shader_source, gl::FRAGMENT_SHADER)); - gl::attach_shader(id, ShaderProgram::compile_shader(vertex_shader_source, gl::VERTEX_SHADER)); + gl::attach_shader( + id, + ShaderProgram::compile_shader(fragment_shader_source, gl::FRAGMENT_SHADER), + ); + gl::attach_shader( + id, + ShaderProgram::compile_shader(vertex_shader_source, gl::VERTEX_SHADER), + ); gl::link_program(id); if gl::get_program_iv(id, gl::LINK_STATUS) == (0 as GLint) { - panic!("Failed to compile shader program: {}", gl::get_program_info_log(id)); + panic!( + "Failed to compile shader program: {}", + gl::get_program_info_log(id) + ); } - ShaderProgram { - id: id, - } + ShaderProgram { id: id } } pub fn compile_shader(source_string: &str, shader_type: GLenum) -> GLuint { let id = gl::create_shader(shader_type); - gl::shader_source(id, &[ source_string.as_bytes() ]); + gl::shader_source(id, &[source_string.as_bytes()]); gl::compile_shader(id); if gl::get_shader_iv(id, gl::COMPILE_STATUS) == (0 as GLint) { panic!("Failed to compile shader: {}", gl::get_shader_info_log(id)); @@ -192,11 +221,12 @@ struct TextureProgram { impl TextureProgram { fn new(sampler_function: &str, sampler_type: &str) -> TextureProgram { - let fragment_shader_source - = fmt::format(format_args!("#define samplerFunction {}\n#define samplerType {}\n{}", - sampler_function, - sampler_type, - TEXTURE_FRAGMENT_SHADER_SOURCE)); + let fragment_shader_source = fmt::format(format_args!( + "#define samplerFunction {}\n#define samplerType {}\n{}", + sampler_function, + sampler_type, + TEXTURE_FRAGMENT_SHADER_SOURCE + )); let program = ShaderProgram::new(TEXTURE_VERTEX_SHADER_SOURCE, &fragment_shader_source); TextureProgram { program: program, @@ -210,31 +240,51 @@ impl TextureProgram { } } - fn bind_uniforms_and_attributes(&self, - vertices: &[TextureVertex; 4], - transform: &Matrix4D, - projection_matrix: &Matrix4D, - texture_space_transform: &Matrix4D, - buffers: &Buffers, - opacity: f32) { + fn bind_uniforms_and_attributes( + &self, + vertices: &[TextureVertex; 4], + transform: &Matrix4D, + projection_matrix: &Matrix4D, + texture_space_transform: &Matrix4D, + buffers: &Buffers, + opacity: f32, + ) { gl::uniform_1i(self.sampler_uniform, 0); - gl::uniform_matrix_4fv(self.modelview_uniform, - false, - &transform.to_row_major_array()); - gl::uniform_matrix_4fv(self.projection_uniform, - false, - &projection_matrix.to_row_major_array()); + gl::uniform_matrix_4fv( + self.modelview_uniform, + false, + &transform.to_row_major_array(), + ); + gl::uniform_matrix_4fv( + self.projection_uniform, + false, + &projection_matrix.to_row_major_array(), + ); let vertex_size = mem::size_of::(); gl::bind_buffer(gl::ARRAY_BUFFER, buffers.quad_vertex_buffer); gl::buffer_data(gl::ARRAY_BUFFER, vertices, gl::DYNAMIC_DRAW); - gl::vertex_attrib_pointer_f32(self.vertex_position_attr as GLuint,2, false, vertex_size as i32, 0); - gl::vertex_attrib_pointer_f32(self.vertex_uv_attr as GLuint, 2, false, vertex_size as i32, 8); - - gl::uniform_matrix_4fv(self.texture_space_transform_uniform, - false, - &texture_space_transform.to_row_major_array()); + gl::vertex_attrib_pointer_f32( + self.vertex_position_attr as GLuint, + 2, + false, + vertex_size as i32, + 0, + ); + gl::vertex_attrib_pointer_f32( + self.vertex_uv_attr as GLuint, + 2, + false, + vertex_size as i32, + 8, + ); + + gl::uniform_matrix_4fv( + self.texture_space_transform_uniform, + false, + &texture_space_transform.to_row_major_array(), + ); gl::uniform_1f(self.opacity_uniform, opacity); } @@ -253,13 +303,13 @@ impl TextureProgram { TextureProgram::new("texture2D", "sampler2D") } - #[cfg(target_os="macos")] + #[cfg(target_os = "macos")] fn create_rectangle_program_if_necessary() -> Option { gl::enable(gl::TEXTURE_RECTANGLE_ARB); Some(TextureProgram::new("texture2DRect", "sampler2DRect")) } - #[cfg(not(target_os="macos"))] + #[cfg(not(target_os = "macos"))] fn create_rectangle_program_if_necessary() -> Option { None } @@ -276,8 +326,10 @@ struct SolidColorProgram { impl SolidColorProgram { fn new() -> SolidColorProgram { - let program = ShaderProgram::new(SOLID_COLOR_VERTEX_SHADER_SOURCE, - SOLID_COLOR_FRAGMENT_SHADER_SOURCE); + let program = ShaderProgram::new( + SOLID_COLOR_VERTEX_SHADER_SOURCE, + SOLID_COLOR_FRAGMENT_SHADER_SOURCE, + ); SolidColorProgram { program: program, vertex_position_attr: program.get_attribute_location("aVertexPosition"), @@ -287,29 +339,39 @@ impl SolidColorProgram { } } - fn bind_uniforms_and_attributes_common(&self, - transform: &Matrix4D, - projection_matrix: &Matrix4D, - color: &Color) { - gl::uniform_matrix_4fv(self.modelview_uniform, - false, - &transform.to_row_major_array()); - gl::uniform_matrix_4fv(self.projection_uniform, - false, - &projection_matrix.to_row_major_array()); - gl::uniform_4f(self.color_uniform, - color.r as GLfloat, - color.g as GLfloat, - color.b as GLfloat, - color.a as GLfloat); - } - - fn bind_uniforms_and_attributes_for_lines(&self, - vertices: &[ColorVertex; 5], - transform: &Matrix4D, - projection_matrix: &Matrix4D, - buffers: &Buffers, - color: &Color) { + fn bind_uniforms_and_attributes_common( + &self, + transform: &Matrix4D, + projection_matrix: &Matrix4D, + color: &Color, + ) { + gl::uniform_matrix_4fv( + self.modelview_uniform, + false, + &transform.to_row_major_array(), + ); + gl::uniform_matrix_4fv( + self.projection_uniform, + false, + &projection_matrix.to_row_major_array(), + ); + gl::uniform_4f( + self.color_uniform, + color.r as GLfloat, + color.g as GLfloat, + color.b as GLfloat, + color.a as GLfloat, + ); + } + + fn bind_uniforms_and_attributes_for_lines( + &self, + vertices: &[ColorVertex; 5], + transform: &Matrix4D, + projection_matrix: &Matrix4D, + buffers: &Buffers, + color: &Color, + ) { self.bind_uniforms_and_attributes_common(transform, projection_matrix, color); gl::bind_buffer(gl::ARRAY_BUFFER, buffers.line_quad_vertex_buffer); @@ -317,12 +379,14 @@ impl SolidColorProgram { gl::vertex_attrib_pointer_f32(self.vertex_position_attr as GLuint, 2, false, 0, 0); } - fn bind_uniforms_and_attributes_for_quad(&self, - vertices: &[ColorVertex; 4], - transform: &Matrix4D, - projection_matrix: &Matrix4D, - buffers: &Buffers, - color: &Color) { + fn bind_uniforms_and_attributes_for_quad( + &self, + vertices: &[ColorVertex; 4], + transform: &Matrix4D, + projection_matrix: &Matrix4D, + buffers: &Buffers, + color: &Color, + ) { self.bind_uniforms_and_attributes_common(transform, projection_matrix, color); gl::bind_buffer(gl::ARRAY_BUFFER, buffers.quad_vertex_buffer); @@ -346,7 +410,7 @@ struct RenderContextChild { z_center: f32, } -pub struct RenderContext3D{ +pub struct RenderContext3D { children: Vec>, clip_rect: Option>, } @@ -354,7 +418,7 @@ pub struct RenderContext3D{ impl RenderContext3D { fn new(layer: Rc>) -> RenderContext3D { let mut render_context = RenderContext3D { - children: vec!(), + children: vec![], clip_rect: RenderContext3D::calculate_context_clip(layer.clone(), None), }; layer.build(&mut render_context); @@ -362,9 +426,10 @@ impl RenderContext3D { render_context } - fn build_child(layer: Rc>, - parent_clip_rect: Option>) - -> Option> { + fn build_child( + layer: Rc>, + parent_clip_rect: Option>, + ) -> Option> { let clip_rect = RenderContext3D::calculate_context_clip(layer.clone(), parent_clip_rect); if let Some(ref clip_rect) = clip_rect { if clip_rect.is_empty() { @@ -373,7 +438,7 @@ impl RenderContext3D { } let mut render_context = RenderContext3D { - children: vec!(), + children: vec![], clip_rect: clip_rect, }; @@ -389,24 +454,23 @@ impl RenderContext3D { // TODO(gw): This is basically what FF does, which breaks badly // when there are intersecting polygons. Need to split polygons // to handle this case correctly (Blink uses a BSP tree). - self.children.sort_by(|a, b| { - if a.z_center < b.z_center { - Ordering::Less - } else if a.z_center > b.z_center { - Ordering::Greater - } else if a.paint_order < b.paint_order { - Ordering::Less - } else if a.paint_order > b.paint_order { - Ordering::Greater - } else { - Ordering::Equal - } + self.children.sort_by(|a, b| if a.z_center < b.z_center { + Ordering::Less + } else if a.z_center > b.z_center { + Ordering::Greater + } else if a.paint_order < b.paint_order { + Ordering::Less + } else if a.paint_order > b.paint_order { + Ordering::Greater + } else { + Ordering::Equal }); } - fn calculate_context_clip(layer: Rc>, - parent_clip_rect: Option>) - -> Option> { + fn calculate_context_clip( + layer: Rc>, + parent_clip_rect: Option>, + ) -> Option> { // TODO(gw): This doesn't work for iframes that are transformed. if !*layer.masks_to_bounds.borrow() { return parent_clip_rect; @@ -418,18 +482,22 @@ impl RenderContext3D { }; match parent_clip_rect { - Some(parent_clip_rect) => match layer_clip.intersection(&parent_clip_rect) { - Some(intersected_clip) => Some(intersected_clip), - None => Some(Rect::zero()), // No intersection. - }, + Some(parent_clip_rect) => { + match layer_clip.intersection(&parent_clip_rect) { + Some(intersected_clip) => Some(intersected_clip), + None => Some(Rect::zero()), // No intersection. + } + } None => Some(layer_clip), } } - fn add_child(&mut self, - layer: Option>>, - child_context: Option>, - z_center: f32) { + fn add_child( + &mut self, + layer: Option>>, + child_context: Option>, + z_center: f32, + ) { let paint_order = self.children.len(); self.children.push(RenderContextChild { layer: layer, @@ -490,9 +558,11 @@ pub struct RenderContext { } impl RenderContext { - pub fn new(compositing_display: NativeDisplay, - show_debug_borders: bool, - force_near_texture_filter: bool) -> RenderContext { + pub fn new( + compositing_display: NativeDisplay, + show_debug_borders: bool, + force_near_texture_filter: bool, + ) -> RenderContext { gl::enable(gl::TEXTURE_2D); // Each layer uses premultiplied alpha! @@ -527,38 +597,47 @@ impl RenderContext { } } - fn bind_and_render_solid_quad(&self, - vertices: &[ColorVertex; 4], - transform: &Matrix4D, - projection: &Matrix4D, - color: &Color) { + fn bind_and_render_solid_quad( + &self, + vertices: &[ColorVertex; 4], + transform: &Matrix4D, + projection: &Matrix4D, + color: &Color, + ) { self.solid_color_program.enable_attribute_arrays(); gl::use_program(self.solid_color_program.program.id); - self.solid_color_program.bind_uniforms_and_attributes_for_quad(vertices, - transform, - projection, - &self.buffers, - color); + self.solid_color_program + .bind_uniforms_and_attributes_for_quad( + vertices, + transform, + projection, + &self.buffers, + color, + ); gl::draw_arrays(gl::TRIANGLE_STRIP, 0, 4); self.solid_color_program.disable_attribute_arrays(); } - fn bind_and_render_quad(&self, - vertices: &[TextureVertex; 4], - texture: &Texture, - transform: &Matrix4D, - projection_matrix: &Matrix4D, - opacity: f32) { + fn bind_and_render_quad( + &self, + vertices: &[TextureVertex; 4], + texture: &Texture, + transform: &Matrix4D, + projection_matrix: &Matrix4D, + opacity: f32, + ) { let mut texture_coordinates_need_to_be_scaled_by_size = false; let program = match texture.target { TextureTarget2D => self.texture_2d_program, - TextureTargetRectangle => match self.texture_rectangle_program { - Some(program) => { - texture_coordinates_need_to_be_scaled_by_size = true; - program + TextureTargetRectangle => { + match self.texture_rectangle_program { + Some(program) => { + texture_coordinates_need_to_be_scaled_by_size = true; + program + } + None => panic!("There is no shader program for texture rectangle"), } - None => panic!("There is no shader program for texture rectangle"), - }, + } }; program.enable_attribute_arrays(); @@ -571,8 +650,16 @@ impl RenderContext { } else { gl::LINEAR } as GLint; - gl::tex_parameter_i(texture.target.as_gl_target(), gl::TEXTURE_MAG_FILTER, filter_mode); - gl::tex_parameter_i(texture.target.as_gl_target(), gl::TEXTURE_MIN_FILTER, filter_mode); + gl::tex_parameter_i( + texture.target.as_gl_target(), + gl::TEXTURE_MAG_FILTER, + filter_mode, + ); + gl::tex_parameter_i( + texture.target.as_gl_target(), + gl::TEXTURE_MIN_FILTER, + filter_mode, + ); // We calculate a transformation matrix for the texture coordinates // which is useful for flipping the texture vertically or scaling the @@ -583,18 +670,23 @@ impl RenderContext { } if texture_coordinates_need_to_be_scaled_by_size { texture_transform = texture_transform.pre_scaled( - texture.size.width as f32, texture.size.height as f32, 1.0); + texture.size.width as f32, + texture.size.height as f32, + 1.0, + ); } if texture.flip == VerticalFlip { texture_transform = texture_transform.pre_translated(0.0, -1.0, 0.0); } - program.bind_uniforms_and_attributes(vertices, - transform, - &projection_matrix, - &texture_transform, - &self.buffers, - opacity); + program.bind_uniforms_and_attributes( + vertices, + transform, + &projection_matrix, + &texture_transform, + &self.buffers, + opacity, + ); // Draw! gl::draw_arrays(gl::TRIANGLE_STRIP, 0, 4); @@ -604,30 +696,37 @@ impl RenderContext { program.disable_attribute_arrays() } - pub fn bind_and_render_quad_lines(&self, - vertices: &[ColorVertex; 5], - transform: &Matrix4D, - projection: &Matrix4D, - color: &Color, - line_thickness: usize) { + pub fn bind_and_render_quad_lines( + &self, + vertices: &[ColorVertex; 5], + transform: &Matrix4D, + projection: &Matrix4D, + color: &Color, + line_thickness: usize, + ) { self.solid_color_program.enable_attribute_arrays(); gl::use_program(self.solid_color_program.program.id); - self.solid_color_program.bind_uniforms_and_attributes_for_lines(vertices, - transform, - projection, - &self.buffers, - color); + self.solid_color_program + .bind_uniforms_and_attributes_for_lines( + vertices, + transform, + projection, + &self.buffers, + color, + ); gl::line_width(line_thickness as GLfloat); gl::draw_arrays(gl::LINE_STRIP, 0, 5); self.solid_color_program.disable_attribute_arrays(); } - fn render_layer(&self, - layer: Rc>, - transform: &Matrix4D, - projection: &Matrix4D, - clip_rect: Option>, - gfx_context: &NativeDisplay) { + fn render_layer( + &self, + layer: Rc>, + transform: &Matrix4D, + projection: &Matrix4D, + clip_rect: Option>, + gfx_context: &NativeDisplay, + ) { let ts = layer.transform_state.borrow(); let transform = transform.pre_mul(&ts.final_transform); let background_color = *layer.background_color.borrow(); @@ -635,11 +734,11 @@ impl RenderContext { // Create native textures for this layer layer.create_textures(gfx_context); - let layer_rect = clip_rect.map_or(ts.world_rect, |clip_rect| { - match clip_rect.intersection(&ts.world_rect) { - Some(layer_rect) => layer_rect, - None => Rect::zero(), - } + let layer_rect = clip_rect.map_or(ts.world_rect, |clip_rect| match clip_rect.intersection( + &ts.world_rect, + ) { + Some(layer_rect) => layer_rect, + None => Rect::zero(), }); if layer_rect.is_empty() { @@ -654,19 +753,23 @@ impl RenderContext { ColorVertex::new(layer_rect.bottom_right()), ]; - self.bind_and_render_solid_quad(&bg_vertices, - &transform, - &projection, - &background_color); + self.bind_and_render_solid_quad( + &bg_vertices, + &transform, + &projection, + &background_color, + ); } layer.do_for_all_tiles(|tile: &Tile| { - self.render_tile(tile, - &ts.world_rect.origin, - &transform, - projection, - clip_rect, - *layer.opacity.borrow()); + self.render_tile( + tile, + &ts.world_rect.origin, + &transform, + projection, + clip_rect, + *layer.opacity.borrow(), + ); }); if self.show_debug_borders { @@ -677,11 +780,13 @@ impl RenderContext { ColorVertex::new(layer_rect.bottom_left()), ColorVertex::new(layer_rect.origin), ]; - self.bind_and_render_quad_lines(&debug_vertices, - &transform, - projection, - &LAYER_DEBUG_BORDER_COLOR, - LAYER_DEBUG_BORDER_THICKNESS); + self.bind_and_render_quad_lines( + &debug_vertices, + &transform, + projection, + &LAYER_DEBUG_BORDER_COLOR, + LAYER_DEBUG_BORDER_THICKNESS, + ); let aabb = ts.screen_rect.as_ref().unwrap().rect; let debug_vertices = [ @@ -691,21 +796,25 @@ impl RenderContext { ColorVertex::new(aabb.bottom_left()), ColorVertex::new(aabb.origin), ]; - self.bind_and_render_quad_lines(&debug_vertices, - &Matrix4D::identity(), - projection, - &LAYER_AABB_DEBUG_BORDER_COLOR, - LAYER_AABB_DEBUG_BORDER_THICKNESS); + self.bind_and_render_quad_lines( + &debug_vertices, + &Matrix4D::identity(), + projection, + &LAYER_AABB_DEBUG_BORDER_COLOR, + LAYER_AABB_DEBUG_BORDER_THICKNESS, + ); } } - fn render_tile(&self, - tile: &Tile, - layer_origin: &Point2D, - transform: &Matrix4D, - projection: &Matrix4D, - clip_rect: Option>, - opacity: f32) { + fn render_tile( + &self, + tile: &Tile, + layer_origin: &Point2D, + transform: &Matrix4D, + projection: &Matrix4D, + clip_rect: Option>, + opacity: f32, + ) { if tile.texture.is_zero() || !tile.bounds.is_some() { return; } @@ -719,22 +828,31 @@ impl RenderContext { }); if clipped_tile_rect.is_empty() { - return; + return; } let texture_rect_origin = clipped_tile_rect.origin - tile_rect.origin; let texture_rect = Rect::new( - Point2D::new(texture_rect_origin.x / tile_rect.size.width, - texture_rect_origin.y / tile_rect.size.height), - Size2D::new(clipped_tile_rect.size.width / tile_rect.size.width, - clipped_tile_rect.size.height / tile_rect.size.height)); - - let tile_vertices: [TextureVertex; 4] = [ - TextureVertex::new(clipped_tile_rect.origin, texture_rect.origin), - TextureVertex::new(clipped_tile_rect.top_right(), texture_rect.top_right()), - TextureVertex::new(clipped_tile_rect.bottom_left(), texture_rect.bottom_left()), - TextureVertex::new(clipped_tile_rect.bottom_right(), texture_rect.bottom_right()), - ]; + Point2D::new( + texture_rect_origin.x / tile_rect.size.width, + texture_rect_origin.y / tile_rect.size.height, + ), + Size2D::new( + clipped_tile_rect.size.width / tile_rect.size.width, + clipped_tile_rect.size.height / tile_rect.size.height, + ), + ); + + let tile_vertices: [TextureVertex; 4] = + [ + TextureVertex::new(clipped_tile_rect.origin, texture_rect.origin), + TextureVertex::new(clipped_tile_rect.top_right(), texture_rect.top_right()), + TextureVertex::new(clipped_tile_rect.bottom_left(), texture_rect.bottom_left()), + TextureVertex::new( + clipped_tile_rect.bottom_right(), + texture_rect.bottom_right(), + ), + ]; if self.show_debug_borders { let debug_vertices = [ @@ -745,25 +863,31 @@ impl RenderContext { ColorVertex::new(clipped_tile_rect.bottom_left()), ColorVertex::new(clipped_tile_rect.origin), ]; - self.bind_and_render_quad_lines(&debug_vertices, - &transform, - projection, - &TILE_DEBUG_BORDER_COLOR, - TILE_DEBUG_BORDER_THICKNESS); + self.bind_and_render_quad_lines( + &debug_vertices, + &transform, + projection, + &TILE_DEBUG_BORDER_COLOR, + TILE_DEBUG_BORDER_THICKNESS, + ); } - self.bind_and_render_quad(&tile_vertices, - &tile.texture, - &transform, - projection, - opacity); - } - - fn render_3d_context(&self, - context: &RenderContext3D, - transform: &Matrix4D, - projection: &Matrix4D, - gfx_context: &NativeDisplay) { + self.bind_and_render_quad( + &tile_vertices, + &tile.texture, + &transform, + projection, + opacity, + ); + } + + fn render_3d_context( + &self, + context: &RenderContext3D, + transform: &Matrix4D, + projection: &Matrix4D, + gfx_context: &NativeDisplay, + ) { if context.children.is_empty() { return; } @@ -784,11 +908,11 @@ impl RenderContext { let m = layer.transform_state.borrow().final_transform; // See https://drafts.csswg.org/css-transforms/#2d-matrix - let is_3d_transform = m.m31 != 0.0 || m.m32 != 0.0 || - m.m13 != 0.0 || m.m23 != 0.0 || - m.m43 != 0.0 || m.m14 != 0.0 || - m.m24 != 0.0 || m.m34 != 0.0 || - m.m33 != 1.0 || m.m44 != 1.0; + let is_3d_transform = m.m31 != 0.0 || m.m32 != 0.0 || m.m13 != 0.0 || + m.m23 != 0.0 || m.m43 != 0.0 || + m.m14 != 0.0 || m.m24 != 0.0 || + m.m34 != 0.0 || m.m33 != 1.0 || + m.m44 != 1.0; if is_3d_transform { None @@ -801,31 +925,26 @@ impl RenderContext { } }); - self.render_layer(layer.clone(), - transform, - projection, - clip_rect, - gfx_context); + self.render_layer(layer.clone(), transform, projection, clip_rect, gfx_context); } if let Some(ref context) = child.context { - self.render_3d_context(context, - transform, - projection, - gfx_context); + self.render_3d_context(context, transform, projection, gfx_context); } } } } -pub fn render_scene(root_layer: Rc>, - render_context: RenderContext, - scene: &Scene) { +pub fn render_scene(root_layer: Rc>, render_context: RenderContext, scene: &Scene) { // Set the viewport. let v = scene.viewport.to_untyped(); - gl::viewport(v.origin.x as GLint, v.origin.y as GLint, - v.size.width as GLsizei, v.size.height as GLsizei); + gl::viewport( + v.origin.x as GLint, + v.origin.y as GLint, + v.size.width as GLsizei, + v.size.height as GLsizei, + ); // Enable depth testing for 3d transforms. Set z-mode to LESS-EQUAL // so that layers with equal Z are able to paint correctly in @@ -840,8 +959,10 @@ pub fn render_scene(root_layer: Rc>, let projection = create_ortho(&scene.viewport.size.to_untyped()); // Build the list of render items - render_context.render_3d_context(&RenderContext3D::new(root_layer.clone()), - &transform, - &projection, - &render_context.compositing_display); + render_context.render_3d_context( + &RenderContext3D::new(root_layer.clone()), + &transform, + &projection, + &render_context.compositing_display, + ); } diff --git a/src/scene.rs b/src/scene.rs old mode 100755 new mode 100644 index f7158f2..eb8dc95 --- a/src/scene.rs +++ b/src/scene.rs @@ -32,13 +32,14 @@ impl Scene { } } - pub fn get_buffer_requests_for_layer(&mut self, - layer: Rc>, - dirty_rect: TypedRect, - viewport_rect: TypedRect, - layers_and_requests: &mut Vec<(Rc>, - Vec)>, - unused_buffers: &mut Vec>) { + pub fn get_buffer_requests_for_layer( + &mut self, + layer: Rc>, + dirty_rect: TypedRect, + viewport_rect: TypedRect, + layers_and_requests: &mut Vec<(Rc>, Vec)>, + unused_buffers: &mut Vec>, + ) { // Get buffers for this layer, in global (screen) coordinates. let requests = layer.get_buffer_requests(dirty_rect, viewport_rect, self.scale); if !requests.is_empty() { @@ -57,33 +58,39 @@ impl Scene { Some(ref child_dirty_rect) => TypedRect::from_untyped(child_dirty_rect), None => return, // The layer is entirely outside the dirty rect. } - }, + } None => return, // The layer is entirely clipped. } }; for kid in layer.children().iter() { - self.get_buffer_requests_for_layer(kid.clone(), - child_dirty_rect, - viewport_rect, - layers_and_requests, - unused_buffers); + self.get_buffer_requests_for_layer( + kid.clone(), + child_dirty_rect, + viewport_rect, + layers_and_requests, + unused_buffers, + ); } } - pub fn get_buffer_requests(&mut self, - requests: &mut Vec<(Rc>, Vec)>, - unused_buffers: &mut Vec>) { + pub fn get_buffer_requests( + &mut self, + requests: &mut Vec<(Rc>, Vec)>, + unused_buffers: &mut Vec>, + ) { let root_layer = match self.root { Some(ref root_layer) => root_layer.clone(), None => return, }; - self.get_buffer_requests_for_layer(root_layer.clone(), - *root_layer.bounds.borrow(), - *root_layer.bounds.borrow(), - requests, - unused_buffers); + self.get_buffer_requests_for_layer( + root_layer.clone(), + *root_layer.bounds.borrow(), + *root_layer.bounds.borrow(), + requests, + unused_buffers, + ); } pub fn mark_layer_contents_as_changed_recursively_for_layer(&self, layer: Rc>) { @@ -103,8 +110,8 @@ impl Scene { pub fn set_root_layer_size(&self, new_size: TypedSize2D) { if let Some(ref root_layer) = self.root { - *root_layer.bounds.borrow_mut() = TypedRect::new(TypedPoint2D::zero(), - new_size / self.scale); + *root_layer.bounds.borrow_mut() = + TypedRect::new(TypedPoint2D::zero(), new_size / self.scale); } } diff --git a/src/texturegl.rs b/src/texturegl.rs index a91d1ff..805e004 100644 --- a/src/texturegl.rs +++ b/src/texturegl.rs @@ -18,7 +18,7 @@ use gleam::gl::{GLenum, GLint, GLuint}; #[derive(Copy, Clone)] pub enum Format { ARGB32Format, - RGB24Format + RGB24Format, } #[cfg(feature = "heapsize")] @@ -27,7 +27,7 @@ known_heap_size!(0, Format); #[derive(Copy, Clone)] pub enum FilterMode { Nearest, - Linear + Linear, } #[cfg(feature = "heapsize")] @@ -46,7 +46,6 @@ pub enum TextureTarget { known_heap_size!(0, TextureTarget); impl TextureTarget { - #[cfg(not(target_os = "android"))] pub fn as_gl_target(self) -> GLenum { match self { @@ -59,7 +58,9 @@ impl TextureTarget { pub fn as_gl_target(self) -> GLenum { match self { TextureTarget::TextureTarget2D => gl::TEXTURE_2D, - TextureTarget::TextureTargetRectangle => panic!("android doesn't supported rectangle targets"), + TextureTarget::TextureTargetRectangle => { + panic!("android doesn't supported rectangle targets") + } } } } @@ -82,13 +83,13 @@ pub struct Texture { pub flip: Flip, // The size of this texture in device pixels. - pub size: Size2D + pub size: Size2D, } impl Drop for Texture { fn drop(&mut self) { if !self.weak { - gl::delete_textures(&[ self.id ]) + gl::delete_textures(&[self.id]) } } } @@ -111,7 +112,7 @@ impl Texture { /// Encapsulates a bound texture. This ensures that the texture is unbound /// properly. pub struct BoundTexture { - pub target: TextureTarget + pub target: TextureTarget, } impl Drop for BoundTexture { @@ -142,7 +143,7 @@ impl Texture { } // Returns whether the layer should be vertically flipped. - #[cfg(target_os="macos")] + #[cfg(target_os = "macos")] pub fn texture_flip_and_target(cpu_painting: bool) -> (Flip, TextureTarget) { let flip = if cpu_painting { Flip::NoFlip @@ -153,7 +154,7 @@ impl Texture { (flip, TextureTarget::TextureTargetRectangle) } - #[cfg(target_os="android")] + #[cfg(target_os = "android")] pub fn texture_flip_and_target(cpu_painting: bool) -> (Flip, TextureTarget) { let flip = if cpu_painting { Flip::NoFlip @@ -164,12 +165,12 @@ impl Texture { (flip, TextureTarget::TextureTarget2D) } - #[cfg(target_os="linux")] + #[cfg(target_os = "linux")] pub fn texture_flip_and_target(_: bool) -> (Flip, TextureTarget) { (Flip::NoFlip, TextureTarget::TextureTarget2D) } - #[cfg(target_os="windows")] + #[cfg(target_os = "windows")] pub fn texture_flip_and_target(_: bool) -> (Flip, TextureTarget) { (Flip::NoFlip, TextureTarget::TextureTarget2D) } @@ -182,10 +183,26 @@ impl Texture { /// Sets default parameters for this texture. fn set_default_params(&self) { let _bound_texture = self.bind(); - gl::tex_parameter_i(self.target.as_gl_target(), gl::TEXTURE_MAG_FILTER, gl::LINEAR as GLint); - gl::tex_parameter_i(self.target.as_gl_target(), gl::TEXTURE_MIN_FILTER, gl::LINEAR as GLint); - gl::tex_parameter_i(self.target.as_gl_target(), gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE as GLint); - gl::tex_parameter_i(self.target.as_gl_target(), gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE as GLint); + gl::tex_parameter_i( + self.target.as_gl_target(), + gl::TEXTURE_MAG_FILTER, + gl::LINEAR as GLint, + ); + gl::tex_parameter_i( + self.target.as_gl_target(), + gl::TEXTURE_MIN_FILTER, + gl::LINEAR as GLint, + ); + gl::tex_parameter_i( + self.target.as_gl_target(), + gl::TEXTURE_WRAP_S, + gl::CLAMP_TO_EDGE as GLint, + ); + gl::tex_parameter_i( + self.target.as_gl_target(), + gl::TEXTURE_WRAP_T, + gl::CLAMP_TO_EDGE as GLint, + ); } /// Sets the filter mode for this texture. @@ -203,9 +220,7 @@ impl Texture { pub fn bind(&self) -> BoundTexture { gl::bind_texture(self.target.as_gl_target(), self.id); - BoundTexture { - target: self.target, - } + BoundTexture { target: self.target } } } diff --git a/src/tiling.rs b/src/tiling.rs index 4f3f3fc..66f82e5 100644 --- a/src/tiling.rs +++ b/src/tiling.rs @@ -75,9 +75,14 @@ impl Tile { // Make a new texture and bind the LayerBuffer's surface to it. self.texture = Texture::new_with_buffer(buffer); - debug!("Tile: binding to native surface {}", - buffer.native_surface.get_id() as isize); - buffer.native_surface.bind_to_texture(display, &self.texture); + debug!( + "Tile: binding to native surface {}", + buffer.native_surface.get_id() as isize + ); + buffer.native_surface.bind_to_texture( + display, + &self.texture, + ); // Set the layer's rect. self.bounds = Some(TypedRect::from_untyped(&buffer.rect)); @@ -111,8 +116,10 @@ pub struct TileGrid { } pub fn rect_uint_as_rect_f32(rect: Rect) -> Rect { - TypedRect::new(Point2D::new(rect.origin.x as f32, rect.origin.y as f32), - Size2D::new(rect.size.width as f32, rect.size.height as f32)) + TypedRect::new( + Point2D::new(rect.origin.x as f32, rect.origin.y as f32), + Size2D::new(rect.size.width as f32, rect.size.height as f32), + ) } impl TileGrid { @@ -124,19 +131,23 @@ impl TileGrid { } } - pub fn get_rect_for_tile_index(&self, - tile_index: Point2D, - current_layer_size: TypedSize2D) - -> TypedRect { + pub fn get_rect_for_tile_index( + &self, + tile_index: Point2D, + current_layer_size: TypedSize2D, + ) -> TypedRect { - let origin : TypedPoint2D = - TypedPoint2D::new(self.tile_size.get() * tile_index.x, - self.tile_size.get() * tile_index.y); + let origin: TypedPoint2D = TypedPoint2D::new( + self.tile_size.get() * tile_index.x, + self.tile_size.get() * tile_index.y, + ); // Don't let tiles extend beyond the layer boundaries. let tile_size = self.tile_size.get() as f32; - let size = Size2D::new(tile_size.min(current_layer_size.width - origin.x as f32), - tile_size.min(current_layer_size.height - origin.y as f32)); + let size = Size2D::new( + tile_size.min(current_layer_size.width - origin.x as f32), + tile_size.min(current_layer_size.height - origin.y as f32), + ); // Round up to texture pixels. let size = TypedSize2D::new(size.width.ceil() as usize, size.height.ceil() as usize); @@ -156,17 +167,18 @@ impl TileGrid { } } - pub fn tile_intersects_rect(&self, - tile_index: &Point2D, - test_rect: &Rect, - current_layer_size: TypedSize2D, - layer_world_origin: &Point2D, - layer_transform: &Matrix4D) -> bool { - let tile_rect = self.get_rect_for_tile_index(*tile_index, - current_layer_size); - let tile_rect = tile_rect.to_f32() - .to_untyped() - .translate(layer_world_origin); + pub fn tile_intersects_rect( + &self, + tile_index: &Point2D, + test_rect: &Rect, + current_layer_size: TypedSize2D, + layer_world_origin: &Point2D, + layer_transform: &Matrix4D, + ) -> bool { + let tile_rect = self.get_rect_for_tile_index(*tile_index, current_layer_size); + let tile_rect = tile_rect.to_f32().to_untyped().translate( + layer_world_origin, + ); let screen_rect = project_rect_to_screen(&tile_rect, layer_transform); @@ -179,19 +191,24 @@ impl TileGrid { false } - pub fn mark_tiles_outside_of_rect_as_unused(&mut self, - rect: TypedRect, - layer_world_origin: &Point2D, - layer_transform: &Matrix4D, - current_layer_size: TypedSize2D) { + pub fn mark_tiles_outside_of_rect_as_unused( + &mut self, + rect: TypedRect, + layer_world_origin: &Point2D, + layer_transform: &Matrix4D, + current_layer_size: TypedSize2D, + ) { let mut tile_indexes_to_take = Vec::new(); for tile_index in self.tiles.keys() { - if !self.tile_intersects_rect(tile_index, - &rect.to_untyped(), - current_layer_size, - layer_world_origin, - layer_transform) { + if !self.tile_intersects_rect( + tile_index, + &rect.to_untyped(), + current_layer_size, + layer_world_origin, + layer_transform, + ) + { tile_indexes_to_take.push(tile_index.clone()); } } @@ -203,11 +220,12 @@ impl TileGrid { } } - pub fn get_buffer_request_for_tile(&mut self, - tile_index: Point2D, - current_layer_size: TypedSize2D, - current_content_age: ContentAge) - -> Option { + pub fn get_buffer_request_for_tile( + &mut self, + tile_index: Point2D, + current_layer_size: TypedSize2D, + current_content_age: ContentAge, + ) -> Option { let tile_rect = self.get_rect_for_tile_index(tile_index, current_layer_size); let tile = match self.tiles.entry(tile_index) { Entry::Occupied(occupied) => occupied.into_mut(), @@ -224,21 +242,24 @@ impl TileGrid { tile.content_age_of_pending_buffer = Some(current_content_age); - Some(BufferRequest::new(tile_rect.to_untyped(), - tile_rect.to_f32().to_untyped(), - current_content_age)) + Some(BufferRequest::new( + tile_rect.to_untyped(), + tile_rect.to_f32().to_untyped(), + current_content_age, + )) } /// Returns buffer requests inside the given dirty rect, and simultaneously throws out tiles /// outside the given viewport rect. - pub fn get_buffer_requests_in_rect(&mut self, - dirty_rect: TypedRect, - viewport: TypedRect, - current_layer_size: TypedSize2D, - layer_world_origin: &Point2D, - layer_transform: &Matrix4D, - current_content_age: ContentAge) - -> Vec { + pub fn get_buffer_requests_in_rect( + &mut self, + dirty_rect: TypedRect, + viewport: TypedRect, + current_layer_size: TypedSize2D, + layer_world_origin: &Point2D, + layer_transform: &Matrix4D, + current_content_age: ContentAge, + ) -> Vec { let mut buffer_requests = Vec::new(); // Get the range of tiles that can fit into the current layer size. @@ -246,30 +267,40 @@ impl TileGrid { // Check if visible against rect let tile_size = self.tile_size.get() as f32; - let x_tile_count = ((current_layer_size.to_untyped().width + tile_size - 1.0) / tile_size) as usize; - let y_tile_count = ((current_layer_size.to_untyped().height + tile_size - 1.0) / tile_size) as usize; + let x_tile_count = ((current_layer_size.to_untyped().width + tile_size - 1.0) / + tile_size) as usize; + let y_tile_count = ((current_layer_size.to_untyped().height + tile_size - 1.0) / + tile_size) as usize; for x in 0..x_tile_count { for y in 0..y_tile_count { let tile_index = Point2D::new(x, y); - if self.tile_intersects_rect(&tile_index, - &dirty_rect.to_untyped(), - current_layer_size, - layer_world_origin, - layer_transform) { - if let Some(buffer) = self.get_buffer_request_for_tile(tile_index, - current_layer_size, - current_content_age) { + if self.tile_intersects_rect( + &tile_index, + &dirty_rect.to_untyped(), + current_layer_size, + layer_world_origin, + layer_transform, + ) + { + if let Some(buffer) = self.get_buffer_request_for_tile( + tile_index, + current_layer_size, + current_content_age, + ) + { buffer_requests.push(buffer); } } } } - self.mark_tiles_outside_of_rect_as_unused(viewport, - layer_world_origin, - layer_transform, - current_layer_size); + self.mark_tiles_outside_of_rect_as_unused( + viewport, + layer_world_origin, + layer_transform, + current_layer_size, + ); buffer_requests } @@ -277,8 +308,10 @@ impl TileGrid { pub fn get_tile_index_for_point(&self, point: Point2D) -> Point2D { assert!(point.x % self.tile_size.get() == 0); assert!(point.y % self.tile_size.get() == 0); - Point2D::new((point.x / self.tile_size.get()) as usize, - (point.y / self.tile_size.get()) as usize) + Point2D::new( + (point.x / self.tile_size.get()) as usize, + (point.y / self.tile_size.get()) as usize, + ) } pub fn add_buffer(&mut self, buffer: Box) { @@ -293,7 +326,10 @@ impl TileGrid { self.add_unused_buffer(replaced_buffer); } - pub fn do_for_all_tiles(&self, mut f: F) where F: FnMut(&Tile) { + pub fn do_for_all_tiles(&self, mut f: F) + where + F: FnMut(&Tile), + { for tile in self.tiles.values() { f(tile); } @@ -301,7 +337,9 @@ impl TileGrid { pub fn collect_buffers(&mut self) -> Vec> { let mut collected_buffers = self.take_unused_buffers(); - collected_buffers.extend(self.tiles.drain().flat_map(|(_, mut tile)| tile.buffer.take())); + collected_buffers.extend(self.tiles.drain().flat_map( + |(_, mut tile)| tile.buffer.take(), + )); collected_buffers } @@ -314,13 +352,16 @@ impl TileGrid { /// Calculate the amount of memory used by all the tiles in the /// tile grid. The memory may be allocated on the heap or in GPU memory. pub fn get_memory_usage(&self) -> usize { - self.tiles.values().map(|ref tile| { - // We cannot use Option::map_or here because rust will - // complain about moving out of borrowed content. - match tile.buffer { - Some(ref buffer) => buffer.get_mem(), - None => 0, - } - }).sum() + self.tiles + .values() + .map(|ref tile| { + // We cannot use Option::map_or here because rust will + // complain about moving out of borrowed content. + match tile.buffer { + Some(ref buffer) => buffer.get_mem(), + None => 0, + } + }) + .sum() } } diff --git a/src/util.rs b/src/util.rs index 56769b3..5ba3b13 100644 --- a/src/util.rs +++ b/src/util.rs @@ -26,30 +26,25 @@ known_heap_size!(0, ScreenRect); pub fn convert_rgb32_to_rgb24(buffer: &[u8]) -> Vec { let mut i = 0; - repeat(buffer.len() * 3 / 4).map(|j| { - match j % 3 { - 0 => { - buffer[i + 2] - } - 1 => { - buffer[i + 1] - } + repeat(buffer.len() * 3 / 4) + .map(|j| match j % 3 { + 0 => buffer[i + 2], + 1 => buffer[i + 1], 2 => { let val = buffer[i]; i += 4; val } - _ => { - panic!() - } - } - }).collect() + _ => panic!(), + }) + .collect() } // Sutherland-Hodgman clipping algorithm -fn clip_polygon_to_near_plane(clip_space_vertices: &[Point4D; 4]) - -> Option>> { - let mut out_vertices = vec!(); +fn clip_polygon_to_near_plane( + clip_space_vertices: &[Point4D; 4], +) -> Option>> { + let mut out_vertices = vec![]; // TODO(gw): Check for trivial accept / reject if all // input vertices are on the same side of the near plane. @@ -58,14 +53,23 @@ fn clip_polygon_to_near_plane(clip_space_vertices: &[Point4D; 4]) let previous_vertex = if i == 0 { clip_space_vertices.last().unwrap() } else { - &clip_space_vertices[i-1] + &clip_space_vertices[i - 1] }; - let previous_dot = if previous_vertex.w < W_CLIPPING_PLANE { -1 } else { 1 }; - let current_dot = if current_vertex.w < W_CLIPPING_PLANE { -1 } else { 1 }; + let previous_dot = if previous_vertex.w < W_CLIPPING_PLANE { + -1 + } else { + 1 + }; + let current_dot = if current_vertex.w < W_CLIPPING_PLANE { + -1 + } else { + 1 + }; if previous_dot * current_dot < 0 { - let int_factor = (previous_vertex.w - W_CLIPPING_PLANE) / (previous_vertex.w - current_vertex.w); + let int_factor = (previous_vertex.w - W_CLIPPING_PLANE) / + (previous_vertex.w - current_vertex.w); // TODO(gw): Impl operators on Point4D for this let int_point = Point4D::new( @@ -84,14 +88,13 @@ fn clip_polygon_to_near_plane(clip_space_vertices: &[Point4D; 4]) } if out_vertices.len() < 3 { - return None + return None; } Some(out_vertices) } -pub fn project_rect_to_screen(rect: &Rect, - transform: &Matrix4D) -> Option { +pub fn project_rect_to_screen(rect: &Rect, transform: &Matrix4D) -> Option { let mut result = None; let x0 = rect.min_x(); @@ -109,7 +112,7 @@ pub fn project_rect_to_screen(rect: &Rect, Point4D::new(x0, y0, 0.0, 1.0), Point4D::new(x1, y0, 0.0, 1.0), Point4D::new(x0, y1, 0.0, 1.0), - Point4D::new(x1, y1, 0.0, 1.0) + Point4D::new(x1, y1, 0.0, 1.0), ]; // Transform vertices to clip space @@ -153,8 +156,7 @@ pub fn project_rect_to_screen(rect: &Rect, } let origin = Point2D::new(min_vertex.x, min_vertex.y); - let size = Size2D::new(max_vertex.x - min_vertex.x, - max_vertex.y - min_vertex.y); + let size = Size2D::new(max_vertex.x - min_vertex.x, max_vertex.y - min_vertex.y); result = Some(ScreenRect { rect: Rect::new(origin, size),