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 enum class TimelinePhase;
46 class AnimationEffectOwner;
47 class EffectTiming;
48 class ComputedEffectTiming;
49 class OptionalEffectTiming;
50 class WorkletAnimation;
51 
52 enum TimingUpdateReason {
53   kTimingUpdateOnDemand,
54   kTimingUpdateForAnimationFrame
55 };
56 
57 // Represents the content of an Animation and its fractional timing state.
58 // https://drafts.csswg.org/web-animations/#the-animationeffect-interface
59 class CORE_EXPORT AnimationEffect : public ScriptWrappable {
60   DEFINE_WRAPPERTYPEINFO();
61   // Calls Attach/Detach, GetAnimation, UpdateInheritedTime.
62   friend class Animation;
63   friend class WorkletAnimation;
64 
65   // Calls GetAnimation().
66   // TODO(majidvp): Remove this. EffectStack should not need to access animation
67   // directly.
68   friend class EffectStack;
69 
70  public:
71   class EventDelegate : public GarbageCollected<EventDelegate> {
72    public:
73     virtual ~EventDelegate() = default;
74     virtual bool RequiresIterationEvents(const AnimationEffect&) = 0;
75     virtual void OnEventCondition(const AnimationEffect&, Timing::Phase) = 0;
IsAnimationEventDelegate()76     virtual bool IsAnimationEventDelegate() const { return false; }
IsTransitionEventDelegate()77     virtual bool IsTransitionEventDelegate() const { return false; }
Trace(Visitor * visitor)78     virtual void Trace(Visitor* visitor) const {}
79   };
80 
81   ~AnimationEffect() override = default;
82 
IsKeyframeEffect()83   virtual bool IsKeyframeEffect() const { return false; }
IsInertEffect()84   virtual bool IsInertEffect() const { return false; }
85 
GetPhase()86   Timing::Phase GetPhase() const { return EnsureCalculated().phase; }
IsCurrent()87   bool IsCurrent() const { return EnsureCalculated().is_current; }
IsInEffect()88   bool IsInEffect() const { return EnsureCalculated().is_in_effect; }
IsInPlay()89   bool IsInPlay() const { return EnsureCalculated().is_in_play; }
CurrentIteration()90   base::Optional<double> CurrentIteration() const {
91     return EnsureCalculated().current_iteration;
92   }
Progress()93   base::Optional<double> Progress() const {
94     return EnsureCalculated().progress;
95   }
TimeToForwardsEffectChange()96   AnimationTimeDelta TimeToForwardsEffectChange() const {
97     return EnsureCalculated().time_to_forwards_effect_change;
98   }
TimeToReverseEffectChange()99   AnimationTimeDelta TimeToReverseEffectChange() const {
100     return EnsureCalculated().time_to_reverse_effect_change;
101   }
LocalTime()102   base::Optional<double> LocalTime() const {
103     return EnsureCalculated().local_time;
104   }
105 
SpecifiedTiming()106   const Timing& SpecifiedTiming() const { return timing_; }
107   void UpdateSpecifiedTiming(const Timing&);
108   void SetIgnoreCssTimingProperties();
109 
GetEventDelegate()110   EventDelegate* GetEventDelegate() { return event_delegate_; }
SetEventDelegate(EventDelegate * delegate)111   void SetEventDelegate(EventDelegate* delegate) { event_delegate_ = delegate; }
112 
113   EffectTiming* getTiming() const;
114   ComputedEffectTiming* getComputedTiming() const;
115   void updateTiming(OptionalEffectTiming*,
116                     ExceptionState& = ASSERT_NO_EXCEPTION);
GetCancelTime()117   double GetCancelTime() const { return cancel_time_; }
SetCancelTime(double cancel_time)118   void SetCancelTime(double cancel_time) { cancel_time_ = cancel_time; }
119 
120   // Attach/Detach the AnimationEffect from its owning animation.
Attach(AnimationEffectOwner * owner)121   virtual void Attach(AnimationEffectOwner* owner) { owner_ = owner; }
Detach()122   virtual void Detach() {
123     DCHECK(owner_);
124     owner_ = nullptr;
125   }
126 
GetAnimationForTesting()127   const Animation* GetAnimationForTesting() const { return GetAnimation(); }
128 
129   void Trace(Visitor*) const override;
130 
131  protected:
132   explicit AnimationEffect(const Timing&, EventDelegate* = nullptr);
133 
134   // When AnimationEffect receives a new inherited time via updateInheritedTime
135   // it will (if necessary) recalculate timings and (if necessary) call
136   // updateChildrenAndEffects.
137   void UpdateInheritedTime(base::Optional<double> inherited_time,
138                            base::Optional<TimelinePhase> inherited_phase,
139                            TimingUpdateReason) const;
Invalidate()140   void Invalidate() const { needs_update_ = true; }
141   void InvalidateAndNotifyOwner() const;
RequiresIterationEvents()142   bool RequiresIterationEvents() const {
143     return event_delegate_ && event_delegate_->RequiresIterationEvents(*this);
144   }
ClearEventDelegate()145   void ClearEventDelegate() { event_delegate_ = nullptr; }
146 
147   virtual void UpdateChildrenAndEffects() const = 0;
148 
149   // This is the value of the iteration duration when it is specified as 'auto'.
150   // In web-animations-1, auto is treated as "the value zero for the purpose of
151   // timing model calculations and for the result of the duration member
152   // returned from getComputedTiming()".
IntrinsicIterationDuration()153   virtual AnimationTimeDelta IntrinsicIterationDuration() const {
154     return AnimationTimeDelta();
155   }
156 
157   virtual AnimationTimeDelta CalculateTimeToEffectChange(
158       bool forwards,
159       base::Optional<double> local_time,
160       AnimationTimeDelta time_to_next_iteration) const = 0;
161 
162   const Animation* GetAnimation() const;
163   Animation* GetAnimation();
164 
165   Member<AnimationEffectOwner> owner_;
166   Timing timing_;
167   Member<EventDelegate> event_delegate_;
168 
169   mutable Timing::CalculatedTiming calculated_;
170   mutable bool needs_update_;
171   mutable base::Optional<double> last_update_time_;
172   mutable base::Optional<Timing::Phase> last_update_phase_;
173   double cancel_time_;
174   const Timing::CalculatedTiming& EnsureCalculated() const;
175 };
176 
177 }  // namespace blink
178 
179 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_ANIMATION_ANIMATION_EFFECT_H_
180