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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
|
// 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/layer_animation_controller.h"
#include <algorithm>
#include "cc/animation.h"
#include "cc/animation_registrar.h"
#include "cc/keyframed_animation_curve.h"
#include "cc/layer_animation_value_observer.h"
#include "cc/scoped_ptr_algorithm.h"
#include "ui/gfx/transform.h"
namespace cc {
LayerAnimationController::LayerAnimationController(int id)
: m_forceSync(false)
, m_id(id)
, m_registrar(0)
, m_isActive(false)
, m_lastTickTime(0)
{
}
LayerAnimationController::~LayerAnimationController()
{
if (m_registrar)
m_registrar->UnregisterAnimationController(this);
}
scoped_refptr<LayerAnimationController> LayerAnimationController::create(int id)
{
return make_scoped_refptr(new LayerAnimationController(id));
}
void LayerAnimationController::pauseAnimation(int animationId, double timeOffset)
{
for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
if (m_activeAnimations[i]->id() == animationId)
m_activeAnimations[i]->setRunState(Animation::Paused, timeOffset + m_activeAnimations[i]->startTime());
}
}
struct HasAnimationId {
HasAnimationId(int id) : m_id(id) { }
bool operator()(Animation* animation) const { return animation->id() == m_id; }
private:
int m_id;
};
void LayerAnimationController::removeAnimation(int animationId)
{
ScopedPtrVector<Animation>& animations = m_activeAnimations;
animations.erase(cc::remove_if(animations, animations.begin(), animations.end(), HasAnimationId(animationId)), animations.end());
updateActivation();
}
struct HasAnimationIdAndProperty {
HasAnimationIdAndProperty(int id, Animation::TargetProperty targetProperty) : m_id(id), m_targetProperty(targetProperty) { }
bool operator()(Animation* animation) const { return animation->id() == m_id && animation->targetProperty() == m_targetProperty; }
private:
int m_id;
Animation::TargetProperty m_targetProperty;
};
void LayerAnimationController::removeAnimation(int animationId, Animation::TargetProperty targetProperty)
{
ScopedPtrVector<Animation>& animations = m_activeAnimations;
animations.erase(cc::remove_if(animations, animations.begin(), animations.end(), HasAnimationIdAndProperty(animationId, targetProperty)), animations.end());
updateActivation();
}
// According to render layer backing, these are for testing only.
void LayerAnimationController::suspendAnimations(double monotonicTime)
{
for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
if (!m_activeAnimations[i]->isFinished())
m_activeAnimations[i]->setRunState(Animation::Paused, monotonicTime);
}
}
// Looking at GraphicsLayerCA, this appears to be the analog to suspendAnimations, which is for testing.
void LayerAnimationController::resumeAnimations(double monotonicTime)
{
for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
if (m_activeAnimations[i]->runState() == Animation::Paused)
m_activeAnimations[i]->setRunState(Animation::Running, monotonicTime);
}
}
// Ensures that the list of active animations on the main thread and the impl thread
// are kept in sync.
void LayerAnimationController::pushAnimationUpdatesTo(LayerAnimationController* controllerImpl)
{
if (m_forceSync) {
replaceImplThreadAnimations(controllerImpl);
m_forceSync = false;
} else {
purgeAnimationsMarkedForDeletion();
pushNewAnimationsToImplThread(controllerImpl);
// Remove finished impl side animations only after pushing,
// and only after the animations are deleted on the main thread
// this insures we will never push an animation twice.
removeAnimationsCompletedOnMainThread(controllerImpl);
pushPropertiesToImplThread(controllerImpl);
}
controllerImpl->updateActivation();
updateActivation();
}
void LayerAnimationController::animate(double monotonicTime)
{
if (!hasActiveObserver())
return;
startAnimationsWaitingForNextTick(monotonicTime);
startAnimationsWaitingForStartTime(monotonicTime);
startAnimationsWaitingForTargetAvailability(monotonicTime);
resolveConflicts(monotonicTime);
tickAnimations(monotonicTime);
m_lastTickTime = monotonicTime;
}
void LayerAnimationController::updateState(AnimationEventsVector* events)
{
if (!hasActiveObserver())
return;
promoteStartedAnimations(m_lastTickTime, events);
markFinishedAnimations(m_lastTickTime);
markAnimationsForDeletion(m_lastTickTime, events);
startAnimationsWaitingForTargetAvailability(m_lastTickTime);
promoteStartedAnimations(m_lastTickTime, events);
updateActivation();
}
void LayerAnimationController::addAnimation(scoped_ptr<Animation> animation)
{
m_activeAnimations.push_back(animation.Pass());
updateActivation();
}
Animation* LayerAnimationController::getAnimation(int groupId, Animation::TargetProperty targetProperty) const
{
for (size_t i = 0; i < m_activeAnimations.size(); ++i)
if (m_activeAnimations[i]->group() == groupId && m_activeAnimations[i]->targetProperty() == targetProperty)
return m_activeAnimations[i];
return 0;
}
Animation* LayerAnimationController::getAnimation(Animation::TargetProperty targetProperty) const
{
for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
size_t index = m_activeAnimations.size() - i - 1;
if (m_activeAnimations[index]->targetProperty() == targetProperty)
return m_activeAnimations[index];
}
return 0;
}
bool LayerAnimationController::hasActiveAnimation() const
{
for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
if (!m_activeAnimations[i]->isFinished())
return true;
}
return false;
}
bool LayerAnimationController::isAnimatingProperty(Animation::TargetProperty targetProperty) const
{
for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
if (m_activeAnimations[i]->runState() != Animation::Finished && m_activeAnimations[i]->runState() != Animation::Aborted && m_activeAnimations[i]->targetProperty() == targetProperty)
return true;
}
return false;
}
void LayerAnimationController::OnAnimationStarted(const AnimationEvent& event)
{
for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
if (m_activeAnimations[i]->group() == event.groupId && m_activeAnimations[i]->targetProperty() == event.targetProperty && m_activeAnimations[i]->needsSynchronizedStartTime()) {
m_activeAnimations[i]->setNeedsSynchronizedStartTime(false);
m_activeAnimations[i]->setStartTime(event.monotonicTime);
return;
}
}
}
void LayerAnimationController::setAnimationRegistrar(AnimationRegistrar* registrar)
{
if (m_registrar == registrar)
return;
if (m_registrar)
m_registrar->UnregisterAnimationController(this);
m_registrar = registrar;
if (m_registrar)
m_registrar->RegisterAnimationController(this);
bool force = true;
updateActivation(force);
}
void LayerAnimationController::addObserver(LayerAnimationValueObserver* observer)
{
if (!m_observers.HasObserver(observer))
m_observers.AddObserver(observer);
}
void LayerAnimationController::removeObserver(LayerAnimationValueObserver* observer)
{
m_observers.RemoveObserver(observer);
}
void LayerAnimationController::pushNewAnimationsToImplThread(LayerAnimationController* controllerImpl) const
{
// Any new animations owned by the main thread's controller are cloned and adde to the impl thread's controller.
for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
// If the animation is already running on the impl thread, there is no need to copy it over.
if (controllerImpl->getAnimation(m_activeAnimations[i]->group(), m_activeAnimations[i]->targetProperty()))
continue;
// If the animation is not running on the impl thread, it does not necessarily mean that it needs
// to be copied over and started; it may have already finished. In this case, the impl thread animation
// will have already notified that it has started and the main thread animation will no longer need
// a synchronized start time.
if (!m_activeAnimations[i]->needsSynchronizedStartTime())
continue;
// The new animation should be set to run as soon as possible.
Animation::RunState initialRunState = Animation::WaitingForTargetAvailability;
double startTime = 0;
scoped_ptr<Animation> toAdd(m_activeAnimations[i]->cloneAndInitialize(Animation::ControllingInstance, initialRunState, startTime));
DCHECK(!toAdd->needsSynchronizedStartTime());
controllerImpl->addAnimation(toAdd.Pass());
}
}
struct IsCompleted {
IsCompleted(const LayerAnimationController& mainThreadController) : m_mainThreadController(mainThreadController) { }
bool operator()(Animation* animation) const { return !m_mainThreadController.getAnimation(animation->group(), animation->targetProperty()); }
private:
const LayerAnimationController& m_mainThreadController;
};
void LayerAnimationController::removeAnimationsCompletedOnMainThread(LayerAnimationController* controllerImpl) const
{
// Delete all impl thread animations for which there is no corresponding main thread animation.
// Each iteration, controller->m_activeAnimations.size() is decremented or i is incremented
// guaranteeing progress towards loop termination.
ScopedPtrVector<Animation>& animations = controllerImpl->m_activeAnimations;
animations.erase(cc::remove_if(animations, animations.begin(), animations.end(), IsCompleted(*this)), animations.end());
}
void LayerAnimationController::pushPropertiesToImplThread(LayerAnimationController* controllerImpl) const
{
for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
Animation* currentImpl = controllerImpl->getAnimation(m_activeAnimations[i]->group(), m_activeAnimations[i]->targetProperty());
if (currentImpl)
m_activeAnimations[i]->pushPropertiesTo(currentImpl);
}
}
void LayerAnimationController::startAnimationsWaitingForNextTick(double monotonicTime)
{
for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
if (m_activeAnimations[i]->runState() == Animation::WaitingForNextTick)
m_activeAnimations[i]->setRunState(Animation::Starting, monotonicTime);
}
}
void LayerAnimationController::startAnimationsWaitingForStartTime(double monotonicTime)
{
for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
if (m_activeAnimations[i]->runState() == Animation::WaitingForStartTime && m_activeAnimations[i]->startTime() <= monotonicTime)
m_activeAnimations[i]->setRunState(Animation::Starting, monotonicTime);
}
}
void LayerAnimationController::startAnimationsWaitingForTargetAvailability(double monotonicTime)
{
// First collect running properties.
TargetProperties blockedProperties;
for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
if (m_activeAnimations[i]->runState() == Animation::Starting || m_activeAnimations[i]->runState() == Animation::Running || m_activeAnimations[i]->runState() == Animation::Finished)
blockedProperties.insert(m_activeAnimations[i]->targetProperty());
}
for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
if (m_activeAnimations[i]->runState() == Animation::WaitingForTargetAvailability) {
// Collect all properties for animations with the same group id (they should all also be in the list of animations).
TargetProperties enqueuedProperties;
enqueuedProperties.insert(m_activeAnimations[i]->targetProperty());
for (size_t j = i + 1; j < m_activeAnimations.size(); ++j) {
if (m_activeAnimations[i]->group() == m_activeAnimations[j]->group())
enqueuedProperties.insert(m_activeAnimations[j]->targetProperty());
}
// Check to see if intersection of the list of properties affected by the group and the list of currently
// blocked properties is null. In any case, the group's target properties need to be added to the list
// of blocked properties.
bool nullIntersection = true;
for (TargetProperties::iterator pIter = enqueuedProperties.begin(); pIter != enqueuedProperties.end(); ++pIter) {
if (!blockedProperties.insert(*pIter).second)
nullIntersection = false;
}
// If the intersection is null, then we are free to start the animations in the group.
if (nullIntersection) {
m_activeAnimations[i]->setRunState(Animation::Starting, monotonicTime);
for (size_t j = i + 1; j < m_activeAnimations.size(); ++j) {
if (m_activeAnimations[i]->group() == m_activeAnimations[j]->group())
m_activeAnimations[j]->setRunState(Animation::Starting, monotonicTime);
}
}
}
}
}
void LayerAnimationController::promoteStartedAnimations(double monotonicTime, AnimationEventsVector* events)
{
for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
if (m_activeAnimations[i]->runState() == Animation::Starting) {
m_activeAnimations[i]->setRunState(Animation::Running, monotonicTime);
if (!m_activeAnimations[i]->hasSetStartTime())
m_activeAnimations[i]->setStartTime(monotonicTime);
if (events)
events->push_back(AnimationEvent(AnimationEvent::Started, m_id, m_activeAnimations[i]->group(), m_activeAnimations[i]->targetProperty(), monotonicTime));
}
}
}
void LayerAnimationController::markFinishedAnimations(double monotonicTime)
{
for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
if (m_activeAnimations[i]->isFinishedAt(monotonicTime))
m_activeAnimations[i]->setRunState(Animation::Finished, monotonicTime);
}
}
void LayerAnimationController::resolveConflicts(double monotonicTime)
{
// Find any animations that are animating the same property and resolve the
// confict. We could eventually blend, but for now we'll just abort the
// previous animation (where 'previous' means: (1) has a prior start time or
// (2) has an equal start time, but was added to the queue earlier, i.e.,
// has a lower index in m_activeAnimations).
for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
if (m_activeAnimations[i]->runState() == Animation::Starting || m_activeAnimations[i]->runState() == Animation::Running) {
for (size_t j = i + 1; j < m_activeAnimations.size(); ++j) {
if ((m_activeAnimations[j]->runState() == Animation::Starting ||
m_activeAnimations[j]->runState() == Animation::Running) &&
m_activeAnimations[i]->targetProperty() == m_activeAnimations[j]->targetProperty()) {
if (m_activeAnimations[i]->startTime() > m_activeAnimations[j]->startTime())
m_activeAnimations[j]->setRunState(Animation::Aborted, monotonicTime);
else
m_activeAnimations[i]->setRunState(Animation::Aborted, monotonicTime);
}
}
}
}
}
void LayerAnimationController::markAnimationsForDeletion(double monotonicTime, AnimationEventsVector* events)
{
for (size_t i = 0; i < m_activeAnimations.size(); i++) {
int groupId = m_activeAnimations[i]->group();
bool allAnimsWithSameIdAreFinished = false;
// If an animation is finished, and not already marked for deletion,
// Find out if all other animations in the same group are also finished.
if (m_activeAnimations[i]->isFinished() && m_activeAnimations[i]->runState() != Animation::WaitingForDeletion) {
allAnimsWithSameIdAreFinished = true;
for (size_t j = 0; j < m_activeAnimations.size(); ++j) {
if (groupId == m_activeAnimations[j]->group() && !m_activeAnimations[j]->isFinished()) {
allAnimsWithSameIdAreFinished = false;
break;
}
}
}
if (allAnimsWithSameIdAreFinished) {
// We now need to remove all animations with the same group id as groupId
// (and send along animation finished notifications, if necessary).
for (size_t j = i; j < m_activeAnimations.size(); j++) {
if (groupId == m_activeAnimations[j]->group()) {
if (events)
events->push_back(AnimationEvent(AnimationEvent::Finished, m_id, m_activeAnimations[j]->group(), m_activeAnimations[j]->targetProperty(), monotonicTime));
m_activeAnimations[j]->setRunState(Animation::WaitingForDeletion, monotonicTime);
}
}
}
}
}
static bool isWaitingForDeletion(Animation* animation) { return animation->runState() == Animation::WaitingForDeletion; }
void LayerAnimationController::purgeAnimationsMarkedForDeletion()
{
ScopedPtrVector<Animation>& animations = m_activeAnimations;
animations.erase(cc::remove_if(animations, animations.begin(), animations.end(), isWaitingForDeletion), animations.end());
}
void LayerAnimationController::replaceImplThreadAnimations(LayerAnimationController* controllerImpl) const
{
controllerImpl->m_activeAnimations.clear();
for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
scoped_ptr<Animation> toAdd;
if (m_activeAnimations[i]->needsSynchronizedStartTime()) {
// We haven't received an animation started notification yet, so it
// is important that we add it in a 'waiting' and not 'running' state.
Animation::RunState initialRunState = Animation::WaitingForTargetAvailability;
double startTime = 0;
toAdd = m_activeAnimations[i]->cloneAndInitialize(Animation::ControllingInstance, initialRunState, startTime).Pass();
} else
toAdd = m_activeAnimations[i]->clone(Animation::ControllingInstance).Pass();
controllerImpl->addAnimation(toAdd.Pass());
}
}
void LayerAnimationController::tickAnimations(double monotonicTime)
{
for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
if (m_activeAnimations[i]->runState() == Animation::Starting ||
m_activeAnimations[i]->runState() == Animation::Running ||
m_activeAnimations[i]->runState() == Animation::Paused) {
double trimmed = m_activeAnimations[i]->trimTimeToCurrentIteration(monotonicTime);
// Animation assumes its initial value until it gets the synchronized start time
// from the impl thread and can start ticking.
if (m_activeAnimations[i]->needsSynchronizedStartTime())
trimmed = 0;
// A just-started animation assumes its initial value.
if (m_activeAnimations[i]->runState() == Animation::Starting && !m_activeAnimations[i]->hasSetStartTime())
trimmed = 0;
switch (m_activeAnimations[i]->targetProperty()) {
case Animation::Transform: {
const TransformAnimationCurve* transformAnimationCurve = m_activeAnimations[i]->curve()->toTransformAnimationCurve();
const gfx::Transform transform = transformAnimationCurve->getValue(trimmed);
notifyObserversTransformAnimated(transform);
break;
}
case Animation::Opacity: {
const FloatAnimationCurve* floatAnimationCurve = m_activeAnimations[i]->curve()->toFloatAnimationCurve();
const float opacity = floatAnimationCurve->getValue(trimmed);
notifyObserversOpacityAnimated(opacity);
break;
}
// Do nothing for sentinel value.
case Animation::TargetPropertyEnumSize:
NOTREACHED();
}
}
}
}
void LayerAnimationController::updateActivation(bool force)
{
if (m_registrar) {
if (!m_activeAnimations.empty() && (!m_isActive || force))
m_registrar->DidActivateAnimationController(this);
else if (m_activeAnimations.empty() && (m_isActive || force))
m_registrar->DidDeactivateAnimationController(this);
m_isActive = !m_activeAnimations.empty();
}
}
void LayerAnimationController::notifyObserversOpacityAnimated(float opacity)
{
FOR_EACH_OBSERVER(LayerAnimationValueObserver,
m_observers,
OnOpacityAnimated(opacity));
}
void LayerAnimationController::notifyObserversTransformAnimated(const gfx::Transform& transform)
{
FOR_EACH_OBSERVER(LayerAnimationValueObserver,
m_observers,
OnTransformAnimated(transform));
}
bool LayerAnimationController::hasActiveObserver()
{
if (m_observers.might_have_observers()) {
ObserverListBase<LayerAnimationValueObserver>::Iterator it(m_observers);
LayerAnimationValueObserver* obs;
while ((obs = it.GetNext()) != NULL)
if (obs->IsActive())
return true;
}
return false;
}
} // namespace cc
|