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