2016-02-24 10:01:12 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "mozilla/ServoRestyleManager.h"
|
2016-07-08 10:08:46 +03:00
|
|
|
#include "mozilla/ServoStyleSet.h"
|
2016-02-24 10:01:12 +03:00
|
|
|
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2016-07-08 10:08:46 +03:00
|
|
|
ServoRestyleManager::ServoRestyleManager(nsPresContext* aPresContext)
|
|
|
|
: RestyleManagerBase(aPresContext)
|
2016-05-05 07:01:37 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-02-24 10:01:12 +03:00
|
|
|
void
|
|
|
|
ServoRestyleManager::PostRestyleEvent(Element* aElement,
|
|
|
|
nsRestyleHint aRestyleHint,
|
|
|
|
nsChangeHint aMinChangeHint)
|
|
|
|
{
|
2016-07-08 10:08:46 +03:00
|
|
|
if (MOZ_UNLIKELY(IsDisconnected()) ||
|
|
|
|
MOZ_UNLIKELY(PresContext()->PresShell()->IsDestroying())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-19 04:02:55 +03:00
|
|
|
if (aRestyleHint == 0 && !aMinChangeHint && !HasPendingRestyles()) {
|
|
|
|
return; // Nothing to do.
|
2016-07-13 23:42:47 +03:00
|
|
|
}
|
|
|
|
|
2016-07-19 04:02:55 +03:00
|
|
|
// Note that unlike in Servo, we don't mark elements as dirty until we process
|
|
|
|
// the restyle hints in ProcessPendingRestyles.
|
|
|
|
if (aRestyleHint || aMinChangeHint) {
|
|
|
|
ServoElementSnapshot* snapshot = SnapshotForElement(aElement);
|
|
|
|
snapshot->AddExplicitRestyleHint(aRestyleHint);
|
|
|
|
snapshot->AddExplicitChangeHint(aMinChangeHint);
|
2016-07-08 10:08:46 +03:00
|
|
|
}
|
|
|
|
|
2016-07-19 04:02:55 +03:00
|
|
|
nsIPresShell* presShell = PresContext()->PresShell();
|
|
|
|
if (!ObservingRefreshDriver()) {
|
|
|
|
SetObservingRefreshDriver(
|
|
|
|
PresContext()->RefreshDriver()->AddStyleFlushObserver(presShell));
|
2016-07-08 10:08:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
presShell->GetDocument()->SetNeedStyleFlush();
|
2016-02-24 10:01:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServoRestyleManager::PostRestyleEventForLazyConstruction()
|
|
|
|
{
|
2016-05-05 07:01:37 +03:00
|
|
|
NS_ERROR("stylo: ServoRestyleManager::PostRestyleEventForLazyConstruction not implemented");
|
2016-02-24 10:01:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServoRestyleManager::RebuildAllStyleData(nsChangeHint aExtraHint,
|
|
|
|
nsRestyleHint aRestyleHint)
|
|
|
|
{
|
2016-05-05 07:01:37 +03:00
|
|
|
NS_ERROR("stylo: ServoRestyleManager::RebuildAllStyleData not implemented");
|
2016-02-24 10:01:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServoRestyleManager::PostRebuildAllStyleDataEvent(nsChangeHint aExtraHint,
|
|
|
|
nsRestyleHint aRestyleHint)
|
|
|
|
{
|
2016-05-05 07:01:37 +03:00
|
|
|
MOZ_CRASH("stylo: ServoRestyleManager::PostRebuildAllStyleDataEvent not implemented");
|
2016-02-24 10:01:12 +03:00
|
|
|
}
|
|
|
|
|
2016-07-13 23:42:47 +03:00
|
|
|
/* static */ void
|
|
|
|
ServoRestyleManager::RecreateStyleContexts(nsIContent* aContent,
|
|
|
|
nsStyleContext* aParentContext,
|
|
|
|
ServoStyleSet* aStyleSet)
|
|
|
|
{
|
|
|
|
nsIFrame* primaryFrame = aContent->GetPrimaryFrame();
|
|
|
|
|
|
|
|
// TODO: AFAIK this can happen when we have, let's say, display: none. Here we
|
|
|
|
// should trigger frame construction if the element is actually dirty (I
|
|
|
|
// guess), but we'd better do that once we have all the restyle hints thing
|
|
|
|
// figured out.
|
|
|
|
if (!primaryFrame) {
|
2016-07-15 21:48:45 +03:00
|
|
|
aContent->UnsetFlags(NODE_IS_DIRTY_FOR_SERVO | NODE_HAS_DIRTY_DESCENDANTS_FOR_SERVO);
|
2016-07-13 23:42:47 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-15 21:48:45 +03:00
|
|
|
if (aContent->IsDirtyForServo()) {
|
|
|
|
RefPtr<ServoComputedValues> computedValues =
|
|
|
|
dont_AddRef(Servo_GetComputedValues(aContent));
|
|
|
|
|
|
|
|
// TODO: Figure out what pseudos does this content have, and do the proper
|
|
|
|
// thing with them.
|
|
|
|
RefPtr<nsStyleContext> context =
|
|
|
|
aStyleSet->GetContext(computedValues.forget(),
|
|
|
|
aParentContext,
|
|
|
|
nullptr,
|
|
|
|
CSSPseudoElementType::NotPseudo);
|
|
|
|
|
|
|
|
// TODO: Compare old and new styles to generate restyle change hints, and
|
|
|
|
// process them.
|
|
|
|
primaryFrame->SetStyleContext(context.get());
|
|
|
|
|
|
|
|
aContent->UnsetFlags(NODE_IS_DIRTY_FOR_SERVO);
|
|
|
|
}
|
2016-07-13 23:42:47 +03:00
|
|
|
|
2016-07-15 21:48:45 +03:00
|
|
|
if (aContent->HasDirtyDescendantsForServo()) {
|
|
|
|
FlattenedChildIterator it(aContent);
|
|
|
|
for (nsIContent* n = it.GetNextChild(); n; n = it.GetNextChild()) {
|
|
|
|
RecreateStyleContexts(n, primaryFrame->StyleContext(), aStyleSet);
|
|
|
|
}
|
|
|
|
aContent->UnsetFlags(NODE_HAS_DIRTY_DESCENDANTS_FOR_SERVO);
|
2016-07-13 23:42:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 04:02:55 +03:00
|
|
|
static void
|
|
|
|
MarkParentsAsHavingDirtyDescendants(Element* aElement)
|
|
|
|
{
|
|
|
|
nsINode* cur = aElement;
|
|
|
|
while ((cur = cur->GetParentNode())) {
|
|
|
|
if (cur->HasDirtyDescendantsForServo()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur->SetHasDirtyDescendantsForServo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-23 01:19:30 +03:00
|
|
|
static void
|
|
|
|
MarkChildrenAsDirtyForServo(nsIContent* aContent)
|
|
|
|
{
|
|
|
|
FlattenedChildIterator it(aContent);
|
|
|
|
|
|
|
|
nsIContent* n = it.GetNextChild();
|
|
|
|
bool hadChildren = bool(n);
|
|
|
|
for (; n; n = it.GetNextChild()) {
|
|
|
|
n->SetIsDirtyForServo();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hadChildren) {
|
|
|
|
aContent->SetHasDirtyDescendantsForServo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 04:02:55 +03:00
|
|
|
void
|
|
|
|
ServoRestyleManager::NoteRestyleHint(Element* aElement, nsRestyleHint aHint)
|
|
|
|
{
|
|
|
|
if (aHint & eRestyle_Self) {
|
|
|
|
aElement->SetIsDirtyForServo();
|
|
|
|
MarkParentsAsHavingDirtyDescendants(aElement);
|
2016-07-23 01:19:30 +03:00
|
|
|
// NB: For Servo, at least for now, restyling and running selector-matching
|
|
|
|
// against the subtree is necessary as part of restyling the element, so
|
|
|
|
// processing eRestyle_Self will perform at least as much work as
|
|
|
|
// eRestyle_Subtree.
|
|
|
|
} else if (aHint & eRestyle_Subtree) {
|
|
|
|
MarkChildrenAsDirtyForServo(aElement);
|
2016-07-19 04:02:55 +03:00
|
|
|
MarkParentsAsHavingDirtyDescendants(aElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aHint & eRestyle_LaterSiblings) {
|
|
|
|
for (nsINode* cur = aElement->GetNextSibling(); cur;
|
|
|
|
cur = cur->GetNextSibling()) {
|
|
|
|
if (cur->IsContent()) {
|
2016-07-23 01:19:30 +03:00
|
|
|
cur->SetIsDirtyForServo();
|
2016-07-19 04:02:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Handle all other nsRestyleHint values.
|
|
|
|
if (aHint & ~(eRestyle_Self | eRestyle_Subtree | eRestyle_LaterSiblings)) {
|
2016-07-23 01:27:05 +03:00
|
|
|
NS_ERROR(nsPrintfCString("stylo: Unhandled restyle hint %s",
|
|
|
|
RestyleManagerBase::RestyleHintToString(aHint).get()).get());
|
2016-07-19 04:02:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-24 10:01:12 +03:00
|
|
|
void
|
|
|
|
ServoRestyleManager::ProcessPendingRestyles()
|
|
|
|
{
|
2016-07-18 21:28:27 +03:00
|
|
|
if (!HasPendingRestyles()) {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-13 23:42:47 +03:00
|
|
|
ServoStyleSet* styleSet = StyleSet();
|
|
|
|
|
|
|
|
nsIDocument* doc = PresContext()->Document();
|
|
|
|
|
2016-07-18 21:28:27 +03:00
|
|
|
Element* root = doc->GetRootElement();
|
2016-07-13 23:42:47 +03:00
|
|
|
if (root) {
|
2016-07-19 04:02:55 +03:00
|
|
|
for (auto iter = mModifiedElements.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
ServoElementSnapshot* snapshot = iter.UserData();
|
|
|
|
Element* element = iter.Key();
|
|
|
|
|
|
|
|
// TODO: avoid the ComputeRestyleHint call if we already have the highest
|
|
|
|
// explicit restyle hint?
|
|
|
|
nsRestyleHint hint = styleSet->ComputeRestyleHint(element, snapshot);
|
|
|
|
hint |= snapshot->ExplicitRestyleHint();
|
|
|
|
|
|
|
|
if (hint) {
|
|
|
|
NoteRestyleHint(element, hint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 23:42:47 +03:00
|
|
|
styleSet->RestyleSubtree(root, /* aForce = */ false);
|
|
|
|
RecreateStyleContexts(root, nullptr, styleSet);
|
|
|
|
}
|
|
|
|
|
2016-07-19 04:02:55 +03:00
|
|
|
mModifiedElements.Clear();
|
|
|
|
|
2016-07-18 21:28:27 +03:00
|
|
|
// NB: we restyle from the root element, but the document also gets the
|
|
|
|
// HAS_DIRTY_DESCENDANTS flag as part of the loop on PostRestyleEvent, and we
|
|
|
|
// use that to check we have pending restyles.
|
|
|
|
//
|
|
|
|
// Thus, they need to get cleared here.
|
|
|
|
MOZ_ASSERT(!doc->IsDirtyForServo());
|
|
|
|
doc->UnsetFlags(NODE_HAS_DIRTY_DESCENDANTS_FOR_SERVO);
|
|
|
|
|
2016-07-08 10:08:46 +03:00
|
|
|
IncrementRestyleGeneration();
|
2016-02-24 10:01:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServoRestyleManager::RestyleForInsertOrChange(Element* aContainer,
|
|
|
|
nsIContent* aChild)
|
|
|
|
{
|
2016-05-05 07:01:37 +03:00
|
|
|
NS_ERROR("stylo: ServoRestyleManager::RestyleForInsertOrChange not implemented");
|
2016-02-24 10:01:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServoRestyleManager::RestyleForAppend(Element* aContainer,
|
|
|
|
nsIContent* aFirstNewContent)
|
|
|
|
{
|
2016-05-05 07:01:37 +03:00
|
|
|
NS_ERROR("stylo: ServoRestyleManager::RestyleForAppend not implemented");
|
2016-02-24 10:01:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServoRestyleManager::RestyleForRemove(Element* aContainer,
|
|
|
|
nsIContent* aOldChild,
|
|
|
|
nsIContent* aFollowingSibling)
|
|
|
|
{
|
2016-05-05 07:01:37 +03:00
|
|
|
NS_ERROR("stylo: ServoRestyleManager::RestyleForRemove not implemented");
|
2016-02-24 10:01:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ServoRestyleManager::ContentStateChanged(nsIContent* aContent,
|
2016-07-19 04:02:55 +03:00
|
|
|
EventStates aChangedBits)
|
2016-02-24 10:01:12 +03:00
|
|
|
{
|
2016-07-08 10:08:46 +03:00
|
|
|
if (!aContent->IsElement()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Element* aElement = aContent->AsElement();
|
|
|
|
nsChangeHint changeHint;
|
|
|
|
nsRestyleHint restyleHint;
|
2016-07-19 04:02:55 +03:00
|
|
|
|
|
|
|
// NOTE: restyleHint here is effectively always 0, since that's what
|
|
|
|
// ServoStyleSet::HasStateDependentStyle returns. Servo computes on
|
|
|
|
// ProcessPendingRestyles using the ElementSnapshot, but in theory could
|
|
|
|
// compute it sequentially easily.
|
|
|
|
//
|
|
|
|
// Determine what's the best way to do it, and how much work do we save
|
|
|
|
// processing the restyle hint early (i.e., computing the style hint here
|
|
|
|
// sequentially, potentially saving the snapshot), vs lazily (snapshot
|
|
|
|
// approach).
|
|
|
|
//
|
|
|
|
// If we take the sequential approach we need to specialize Servo's restyle
|
|
|
|
// hints system a bit more, and mesure whether we save something storing the
|
|
|
|
// restyle hint in the table and deferring the dirtiness setting until
|
|
|
|
// ProcessPendingRestyles (that's a requirement if we store snapshots though),
|
|
|
|
// vs processing the restyle hint in-place, dirtying the nodes on
|
|
|
|
// PostRestyleEvent.
|
|
|
|
//
|
|
|
|
// If we definitely take the snapshot approach, we should take rid of
|
|
|
|
// HasStateDependentStyle, etc (though right now they're no-ops).
|
|
|
|
ContentStateChangedInternal(aElement, aChangedBits, &changeHint,
|
|
|
|
&restyleHint);
|
|
|
|
|
|
|
|
EventStates previousState = aElement->StyleState() ^ aChangedBits;
|
|
|
|
ServoElementSnapshot* snapshot = SnapshotForElement(aElement);
|
|
|
|
snapshot->AddState(previousState);
|
2016-07-08 10:08:46 +03:00
|
|
|
|
2016-07-18 21:28:57 +03:00
|
|
|
PostRestyleEvent(aElement, restyleHint, changeHint);
|
2016-05-05 07:01:37 +03:00
|
|
|
return NS_OK;
|
2016-02-24 10:01:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServoRestyleManager::AttributeWillChange(Element* aElement,
|
|
|
|
int32_t aNameSpaceID,
|
2016-07-19 04:02:55 +03:00
|
|
|
nsIAtom* aAttribute, int32_t aModType,
|
2016-02-24 10:01:12 +03:00
|
|
|
const nsAttrValue* aNewValue)
|
|
|
|
{
|
2016-07-20 00:35:37 +03:00
|
|
|
ServoElementSnapshot* snapshot = SnapshotForElement(aElement);
|
|
|
|
snapshot->AddAttrs(aElement);
|
2016-02-24 10:01:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-07-19 04:02:55 +03:00
|
|
|
ServoRestyleManager::AttributeChanged(Element* aElement, int32_t aNameSpaceID,
|
|
|
|
nsIAtom* aAttribute, int32_t aModType,
|
2016-02-24 10:01:12 +03:00
|
|
|
const nsAttrValue* aOldValue)
|
|
|
|
{
|
2016-05-05 07:01:37 +03:00
|
|
|
NS_ERROR("stylo: ServoRestyleManager::AttributeChanged not implemented");
|
2016-02-24 10:01:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ServoRestyleManager::ReparentStyleContext(nsIFrame* aFrame)
|
|
|
|
{
|
2016-05-05 07:01:37 +03:00
|
|
|
MOZ_CRASH("stylo: ServoRestyleManager::ReparentStyleContext not implemented");
|
2016-02-24 10:01:12 +03:00
|
|
|
}
|
|
|
|
|
2016-07-19 04:02:55 +03:00
|
|
|
ServoElementSnapshot*
|
|
|
|
ServoRestyleManager::SnapshotForElement(Element* aElement)
|
|
|
|
{
|
|
|
|
ServoElementSnapshot* snapshot = mModifiedElements.LookupOrAdd(aElement);
|
|
|
|
if (!snapshot->HasAny(
|
|
|
|
ServoElementSnapshot::Flags::HTMLElementInHTMLDocument)) {
|
|
|
|
snapshot->SetIsHTMLElementInHTMLDocument(
|
|
|
|
aElement->IsHTMLElement() && aElement->OwnerDoc()->IsHTMLDocument());
|
|
|
|
}
|
|
|
|
return snapshot;
|
|
|
|
}
|
|
|
|
|
2016-02-24 10:01:12 +03:00
|
|
|
} // namespace mozilla
|