summaryrefslogtreecommitdiffstats
path: root/layout/style/AnimationCollection.cpp
blob: f7826f5ed3f4c8e3f5d33f10d158d664889b496f (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
/* -*- 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/AnimationCollection.h"

#include "mozilla/RestyleManagerHandle.h"
#include "mozilla/RestyleManagerHandleInlines.h"
#include "nsAnimationManager.h" // For dom::CSSAnimation
#include "nsPresContext.h"
#include "nsTransitionManager.h" // For dom::CSSTransition

namespace mozilla {

template <class AnimationType>
/* static */ void
AnimationCollection<AnimationType>::PropertyDtor(void* aObject,
                                                 nsIAtom* aPropertyName,
                                                 void* aPropertyValue,
                                                 void* aData)
{
  AnimationCollection* collection =
    static_cast<AnimationCollection*>(aPropertyValue);
#ifdef DEBUG
  MOZ_ASSERT(!collection->mCalledPropertyDtor, "can't call dtor twice");
  collection->mCalledPropertyDtor = true;
#endif
  {
    nsAutoAnimationMutationBatch mb(collection->mElement->OwnerDoc());

    for (size_t animIdx = collection->mAnimations.Length(); animIdx-- != 0; ) {
      collection->mAnimations[animIdx]->CancelFromStyle();
    }
  }
  delete collection;
}

template <class AnimationType>
/* static */ AnimationCollection<AnimationType>*
AnimationCollection<AnimationType>::GetAnimationCollection(
  dom::Element *aElement,
  CSSPseudoElementType aPseudoType)
{
  if (!aElement->MayHaveAnimations()) {
    // Early return for the most common case.
    return nullptr;
  }

  nsIAtom* propName = GetPropertyAtomForPseudoType(aPseudoType);
  if (!propName) {
    return nullptr;
  }

  return
    static_cast<AnimationCollection<AnimationType>*>(aElement->
                                                     GetProperty(propName));
}

template <class AnimationType>
/* static */ AnimationCollection<AnimationType>*
AnimationCollection<AnimationType>::GetAnimationCollection(
  const nsIFrame* aFrame)
{
  Maybe<NonOwningAnimationTarget> pseudoElement =
    EffectCompositor::GetAnimationElementAndPseudoForFrame(aFrame);
  if (!pseudoElement) {
    return nullptr;
  }

  if (!pseudoElement->mElement->MayHaveAnimations()) {
    return nullptr;
  }

  return GetAnimationCollection(pseudoElement->mElement,
                                pseudoElement->mPseudoType);
}

template <class AnimationType>
/* static */ AnimationCollection<AnimationType>*
AnimationCollection<AnimationType>::GetOrCreateAnimationCollection(
  dom::Element* aElement,
  CSSPseudoElementType aPseudoType,
  bool* aCreatedCollection)
{
  MOZ_ASSERT(aCreatedCollection);
  *aCreatedCollection = false;

  nsIAtom* propName = GetPropertyAtomForPseudoType(aPseudoType);
  MOZ_ASSERT(propName, "Should only try to create animations for one of the"
             " recognized pseudo types");

  auto collection = static_cast<AnimationCollection<AnimationType>*>(
                      aElement->GetProperty(propName));
  if (!collection) {
    // FIXME: Consider arena-allocating?
    collection = new AnimationCollection<AnimationType>(aElement, propName);
    nsresult rv =
      aElement->SetProperty(propName, collection,
                            &AnimationCollection<AnimationType>::PropertyDtor,
                            false);
    if (NS_FAILED(rv)) {
      NS_WARNING("SetProperty failed");
      // The collection must be destroyed via PropertyDtor, otherwise
      // mCalledPropertyDtor assertion is triggered in destructor.
      AnimationCollection<AnimationType>::PropertyDtor(aElement, propName,
                                                       collection, nullptr);
      return nullptr;
    }

    *aCreatedCollection = true;
    aElement->SetMayHaveAnimations();
  }

  return collection;
}

template <class AnimationType>
/* static */ nsString
AnimationCollection<AnimationType>::PseudoTypeAsString(
  CSSPseudoElementType aPseudoType)
{
  switch (aPseudoType) {
    case CSSPseudoElementType::before:
      return NS_LITERAL_STRING("::before");
    case CSSPseudoElementType::after:
      return NS_LITERAL_STRING("::after");
    default:
      MOZ_ASSERT(aPseudoType == CSSPseudoElementType::NotPseudo,
                 "Unexpected pseudo type");
      return EmptyString();
  }
}

template <class AnimationType>
void
AnimationCollection<AnimationType>::UpdateCheckGeneration(
  nsPresContext* aPresContext)
{
  if (aPresContext->RestyleManager()->IsServo()) {
    // stylo: ServoRestyleManager does not support animations yet.
    return;
  }
  mCheckGeneration =
    aPresContext->RestyleManager()->AsGecko()->GetAnimationGeneration();
}

template<class AnimationType>
/*static*/ nsIAtom*
AnimationCollection<AnimationType>::GetPropertyAtomForPseudoType(
  CSSPseudoElementType aPseudoType)
{
  nsIAtom* propName = nullptr;

  if (aPseudoType == CSSPseudoElementType::NotPseudo) {
    propName = TraitsType::ElementPropertyAtom();
  } else if (aPseudoType == CSSPseudoElementType::before) {
    propName = TraitsType::BeforePropertyAtom();
  } else if (aPseudoType == CSSPseudoElementType::after) {
    propName = TraitsType::AfterPropertyAtom();
  }

  return propName;
}

// Explicit class instantiations

template class AnimationCollection<dom::CSSAnimation>;
template class AnimationCollection<dom::CSSTransition>;

} // namespace mozilla