123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- // Copyright (c) 2012 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #ifndef UI_COMPOSITOR_LAYER_ANIMATION_ELEMENT_H_
- #define UI_COMPOSITOR_LAYER_ANIMATION_ELEMENT_H_
- #include <stdint.h>
- #include <memory>
- #include <set>
- #include "base/memory/weak_ptr.h"
- #include "base/time/time.h"
- #include "cc/animation/keyframe_model.h"
- #include "cc/trees/target_property.h"
- #include "third_party/skia/include/core/SkColor.h"
- #include "ui/compositor/compositor_export.h"
- #include "ui/gfx/animation/tween.h"
- #include "ui/gfx/geometry/linear_gradient.h"
- #include "ui/gfx/geometry/rect.h"
- #include "ui/gfx/geometry/rounded_corners_f.h"
- #include "ui/gfx/geometry/transform.h"
- namespace ui {
- class InterpolatedTransform;
- class LayerAnimationDelegate;
- // LayerAnimationElements represent one segment of an animation between two
- // keyframes. They know how to update a LayerAnimationDelegate given a value
- // between 0 and 1 (0 for initial, and 1 for final).
- class COMPOSITOR_EXPORT LayerAnimationElement {
- public:
- enum AnimatableProperty {
- UNKNOWN = 0,
- TRANSFORM = (1 << 0),
- BOUNDS = (1 << 1),
- OPACITY = (1 << 2),
- VISIBILITY = (1 << 3),
- BRIGHTNESS = (1 << 4),
- GRAYSCALE = (1 << 5),
- COLOR = (1 << 6),
- CLIP = (1 << 7),
- ROUNDED_CORNERS = (1 << 8),
- GRADIENT_MASK = (1 << 9),
- // Used when iterating over properties.
- FIRST_PROPERTY = TRANSFORM,
- SENTINEL = (1 << 10)
- };
- static AnimatableProperty ToAnimatableProperty(
- cc::TargetProperty::Type property);
- struct COMPOSITOR_EXPORT TargetValue {
- TargetValue();
- // Initializes the target value to match the delegate. NULL may be supplied.
- explicit TargetValue(const LayerAnimationDelegate* delegate);
- gfx::Rect bounds;
- gfx::Transform transform;
- float opacity;
- bool visibility;
- float brightness;
- float grayscale;
- SkColor color;
- gfx::Rect clip_rect;
- gfx::RoundedCornersF rounded_corners;
- gfx::LinearGradient gradient_mask;
- };
- typedef uint32_t AnimatableProperties;
- LayerAnimationElement(AnimatableProperties properties,
- base::TimeDelta duration);
- LayerAnimationElement& operator=(const LayerAnimationElement&) = delete;
- virtual ~LayerAnimationElement();
- static std::string AnimatablePropertiesToString(
- AnimatableProperties properties);
- // Creates an element that transitions to the given transform. The caller owns
- // the return value.
- static std::unique_ptr<LayerAnimationElement> CreateTransformElement(
- const gfx::Transform& transform,
- base::TimeDelta duration);
- // Creates an element that transitions to another in a way determined by an
- // interpolated transform. The element accepts ownership of the interpolated
- // transform. NB: at every step, the interpolated transform clobbers the
- // existing transform. That is, it does not interpolate between the existing
- // transform and the last value the interpolated transform will assume. It is
- // therefore important that the value of the interpolated at time 0 matches
- // the current transform.
- static std::unique_ptr<LayerAnimationElement>
- CreateInterpolatedTransformElement(
- std::unique_ptr<InterpolatedTransform> interpolated_transform,
- base::TimeDelta duration);
- // Creates an element that transitions to the given bounds. The caller owns
- // the return value.
- static std::unique_ptr<LayerAnimationElement> CreateBoundsElement(
- const gfx::Rect& bounds,
- base::TimeDelta duration);
- // Creates an element that transitions to the given opacity. The caller owns
- // the return value.
- static std::unique_ptr<LayerAnimationElement> CreateOpacityElement(
- float opacity,
- base::TimeDelta duration);
- // Creates an element that sets visibily following a delay. The caller owns
- // the return value.
- static std::unique_ptr<LayerAnimationElement> CreateVisibilityElement(
- bool visibility,
- base::TimeDelta duration);
- // Creates an element that transitions to the given brightness.
- // The caller owns the return value.
- static std::unique_ptr<LayerAnimationElement> CreateBrightnessElement(
- float brightness,
- base::TimeDelta duration);
- // Creates an element that transitions to the given grayscale value.
- // The caller owns the return value.
- static std::unique_ptr<LayerAnimationElement> CreateGrayscaleElement(
- float grayscale,
- base::TimeDelta duration);
- // Creates an element that pauses the given properties. The caller owns the
- // return value.
- static std::unique_ptr<LayerAnimationElement> CreatePauseElement(
- AnimatableProperties properties,
- base::TimeDelta duration);
- // Creates an element that transitions to the given color. The caller owns the
- // return value.
- static std::unique_ptr<LayerAnimationElement> CreateColorElement(
- SkColor color,
- base::TimeDelta duration);
- // Creates an element that transitions the clip rect of the layer to the given
- // bounds. The caller owns the return value.
- static std::unique_ptr<LayerAnimationElement> CreateClipRectElement(
- const gfx::Rect& clip_rect,
- base::TimeDelta duration);
- // Creates an element that transitions the rounded corners of the layer to the
- // given ones. The caller owns the return value.
- static std::unique_ptr<LayerAnimationElement> CreateRoundedCornersElement(
- const gfx::RoundedCornersF& rounded_corners,
- base::TimeDelta duration);
- // Creates an element that transitions the gradient mask to the
- // given one. The caller owns the return value.
- static std::unique_ptr<LayerAnimationElement> CreateGradientMaskElement(
- const gfx::LinearGradient& gradient_mask,
- base::TimeDelta duration);
- // Sets the start time for the animation. This must be called before the first
- // call to {Start, IsFinished}. Once the animation is finished, this must
- // be called again in order to restart the animation.
- void set_requested_start_time(base::TimeTicks start_time) {
- requested_start_time_ = start_time;
- }
- base::TimeTicks requested_start_time() const { return requested_start_time_; }
- // Sets the actual start time for the animation, taking into account any
- // queueing delays.
- void set_effective_start_time(base::TimeTicks start_time) {
- effective_start_time_ = start_time;
- }
- base::TimeTicks effective_start_time() const { return effective_start_time_; }
- // This must be called before the first call to Progress. If starting the
- // animation involves dispatching to another thread, then this will proceed
- // with that dispatch, ultimately resulting in the animation getting an
- // effective start time (the time the animation starts on the other thread).
- void Start(LayerAnimationDelegate* delegate, int animation_group_id);
- // Returns true if the animation has started but hasn't finished.
- bool Started() const { return !first_frame_; }
- // Updates the delegate to the appropriate value for |now|. Returns true
- // if a redraw is required.
- bool Progress(base::TimeTicks now, LayerAnimationDelegate* delegate);
- // If calling Progress now, with the given time, will finish the animation,
- // returns true and sets |end_duration| to the actual duration for this
- // animation, incuding any queueing delays.
- bool IsFinished(base::TimeTicks time, base::TimeDelta* total_duration);
- // Updates the delegate to the end of the animation. Returns true if a
- // redraw is required.
- bool ProgressToEnd(LayerAnimationDelegate* delegate);
- // Called if the animation is not allowed to complete. This may be called
- // before OnStarted or Progress.
- void Abort(LayerAnimationDelegate* delegate);
- // Assigns the target value to |target|.
- void GetTargetValue(TargetValue* target) const;
- // The properties that the element modifies.
- AnimatableProperties properties() const { return properties_; }
- // Whether this element animates on the compositor thread.
- virtual bool IsThreaded(LayerAnimationDelegate* delegate) const;
- gfx::Tween::Type tween_type() const { return tween_type_; }
- void set_tween_type(gfx::Tween::Type tween_type) { tween_type_ = tween_type; }
- // Each LayerAnimationElement has a unique keyframe_model_id. Elements
- // belonging to sequences that are supposed to start together have the same
- // animation_group_id.
- int keyframe_model_id() const { return keyframe_model_id_; }
- int animation_group_id() const { return animation_group_id_; }
- void set_animation_group_id(int id) { animation_group_id_ = id; }
- base::TimeDelta duration() const { return duration_; }
- // The fraction of the animation that has been completed after the last
- // call made to {Progress, ProgressToEnd}.
- double last_progressed_fraction() const { return last_progressed_fraction_; }
- std::string ToString() const;
- protected:
- void UpdateKeyframeModelId();
- virtual std::string DebugName() const;
- // Called once each time the animation element is run before any call to
- // OnProgress.
- virtual void OnStart(LayerAnimationDelegate* delegate) = 0;
- virtual bool OnProgress(double t, LayerAnimationDelegate* delegate) = 0;
- virtual void OnGetTarget(TargetValue* target) const = 0;
- virtual void OnAbort(LayerAnimationDelegate* delegate) = 0;
- // Actually start the animation, dispatching to another thread if needed.
- virtual void RequestEffectiveStart(LayerAnimationDelegate* delegate);
- LayerAnimationElement(const LayerAnimationElement& element);
- private:
- // For debugging purposes, we sometimes alter the duration we actually use.
- // For example, during tests we often set duration = 0, and it is sometimes
- // useful to slow animations down to see them more clearly.
- base::TimeDelta GetEffectiveDuration(const base::TimeDelta& delta);
- bool first_frame_;
- const AnimatableProperties properties_;
- base::TimeTicks requested_start_time_;
- // When the animation actually started, taking into account queueing delays.
- base::TimeTicks effective_start_time_;
- const base::TimeDelta duration_;
- gfx::Tween::Type tween_type_;
- int keyframe_model_id_;
- int animation_group_id_;
- double last_progressed_fraction_;
- base::WeakPtrFactory<LayerAnimationElement> weak_ptr_factory_{this};
- };
- } // namespace ui
- #endif // UI_COMPOSITOR_LAYER_ANIMATION_ELEMENT_H_
|