Bug 1330041 - Basic handling framework for presentation attributes in Stylo, with handling for font-size and color; r=bz,emilio

This introduces a basic framework for servo's style system to be able
to query the style of presentation attributes which it can then insert
into the cascade. It uses that framework to implement the size and
color attributes on <font>.

There are a number of improvements that can be done on top of this:

 - Implement all other properties
 - Abstractify the ruledata parameter of the mappers using templates or virtual dispatch so that it can be a Servo decl block instead
 - Implement aforementiond abstraction over Servo decl blocks (this obsoletes the code in the first item above, so it might just be better to skip that and directly do this)
 - Replace uses of nsHTMLStyleSheet with an abstract base class containing common elements between Servo and Gecko

I'd prefer for these to be done in separate steps.

MozReview-Commit-ID: GO60qfeZOfl

--HG--
extra : rebase_source : 516d369a8627e413983361aaf85ccb7132b0a06c
This commit is contained in:
Manish Goregaokar 2017-01-19 15:56:53 -08:00
parent 74a3b5d2c6
commit 5b55368eda
16 changed files with 270 additions and 46 deletions

View File

@ -2023,6 +2023,12 @@ Element::GetInlineStyleDeclaration() const
return nullptr;
}
const nsMappedAttributes*
Element::GetMappedAttributes() const
{
return mAttrsAndChildren.GetMapped();
}
nsresult
Element::SetInlineStyleDeclaration(DeclarationBlock* aDeclaration,
const nsAString* aSerialized,

View File

@ -281,6 +281,11 @@ public:
*/
DeclarationBlock* GetInlineStyleDeclaration() const;
/**
* Get the mapped attributes, if any, for this element.
*/
const nsMappedAttributes* GetMappedAttributes() const;
/**
* Set the inline style declaration for this element. This will send
* an appropriate AttributeChanged notification if aNotify is true.

View File

@ -772,6 +772,11 @@ nsAttrAndChildArray::MakeMappedUnique(nsMappedAttributes* aAttributes)
return NS_OK;
}
const nsMappedAttributes*
nsAttrAndChildArray::GetMapped() const
{
return mImpl ? mImpl->mMappedAttrs : nullptr;
}
bool
nsAttrAndChildArray::GrowBy(uint32_t aGrowSize)

View File

@ -134,6 +134,7 @@ public:
{
return MappedAttrCount();
}
const nsMappedAttributes* GetMapped() const;
private:
nsAttrAndChildArray(const nsAttrAndChildArray& aOther) = delete;

View File

@ -12,8 +12,10 @@
#include "nsMappedAttributes.h"
#include "nsHTMLStyleSheet.h"
#include "nsRuleWalker.h"
#include "nsRuleData.h"
#include "mozilla/HashFunctions.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/ServoDeclarationBlock.h"
using namespace mozilla;
@ -21,14 +23,18 @@ nsMappedAttributes::nsMappedAttributes(nsHTMLStyleSheet* aSheet,
nsMapRuleToAttributesFunc aMapRuleFunc)
: mAttrCount(0),
mSheet(aSheet),
mRuleMapper(aMapRuleFunc)
mRuleMapper(aMapRuleFunc),
mServoStyle(nullptr)
{
}
nsMappedAttributes::nsMappedAttributes(const nsMappedAttributes& aCopy)
: mAttrCount(aCopy.mAttrCount),
mSheet(aCopy.mSheet),
mRuleMapper(aCopy.mRuleMapper)
mRuleMapper(aCopy.mRuleMapper),
// This is only called by ::Clone, which is used to create independent
// nsMappedAttributes objects which should not share a ServoDeclarationBlock
mServoStyle(nullptr)
{
NS_ASSERTION(mBufferSize >= aCopy.mAttrCount, "can't fit attributes");
@ -281,3 +287,20 @@ nsMappedAttributes::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
return n;
}
void
nsMappedAttributes::LazilyResolveServoDeclaration(nsRuleData* aRuleData,
nsCSSPropertyID* aIndexToIdMapping,
size_t aRuleDataSize)
{
MapRuleInfoInto(aRuleData);
MOZ_ASSERT(!mServoStyle,
"LazilyResolveServoDeclaration should not be called if mServoStyle is already set");
mServoStyle = Servo_DeclarationBlock_CreateEmpty().Consume();
for (size_t i = 0; i < aRuleDataSize; i++) {
nsCSSValue& val = aRuleData->mValueStorage[i];
if (val.GetUnit() != eCSSUnit_Null) {
Servo_DeclarationBlock_AddPresValue(mServoStyle.get(), aIndexToIdMapping[i], &val);
}
}
}

View File

@ -16,6 +16,7 @@
#include "nsMappedAttributeElement.h"
#include "nsIStyleRule.h"
#include "mozilla/Attributes.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/MemoryReporting.h"
class nsIAtom;
@ -70,9 +71,27 @@ public:
void RemoveAttrAt(uint32_t aPos, nsAttrValue& aValue);
const nsAttrName* GetExistingAttrNameFromQName(const nsAString& aName) const;
int32_t IndexOfAttr(nsIAtom* aLocalName) const;
// nsIStyleRule
// Apply the contained mapper to an empty nsRuleData object
// that is able to contain all properties. Set contained servo declaration block
// to result of this computation.
// aIndexToIdMapping is
// a table that maps from an index in the rule data to the corresponding
// property ID. aRuleDataSize is the length of the backing storage
// of the rule data.
void LazilyResolveServoDeclaration(nsRuleData* aRuleData,
nsCSSPropertyID* aIndexToIdMapping,
size_t aRuleDataSize);
// Obtain the contained servo declaration block
// May return null if called before the inner block
// has been (lazily) resolved
const RefPtr<RawServoDeclarationBlock>& GetServoStyle() const
{
return mServoStyle;
}
// nsIStyleRule
virtual void MapRuleInfoInto(nsRuleData* aRuleData) override;
virtual bool MightMapInheritedStyleData() override;
virtual bool GetDiscretelyAnimatedCSSValue(nsCSSPropertyID aProperty,
@ -115,6 +134,7 @@ private:
#endif
nsHTMLStyleSheet* mSheet; //weak
nsMapRuleToAttributesFunc mRuleMapper;
RefPtr<RawServoDeclarationBlock> mServoStyle;
void* mAttrs[1];
};

View File

@ -172,6 +172,10 @@ SERVO_BINDING_FUNC(Servo_DeclarationBlock_RemoveProperty, void,
SERVO_BINDING_FUNC(Servo_DeclarationBlock_RemovePropertyById, void,
RawServoDeclarationBlockBorrowed declarations,
nsCSSPropertyID property)
SERVO_BINDING_FUNC(Servo_DeclarationBlock_AddPresValue, void,
RawServoDeclarationBlockBorrowed declarations,
nsCSSPropertyID property,
nsCSSValueBorrowedMut css_value)
// CSS supports()
SERVO_BINDING_FUNC(Servo_CSSSupports2, bool,
@ -222,6 +226,8 @@ SERVO_BINDING_FUNC(Servo_ResolveStyleLazily, ServoComputedValuesStrong,
RawServoStyleSetBorrowed set)
// Restyle the given subtree.
// Use ServoStyleSet::PrepareAndTraverseSubtree instead of calling this
// directly
SERVO_BINDING_FUNC(Servo_TraverseSubtree, void,
RawGeckoElementBorrowed root, RawServoStyleSetBorrowed set,
mozilla::TraversalRootBehavior root_behavior)

View File

@ -68,6 +68,7 @@ typedef nsTArray<const RawServoAnimationValue*> RawServoAnimationValueBorrowedLi
#define SERVO_ARC_TYPE(name_, type_) \
DECL_NULLABLE_BORROWED_REF_TYPE_FOR(type_) \
DECL_BORROWED_REF_TYPE_FOR(type_) \
DECL_BORROWED_MUT_REF_TYPE_FOR(type_) \
struct MOZ_MUST_USE_TYPE type_##Strong \
{ \
type_* mPtr; \

View File

@ -21,6 +21,7 @@
#include "nsIFrame.h"
#include "nsINode.h"
#include "nsIPrincipal.h"
#include "nsMappedAttributes.h"
#include "nsMediaFeatures.h"
#include "nsNameSpaceManager.h"
#include "nsNetUtil.h"
@ -312,7 +313,7 @@ Gecko_DropElementSnapshot(ServoElementSnapshotOwned aSnapshot)
}
RawServoDeclarationBlockStrongBorrowedOrNull
Gecko_GetServoDeclarationBlock(RawGeckoElementBorrowed aElement)
Gecko_GetStyleAttrDeclarationBlock(RawGeckoElementBorrowed aElement)
{
DeclarationBlock* decl = aElement->GetInlineStyleDeclaration();
if (!decl) {
@ -328,6 +329,21 @@ Gecko_GetServoDeclarationBlock(RawGeckoElementBorrowed aElement)
(decl->AsServo()->RefRaw());
}
RawServoDeclarationBlockStrongBorrowedOrNull
Gecko_GetHTMLPresentationAttrDeclarationBlock(RawGeckoElementBorrowed aElement)
{
static_assert(sizeof(RefPtr<RawServoDeclarationBlock>) ==
sizeof(RawServoDeclarationBlockStrong),
"RefPtr should just be a pointer");
const nsMappedAttributes* attrs = aElement->GetMappedAttributes();
if (!attrs) {
return nullptr;
}
const RefPtr<RawServoDeclarationBlock>& servo = attrs->GetServoStyle();
return reinterpret_cast<const RawServoDeclarationBlockStrong*>(&servo);
}
RawServoDeclarationBlockStrong
Gecko_GetAnimationRule(RawGeckoElementBorrowed aElement,
nsIAtom* aPseudoTag,

View File

@ -158,7 +158,9 @@ SERVO_DECLARE_ELEMENT_ATTR_MATCHING_FUNCTIONS(Gecko_Snapshot,
// Style attributes.
RawServoDeclarationBlockStrongBorrowedOrNull
Gecko_GetServoDeclarationBlock(RawGeckoElementBorrowed element);
Gecko_GetStyleAttrDeclarationBlock(RawGeckoElementBorrowed element);
RawServoDeclarationBlockStrongBorrowedOrNull
Gecko_GetHTMLPresentationAttrDeclarationBlock(RawGeckoElementBorrowed element);
// Animations
RawServoDeclarationBlockStrong

View File

@ -14,6 +14,7 @@
#include "nsAnimationManager.h"
#include "nsCSSAnonBoxes.h"
#include "nsCSSPseudoElements.h"
#include "nsHTMLStyleSheet.h"
#include "nsIDocumentInlines.h"
#include "nsPrintfCString.h"
#include "nsStyleContext.h"
@ -127,6 +128,8 @@ ServoStyleSet::GetContext(nsIContent* aContent,
MOZ_ASSERT(aContent->IsElement());
Element* element = aContent->AsElement();
ResolveMappedAttrDeclarationBlocks();
RefPtr<ServoComputedValues> computedValues;
if (aMayCompute == LazyComputeBehavior::Allow) {
computedValues =
@ -176,6 +179,21 @@ ServoStyleSet::GetContext(already_AddRefed<ServoComputedValues> aComputedValues,
return result.forget();
}
void
ServoStyleSet::ResolveMappedAttrDeclarationBlocks()
{
if (nsHTMLStyleSheet* sheet = mPresContext->Document()->GetAttributeStyleSheet()) {
sheet->CalculateMappedServoDeclarations();
}
}
void
ServoStyleSet::PrepareAndTraverseSubtree(RawGeckoElementBorrowed aRoot,
mozilla::TraversalRootBehavior aRootBehavior) {
ResolveMappedAttrDeclarationBlocks();
Servo_TraverseSubtree(aRoot, mRawSet.get(), aRootBehavior);
}
already_AddRefed<nsStyleContext>
ServoStyleSet::ResolveStyleFor(Element* aElement,
nsStyleContext* aParentContext,
@ -541,7 +559,7 @@ ServoStyleSet::StyleDocument()
DocumentStyleRootIterator iter(mPresContext->Document());
while (Element* root = iter.GetNextStyleRoot()) {
if (root->ShouldTraverseForServo()) {
Servo_TraverseSubtree(root, mRawSet.get(), TraversalRootBehavior::Normal);
PrepareAndTraverseSubtree(root, TraversalRootBehavior::Normal);
}
}
}
@ -550,14 +568,13 @@ void
ServoStyleSet::StyleNewSubtree(Element* aRoot)
{
MOZ_ASSERT(!aRoot->HasServoData());
Servo_TraverseSubtree(aRoot, mRawSet.get(), TraversalRootBehavior::Normal);
PrepareAndTraverseSubtree(aRoot, TraversalRootBehavior::Normal);
}
void
ServoStyleSet::StyleNewChildren(Element* aParent)
{
Servo_TraverseSubtree(aParent, mRawSet.get(),
TraversalRootBehavior::UnstyledChildrenOnly);
PrepareAndTraverseSubtree(aParent, TraversalRootBehavior::UnstyledChildrenOnly);
}
void

View File

@ -199,6 +199,20 @@ private:
CSSPseudoElementType aPseudoType,
LazyComputeBehavior aMayCompute);
/**
* Resolve all ServoDeclarationBlocks attached to mapped
* presentation attributes cached on the document.
* Call this before jumping into Servo's style system.
*/
void ResolveMappedAttrDeclarationBlocks();
/**
* Perform all lazy operations required before traversing
* a subtree.
*/
void PrepareAndTraverseSubtree(RawGeckoElementBorrowed aRoot,
mozilla::TraversalRootBehavior aRootBehavior);
nsPresContext* mPresContext;
UniquePtr<RawServoStyleSet> mRawSet;
EnumeratedArray<SheetType, SheetType::Count,

View File

@ -289,6 +289,7 @@ nsHTMLStyleSheet::nsHTMLStyleSheet(nsIDocument* aDocument)
, mTableQuirkColorRule(new TableQuirkColorRule())
, mTableTHRule(new TableTHRule())
, mMappedAttrTable(&MappedAttrTable_Ops, sizeof(MappedAttrTableEntry))
, mMappedAttrsDirty(false)
, mLangRuleTable(&LangRuleTable_Ops, sizeof(LangRuleTableEntry))
{
MOZ_ASSERT(aDocument);
@ -478,6 +479,7 @@ nsHTMLStyleSheet::Reset()
mLangRuleTable.Clear();
mMappedAttrTable.Clear();
mMappedAttrsDirty = false;
}
nsresult
@ -526,6 +528,7 @@ nsHTMLStyleSheet::SetVisitedLinkColor(nscolor aColor)
already_AddRefed<nsMappedAttributes>
nsHTMLStyleSheet::UniqueMappedAttributes(nsMappedAttributes* aMapped)
{
mMappedAttrsDirty = true;
auto entry = static_cast<MappedAttrTableEntry*>
(mMappedAttrTable.Add(aMapped, fallible));
if (!entry)
@ -542,7 +545,6 @@ void
nsHTMLStyleSheet::DropMappedAttributes(nsMappedAttributes* aMapped)
{
NS_ENSURE_TRUE_VOID(aMapped);
#ifdef DEBUG
uint32_t entryCount = mMappedAttrTable.EntryCount() - 1;
#endif
@ -552,6 +554,97 @@ nsHTMLStyleSheet::DropMappedAttributes(nsMappedAttributes* aMapped)
NS_ASSERTION(entryCount == mMappedAttrTable.EntryCount(), "not removed");
}
namespace {
// Struct containing once-initialized information about
// our synthesized rule data
struct StaticRuleDataInfo
{
// the bitmask used.
uint64_t mMask;
// the number of properties contained
size_t mPropCount;
// offset of given style struct in array
size_t mOffsets[nsStyleStructID_Length];
// location of property in given array
nsCSSPropertyID* mIndexToPropertyMapping;
};
}
static void
CalculateIndexArray(StaticRuleDataInfo* aInfo)
{
// this will leak at shutdown, but it's not much and this code is temporary
// anyway.
aInfo->mIndexToPropertyMapping = new nsCSSPropertyID[aInfo->mPropCount];
size_t structOffset;
size_t propertyIndex;
#define CSS_PROP_LIST_EXCLUDE_LOGICAL
#define CSS_PROP(name_, id_, method_, flags_, pref_, parsevariant_, \
kwtable_, stylestruct_, stylestructoffset_, animtype_) \
structOffset = aInfo->mOffsets[eStyleStruct_##stylestruct_]; \
propertyIndex = nsCSSProps::PropertyIndexInStruct(eCSSProperty_##id_); \
aInfo->mIndexToPropertyMapping[structOffset + propertyIndex] = eCSSProperty_##id_;
#include "nsCSSPropList.h"
#undef CSS_PROP
#undef CSS_PROP_LIST_EXCLUDE_LOGICAL
}
static StaticRuleDataInfo
CalculateRuleDataInfo()
{
StaticRuleDataInfo sizes;
sizes.mMask = 0;
sizes.mPropCount = 0;
#define STYLE_STRUCT(name, checkdata_cb) \
sizes.mMask |= NS_STYLE_INHERIT_BIT(name); \
sizes.mOffsets[eStyleStruct_##name] = sizes.mPropCount; \
sizes.mPropCount += nsCSSProps::PropertyCountInStruct(eStyleStruct_##name);
#include "nsStyleStructList.h"
#undef STYLE_STRUCT
CalculateIndexArray(&sizes);
return sizes;
}
void
nsHTMLStyleSheet::CalculateMappedServoDeclarations()
{
// avoid recalculating or reallocating
static StaticRuleDataInfo sizes = CalculateRuleDataInfo();
if (!mMappedAttrsDirty) {
return;
}
mMappedAttrsDirty = false;
void* dataStorage = alloca(sizes.mPropCount * sizeof(nsCSSValue));
for (auto iter = mMappedAttrTable.Iter(); !iter.Done(); iter.Next()) {
MappedAttrTableEntry* attr = static_cast<MappedAttrTableEntry*>(iter.Get());
if (attr->mAttributes->GetServoStyle()) {
// Only handle cases which haven't been filled in already
continue;
}
// Construction cleans up any values we may have set
AutoCSSValueArray dataArray(dataStorage, sizes.mPropCount);
// synthesized ruleData
// we pass null for the style context because the code we call into
// doesn't deal with the style context. This is temporary.
nsRuleData ruleData(sizes.mMask, dataArray.get(),
mDocument->GetShell()->GetPresContext(), nullptr);
// Copy the offsets; ruleData won't know where to find properties otherwise
mozilla::PodCopy(ruleData.mValueOffsets,
sizes.mOffsets,
nsStyleStructID_Length);
attr->mAttributes->LazilyResolveServoDeclaration(&ruleData,
sizes.mIndexToPropertyMapping,
sizes.mPropCount);
}
}
nsIStyleRule*
nsHTMLStyleSheet::LangRuleFor(const nsString& aLanguage)
{

View File

@ -62,6 +62,10 @@ public:
already_AddRefed<nsMappedAttributes>
UniqueMappedAttributes(nsMappedAttributes* aMapped);
void DropMappedAttributes(nsMappedAttributes* aMapped);
// For each mapped presentation attribute in the cache, resolve
// the attached ServoDeclarationBlock by running the mapping
// and converting the ruledata to Servo specified values.
void CalculateMappedServoDeclarations();
nsIStyleRule* LangRuleFor(const nsString& aLanguage);
@ -175,6 +179,10 @@ private:
RefPtr<TableTHRule> mTableTHRule;
PLDHashTable mMappedAttrTable;
// Whether or not the mapped attributes table
// has been changed since the last call to
// CalculateMappedServoDeclarations()
bool mMappedAttrsDirty;
PLDHashTable mLangRuleTable;
};

View File

@ -2354,44 +2354,25 @@ UnsetPropertiesWithoutFlags(const nsStyleStructID aSID,
}
}
/**
* We allocate arrays of CSS values with alloca. (These arrays are a
* fixed size per style struct, but we don't want to waste the
* allocation and construction/destruction costs of the big structs when
* we're handling much smaller ones.) Since the lifetime of an alloca
* allocation is the life of the calling function, the caller must call
* alloca. However, to ensure that constructors and destructors are
* balanced, we do the constructor and destructor calling from this RAII
* class, AutoCSSValueArray.
*/
struct AutoCSSValueArray {
/**
* aStorage must be the result of alloca(aCount * sizeof(nsCSSValue))
*/
AutoCSSValueArray(void* aStorage, size_t aCount) {
MOZ_ASSERT(size_t(aStorage) % NS_ALIGNMENT_OF(nsCSSValue) == 0,
"bad alignment from alloca");
mCount = aCount;
// Don't use placement new[], since it might store extra data
// for the count (on Windows!).
mArray = static_cast<nsCSSValue*>(aStorage);
for (size_t i = 0; i < mCount; ++i) {
new (KnownNotNull, mArray + i) nsCSSValue();
}
AutoCSSValueArray::AutoCSSValueArray(void* aStorage, size_t aCount)
{
MOZ_ASSERT(size_t(aStorage) % NS_ALIGNMENT_OF(nsCSSValue) == 0,
"bad alignment from alloca");
mCount = aCount;
// Don't use placement new[], since it might store extra data
// for the count (on Windows!).
mArray = static_cast<nsCSSValue*>(aStorage);
for (size_t i = 0; i < mCount; ++i) {
new (KnownNotNull, mArray + i) nsCSSValue();
}
}
~AutoCSSValueArray() {
for (size_t i = 0; i < mCount; ++i) {
mArray[i].~nsCSSValue();
}
AutoCSSValueArray::~AutoCSSValueArray()
{
for (size_t i = 0; i < mCount; ++i) {
mArray[i].~nsCSSValue();
}
nsCSSValue* get() { return mArray; }
private:
nsCSSValue *mArray;
size_t mCount;
};
}
/* static */ bool
nsRuleNode::ResolveVariableReferences(const nsStyleStructID aSID,

View File

@ -1085,4 +1085,30 @@ private:
void* aStruct);
};
/**
* We allocate arrays of CSS values with alloca. (These arrays are a
* fixed size per style struct, but we don't want to waste the
* allocation and construction/destruction costs of the big structs when
* we're handling much smaller ones.) Since the lifetime of an alloca
* allocation is the life of the calling function, the caller must call
* alloca. However, to ensure that constructors and destructors are
* balanced, we do the constructor and destructor calling from this RAII
* class, AutoCSSValueArray.
*/
struct AutoCSSValueArray
{
/**
* aStorage must be the result of alloca(aCount * sizeof(nsCSSValue))
*/
AutoCSSValueArray(void* aStorage, size_t aCount);
~AutoCSSValueArray();
nsCSSValue* get() { return mArray; }
private:
nsCSSValue *mArray;
size_t mCount;
};
#endif