From 3a08c0392553435f46ad922c09cabb8ccd8d11bd Mon Sep 17 00:00:00 2001 From: Cameron McCormack Date: Sat, 30 Mar 2019 00:16:27 +0000 Subject: [PATCH] Bug 1474793 - Part 11: Add FFI API to use SharedMemoryBuilder. r=emilio Depends on D17197 Differential Revision: https://phabricator.services.mozilla.com/D17198 --HG-- extra : moz-landing-system : lando --- Cargo.lock | 2 + layout/style/ServoBindingTypes.h | 1 + layout/style/ServoBindings.h | 10 ++++ servo/components/style/gecko/boxed_types.rs | 21 ++++++++ servo/components/style/gecko/mod.rs | 1 + servo/ports/geckolib/Cargo.toml | 1 + servo/ports/geckolib/glue.rs | 58 ++++++++++++++++++++- servo/ports/geckolib/lib.rs | 1 + servo/ports/geckolib/tests/Cargo.toml | 1 + servo/ports/geckolib/tests/lib.rs | 1 + servo/tests/unit/style/Cargo.toml | 1 + 11 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 servo/components/style/gecko/boxed_types.rs diff --git a/Cargo.lock b/Cargo.lock index 597540bc6707..7fd97aa4d6b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1187,6 +1187,7 @@ dependencies = [ "smallvec 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)", "style 0.0.1", "style_traits 0.0.1", + "to_shmem 0.0.1", ] [[package]] @@ -2792,6 +2793,7 @@ dependencies = [ "smallvec 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)", "style 0.0.1", "style_traits 0.0.1", + "to_shmem 0.0.1", ] [[package]] diff --git a/layout/style/ServoBindingTypes.h b/layout/style/ServoBindingTypes.h index 26af2d1b7971..6600b72f6a6a 100644 --- a/layout/style/ServoBindingTypes.h +++ b/layout/style/ServoBindingTypes.h @@ -180,6 +180,7 @@ SERVO_ARC_TYPE(ComputedStyle, mozilla::ComputedStyle) SERVO_BOXED_TYPE(StyleSet, RawServoStyleSet) SERVO_BOXED_TYPE(AuthorStyles, RawServoAuthorStyles) SERVO_BOXED_TYPE(SelectorList, RawServoSelectorList) +SERVO_BOXED_TYPE(SharedMemoryBuilder, RawServoSharedMemoryBuilder) SERVO_BOXED_TYPE(SourceSizeList, RawServoSourceSizeList) SERVO_BOXED_TYPE(UseCounters, StyleUseCounters) #undef SERVO_BOXED_TYPE diff --git a/layout/style/ServoBindings.h b/layout/style/ServoBindings.h index cf2db445cbee..10fcab9e5f53 100644 --- a/layout/style/ServoBindings.h +++ b/layout/style/ServoBindings.h @@ -1002,6 +1002,16 @@ void Servo_Quotes_GetQuote(const RawServoQuotes* quotes, int32_t depth, mozilla::StyleContentType quote_type, nsAString* result); +RawServoSharedMemoryBuilder* Servo_SharedMemoryBuilder_Create(uint8_t* buffer, + size_t len); + +const ServoCssRules* Servo_SharedMemoryBuilder_AddStylesheet( + RawServoSharedMemoryBuilder* builder, + const RawServoStyleSheetContents* sheet); + +size_t Servo_SharedMemoryBuilder_GetLength( + RawServoSharedMemoryBuilder* builder); + } // extern "C" #pragma GCC diagnostic pop diff --git a/servo/components/style/gecko/boxed_types.rs b/servo/components/style/gecko/boxed_types.rs new file mode 100644 index 000000000000..857f99e14891 --- /dev/null +++ b/servo/components/style/gecko/boxed_types.rs @@ -0,0 +1,21 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +//! FFI implementations for types listed in ServoBoxedTypeList.h. + +use crate::gecko_bindings::sugar::ownership::{HasBoxFFI, HasFFI, HasSimpleFFI}; +use to_shmem::SharedMemoryBuilder; + +// TODO(heycam): The FFI impls for most of the types in ServoBoxedTypeList.h are spread across +// various files at the moment, but should probably all move here, and use macros to define +// them more succinctly, like we do in arc_types.rs. + +#[cfg(feature = "gecko")] +unsafe impl HasFFI for SharedMemoryBuilder { + type FFIType = crate::gecko_bindings::bindings::RawServoSharedMemoryBuilder; +} +#[cfg(feature = "gecko")] +unsafe impl HasSimpleFFI for SharedMemoryBuilder {} +#[cfg(feature = "gecko")] +unsafe impl HasBoxFFI for SharedMemoryBuilder {} diff --git a/servo/components/style/gecko/mod.rs b/servo/components/style/gecko/mod.rs index 1051f84fc523..fb332dcce6e2 100644 --- a/servo/components/style/gecko/mod.rs +++ b/servo/components/style/gecko/mod.rs @@ -8,6 +8,7 @@ mod non_ts_pseudo_class_list; pub mod arc_types; +pub mod boxed_types; pub mod conversions; pub mod data; pub mod media_features; diff --git a/servo/ports/geckolib/Cargo.toml b/servo/ports/geckolib/Cargo.toml index 3c294510ae5f..024ecc94750e 100644 --- a/servo/ports/geckolib/Cargo.toml +++ b/servo/ports/geckolib/Cargo.toml @@ -27,3 +27,4 @@ servo_arc = {path = "../../components/servo_arc"} smallvec = "0.6" style = {path = "../../components/style", features = ["gecko"]} style_traits = {path = "../../components/style_traits"} +to_shmem = {path = "../../components/to_shmem"} diff --git a/servo/ports/geckolib/glue.rs b/servo/ports/geckolib/glue.rs index f3de8ffb73d5..5c2452bdfdff 100644 --- a/servo/ports/geckolib/glue.rs +++ b/servo/ports/geckolib/glue.rs @@ -55,7 +55,8 @@ use style::gecko_bindings::structs::{ RawServoKeyframesRule, ServoCssRules, RawServoStyleSheetContents, RawServoPageRule, RawServoNamespaceRule, RawServoMozDocumentRule, RawServoKeyframe, RawServoMediaRule, RawServoImportRule, - RawServoFontFaceRule, RawServoFontFeatureValuesRule + RawServoFontFaceRule, RawServoFontFeatureValuesRule, + RawServoSharedMemoryBuilder }; use style::gecko_bindings::structs::gfxFontFeatureValueSet; use style::gecko_bindings::structs::nsAtom; @@ -107,7 +108,7 @@ use style::properties::{parse_one_declaration_into, parse_style_attribute}; use style::properties::{ComputedValues, Importance, NonCustomPropertyId}; use style::properties::{LonghandId, LonghandIdSet, PropertyDeclarationBlock, PropertyId}; use style::properties::{PropertyDeclarationId, ShorthandId}; -use style::properties::{SourcePropertyDeclaration, StyleBuilder}; +use style::properties::{SourcePropertyDeclaration, StyleBuilder, UnparsedValue}; use style::rule_cache::RuleCacheConditions; use style::rule_tree::{CascadeLevel, StrongRuleNode}; use style::selector_parser::{PseudoElementCascadeType, SelectorImpl}; @@ -137,6 +138,7 @@ use style::values::specified::gecko::IntersectionObserverRootMargin; use style::values::specified::source_size_list::SourceSizeList; use style::values::{CustomIdent, KeyframesName}; use style_traits::{CssWriter, ParsingMode, StyleParseErrorKind, ToCss}; +use to_shmem::SharedMemoryBuilder; trait ClosureHelper { fn invoke(&self); @@ -6337,3 +6339,55 @@ pub unsafe extern "C" fn Servo_Quotes_GetQuote( }; (*result).write_str(quote).unwrap(); } + +#[no_mangle] +pub unsafe extern "C" fn Servo_SharedMemoryBuilder_Create( + buffer: *mut u8, + len: usize, +) -> *mut RawServoSharedMemoryBuilder { + let mut builder = Box::new(SharedMemoryBuilder::new(buffer, len)); + + // We have Arcs in style sheets due to CSS variables being + // used in shorthand property declarations. There aren't many, though, + // and they aren't big, so we just allow their duplication for now. + builder.add_allowed_duplication_type::(); + + Box::into_raw(builder) as *mut _ +} + +#[no_mangle] +pub unsafe extern "C" fn Servo_SharedMemoryBuilder_AddStylesheet( + builder: &mut RawServoSharedMemoryBuilder, + raw_contents: &RawServoStyleSheetContents, +) -> *const ServoCssRules { + let builder = SharedMemoryBuilder::from_ffi_mut(builder); + let contents = StylesheetContents::as_arc(&raw_contents); + + // Assert some things we assume when we create a style sheet from shared + // memory. + debug_assert_eq!(contents.origin, Origin::UserAgent); + debug_assert_eq!(contents.quirks_mode, QuirksMode::NoQuirks); + debug_assert!(contents.source_map_url.read().is_none()); + debug_assert!(contents.source_url.read().is_none()); + + let rules = &contents.rules; + let shared_rules: &Arc> = &*builder.write(rules); + (&*shared_rules).with_raw_offset_arc(|arc| { + *Locked::::arc_as_borrowed(arc) as *const _ + }) +} + +#[no_mangle] +pub unsafe extern "C" fn Servo_SharedMemoryBuilder_GetLength( + builder: &mut RawServoSharedMemoryBuilder, +) -> usize { + let builder = SharedMemoryBuilder::from_ffi_mut(builder); + builder.len() +} + +#[no_mangle] +pub unsafe extern "C" fn Servo_SharedMemoryBuilder_Drop( + builder: Owned +) { + let _ = builder.into_box::(); +} diff --git a/servo/ports/geckolib/lib.rs b/servo/ports/geckolib/lib.rs index b1e179ce892c..2e496b103a8d 100644 --- a/servo/ports/geckolib/lib.rs +++ b/servo/ports/geckolib/lib.rs @@ -17,6 +17,7 @@ extern crate smallvec; #[macro_use] extern crate style; extern crate style_traits; +extern crate to_shmem; mod error_reporter; #[allow(non_snake_case)] diff --git a/servo/ports/geckolib/tests/Cargo.toml b/servo/ports/geckolib/tests/Cargo.toml index 4d9aae38b3a7..baaf5c230980 100644 --- a/servo/ports/geckolib/tests/Cargo.toml +++ b/servo/ports/geckolib/tests/Cargo.toml @@ -24,3 +24,4 @@ size_of_test = {path = "../../../components/size_of_test"} smallvec = "0.6" style_traits = {path = "../../../components/style_traits"} style = {path = "../../../components/style", features = ["gecko"]} +to_shmem = {path = "../../../components/to_shmem"} diff --git a/servo/ports/geckolib/tests/lib.rs b/servo/ports/geckolib/tests/lib.rs index 891315e197cd..0addec4ab3a6 100644 --- a/servo/ports/geckolib/tests/lib.rs +++ b/servo/ports/geckolib/tests/lib.rs @@ -26,6 +26,7 @@ extern crate size_of_test; #[cfg_attr(target_pointer_width = "64", macro_use)] extern crate style; extern crate style_traits; +extern crate to_shmem; #[cfg(target_pointer_width = "64")] mod size_of; diff --git a/servo/tests/unit/style/Cargo.toml b/servo/tests/unit/style/Cargo.toml index 65937bbac7e9..e67978da8547 100644 --- a/servo/tests/unit/style/Cargo.toml +++ b/servo/tests/unit/style/Cargo.toml @@ -27,3 +27,4 @@ size_of_test = {path = "../../../components/size_of_test"} style = {path = "../../../components/style"} style_traits = {path = "../../../components/style_traits"} std_test_override = { path = "../../../components/std_test_override" } +to_shmem = { path = "../../../components/to_shmem" }