summaryrefslogtreecommitdiffstats
path: root/chromium/cc/animation/animation.cc
blob: 4adb88a12e5ea3a500e7a1774f1445eaf1e47d99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright 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.

#include "cc/animation/animation.h"

#include <cmath>

#include "base/debug/trace_event.h"
#include "base/strings/string_util.h"
#include "cc/animation/animation_curve.h"

namespace {

// This should match the RunState enum.
static const char* const s_runStateNames[] = {
  "WaitingForTargetAvailability",
  "WaitingForDeletion",
  "Starting",
  "Running",
  "Paused",
  "Finished",
  "Aborted"
};

COMPILE_ASSERT(static_cast<int>(cc::Animation::RunStateEnumSize) ==
               arraysize(s_runStateNames),
               RunState_names_match_enum);

// This should match the TargetProperty enum.
static const char* const s_targetPropertyNames[] = {
  "Transform",
  "Opacity",
  "Filter",
  "ScrollOffset",
  "BackgroundColor"
};

COMPILE_ASSERT(static_cast<int>(cc::Animation::TargetPropertyEnumSize) ==
               arraysize(s_targetPropertyNames),
               TargetProperty_names_match_enum);

}  // namespace

namespace cc {

scoped_ptr<Animation> Animation::Create(
    scoped_ptr<AnimationCurve> curve,
    int animation_id,
    int group_id,
    TargetProperty target_property) {
  return make_scoped_ptr(new Animation(curve.Pass(),
                                       animation_id,
                                       group_id,
                                       target_property)); }

Animation::Animation(scoped_ptr<AnimationCurve> curve,
                     int animation_id,
                     int group_id,
                     TargetProperty target_property)
    : curve_(curve.Pass()),
      id_(animation_id),
      group_(group_id),
      target_property_(target_property),
      run_state_(WaitingForTargetAvailability),
      iterations_(1),
      direction_(Normal),
      needs_synchronized_start_time_(false),
      received_finished_event_(false),
      suspended_(false),
      is_controlling_instance_(false),
      is_impl_only_(false),
      affects_active_observers_(true),
      affects_pending_observers_(true) {
}

Animation::~Animation() {
  if (run_state_ == Running || run_state_ == Paused)
    SetRunState(Aborted, base::TimeTicks());
}

void Animation::SetRunState(RunState run_state,
                            base::TimeTicks monotonic_time) {
  if (suspended_)
    return;

  char name_buffer[256];
  base::snprintf(name_buffer,
                 sizeof(name_buffer),
                 "%s-%d%s",
                 s_targetPropertyNames[target_property_],
                 group_,
                 is_controlling_instance_ ? "(impl)" : "");

  bool is_waiting_to_start = run_state_ == WaitingForTargetAvailability ||
                             run_state_ == Starting;

  if (is_waiting_to_start && run_state == Running) {
    TRACE_EVENT_ASYNC_BEGIN1(
        "cc", "Animation", this, "Name", TRACE_STR_COPY(name_buffer));
  }

  bool was_finished = is_finished();

  const char* old_run_state_name = s_runStateNames[run_state_];

  if (run_state == Running && run_state_ == Paused)
    total_paused_time_ += (monotonic_time - pause_time_);
  else if (run_state == Paused)
    pause_time_ = monotonic_time;
  run_state_ = run_state;

  const char* new_run_state_name = s_runStateNames[run_state];

  if (!was_finished && is_finished())
    TRACE_EVENT_ASYNC_END0("cc", "Animation", this);

  char state_buffer[256];
  base::snprintf(state_buffer,
                 sizeof(state_buffer),
                 "%s->%s",
                 old_run_state_name,
                 new_run_state_name);

  TRACE_EVENT_INSTANT2("cc",
                       "LayerAnimationController::SetRunState",
                       TRACE_EVENT_SCOPE_THREAD,
                       "Name",
                       TRACE_STR_COPY(name_buffer),
                       "State",
                       TRACE_STR_COPY(state_buffer));
}

void Animation::Suspend(base::TimeTicks monotonic_time) {
  SetRunState(Paused, monotonic_time);
  suspended_ = true;
}

void Animation::Resume(base::TimeTicks monotonic_time) {
  suspended_ = false;
  SetRunState(Running, monotonic_time);
}

bool Animation::IsFinishedAt(base::TimeTicks monotonic_time) const {
  if (is_finished())
    return true;

  if (needs_synchronized_start_time_)
    return false;

  return run_state_ == Running && iterations_ >= 0 &&
         iterations_ * curve_->Duration() <=
             (monotonic_time + time_offset_ - start_time_ - total_paused_time_)
                 .InSecondsF();
}

double Animation::TrimTimeToCurrentIteration(
    base::TimeTicks monotonic_time) const {
  base::TimeTicks trimmed = monotonic_time + time_offset_;

  // If we're paused, time is 'stuck' at the pause time.
  if (run_state_ == Paused)
    trimmed = pause_time_;

  // Returned time should always be relative to the start time and should
  // subtract all time spent paused.
  trimmed -= (start_time_ - base::TimeTicks()) + total_paused_time_;

  // If we're just starting or we're waiting on receiving a start time,
  // time is 'stuck' at the initial state.
  if ((run_state_ == Starting && !has_set_start_time()) ||
      needs_synchronized_start_time())
    trimmed = base::TimeTicks() + time_offset_;

  double trimmed_in_seconds = (trimmed - base::TimeTicks()).InSecondsF();

  // Return 0 if we are before the start of the animation
  if (trimmed_in_seconds < 0)
    return 0;

  // Always return zero if we have no iterations.
  if (!iterations_)
    return 0;

  // Don't attempt to trim if we have no duration.
  if (curve_->Duration() <= 0)
    return 0;

  // check if we are past active interval
  bool is_past_total_duration =
      (iterations_ > 0 &&
       trimmed_in_seconds >= curve_->Duration() * iterations_);

  // We need to know the current iteration if we're alternating.
  int iteration = 0;

  // If we are past the active interval, return iteration duration.
  if (is_past_total_duration) {
    iteration = iterations_ - 1;
    trimmed_in_seconds = curve_->Duration();
  } else {
    iteration = static_cast<int>(trimmed_in_seconds / curve_->Duration());
    // Calculate x where trimmed = x + n * curve_->Duration() for some positive
    // integer n.
    trimmed_in_seconds = fmod(trimmed_in_seconds, curve_->Duration());
  }

  // check if we are running the animation in reverse direction for the current
  // iteration
  bool reverse = (direction_ == Reverse) ||
                 (direction_ == Alternate && iteration % 2 == 1) ||
                 (direction_ == AlternateReverse && iteration % 2 == 0);

  // if we are running the animation in reverse direction, reverse the result
  if (reverse)
    return curve_->Duration() - trimmed_in_seconds;

  return trimmed_in_seconds;
}

scoped_ptr<Animation> Animation::CloneAndInitialize(
    RunState initial_run_state) const {
  scoped_ptr<Animation> to_return(
      new Animation(curve_->Clone(), id_, group_, target_property_));
  to_return->run_state_ = initial_run_state;
  to_return->iterations_ = iterations_;
  to_return->start_time_ = start_time_;
  to_return->pause_time_ = pause_time_;
  to_return->total_paused_time_ = total_paused_time_;
  to_return->time_offset_ = time_offset_;
  to_return->direction_ = direction_;
  DCHECK(!to_return->is_controlling_instance_);
  to_return->is_controlling_instance_ = true;
  return to_return.Pass();
}

void Animation::PushPropertiesTo(Animation* other) const {
  // Currently, we only push changes due to pausing and resuming animations on
  // the main thread.
  if (run_state_ == Animation::Paused ||
      other->run_state_ == Animation::Paused) {
    other->run_state_ = run_state_;
    other->pause_time_ = pause_time_;
    other->total_paused_time_ = total_paused_time_;
  }
}

}  // namespace cc