michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* Code to start and animate CSS transitions. */ michael@0: michael@0: #include "nsTransitionManager.h" michael@0: #include "nsAnimationManager.h" michael@0: #include "nsIContent.h" michael@0: #include "nsStyleContext.h" michael@0: #include "nsCSSProps.h" michael@0: #include "mozilla/MemoryReporting.h" michael@0: #include "mozilla/TimeStamp.h" michael@0: #include "nsRefreshDriver.h" michael@0: #include "nsRuleProcessorData.h" michael@0: #include "nsRuleWalker.h" michael@0: #include "nsCSSPropertySet.h" michael@0: #include "nsStyleAnimation.h" michael@0: #include "mozilla/EventDispatcher.h" michael@0: #include "mozilla/ContentEvents.h" michael@0: #include "mozilla/dom/Element.h" michael@0: #include "nsIFrame.h" michael@0: #include "Layers.h" michael@0: #include "FrameLayerBuilder.h" michael@0: #include "nsDisplayList.h" michael@0: #include "nsStyleChangeList.h" michael@0: #include "nsStyleSet.h" michael@0: #include "RestyleManager.h" michael@0: #include "ActiveLayerTracker.h" michael@0: michael@0: using mozilla::TimeStamp; michael@0: using mozilla::TimeDuration; michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::layers; michael@0: using namespace mozilla::css; michael@0: michael@0: ElementTransitions::ElementTransitions(mozilla::dom::Element *aElement, michael@0: nsIAtom *aElementProperty, michael@0: nsTransitionManager *aTransitionManager, michael@0: TimeStamp aNow) michael@0: : CommonElementAnimationData(aElement, aElementProperty, michael@0: aTransitionManager, aNow) michael@0: { michael@0: } michael@0: michael@0: double michael@0: ElementPropertyTransition::ValuePortionFor(TimeStamp aRefreshTime) const michael@0: { michael@0: // Set |timePortion| to the portion of the way we are through the time michael@0: // input to the transition's timing function (always within the range michael@0: // 0-1). michael@0: double duration = mIterationDuration.ToSeconds(); michael@0: NS_ABORT_IF_FALSE(duration >= 0.0, "negative duration forbidden"); michael@0: double timePortion; michael@0: if (IsRemovedSentinel()) { michael@0: // The transition is being removed, but we still want an update so that any michael@0: // new transitions start in the right place. michael@0: timePortion = 1.0; michael@0: } else if (duration == 0.0) { michael@0: // When duration is zero, we can still have a transition when delay michael@0: // is nonzero. michael@0: if (aRefreshTime >= mStartTime + mDelay) { michael@0: timePortion = 1.0; michael@0: } else { michael@0: timePortion = 0.0; michael@0: } michael@0: } else { michael@0: timePortion = (aRefreshTime - (mStartTime + mDelay)).ToSeconds() / duration; michael@0: if (timePortion < 0.0) michael@0: timePortion = 0.0; // use start value during transition-delay michael@0: if (timePortion > 1.0) michael@0: timePortion = 1.0; // we might be behind on flushing michael@0: } michael@0: MOZ_ASSERT(mProperties.Length() == 1, michael@0: "Should have one animation property for a transition"); michael@0: MOZ_ASSERT(mProperties[0].mSegments.Length() == 1, michael@0: "Animation property should have one segment for a transition"); michael@0: michael@0: return mProperties[0].mSegments[0].mTimingFunction.GetValue(timePortion); michael@0: } michael@0: michael@0: static void michael@0: ElementTransitionsPropertyDtor(void *aObject, michael@0: nsIAtom *aPropertyName, michael@0: void *aPropertyValue, michael@0: void *aData) michael@0: { michael@0: ElementTransitions *et = static_cast(aPropertyValue); michael@0: #ifdef DEBUG michael@0: NS_ABORT_IF_FALSE(!et->mCalledPropertyDtor, "can't call dtor twice"); michael@0: et->mCalledPropertyDtor = true; michael@0: #endif michael@0: delete et; michael@0: } michael@0: michael@0: void michael@0: ElementTransitions::EnsureStyleRuleFor(TimeStamp aRefreshTime) michael@0: { michael@0: if (!mStyleRule || mStyleRuleRefreshTime != aRefreshTime) { michael@0: mStyleRule = new css::AnimValuesStyleRule(); michael@0: mStyleRuleRefreshTime = aRefreshTime; michael@0: michael@0: for (uint32_t i = 0, i_end = mPropertyTransitions.Length(); i < i_end; ++i) michael@0: { michael@0: ElementPropertyTransition &pt = mPropertyTransitions[i]; michael@0: if (pt.IsRemovedSentinel()) { michael@0: continue; michael@0: } michael@0: michael@0: MOZ_ASSERT(pt.mProperties.Length() == 1, michael@0: "Should have one animation property for a transition"); michael@0: const AnimationProperty &prop = pt.mProperties[0]; michael@0: michael@0: nsStyleAnimation::Value *val = mStyleRule->AddEmptyValue(prop.mProperty); michael@0: michael@0: double valuePortion = pt.ValuePortionFor(aRefreshTime); michael@0: michael@0: MOZ_ASSERT(prop.mSegments.Length() == 1, michael@0: "Animation property should have one segment for a transition"); michael@0: #ifdef DEBUG michael@0: bool ok = michael@0: #endif michael@0: nsStyleAnimation::Interpolate(prop.mProperty, michael@0: prop.mSegments[0].mFromValue, michael@0: prop.mSegments[0].mToValue, michael@0: valuePortion, *val); michael@0: NS_ABORT_IF_FALSE(ok, "could not interpolate values"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: bool michael@0: ElementTransitions::HasAnimationOfProperty(nsCSSProperty aProperty) const michael@0: { michael@0: for (uint32_t tranIdx = mPropertyTransitions.Length(); tranIdx-- != 0; ) { michael@0: const ElementPropertyTransition& pt = mPropertyTransitions[tranIdx]; michael@0: if (pt.HasAnimationOfProperty(aProperty) && !pt.IsRemovedSentinel()) { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: bool michael@0: ElementTransitions::CanPerformOnCompositorThread(CanAnimateFlags aFlags) const michael@0: { michael@0: nsIFrame* frame = nsLayoutUtils::GetStyleFrame(mElement); michael@0: if (!frame) { michael@0: return false; michael@0: } michael@0: michael@0: if (mElementProperty != nsGkAtoms::transitionsProperty) { michael@0: if (nsLayoutUtils::IsAnimationLoggingEnabled()) { michael@0: nsCString message; michael@0: message.AppendLiteral("Gecko bug: Async transition of pseudoelements not supported. See bug 771367"); michael@0: LogAsyncAnimationFailure(message, mElement); michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: TimeStamp now = frame->PresContext()->RefreshDriver()->MostRecentRefresh(); michael@0: michael@0: for (uint32_t i = 0, i_end = mPropertyTransitions.Length(); i < i_end; ++i) { michael@0: const ElementPropertyTransition& pt = mPropertyTransitions[i]; michael@0: MOZ_ASSERT(pt.mProperties.Length() == 1, michael@0: "Should have one animation property for a transition"); michael@0: if (css::IsGeometricProperty(pt.mProperties[0].mProperty) && michael@0: pt.IsRunningAt(now)) { michael@0: aFlags = CanAnimateFlags(aFlags | CanAnimate_HasGeometricProperty); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: bool hasOpacity = false; michael@0: bool hasTransform = false; michael@0: bool existsProperty = false; michael@0: for (uint32_t i = 0, i_end = mPropertyTransitions.Length(); i < i_end; ++i) { michael@0: const ElementPropertyTransition& pt = mPropertyTransitions[i]; michael@0: if (!pt.IsRunningAt(now)) { michael@0: continue; michael@0: } michael@0: michael@0: existsProperty = true; michael@0: michael@0: MOZ_ASSERT(pt.mProperties.Length() == 1, michael@0: "Should have one animation property for a transition"); michael@0: const AnimationProperty& prop = pt.mProperties[0]; michael@0: michael@0: if (!css::CommonElementAnimationData::CanAnimatePropertyOnCompositor( michael@0: mElement, prop.mProperty, aFlags) || michael@0: css::CommonElementAnimationData::IsCompositorAnimationDisabledForFrame(frame)) { michael@0: return false; michael@0: } michael@0: if (prop.mProperty == eCSSProperty_opacity) { michael@0: hasOpacity = true; michael@0: } else if (prop.mProperty == eCSSProperty_transform) { michael@0: hasTransform = true; michael@0: } michael@0: } michael@0: michael@0: // No properties to animate michael@0: if (!existsProperty) { michael@0: return false; michael@0: } michael@0: michael@0: // This transition can be done on the compositor. Mark the frame as active, in michael@0: // case we are able to throttle this transition. michael@0: if (hasOpacity) { michael@0: ActiveLayerTracker::NotifyAnimated(frame, eCSSProperty_opacity); michael@0: } michael@0: if (hasTransform) { michael@0: ActiveLayerTracker::NotifyAnimated(frame, eCSSProperty_transform); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: /***************************************************************************** michael@0: * nsTransitionManager * michael@0: *****************************************************************************/ michael@0: michael@0: void michael@0: nsTransitionManager::UpdateThrottledStylesForSubtree(nsIContent* aContent, michael@0: nsStyleContext* aParentStyle, michael@0: nsStyleChangeList& aChangeList) michael@0: { michael@0: dom::Element* element; michael@0: if (aContent->IsElement()) { michael@0: element = aContent->AsElement(); michael@0: } else { michael@0: element = nullptr; michael@0: } michael@0: michael@0: nsRefPtr newStyle; michael@0: michael@0: ElementTransitions* et; michael@0: if (element && michael@0: (et = GetElementTransitions(element, michael@0: nsCSSPseudoElements::ePseudo_NotPseudoElement, michael@0: false))) { michael@0: // re-resolve our style michael@0: newStyle = UpdateThrottledStyle(element, aParentStyle, aChangeList); michael@0: // remove the current transition from the working set michael@0: et->mFlushGeneration = mPresContext->RefreshDriver()->MostRecentRefresh(); michael@0: } else { michael@0: newStyle = ReparentContent(aContent, aParentStyle); michael@0: } michael@0: michael@0: // walk the children michael@0: if (newStyle) { michael@0: for (nsIContent *child = aContent->GetFirstChild(); child; michael@0: child = child->GetNextSibling()) { michael@0: UpdateThrottledStylesForSubtree(child, newStyle, aChangeList); michael@0: } michael@0: } michael@0: } michael@0: michael@0: IMPL_UPDATE_ALL_THROTTLED_STYLES_INTERNAL(nsTransitionManager, michael@0: GetElementTransitions) michael@0: michael@0: void michael@0: nsTransitionManager::UpdateAllThrottledStyles() michael@0: { michael@0: if (PR_CLIST_IS_EMPTY(&mElementData)) { michael@0: // no throttled transitions, leave early michael@0: mPresContext->TickLastUpdateThrottledTransitionStyle(); michael@0: return; michael@0: } michael@0: michael@0: if (mPresContext->ThrottledTransitionStyleIsUpToDate()) { michael@0: // throttled transitions are up to date, leave early michael@0: return; michael@0: } michael@0: michael@0: mPresContext->TickLastUpdateThrottledTransitionStyle(); michael@0: UpdateAllThrottledStylesInternal(); michael@0: } michael@0: michael@0: void michael@0: nsTransitionManager::ElementDataRemoved() michael@0: { michael@0: // If we have no transitions or animations left, remove ourselves from michael@0: // the refresh driver. michael@0: if (PR_CLIST_IS_EMPTY(&mElementData)) { michael@0: mPresContext->RefreshDriver()->RemoveRefreshObserver(this, Flush_Style); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsTransitionManager::AddElementData(CommonElementAnimationData* aData) michael@0: { michael@0: if (PR_CLIST_IS_EMPTY(&mElementData)) { michael@0: // We need to observe the refresh driver. michael@0: nsRefreshDriver *rd = mPresContext->RefreshDriver(); michael@0: rd->AddRefreshObserver(this, Flush_Style); michael@0: } michael@0: michael@0: PR_INSERT_BEFORE(aData, &mElementData); michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsTransitionManager::StyleContextChanged(dom::Element *aElement, michael@0: nsStyleContext *aOldStyleContext, michael@0: nsStyleContext *aNewStyleContext) michael@0: { michael@0: NS_PRECONDITION(aOldStyleContext->GetPseudo() == michael@0: aNewStyleContext->GetPseudo(), michael@0: "pseudo type mismatch"); michael@0: // If we were called from ReparentStyleContext, this assertion would michael@0: // actually fire. If we need to be called from there, we can probably michael@0: // just remove it; the condition probably isn't critical, although michael@0: // it's worth thinking about some more. michael@0: NS_PRECONDITION(aOldStyleContext->HasPseudoElementData() == michael@0: aNewStyleContext->HasPseudoElementData(), michael@0: "pseudo type mismatch"); michael@0: michael@0: if (!mPresContext->IsDynamic()) { michael@0: // For print or print preview, ignore transitions. michael@0: return nullptr; michael@0: } michael@0: michael@0: // NOTE: Things in this function (and ConsiderStartingTransition) michael@0: // should never call PeekStyleData because we don't preserve gotten michael@0: // structs across reframes. michael@0: michael@0: // Return sooner (before the startedAny check below) for the most michael@0: // common case: no transitions specified or running. michael@0: const nsStyleDisplay *disp = aNewStyleContext->StyleDisplay(); michael@0: nsCSSPseudoElements::Type pseudoType = aNewStyleContext->GetPseudoType(); michael@0: if (pseudoType != nsCSSPseudoElements::ePseudo_NotPseudoElement) { michael@0: if (pseudoType != nsCSSPseudoElements::ePseudo_before && michael@0: pseudoType != nsCSSPseudoElements::ePseudo_after) { michael@0: return nullptr; michael@0: } michael@0: michael@0: NS_ASSERTION((pseudoType == nsCSSPseudoElements::ePseudo_before && michael@0: aElement->Tag() == nsGkAtoms::mozgeneratedcontentbefore) || michael@0: (pseudoType == nsCSSPseudoElements::ePseudo_after && michael@0: aElement->Tag() == nsGkAtoms::mozgeneratedcontentafter), michael@0: "Unexpected aElement coming through"); michael@0: michael@0: // Else the element we want to use from now on is the element the michael@0: // :before or :after is attached to. michael@0: aElement = aElement->GetParent()->AsElement(); michael@0: } michael@0: michael@0: ElementTransitions *et = michael@0: GetElementTransitions(aElement, pseudoType, false); michael@0: if (!et && michael@0: disp->mTransitionPropertyCount == 1 && michael@0: disp->mTransitions[0].GetDelay() == 0.0f && michael@0: disp->mTransitions[0].GetDuration() == 0.0f) { michael@0: return nullptr; michael@0: } michael@0: michael@0: michael@0: if (aNewStyleContext->PresContext()->IsProcessingAnimationStyleChange()) { michael@0: return nullptr; michael@0: } michael@0: michael@0: if (aNewStyleContext->GetParent() && michael@0: aNewStyleContext->GetParent()->HasPseudoElementData()) { michael@0: // Ignore transitions on things that inherit properties from michael@0: // pseudo-elements. michael@0: // FIXME (Bug 522599): Add tests for this. michael@0: return nullptr; michael@0: } michael@0: michael@0: NS_WARN_IF_FALSE(!nsLayoutUtils::AreAsyncAnimationsEnabled() || michael@0: mPresContext->ThrottledTransitionStyleIsUpToDate(), michael@0: "throttled animations not up to date"); michael@0: michael@0: // Per http://lists.w3.org/Archives/Public/www-style/2009Aug/0109.html michael@0: // I'll consider only the transitions from the number of items in michael@0: // 'transition-property' on down, and later ones will override earlier michael@0: // ones (tracked using |whichStarted|). michael@0: bool startedAny = false; michael@0: nsCSSPropertySet whichStarted; michael@0: for (uint32_t i = disp->mTransitionPropertyCount; i-- != 0; ) { michael@0: const nsTransition& t = disp->mTransitions[i]; michael@0: // Check delay and duration first, since they default to zero, and michael@0: // when they're both zero, we can ignore the transition. michael@0: if (t.GetDelay() != 0.0f || t.GetDuration() != 0.0f) { michael@0: // We might have something to transition. See if any of the michael@0: // properties in question changed and are animatable. michael@0: // FIXME: Would be good to find a way to share code between this michael@0: // interpretation of transition-property and the one below. michael@0: nsCSSProperty property = t.GetProperty(); michael@0: if (property == eCSSPropertyExtra_no_properties || michael@0: property == eCSSPropertyExtra_variable || michael@0: property == eCSSProperty_UNKNOWN) { michael@0: // Nothing to do, but need to exclude this from cases below. michael@0: } else if (property == eCSSPropertyExtra_all_properties) { michael@0: for (nsCSSProperty p = nsCSSProperty(0); michael@0: p < eCSSProperty_COUNT_no_shorthands; michael@0: p = nsCSSProperty(p + 1)) { michael@0: ConsiderStartingTransition(p, t, aElement, et, michael@0: aOldStyleContext, aNewStyleContext, michael@0: &startedAny, &whichStarted); michael@0: } michael@0: } else if (nsCSSProps::IsShorthand(property)) { michael@0: CSSPROPS_FOR_SHORTHAND_SUBPROPERTIES(subprop, property) { michael@0: ConsiderStartingTransition(*subprop, t, aElement, et, michael@0: aOldStyleContext, aNewStyleContext, michael@0: &startedAny, &whichStarted); michael@0: } michael@0: } else { michael@0: ConsiderStartingTransition(property, t, aElement, et, michael@0: aOldStyleContext, aNewStyleContext, michael@0: &startedAny, &whichStarted); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Stop any transitions for properties that are no longer in michael@0: // 'transition-property'. michael@0: // Also stop any transitions for properties that just changed (and are michael@0: // still in the set of properties to transition), but we didn't just michael@0: // start the transition because delay and duration are both zero. michael@0: if (et) { michael@0: bool checkProperties = michael@0: disp->mTransitions[0].GetProperty() != eCSSPropertyExtra_all_properties; michael@0: nsCSSPropertySet allTransitionProperties; michael@0: if (checkProperties) { michael@0: for (uint32_t i = disp->mTransitionPropertyCount; i-- != 0; ) { michael@0: const nsTransition& t = disp->mTransitions[i]; michael@0: // FIXME: Would be good to find a way to share code between this michael@0: // interpretation of transition-property and the one above. michael@0: nsCSSProperty property = t.GetProperty(); michael@0: if (property == eCSSPropertyExtra_no_properties || michael@0: property == eCSSPropertyExtra_variable || michael@0: property == eCSSProperty_UNKNOWN) { michael@0: // Nothing to do, but need to exclude this from cases below. michael@0: } else if (property == eCSSPropertyExtra_all_properties) { michael@0: for (nsCSSProperty p = nsCSSProperty(0); michael@0: p < eCSSProperty_COUNT_no_shorthands; michael@0: p = nsCSSProperty(p + 1)) { michael@0: allTransitionProperties.AddProperty(p); michael@0: } michael@0: } else if (nsCSSProps::IsShorthand(property)) { michael@0: CSSPROPS_FOR_SHORTHAND_SUBPROPERTIES(subprop, property) { michael@0: allTransitionProperties.AddProperty(*subprop); michael@0: } michael@0: } else { michael@0: allTransitionProperties.AddProperty(property); michael@0: } michael@0: } michael@0: } michael@0: michael@0: nsTArray &pts = et->mPropertyTransitions; michael@0: uint32_t i = pts.Length(); michael@0: NS_ABORT_IF_FALSE(i != 0, "empty transitions list?"); michael@0: nsStyleAnimation::Value currentValue; michael@0: do { michael@0: --i; michael@0: ElementPropertyTransition &pt = pts[i]; michael@0: MOZ_ASSERT(pt.mProperties.Length() == 1, michael@0: "Should have one animation property for a transition"); michael@0: MOZ_ASSERT(pt.mProperties[0].mSegments.Length() == 1, michael@0: "Animation property should have one segment for a transition"); michael@0: const AnimationProperty& prop = pt.mProperties[0]; michael@0: const AnimationPropertySegment& segment = prop.mSegments[0]; michael@0: // properties no longer in 'transition-property' michael@0: if ((checkProperties && michael@0: !allTransitionProperties.HasProperty(prop.mProperty)) || michael@0: // properties whose computed values changed but delay and michael@0: // duration are both zero michael@0: !ExtractComputedValueForTransition(prop.mProperty, aNewStyleContext, michael@0: currentValue) || michael@0: currentValue != segment.mToValue) { michael@0: // stop the transition michael@0: pts.RemoveElementAt(i); michael@0: et->UpdateAnimationGeneration(mPresContext); michael@0: } michael@0: } while (i != 0); michael@0: michael@0: if (pts.IsEmpty()) { michael@0: et->Destroy(); michael@0: et = nullptr; michael@0: } michael@0: } michael@0: michael@0: if (!startedAny) { michael@0: return nullptr; michael@0: } michael@0: michael@0: NS_ABORT_IF_FALSE(et, "must have element transitions if we started " michael@0: "any transitions"); michael@0: michael@0: // In the CSS working group discussion (2009 Jul 15 telecon, michael@0: // http://www.w3.org/mid/4A5E1470.4030904@inkedblade.net ) of michael@0: // http://lists.w3.org/Archives/Public/www-style/2009Jun/0121.html , michael@0: // the working group decided that a transition property on an michael@0: // element should not cause any transitions if the property change michael@0: // is itself inheriting a value that is transitioning on an michael@0: // ancestor. So, to get the correct behavior, we continue the michael@0: // restyle that caused this transition using a "covering" rule that michael@0: // covers up any changes on which we started transitions, so that michael@0: // descendants don't start their own transitions. (In the case of michael@0: // negative transition delay, this covering rule produces different michael@0: // results than applying the transition rule immediately would). michael@0: // Our caller is responsible for restyling again using this covering michael@0: // rule. michael@0: michael@0: nsRefPtr coverRule = new css::AnimValuesStyleRule; michael@0: michael@0: nsTArray &pts = et->mPropertyTransitions; michael@0: for (uint32_t i = 0, i_end = pts.Length(); i < i_end; ++i) { michael@0: ElementPropertyTransition &pt = pts[i]; michael@0: MOZ_ASSERT(pt.mProperties.Length() == 1, michael@0: "Should have one animation property for a transition"); michael@0: MOZ_ASSERT(pt.mProperties[0].mSegments.Length() == 1, michael@0: "Animation property should have one segment for a transition"); michael@0: AnimationProperty& prop = pt.mProperties[0]; michael@0: AnimationPropertySegment& segment = prop.mSegments[0]; michael@0: if (whichStarted.HasProperty(prop.mProperty)) { michael@0: coverRule->AddValue(prop.mProperty, segment.mFromValue); michael@0: } michael@0: } michael@0: michael@0: et->mStyleRule = nullptr; michael@0: michael@0: return coverRule.forget(); michael@0: } michael@0: michael@0: void michael@0: nsTransitionManager::ConsiderStartingTransition(nsCSSProperty aProperty, michael@0: const nsTransition& aTransition, michael@0: dom::Element* aElement, michael@0: ElementTransitions*& aElementTransitions, michael@0: nsStyleContext* aOldStyleContext, michael@0: nsStyleContext* aNewStyleContext, michael@0: bool* aStartedAny, michael@0: nsCSSPropertySet* aWhichStarted) michael@0: { michael@0: // IsShorthand itself will assert if aProperty is not a property. michael@0: NS_ABORT_IF_FALSE(!nsCSSProps::IsShorthand(aProperty), michael@0: "property out of range"); michael@0: NS_ASSERTION(!aElementTransitions || michael@0: aElementTransitions->mElement == aElement, "Element mismatch"); michael@0: michael@0: if (aWhichStarted->HasProperty(aProperty)) { michael@0: // A later item in transition-property already started a michael@0: // transition for this property, so we ignore this one. michael@0: // See comment above and michael@0: // http://lists.w3.org/Archives/Public/www-style/2009Aug/0109.html . michael@0: return; michael@0: } michael@0: michael@0: if (nsCSSProps::kAnimTypeTable[aProperty] == eStyleAnimType_None) { michael@0: return; michael@0: } michael@0: michael@0: ElementPropertyTransition pt; michael@0: michael@0: nsStyleAnimation::Value startValue, endValue, dummyValue; michael@0: bool haveValues = michael@0: ExtractComputedValueForTransition(aProperty, aOldStyleContext, michael@0: startValue) && michael@0: ExtractComputedValueForTransition(aProperty, aNewStyleContext, michael@0: endValue); michael@0: michael@0: bool haveChange = startValue != endValue; michael@0: michael@0: bool shouldAnimate = michael@0: haveValues && michael@0: haveChange && michael@0: // Check that we can interpolate between these values michael@0: // (If this is ever a performance problem, we could add a michael@0: // CanInterpolate method, but it seems fine for now.) michael@0: nsStyleAnimation::Interpolate(aProperty, startValue, endValue, michael@0: 0.5, dummyValue); michael@0: michael@0: bool haveCurrentTransition = false; michael@0: uint32_t currentIndex = nsTArray::NoIndex; michael@0: const ElementPropertyTransition *oldPT = nullptr; michael@0: if (aElementTransitions) { michael@0: nsTArray &pts = michael@0: aElementTransitions->mPropertyTransitions; michael@0: for (uint32_t i = 0, i_end = pts.Length(); i < i_end; ++i) { michael@0: MOZ_ASSERT(pts[i].mProperties.Length() == 1, michael@0: "Should have one animation property for a transition"); michael@0: if (pts[i].mProperties[0].mProperty == aProperty) { michael@0: haveCurrentTransition = true; michael@0: currentIndex = i; michael@0: oldPT = &aElementTransitions->mPropertyTransitions[currentIndex]; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // If we got a style change that changed the value to the endpoint michael@0: // of the currently running transition, we don't want to interrupt michael@0: // its timing function. michael@0: // This needs to be before the !shouldAnimate && haveCurrentTransition michael@0: // case below because we might be close enough to the end of the michael@0: // transition that the current value rounds to the final value. In michael@0: // this case, we'll end up with shouldAnimate as false (because michael@0: // there's no value change), but we need to return early here rather michael@0: // than cancel the running transition because shouldAnimate is false! michael@0: MOZ_ASSERT(!oldPT || oldPT->mProperties[0].mSegments.Length() == 1, michael@0: "Should have one animation property segment for a transition"); michael@0: if (haveCurrentTransition && haveValues && michael@0: oldPT->mProperties[0].mSegments[0].mToValue == endValue) { michael@0: // WalkTransitionRule already called RestyleForAnimation. michael@0: return; michael@0: } michael@0: michael@0: nsPresContext *presContext = aNewStyleContext->PresContext(); michael@0: michael@0: if (!shouldAnimate) { michael@0: if (haveCurrentTransition) { michael@0: // We're in the middle of a transition, and just got a non-transition michael@0: // style change to something that we can't animate. This might happen michael@0: // because we got a non-transition style change changing to the current michael@0: // in-progress value (which is particularly easy to cause when we're michael@0: // currently in the 'transition-delay'). It also might happen because we michael@0: // just got a style change to a value that can't be interpolated. michael@0: nsTArray &pts = michael@0: aElementTransitions->mPropertyTransitions; michael@0: pts.RemoveElementAt(currentIndex); michael@0: aElementTransitions->UpdateAnimationGeneration(mPresContext); michael@0: michael@0: if (pts.IsEmpty()) { michael@0: aElementTransitions->Destroy(); michael@0: // |aElementTransitions| is now a dangling pointer! michael@0: aElementTransitions = nullptr; michael@0: } michael@0: // WalkTransitionRule already called RestyleForAnimation. michael@0: } michael@0: return; michael@0: } michael@0: michael@0: TimeStamp mostRecentRefresh = michael@0: presContext->RefreshDriver()->MostRecentRefresh(); michael@0: michael@0: const nsTimingFunction &tf = aTransition.GetTimingFunction(); michael@0: float delay = aTransition.GetDelay(); michael@0: float duration = aTransition.GetDuration(); michael@0: if (duration < 0.0) { michael@0: // The spec says a negative duration is treated as zero. michael@0: duration = 0.0; michael@0: } michael@0: pt.mStartForReversingTest = startValue; michael@0: pt.mReversePortion = 1.0; michael@0: michael@0: // If the new transition reverses an existing one, we'll need to michael@0: // handle the timing differently. michael@0: if (haveCurrentTransition && michael@0: !oldPT->IsRemovedSentinel() && michael@0: oldPT->mStartForReversingTest == endValue) { michael@0: // Compute the appropriate negative transition-delay such that right michael@0: // now we'd end up at the current position. michael@0: double valuePortion = michael@0: oldPT->ValuePortionFor(mostRecentRefresh) * oldPT->mReversePortion + michael@0: (1.0 - oldPT->mReversePortion); michael@0: // A timing function with negative y1 (or y2!) might make michael@0: // valuePortion negative. In this case, we still want to apply our michael@0: // reversing logic based on relative distances, not make duration michael@0: // negative. michael@0: if (valuePortion < 0.0) { michael@0: valuePortion = -valuePortion; michael@0: } michael@0: // A timing function with y2 (or y1!) greater than one might michael@0: // advance past its terminal value. It's probably a good idea to michael@0: // clamp valuePortion to be at most one to preserve the invariant michael@0: // that a transition will complete within at most its specified michael@0: // time. michael@0: if (valuePortion > 1.0) { michael@0: valuePortion = 1.0; michael@0: } michael@0: michael@0: // Negative delays are essentially part of the transition michael@0: // function, so reduce them along with the duration, but don't michael@0: // reduce positive delays. michael@0: if (delay < 0.0f) { michael@0: delay *= valuePortion; michael@0: } michael@0: michael@0: duration *= valuePortion; michael@0: michael@0: pt.mStartForReversingTest = oldPT->mProperties[0].mSegments[0].mToValue; michael@0: pt.mReversePortion = valuePortion; michael@0: } michael@0: michael@0: AnimationProperty& prop = *pt.mProperties.AppendElement(); michael@0: prop.mProperty = aProperty; michael@0: michael@0: AnimationPropertySegment& segment = *prop.mSegments.AppendElement(); michael@0: segment.mFromValue = startValue; michael@0: segment.mToValue = endValue; michael@0: segment.mFromKey = 0; michael@0: segment.mToKey = 1; michael@0: segment.mTimingFunction.Init(tf); michael@0: michael@0: pt.mStartTime = mostRecentRefresh; michael@0: pt.mDelay = TimeDuration::FromMilliseconds(delay); michael@0: pt.mIterationDuration = TimeDuration::FromMilliseconds(duration); michael@0: pt.mIterationCount = 1; michael@0: pt.mDirection = NS_STYLE_ANIMATION_DIRECTION_NORMAL; michael@0: pt.mFillMode = NS_STYLE_ANIMATION_FILL_MODE_BACKWARDS; michael@0: pt.mPlayState = NS_STYLE_ANIMATION_PLAY_STATE_RUNNING; michael@0: pt.mPauseStart = TimeStamp(); michael@0: michael@0: if (!aElementTransitions) { michael@0: aElementTransitions = michael@0: GetElementTransitions(aElement, aNewStyleContext->GetPseudoType(), michael@0: true); michael@0: if (!aElementTransitions) { michael@0: NS_WARNING("allocating ElementTransitions failed"); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: nsTArray &pts = michael@0: aElementTransitions->mPropertyTransitions; michael@0: #ifdef DEBUG michael@0: for (uint32_t i = 0, i_end = pts.Length(); i < i_end; ++i) { michael@0: NS_ABORT_IF_FALSE(pts[i].mProperties.Length() == 1, michael@0: "Should have one animation property for a transition"); michael@0: NS_ABORT_IF_FALSE(i == currentIndex || michael@0: pts[i].mProperties[0].mProperty != aProperty, michael@0: "duplicate transitions for property"); michael@0: } michael@0: #endif michael@0: if (haveCurrentTransition) { michael@0: pts[currentIndex] = pt; michael@0: } else { michael@0: if (!pts.AppendElement(pt)) { michael@0: NS_WARNING("out of memory"); michael@0: return; michael@0: } michael@0: } michael@0: aElementTransitions->UpdateAnimationGeneration(mPresContext); michael@0: michael@0: nsRestyleHint hint = michael@0: aNewStyleContext->GetPseudoType() == michael@0: nsCSSPseudoElements::ePseudo_NotPseudoElement ? michael@0: eRestyle_Self : eRestyle_Subtree; michael@0: presContext->PresShell()->RestyleForAnimation(aElement, hint); michael@0: michael@0: *aStartedAny = true; michael@0: aWhichStarted->AddProperty(aProperty); michael@0: } michael@0: michael@0: ElementTransitions* michael@0: nsTransitionManager::GetElementTransitions(dom::Element *aElement, michael@0: nsCSSPseudoElements::Type aPseudoType, michael@0: bool aCreateIfNeeded) michael@0: { michael@0: if (!aCreateIfNeeded && PR_CLIST_IS_EMPTY(&mElementData)) { michael@0: // Early return for the most common case. michael@0: return nullptr; michael@0: } michael@0: michael@0: nsIAtom *propName; michael@0: if (aPseudoType == nsCSSPseudoElements::ePseudo_NotPseudoElement) { michael@0: propName = nsGkAtoms::transitionsProperty; michael@0: } else if (aPseudoType == nsCSSPseudoElements::ePseudo_before) { michael@0: propName = nsGkAtoms::transitionsOfBeforeProperty; michael@0: } else if (aPseudoType == nsCSSPseudoElements::ePseudo_after) { michael@0: propName = nsGkAtoms::transitionsOfAfterProperty; michael@0: } else { michael@0: NS_ASSERTION(!aCreateIfNeeded, michael@0: "should never try to create transitions for pseudo " michael@0: "other than :before or :after"); michael@0: return nullptr; michael@0: } michael@0: ElementTransitions *et = static_cast( michael@0: aElement->GetProperty(propName)); michael@0: if (!et && aCreateIfNeeded) { michael@0: // FIXME: Consider arena-allocating? michael@0: et = new ElementTransitions(aElement, propName, this, michael@0: mPresContext->RefreshDriver()->MostRecentRefresh()); michael@0: nsresult rv = aElement->SetProperty(propName, et, michael@0: ElementTransitionsPropertyDtor, false); michael@0: if (NS_FAILED(rv)) { michael@0: NS_WARNING("SetProperty failed"); michael@0: delete et; michael@0: return nullptr; michael@0: } michael@0: if (propName == nsGkAtoms::transitionsProperty) { michael@0: aElement->SetMayHaveAnimations(); michael@0: } michael@0: michael@0: AddElementData(et); michael@0: } michael@0: michael@0: return et; michael@0: } michael@0: michael@0: /* michael@0: * nsIStyleRuleProcessor implementation michael@0: */ michael@0: michael@0: void michael@0: nsTransitionManager::WalkTransitionRule(ElementDependentRuleProcessorData* aData, michael@0: nsCSSPseudoElements::Type aPseudoType) michael@0: { michael@0: ElementTransitions *et = michael@0: GetElementTransitions(aData->mElement, aPseudoType, false); michael@0: if (!et) { michael@0: return; michael@0: } michael@0: michael@0: if (!mPresContext->IsDynamic()) { michael@0: // For print or print preview, ignore animations. michael@0: return; michael@0: } michael@0: michael@0: if (aData->mPresContext->IsProcessingRestyles() && michael@0: !aData->mPresContext->IsProcessingAnimationStyleChange()) { michael@0: // If we're processing a normal style change rather than one from michael@0: // animation, don't add the transition rule. This allows us to michael@0: // compute the new style value rather than having the transition michael@0: // override it, so that we can start transitioning differently. michael@0: michael@0: // We need to immediately restyle with animation michael@0: // after doing this. michael@0: nsRestyleHint hint = michael@0: aPseudoType == nsCSSPseudoElements::ePseudo_NotPseudoElement ? michael@0: eRestyle_Self : eRestyle_Subtree; michael@0: mPresContext->PresShell()->RestyleForAnimation(aData->mElement, hint); michael@0: return; michael@0: } michael@0: michael@0: et->EnsureStyleRuleFor( michael@0: aData->mPresContext->RefreshDriver()->MostRecentRefresh()); michael@0: michael@0: aData->mRuleWalker->Forward(et->mStyleRule); michael@0: } michael@0: michael@0: /* virtual */ void michael@0: nsTransitionManager::RulesMatching(ElementRuleProcessorData* aData) michael@0: { michael@0: NS_ABORT_IF_FALSE(aData->mPresContext == mPresContext, michael@0: "pres context mismatch"); michael@0: WalkTransitionRule(aData, michael@0: nsCSSPseudoElements::ePseudo_NotPseudoElement); michael@0: } michael@0: michael@0: /* virtual */ void michael@0: nsTransitionManager::RulesMatching(PseudoElementRuleProcessorData* aData) michael@0: { michael@0: NS_ABORT_IF_FALSE(aData->mPresContext == mPresContext, michael@0: "pres context mismatch"); michael@0: michael@0: // Note: If we're the only thing keeping a pseudo-element frame alive michael@0: // (per ProbePseudoStyleContext), we still want to keep it alive, so michael@0: // this is ok. michael@0: WalkTransitionRule(aData, aData->mPseudoType); michael@0: } michael@0: michael@0: /* virtual */ void michael@0: nsTransitionManager::RulesMatching(AnonBoxRuleProcessorData* aData) michael@0: { michael@0: } michael@0: michael@0: #ifdef MOZ_XUL michael@0: /* virtual */ void michael@0: nsTransitionManager::RulesMatching(XULTreeRuleProcessorData* aData) michael@0: { michael@0: } michael@0: #endif michael@0: michael@0: /* virtual */ size_t michael@0: nsTransitionManager::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const michael@0: { michael@0: return CommonAnimationManager::SizeOfExcludingThis(aMallocSizeOf); michael@0: } michael@0: michael@0: /* virtual */ size_t michael@0: nsTransitionManager::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const michael@0: { michael@0: return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); michael@0: } michael@0: michael@0: struct TransitionEventInfo { michael@0: nsCOMPtr mElement; michael@0: InternalTransitionEvent mEvent; michael@0: michael@0: TransitionEventInfo(nsIContent *aElement, nsCSSProperty aProperty, michael@0: TimeDuration aDuration, const nsAString& aPseudoElement) michael@0: : mElement(aElement) michael@0: , mEvent(true, NS_TRANSITION_END) michael@0: { michael@0: // XXX Looks like nobody initialize WidgetEvent::time michael@0: mEvent.propertyName = michael@0: NS_ConvertUTF8toUTF16(nsCSSProps::GetStringValue(aProperty)); michael@0: mEvent.elapsedTime = aDuration.ToSeconds(); michael@0: mEvent.pseudoElement = aPseudoElement; michael@0: } michael@0: michael@0: // InternalTransitionEvent doesn't support copy-construction, so we need michael@0: // to ourselves in order to work with nsTArray michael@0: TransitionEventInfo(const TransitionEventInfo &aOther) michael@0: : mElement(aOther.mElement) michael@0: , mEvent(true, NS_TRANSITION_END) michael@0: { michael@0: mEvent.AssignTransitionEventData(aOther.mEvent, false); michael@0: } michael@0: }; michael@0: michael@0: /* virtual */ void michael@0: nsTransitionManager::WillRefresh(mozilla::TimeStamp aTime) michael@0: { michael@0: NS_ABORT_IF_FALSE(mPresContext, michael@0: "refresh driver should not notify additional observers " michael@0: "after pres context has been destroyed"); michael@0: if (!mPresContext->GetPresShell()) { michael@0: // Someone might be keeping mPresContext alive past the point michael@0: // where it has been torn down; don't bother doing anything in michael@0: // this case. But do get rid of all our transitions so we stop michael@0: // triggering refreshes. michael@0: RemoveAllElementData(); michael@0: return; michael@0: } michael@0: michael@0: FlushTransitions(Can_Throttle); michael@0: } michael@0: michael@0: void michael@0: nsTransitionManager::FlushTransitions(FlushFlags aFlags) michael@0: { michael@0: if (PR_CLIST_IS_EMPTY(&mElementData)) { michael@0: // no transitions, leave early michael@0: return; michael@0: } michael@0: michael@0: nsTArray events; michael@0: TimeStamp now = mPresContext->RefreshDriver()->MostRecentRefresh(); michael@0: bool didThrottle = false; michael@0: // Trim transitions that have completed, post restyle events for frames that michael@0: // are still transitioning, and start transitions with delays. michael@0: { michael@0: PRCList *next = PR_LIST_HEAD(&mElementData); michael@0: while (next != &mElementData) { michael@0: ElementTransitions *et = static_cast(next); michael@0: next = PR_NEXT_LINK(next); michael@0: michael@0: bool canThrottleTick = aFlags == Can_Throttle && michael@0: et->CanPerformOnCompositorThread( michael@0: CommonElementAnimationData::CanAnimateFlags(0)) && michael@0: et->CanThrottleAnimation(now); michael@0: michael@0: NS_ABORT_IF_FALSE(et->mElement->GetCurrentDoc() == michael@0: mPresContext->Document(), michael@0: "Element::UnbindFromTree should have " michael@0: "destroyed the element transitions object"); michael@0: michael@0: uint32_t i = et->mPropertyTransitions.Length(); michael@0: NS_ABORT_IF_FALSE(i != 0, "empty transitions list?"); michael@0: bool transitionStartedOrEnded = false; michael@0: do { michael@0: --i; michael@0: ElementPropertyTransition &pt = et->mPropertyTransitions[i]; michael@0: if (pt.IsRemovedSentinel()) { michael@0: // Actually remove transitions one throttle-able cycle after their michael@0: // completion. We only clear on a throttle-able cycle because that michael@0: // means it is a regular restyle tick and thus it is safe to discard michael@0: // the transition. If the flush is not throttle-able, we might still michael@0: // have new transitions left to process. See comment below. michael@0: if (aFlags == Can_Throttle) { michael@0: et->mPropertyTransitions.RemoveElementAt(i); michael@0: } michael@0: } else if (pt.mStartTime + pt.mDelay + pt.mIterationDuration <= now) { michael@0: MOZ_ASSERT(pt.mProperties.Length() == 1, michael@0: "Should have one animation property for a transition"); michael@0: nsCSSProperty prop = pt.mProperties[0].mProperty; michael@0: if (nsCSSProps::PropHasFlags(prop, CSS_PROPERTY_REPORT_OTHER_NAME)) michael@0: { michael@0: prop = nsCSSProps::OtherNameFor(prop); michael@0: } michael@0: nsIAtom* ep = et->mElementProperty; michael@0: NS_NAMED_LITERAL_STRING(before, "::before"); michael@0: NS_NAMED_LITERAL_STRING(after, "::after"); michael@0: events.AppendElement( michael@0: TransitionEventInfo(et->mElement, prop, pt.mIterationDuration, michael@0: ep == nsGkAtoms::transitionsProperty ? michael@0: EmptyString() : michael@0: ep == nsGkAtoms::transitionsOfBeforeProperty ? michael@0: before : michael@0: after)); michael@0: michael@0: // Leave this transition in the list for one more refresh michael@0: // cycle, since we haven't yet processed its style change, and michael@0: // if we also have (already, or will have from processing michael@0: // transitionend events or other refresh driver notifications) michael@0: // a non-animation style change that would affect it, we need michael@0: // to know not to start a new transition for the transition michael@0: // from the almost-completed value to the final value. michael@0: pt.SetRemovedSentinel(); michael@0: et->UpdateAnimationGeneration(mPresContext); michael@0: transitionStartedOrEnded = true; michael@0: } else if (pt.mStartTime + pt.mDelay <= now && canThrottleTick && michael@0: !pt.mIsRunningOnCompositor) { michael@0: // Start a transition with a delay where we should start the michael@0: // transition proper. michael@0: et->UpdateAnimationGeneration(mPresContext); michael@0: transitionStartedOrEnded = true; michael@0: } michael@0: } while (i != 0); michael@0: michael@0: // We need to restyle even if the transition rule no longer michael@0: // applies (in which case we just made it not apply). michael@0: NS_ASSERTION(et->mElementProperty == nsGkAtoms::transitionsProperty || michael@0: et->mElementProperty == nsGkAtoms::transitionsOfBeforeProperty || michael@0: et->mElementProperty == nsGkAtoms::transitionsOfAfterProperty, michael@0: "Unexpected element property; might restyle too much"); michael@0: if (!canThrottleTick || transitionStartedOrEnded) { michael@0: nsRestyleHint hint = et->mElementProperty == nsGkAtoms::transitionsProperty ? michael@0: eRestyle_Self : eRestyle_Subtree; michael@0: mPresContext->PresShell()->RestyleForAnimation(et->mElement, hint); michael@0: } else { michael@0: didThrottle = true; michael@0: } michael@0: michael@0: if (et->mPropertyTransitions.IsEmpty()) { michael@0: et->Destroy(); michael@0: // |et| is now a dangling pointer! michael@0: et = nullptr; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (didThrottle) { michael@0: mPresContext->Document()->SetNeedStyleFlush(); michael@0: } michael@0: michael@0: for (uint32_t i = 0, i_end = events.Length(); i < i_end; ++i) { michael@0: TransitionEventInfo &info = events[i]; michael@0: EventDispatcher::Dispatch(info.mElement, mPresContext, &info.mEvent); michael@0: michael@0: if (!mPresContext) { michael@0: break; michael@0: } michael@0: } michael@0: }