EffectSet.cpp (4072B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "EffectSet.h" 8 9 #include "ElementAnimationData.h" 10 #include "mozilla/LayerAnimationInfo.h" 11 #include "mozilla/PseudoStyleType.h" // For PseudoStyleRequest 12 #include "mozilla/RestyleManager.h" 13 #include "mozilla/dom/Element.h" // For Element 14 #include "nsCycleCollectionNoteChild.h" // For CycleCollectionNoteChild 15 #include "nsLayoutUtils.h" 16 #include "nsPresContext.h" 17 18 namespace mozilla { 19 20 void EffectSet::Traverse(nsCycleCollectionTraversalCallback& aCallback) { 21 for (const auto& key : mEffects) { 22 CycleCollectionNoteChild(aCallback, key, "EffectSet::mEffects[]", 23 aCallback.Flags()); 24 } 25 } 26 27 /* static */ 28 EffectSet* EffectSet::Get(const dom::Element* aElement, 29 const PseudoStyleRequest& aPseudoRequest) { 30 if (auto* data = aElement->GetAnimationData()) { 31 return data->GetEffectSetFor(aPseudoRequest); 32 } 33 return nullptr; 34 } 35 36 /* static */ 37 EffectSet* EffectSet::GetOrCreate(dom::Element* aElement, 38 const PseudoStyleRequest& aPseudoRequest) { 39 return &aElement->EnsureAnimationData().EnsureEffectSetFor(aPseudoRequest); 40 } 41 42 /* static */ 43 EffectSet* EffectSet::GetForFrame(const nsIFrame* aFrame, 44 const nsCSSPropertyIDSet& aProperties) { 45 MOZ_ASSERT(aFrame); 46 47 // Transform animations are run on the primary frame (but stored on the 48 // content associated with the style frame). 49 const nsIFrame* frameToQuery = nullptr; 50 if (aProperties.IsSubsetOf(nsCSSPropertyIDSet::TransformLikeProperties())) { 51 // Make sure to return nullptr if we're looking for transform animations on 52 // the inner table frame. 53 if (!aFrame->SupportsCSSTransforms()) { 54 return nullptr; 55 } 56 frameToQuery = nsLayoutUtils::GetStyleFrame(aFrame); 57 } else { 58 MOZ_ASSERT( 59 !aProperties.Intersects(nsCSSPropertyIDSet::TransformLikeProperties()), 60 "We should have only transform properties or no transform properties"); 61 // We don't need to explicitly return nullptr when |aFrame| is NOT the style 62 // frame since there will be no effect set in that case. 63 frameToQuery = aFrame; 64 } 65 66 Maybe<NonOwningAnimationTarget> target = 67 EffectCompositor::GetAnimationElementAndPseudoForFrame(frameToQuery); 68 return target ? Get(*target) : nullptr; 69 } 70 71 /* static */ 72 EffectSet* EffectSet::GetForFrame(const nsIFrame* aFrame, 73 DisplayItemType aDisplayItemType) { 74 return EffectSet::GetForFrame( 75 aFrame, LayerAnimationInfo::GetCSSPropertiesFor(aDisplayItemType)); 76 } 77 78 /* static */ 79 EffectSet* EffectSet::GetForStyleFrame(const nsIFrame* aStyleFrame) { 80 Maybe<NonOwningAnimationTarget> target = 81 EffectCompositor::GetAnimationElementAndPseudoForFrame(aStyleFrame); 82 return target ? Get(*target) : nullptr; 83 } 84 85 /* static */ 86 EffectSet* EffectSet::GetForEffect(const dom::KeyframeEffect* aEffect) { 87 NonOwningAnimationTarget target = aEffect->GetAnimationTarget(); 88 return target ? Get(target) : nullptr; 89 } 90 91 /* static */ 92 void EffectSet::DestroyEffectSet(dom::Element* aElement, 93 const PseudoStyleRequest& aPseudoRequest) { 94 if (auto* data = aElement->GetAnimationData()) { 95 data->ClearEffectSetFor(aPseudoRequest); 96 } 97 } 98 99 void EffectSet::UpdateAnimationGeneration(nsPresContext* aPresContext) { 100 mAnimationGeneration = 101 aPresContext->RestyleManager()->GetAnimationGeneration(); 102 } 103 104 void EffectSet::AddEffect(dom::KeyframeEffect& aEffect) { 105 if (!mEffects.EnsureInserted(&aEffect)) { 106 return; 107 } 108 109 MarkCascadeNeedsUpdate(); 110 } 111 112 void EffectSet::RemoveEffect(dom::KeyframeEffect& aEffect) { 113 if (!mEffects.EnsureRemoved(&aEffect)) { 114 return; 115 } 116 117 MarkCascadeNeedsUpdate(); 118 } 119 120 } // namespace mozilla