Skip to content

Commit

Permalink
Wrap BlockLevelBox and InlineLevelBox with AtomicRefCell
Browse files Browse the repository at this point in the history
  • Loading branch information
pcwalton committed Mar 17, 2020
1 parent 2ff776b commit 9cb824e
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 95 deletions.
5 changes: 3 additions & 2 deletions components/layout_2020/element_data.rs
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::cell::ArcRefCell;
use crate::flow::inline::InlineLevelBox;
use crate::flow::BlockLevelBox;
use atomic_refcell::AtomicRefCell;
Expand All @@ -21,6 +22,6 @@ pub(super) struct PseudoElementBoxes {

pub(super) enum LayoutBox {
DisplayContents,
BlockLevel(Arc<BlockLevelBox>),
InlineLevel(Arc<InlineLevelBox>),
BlockLevel(ArcRefCell<BlockLevelBox>),
InlineLevel(ArcRefCell<InlineLevelBox>),
}
144 changes: 72 additions & 72 deletions components/layout_2020/flow/construct.rs
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::cell::ArcRefCell;
use crate::context::LayoutContext;
use crate::dom_traversal::{BoxSlot, Contents, NodeExt, NonReplacedContents, TraversalHandler};
use crate::element_data::LayoutBox;
Expand Down Expand Up @@ -282,54 +283,48 @@ where
// context with the parent style of that builder.
let inlines = self.current_inline_level_boxes();

fn last_text(inlines: &mut [Arc<InlineLevelBox>]) -> Option<&mut String> {
let last = inlines.last_mut()?;
if let InlineLevelBox::TextRun(_) = &**last {
// We never clone text run boxes, so the refcount is 1 and unwrap succeeds:
let last = Arc::get_mut(last).unwrap();
if let InlineLevelBox::TextRun(TextRun { text, .. }) = last {
Some(text)
} else {
unreachable!()
}
} else {
None
}
}

let mut new_text_run_contents;
let output;
if let Some(text) = last_text(inlines) {
// Append to the existing text run
new_text_run_contents = None;
output = text;
} else {
new_text_run_contents = Some(String::new());
output = new_text_run_contents.as_mut().unwrap();
}

if leading_whitespace {
output.push(' ')
}
loop {
if let Some(i) = input.bytes().position(|b| b.is_ascii_whitespace()) {
let (non_whitespace, rest) = input.split_at(i);
output.push_str(non_whitespace);
output.push(' ');
if let Some(i) = rest.bytes().position(|b| !b.is_ascii_whitespace()) {
input = &rest[i..];
{
let mut last_box = inlines.last_mut().map(|last| last.borrow_mut());
let last_text = last_box.as_mut().and_then(|last| match &mut **last {
InlineLevelBox::TextRun(last) => Some(&mut last.text),
_ => None,
});

if let Some(text) = last_text {
// Append to the existing text run
new_text_run_contents = None;
output = text;
} else {
new_text_run_contents = Some(String::new());
output = new_text_run_contents.as_mut().unwrap();
}

if leading_whitespace {
output.push(' ')
}
loop {
if let Some(i) = input.bytes().position(|b| b.is_ascii_whitespace()) {
let (non_whitespace, rest) = input.split_at(i);
output.push_str(non_whitespace);
output.push(' ');
if let Some(i) = rest.bytes().position(|b| !b.is_ascii_whitespace()) {
input = &rest[i..];
} else {
break;
}
} else {
output.push_str(input);
break;
}
} else {
output.push_str(input);
break;
}
}

if let Some(text) = new_text_run_contents {
let parent_style = parent_style.clone();
inlines.push(Arc::new(InlineLevelBox::TextRun(TextRun {
inlines.push(ArcRefCell::new(InlineLevelBox::TextRun(TextRun {
tag: node.as_opaque(),
parent_style,
text,
Expand All @@ -356,23 +351,27 @@ where
let mut inline_level_boxes = self.current_inline_level_boxes().iter().rev();
let mut stack = Vec::new();
let preserved = loop {
match inline_level_boxes.next().map(|b| &**b) {
Some(InlineLevelBox::TextRun(r)) => break !r.text.ends_with(' '),
Some(InlineLevelBox::Atomic { .. }) => break false,
Some(InlineLevelBox::OutOfFlowAbsolutelyPositionedBox(_)) |
Some(InlineLevelBox::OutOfFlowFloatBox(_)) => {},
Some(InlineLevelBox::InlineBox(b)) => {
let inline_box = match inline_level_boxes.next() {
Some(box_) => box_,
None => match stack.pop() {
Some(iter) => {
inline_level_boxes = iter;
continue;
},
None => break false,
},
};{
match &*inline_box.borrow() {
InlineLevelBox::TextRun(r) => break !r.text.ends_with(' '),
InlineLevelBox::Atomic { .. } => break false,
InlineLevelBox::OutOfFlowAbsolutelyPositionedBox(_) |
InlineLevelBox::OutOfFlowFloatBox(_) => {},
InlineLevelBox::InlineBox(b) => {
stack.push(inline_level_boxes);
inline_level_boxes = b.children.iter().rev()
},
None => {
if let Some(iter) = stack.pop() {
inline_level_boxes = iter
} else {
break false; // Paragraph start
}
},
}
};}
()
};
let text = text.trim_start_matches(|c: char| c.is_ascii_whitespace());
(preserved, text)
Expand All @@ -384,7 +383,7 @@ where
style: &Arc<ComputedValues>,
display_inside: DisplayInside,
contents: Contents,
) -> Arc<InlineLevelBox> {
) -> ArcRefCell<InlineLevelBox> {
let box_ = if display_inside == DisplayInside::Flow && !contents.is_replaced() {
// We found un inline box.
// Whatever happened before, all we need to do before recurring
Expand All @@ -410,9 +409,9 @@ where
.pop()
.expect("no ongoing inline level box found");
inline_box.last_fragment = true;
Arc::new(InlineLevelBox::InlineBox(inline_box))
ArcRefCell::new(InlineLevelBox::InlineBox(inline_box))
} else {
Arc::new(InlineLevelBox::Atomic(
ArcRefCell::new(InlineLevelBox::Atomic(
IndependentFormattingContext::construct(
self.context,
node,
Expand Down Expand Up @@ -466,13 +465,13 @@ where
for mut fragmented_parent_inline_box in fragmented_inline_boxes {
fragmented_parent_inline_box
.children
.push(Arc::new(fragmented_inline));
.push(ArcRefCell::new(fragmented_inline));
fragmented_inline = InlineLevelBox::InlineBox(fragmented_parent_inline_box);
}

self.ongoing_inline_formatting_context
.inline_level_boxes
.push(Arc::new(fragmented_inline));
.push(ArcRefCell::new(fragmented_inline));
}

// We found a block level element, so the ongoing inline formatting
Expand Down Expand Up @@ -525,7 +524,7 @@ where
kind,
});
} else {
let box_ = Arc::new(InlineLevelBox::OutOfFlowAbsolutelyPositionedBox(Arc::new(
let box_ = ArcRefCell::new(InlineLevelBox::OutOfFlowAbsolutelyPositionedBox(Arc::new(
AbsolutelyPositionedBox::construct(
self.context,
node,
Expand Down Expand Up @@ -561,7 +560,7 @@ where
kind,
});
} else {
let box_ = Arc::new(InlineLevelBox::OutOfFlowFloatBox(FloatBox::construct(
let box_ = ArcRefCell::new(InlineLevelBox::OutOfFlowFloatBox(FloatBox::construct(
self.context,
node,
style,
Expand Down Expand Up @@ -610,7 +609,7 @@ where
});
}

fn current_inline_level_boxes(&mut self) -> &mut Vec<Arc<InlineLevelBox>> {
fn current_inline_level_boxes(&mut self) -> &mut Vec<ArcRefCell<InlineLevelBox>> {
match self.ongoing_inline_boxes_stack.last_mut() {
Some(last) => &mut last.children,
None => &mut self.ongoing_inline_formatting_context.inline_level_boxes,
Expand All @@ -634,7 +633,7 @@ where
self,
context: &LayoutContext,
max_assign_in_flow_outer_content_sizes_to: Option<&mut ContentSizes>,
) -> (Arc<BlockLevelBox>, ContainsFloats) {
) -> (ArcRefCell<BlockLevelBox>, ContainsFloats) {
let node = self.node;
let style = self.style;
let (block_level_box, contains_floats) = match self.kind {
Expand All @@ -651,7 +650,7 @@ where
if let Some(to) = max_assign_in_flow_outer_content_sizes_to {
to.max_assign(&box_content_sizes.outer_inline(&style))
}
let block_level_box = Arc::new(BlockLevelBox::SameFormattingContextBlock {
let block_level_box = ArcRefCell::new(BlockLevelBox::SameFormattingContextBlock {
tag: node.as_opaque(),
contents,
style,
Expand All @@ -678,30 +677,31 @@ where
to.max_assign(&contents.content_sizes.outer_inline(&contents.style))
}
(
Arc::new(BlockLevelBox::Independent(contents)),
ArcRefCell::new(BlockLevelBox::Independent(contents)),
ContainsFloats::No,
)
},
BlockLevelCreator::OutOfFlowAbsolutelyPositionedBox {
display_inside,
contents,
} => {
let block_level_box = Arc::new(BlockLevelBox::OutOfFlowAbsolutelyPositionedBox(
Arc::new(AbsolutelyPositionedBox::construct(
context,
node,
style,
display_inside,
contents,
)),
));
let block_level_box =
ArcRefCell::new(BlockLevelBox::OutOfFlowAbsolutelyPositionedBox(Arc::new(
AbsolutelyPositionedBox::construct(
context,
node,
style,
display_inside,
contents,
),
)));
(block_level_box, ContainsFloats::No)
},
BlockLevelCreator::OutOfFlowFloatBox {
display_inside,
contents,
} => {
let block_level_box = Arc::new(BlockLevelBox::OutOfFlowFloatBox(
let block_level_box = ArcRefCell::new(BlockLevelBox::OutOfFlowFloatBox(
FloatBox::construct(context, node, style, display_inside, contents),
));
(block_level_box, ContainsFloats::Yes)
Expand Down
13 changes: 7 additions & 6 deletions components/layout_2020/flow/inline.rs
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::cell::ArcRefCell;
use crate::context::LayoutContext;
use crate::flow::float::FloatBox;
use crate::flow::FlowLayout;
Expand All @@ -27,7 +28,7 @@ use webrender_api::FontInstanceKey;

#[derive(Debug, Default, Serialize)]
pub(crate) struct InlineFormattingContext {
pub(super) inline_level_boxes: Vec<Arc<InlineLevelBox>>,
pub(super) inline_level_boxes: Vec<ArcRefCell<InlineLevelBox>>,
}

#[derive(Debug, Serialize)]
Expand All @@ -46,7 +47,7 @@ pub(crate) struct InlineBox {
pub style: Arc<ComputedValues>,
pub first_fragment: bool,
pub last_fragment: bool,
pub children: Vec<Arc<InlineLevelBox>>,
pub children: Vec<ArcRefCell<InlineLevelBox>>,
}

/// https://www.w3.org/TR/css-display-3/#css-text-run
Expand All @@ -59,7 +60,7 @@ pub(crate) struct TextRun {
}

struct InlineNestingLevelState<'box_tree> {
remaining_boxes: std::slice::Iter<'box_tree, Arc<InlineLevelBox>>,
remaining_boxes: std::slice::Iter<'box_tree, ArcRefCell<InlineLevelBox>>,
fragments_so_far: Vec<Fragment>,
inline_start: Length,
max_block_size_of_fragments_so_far: Length,
Expand Down Expand Up @@ -105,10 +106,10 @@ impl InlineFormattingContext {
fn traverse(
&mut self,
layout_context: &LayoutContext,
inline_level_boxes: &[Arc<InlineLevelBox>],
inline_level_boxes: &[ArcRefCell<InlineLevelBox>],
) {
for inline_level_box in inline_level_boxes {
match &**inline_level_box {
match &*inline_level_box.borrow() {
InlineLevelBox::InlineBox(inline_box) => {
let padding = inline_box.style.padding();
let border = inline_box.style.border_width();
Expand Down Expand Up @@ -229,7 +230,7 @@ impl InlineFormattingContext {
};
loop {
if let Some(child) = ifc.current_nesting_level.remaining_boxes.next() {
match &**child {
match &*child.borrow() {
InlineLevelBox::InlineBox(inline) => {
let partial = inline.start_layout(&mut ifc);
ifc.partial_inline_boxes_stack.push(partial)
Expand Down
9 changes: 5 additions & 4 deletions components/layout_2020/flow/mod.rs
Expand Up @@ -4,6 +4,7 @@

//! Flow layout, also known as block-and-inline layout.

use crate::cell::ArcRefCell;
use crate::context::LayoutContext;
use crate::flow::float::{FloatBox, FloatContext};
use crate::flow::inline::InlineFormattingContext;
Expand Down Expand Up @@ -38,7 +39,7 @@ pub(crate) struct BlockFormattingContext {

#[derive(Debug, Serialize)]
pub(crate) enum BlockContainer {
BlockLevelBoxes(Vec<Arc<BlockLevelBox>>),
BlockLevelBoxes(Vec<ArcRefCell<BlockLevelBox>>),
InlineFormattingContext(InlineFormattingContext),
}

Expand Down Expand Up @@ -133,7 +134,7 @@ impl BlockContainer {
fn layout_block_level_children(
layout_context: &LayoutContext,
positioning_context: &mut PositioningContext,
child_boxes: &[Arc<BlockLevelBox>],
child_boxes: &[ArcRefCell<BlockLevelBox>],
containing_block: &ContainingBlock,
tree_rank: usize,
mut float_context: Option<&mut FloatContext>,
Expand Down Expand Up @@ -204,7 +205,7 @@ fn layout_block_level_children(
.iter()
.enumerate()
.map(|(tree_rank, box_)| {
let mut fragment = box_.layout(
let mut fragment = box_.borrow().layout(
layout_context,
positioning_context,
containing_block,
Expand All @@ -224,7 +225,7 @@ fn layout_block_level_children(
.mapfold_reduce_into(
positioning_context,
|positioning_context, (tree_rank, box_)| {
box_.layout(
box_.borrow().layout(
layout_context,
positioning_context,
containing_block,
Expand Down

0 comments on commit 9cb824e

Please sign in to comment.