Home | History | Annotate | Download | only in animation
      1 // Copyright 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 
      5 #ifndef CC_ANIMATION_ANIMATION_H_
      6 #define CC_ANIMATION_ANIMATION_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "cc/base/cc_export.h"
     11 
     12 namespace cc {
     13 
     14 class AnimationCurve;
     15 
     16 // An Animation, contains all the state required to play an AnimationCurve.
     17 // Specifically, the affected property, the run state (paused, finished, etc.),
     18 // loop count, last pause time, and the total time spent paused.
     19 class CC_EXPORT Animation {
     20  public:
     21   // Animations begin in one of the 'waiting' states. Animations waiting for the
     22   // next tick will start the next time the controller animates. Animations
     23   // waiting for target availibility will run as soon as their target property
     24   // is free (and all the animations animating with it are also able to run).
     25   // Animations waiting for their start time to come have be scheduled to run at
     26   // a particular point in time. When this time arrives, the controller will
     27   // move the animations into the Starting state, and then into the Running
     28   // state. Running animations may toggle between Running and Paused, and may be
     29   // stopped by moving into either the Aborted or Finished states. A Finished
     30   // animation was allowed to run to completion, but an Aborted animation was
     31   // not.
     32   enum RunState {
     33     WaitingForNextTick = 0,
     34     WaitingForTargetAvailability,
     35     WaitingForStartTime,
     36     WaitingForDeletion,
     37     Starting,
     38     Running,
     39     Paused,
     40     Finished,
     41     Aborted,
     42     // This sentinel must be last.
     43     RunStateEnumSize
     44   };
     45 
     46   enum TargetProperty {
     47     Transform = 0,
     48     Opacity,
     49     // This sentinel must be last.
     50     TargetPropertyEnumSize
     51   };
     52 
     53   static scoped_ptr<Animation> Create(scoped_ptr<AnimationCurve> curve,
     54                                       int animation_id,
     55                                       int group_id,
     56                                       TargetProperty target_property);
     57 
     58   virtual ~Animation();
     59 
     60   int id() const { return id_; }
     61   int group() const { return group_; }
     62   TargetProperty target_property() const { return target_property_; }
     63 
     64   RunState run_state() const { return run_state_; }
     65   void SetRunState(RunState run_state, double monotonic_time);
     66 
     67   // This is the number of times that the animation will play. If this
     68   // value is zero the animation will not play. If it is negative, then
     69   // the animation will loop indefinitely.
     70   int iterations() const { return iterations_; }
     71   void set_iterations(int n) { iterations_ = n; }
     72 
     73   double start_time() const { return start_time_; }
     74   void set_start_time(double monotonic_time) { start_time_ = monotonic_time; }
     75   bool has_set_start_time() const { return !!start_time_; }
     76 
     77   double time_offset() const { return time_offset_; }
     78   void set_time_offset(double monotonic_time) { time_offset_ = monotonic_time; }
     79 
     80   void Suspend(double monotonic_time);
     81   void Resume(double monotonic_time);
     82 
     83   // If alternates_direction is true, on odd numbered iterations we reverse the
     84   // curve.
     85   bool alternates_direction() const { return alternates_direction_; }
     86   void set_alternates_direction(bool alternates) {
     87     alternates_direction_ = alternates;
     88   }
     89 
     90   bool IsFinishedAt(double monotonic_time) const;
     91   bool is_finished() const {
     92     return run_state_ == Finished ||
     93         run_state_ == Aborted ||
     94         run_state_ == WaitingForDeletion;
     95   }
     96 
     97   AnimationCurve* curve() { return curve_.get(); }
     98   const AnimationCurve* curve() const { return curve_.get(); }
     99 
    100   // If this is true, even if the animation is running, it will not be tickable
    101   // until it is given a start time. This is true for animations running on the
    102   // main thread.
    103   bool needs_synchronized_start_time() const {
    104     return needs_synchronized_start_time_;
    105   }
    106   void set_needs_synchronized_start_time(bool needs_synchronized_start_time) {
    107     needs_synchronized_start_time_ = needs_synchronized_start_time;
    108   }
    109 
    110   // This is true for animations running on the main thread when the Finished
    111   // event sent by the corresponding impl animation has been received.
    112   bool received_finished_event() const {
    113     return received_finished_event_;
    114   }
    115   void set_received_finished_event(bool received_finished_event) {
    116     received_finished_event_ = received_finished_event;
    117   }
    118 
    119   // Takes the given absolute time, and using the start time and the number
    120   // of iterations, returns the relative time in the current iteration.
    121   double TrimTimeToCurrentIteration(double monotonic_time) const;
    122 
    123   enum InstanceType {
    124     ControllingInstance = 0,
    125     NonControllingInstance
    126   };
    127 
    128   scoped_ptr<Animation> Clone(InstanceType instance_type) const;
    129   scoped_ptr<Animation> CloneAndInitialize(InstanceType instance_type,
    130                                            RunState initial_run_state,
    131                                            double start_time) const;
    132   bool is_controlling_instance() const { return is_controlling_instance_; }
    133 
    134   void PushPropertiesTo(Animation* other) const;
    135 
    136   void set_is_impl_only(bool is_impl_only) { is_impl_only_ = is_impl_only; }
    137   bool is_impl_only() const { return is_impl_only_; }
    138 
    139  private:
    140   Animation(scoped_ptr<AnimationCurve> curve,
    141             int animation_id,
    142             int group_id,
    143             TargetProperty target_property);
    144 
    145   scoped_ptr<AnimationCurve> curve_;
    146 
    147   // IDs are not necessarily unique.
    148   int id_;
    149 
    150   // Animations that must be run together are called 'grouped' and have the same
    151   // group id. Grouped animations are guaranteed to start at the same time and
    152   // no other animations may animate any of the group's target properties until
    153   // all animations in the group have finished animating. Note: an active
    154   // animation's group id and target property uniquely identify that animation.
    155   int group_;
    156 
    157   TargetProperty target_property_;
    158   RunState run_state_;
    159   int iterations_;
    160   double start_time_;
    161   bool alternates_direction_;
    162 
    163   // The time offset effectively pushes the start of the animation back in time.
    164   // This is used for resuming paused animations -- an animation is added with a
    165   // non-zero time offset, causing the animation to skip ahead to the desired
    166   // point in time.
    167   double time_offset_;
    168 
    169   bool needs_synchronized_start_time_;
    170   bool received_finished_event_;
    171 
    172   // When an animation is suspended, it behaves as if it is paused and it also
    173   // ignores all run state changes until it is resumed. This is used for testing
    174   // purposes.
    175   bool suspended_;
    176 
    177   // These are used in TrimTimeToCurrentIteration to account for time
    178   // spent while paused. This is not included in AnimationState since it
    179   // there is absolutely no need for clients of this controller to know
    180   // about these values.
    181   double pause_time_;
    182   double total_paused_time_;
    183 
    184   // Animations lead dual lives. An active animation will be conceptually owned
    185   // by two controllers, one on the impl thread and one on the main. In reality,
    186   // there will be two separate Animation instances for the same animation. They
    187   // will have the same group id and the same target property (these two values
    188   // uniquely identify an animation). The instance on the impl thread is the
    189   // instance that ultimately controls the values of the animating layer and so
    190   // we will refer to it as the 'controlling instance'.
    191   bool is_controlling_instance_;
    192 
    193   bool is_impl_only_;
    194 
    195   DISALLOW_COPY_AND_ASSIGN(Animation);
    196 };
    197 
    198 }  // namespace cc
    199 
    200 #endif  // CC_ANIMATION_ANIMATION_H_
    201