Skip to content

Commit

Permalink
WPT: reuse FontContext to avoid reloading system fonts
Browse files Browse the repository at this point in the history
Takes runtime for css-flexbox from 12s -> 8s
  • Loading branch information
nicoburns committed Nov 6, 2024
1 parent 27981d2 commit 0df6434
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 15 deletions.
2 changes: 1 addition & 1 deletion apps/readme/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn main() {
Arc::clone(&net_callback) as SharedCallback<Resource>,
));

let document = HtmlDocument::from_html(&html, Some(base_url), stylesheets, net_provider);
let document = HtmlDocument::from_html(&html, Some(base_url), stylesheets, net_provider, None);
dioxus_native::launch_with_document(document, rt, Some(net_callback));
}

Expand Down
1 change: 1 addition & 0 deletions apps/wpt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ blitz-net = { path = "../../packages/blitz-net" }
blitz-traits = { path = "../../packages/blitz-traits" }
blitz-renderer-vello = { path = "../../packages/blitz-renderer-vello" }
dioxus-native = { path = "../../packages/dioxus-native", features = ["tracing"] }
parley = { workspace = true }
tokio = { workspace = true }
png = { version = "0.17" }
glob = {version = "0.3.1"}
Expand Down
26 changes: 23 additions & 3 deletions apps/wpt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use blitz_dom::{HtmlDocument, Viewport};
use blitz_net::{MpscCallback, Provider};
use blitz_renderer_vello::VelloImageRenderer;
use blitz_traits::net::SharedProvider;
use parley::FontContext;
use reqwest::Url;
use tower_http::services::ServeDir;

Expand Down Expand Up @@ -98,6 +99,13 @@ fn setup_blitz() -> BlitzContext {
}
}

fn clone_font_ctx(ctx: &FontContext) -> FontContext {
FontContext {
collection: ctx.collection.clone(),
source_cache: ctx.source_cache.clone(),
}
}

fn main() {
env_logger::init();
let rt = tokio::runtime::Builder::new_current_thread()
Expand Down Expand Up @@ -141,13 +149,16 @@ fn main() {

let num = AtomicU32::new(0);

let base_font_context = parley::FontContext::default();

test_paths.into_par_iter().for_each_init(
|| {
let renderer = rt.block_on(VelloImageRenderer::new(WIDTH, HEIGHT, SCALE));
let font_ctx = clone_font_ctx(&base_font_context);
let test_buffer = Vec::with_capacity((WIDTH * HEIGHT * 4) as usize);
let ref_buffer = Vec::with_capacity((WIDTH * HEIGHT * 4) as usize);
let guard = rt.enter();
(renderer, test_buffer, ref_buffer, guard)
(renderer, font_ctx, test_buffer, ref_buffer, guard)
},
|state, path| {
let num = num.fetch_add(1, Ordering::SeqCst) + 1;
Expand All @@ -162,13 +173,15 @@ fn main() {
let url = base_url.join(relative_path.as_str()).unwrap();

let renderer = &mut state.0;
let test_buffer = &mut state.1;
let ref_buffer = &mut state.2;
let font_ctx = &state.1;
let test_buffer = &mut state.2;
let ref_buffer = &mut state.3;

let result = catch_unwind(AssertUnwindSafe(|| {
let mut blitz_context = setup_blitz();
let result = rt.block_on(process_test_file(
renderer,
font_ctx,
test_buffer,
ref_buffer,
&url,
Expand Down Expand Up @@ -231,6 +244,7 @@ async fn serve(app: Router, port: u16) {
#[allow(clippy::too_many_arguments)]
async fn process_test_file(
renderer: &mut VelloImageRenderer,
font_ctx: &FontContext,
test_buffer: &mut Vec<u8>,
ref_buffer: &mut Vec<u8>,
path: &Url,
Expand All @@ -255,6 +269,7 @@ async fn process_test_file(
if let Some(reference) = reference {
process_test_file_with_ref(
renderer,
font_ctx,
test_buffer,
ref_buffer,
path,
Expand All @@ -274,6 +289,7 @@ async fn process_test_file(
#[allow(clippy::too_many_arguments)]
async fn process_test_file_with_ref(
renderer: &mut VelloImageRenderer,
font_ctx: &FontContext,
test_buffer: &mut Vec<u8>,
ref_buffer: &mut Vec<u8>,
test_url: &Url,
Expand Down Expand Up @@ -306,6 +322,7 @@ async fn process_test_file_with_ref(
render_html_to_buffer(
blitz_context,
renderer,
font_ctx,
test_file_contents,
test_url,
test_buffer,
Expand All @@ -317,6 +334,7 @@ async fn process_test_file_with_ref(
render_html_to_buffer(
blitz_context,
renderer,
font_ctx,
&reference_file_contents,
&ref_url,
ref_buffer,
Expand Down Expand Up @@ -345,6 +363,7 @@ async fn process_test_file_with_ref(
async fn render_html_to_buffer(
blitz_context: &mut BlitzContext,
renderer: &mut VelloImageRenderer,
font_ctx: &FontContext,
html: &str,
base_url: &Url,
buf: &mut Vec<u8>,
Expand All @@ -355,6 +374,7 @@ async fn render_html_to_buffer(
Some(base_url.to_string()),
Vec::new(),
Arc::clone(&blitz_context.net) as SharedProvider<Resource>,
Some(clone_font_ctx(font_ctx)),
);

document
Expand Down
1 change: 1 addition & 0 deletions examples/screenshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ async fn main() {
Some(url_string.clone()),
Vec::new(),
Arc::clone(&net) as SharedProvider<Resource>,
None,
);

timer.time("Parsed document");
Expand Down
17 changes: 8 additions & 9 deletions packages/blitz-dom/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::node::{ImageData, NodeSpecificData, TextBrush};
use crate::{ElementNodeData, Node, NodeData, TextNodeData, Viewport};
use app_units::Au;
use html5ever::local_name;
use parley::PlainEditorOp;
use parley::{FontContext, PlainEditorOp};
use peniko::kurbo;
use string_cache::Atom;
use style::attr::{AttrIdentifier, AttrValue};
Expand Down Expand Up @@ -382,6 +382,10 @@ impl DocumentLike for Document {

impl Document {
pub fn new(viewport: Viewport) -> Self {
Self::with_font_ctx(viewport, parley::FontContext::default())
}

pub fn with_font_ctx(viewport: Viewport, mut font_ctx: FontContext) -> Self {
let device = viewport.make_device();
let stylist = Stylist::new(device, QuirksMode::NoQuirks);
let snapshots = SnapshotMap::new();
Expand All @@ -395,7 +399,9 @@ impl Document {
style_config::set_bool("layout.legacy_layout", true);
style_config::set_bool("layout.columns.enabled", true);

let font_ctx = Self::create_font_context();
font_ctx
.collection
.register_fonts(include_bytes!("moz-bullet-font.otf").to_vec());

let mut doc = Self {
guard,
Expand Down Expand Up @@ -423,13 +429,6 @@ impl Document {
doc
}

fn create_font_context() -> parley::FontContext {
let mut ctx = parley::FontContext::default();
ctx.collection
.register_fonts(include_bytes!("moz-bullet-font.otf").to_vec());
ctx
}

/// Set base url for resolving linked resources (stylesheets, images, fonts, etc)
pub fn set_base_url(&mut self, url: &str) {
self.base_url = Some(Url::parse(url).unwrap());
Expand Down
7 changes: 6 additions & 1 deletion packages/blitz-dom/src/html_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{Document, DocumentHtmlParser, DocumentLike, Viewport};
use crate::net::Resource;
use crate::DEFAULT_CSS;
use blitz_traits::net::SharedProvider;
use parley::FontContext;

pub struct HtmlDocument {
inner: Document,
Expand Down Expand Up @@ -38,10 +39,14 @@ impl HtmlDocument {
base_url: Option<String>,
stylesheets: Vec<String>,
net_provider: SharedProvider<Resource>,
font_ctx: Option<FontContext>,
) -> Self {
// Spin up the virtualdom and include the default stylesheet
let viewport = Viewport::new(0, 0, 1.0);
let mut dom = Document::new(viewport);
let mut dom = match font_ctx {
Some(font_ctx) => Document::with_font_ctx(viewport, font_ctx),
None => Document::new(viewport),
};

// Set base url if configured
if let Some(url) = &base_url {
Expand Down
2 changes: 1 addition & 1 deletion packages/dioxus-native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub fn launch_static_html_cfg(html: &str, cfg: Config) {
Arc::clone(&net_callback) as SharedCallback<Resource>,
));

let document = HtmlDocument::from_html(html, cfg.base_url, cfg.stylesheets, net_provider);
let document = HtmlDocument::from_html(html, cfg.base_url, cfg.stylesheets, net_provider, None);
launch_with_document(document, rt, Some(net_callback));
}

Expand Down

0 comments on commit 0df6434

Please sign in to comment.