[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 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 | |
[email protected] | d50c686 | 2012-10-23 02:08:31 | [diff] [blame] | 5 | #include "cc/layer_animation_controller.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 6 | |
[email protected] | aa0a9d3 | 2012-10-24 01:58:10 | [diff] [blame] | 7 | #include "cc/active_animation.h" |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 8 | #include "cc/animation_registrar.h" |
[email protected] | d50c686 | 2012-10-23 02:08:31 | [diff] [blame] | 9 | #include "cc/keyframed_animation_curve.h" |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 10 | #include "cc/layer_animation_value_observer.h" |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 11 | #include "ui/gfx/transform.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 12 | |
[email protected] | 9c88e56 | 2012-09-14 22:21:30 | [diff] [blame] | 13 | namespace cc { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 14 | |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 15 | LayerAnimationController::LayerAnimationController(int id) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 16 | : m_forceSync(false) |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 17 | , m_id(id) |
| 18 | , m_registrar(0) |
| 19 | , m_isActive(false) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 20 | { |
| 21 | } |
| 22 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 23 | LayerAnimationController::~LayerAnimationController() |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 24 | { |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 25 | if (m_registrar) |
| 26 | m_registrar->UnregisterAnimationController(this); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 27 | } |
| 28 | |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 29 | scoped_refptr<LayerAnimationController> LayerAnimationController::create(int id) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 30 | { |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 31 | return make_scoped_refptr(new LayerAnimationController(id)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 32 | } |
| 33 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 34 | void LayerAnimationController::pauseAnimation(int animationId, double timeOffset) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 35 | { |
| 36 | for (size_t i = 0; i < m_activeAnimations.size(); ++i) { |
| 37 | if (m_activeAnimations[i]->id() == animationId) |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 38 | m_activeAnimations[i]->setRunState(ActiveAnimation::Paused, timeOffset + m_activeAnimations[i]->startTime()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 42 | void LayerAnimationController::removeAnimation(int animationId) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 43 | { |
| 44 | for (size_t i = 0; i < m_activeAnimations.size();) { |
| 45 | if (m_activeAnimations[i]->id() == animationId) |
| 46 | m_activeAnimations.remove(i); |
| 47 | else |
| 48 | i++; |
| 49 | } |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 50 | updateActivation(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 51 | } |
| 52 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 53 | void LayerAnimationController::removeAnimation(int animationId, ActiveAnimation::TargetProperty targetProperty) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 54 | { |
| 55 | for (size_t i = 0; i < m_activeAnimations.size();) { |
| 56 | if (m_activeAnimations[i]->id() == animationId && m_activeAnimations[i]->targetProperty() == targetProperty) |
| 57 | m_activeAnimations.remove(i); |
| 58 | else |
| 59 | i++; |
| 60 | } |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 61 | updateActivation(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // According to render layer backing, these are for testing only. |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 65 | void LayerAnimationController::suspendAnimations(double monotonicTime) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 66 | { |
| 67 | for (size_t i = 0; i < m_activeAnimations.size(); ++i) { |
| 68 | if (!m_activeAnimations[i]->isFinished()) |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 69 | m_activeAnimations[i]->setRunState(ActiveAnimation::Paused, monotonicTime); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | // Looking at GraphicsLayerCA, this appears to be the analog to suspendAnimations, which is for testing. |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 74 | void LayerAnimationController::resumeAnimations(double monotonicTime) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 75 | { |
| 76 | for (size_t i = 0; i < m_activeAnimations.size(); ++i) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 77 | if (m_activeAnimations[i]->runState() == ActiveAnimation::Paused) |
| 78 | m_activeAnimations[i]->setRunState(ActiveAnimation::Running, monotonicTime); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | // Ensures that the list of active animations on the main thread and the impl thread |
| 83 | // are kept in sync. |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 84 | void LayerAnimationController::pushAnimationUpdatesTo(LayerAnimationController* controllerImpl) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 85 | { |
| 86 | if (m_forceSync) { |
| 87 | replaceImplThreadAnimations(controllerImpl); |
| 88 | m_forceSync = false; |
| 89 | } else { |
| 90 | purgeAnimationsMarkedForDeletion(); |
| 91 | pushNewAnimationsToImplThread(controllerImpl); |
| 92 | |
| 93 | // Remove finished impl side animations only after pushing, |
| 94 | // and only after the animations are deleted on the main thread |
| 95 | // this insures we will never push an animation twice. |
| 96 | removeAnimationsCompletedOnMainThread(controllerImpl); |
| 97 | |
| 98 | pushPropertiesToImplThread(controllerImpl); |
| 99 | } |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 100 | controllerImpl->updateActivation(); |
| 101 | updateActivation(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 102 | } |
| 103 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 104 | void LayerAnimationController::animate(double monotonicTime, AnimationEventsVector* events) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 105 | { |
| 106 | startAnimationsWaitingForNextTick(monotonicTime, events); |
| 107 | startAnimationsWaitingForStartTime(monotonicTime, events); |
| 108 | startAnimationsWaitingForTargetAvailability(monotonicTime, events); |
| 109 | resolveConflicts(monotonicTime); |
| 110 | tickAnimations(monotonicTime); |
| 111 | markAnimationsForDeletion(monotonicTime, events); |
| 112 | startAnimationsWaitingForTargetAvailability(monotonicTime, events); |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 113 | |
| 114 | updateActivation(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 115 | } |
| 116 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 117 | void LayerAnimationController::addAnimation(scoped_ptr<ActiveAnimation> animation) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 118 | { |
[email protected] | 9aca359 | 2012-10-12 15:57:09 | [diff] [blame] | 119 | m_activeAnimations.append(animation.Pass()); |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 120 | updateActivation(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 123 | ActiveAnimation* LayerAnimationController::getActiveAnimation(int groupId, ActiveAnimation::TargetProperty targetProperty) const |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 124 | { |
| 125 | for (size_t i = 0; i < m_activeAnimations.size(); ++i) |
| 126 | if (m_activeAnimations[i]->group() == groupId && m_activeAnimations[i]->targetProperty() == targetProperty) |
[email protected] | 0920e24f | 2012-09-20 03:34:03 | [diff] [blame] | 127 | return m_activeAnimations[i]; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 131 | ActiveAnimation* LayerAnimationController::getActiveAnimation(ActiveAnimation::TargetProperty targetProperty) const |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 132 | { |
| 133 | for (size_t i = 0; i < m_activeAnimations.size(); ++i) { |
| 134 | size_t index = m_activeAnimations.size() - i - 1; |
| 135 | if (m_activeAnimations[index]->targetProperty() == targetProperty) |
[email protected] | 0920e24f | 2012-09-20 03:34:03 | [diff] [blame] | 136 | return m_activeAnimations[index]; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 137 | } |
| 138 | return 0; |
| 139 | } |
| 140 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 141 | bool LayerAnimationController::hasActiveAnimation() const |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 142 | { |
| 143 | for (size_t i = 0; i < m_activeAnimations.size(); ++i) { |
| 144 | if (!m_activeAnimations[i]->isFinished()) |
| 145 | return true; |
| 146 | } |
| 147 | return false; |
| 148 | } |
| 149 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 150 | bool LayerAnimationController::isAnimatingProperty(ActiveAnimation::TargetProperty targetProperty) const |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 151 | { |
| 152 | for (size_t i = 0; i < m_activeAnimations.size(); ++i) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 153 | if (m_activeAnimations[i]->runState() != ActiveAnimation::Finished && m_activeAnimations[i]->runState() != ActiveAnimation::Aborted && m_activeAnimations[i]->targetProperty() == targetProperty) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 154 | return true; |
| 155 | } |
| 156 | return false; |
| 157 | } |
| 158 | |
[email protected] | e10cd02 | 2012-12-18 00:32:26 | [diff] [blame] | 159 | void LayerAnimationController::OnAnimationStarted(const AnimationEvent& event) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 160 | { |
| 161 | for (size_t i = 0; i < m_activeAnimations.size(); ++i) { |
| 162 | if (m_activeAnimations[i]->group() == event.groupId && m_activeAnimations[i]->targetProperty() == event.targetProperty && m_activeAnimations[i]->needsSynchronizedStartTime()) { |
| 163 | m_activeAnimations[i]->setNeedsSynchronizedStartTime(false); |
| 164 | m_activeAnimations[i]->setStartTime(event.monotonicTime); |
| 165 | return; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 170 | void LayerAnimationController::setAnimationRegistrar(AnimationRegistrar* registrar) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 171 | { |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 172 | if (m_registrar == registrar) |
| 173 | return; |
| 174 | |
| 175 | if (m_registrar) |
| 176 | m_registrar->UnregisterAnimationController(this); |
| 177 | |
| 178 | m_registrar = registrar; |
| 179 | if (m_registrar) |
| 180 | m_registrar->RegisterAnimationController(this); |
| 181 | |
| 182 | bool force = true; |
| 183 | updateActivation(force); |
| 184 | } |
| 185 | |
| 186 | void LayerAnimationController::addObserver(LayerAnimationValueObserver* observer) |
| 187 | { |
| 188 | if (!m_observers.HasObserver(observer)) |
| 189 | m_observers.AddObserver(observer); |
| 190 | } |
| 191 | |
| 192 | void LayerAnimationController::removeObserver(LayerAnimationValueObserver* observer) |
| 193 | { |
| 194 | m_observers.RemoveObserver(observer); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 195 | } |
| 196 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 197 | void LayerAnimationController::pushNewAnimationsToImplThread(LayerAnimationController* controllerImpl) const |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 198 | { |
| 199 | // Any new animations owned by the main thread's controller are cloned and adde to the impl thread's controller. |
| 200 | for (size_t i = 0; i < m_activeAnimations.size(); ++i) { |
| 201 | // If the animation is already running on the impl thread, there is no need to copy it over. |
| 202 | if (controllerImpl->getActiveAnimation(m_activeAnimations[i]->group(), m_activeAnimations[i]->targetProperty())) |
| 203 | continue; |
| 204 | |
| 205 | // If the animation is not running on the impl thread, it does not necessarily mean that it needs |
| 206 | // to be copied over and started; it may have already finished. In this case, the impl thread animation |
| 207 | // will have already notified that it has started and the main thread animation will no longer need |
| 208 | // a synchronized start time. |
| 209 | if (!m_activeAnimations[i]->needsSynchronizedStartTime()) |
| 210 | continue; |
| 211 | |
| 212 | // The new animation should be set to run as soon as possible. |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 213 | ActiveAnimation::RunState initialRunState = ActiveAnimation::WaitingForTargetAvailability; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 214 | double startTime = 0; |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 215 | scoped_ptr<ActiveAnimation> toAdd(m_activeAnimations[i]->cloneAndInitialize(ActiveAnimation::ControllingInstance, initialRunState, startTime)); |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 216 | DCHECK(!toAdd->needsSynchronizedStartTime()); |
[email protected] | 9aca359 | 2012-10-12 15:57:09 | [diff] [blame] | 217 | controllerImpl->addAnimation(toAdd.Pass()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 221 | void LayerAnimationController::removeAnimationsCompletedOnMainThread(LayerAnimationController* controllerImpl) const |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 222 | { |
| 223 | // Delete all impl thread animations for which there is no corresponding main thread animation. |
| 224 | // Each iteration, controller->m_activeAnimations.size() is decremented or i is incremented |
| 225 | // guaranteeing progress towards loop termination. |
| 226 | for (size_t i = 0; i < controllerImpl->m_activeAnimations.size();) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 227 | ActiveAnimation* current = getActiveAnimation(controllerImpl->m_activeAnimations[i]->group(), controllerImpl->m_activeAnimations[i]->targetProperty()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 228 | if (!current) |
| 229 | controllerImpl->m_activeAnimations.remove(i); |
| 230 | else |
| 231 | i++; |
| 232 | } |
| 233 | } |
| 234 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 235 | void LayerAnimationController::pushPropertiesToImplThread(LayerAnimationController* controllerImpl) const |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 236 | { |
| 237 | for (size_t i = 0; i < m_activeAnimations.size(); ++i) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 238 | ActiveAnimation* currentImpl = controllerImpl->getActiveAnimation(m_activeAnimations[i]->group(), m_activeAnimations[i]->targetProperty()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 239 | if (currentImpl) |
| 240 | m_activeAnimations[i]->pushPropertiesTo(currentImpl); |
| 241 | } |
| 242 | } |
| 243 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 244 | void LayerAnimationController::startAnimationsWaitingForNextTick(double monotonicTime, AnimationEventsVector* events) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 245 | { |
| 246 | for (size_t i = 0; i < m_activeAnimations.size(); ++i) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 247 | if (m_activeAnimations[i]->runState() == ActiveAnimation::WaitingForNextTick) { |
| 248 | m_activeAnimations[i]->setRunState(ActiveAnimation::Running, monotonicTime); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 249 | if (!m_activeAnimations[i]->hasSetStartTime()) |
| 250 | m_activeAnimations[i]->setStartTime(monotonicTime); |
| 251 | if (events) |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 252 | events->push_back(AnimationEvent(AnimationEvent::Started, m_id, m_activeAnimations[i]->group(), m_activeAnimations[i]->targetProperty(), monotonicTime)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 257 | void LayerAnimationController::startAnimationsWaitingForStartTime(double monotonicTime, AnimationEventsVector* events) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 258 | { |
| 259 | for (size_t i = 0; i < m_activeAnimations.size(); ++i) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 260 | if (m_activeAnimations[i]->runState() == ActiveAnimation::WaitingForStartTime && m_activeAnimations[i]->startTime() <= monotonicTime) { |
| 261 | m_activeAnimations[i]->setRunState(ActiveAnimation::Running, monotonicTime); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 262 | if (events) |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 263 | events->push_back(AnimationEvent(AnimationEvent::Started, m_id, m_activeAnimations[i]->group(), m_activeAnimations[i]->targetProperty(), monotonicTime)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 268 | void LayerAnimationController::startAnimationsWaitingForTargetAvailability(double monotonicTime, AnimationEventsVector* events) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 269 | { |
| 270 | // First collect running properties. |
| 271 | TargetProperties blockedProperties; |
| 272 | for (size_t i = 0; i < m_activeAnimations.size(); ++i) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 273 | if (m_activeAnimations[i]->runState() == ActiveAnimation::Running || m_activeAnimations[i]->runState() == ActiveAnimation::Finished) |
[email protected] | c7278e5b | 2012-10-11 02:35:46 | [diff] [blame] | 274 | blockedProperties.insert(m_activeAnimations[i]->targetProperty()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | for (size_t i = 0; i < m_activeAnimations.size(); ++i) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 278 | if (m_activeAnimations[i]->runState() == ActiveAnimation::WaitingForTargetAvailability) { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 279 | // Collect all properties for animations with the same group id (they should all also be in the list of animations). |
| 280 | TargetProperties enqueuedProperties; |
[email protected] | c7278e5b | 2012-10-11 02:35:46 | [diff] [blame] | 281 | enqueuedProperties.insert(m_activeAnimations[i]->targetProperty()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 282 | for (size_t j = i + 1; j < m_activeAnimations.size(); ++j) { |
| 283 | if (m_activeAnimations[i]->group() == m_activeAnimations[j]->group()) |
[email protected] | c7278e5b | 2012-10-11 02:35:46 | [diff] [blame] | 284 | enqueuedProperties.insert(m_activeAnimations[j]->targetProperty()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | // Check to see if intersection of the list of properties affected by the group and the list of currently |
| 288 | // blocked properties is null. In any case, the group's target properties need to be added to the list |
| 289 | // of blocked properties. |
| 290 | bool nullIntersection = true; |
| 291 | for (TargetProperties::iterator pIter = enqueuedProperties.begin(); pIter != enqueuedProperties.end(); ++pIter) { |
[email protected] | c7278e5b | 2012-10-11 02:35:46 | [diff] [blame] | 292 | if (!blockedProperties.insert(*pIter).second) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 293 | nullIntersection = false; |
| 294 | } |
| 295 | |
| 296 | // If the intersection is null, then we are free to start the animations in the group. |
| 297 | if (nullIntersection) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 298 | m_activeAnimations[i]->setRunState(ActiveAnimation::Running, monotonicTime); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 299 | if (!m_activeAnimations[i]->hasSetStartTime()) |
| 300 | m_activeAnimations[i]->setStartTime(monotonicTime); |
| 301 | if (events) |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 302 | events->push_back(AnimationEvent(AnimationEvent::Started, m_id, m_activeAnimations[i]->group(), m_activeAnimations[i]->targetProperty(), monotonicTime)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 303 | for (size_t j = i + 1; j < m_activeAnimations.size(); ++j) { |
| 304 | if (m_activeAnimations[i]->group() == m_activeAnimations[j]->group()) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 305 | m_activeAnimations[j]->setRunState(ActiveAnimation::Running, monotonicTime); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 306 | if (!m_activeAnimations[j]->hasSetStartTime()) |
| 307 | m_activeAnimations[j]->setStartTime(monotonicTime); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 315 | void LayerAnimationController::resolveConflicts(double monotonicTime) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 316 | { |
| 317 | // Find any animations that are animating the same property and resolve the |
| 318 | // confict. We could eventually blend, but for now we'll just abort the |
| 319 | // previous animation (where 'previous' means: (1) has a prior start time or |
| 320 | // (2) has an equal start time, but was added to the queue earlier, i.e., |
| 321 | // has a lower index in m_activeAnimations). |
| 322 | for (size_t i = 0; i < m_activeAnimations.size(); ++i) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 323 | if (m_activeAnimations[i]->runState() == ActiveAnimation::Running) { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 324 | for (size_t j = i + 1; j < m_activeAnimations.size(); ++j) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 325 | if (m_activeAnimations[j]->runState() == ActiveAnimation::Running && m_activeAnimations[i]->targetProperty() == m_activeAnimations[j]->targetProperty()) { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 326 | if (m_activeAnimations[i]->startTime() > m_activeAnimations[j]->startTime()) |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 327 | m_activeAnimations[j]->setRunState(ActiveAnimation::Aborted, monotonicTime); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 328 | else |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 329 | m_activeAnimations[i]->setRunState(ActiveAnimation::Aborted, monotonicTime); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 336 | void LayerAnimationController::markAnimationsForDeletion(double monotonicTime, AnimationEventsVector* events) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 337 | { |
| 338 | for (size_t i = 0; i < m_activeAnimations.size(); i++) { |
| 339 | int groupId = m_activeAnimations[i]->group(); |
| 340 | bool allAnimsWithSameIdAreFinished = false; |
| 341 | // If an animation is finished, and not already marked for deletion, |
| 342 | // Find out if all other animations in the same group are also finished. |
| 343 | if (m_activeAnimations[i]->isFinished()) { |
| 344 | allAnimsWithSameIdAreFinished = true; |
| 345 | for (size_t j = 0; j < m_activeAnimations.size(); ++j) { |
| 346 | if (groupId == m_activeAnimations[j]->group() && !m_activeAnimations[j]->isFinished()) { |
| 347 | allAnimsWithSameIdAreFinished = false; |
| 348 | break; |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | if (allAnimsWithSameIdAreFinished) { |
| 353 | // We now need to remove all animations with the same group id as groupId |
| 354 | // (and send along animation finished notifications, if necessary). |
| 355 | for (size_t j = i; j < m_activeAnimations.size(); j++) { |
| 356 | if (groupId == m_activeAnimations[j]->group()) { |
| 357 | if (events) |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 358 | events->push_back(AnimationEvent(AnimationEvent::Finished, m_id, m_activeAnimations[j]->group(), m_activeAnimations[j]->targetProperty(), monotonicTime)); |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 359 | m_activeAnimations[j]->setRunState(ActiveAnimation::WaitingForDeletion, monotonicTime); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 366 | void LayerAnimationController::purgeAnimationsMarkedForDeletion() |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 367 | { |
| 368 | for (size_t i = 0; i < m_activeAnimations.size();) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 369 | if (m_activeAnimations[i]->runState() == ActiveAnimation::WaitingForDeletion) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 370 | m_activeAnimations.remove(i); |
| 371 | else |
| 372 | i++; |
| 373 | } |
| 374 | } |
| 375 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 376 | void LayerAnimationController::replaceImplThreadAnimations(LayerAnimationController* controllerImpl) const |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 377 | { |
| 378 | controllerImpl->m_activeAnimations.clear(); |
| 379 | for (size_t i = 0; i < m_activeAnimations.size(); ++i) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 380 | scoped_ptr<ActiveAnimation> toAdd; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 381 | if (m_activeAnimations[i]->needsSynchronizedStartTime()) { |
| 382 | // We haven't received an animation started notification yet, so it |
| 383 | // is important that we add it in a 'waiting' and not 'running' state. |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 384 | ActiveAnimation::RunState initialRunState = ActiveAnimation::WaitingForTargetAvailability; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 385 | double startTime = 0; |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 386 | toAdd = m_activeAnimations[i]->cloneAndInitialize(ActiveAnimation::ControllingInstance, initialRunState, startTime).Pass(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 387 | } else |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 388 | toAdd = m_activeAnimations[i]->clone(ActiveAnimation::ControllingInstance).Pass(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 389 | |
[email protected] | 9aca359 | 2012-10-12 15:57:09 | [diff] [blame] | 390 | controllerImpl->addAnimation(toAdd.Pass()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 394 | void LayerAnimationController::tickAnimations(double monotonicTime) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 395 | { |
[email protected] | df1ec1a | 2012-12-08 17:01:18 | [diff] [blame] | 396 | for (size_t i = 0; i < m_activeAnimations.size(); ++i) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 397 | if (m_activeAnimations[i]->runState() == ActiveAnimation::Running || m_activeAnimations[i]->runState() == ActiveAnimation::Paused) { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 398 | double trimmed = m_activeAnimations[i]->trimTimeToCurrentIteration(monotonicTime); |
| 399 | |
| 400 | // Animation assumes its initial value until it gets the synchronized start time |
| 401 | // from the impl thread and can start ticking. |
| 402 | if (m_activeAnimations[i]->needsSynchronizedStartTime()) |
| 403 | trimmed = 0; |
| 404 | |
| 405 | switch (m_activeAnimations[i]->targetProperty()) { |
| 406 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 407 | case ActiveAnimation::Transform: { |
| 408 | const TransformAnimationCurve* transformAnimationCurve = m_activeAnimations[i]->curve()->toTransformAnimationCurve(); |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 409 | const gfx::Transform transform = transformAnimationCurve->getValue(trimmed).toTransform(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 410 | if (m_activeAnimations[i]->isFinishedAt(monotonicTime)) |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 411 | m_activeAnimations[i]->setRunState(ActiveAnimation::Finished, monotonicTime); |
[email protected] | df1ec1a | 2012-12-08 17:01:18 | [diff] [blame] | 412 | |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 413 | notifyObserversTransformAnimated(transform); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 414 | break; |
| 415 | } |
| 416 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 417 | case ActiveAnimation::Opacity: { |
| 418 | const FloatAnimationCurve* floatAnimationCurve = m_activeAnimations[i]->curve()->toFloatAnimationCurve(); |
[email protected] | df1ec1a | 2012-12-08 17:01:18 | [diff] [blame] | 419 | const float opacity = floatAnimationCurve->getValue(trimmed); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 420 | if (m_activeAnimations[i]->isFinishedAt(monotonicTime)) |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 421 | m_activeAnimations[i]->setRunState(ActiveAnimation::Finished, monotonicTime); |
[email protected] | df1ec1a | 2012-12-08 17:01:18 | [diff] [blame] | 422 | |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 423 | notifyObserversOpacityAnimated(opacity); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 424 | break; |
| 425 | } |
| 426 | |
| 427 | // Do nothing for sentinel value. |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 428 | case ActiveAnimation::TargetPropertyEnumSize: |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 429 | NOTREACHED(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame^] | 435 | void LayerAnimationController::updateActivation(bool force) |
| 436 | { |
| 437 | if (m_registrar) { |
| 438 | if (!m_activeAnimations.isEmpty() && (!m_isActive || force)) |
| 439 | m_registrar->DidActivateAnimationController(this); |
| 440 | else if (m_activeAnimations.isEmpty() && (m_isActive || force)) |
| 441 | m_registrar->DidDeactivateAnimationController(this); |
| 442 | m_isActive = !m_activeAnimations.isEmpty(); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | void LayerAnimationController::notifyObserversOpacityAnimated(float opacity) |
| 447 | { |
| 448 | FOR_EACH_OBSERVER(LayerAnimationValueObserver, |
| 449 | m_observers, |
| 450 | OnOpacityAnimated(opacity)); |
| 451 | } |
| 452 | |
| 453 | void LayerAnimationController::notifyObserversTransformAnimated(const gfx::Transform& transform) |
| 454 | { |
| 455 | FOR_EACH_OBSERVER(LayerAnimationValueObserver, |
| 456 | m_observers, |
| 457 | OnTransformAnimated(transform)); |
| 458 | } |
| 459 | |
[email protected] | d3143c73 | 2012-10-05 19:17:59 | [diff] [blame] | 460 | } // namespace cc |