Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layout viewer for layout 2020 #25803

Merged
merged 10 commits into from Feb 24, 2020
Prev

Minor improvements to layout_debug and associated code

  • Loading branch information
ferjm committed Feb 21, 2020
commit 5e76c93cc79613d5845fc726be5e420e431fea04
@@ -4,10 +4,12 @@

use crate::geom::flow_relative::{Rect, Sides, Vec2};
use crate::geom::{PhysicalPoint, PhysicalRect};
#[cfg(debug_assertions)]
use crate::layout_debug;
use gfx::text::glyph::GlyphStore;
use gfx_traits::print_tree::PrintTree;
use serde::ser::{Serialize, SerializeStruct, Serializer};
#[cfg(not(debug_assertions))]
use serde::ser::{Serialize, Serializer};
use servo_arc::Arc as ServoArc;
use std::sync::Arc;
use style::computed_values::overflow_x::T as ComputedOverflow;
@@ -26,9 +28,11 @@ pub(crate) enum Fragment {
Image(ImageFragment),
}

#[derive(Serialize)]
pub(crate) struct BoxFragment {
pub tag: OpaqueNode,
pub debug_id: DebugId,
#[serde(skip_serializing)]
pub style: ServoArc<ComputedValues>,
pub children: Vec<Fragment>,

@@ -72,20 +76,26 @@ pub(crate) struct AnonymousFragment {
pub scrollable_overflow: PhysicalRect<Length>,
}

#[derive(Serialize)]
pub(crate) struct TextFragment {
pub debug_id: DebugId,
pub tag: OpaqueNode,
#[serde(skip_serializing)]
pub parent_style: ServoArc<ComputedValues>,
pub rect: Rect<Length>,
pub ascent: Length,
#[serde(skip_serializing)]
pub font_key: FontInstanceKey,
pub glyphs: Vec<Arc<GlyphStore>>,
}

#[derive(Serialize)]
pub(crate) struct ImageFragment {
pub debug_id: DebugId,
#[serde(skip_serializing)]
pub style: ServoArc<ComputedValues>,
pub rect: Rect<Length>,
#[serde(skip_serializing)]
pub image_key: ImageKey,
}

@@ -355,49 +365,13 @@ impl CollapsedMargin {
}
}

impl Serialize for BoxFragment {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut serializer = serializer.serialize_struct("BoxFragment", 7)?;
serializer.serialize_field("debug_id", &self.debug_id)?;
serializer.serialize_field("content_rect", &self.content_rect)?;
serializer.serialize_field("padding", &self.padding)?;
serializer.serialize_field("border", &self.border)?;
serializer.serialize_field("margin", &self.margin)?;
serializer.serialize_field(
"block_margins_collapsed_with_children",
&self.block_margins_collapsed_with_children,
)?;
serializer.serialize_field("children", &self.children)?;
serializer.end()
}
}

impl Serialize for TextFragment {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut serializer = serializer.serialize_struct("TextFragment", 4)?;
serializer.serialize_field("debug_id", &self.debug_id)?;
serializer.serialize_field("rect", &self.rect)?;
serializer.serialize_field("ascent", &self.ascent)?;
serializer.serialize_field("glyphs", &self.glyphs)?;
serializer.end()
}
}

impl Serialize for ImageFragment {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut serializer = serializer.serialize_struct("ImageFragment", 2)?;
serializer.serialize_field("debug_id", &self.debug_id)?;
serializer.serialize_field("rect", &self.rect)?;
serializer.end()
}
}

#[cfg(not(debug_assertions))]
#[derive(Clone)]
pub struct DebugId;

#[cfg(debug_assertions)]
#[derive(Clone)]
#[derive(Clone, Serialize)]
#[serde(transparent)]
pub struct DebugId(u16);

#[cfg(not(debug_assertions))]
@@ -420,10 +394,3 @@ impl Serialize for DebugId {
serializer.serialize_str(&format!("{:p}", &self))
}
}
This conversation was marked as resolved by SimonSapin

This comment has been minimized.

Copy link
@SimonSapin

SimonSapin Feb 20, 2020

Member

For these three impls: why are they not derived? Could they be derived with some attributes from https://serde.rs/field-attrs.html ?

This comment has been minimized.

Copy link
@ferjm

ferjm Feb 21, 2020

Author Member

I think I learnt about #[serde(skip_serializing)] after implementing the serializers. Fixed.


#[cfg(debug_assertions)]
impl Serialize for DebugId {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_u16(self.0)
}
}
@@ -8,8 +8,7 @@
use crate::flow::{BoxTreeRoot, FragmentTreeRoot};
use serde_json::{to_string, to_value, Value};
use std::cell::RefCell;
use std::fs::File;
use std::io::Write;
use std::fs;
#[cfg(debug_assertions)]
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
@@ -141,6 +140,9 @@ pub fn end_trace(generation: u32) {
fragment_tree: to_value(&thread_state.fragment_tree).unwrap_or(Value::Null),
};
let result = to_string(&root_scope).unwrap();
let mut file = File::create(format!("layout_trace-{}.json", generation)).unwrap();
file.write_all(result.as_bytes()).unwrap();
fs::write(
format!("layout_trace-{}.json", generation),
result.as_bytes(),
)
.unwrap();
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.