diff --git a/src/geometry.rs b/src/geometry.rs new file mode 100644 index 0000000..e146b9c --- /dev/null +++ b/src/geometry.rs @@ -0,0 +1,18 @@ +// Copyright 2014 The Servo Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Units for use with geom::length and geom::scale_factor. + +/// One hardware pixel. +/// +/// This unit corresponds to the smallest addressable element of the display hardware. +#[deriving(Encodable)] +pub enum DevicePixel {} + + diff --git a/src/layers.rs b/src/layers.rs index 9c81d6f..1a7b479 100644 --- a/src/layers.rs +++ b/src/layers.rs @@ -7,12 +7,13 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use geometry::DevicePixel; use tiling::{Tile, TileGrid}; use geom::matrix::{Matrix4, identity}; -use geom::size::Size2D; -use geom::point::Point2D; -use geom::rect::Rect; +use geom::size::{Size2D, TypedSize2D}; +use geom::point::TypedPoint2D; +use geom::rect::{Rect, TypedRect}; use platform::surface::{NativeSurfaceMethods, NativeSurface}; use platform::surface::{NativeCompositingGraphicsContext, NativePaintingGraphicsContext}; use std::cell::{RefCell, RefMut}; @@ -38,20 +39,22 @@ impl ContentAge { pub struct Layer { pub children: RefCell>>>, pub transform: RefCell>, - pub bounds: RefCell>, pub tile_size: uint, pub extra_data: RefCell, tile_grid: RefCell, + /// The boundaries of this layer in the coordinate system of the parent layer. + pub bounds: RefCell>, + /// A monotonically increasing counter that keeps track of the current content age. pub content_age: RefCell, /// The content offset for this layer in device pixels. - pub content_offset: RefCell>, + pub content_offset: RefCell>, } impl Layer { - pub fn new(bounds: Rect, tile_size: uint, data: T) -> Layer { + pub fn new(bounds: TypedRect, tile_size: uint, data: T) -> Layer { Layer { children: RefCell::new(vec!()), transform: RefCell::new(identity()), @@ -60,7 +63,7 @@ impl Layer { extra_data: RefCell::new(data), tile_grid: RefCell::new(TileGrid::new(tile_size)), content_age: RefCell::new(ContentAge::new()), - content_offset: RefCell::new(Point2D(0f32, 0f32)), + content_offset: RefCell::new(TypedPoint2D(0f32, 0f32)), } } @@ -72,12 +75,12 @@ impl Layer { self.children().push(new_child); } - pub fn get_buffer_requests(&self, rect_in_layer: Rect) -> Vec { + pub fn get_buffer_requests(&self, rect_in_layer: TypedRect) -> Vec { let mut tile_grid = self.tile_grid.borrow_mut(); return tile_grid.get_buffer_requests_in_rect(rect_in_layer, *self.content_age.borrow()); } - pub fn resize(&self, new_size: Size2D) { + pub fn resize(&self, new_size: TypedSize2D) { self.bounds.borrow_mut().size = new_size; } diff --git a/src/lib.rs b/src/lib.rs index db5c1ff..f7c780e 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,13 +35,14 @@ extern crate xlib; #[cfg(target_os="android")] extern crate egl; -pub mod layers; pub mod color; +pub mod geometry; +pub mod layers; pub mod rendergl; pub mod scene; pub mod texturegl; -pub mod util; pub mod tiling; +pub mod util; pub mod platform { #[cfg(target_os="linux")] diff --git a/src/rendergl.rs b/src/rendergl.rs index 48b4035..5ed3200 100755 --- a/src/rendergl.rs +++ b/src/rendergl.rs @@ -495,7 +495,7 @@ impl Render for layers::Layer { render_context: RenderContext, transform: Matrix4, scene_size: Size2D) { - let bounds = self.bounds.borrow(); + let bounds = self.bounds.borrow().to_untyped(); let transform = transform.translate(bounds.origin.x, bounds.origin.y, 0.0) .mul(&*self.transform.borrow()); diff --git a/src/scene.rs b/src/scene.rs index 245b9dc..94989b5 100755 --- a/src/scene.rs +++ b/src/scene.rs @@ -10,8 +10,9 @@ use color::Color; use geom::matrix::Matrix4; use geom::point::Point2D; -use geom::rect::Rect; +use geom::rect::{Rect, TypedRect}; use geom::size::Size2D; +use geometry::DevicePixel; use layers::{BufferRequest, Layer, LayerBuffer}; use std::mem; use std::rc::Rc; @@ -44,16 +45,16 @@ impl Scene { layer: Rc>, layers_and_requests: &mut Vec<(Rc>, Vec)>, - rect_in_window: Rect) { + rect_in_window: TypedRect) { // The rectangle passed in is in the coordinate system of our parent, so we // need to intersect with our boundaries and convert it to our coordinate system. let content_offset = layer.content_offset.borrow(); - let layer_bounds = layer.bounds.borrow(); + let layer_bounds = layer.bounds.borrow().clone(); let layer_rect = Rect(Point2D(rect_in_window.origin.x - content_offset.x, rect_in_window.origin.y - content_offset.y), rect_in_window.size); - match layer_rect.intersection(&*layer_bounds) { + match layer_rect.intersection(&layer_bounds) { Some(mut intersected_rect) => { // Child layers act as if they are rendered at (0,0), so we // subtract the layer's (x,y) coords in its containing page @@ -79,15 +80,13 @@ impl Scene { pub fn get_buffer_requests(&mut self, requests: &mut Vec<(Rc>, Vec)>, - window_rect_in_device_pixels: Rect) { + window_rect: TypedRect) { let root_layer = match self.root { Some(ref root_layer) => root_layer.clone(), None => return, }; - self.get_buffer_requests_for_layer(root_layer.clone(), - requests, - window_rect_in_device_pixels); + self.get_buffer_requests_for_layer(root_layer.clone(), requests, window_rect); } pub fn collect_unused_buffers(&mut self) -> Vec> { diff --git a/src/tiling.rs b/src/tiling.rs index 75cbc35..f43aa77 100644 --- a/src/tiling.rs +++ b/src/tiling.rs @@ -7,14 +7,16 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use geometry::DevicePixel; use layers::{BufferRequest, ContentAge, LayerBuffer}; use platform::surface::{NativeCompositingGraphicsContext, NativeSurfaceMethods}; use texturegl::Texture; +use geom::length::Length; use geom::matrix::{Matrix4, identity}; use geom::point::Point2D; +use geom::rect::{Rect, TypedRect}; use geom::size::Size2D; -use geom::rect::Rect; use std::collections::hashmap::HashMap; use std::iter::range_inclusive; use std::mem; @@ -113,8 +115,8 @@ impl Tile { pub struct TileGrid { pub tiles: HashMap, Tile>, - // The size of tiles in this grid in device pixels. - tile_size: uint, + /// The size of tiles in this grid in device pixels. + tile_size: Length, // Buffers that are currently unused. unused_buffers: Vec>, @@ -129,21 +131,28 @@ impl TileGrid { pub fn new(tile_size: uint) -> TileGrid { TileGrid { tiles: HashMap::new(), - tile_size: tile_size, + tile_size: Length(tile_size), unused_buffers: Vec::new(), } } - pub fn get_tile_index_range_for_rect(&self, rect: Rect) -> (Point2D, Point2D) { - (Point2D((rect.origin.x / self.tile_size as f32) as uint, - (rect.origin.y / self.tile_size as f32) as uint), - Point2D(((rect.origin.x + rect.size.width) / self.tile_size as f32) as uint, - ((rect.origin.y + rect.size.height) / self.tile_size as f32) as uint)) + pub fn get_tile_index_range_for_rect(&self, + rect: TypedRect) + -> (Point2D, Point2D) { + let rect = rect.to_untyped(); + (Point2D((rect.origin.x / self.tile_size.get() as f32) as uint, + (rect.origin.y / self.tile_size.get() as f32) as uint), + Point2D(((rect.origin.x + rect.size.width) / self.tile_size.get() as f32) as uint, + ((rect.origin.y + rect.size.height) / self.tile_size.get() as f32) as uint)) } - pub fn get_rect_for_tile_index(&self, tile_index: Point2D) -> Rect { - Rect(Point2D(self.tile_size * tile_index.x, self.tile_size * tile_index.y), - Size2D(self.tile_size, self.tile_size)) + pub fn get_rect_for_tile_index(&self, + tile_index: Point2D) + -> TypedRect { + let tile_rect = Rect(Point2D(self.tile_size.get() * tile_index.x, + self.tile_size.get() * tile_index.y), + Size2D(self.tile_size.get(), self.tile_size.get())); + Rect::from_untyped(&tile_rect) } pub fn take_unused_buffers(&mut self) -> Vec> { @@ -159,10 +168,10 @@ impl TileGrid { } } - pub fn mark_tiles_outside_of_rect_as_unused(&mut self, rect: Rect) { + pub fn mark_tiles_outside_of_rect_as_unused(&mut self, rect: TypedRect) { let mut tile_indexes_to_take = Vec::new(); for tile_index in self.tiles.keys() { - if !rect_uint_as_rect_f32(self.get_rect_for_tile_index(*tile_index)).intersects(&rect) { + if !self.get_rect_for_tile_index(*tile_index).as_f32().intersects(&rect) { tile_indexes_to_take.push(tile_index.clone()); } } @@ -180,25 +189,25 @@ impl TileGrid { current_content_age: ContentAge) -> Option { let tile_rect = self.get_rect_for_tile_index(tile_index); - let tile_screen_rect = rect_uint_as_rect_f32(tile_rect); - let tile = self.tiles.find_or_insert_with(tile_index, |_| Tile::new()); if !tile.should_request_buffer(current_content_age) { return None; } tile.content_age_of_pending_buffer = Some(current_content_age); - return Some(BufferRequest::new(tile_rect, tile_screen_rect, current_content_age)); + + return Some(BufferRequest::new(tile_rect.to_untyped(), + tile_rect.as_f32().to_untyped(), + current_content_age)); } pub fn get_buffer_requests_in_rect(&mut self, - screen_rect: Rect, + rect_in_layer: TypedRect, current_content_age: ContentAge) -> Vec { let mut buffer_requests = Vec::new(); - let rect_in_layer_pixels = screen_rect; let (top_left_index, bottom_right_index) = - self.get_tile_index_range_for_rect(rect_in_layer_pixels); + self.get_tile_index_range_for_rect(rect_in_layer); for x in range_inclusive(top_left_index.x, bottom_right_index.x) { for y in range_inclusive(top_left_index.y, bottom_right_index.y) { @@ -209,15 +218,15 @@ impl TileGrid { } } - self.mark_tiles_outside_of_rect_as_unused(rect_in_layer_pixels); + self.mark_tiles_outside_of_rect_as_unused(rect_in_layer); return buffer_requests; } pub fn get_tile_index_for_point(&self, point: Point2D) -> Point2D { - assert!(point.x % self.tile_size == 0); - assert!(point.y % self.tile_size == 0); - Point2D((point.x / self.tile_size) as uint, - (point.y / self.tile_size) as uint) + assert!(point.x % self.tile_size.get() == 0); + assert!(point.y % self.tile_size.get() == 0); + Point2D((point.x / self.tile_size.get()) as uint, + (point.y / self.tile_size.get()) as uint) } pub fn add_buffer(&mut self, buffer: Box) {