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: #include "ActiveLayerTracker.h" michael@0: michael@0: #include "nsExpirationTracker.h" michael@0: #include "nsIFrame.h" michael@0: #include "nsIContent.h" michael@0: #include "nsRefreshDriver.h" michael@0: #include "nsPIDOMWindow.h" michael@0: #include "nsIDocument.h" michael@0: #include "nsAnimationManager.h" michael@0: #include "nsTransitionManager.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: /** michael@0: * This tracks the state of a frame that may need active layers due to michael@0: * ongoing content changes or style changes that indicate animation. michael@0: * michael@0: * When no changes of *any* kind are detected after 75-100ms we remove this michael@0: * object. Because we only track all kinds of activity with a single michael@0: * nsExpirationTracker, it's possible a frame might remain active somewhat michael@0: * spuriously if different kinds of changes kept happening, but that almost michael@0: * certainly doesn't matter. michael@0: */ michael@0: class LayerActivity { michael@0: public: michael@0: LayerActivity(nsIFrame* aFrame) michael@0: : mFrame(aFrame) michael@0: , mOpacityRestyleCount(0) michael@0: , mTransformRestyleCount(0) michael@0: , mLeftRestyleCount(0) michael@0: , mTopRestyleCount(0) michael@0: , mRightRestyleCount(0) michael@0: , mBottomRestyleCount(0) michael@0: , mMarginLeftRestyleCount(0) michael@0: , mMarginTopRestyleCount(0) michael@0: , mMarginRightRestyleCount(0) michael@0: , mMarginBottomRestyleCount(0) michael@0: , mContentActive(false) michael@0: {} michael@0: ~LayerActivity(); michael@0: nsExpirationState* GetExpirationState() { return &mState; } michael@0: uint8_t& RestyleCountForProperty(nsCSSProperty aProperty) michael@0: { michael@0: switch (aProperty) { michael@0: case eCSSProperty_opacity: return mOpacityRestyleCount; michael@0: case eCSSProperty_transform: return mTransformRestyleCount; michael@0: case eCSSProperty_left: return mLeftRestyleCount; michael@0: case eCSSProperty_top: return mTopRestyleCount; michael@0: case eCSSProperty_right: return mRightRestyleCount; michael@0: case eCSSProperty_bottom: return mBottomRestyleCount; michael@0: case eCSSProperty_margin_left: return mMarginLeftRestyleCount; michael@0: case eCSSProperty_margin_top: return mMarginTopRestyleCount; michael@0: case eCSSProperty_margin_right: return mMarginRightRestyleCount; michael@0: case eCSSProperty_margin_bottom: return mMarginBottomRestyleCount; michael@0: default: MOZ_ASSERT(false); return mOpacityRestyleCount; michael@0: } michael@0: } michael@0: michael@0: nsIFrame* mFrame; michael@0: nsExpirationState mState; michael@0: // Number of restyle operations detected michael@0: uint8_t mOpacityRestyleCount; michael@0: uint8_t mTransformRestyleCount; michael@0: uint8_t mLeftRestyleCount; michael@0: uint8_t mTopRestyleCount; michael@0: uint8_t mRightRestyleCount; michael@0: uint8_t mBottomRestyleCount; michael@0: uint8_t mMarginLeftRestyleCount; michael@0: uint8_t mMarginTopRestyleCount; michael@0: uint8_t mMarginRightRestyleCount; michael@0: uint8_t mMarginBottomRestyleCount; michael@0: bool mContentActive; michael@0: }; michael@0: michael@0: class LayerActivityTracker MOZ_FINAL : public nsExpirationTracker { michael@0: public: michael@0: // 75-100ms is a good timeout period. We use 4 generations of 25ms each. michael@0: enum { GENERATION_MS = 100 }; michael@0: LayerActivityTracker() michael@0: : nsExpirationTracker(GENERATION_MS) {} michael@0: ~LayerActivityTracker() { michael@0: AgeAllGenerations(); michael@0: } michael@0: michael@0: virtual void NotifyExpired(LayerActivity* aObject); michael@0: }; michael@0: michael@0: static LayerActivityTracker* gLayerActivityTracker = nullptr; michael@0: michael@0: LayerActivity::~LayerActivity() michael@0: { michael@0: if (mFrame) { michael@0: NS_ASSERTION(gLayerActivityTracker, "Should still have a tracker"); michael@0: gLayerActivityTracker->RemoveObject(this); michael@0: } michael@0: } michael@0: michael@0: static void DestroyLayerActivity(void* aPropertyValue) michael@0: { michael@0: delete static_cast(aPropertyValue); michael@0: } michael@0: michael@0: // Frames with this property have NS_FRAME_HAS_LAYER_ACTIVITY_PROPERTY set michael@0: NS_DECLARE_FRAME_PROPERTY(LayerActivityProperty, DestroyLayerActivity) michael@0: michael@0: void michael@0: LayerActivityTracker::NotifyExpired(LayerActivity* aObject) michael@0: { michael@0: RemoveObject(aObject); michael@0: michael@0: nsIFrame* f = aObject->mFrame; michael@0: aObject->mFrame = nullptr; michael@0: michael@0: // The pres context might have been detached during the delay - michael@0: // that's fine, just skip the paint. michael@0: if (f->PresContext()->GetContainerWeak()) { michael@0: f->SchedulePaint(); michael@0: } michael@0: f->RemoveStateBits(NS_FRAME_HAS_LAYER_ACTIVITY_PROPERTY); michael@0: f->Properties().Delete(LayerActivityProperty()); michael@0: } michael@0: michael@0: static LayerActivity* michael@0: GetLayerActivity(nsIFrame* aFrame) michael@0: { michael@0: if (!aFrame->HasAnyStateBits(NS_FRAME_HAS_LAYER_ACTIVITY_PROPERTY)) { michael@0: return nullptr; michael@0: } michael@0: FrameProperties properties = aFrame->Properties(); michael@0: return static_cast(properties.Get(LayerActivityProperty())); michael@0: } michael@0: michael@0: static LayerActivity* michael@0: GetLayerActivityForUpdate(nsIFrame* aFrame) michael@0: { michael@0: FrameProperties properties = aFrame->Properties(); michael@0: LayerActivity* layerActivity = michael@0: static_cast(properties.Get(LayerActivityProperty())); michael@0: if (layerActivity) { michael@0: gLayerActivityTracker->MarkUsed(layerActivity); michael@0: } else { michael@0: if (!gLayerActivityTracker) { michael@0: gLayerActivityTracker = new LayerActivityTracker(); michael@0: } michael@0: layerActivity = new LayerActivity(aFrame); michael@0: gLayerActivityTracker->AddObject(layerActivity); michael@0: aFrame->AddStateBits(NS_FRAME_HAS_LAYER_ACTIVITY_PROPERTY); michael@0: properties.Set(LayerActivityProperty(), layerActivity); michael@0: } michael@0: return layerActivity; michael@0: } michael@0: michael@0: static void michael@0: IncrementMutationCount(uint8_t* aCount) michael@0: { michael@0: *aCount = uint8_t(std::min(0xFF, *aCount + 1)); michael@0: } michael@0: michael@0: /* static */ void michael@0: ActiveLayerTracker::NotifyRestyle(nsIFrame* aFrame, nsCSSProperty aProperty) michael@0: { michael@0: LayerActivity* layerActivity = GetLayerActivityForUpdate(aFrame); michael@0: uint8_t& mutationCount = layerActivity->RestyleCountForProperty(aProperty); michael@0: IncrementMutationCount(&mutationCount); michael@0: } michael@0: michael@0: /* static */ void michael@0: ActiveLayerTracker::NotifyOffsetRestyle(nsIFrame* aFrame) michael@0: { michael@0: LayerActivity* layerActivity = GetLayerActivityForUpdate(aFrame); michael@0: IncrementMutationCount(&layerActivity->mLeftRestyleCount); michael@0: IncrementMutationCount(&layerActivity->mTopRestyleCount); michael@0: IncrementMutationCount(&layerActivity->mRightRestyleCount); michael@0: IncrementMutationCount(&layerActivity->mBottomRestyleCount); michael@0: } michael@0: michael@0: /* static */ void michael@0: ActiveLayerTracker::NotifyAnimated(nsIFrame* aFrame, nsCSSProperty aProperty) michael@0: { michael@0: LayerActivity* layerActivity = GetLayerActivityForUpdate(aFrame); michael@0: uint8_t& mutationCount = layerActivity->RestyleCountForProperty(aProperty); michael@0: // We know this is animated, so just hack the mutation count. michael@0: mutationCount = 0xFF; michael@0: } michael@0: michael@0: static bool michael@0: IsPresContextInScriptAnimationCallback(nsPresContext* aPresContext) michael@0: { michael@0: if (aPresContext->RefreshDriver()->IsInRefresh()) { michael@0: return true; michael@0: } michael@0: // Treat timeouts/setintervals as scripted animation callbacks for our michael@0: // purposes. michael@0: nsPIDOMWindow* win = aPresContext->Document()->GetInnerWindow(); michael@0: return win && win->IsRunningTimeout(); michael@0: } michael@0: michael@0: /* static */ void michael@0: ActiveLayerTracker::NotifyInlineStyleRuleModified(nsIFrame* aFrame, michael@0: nsCSSProperty aProperty) michael@0: { michael@0: if (!IsPresContextInScriptAnimationCallback(aFrame->PresContext())) { michael@0: return; michael@0: } michael@0: NotifyAnimated(aFrame, aProperty); michael@0: } michael@0: michael@0: /* static */ bool michael@0: ActiveLayerTracker::IsStyleAnimated(nsIFrame* aFrame, nsCSSProperty aProperty) michael@0: { michael@0: // TODO: Add some abuse restrictions michael@0: if ((aFrame->StyleDisplay()->mWillChangeBitField & NS_STYLE_WILL_CHANGE_TRANSFORM) && michael@0: aProperty == eCSSProperty_transform) { michael@0: return true; michael@0: } michael@0: if ((aFrame->StyleDisplay()->mWillChangeBitField & NS_STYLE_WILL_CHANGE_OPACITY) && michael@0: aProperty == eCSSProperty_opacity) { michael@0: return true; michael@0: } michael@0: michael@0: LayerActivity* layerActivity = GetLayerActivity(aFrame); michael@0: if (layerActivity) { michael@0: if (layerActivity->RestyleCountForProperty(aProperty) >= 2) { michael@0: return true; michael@0: } michael@0: } michael@0: if (aProperty == eCSSProperty_transform && aFrame->Preserves3D()) { michael@0: return IsStyleAnimated(aFrame->GetParent(), aProperty); michael@0: } michael@0: nsIContent* content = aFrame->GetContent(); michael@0: if (content) { michael@0: if (mozilla::HasAnimationOrTransition( michael@0: content, nsGkAtoms::animationsProperty, aProperty)) { michael@0: return true; michael@0: } michael@0: if (mozilla::HasAnimationOrTransition( michael@0: content, nsGkAtoms::transitionsProperty, aProperty)) { michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: /* static */ bool michael@0: ActiveLayerTracker::IsOffsetOrMarginStyleAnimated(nsIFrame* aFrame) michael@0: { michael@0: LayerActivity* layerActivity = GetLayerActivity(aFrame); michael@0: if (layerActivity) { michael@0: if (layerActivity->mLeftRestyleCount >= 2 || michael@0: layerActivity->mTopRestyleCount >= 2 || michael@0: layerActivity->mRightRestyleCount >= 2 || michael@0: layerActivity->mBottomRestyleCount >= 2 || michael@0: layerActivity->mMarginLeftRestyleCount >= 2 || michael@0: layerActivity->mMarginTopRestyleCount >= 2 || michael@0: layerActivity->mMarginRightRestyleCount >= 2 || michael@0: layerActivity->mMarginBottomRestyleCount >= 2) { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: /* static */ void michael@0: ActiveLayerTracker::NotifyContentChange(nsIFrame* aFrame) michael@0: { michael@0: LayerActivity* layerActivity = GetLayerActivityForUpdate(aFrame); michael@0: layerActivity->mContentActive = true; michael@0: } michael@0: michael@0: /* static */ bool michael@0: ActiveLayerTracker::IsContentActive(nsIFrame* aFrame) michael@0: { michael@0: LayerActivity* layerActivity = GetLayerActivity(aFrame); michael@0: return layerActivity && layerActivity->mContentActive; michael@0: } michael@0: michael@0: /* static */ void michael@0: ActiveLayerTracker::Shutdown() michael@0: { michael@0: delete gLayerActivityTracker; michael@0: gLayerActivityTracker = nullptr; michael@0: } michael@0: michael@0: }