1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_ANIMATION_ANIMATION_EFFECT_H_
32 #define THIRD_PARTY_BLINK_RENDERER_CORE_ANIMATION_ANIMATION_EFFECT_H_
33 
34 #include "base/optional.h"
35 #include "third_party/blink/renderer/core/animation/animation_time_delta.h"
36 #include "third_party/blink/renderer/core/animation/timing.h"
37 #include "third_party/blink/renderer/core/core_export.h"
38 #include "third_party/blink/renderer/platform/bindings/exception_state.h"
39 #include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
40 #include "third_party/blink/renderer/platform/heap/handle.h"
41 
42 namespace blink {
43 
44 class Animation;
45 class AnimationEffectOwner;
46 class EffectTiming;
47 class ComputedEffectTiming;
48 class OptionalEffectTiming;
49 class WorkletAnimation;
50 
51 enum TimingUpdateReason {
52   kTimingUpdateOnDemand,
53   kTimingUpdateForAnimationFrame
54 };
55 
56 // Represents the content of an Animation and its fractional timing state.
57 // https://drafts.csswg.org/web-animations/#the-animationeffect-interface
58 class CORE_EXPORT AnimationEffect : public ScriptWrappable {
59   DEFINE_WRAPPERTYPEINFO();
60   // Calls Attach/Detach, GetAnimation, UpdateInheritedTime.
61   friend class Animation;
62   friend class WorkletAnimation;
63 
64   // Calls GetAnimation().
65   // TODO(majidvp): Remove this. EffectStack should not need to access animation
66   // directly.
67   friend class EffectStack;
68 
69  public:
70   class EventDelegate : public GarbageCollected<EventDelegate> {
71    public:
72     virtual ~EventDelegate() = default;
73     virtual bool RequiresIterationEvents(const AnimationEffect&) = 0;
74     virtual void OnEventCondition(const AnimationEffect&, Timing::Phase) = 0;
IsAnimationEventDelegate()75     virtual bool IsAnimationEventDelegate() const { return false; }
IsTransitionEventDelegate()76     virtual bool IsTransitionEventDelegate() const { return false; }
Trace(Visitor * visitor)77     virtual void Trace(Visitor* visitor) {}
78   };
79 
80   ~AnimationEffect() override = default;
81 
IsKeyframeEffect()82   virtual bool IsKeyframeEffect() const { return false; }
IsInertEffect()83   virtual bool IsInertEffect() const { return false; }
84 
GetPhase()85   Timing::Phase GetPhase() const { return EnsureCalculated().phase; }
IsCurrent()86   bool IsCurrent() const { return EnsureCalculated().is_current; }
IsInEffect()87   bool IsInEffect() const { return EnsureCalculated().is_in_effect; }
IsInPlay()88   bool IsInPlay() const { return EnsureCalculated().is_in_play; }
CurrentIteration()89   base::Optional<double> CurrentIteration() const {
90     return EnsureCalculated().current_iteration;
91   }
Progress()92   base::Optional<double> Progress() const {
93     return EnsureCalculated().progress;
94   }
TimeToForwardsEffectChange()95   AnimationTimeDelta TimeToForwardsEffectChange() const {
96     return EnsureCalculated().time_to_forwards_effect_change;
97   }
TimeToReverseEffectChange()98   AnimationTimeDelta TimeToReverseEffectChange() const {
99     return EnsureCalculated().time_to_reverse_effect_change;
100   }
LocalTime()101   double LocalTime() const {
102     return EnsureCalculated().local_time.value_or(Timing::NullValue());
103   }
104 
SpecifiedTiming()105   const Timing& SpecifiedTiming() const { return timing_; }
106   void UpdateSpecifiedTiming(const Timing&);
107   void SetIgnoreCssTimingProperties();
108 
GetEventDelegate()109   EventDelegate* GetEventDelegate() { return event_delegate_; }
SetEventDelegate(EventDelegate * delegate)110   void SetEventDelegate(EventDelegate* delegate) { event_delegate_ = delegate; }
111 
112   EffectTiming* getTiming() const;
113   ComputedEffectTiming* getComputedTiming() const;
114   void updateTiming(OptionalEffectTiming*,
115                     ExceptionState& = ASSERT_NO_EXCEPTION);
GetCancelTime()116   double GetCancelTime() const { return cancel_time_; }
SetCancelTime(double cancel_time)117   void SetCancelTime(double cancel_time) { cancel_time_ = cancel_time; }
118 
119   // Attach/Detach the AnimationEffect from its owning animation.
Attach(AnimationEffectOwner * owner)120   virtual void Attach(AnimationEffectOwner* owner) { owner_ = owner; }
Detach()121   virtual void Detach() {
122     DCHECK(owner_);
123     owner_ = nullptr;
124   }
125 
GetAnimationForTesting()126   const Animation* GetAnimationForTesting() const { return GetAnimation(); }
127 
128   void Trace(Visitor*) override;
129 
130  protected:
131   explicit AnimationEffect(const Timing&, EventDelegate* = nullptr);
132 
133   // When AnimationEffect receives a new inherited time via updateInheritedTime
134   // it will (if necessary) recalculate timings and (if necessary) call
135   // updateChildrenAndEffects.
136   void UpdateInheritedTime(base::Optional<double> inherited_time,
137                            TimingUpdateReason) const;
Invalidate()138   void Invalidate() const { needs_update_ = true; }
139   void InvalidateAndNotifyOwner() const;
RequiresIterationEvents()140   bool RequiresIterationEvents() const {
141     return event_delegate_ && event_delegate_->RequiresIterationEvents(*this);
142   }
ClearEventDelegate()143   void ClearEventDelegate() { event_delegate_ = nullptr; }
144 
145   virtual void UpdateChildrenAndEffects() const = 0;
146 
147   // This is the value of the iteration duration when it is specified as 'auto'.
148   // In web-animations-1, auto is treated as "the value zero for the purpose of
149   // timing model calculations and for the result of the duration member
150   // returned from getComputedTiming()".
IntrinsicIterationDuration()151   virtual AnimationTimeDelta IntrinsicIterationDuration() const {
152     return AnimationTimeDelta();
153   }
154 
155   virtual AnimationTimeDelta CalculateTimeToEffectChange(
156       bool forwards,
157       base::Optional<double> local_time,
158       AnimationTimeDelta time_to_next_iteration) const = 0;
159 
160   const Animation* GetAnimation() const;
161   Animation* GetAnimation();
162 
163   Member<AnimationEffectOwner> owner_;
164   Timing timing_;
165   Member<EventDelegate> event_delegate_;
166 
167   mutable Timing::CalculatedTiming calculated_;
168   mutable bool needs_update_;
169   mutable base::Optional<double> last_update_time_;
170   double cancel_time_;
171   const Timing::CalculatedTiming& EnsureCalculated() const;
172 };
173 
174 }  // namespace blink
175 
176 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_ANIMATION_ANIMATION_EFFECT_H_
177