summaryrefslogtreecommitdiff
path: root/dom/animation/EffectSet.h
blob: 2e0f01f1a723133c3caa2370c6b527015e6444b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

#ifndef mozilla_EffectSet_h
#define mozilla_EffectSet_h

#include "mozilla/AnimValuesStyleRule.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/EffectCompositor.h"
#include "mozilla/EnumeratedArray.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/dom/KeyframeEffectReadOnly.h"
#include "nsHashKeys.h" // For nsPtrHashKey
#include "nsTHashtable.h" // For nsTHashtable

class nsPresContext;

namespace mozilla {

namespace dom {
class Element;
} // namespace dom

enum class CSSPseudoElementType : uint8_t;

// A wrapper around a hashset of AnimationEffect objects to handle
// storing the set as a property of an element.
class EffectSet
{
public:
  EffectSet()
    : mCascadeNeedsUpdate(false)
    , mAnimationGeneration(0)
#ifdef DEBUG
    , mActiveIterators(0)
    , mCalledPropertyDtor(false)
#endif
  {
    MOZ_COUNT_CTOR(EffectSet);
  }

  ~EffectSet()
  {
    MOZ_ASSERT(mCalledPropertyDtor,
               "must call destructor through element property dtor");
    MOZ_ASSERT(mActiveIterators == 0,
               "Effect set should not be destroyed while it is being "
               "enumerated");
    MOZ_COUNT_DTOR(EffectSet);
  }
  static void PropertyDtor(void* aObject, nsIAtom* aPropertyName,
                           void* aPropertyValue, void* aData);

  // Methods for supporting cycle-collection
  void Traverse(nsCycleCollectionTraversalCallback& aCallback);

  static EffectSet* GetEffectSet(dom::Element* aElement,
                                 CSSPseudoElementType aPseudoType);
  static EffectSet* GetEffectSet(const nsIFrame* aFrame);
  static EffectSet* GetOrCreateEffectSet(dom::Element* aElement,
                                         CSSPseudoElementType aPseudoType);
  static void DestroyEffectSet(dom::Element* aElement,
                               CSSPseudoElementType aPseudoType);

  void AddEffect(dom::KeyframeEffectReadOnly& aEffect);
  void RemoveEffect(dom::KeyframeEffectReadOnly& aEffect);

private:
  typedef nsTHashtable<nsRefPtrHashKey<dom::KeyframeEffectReadOnly>>
    OwningEffectSet;

public:
  // A simple iterator to support iterating over the effects in this object in
  // range-based for loops.
  //
  // This allows us to avoid exposing mEffects directly and saves the
  // caller from having to dereference hashtable iterators using
  // the rather complicated: iter.Get()->GetKey().
  class Iterator
  {
  public:
    explicit Iterator(EffectSet& aEffectSet)
      : mEffectSet(aEffectSet)
      , mHashIterator(mozilla::Move(aEffectSet.mEffects.Iter()))
      , mIsEndIterator(false)
    {
#ifdef DEBUG
      mEffectSet.mActiveIterators++;
#endif
    }

    Iterator(Iterator&& aOther)
      : mEffectSet(aOther.mEffectSet)
      , mHashIterator(mozilla::Move(aOther.mHashIterator))
      , mIsEndIterator(aOther.mIsEndIterator)
    {
#ifdef DEBUG
      mEffectSet.mActiveIterators++;
#endif
    }

    static Iterator EndIterator(EffectSet& aEffectSet)
    {
      Iterator result(aEffectSet);
      result.mIsEndIterator = true;
      return result;
    }

    ~Iterator()
    {
#ifdef DEBUG
      MOZ_ASSERT(mEffectSet.mActiveIterators > 0);
      mEffectSet.mActiveIterators--;
#endif
    }

    bool operator!=(const Iterator& aOther) const {
      if (Done() || aOther.Done()) {
        return Done() != aOther.Done();
      }
      return mHashIterator.Get() != aOther.mHashIterator.Get();
    }

    Iterator& operator++() {
      MOZ_ASSERT(!Done());
      mHashIterator.Next();
      return *this;
    }

    dom::KeyframeEffectReadOnly* operator* ()
    {
      MOZ_ASSERT(!Done());
      return mHashIterator.Get()->GetKey();
    }

  private:
    Iterator() = delete;
    Iterator(const Iterator&) = delete;
    Iterator& operator=(const Iterator&) = delete;
    Iterator& operator=(const Iterator&&) = delete;

    bool Done() const {
      return mIsEndIterator || mHashIterator.Done();
    }

    EffectSet& mEffectSet;
    OwningEffectSet::Iterator mHashIterator;
    bool mIsEndIterator;
  };

  friend class Iterator;

  Iterator begin() { return Iterator(*this); }
  Iterator end() { return Iterator::EndIterator(*this); }
#ifdef DEBUG
  bool IsBeingEnumerated() const { return mActiveIterators != 0; }
#endif

  bool IsEmpty() const { return mEffects.IsEmpty(); }

  size_t Count() const { return mEffects.Count(); }

  RefPtr<AnimValuesStyleRule>& AnimationRule(EffectCompositor::CascadeLevel
                                             aCascadeLevel)
  {
    return mAnimationRule[aCascadeLevel];
  }

  const TimeStamp& AnimationRuleRefreshTime(EffectCompositor::CascadeLevel
                                              aCascadeLevel) const
  {
    return mAnimationRuleRefreshTime[aCascadeLevel];
  }
  void UpdateAnimationRuleRefreshTime(EffectCompositor::CascadeLevel
                                        aCascadeLevel,
                                      const TimeStamp& aRefreshTime)
  {
    mAnimationRuleRefreshTime[aCascadeLevel] = aRefreshTime;
  }

  bool CascadeNeedsUpdate() const { return mCascadeNeedsUpdate; }
  void MarkCascadeNeedsUpdate() { mCascadeNeedsUpdate = true; }
  void MarkCascadeUpdated() { mCascadeNeedsUpdate = false; }

  void UpdateAnimationGeneration(nsPresContext* aPresContext);
  uint64_t GetAnimationGeneration() const { return mAnimationGeneration; }

  static nsIAtom** GetEffectSetPropertyAtoms();

  nsCSSPropertyIDSet& PropertiesWithImportantRules()
  {
    return mPropertiesWithImportantRules;
  }
  nsCSSPropertyIDSet& PropertiesForAnimationsLevel()
  {
    return mPropertiesForAnimationsLevel;
  }

private:
  static nsIAtom* GetEffectSetPropertyAtom(CSSPseudoElementType aPseudoType);

  OwningEffectSet mEffects;

  // These style rules contain the style data for currently animating
  // values.  They only match when styling with animation.  When we
  // style without animation, we need to not use them so that we can
  // detect any new changes; if necessary we restyle immediately
  // afterwards with animation.
  EnumeratedArray<EffectCompositor::CascadeLevel,
                  EffectCompositor::CascadeLevel(
                    EffectCompositor::kCascadeLevelCount),
                  RefPtr<AnimValuesStyleRule>> mAnimationRule;

  // A parallel array to mAnimationRule that records the refresh driver
  // timestamp when the rule was last updated. This is used for certain
  // animations which are updated only periodically (e.g. transform animations
  // running on the compositor that affect the scrollable overflow region).
  EnumeratedArray<EffectCompositor::CascadeLevel,
                  EffectCompositor::CascadeLevel(
                    EffectCompositor::kCascadeLevelCount),
                  TimeStamp> mAnimationRuleRefreshTime;

  // Dirty flag to represent when the mPropertiesWithImportantRules and
  // mPropertiesForAnimationsLevel on effects in this set might need to be
  // updated.
  //
  // Set to true any time the set of effects is changed or when
  // one the effects goes in or out of the "in effect" state.
  bool mCascadeNeedsUpdate;

  // RestyleManager keeps track of the number of animation restyles.
  // 'mini-flushes' (see nsTransitionManager::UpdateAllThrottledStyles()).
  // mAnimationGeneration is the sequence number of the last flush where a
  // transition/animation changed.  We keep a similar count on the
  // corresponding layer so we can check that the layer is up to date with
  // the animation manager.
  uint64_t mAnimationGeneration;

  // Specifies the compositor-animatable properties that are overridden by
  // !important rules.
  nsCSSPropertyIDSet mPropertiesWithImportantRules;
  // Specifies the properties for which the result will be added to the
  // animations level of the cascade and hence should be skipped when we are
  // composing the animation style for the transitions level of the cascede.
  nsCSSPropertyIDSet mPropertiesForAnimationsLevel;

#ifdef DEBUG
  // Track how many iterators are referencing this effect set when we are
  // destroyed, we can assert that nothing is still pointing to us.
  uint64_t mActiveIterators;

  bool mCalledPropertyDtor;
#endif
};

} // namespace mozilla

#endif // mozilla_EffectSet_h