#ifndef CC_ANIMATION_ANIMATION_H_
#define CC_ANIMATION_ANIMATION_H_
#include <memory>
#include <string>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/time/time.h"
#include "cc/animation/animation_export.h"
#include "cc/animation/element_animations.h"
#include "cc/animation/keyframe_model.h"
#include "cc/base/protected_sequence_synchronizer.h"
#include "cc/paint/element_id.h"
#include "ui/gfx/animation/keyframe/animation_curve.h"
namespace cc {
class AnimationDelegate;
class AnimationEvents;
class AnimationHost;
class AnimationTimeline;
class KeyframeEffect;
struct AnimationEvent;
class CC_ANIMATION_EXPORT Animation : public base::RefCounted<Animation>,
public ProtectedSequenceSynchronizer {
public:
static scoped_refptr<Animation> Create(int id);
virtual scoped_refptr<Animation> CreateImplInstance() const;
Animation(const Animation&) = delete;
Animation& operator=(const Animation&) = delete;
int id() const { return id_; }
ElementId element_id() const;
KeyframeEffect* keyframe_effect() {
return keyframe_effect_.Write(*this).get();
}
const KeyframeEffect* keyframe_effect() const {
return keyframe_effect_.Read(*this);
}
AnimationHost* animation_host() {
DCHECK(IsOwnerThread() || InProtectedSequence());
return animation_host_;
}
const AnimationHost* animation_host() const {
DCHECK(IsOwnerThread() || InProtectedSequence());
return animation_host_;
}
void SetAnimationHost(AnimationHost* animation_host);
bool has_animation_host() const { return !!animation_host(); }
AnimationTimeline* animation_timeline() {
return animation_timeline_.Read(*this);
}
const AnimationTimeline* animation_timeline() const {
return animation_timeline_.Read(*this);
}
void SetAnimationTimeline(AnimationTimeline* timeline);
scoped_refptr<const ElementAnimations> element_animations() const;
void set_animation_delegate(AnimationDelegate* delegate) {
animation_delegate_ = delegate;
}
void AttachElement(ElementId element_id);
void AttachPaintWorkletElement();
void DetachElement();
void AddKeyframeModel(std::unique_ptr<KeyframeModel> keyframe_model);
void PauseKeyframeModel(int keyframe_model_id, base::TimeDelta time_offset);
virtual void RemoveKeyframeModel(int keyframe_model_id);
void AbortKeyframeModel(int keyframe_model_id);
void NotifyKeyframeModelFinishedForTesting(
int timeline_id,
int keyframe_model_id,
TargetProperty::Type target_property,
int group_id);
void AbortKeyframeModelsWithProperty(TargetProperty::Type target_property,
bool needs_completion);
virtual void PushPropertiesTo(Animation* animation_impl);
virtual void UpdateState(bool start_ready_keyframe_models,
AnimationEvents* events);
virtual void TakeTimeUpdatedEvent(AnimationEvents* events) {}
virtual bool Tick(base::TimeTicks tick_time);
bool IsScrollLinkedAnimation() const;
void AddToTicking();
void RemoveFromTicking();
void DispatchAndDelegateAnimationEvent(const AnimationEvent& event);
bool RequiresInvalidation() const;
bool AffectsNativeProperty() const;
void SetNeedsPushProperties();
void ActivateKeyframeModels();
KeyframeModel* GetKeyframeModel(TargetProperty::Type target_property) const;
std::string ToString() const;
void SetNeedsCommit();
void set_is_replacement() { is_replacement_ = true; }
std::optional<base::TimeTicks> GetStartTime() const;
virtual bool IsWorkletAnimation() const;
void SetKeyframeEffectForTesting(std::unique_ptr<KeyframeEffect>);
bool IsOwnerThread() const override;
bool InProtectedSequence() const override;
void WaitForProtectedSequenceCompletion() const override;
private:
friend class base::RefCounted<Animation>;
void RegisterAnimation();
void UnregisterAnimation();
void DelegateAnimationEvent(const AnimationEvent& event);
void AttachElementInternal(ElementId element_id);
protected:
explicit Animation(int id);
~Animation() override;
raw_ptr<AnimationDelegate> animation_delegate_ = nullptr;
const int id_;
private:
bool is_replacement_ = false;
raw_ptr<AnimationHost> animation_host_ = nullptr;
ProtectedSequenceReadable<raw_ptr<AnimationTimeline>> animation_timeline_{
nullptr};
ProtectedSequenceWritable<std::unique_ptr<KeyframeEffect>> keyframe_effect_;
};
}
#endif