Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Side by Side Diff: cc/layer_animation_controller.cc

Issue 11598005: Ref count layer animation controllers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "cc/layer_animation_controller.h" 5 #include "cc/layer_animation_controller.h"
6 6
7 #include "cc/active_animation.h" 7 #include "cc/active_animation.h"
8 #include "cc/animation_registrar.h"
8 #include "cc/keyframed_animation_curve.h" 9 #include "cc/keyframed_animation_curve.h"
9 #include "ui/gfx/transform.h" 10 #include "ui/gfx/transform.h"
10 11
11 namespace cc { 12 namespace cc {
12 13
13 LayerAnimationController::LayerAnimationController(LayerAnimationControllerClien t* client) 14 LayerAnimationController::LayerAnimationController(int id)
14 : m_forceSync(false) 15 : m_forceSync(false)
15 , m_client(client) 16 , m_id(id)
17 , m_registrar(0)
18 , m_isActive(false)
16 { 19 {
17 } 20 }
18 21
19 LayerAnimationController::~LayerAnimationController() 22 LayerAnimationController::~LayerAnimationController()
20 { 23 {
24 if (m_registrar)
25 m_registrar->UnregisterAnimationController(this);
21 } 26 }
22 27
23 scoped_ptr<LayerAnimationController> LayerAnimationController::create(LayerAnima tionControllerClient* client) 28 scoped_refptr<LayerAnimationController> LayerAnimationController::create(int id)
24 { 29 {
25 return make_scoped_ptr(new LayerAnimationController(client)); 30 return make_scoped_refptr(new LayerAnimationController(id));
26 } 31 }
27 32
28 void LayerAnimationController::pauseAnimation(int animationId, double timeOffset ) 33 void LayerAnimationController::pauseAnimation(int animationId, double timeOffset )
29 { 34 {
30 for (size_t i = 0; i < m_activeAnimations.size(); ++i) { 35 for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
31 if (m_activeAnimations[i]->id() == animationId) 36 if (m_activeAnimations[i]->id() == animationId)
32 m_activeAnimations[i]->setRunState(ActiveAnimation::Paused, timeOffs et + m_activeAnimations[i]->startTime()); 37 m_activeAnimations[i]->setRunState(ActiveAnimation::Paused, timeOffs et + m_activeAnimations[i]->startTime());
33 } 38 }
34 } 39 }
35 40
36 void LayerAnimationController::removeAnimation(int animationId) 41 void LayerAnimationController::removeAnimation(int animationId)
37 { 42 {
38 for (size_t i = 0; i < m_activeAnimations.size();) { 43 for (size_t i = 0; i < m_activeAnimations.size();) {
39 if (m_activeAnimations[i]->id() == animationId) 44 if (m_activeAnimations[i]->id() == animationId)
40 m_activeAnimations.remove(i); 45 m_activeAnimations.remove(i);
41 else 46 else
42 i++; 47 i++;
43 } 48 }
49 updateActivation();
44 } 50 }
45 51
46 void LayerAnimationController::removeAnimation(int animationId, ActiveAnimation: :TargetProperty targetProperty) 52 void LayerAnimationController::removeAnimation(int animationId, ActiveAnimation: :TargetProperty targetProperty)
47 { 53 {
48 for (size_t i = 0; i < m_activeAnimations.size();) { 54 for (size_t i = 0; i < m_activeAnimations.size();) {
49 if (m_activeAnimations[i]->id() == animationId && m_activeAnimations[i]- >targetProperty() == targetProperty) 55 if (m_activeAnimations[i]->id() == animationId && m_activeAnimations[i]- >targetProperty() == targetProperty)
50 m_activeAnimations.remove(i); 56 m_activeAnimations.remove(i);
51 else 57 else
52 i++; 58 i++;
53 } 59 }
60 updateActivation();
54 } 61 }
55 62
56 // According to render layer backing, these are for testing only. 63 // According to render layer backing, these are for testing only.
57 void LayerAnimationController::suspendAnimations(double monotonicTime) 64 void LayerAnimationController::suspendAnimations(double monotonicTime)
58 { 65 {
59 for (size_t i = 0; i < m_activeAnimations.size(); ++i) { 66 for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
60 if (!m_activeAnimations[i]->isFinished()) 67 if (!m_activeAnimations[i]->isFinished())
61 m_activeAnimations[i]->setRunState(ActiveAnimation::Paused, monotoni cTime); 68 m_activeAnimations[i]->setRunState(ActiveAnimation::Paused, monotoni cTime);
62 } 69 }
63 } 70 }
(...skipping 18 matching lines...) Expand all
82 purgeAnimationsMarkedForDeletion(); 89 purgeAnimationsMarkedForDeletion();
83 pushNewAnimationsToImplThread(controllerImpl); 90 pushNewAnimationsToImplThread(controllerImpl);
84 91
85 // Remove finished impl side animations only after pushing, 92 // Remove finished impl side animations only after pushing,
86 // and only after the animations are deleted on the main thread 93 // and only after the animations are deleted on the main thread
87 // this insures we will never push an animation twice. 94 // this insures we will never push an animation twice.
88 removeAnimationsCompletedOnMainThread(controllerImpl); 95 removeAnimationsCompletedOnMainThread(controllerImpl);
89 96
90 pushPropertiesToImplThread(controllerImpl); 97 pushPropertiesToImplThread(controllerImpl);
91 } 98 }
99 controllerImpl->updateActivation();
100 updateActivation();
92 } 101 }
93 102
94 void LayerAnimationController::animate(double monotonicTime, AnimationEventsVect or* events) 103 void LayerAnimationController::animate(double monotonicTime, AnimationEventsVect or* events)
95 { 104 {
96 startAnimationsWaitingForNextTick(monotonicTime, events); 105 startAnimationsWaitingForNextTick(monotonicTime, events);
97 startAnimationsWaitingForStartTime(monotonicTime, events); 106 startAnimationsWaitingForStartTime(monotonicTime, events);
98 startAnimationsWaitingForTargetAvailability(monotonicTime, events); 107 startAnimationsWaitingForTargetAvailability(monotonicTime, events);
99 resolveConflicts(monotonicTime); 108 resolveConflicts(monotonicTime);
100 tickAnimations(monotonicTime); 109 tickAnimations(monotonicTime);
101 markAnimationsForDeletion(monotonicTime, events); 110 markAnimationsForDeletion(monotonicTime, events);
102 startAnimationsWaitingForTargetAvailability(monotonicTime, events); 111 startAnimationsWaitingForTargetAvailability(monotonicTime, events);
112
113 updateActivation();
103 } 114 }
104 115
105 void LayerAnimationController::addAnimation(scoped_ptr<ActiveAnimation> animatio n) 116 void LayerAnimationController::addAnimation(scoped_ptr<ActiveAnimation> animatio n)
106 { 117 {
107 m_activeAnimations.append(animation.Pass()); 118 m_activeAnimations.append(animation.Pass());
119 // Activate each time an animation is added.
enne (OOO) 2012/12/17 20:21:22 I'm not sure I follow the force parameter here. W
120 bool force = true;
121 updateActivation(force);
108 } 122 }
109 123
110 ActiveAnimation* LayerAnimationController::getActiveAnimation(int groupId, Activ eAnimation::TargetProperty targetProperty) const 124 ActiveAnimation* LayerAnimationController::getActiveAnimation(int groupId, Activ eAnimation::TargetProperty targetProperty) const
111 { 125 {
112 for (size_t i = 0; i < m_activeAnimations.size(); ++i) 126 for (size_t i = 0; i < m_activeAnimations.size(); ++i)
113 if (m_activeAnimations[i]->group() == groupId && m_activeAnimations[i]-> targetProperty() == targetProperty) 127 if (m_activeAnimations[i]->group() == groupId && m_activeAnimations[i]-> targetProperty() == targetProperty)
114 return m_activeAnimations[i]; 128 return m_activeAnimations[i];
115 return 0; 129 return 0;
116 } 130 }
117 131
(...skipping 29 matching lines...) Expand all
147 { 161 {
148 for (size_t i = 0; i < m_activeAnimations.size(); ++i) { 162 for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
149 if (m_activeAnimations[i]->group() == event.groupId && m_activeAnimation s[i]->targetProperty() == event.targetProperty && m_activeAnimations[i]->needsSy nchronizedStartTime()) { 163 if (m_activeAnimations[i]->group() == event.groupId && m_activeAnimation s[i]->targetProperty() == event.targetProperty && m_activeAnimations[i]->needsSy nchronizedStartTime()) {
150 m_activeAnimations[i]->setNeedsSynchronizedStartTime(false); 164 m_activeAnimations[i]->setNeedsSynchronizedStartTime(false);
151 m_activeAnimations[i]->setStartTime(event.monotonicTime); 165 m_activeAnimations[i]->setStartTime(event.monotonicTime);
152 return; 166 return;
153 } 167 }
154 } 168 }
155 } 169 }
156 170
157 void LayerAnimationController::setClient(LayerAnimationControllerClient* client) 171 void LayerAnimationController::setAnimationRegistrar(AnimationRegistrar* registr ar)
158 { 172 {
159 m_client = client; 173 if (m_registrar == registrar)
174 return;
175
176 if (m_registrar)
177 m_registrar->UnregisterAnimationController(this);
178
179 m_registrar = registrar;
180 if (m_registrar)
181 m_registrar->RegisterAnimationController(this);
182
183 bool force = true;
184 updateActivation(force);
185 }
186
187 void LayerAnimationController::addObserver(LayerAnimationControllerObserver* obs erver)
188 {
189 if (!m_observers.HasObserver(observer))
190 m_observers.AddObserver(observer);
191 }
192
193 void LayerAnimationController::removeObserver(LayerAnimationControllerObserver* observer)
194 {
195 m_observers.RemoveObserver(observer);
160 } 196 }
161 197
162 void LayerAnimationController::pushNewAnimationsToImplThread(LayerAnimationContr oller* controllerImpl) const 198 void LayerAnimationController::pushNewAnimationsToImplThread(LayerAnimationContr oller* controllerImpl) const
163 { 199 {
164 // Any new animations owned by the main thread's controller are cloned and a dde to the impl thread's controller. 200 // Any new animations owned by the main thread's controller are cloned and a dde to the impl thread's controller.
165 for (size_t i = 0; i < m_activeAnimations.size(); ++i) { 201 for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
166 // If the animation is already running on the impl thread, there is no n eed to copy it over. 202 // If the animation is already running on the impl thread, there is no n eed to copy it over.
167 if (controllerImpl->getActiveAnimation(m_activeAnimations[i]->group(), m _activeAnimations[i]->targetProperty())) 203 if (controllerImpl->getActiveAnimation(m_activeAnimations[i]->group(), m _activeAnimations[i]->targetProperty()))
168 continue; 204 continue;
169 205
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 } 243 }
208 244
209 void LayerAnimationController::startAnimationsWaitingForNextTick(double monotoni cTime, AnimationEventsVector* events) 245 void LayerAnimationController::startAnimationsWaitingForNextTick(double monotoni cTime, AnimationEventsVector* events)
210 { 246 {
211 for (size_t i = 0; i < m_activeAnimations.size(); ++i) { 247 for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
212 if (m_activeAnimations[i]->runState() == ActiveAnimation::WaitingForNext Tick) { 248 if (m_activeAnimations[i]->runState() == ActiveAnimation::WaitingForNext Tick) {
213 m_activeAnimations[i]->setRunState(ActiveAnimation::Running, monoton icTime); 249 m_activeAnimations[i]->setRunState(ActiveAnimation::Running, monoton icTime);
214 if (!m_activeAnimations[i]->hasSetStartTime()) 250 if (!m_activeAnimations[i]->hasSetStartTime())
215 m_activeAnimations[i]->setStartTime(monotonicTime); 251 m_activeAnimations[i]->setStartTime(monotonicTime);
216 if (events) 252 if (events)
217 events->push_back(AnimationEvent(AnimationEvent::Started, m_clie nt->id(), m_activeAnimations[i]->group(), m_activeAnimations[i]->targetProperty( ), monotonicTime)); 253 events->push_back(AnimationEvent(AnimationEvent::Started, m_id, m_activeAnimations[i]->group(), m_activeAnimations[i]->targetProperty(), monoton icTime));
218 } 254 }
219 } 255 }
220 } 256 }
221 257
222 void LayerAnimationController::startAnimationsWaitingForStartTime(double monoton icTime, AnimationEventsVector* events) 258 void LayerAnimationController::startAnimationsWaitingForStartTime(double monoton icTime, AnimationEventsVector* events)
223 { 259 {
224 for (size_t i = 0; i < m_activeAnimations.size(); ++i) { 260 for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
225 if (m_activeAnimations[i]->runState() == ActiveAnimation::WaitingForStar tTime && m_activeAnimations[i]->startTime() <= monotonicTime) { 261 if (m_activeAnimations[i]->runState() == ActiveAnimation::WaitingForStar tTime && m_activeAnimations[i]->startTime() <= monotonicTime) {
226 m_activeAnimations[i]->setRunState(ActiveAnimation::Running, monoton icTime); 262 m_activeAnimations[i]->setRunState(ActiveAnimation::Running, monoton icTime);
227 if (events) 263 if (events)
228 events->push_back(AnimationEvent(AnimationEvent::Started, m_clie nt->id(), m_activeAnimations[i]->group(), m_activeAnimations[i]->targetProperty( ), monotonicTime)); 264 events->push_back(AnimationEvent(AnimationEvent::Started, m_id, m_activeAnimations[i]->group(), m_activeAnimations[i]->targetProperty(), monoton icTime));
229 } 265 }
230 } 266 }
231 } 267 }
232 268
233 void LayerAnimationController::startAnimationsWaitingForTargetAvailability(doubl e monotonicTime, AnimationEventsVector* events) 269 void LayerAnimationController::startAnimationsWaitingForTargetAvailability(doubl e monotonicTime, AnimationEventsVector* events)
234 { 270 {
235 // First collect running properties. 271 // First collect running properties.
236 TargetProperties blockedProperties; 272 TargetProperties blockedProperties;
237 for (size_t i = 0; i < m_activeAnimations.size(); ++i) { 273 for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
238 if (m_activeAnimations[i]->runState() == ActiveAnimation::Running || m_a ctiveAnimations[i]->runState() == ActiveAnimation::Finished) 274 if (m_activeAnimations[i]->runState() == ActiveAnimation::Running || m_a ctiveAnimations[i]->runState() == ActiveAnimation::Finished)
(...skipping 18 matching lines...) Expand all
257 if (!blockedProperties.insert(*pIter).second) 293 if (!blockedProperties.insert(*pIter).second)
258 nullIntersection = false; 294 nullIntersection = false;
259 } 295 }
260 296
261 // If the intersection is null, then we are free to start the animat ions in the group. 297 // If the intersection is null, then we are free to start the animat ions in the group.
262 if (nullIntersection) { 298 if (nullIntersection) {
263 m_activeAnimations[i]->setRunState(ActiveAnimation::Running, mon otonicTime); 299 m_activeAnimations[i]->setRunState(ActiveAnimation::Running, mon otonicTime);
264 if (!m_activeAnimations[i]->hasSetStartTime()) 300 if (!m_activeAnimations[i]->hasSetStartTime())
265 m_activeAnimations[i]->setStartTime(monotonicTime); 301 m_activeAnimations[i]->setStartTime(monotonicTime);
266 if (events) 302 if (events)
267 events->push_back(AnimationEvent(AnimationEvent::Started, m_ client->id(), m_activeAnimations[i]->group(), m_activeAnimations[i]->targetPrope rty(), monotonicTime)); 303 events->push_back(AnimationEvent(AnimationEvent::Started, m_ id, m_activeAnimations[i]->group(), m_activeAnimations[i]->targetProperty(), mon otonicTime));
268 for (size_t j = i + 1; j < m_activeAnimations.size(); ++j) { 304 for (size_t j = i + 1; j < m_activeAnimations.size(); ++j) {
269 if (m_activeAnimations[i]->group() == m_activeAnimations[j]- >group()) { 305 if (m_activeAnimations[i]->group() == m_activeAnimations[j]- >group()) {
270 m_activeAnimations[j]->setRunState(ActiveAnimation::Runn ing, monotonicTime); 306 m_activeAnimations[j]->setRunState(ActiveAnimation::Runn ing, monotonicTime);
271 if (!m_activeAnimations[j]->hasSetStartTime()) 307 if (!m_activeAnimations[j]->hasSetStartTime())
272 m_activeAnimations[j]->setStartTime(monotonicTime); 308 m_activeAnimations[j]->setStartTime(monotonicTime);
273 } 309 }
274 } 310 }
275 } 311 }
276 } 312 }
277 } 313 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 break; 349 break;
314 } 350 }
315 } 351 }
316 } 352 }
317 if (allAnimsWithSameIdAreFinished) { 353 if (allAnimsWithSameIdAreFinished) {
318 // We now need to remove all animations with the same group id as gr oupId 354 // We now need to remove all animations with the same group id as gr oupId
319 // (and send along animation finished notifications, if necessary). 355 // (and send along animation finished notifications, if necessary).
320 for (size_t j = i; j < m_activeAnimations.size(); j++) { 356 for (size_t j = i; j < m_activeAnimations.size(); j++) {
321 if (groupId == m_activeAnimations[j]->group()) { 357 if (groupId == m_activeAnimations[j]->group()) {
322 if (events) 358 if (events)
323 events->push_back(AnimationEvent(AnimationEvent::Finishe d, m_client->id(), m_activeAnimations[j]->group(), m_activeAnimations[j]->target Property(), monotonicTime)); 359 events->push_back(AnimationEvent(AnimationEvent::Finishe d, m_id, m_activeAnimations[j]->group(), m_activeAnimations[j]->targetProperty() , monotonicTime));
324 m_activeAnimations[j]->setRunState(ActiveAnimation::WaitingF orDeletion, monotonicTime); 360 m_activeAnimations[j]->setRunState(ActiveAnimation::WaitingF orDeletion, monotonicTime);
325 } 361 }
326 } 362 }
327 } 363 }
328 } 364 }
329 } 365 }
330 366
331 void LayerAnimationController::purgeAnimationsMarkedForDeletion() 367 void LayerAnimationController::purgeAnimationsMarkedForDeletion()
332 { 368 {
333 for (size_t i = 0; i < m_activeAnimations.size();) { 369 for (size_t i = 0; i < m_activeAnimations.size();) {
(...skipping 30 matching lines...) Expand all
364 400
365 // Animation assumes its initial value until it gets the synchronize d start time 401 // Animation assumes its initial value until it gets the synchronize d start time
366 // from the impl thread and can start ticking. 402 // from the impl thread and can start ticking.
367 if (m_activeAnimations[i]->needsSynchronizedStartTime()) 403 if (m_activeAnimations[i]->needsSynchronizedStartTime())
368 trimmed = 0; 404 trimmed = 0;
369 405
370 switch (m_activeAnimations[i]->targetProperty()) { 406 switch (m_activeAnimations[i]->targetProperty()) {
371 407
372 case ActiveAnimation::Transform: { 408 case ActiveAnimation::Transform: {
373 const TransformAnimationCurve* transformAnimationCurve = m_activ eAnimations[i]->curve()->toTransformAnimationCurve(); 409 const TransformAnimationCurve* transformAnimationCurve = m_activ eAnimations[i]->curve()->toTransformAnimationCurve();
374 const gfx::Transform matrix = transformAnimationCurve->getValue( trimmed).toTransform(); 410 const gfx::Transform transform = transformAnimationCurve->getVal ue(trimmed).toTransform();
375 if (m_activeAnimations[i]->isFinishedAt(monotonicTime)) 411 if (m_activeAnimations[i]->isFinishedAt(monotonicTime))
376 m_activeAnimations[i]->setRunState(ActiveAnimation::Finished , monotonicTime); 412 m_activeAnimations[i]->setRunState(ActiveAnimation::Finished , monotonicTime);
377 413
378 m_client->setTransformFromAnimation(matrix); 414 notifyObserversTransformAnimated(transform);
379 break; 415 break;
380 } 416 }
381 417
382 case ActiveAnimation::Opacity: { 418 case ActiveAnimation::Opacity: {
383 const FloatAnimationCurve* floatAnimationCurve = m_activeAnimati ons[i]->curve()->toFloatAnimationCurve(); 419 const FloatAnimationCurve* floatAnimationCurve = m_activeAnimati ons[i]->curve()->toFloatAnimationCurve();
384 const float opacity = floatAnimationCurve->getValue(trimmed); 420 const float opacity = floatAnimationCurve->getValue(trimmed);
385 if (m_activeAnimations[i]->isFinishedAt(monotonicTime)) 421 if (m_activeAnimations[i]->isFinishedAt(monotonicTime))
386 m_activeAnimations[i]->setRunState(ActiveAnimation::Finished , monotonicTime); 422 m_activeAnimations[i]->setRunState(ActiveAnimation::Finished , monotonicTime);
387 423
388 m_client->setOpacityFromAnimation(opacity); 424 notifyObserversOpacityAnimated(opacity);
389 break; 425 break;
390 } 426 }
391 427
392 // Do nothing for sentinel value. 428 // Do nothing for sentinel value.
393 case ActiveAnimation::TargetPropertyEnumSize: 429 case ActiveAnimation::TargetPropertyEnumSize:
394 NOTREACHED(); 430 NOTREACHED();
395 } 431 }
396 } 432 }
397 } 433 }
398 } 434 }
399 435
436 void LayerAnimationController::updateActivation(bool force)
437 {
438 if (m_registrar) {
439 if (!m_activeAnimations.isEmpty() && (!m_isActive || force))
440 m_registrar->DidActivateAnimationController(this);
441 else if (m_activeAnimations.isEmpty() && (m_isActive || force))
442 m_registrar->DidDeactivateAnimationController(this);
443 m_isActive = !m_activeAnimations.isEmpty();
444 }
445 }
446
447 void LayerAnimationController::notifyObserversOpacityAnimated(float opacity)
448 {
449 FOR_EACH_OBSERVER(LayerAnimationControllerObserver,
450 m_observers,
451 OnOpacityAnimated(opacity));
452 }
453
454 void LayerAnimationController::notifyObserversTransformAnimated(const gfx::Trans form& transform)
455 {
456 FOR_EACH_OBSERVER(LayerAnimationControllerObserver,
457 m_observers,
458 OnTransformAnimated(transform));
459 }
460
400 } // namespace cc 461 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698