Skip to content

Commit

Permalink
script: Move the layout_wrapper outside of script.
Browse files Browse the repository at this point in the history
This allows us to have ensure_data() and clear_data() functions on the TElement
trait, instead of hacking around it adding methods in random traits.

This also allows us to do some further cleanup, which I'd rather do in a
followup.
  • Loading branch information
emilio committed Jul 15, 2017
1 parent f9642b3 commit 9a933ef
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 161 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions components/layout/lib.rs
Expand Up @@ -14,7 +14,6 @@ extern crate atomic_refcell;
#[macro_use]
extern crate bitflags;
extern crate canvas_traits;
extern crate core;
extern crate euclid;
extern crate fnv;
extern crate gfx;
Expand Down Expand Up @@ -55,7 +54,7 @@ pub mod animation;
mod block;
pub mod construct;
pub mod context;
mod data;
pub mod data;
pub mod display_list_builder;
mod flex;
mod floats;
Expand Down
15 changes: 2 additions & 13 deletions components/layout/traversal.rs
Expand Up @@ -4,7 +4,6 @@

//! Traversals over the DOM and flow trees, running the layout computations.

use atomic_refcell::AtomicRefCell;
use construct::FlowConstructor;
use context::LayoutContext;
use display_list_builder::DisplayListBuildState;
Expand All @@ -13,13 +12,12 @@ use flow::{CAN_BE_FRAGMENTED, Flow, ImmutableFlowUtils, PostorderFlowTraversal};
use script_layout_interface::wrapper_traits::{LayoutNode, ThreadSafeLayoutNode};
use servo_config::opts;
use style::context::{SharedStyleContext, StyleContext};
use style::data::ElementData;
use style::dom::{NodeInfo, TElement, TNode};
use style::selector_parser::RestyleDamage;
use style::servo::restyle_damage::{BUBBLE_ISIZES, REFLOW, REFLOW_OUT_OF_FLOW, REPAINT};
use style::traversal::{DomTraversal, TraversalDriver, recalc_style_at};
use style::traversal::PerLevelTraversalData;
use wrapper::{GetRawData, LayoutNodeHelpers, LayoutNodeLayoutData};
use wrapper::{GetRawData, LayoutNodeLayoutData};
use wrapper::ThreadSafeLayoutNodeHelpers;

pub struct RecalcStyleAndConstructFlows<'a> {
Expand Down Expand Up @@ -59,7 +57,7 @@ impl<'a, E> DomTraversal<E> for RecalcStyleAndConstructFlows<'a>
context: &mut StyleContext<E>, node: E::ConcreteNode) {
// FIXME(pcwalton): Stop allocating here. Ideally this should just be
// done by the HTML parser.
node.initialize_data();
unsafe { node.initialize_data() };

if !node.is_text_node() {
let el = node.as_element().unwrap();
Expand All @@ -81,15 +79,6 @@ impl<'a, E> DomTraversal<E> for RecalcStyleAndConstructFlows<'a>
node.parent_node().unwrap().to_threadsafe().restyle_damage() != RestyleDamage::empty()
}

unsafe fn ensure_element_data(element: &E) -> &AtomicRefCell<ElementData> {
element.as_node().initialize_data();
element.get_data().unwrap()
}

unsafe fn clear_element_data(element: &E) {
element.as_node().clear_data();
}

fn shared_context(&self) -> &SharedStyleContext {
&self.context.style_context
}
Expand Down
34 changes: 1 addition & 33 deletions components/layout/wrapper.rs
Expand Up @@ -31,21 +31,13 @@
#![allow(unsafe_code)]

use atomic_refcell::{AtomicRef, AtomicRefMut};
use core::nonzero::NonZero;
use data::{LayoutData, LayoutDataFlags, StyleAndLayoutData};
use script_layout_interface::{OpaqueStyleAndLayoutData, StyleData};
use script_layout_interface::wrapper_traits::{LayoutNode, ThreadSafeLayoutElement, ThreadSafeLayoutNode};
use script_layout_interface::wrapper_traits::{ThreadSafeLayoutElement, ThreadSafeLayoutNode};
use script_layout_interface::wrapper_traits::GetLayoutData;
use style::computed_values::content::{self, ContentItem};
use style::dom::{NodeInfo, TNode};
use style::selector_parser::RestyleDamage;

pub unsafe fn drop_style_and_layout_data(data: OpaqueStyleAndLayoutData) {
let ptr: *mut StyleData = data.ptr.get();
let non_opaque: *mut StyleAndLayoutData = ptr as *mut _;
let _ = Box::from_raw(non_opaque);
}

pub trait LayoutNodeLayoutData {
/// Similar to borrow_data*, but returns the full PersistentLayoutData rather
/// than only the style::data::ElementData.
Expand Down Expand Up @@ -81,30 +73,6 @@ impl<T: GetLayoutData> GetRawData for T {
}
}

pub trait LayoutNodeHelpers {
fn initialize_data(&self);
fn clear_data(&self);
}

impl<T: LayoutNode> LayoutNodeHelpers for T {
fn initialize_data(&self) {
if self.get_raw_data().is_none() {
let ptr: *mut StyleAndLayoutData =
Box::into_raw(Box::new(StyleAndLayoutData::new()));
let opaque = OpaqueStyleAndLayoutData {
ptr: unsafe { NonZero::new(ptr as *mut StyleData) }
};
unsafe { self.init_style_and_layout_data(opaque) };
};
}

fn clear_data(&self) {
if self.get_raw_data().is_some() {
unsafe { drop_style_and_layout_data(self.take_style_and_layout_data()) };
}
}
}

pub trait ThreadSafeLayoutNodeHelpers {
/// Returns the layout data flags for this node.
fn flags(self) -> LayoutDataFlags;
Expand Down
3 changes: 3 additions & 0 deletions components/layout_thread/Cargo.toml
Expand Up @@ -11,11 +11,13 @@ path = "lib.rs"

[dependencies]
app_units = "0.5"
atomic_refcell = "0.1"
euclid = "0.15"
fnv = "1.0"
gfx = {path = "../gfx"}
gfx_traits = {path = "../gfx_traits"}
heapsize = "0.4"
html5ever = "0.18"
ipc-channel = "0.8"
layout = {path = "../layout"}
layout_traits = {path = "../layout_traits"}
Expand All @@ -25,6 +27,7 @@ msg = {path = "../msg"}
net_traits = {path = "../net_traits"}
parking_lot = {version = "0.4", features = ["nightly"]}
profile_traits = {path = "../profile_traits"}
range = {path = "../range"}
rayon = "0.8"
script = {path = "../script"}
script_layout_interface = {path = "../script_layout_interface"}
Expand Down
Expand Up @@ -30,21 +30,22 @@

#![allow(unsafe_code)]

use atomic_refcell::{AtomicRef, AtomicRefCell};
use dom::bindings::inheritance::{CharacterDataTypeId, ElementTypeId};
use dom::bindings::inheritance::{HTMLElementTypeId, NodeTypeId};
use dom::bindings::js::LayoutJS;
use dom::characterdata::LayoutCharacterDataHelpers;
use dom::document::{Document, LayoutDocumentHelpers, PendingRestyle};
use dom::element::{Element, LayoutElementHelpers, RawLayoutElementHelpers};
use dom::node::{CAN_BE_FRAGMENTED, DIRTY_ON_VIEWPORT_SIZE_CHANGE, HAS_DIRTY_DESCENDANTS, IS_IN_DOC};
use dom::node::{HANDLED_SNAPSHOT, HAS_SNAPSHOT};
use dom::node::{LayoutNodeHelpers, Node};
use dom::text::Text;
use atomic_refcell::{AtomicRef, AtomicRefMut, AtomicRefCell};
use core::nonzero::NonZero;
use gfx_traits::ByteIndex;
use html5ever::{LocalName, Namespace};
use layout::data::StyleAndLayoutData;
use layout::wrapper::GetRawData;
use msg::constellation_msg::{BrowsingContextId, PipelineId};
use range::Range;
use script::layout_exports::{CAN_BE_FRAGMENTED, DIRTY_ON_VIEWPORT_SIZE_CHANGE, HAS_DIRTY_DESCENDANTS, IS_IN_DOC};
use script::layout_exports::{CharacterDataTypeId, ElementTypeId, HTMLElementTypeId, NodeTypeId};
use script::layout_exports::{Document, Element, Node, Text};
use script::layout_exports::{HANDLED_SNAPSHOT, HAS_SNAPSHOT};
use script::layout_exports::{LayoutCharacterDataHelpers, LayoutDocumentHelpers};
use script::layout_exports::{LayoutElementHelpers, LayoutNodeHelpers, RawLayoutElementHelpers};
use script::layout_exports::LayoutJS;
use script::layout_exports::PendingRestyle;
use script_layout_interface::{HTMLCanvasData, LayoutNodeType, SVGSVGData, TrustedNodeAddress};
use script_layout_interface::{OpaqueStyleAndLayoutData, StyleData};
use script_layout_interface::wrapper_traits::{DangerousThreadSafeLayoutNode, GetLayoutData, LayoutNode};
Expand Down Expand Up @@ -79,6 +80,12 @@ use style::shared_lock::{SharedRwLock as StyleSharedRwLock, Locked as StyleLocke
use style::str::is_whitespace;
use style::stylearc::Arc;

pub unsafe fn drop_style_and_layout_data(data: OpaqueStyleAndLayoutData) {
let ptr: *mut StyleData = data.ptr.get();
let non_opaque: *mut StyleAndLayoutData = ptr as *mut _;
let _ = Box::from_raw(non_opaque);
}

#[derive(Copy, Clone)]
pub struct ServoLayoutNode<'a> {
/// The wrapped node.
Expand Down Expand Up @@ -244,6 +251,17 @@ impl<'ln> LayoutNode for ServoLayoutNode<'ln> {
self.script_type_id().into()
}

unsafe fn initialize_data(&self) {
if self.get_raw_data().is_none() {
let ptr: *mut StyleAndLayoutData =
Box::into_raw(Box::new(StyleAndLayoutData::new()));
let opaque = OpaqueStyleAndLayoutData {
ptr: NonZero::new(ptr as *mut StyleData),
};
self.init_style_and_layout_data(opaque);
};
}

unsafe fn init_style_and_layout_data(&self, data: OpaqueStyleAndLayoutData) {
self.get_jsmanaged().init_style_and_layout_data(data);
}
Expand Down Expand Up @@ -477,6 +495,17 @@ impl<'le> TElement for ServoLayoutElement<'le> {
old_value - 1
}

unsafe fn clear_data(&self) {
if self.get_raw_data().is_some() {
drop_style_and_layout_data(self.as_node().take_style_and_layout_data());
}
}

unsafe fn ensure_data(&self) -> AtomicRefMut<ElementData> {
self.as_node().initialize_data();
self.mutate_data().unwrap()
}

fn get_data(&self) -> Option<&AtomicRefCell<ElementData>> {
unsafe {
self.get_style_and_layout_data().map(|d| {
Expand Down
12 changes: 10 additions & 2 deletions components/layout_thread/lib.rs
Expand Up @@ -7,13 +7,18 @@

#![feature(box_syntax)]
#![feature(mpsc_select)]
#![feature(nonzero)]

extern crate app_units;
extern crate atomic_refcell;
extern crate core;
extern crate euclid;
extern crate fnv;
extern crate gfx;
extern crate gfx_traits;
extern crate heapsize;
#[macro_use]
extern crate html5ever;
extern crate ipc_channel;
#[macro_use]
extern crate layout;
Expand All @@ -27,6 +32,7 @@ extern crate net_traits;
extern crate parking_lot;
#[macro_use]
extern crate profile_traits;
extern crate range;
extern crate rayon;
extern crate script;
extern crate script_layout_interface;
Expand All @@ -40,7 +46,11 @@ extern crate servo_url;
extern crate style;
extern crate webrender_api;

mod dom_wrapper;

use app_units::Au;
use dom_wrapper::{ServoLayoutElement, ServoLayoutDocument, ServoLayoutNode};
use dom_wrapper::drop_style_and_layout_data;
use euclid::{Point2D, Rect, Size2D, ScaleFactor};
use fnv::FnvHashMap;
use gfx::display_list::{OpaqueNode, WebRenderImageInfo};
Expand Down Expand Up @@ -71,7 +81,6 @@ use layout::sequential;
use layout::traversal::{ComputeAbsolutePositions, RecalcStyleAndConstructFlows};
use layout::webrender_helpers::WebRenderDisplayListConverter;
use layout::wrapper::LayoutNodeLayoutData;
use layout::wrapper::drop_style_and_layout_data;
use layout_traits::LayoutThreadFactory;
use msg::constellation_msg::PipelineId;
use msg::constellation_msg::TopLevelBrowsingContextId;
Expand All @@ -80,7 +89,6 @@ use parking_lot::RwLock;
use profile_traits::mem::{self, Report, ReportKind, ReportsChan};
use profile_traits::time::{self, TimerMetadata, profile};
use profile_traits::time::{TimerMetadataFrameType, TimerMetadataReflowType};
use script::layout_wrapper::{ServoLayoutElement, ServoLayoutDocument, ServoLayoutNode};
use script_layout_interface::message::{Msg, NewLayoutThreadInfo, Reflow, ReflowQueryType};
use script_layout_interface::message::{ScriptReflow, ReflowComplete};
use script_layout_interface::rpc::{LayoutRPC, MarginStyleResponse, NodeOverflowResponse, OffsetParentResponse};
Expand Down
3 changes: 0 additions & 3 deletions components/script/Cargo.toml
Expand Up @@ -27,7 +27,6 @@ tinyfiledialogs = "2.5.9"
angle = {git = "https://github.com/servo/angle", branch = "servo"}
app_units = "0.5"
audio-video-metadata = "0.1.2"
atomic_refcell = "0.1"
base64 = "0.5.2"
bitflags = "0.7"
bluetooth_traits = {path = "../bluetooth_traits"}
Expand All @@ -44,7 +43,6 @@ encoding = "0.2"
euclid = "0.15"
fnv = "1.0"
gleam = "0.4"
gfx_traits = {path = "../gfx_traits"}
half = "1.0"
heapsize = "0.4"
heapsize_derive = "0.1"
Expand All @@ -68,7 +66,6 @@ open = "1.1.1"
parking_lot = "0.4"
phf = "0.7.18"
profile_traits = {path = "../profile_traits"}
range = {path = "../range"}
ref_filter_map = "1.0.1"
ref_slice = "1.0"
regex = "0.2"
Expand Down

0 comments on commit 9a933ef

Please sign in to comment.