layer_animation_element.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_COMPOSITOR_LAYER_ANIMATION_ELEMENT_H_
  5. #define UI_COMPOSITOR_LAYER_ANIMATION_ELEMENT_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <set>
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/time/time.h"
  11. #include "cc/animation/keyframe_model.h"
  12. #include "cc/trees/target_property.h"
  13. #include "third_party/skia/include/core/SkColor.h"
  14. #include "ui/compositor/compositor_export.h"
  15. #include "ui/gfx/animation/tween.h"
  16. #include "ui/gfx/geometry/linear_gradient.h"
  17. #include "ui/gfx/geometry/rect.h"
  18. #include "ui/gfx/geometry/rounded_corners_f.h"
  19. #include "ui/gfx/geometry/transform.h"
  20. namespace ui {
  21. class InterpolatedTransform;
  22. class LayerAnimationDelegate;
  23. // LayerAnimationElements represent one segment of an animation between two
  24. // keyframes. They know how to update a LayerAnimationDelegate given a value
  25. // between 0 and 1 (0 for initial, and 1 for final).
  26. class COMPOSITOR_EXPORT LayerAnimationElement {
  27. public:
  28. enum AnimatableProperty {
  29. UNKNOWN = 0,
  30. TRANSFORM = (1 << 0),
  31. BOUNDS = (1 << 1),
  32. OPACITY = (1 << 2),
  33. VISIBILITY = (1 << 3),
  34. BRIGHTNESS = (1 << 4),
  35. GRAYSCALE = (1 << 5),
  36. COLOR = (1 << 6),
  37. CLIP = (1 << 7),
  38. ROUNDED_CORNERS = (1 << 8),
  39. GRADIENT_MASK = (1 << 9),
  40. // Used when iterating over properties.
  41. FIRST_PROPERTY = TRANSFORM,
  42. SENTINEL = (1 << 10)
  43. };
  44. static AnimatableProperty ToAnimatableProperty(
  45. cc::TargetProperty::Type property);
  46. struct COMPOSITOR_EXPORT TargetValue {
  47. TargetValue();
  48. // Initializes the target value to match the delegate. NULL may be supplied.
  49. explicit TargetValue(const LayerAnimationDelegate* delegate);
  50. gfx::Rect bounds;
  51. gfx::Transform transform;
  52. float opacity;
  53. bool visibility;
  54. float brightness;
  55. float grayscale;
  56. SkColor color;
  57. gfx::Rect clip_rect;
  58. gfx::RoundedCornersF rounded_corners;
  59. gfx::LinearGradient gradient_mask;
  60. };
  61. typedef uint32_t AnimatableProperties;
  62. LayerAnimationElement(AnimatableProperties properties,
  63. base::TimeDelta duration);
  64. LayerAnimationElement& operator=(const LayerAnimationElement&) = delete;
  65. virtual ~LayerAnimationElement();
  66. static std::string AnimatablePropertiesToString(
  67. AnimatableProperties properties);
  68. // Creates an element that transitions to the given transform. The caller owns
  69. // the return value.
  70. static std::unique_ptr<LayerAnimationElement> CreateTransformElement(
  71. const gfx::Transform& transform,
  72. base::TimeDelta duration);
  73. // Creates an element that transitions to another in a way determined by an
  74. // interpolated transform. The element accepts ownership of the interpolated
  75. // transform. NB: at every step, the interpolated transform clobbers the
  76. // existing transform. That is, it does not interpolate between the existing
  77. // transform and the last value the interpolated transform will assume. It is
  78. // therefore important that the value of the interpolated at time 0 matches
  79. // the current transform.
  80. static std::unique_ptr<LayerAnimationElement>
  81. CreateInterpolatedTransformElement(
  82. std::unique_ptr<InterpolatedTransform> interpolated_transform,
  83. base::TimeDelta duration);
  84. // Creates an element that transitions to the given bounds. The caller owns
  85. // the return value.
  86. static std::unique_ptr<LayerAnimationElement> CreateBoundsElement(
  87. const gfx::Rect& bounds,
  88. base::TimeDelta duration);
  89. // Creates an element that transitions to the given opacity. The caller owns
  90. // the return value.
  91. static std::unique_ptr<LayerAnimationElement> CreateOpacityElement(
  92. float opacity,
  93. base::TimeDelta duration);
  94. // Creates an element that sets visibily following a delay. The caller owns
  95. // the return value.
  96. static std::unique_ptr<LayerAnimationElement> CreateVisibilityElement(
  97. bool visibility,
  98. base::TimeDelta duration);
  99. // Creates an element that transitions to the given brightness.
  100. // The caller owns the return value.
  101. static std::unique_ptr<LayerAnimationElement> CreateBrightnessElement(
  102. float brightness,
  103. base::TimeDelta duration);
  104. // Creates an element that transitions to the given grayscale value.
  105. // The caller owns the return value.
  106. static std::unique_ptr<LayerAnimationElement> CreateGrayscaleElement(
  107. float grayscale,
  108. base::TimeDelta duration);
  109. // Creates an element that pauses the given properties. The caller owns the
  110. // return value.
  111. static std::unique_ptr<LayerAnimationElement> CreatePauseElement(
  112. AnimatableProperties properties,
  113. base::TimeDelta duration);
  114. // Creates an element that transitions to the given color. The caller owns the
  115. // return value.
  116. static std::unique_ptr<LayerAnimationElement> CreateColorElement(
  117. SkColor color,
  118. base::TimeDelta duration);
  119. // Creates an element that transitions the clip rect of the layer to the given
  120. // bounds. The caller owns the return value.
  121. static std::unique_ptr<LayerAnimationElement> CreateClipRectElement(
  122. const gfx::Rect& clip_rect,
  123. base::TimeDelta duration);
  124. // Creates an element that transitions the rounded corners of the layer to the
  125. // given ones. The caller owns the return value.
  126. static std::unique_ptr<LayerAnimationElement> CreateRoundedCornersElement(
  127. const gfx::RoundedCornersF& rounded_corners,
  128. base::TimeDelta duration);
  129. // Creates an element that transitions the gradient mask to the
  130. // given one. The caller owns the return value.
  131. static std::unique_ptr<LayerAnimationElement> CreateGradientMaskElement(
  132. const gfx::LinearGradient& gradient_mask,
  133. base::TimeDelta duration);
  134. // Sets the start time for the animation. This must be called before the first
  135. // call to {Start, IsFinished}. Once the animation is finished, this must
  136. // be called again in order to restart the animation.
  137. void set_requested_start_time(base::TimeTicks start_time) {
  138. requested_start_time_ = start_time;
  139. }
  140. base::TimeTicks requested_start_time() const { return requested_start_time_; }
  141. // Sets the actual start time for the animation, taking into account any
  142. // queueing delays.
  143. void set_effective_start_time(base::TimeTicks start_time) {
  144. effective_start_time_ = start_time;
  145. }
  146. base::TimeTicks effective_start_time() const { return effective_start_time_; }
  147. // This must be called before the first call to Progress. If starting the
  148. // animation involves dispatching to another thread, then this will proceed
  149. // with that dispatch, ultimately resulting in the animation getting an
  150. // effective start time (the time the animation starts on the other thread).
  151. void Start(LayerAnimationDelegate* delegate, int animation_group_id);
  152. // Returns true if the animation has started but hasn't finished.
  153. bool Started() const { return !first_frame_; }
  154. // Updates the delegate to the appropriate value for |now|. Returns true
  155. // if a redraw is required.
  156. bool Progress(base::TimeTicks now, LayerAnimationDelegate* delegate);
  157. // If calling Progress now, with the given time, will finish the animation,
  158. // returns true and sets |end_duration| to the actual duration for this
  159. // animation, incuding any queueing delays.
  160. bool IsFinished(base::TimeTicks time, base::TimeDelta* total_duration);
  161. // Updates the delegate to the end of the animation. Returns true if a
  162. // redraw is required.
  163. bool ProgressToEnd(LayerAnimationDelegate* delegate);
  164. // Called if the animation is not allowed to complete. This may be called
  165. // before OnStarted or Progress.
  166. void Abort(LayerAnimationDelegate* delegate);
  167. // Assigns the target value to |target|.
  168. void GetTargetValue(TargetValue* target) const;
  169. // The properties that the element modifies.
  170. AnimatableProperties properties() const { return properties_; }
  171. // Whether this element animates on the compositor thread.
  172. virtual bool IsThreaded(LayerAnimationDelegate* delegate) const;
  173. gfx::Tween::Type tween_type() const { return tween_type_; }
  174. void set_tween_type(gfx::Tween::Type tween_type) { tween_type_ = tween_type; }
  175. // Each LayerAnimationElement has a unique keyframe_model_id. Elements
  176. // belonging to sequences that are supposed to start together have the same
  177. // animation_group_id.
  178. int keyframe_model_id() const { return keyframe_model_id_; }
  179. int animation_group_id() const { return animation_group_id_; }
  180. void set_animation_group_id(int id) { animation_group_id_ = id; }
  181. base::TimeDelta duration() const { return duration_; }
  182. // The fraction of the animation that has been completed after the last
  183. // call made to {Progress, ProgressToEnd}.
  184. double last_progressed_fraction() const { return last_progressed_fraction_; }
  185. std::string ToString() const;
  186. protected:
  187. void UpdateKeyframeModelId();
  188. virtual std::string DebugName() const;
  189. // Called once each time the animation element is run before any call to
  190. // OnProgress.
  191. virtual void OnStart(LayerAnimationDelegate* delegate) = 0;
  192. virtual bool OnProgress(double t, LayerAnimationDelegate* delegate) = 0;
  193. virtual void OnGetTarget(TargetValue* target) const = 0;
  194. virtual void OnAbort(LayerAnimationDelegate* delegate) = 0;
  195. // Actually start the animation, dispatching to another thread if needed.
  196. virtual void RequestEffectiveStart(LayerAnimationDelegate* delegate);
  197. LayerAnimationElement(const LayerAnimationElement& element);
  198. private:
  199. // For debugging purposes, we sometimes alter the duration we actually use.
  200. // For example, during tests we often set duration = 0, and it is sometimes
  201. // useful to slow animations down to see them more clearly.
  202. base::TimeDelta GetEffectiveDuration(const base::TimeDelta& delta);
  203. bool first_frame_;
  204. const AnimatableProperties properties_;
  205. base::TimeTicks requested_start_time_;
  206. // When the animation actually started, taking into account queueing delays.
  207. base::TimeTicks effective_start_time_;
  208. const base::TimeDelta duration_;
  209. gfx::Tween::Type tween_type_;
  210. int keyframe_model_id_;
  211. int animation_group_id_;
  212. double last_progressed_fraction_;
  213. base::WeakPtrFactory<LayerAnimationElement> weak_ptr_factory_{this};
  214. };
  215. } // namespace ui
  216. #endif // UI_COMPOSITOR_LAYER_ANIMATION_ELEMENT_H_