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