From 51582307a34a1ef9302900ced8fcc3617973777a Mon Sep 17 00:00:00 2001 From: Martin Robinson Date: Tue, 1 Jul 2014 18:19:14 -0700 Subject: [PATCH 1/3] Remove CommonLayer and unused ContainerLayer methods CommonLayer is only used by ContainerLayer, so it can be moved directly into ContainerLayer. A vector can also be used for storing children instead of an embedded linked-list since the operations we do on the list of children are still very limited. --- layers.rs | 164 ++++------------------------------------------------ rendergl.rs | 6 +- 2 files changed, 14 insertions(+), 156 deletions(-) diff --git a/layers.rs b/layers.rs index 1211e31..b467afc 100644 --- a/layers.rs +++ b/layers.rs @@ -27,67 +27,21 @@ pub enum Format { RGB24Format } -pub struct CommonLayer { - pub parent: Option>>, - pub prev_sibling: Option>>, - pub next_sibling: Option>>, - - pub transform: Matrix4, - pub origin: Point2D, -} - -impl CommonLayer { - // FIXME: Workaround for cross-crate bug regarding mutability of class fields - pub fn set_transform(&mut self, new_transform: Matrix4) { - self.transform = new_transform; - } - - pub fn new() -> CommonLayer { - CommonLayer { - parent: None, - prev_sibling: None, - next_sibling: None, - transform: identity(), - origin: Zero::zero(), - } - } -} - pub struct ContainerLayer { - pub common: RefCell>, - pub first_child: RefCell>>>, - pub last_child: RefCell>>>, + pub children: RefCell>>>, pub tiles: RefCell>>, pub quadtree: RefCell, - + pub transform: RefCell>, + pub origin: RefCell>, tile_size: uint, pub extra_data: RefCell, } -pub struct ChildIterator { - current: Option>>, -} - -impl Iterator>> for ChildIterator { - fn next(&mut self) -> Option>> { - let (new_current, result) = - match self.current { - None => (None, None), - Some(ref child) => { - (child.common().next_sibling.clone(), Some(child.clone())) - } - }; - self.current = new_current; - result - } -} - impl ContainerLayer { pub fn new(page_size: Option>, tile_size: uint, data: T) -> ContainerLayer { ContainerLayer { - common: RefCell::new(CommonLayer::new()), - first_child: RefCell::new(None), - last_child: RefCell::new(None), + children: RefCell::new(vec!()), + tiles: RefCell::new(vec!()), quadtree: match page_size { None => { RefCell::new(Quadtree::new(Size2D(tile_size, tile_size), @@ -100,115 +54,19 @@ impl ContainerLayer { Some(MAX_TILE_MEMORY_PER_LAYER))) } }, - tiles: RefCell::new(vec!()), + transform: RefCell::new(identity()), + origin: RefCell::new(Zero::zero()), tile_size: tile_size, extra_data: RefCell::new(data), } } - pub fn children(&self) -> ChildIterator { - ChildIterator { - current: self.first_child.borrow().clone(), - } - } - - pub fn common<'a>(&'a self) -> RefMut<'a,CommonLayer> { - self.common.borrow_mut() - } - - /// Adds a child to the beginning of the list. - /// Only works when the child is disconnected from the layer tree. - pub fn add_child_start(this: Rc>, new_child: Rc>) { - let mut new_child_common = new_child.common(); - assert!(new_child_common.parent.is_none()); - assert!(new_child_common.prev_sibling.is_none()); - assert!(new_child_common.next_sibling.is_none()); - - new_child_common.parent = Some(this.clone()); - - match *this.first_child.borrow() { - None => {} - Some(ref first_child) => { - let mut first_child_common = first_child.common(); - assert!(first_child_common.prev_sibling.is_none()); - first_child_common.prev_sibling = Some(new_child.clone()); - new_child_common.next_sibling = Some(first_child.clone()); - } - } - - *this.first_child.borrow_mut() = Some(new_child.clone()); - - let should_set = this.last_child.borrow().is_none(); - if should_set { - *this.last_child.borrow_mut() = Some(new_child.clone()); - } - } - - /// Adds a child to the end of the list. - /// Only works when the child is disconnected from the layer tree. - pub fn add_child_end(this: Rc>, new_child: Rc>) { - let mut new_child_common = new_child.common(); - assert!(new_child_common.parent.is_none()); - assert!(new_child_common.prev_sibling.is_none()); - assert!(new_child_common.next_sibling.is_none()); - - new_child_common.parent = Some(this.clone()); - - - match *this.last_child.borrow() { - None => {} - Some(ref last_child) => { - let mut last_child_common = last_child.common(); - assert!(last_child_common.next_sibling.is_none()); - last_child_common.next_sibling = Some(new_child.clone()); - new_child_common.prev_sibling = Some(last_child.clone()); - } - } - - *this.last_child.borrow_mut() = Some(new_child.clone()); - - let mut child = this.first_child.borrow_mut(); - match *child { - Some(_) => {}, - None => *child = Some(new_child.clone()), - } - } - - pub fn remove_child(this: Rc>, child: Rc>) { - let mut child_common = child.common(); - assert!(child_common.parent.is_some()); - match child_common.parent { - Some(ref container) => { - assert!(container.deref() as *ContainerLayer == - this.deref() as *ContainerLayer); - }, - _ => fail!("Invalid parent of child in layer tree"), - } - - let previous_sibling = child_common.prev_sibling.clone(); - match child_common.next_sibling { - None => { // this is the last child - *this.last_child.borrow_mut() = previous_sibling; - }, - Some(ref sibling) => { - sibling.common().prev_sibling = previous_sibling; - } - } - - let next_sibling = child_common.next_sibling.clone(); - match child_common.prev_sibling { - None => { // this is the first child - *this.first_child.borrow_mut() = next_sibling; - }, - Some(ref sibling) => { - sibling.common().next_sibling = next_sibling; - } - } + pub fn children<'a>(&'a self) -> RefMut<'a,Vec>>> { + self.children.borrow_mut() } - pub fn remove_all_children(&self) { - *self.first_child.borrow_mut() = None; - *self.last_child.borrow_mut() = None; + pub fn add_child(this: Rc>, new_child: Rc>) { + this.children().push(new_child); } pub fn tile_size(this: Rc>) -> uint { diff --git a/rendergl.rs b/rendergl.rs index d588133..31cfd6a 100755 --- a/rendergl.rs +++ b/rendergl.rs @@ -367,12 +367,12 @@ impl Render for layers::ContainerLayer { render_context: RenderContext, transform: Matrix4, scene_size: Size2D) { - let tmp = self.common.borrow(); - let transform = transform.translate(tmp.origin.x, tmp.origin.y, 0.0).mul(&tmp.transform); + let origin = self.origin.borrow(); + let transform = transform.translate(origin.x, origin.y, 0.0).mul(&*self.transform.borrow()); for tile in self.tiles.borrow().iter() { tile.render(render_context, transform, scene_size) } - for child in self.children() { + for child in self.children().iter() { child.render(render_context, transform, scene_size) } } From ad12d195fb6cebd9a7b83f21d793f480382c7e6a Mon Sep 17 00:00:00 2001 From: Martin Robinson Date: Tue, 1 Jul 2014 19:03:36 -0700 Subject: [PATCH 2/3] Rename ContainerLayer to Layer --- layers.rs | 28 ++++++++++++++-------------- rendergl.rs | 6 +++--- scene.rs | 4 ++-- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/layers.rs b/layers.rs index b467afc..ce79d28 100644 --- a/layers.rs +++ b/layers.rs @@ -27,8 +27,8 @@ pub enum Format { RGB24Format } -pub struct ContainerLayer { - pub children: RefCell>>>, +pub struct Layer { + pub children: RefCell>>>, pub tiles: RefCell>>, pub quadtree: RefCell, pub transform: RefCell>, @@ -37,9 +37,9 @@ pub struct ContainerLayer { pub extra_data: RefCell, } -impl ContainerLayer { - pub fn new(page_size: Option>, tile_size: uint, data: T) -> ContainerLayer { - ContainerLayer { +impl Layer { + pub fn new(page_size: Option>, tile_size: uint, data: T) -> Layer { + Layer { children: RefCell::new(vec!()), tiles: RefCell::new(vec!()), quadtree: match page_size { @@ -61,41 +61,41 @@ impl ContainerLayer { } } - pub fn children<'a>(&'a self) -> RefMut<'a,Vec>>> { + pub fn children<'a>(&'a self) -> RefMut<'a,Vec>>> { self.children.borrow_mut() } - pub fn add_child(this: Rc>, new_child: Rc>) { + pub fn add_child(this: Rc>, new_child: Rc>) { this.children().push(new_child); } - pub fn tile_size(this: Rc>) -> uint { + pub fn tile_size(this: Rc>) -> uint { this.tile_size } - pub fn get_tile_rects_page(this: Rc>, window: Rect, scale: f32) -> (Vec, Vec>) { + pub fn get_tile_rects_page(this: Rc>, window: Rect, scale: f32) -> (Vec, Vec>) { this.quadtree.borrow_mut().get_tile_rects_page(window, scale) } - pub fn set_status_page(this: Rc>, rect: Rect, status: NodeStatus, include_border: bool) { + pub fn set_status_page(this: Rc>, rect: Rect, status: NodeStatus, include_border: bool) { this.quadtree.borrow_mut().set_status_page(rect, Normal, false); // Rect is unhidden } - pub fn resize(this: Rc>, new_size: Size2D) -> Vec> { + pub fn resize(this: Rc>, new_size: Size2D) -> Vec> { this.quadtree.borrow_mut().resize(new_size.width as uint, new_size.height as uint) } - pub fn do_for_all_tiles(this: Rc>, f: |&Box|) { + pub fn do_for_all_tiles(this: Rc>, f: |&Box|) { this.quadtree.borrow_mut().do_for_all_tiles(f); } - pub fn add_tile_pixel(this: Rc>, tile: Box) -> Vec> { + pub fn add_tile_pixel(this: Rc>, tile: Box) -> Vec> { this.quadtree.borrow_mut().add_tile_pixel(tile.screen_pos.origin.x, tile.screen_pos.origin.y, tile.resolution, tile) } - pub fn collect_tiles(this: Rc>) -> Vec> { + pub fn collect_tiles(this: Rc>) -> Vec> { this.quadtree.borrow_mut().collect_tiles() } } diff --git a/rendergl.rs b/rendergl.rs index 31cfd6a..a11095f 100755 --- a/rendergl.rs +++ b/rendergl.rs @@ -7,7 +7,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use layers::{ContainerLayer, TextureLayer, Flip, NoFlip, VerticalFlip}; +use layers::{Layer, TextureLayer, Flip, NoFlip, VerticalFlip}; use layers; use scene::Scene; use texturegl::{Texture, TextureTarget2D, TextureTargetRectangle}; @@ -362,7 +362,7 @@ pub trait Render { scene_size: Size2D); } -impl Render for layers::ContainerLayer { +impl Render for layers::Layer { fn render(&self, render_context: RenderContext, transform: Matrix4, @@ -388,7 +388,7 @@ impl Render for layers::TextureLayer { } } -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. viewport(0 as GLint, 0 as GLint, scene.size.width as GLsizei, scene.size.height as GLsizei); diff --git a/scene.rs b/scene.rs index c75fb07..607ca78 100755 --- a/scene.rs +++ b/scene.rs @@ -10,11 +10,11 @@ use color::Color; use geom::size::Size2D; use geom::matrix::Matrix4; -use layers::ContainerLayer; +use layers::Layer; use std::rc::Rc; pub struct Scene { - pub root: Option>>, + pub root: Option>>, pub size: Size2D, pub transform: Matrix4, pub background_color: Color From ee9f50cdf1ba8f8e79909aaaf24b644648d78589 Mon Sep 17 00:00:00 2001 From: Martin Robinson Date: Wed, 2 Jul 2014 08:10:07 -0700 Subject: [PATCH 3/3] No longer accept an Option for layer size This is currently unused in Servo. --- layers.rs | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/layers.rs b/layers.rs index ce79d28..4f81d8b 100644 --- a/layers.rs +++ b/layers.rs @@ -38,22 +38,12 @@ pub struct Layer { } impl Layer { - pub fn new(page_size: Option>, tile_size: uint, data: T) -> Layer { + pub fn new(page_size: Size2D, tile_size: uint, data: T) -> Layer { Layer { children: RefCell::new(vec!()), tiles: RefCell::new(vec!()), - quadtree: match page_size { - None => { - RefCell::new(Quadtree::new(Size2D(tile_size, tile_size), - tile_size, - Some(MAX_TILE_MEMORY_PER_LAYER))) - } - Some(page_size) => { - RefCell::new(Quadtree::new(Size2D(page_size.width as uint, page_size.height as uint), - tile_size, - Some(MAX_TILE_MEMORY_PER_LAYER))) - } - }, + quadtree: RefCell::new(Quadtree::new(Size2D(page_size.width as uint, page_size.height as uint), + tile_size, Some(MAX_TILE_MEMORY_PER_LAYER))), transform: RefCell::new(identity()), origin: RefCell::new(Zero::zero()), tile_size: tile_size,