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

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
« no previous file with comments | « cc/layer_animation_controller.h ('k') | cc/layer_animation_controller_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
10 #include "cc/layer_animation_value_observer.h"
9 #include "ui/gfx/transform.h" 11 #include "ui/gfx/transform.h"
10 12
11 namespace cc { 13 namespace cc {
12 14
13 LayerAnimationController::LayerAnimationController(LayerAnimationControllerClien t* client) 15 LayerAnimationController::LayerAnimationController(int id)
14 : m_forceSync(false) 16 : m_forceSync(false)
15 , m_client(client) 17 , m_id(id)
18 , m_registrar(0)
19 , m_isActive(false)
16 { 20 {
17 } 21 }
18 22
19 LayerAnimationController::~LayerAnimationController() 23 LayerAnimationController::~LayerAnimationController()
20 { 24 {
25 if (m_registrar)
26 m_registrar->UnregisterAnimationController(this);
21 } 27 }
22 28
23 scoped_ptr<LayerAnimationController> LayerAnimationController::create(LayerAnima tionControllerClient* client) 29 scoped_refptr<LayerAnimationController> LayerAnimationController::create(int id)
24 { 30 {
25 return make_scoped_ptr(new LayerAnimationController(client)); 31 return make_scoped_refptr(new LayerAnimationController(id));
26 } 32 }
27 33
28 void LayerAnimationController::pauseAnimation(int animationId, double timeOffset ) 34 void LayerAnimationController::pauseAnimation(int animationId, double timeOffset )
29 { 35 {
30 for (size_t i = 0; i < m_activeAnimations.size(); ++i) { 36 for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
31 if (m_activeAnimations[i]->id() == animationId) 37 if (m_activeAnimations[i]->id() == animationId)
32 m_activeAnimations[i]->setRunState(ActiveAnimation::Paused, timeOffs et + m_activeAnimations[i]->startTime()); 38 m_activeAnimations[i]->setRunState(ActiveAnimation::Paused, timeOffs et + m_activeAnimations[i]->startTime());
33 } 39 }
34 } 40 }
35 41
36 void LayerAnimationController::removeAnimation(int animationId) 42 void LayerAnimationController::removeAnimation(int animationId)
37 { 43 {
38 for (size_t i = 0; i < m_activeAnimations.size();) { 44 for (size_t i = 0; i < m_activeAnimations.size();) {
39 if (m_activeAnimations[i]->id() == animationId) 45 if (m_activeAnimations[i]->id() == animationId)
40 m_activeAnimations.remove(i); 46 m_activeAnimations.remove(i);
41 else 47 else
42 i++; 48 i++;
43 } 49 }
50 updateActivation();
44 } 51 }
45 52
46 void LayerAnimationController::removeAnimation(int animationId, ActiveAnimation: :TargetProperty targetProperty) 53 void LayerAnimationController::removeAnimation(int animationId, ActiveAnimation: :TargetProperty targetProperty)
47 { 54 {
48 for (size_t i = 0; i < m_activeAnimations.size();) { 55 for (size_t i = 0; i < m_activeAnimations.size();) {
49 if (m_activeAnimations[i]->id() == animationId && m_activeAnimations[i]- >targetProperty() == targetProperty) 56 if (m_activeAnimations[i]->id() == animationId && m_activeAnimations[i]- >targetProperty() == targetProperty)
50 m_activeAnimations.remove(i); 57 m_activeAnimations.remove(i);
51 else 58 else
52 i++; 59 i++;
53 } 60 }
61 updateActivation();
54 } 62 }
55 63
56 // According to render layer backing, these are for testing only. 64 // According to render layer backing, these are for testing only.
57 void LayerAnimationController::suspendAnimations(double monotonicTime) 65 void LayerAnimationController::suspendAnimations(double monotonicTime)
58 { 66 {
59 for (size_t i = 0; i < m_activeAnimations.size(); ++i) { 67 for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
60 if (!m_activeAnimations[i]->isFinished()) 68 if (!m_activeAnimations[i]->isFinished())
61 m_activeAnimations[i]->setRunState(ActiveAnimation::Paused, monotoni cTime); 69 m_activeAnimations[i]->setRunState(ActiveAnimation::Paused, monotoni cTime);
62 } 70 }
63 } 71 }
(...skipping 18 matching lines...) Expand all
82 purgeAnimationsMarkedForDeletion(); 90 purgeAnimationsMarkedForDeletion();
83 pushNewAnimationsToImplThread(controllerImpl); 91 pushNewAnimationsToImplThread(controllerImpl);
84 92
85 // Remove finished impl side animations only after pushing, 93 // Remove finished impl side animations only after pushing,
86 // and only after the animations are deleted on the main thread 94 // and only after the animations are deleted on the main thread
87 // this insures we will never push an animation twice. 95 // this insures we will never push an animation twice.
88 removeAnimationsCompletedOnMainThread(controllerImpl); 96 removeAnimationsCompletedOnMainThread(controllerImpl);
89 97
90 pushPropertiesToImplThread(controllerImpl); 98 pushPropertiesToImplThread(controllerImpl);
91 } 99 }
100 controllerImpl->updateActivation();
101 updateActivation();
92 } 102 }
93 103
94 void LayerAnimationController::animate(double monotonicTime, AnimationEventsVect or* events) 104 void LayerAnimationController::animate(double monotonicTime, AnimationEventsVect or* events)
95 { 105 {
96 startAnimationsWaitingForNextTick(monotonicTime, events); 106 startAnimationsWaitingForNextTick(monotonicTime, events);
97 startAnimationsWaitingForStartTime(monotonicTime, events); 107 startAnimationsWaitingForStartTime(monotonicTime, events);
98 startAnimationsWaitingForTargetAvailability(monotonicTime, events); 108 startAnimationsWaitingForTargetAvailability(monotonicTime, events);
99 resolveConflicts(monotonicTime); 109 resolveConflicts(monotonicTime);
100 tickAnimations(monotonicTime); 110 tickAnimations(monotonicTime);
101 markAnimationsForDeletion(monotonicTime, events); 111 markAnimationsForDeletion(monotonicTime, events);
102 startAnimationsWaitingForTargetAvailability(monotonicTime, events); 112 startAnimationsWaitingForTargetAvailability(monotonicTime, events);
113
114 updateActivation();
103 } 115 }
104 116
105 void LayerAnimationController::addAnimation(scoped_ptr<ActiveAnimation> animatio n) 117 void LayerAnimationController::addAnimation(scoped_ptr<ActiveAnimation> animatio n)
106 { 118 {
107 m_activeAnimations.append(animation.Pass()); 119 m_activeAnimations.append(animation.Pass());
120 updateActivation();
108 } 121 }
109 122
110 ActiveAnimation* LayerAnimationController::getActiveAnimation(int groupId, Activ eAnimation::TargetProperty targetProperty) const 123 ActiveAnimation* LayerAnimationController::getActiveAnimation(int groupId, Activ eAnimation::TargetProperty targetProperty) const
111 { 124 {
112 for (size_t i = 0; i < m_activeAnimations.size(); ++i) 125 for (size_t i = 0; i < m_activeAnimations.size(); ++i)
113 if (m_activeAnimations[i]->group() == groupId && m_activeAnimations[i]-> targetProperty() == targetProperty) 126 if (m_activeAnimations[i]->group() == groupId && m_activeAnimations[i]-> targetProperty() == targetProperty)
114 return m_activeAnimations[i]; 127 return m_activeAnimations[i];
115 return 0; 128 return 0;
116 } 129 }
117 130
(...skipping 29 matching lines...) Expand all
147 { 160 {
148 for (size_t i = 0; i < m_activeAnimations.size(); ++i) { 161 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()) { 162 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); 163 m_activeAnimations[i]->setNeedsSynchronizedStartTime(false);
151 m_activeAnimations[i]->setStartTime(event.monotonicTime); 164 m_activeAnimations[i]->setStartTime(event.monotonicTime);
152 return; 165 return;
153 } 166 }
154 } 167 }
155 } 168 }
156 169
157 void LayerAnimationController::setClient(LayerAnimationControllerClient* client) 170 void LayerAnimationController::setAnimationRegistrar(AnimationRegistrar* registr ar)
158 { 171 {
159 m_client = client; 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* obser ver)
193 {
194 m_observers.RemoveObserver(observer);
160 } 195 }
161 196
162 void LayerAnimationController::pushNewAnimationsToImplThread(LayerAnimationContr oller* controllerImpl) const 197 void LayerAnimationController::pushNewAnimationsToImplThread(LayerAnimationContr oller* controllerImpl) const
163 { 198 {
164 // Any new animations owned by the main thread's controller are cloned and a dde to the impl thread's controller. 199 // 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) { 200 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. 201 // 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())) 202 if (controllerImpl->getActiveAnimation(m_activeAnimations[i]->group(), m _activeAnimations[i]->targetProperty()))
168 continue; 203 continue;
169 204
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 } 242 }
208 243
209 void LayerAnimationController::startAnimationsWaitingForNextTick(double monotoni cTime, AnimationEventsVector* events) 244 void LayerAnimationController::startAnimationsWaitingForNextTick(double monotoni cTime, AnimationEventsVector* events)
210 { 245 {
211 for (size_t i = 0; i < m_activeAnimations.size(); ++i) { 246 for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
212 if (m_activeAnimations[i]->runState() == ActiveAnimation::WaitingForNext Tick) { 247 if (m_activeAnimations[i]->runState() == ActiveAnimation::WaitingForNext Tick) {
213 m_activeAnimations[i]->setRunState(ActiveAnimation::Running, monoton icTime); 248 m_activeAnimations[i]->setRunState(ActiveAnimation::Running, monoton icTime);
214 if (!m_activeAnimations[i]->hasSetStartTime()) 249 if (!m_activeAnimations[i]->hasSetStartTime())
215 m_activeAnimations[i]->setStartTime(monotonicTime); 250 m_activeAnimations[i]->setStartTime(monotonicTime);
216 if (events) 251 if (events)
217 events->push_back(AnimationEvent(AnimationEvent::Started, m_clie nt->id(), m_activeAnimations[i]->group(), m_activeAnimations[i]->targetProperty( ), monotonicTime)); 252 events->push_back(AnimationEvent(AnimationEvent::Started, m_id, m_activeAnimations[i]->group(), m_activeAnimations[i]->targetProperty(), monoton icTime));
218 } 253 }
219 } 254 }
220 } 255 }
221 256
222 void LayerAnimationController::startAnimationsWaitingForStartTime(double monoton icTime, AnimationEventsVector* events) 257 void LayerAnimationController::startAnimationsWaitingForStartTime(double monoton icTime, AnimationEventsVector* events)
223 { 258 {
224 for (size_t i = 0; i < m_activeAnimations.size(); ++i) { 259 for (size_t i = 0; i < m_activeAnimations.size(); ++i) {
225 if (m_activeAnimations[i]->runState() == ActiveAnimation::WaitingForStar tTime && m_activeAnimations[i]->startTime() <= monotonicTime) { 260 if (m_activeAnimations[i]->runState() == ActiveAnimation::WaitingForStar tTime && m_activeAnimations[i]->startTime() <= monotonicTime) {
226 m_activeAnimations[i]->setRunState(ActiveAnimation::Running, monoton icTime); 261 m_activeAnimations[i]->setRunState(ActiveAnimation::Running, monoton icTime);
227 if (events) 262 if (events)
228 events->push_back(AnimationEvent(AnimationEvent::Started, m_clie nt->id(), m_activeAnimations[i]->group(), m_activeAnimations[i]->targetProperty( ), monotonicTime)); 263 events->push_back(AnimationEvent(AnimationEvent::Started, m_id, m_activeAnimations[i]->group(), m_activeAnimations[i]->targetProperty(), monoton icTime));
229 } 264 }
230 } 265 }
231 } 266 }
232 267
233 void LayerAnimationController::startAnimationsWaitingForTargetAvailability(doubl e monotonicTime, AnimationEventsVector* events) 268 void LayerAnimationController::startAnimationsWaitingForTargetAvailability(doubl e monotonicTime, AnimationEventsVector* events)
234 { 269 {
235 // First collect running properties. 270 // First collect running properties.
236 TargetProperties blockedProperties; 271 TargetProperties blockedProperties;
237 for (size_t i = 0; i < m_activeAnimations.size(); ++i) { 272 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) 273 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) 292 if (!blockedProperties.insert(*pIter).second)
258 nullIntersection = false; 293 nullIntersection = false;
259 } 294 }
260 295
261 // If the intersection is null, then we are free to start the animat ions in the group. 296 // If the intersection is null, then we are free to start the animat ions in the group.
262 if (nullIntersection) { 297 if (nullIntersection) {
263 m_activeAnimations[i]->setRunState(ActiveAnimation::Running, mon otonicTime); 298 m_activeAnimations[i]->setRunState(ActiveAnimation::Running, mon otonicTime);
264 if (!m_activeAnimations[i]->hasSetStartTime()) 299 if (!m_activeAnimations[i]->hasSetStartTime())
265 m_activeAnimations[i]->setStartTime(monotonicTime); 300 m_activeAnimations[i]->setStartTime(monotonicTime);
266 if (events) 301 if (events)
267 events->push_back(AnimationEvent(AnimationEvent::Started, m_ client->id(), m_activeAnimations[i]->group(), m_activeAnimations[i]->targetPrope rty(), monotonicTime)); 302 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) { 303 for (size_t j = i + 1; j < m_activeAnimations.size(); ++j) {
269 if (m_activeAnimations[i]->group() == m_activeAnimations[j]- >group()) { 304 if (m_activeAnimations[i]->group() == m_activeAnimations[j]- >group()) {
270 m_activeAnimations[j]->setRunState(ActiveAnimation::Runn ing, monotonicTime); 305 m_activeAnimations[j]->setRunState(ActiveAnimation::Runn ing, monotonicTime);
271 if (!m_activeAnimations[j]->hasSetStartTime()) 306 if (!m_activeAnimations[j]->hasSetStartTime())
272 m_activeAnimations[j]->setStartTime(monotonicTime); 307 m_activeAnimations[j]->setStartTime(monotonicTime);
273 } 308 }
274 } 309 }
275 } 310 }
276 } 311 }
277 } 312 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 break; 348 break;
314 } 349 }
315 } 350 }
316 } 351 }
317 if (allAnimsWithSameIdAreFinished) { 352 if (allAnimsWithSameIdAreFinished) {
318 // We now need to remove all animations with the same group id as gr oupId 353 // We now need to remove all animations with the same group id as gr oupId
319 // (and send along animation finished notifications, if necessary). 354 // (and send along animation finished notifications, if necessary).
320 for (size_t j = i; j < m_activeAnimations.size(); j++) { 355 for (size_t j = i; j < m_activeAnimations.size(); j++) {
321 if (groupId == m_activeAnimations[j]->group()) { 356 if (groupId == m_activeAnimations[j]->group()) {
322 if (events) 357 if (events)
323 events->push_back(AnimationEvent(AnimationEvent::Finishe d, m_client->id(), m_activeAnimations[j]->group(), m_activeAnimations[j]->target Property(), monotonicTime)); 358 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); 359 m_activeAnimations[j]->setRunState(ActiveAnimation::WaitingF orDeletion, monotonicTime);
325 } 360 }
326 } 361 }
327 } 362 }
328 } 363 }
329 } 364 }
330 365
331 void LayerAnimationController::purgeAnimationsMarkedForDeletion() 366 void LayerAnimationController::purgeAnimationsMarkedForDeletion()
332 { 367 {
333 for (size_t i = 0; i < m_activeAnimations.size();) { 368 for (size_t i = 0; i < m_activeAnimations.size();) {
(...skipping 30 matching lines...) Expand all
364 399
365 // Animation assumes its initial value until it gets the synchronize d start time 400 // Animation assumes its initial value until it gets the synchronize d start time
366 // from the impl thread and can start ticking. 401 // from the impl thread and can start ticking.
367 if (m_activeAnimations[i]->needsSynchronizedStartTime()) 402 if (m_activeAnimations[i]->needsSynchronizedStartTime())
368 trimmed = 0; 403 trimmed = 0;
369 404
370 switch (m_activeAnimations[i]->targetProperty()) { 405 switch (m_activeAnimations[i]->targetProperty()) {
371 406
372 case ActiveAnimation::Transform: { 407 case ActiveAnimation::Transform: {
373 const TransformAnimationCurve* transformAnimationCurve = m_activ eAnimations[i]->curve()->toTransformAnimationCurve(); 408 const TransformAnimationCurve* transformAnimationCurve = m_activ eAnimations[i]->curve()->toTransformAnimationCurve();
374 const gfx::Transform matrix = transformAnimationCurve->getValue( trimmed).toTransform(); 409 const gfx::Transform transform = transformAnimationCurve->getVal ue(trimmed).toTransform();
375 if (m_activeAnimations[i]->isFinishedAt(monotonicTime)) 410 if (m_activeAnimations[i]->isFinishedAt(monotonicTime))
376 m_activeAnimations[i]->setRunState(ActiveAnimation::Finished , monotonicTime); 411 m_activeAnimations[i]->setRunState(ActiveAnimation::Finished , monotonicTime);
377 412
378 m_client->setTransformFromAnimation(matrix); 413 notifyObserversTransformAnimated(transform);
379 break; 414 break;
380 } 415 }
381 416
382 case ActiveAnimation::Opacity: { 417 case ActiveAnimation::Opacity: {
383 const FloatAnimationCurve* floatAnimationCurve = m_activeAnimati ons[i]->curve()->toFloatAnimationCurve(); 418 const FloatAnimationCurve* floatAnimationCurve = m_activeAnimati ons[i]->curve()->toFloatAnimationCurve();
384 const float opacity = floatAnimationCurve->getValue(trimmed); 419 const float opacity = floatAnimationCurve->getValue(trimmed);
385 if (m_activeAnimations[i]->isFinishedAt(monotonicTime)) 420 if (m_activeAnimations[i]->isFinishedAt(monotonicTime))
386 m_activeAnimations[i]->setRunState(ActiveAnimation::Finished , monotonicTime); 421 m_activeAnimations[i]->setRunState(ActiveAnimation::Finished , monotonicTime);
387 422
388 m_client->setOpacityFromAnimation(opacity); 423 notifyObserversOpacityAnimated(opacity);
389 break; 424 break;
390 } 425 }
391 426
392 // Do nothing for sentinel value. 427 // Do nothing for sentinel value.
393 case ActiveAnimation::TargetPropertyEnumSize: 428 case ActiveAnimation::TargetPropertyEnumSize:
394 NOTREACHED(); 429 NOTREACHED();
395 } 430 }
396 } 431 }
397 } 432 }
398 } 433 }
399 434
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::Trans form& transform)
454 {
455 FOR_EACH_OBSERVER(LayerAnimationValueObserver,
456 m_observers,
457 OnTransformAnimated(transform));
458 }
459
400 } // namespace cc 460 } // namespace cc
OLDNEW
« no previous file with comments | « cc/layer_animation_controller.h ('k') | cc/layer_animation_controller_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698