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

Side by Side Diff: cc/trees/layer_tree_host_unittest_animation.cc

Issue 13613003: cc: Make animations tick regardless of drawing. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months 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/trees/layer_tree_host.h" 5 #include "cc/trees/layer_tree_host.h"
6 6
7 #include "cc/animation/animation_curve.h" 7 #include "cc/animation/animation_curve.h"
8 #include "cc/animation/layer_animation_controller.h" 8 #include "cc/animation/layer_animation_controller.h"
9 #include "cc/animation/timing_function.h" 9 #include "cc/animation/timing_function.h"
10 #include "cc/layers/layer.h" 10 #include "cc/layers/layer.h"
11 #include "cc/layers/layer_impl.h" 11 #include "cc/layers/layer_impl.h"
12 #include "cc/test/fake_content_layer.h" 12 #include "cc/test/fake_content_layer.h"
13 #include "cc/test/fake_content_layer_client.h" 13 #include "cc/test/fake_content_layer_client.h"
14 #include "cc/test/layer_tree_test.h" 14 #include "cc/test/layer_tree_test.h"
15 #include "cc/trees/layer_tree_impl.h"
15 16
16 namespace cc { 17 namespace cc {
17 namespace { 18 namespace {
18 19
19 class LayerTreeHostAnimationTest : public LayerTreeTest { 20 class LayerTreeHostAnimationTest : public LayerTreeTest {
20 public: 21 public:
21 virtual void SetupTree() OVERRIDE { 22 virtual void SetupTree() OVERRIDE {
22 LayerTreeTest::SetupTree(); 23 LayerTreeTest::SetupTree();
23 layer_tree_host()->root_layer()->set_layer_animation_delegate(this); 24 layer_tree_host()->root_layer()->set_layer_animation_delegate(this);
24 } 25 }
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 : num_animates_(0), 116 : num_animates_(0),
116 received_animation_started_notification_(false), 117 received_animation_started_notification_(false),
117 start_time_(0.0) { 118 start_time_(0.0) {
118 } 119 }
119 120
120 virtual void BeginTest() OVERRIDE { 121 virtual void BeginTest() OVERRIDE {
121 PostAddInstantAnimationToMainThread(); 122 PostAddInstantAnimationToMainThread();
122 } 123 }
123 124
124 virtual void UpdateAnimationState( 125 virtual void UpdateAnimationState(
125 LayerTreeHostImpl* impl_host, 126 LayerTreeHostImpl* host_impl,
126 bool has_unfinished_animation) OVERRIDE { 127 bool has_unfinished_animation) OVERRIDE {
127 if (!num_animates_) { 128 if (!num_animates_) {
128 // The animation had zero duration so LayerTreeHostImpl should no 129 // The animation had zero duration so LayerTreeHostImpl should no
129 // longer need to animate its layers. 130 // longer need to animate its layers.
130 EXPECT_FALSE(has_unfinished_animation); 131 EXPECT_FALSE(has_unfinished_animation);
131 num_animates_++; 132 num_animates_++;
132 return; 133 return;
133 } 134 }
134 135
135 if (received_animation_started_notification_) { 136 if (received_animation_started_notification_) {
136 EXPECT_LT(0.0, start_time_); 137 EXPECT_LT(0.0, start_time_);
138
139 LayerAnimationController* controller_impl =
140 host_impl->active_tree()->root_layer()->layer_animation_controller();
141 Animation* animation_impl =
142 controller_impl->GetAnimation(0, Animation::Opacity);
143 if (animation_impl)
144 controller_impl->RemoveAnimation(animation_impl->id());
145
137 EndTest(); 146 EndTest();
138 } 147 }
139 } 148 }
140 149
141 virtual void notifyAnimationStarted(double wall_clock_time) OVERRIDE { 150 virtual void notifyAnimationStarted(double wall_clock_time) OVERRIDE {
142 received_animation_started_notification_ = true; 151 received_animation_started_notification_ = true;
143 start_time_ = wall_clock_time; 152 start_time_ = wall_clock_time;
144 if (num_animates_) { 153 if (num_animates_) {
145 EXPECT_LT(0.0, start_time_); 154 EXPECT_LT(0.0, start_time_);
155
156 LayerAnimationController* controller =
157 layer_tree_host()->root_layer()->layer_animation_controller();
158 Animation* animation =
159 controller->GetAnimation(0, Animation::Opacity);
160 if (animation)
161 controller->RemoveAnimation(animation->id());
162
146 EndTest(); 163 EndTest();
147 } 164 }
148 } 165 }
149 166
150 virtual void AfterTest() OVERRIDE {} 167 virtual void AfterTest() OVERRIDE {}
151 168
152 private: 169 private:
153 int num_animates_; 170 int num_animates_;
154 bool received_animation_started_notification_; 171 bool received_animation_started_notification_;
155 double start_time_; 172 double start_time_;
156 }; 173 };
157 174
158 MULTI_THREAD_TEST_F(LayerTreeHostAnimationTestAddAnimation); 175 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostAnimationTestAddAnimation);
159 176
160 // Add a layer animation to a layer, but continually fail to draw. Confirm that 177 // Add a layer animation to a layer, but continually fail to draw. Confirm that
161 // after a while, we do eventually force a draw. 178 // after a while, we do eventually force a draw.
162 class LayerTreeHostAnimationTestCheckerboardDoesNotStarveDraws 179 class LayerTreeHostAnimationTestCheckerboardDoesNotStarveDraws
163 : public LayerTreeHostAnimationTest { 180 : public LayerTreeHostAnimationTest {
164 public: 181 public:
165 LayerTreeHostAnimationTestCheckerboardDoesNotStarveDraws() 182 LayerTreeHostAnimationTestCheckerboardDoesNotStarveDraws()
166 : started_animating_(false) {} 183 : started_animating_(false) {}
167 184
168 virtual void BeginTest() OVERRIDE { 185 virtual void BeginTest() OVERRIDE {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 250
234 SINGLE_AND_MULTI_THREAD_TEST_F( 251 SINGLE_AND_MULTI_THREAD_TEST_F(
235 LayerTreeHostAnimationTestTickAnimationWhileBackgrounded); 252 LayerTreeHostAnimationTestTickAnimationWhileBackgrounded);
236 253
237 // Ensures that animations continue to be ticked when we are backgrounded. 254 // Ensures that animations continue to be ticked when we are backgrounded.
238 class LayerTreeHostAnimationTestAddAnimationWithTimingFunction 255 class LayerTreeHostAnimationTestAddAnimationWithTimingFunction
239 : public LayerTreeHostAnimationTest { 256 : public LayerTreeHostAnimationTest {
240 public: 257 public:
241 LayerTreeHostAnimationTestAddAnimationWithTimingFunction() {} 258 LayerTreeHostAnimationTestAddAnimationWithTimingFunction() {}
242 259
260 virtual void SetupTree() OVERRIDE {
261 LayerTreeHostAnimationTest::SetupTree();
262 content_ = FakeContentLayer::Create(&client_);
263 content_->SetBounds(gfx::Size(4, 4));
264 layer_tree_host()->root_layer()->AddChild(content_);
265 }
266
243 virtual void BeginTest() OVERRIDE { 267 virtual void BeginTest() OVERRIDE {
244 PostAddAnimationToMainThread(layer_tree_host()->root_layer()); 268 PostAddAnimationToMainThread(content_);
245 } 269 }
246 270
247 virtual void AnimateLayers( 271 virtual void AnimateLayers(
248 LayerTreeHostImpl* host_impl, 272 LayerTreeHostImpl* host_impl,
249 base::TimeTicks monotonic_time) OVERRIDE { 273 base::TimeTicks monotonic_time) OVERRIDE {
250 LayerAnimationController* controller = 274 LayerAnimationController* controller =
251 layer_tree_host()->root_layer()->layer_animation_controller(); 275 layer_tree_host()->root_layer()->children()[0]->
276 layer_animation_controller();
252 Animation* animation = 277 Animation* animation =
253 controller->GetAnimation(0, Animation::Opacity); 278 controller->GetAnimation(0, Animation::Opacity);
254 if (!animation) 279 if (!animation)
255 return; 280 return;
256 281
257 const FloatAnimationCurve* curve = 282 const FloatAnimationCurve* curve =
258 animation->curve()->ToFloatAnimationCurve(); 283 animation->curve()->ToFloatAnimationCurve();
259 float start_opacity = curve->GetValue(0.0); 284 float start_opacity = curve->GetValue(0.0);
260 float end_opacity = curve->GetValue(curve->Duration()); 285 float end_opacity = curve->GetValue(curve->Duration());
261 float linearly_interpolated_opacity = 286 float linearly_interpolated_opacity =
262 0.25f * end_opacity + 0.75f * start_opacity; 287 0.25f * end_opacity + 0.75f * start_opacity;
263 double time = curve->Duration() * 0.25; 288 double time = curve->Duration() * 0.25;
264 // If the linear timing function associated with this animation was not 289 // If the linear timing function associated with this animation was not
265 // picked up, then the linearly interpolated opacity would be different 290 // picked up, then the linearly interpolated opacity would be different
266 // because of the default ease timing function. 291 // because of the default ease timing function.
267 EXPECT_FLOAT_EQ(linearly_interpolated_opacity, curve->GetValue(time)); 292 EXPECT_FLOAT_EQ(linearly_interpolated_opacity, curve->GetValue(time));
268 293
269 LayerAnimationController* controller_impl = 294 LayerAnimationController* controller_impl =
270 host_impl->RootLayer()->layer_animation_controller(); 295 host_impl->active_tree()->root_layer()->children()[0]->
296 layer_animation_controller();
271 Animation* animation_impl = 297 Animation* animation_impl =
272 controller_impl->GetAnimation(0, Animation::Opacity); 298 controller_impl->GetAnimation(0, Animation::Opacity);
273 299
274 controller->RemoveAnimation(animation->id()); 300 controller->RemoveAnimation(animation->id());
275 controller_impl->RemoveAnimation(animation_impl->id()); 301 controller_impl->RemoveAnimation(animation_impl->id());
276 EndTest(); 302 EndTest();
277 } 303 }
278 304
279 virtual void AfterTest() OVERRIDE {} 305 virtual void AfterTest() OVERRIDE {}
306
307 FakeContentLayerClient client_;
308 scoped_refptr<FakeContentLayer> content_;
280 }; 309 };
281 310
282 SINGLE_AND_MULTI_THREAD_TEST_F( 311 SINGLE_AND_MULTI_THREAD_TEST_F(
283 LayerTreeHostAnimationTestAddAnimationWithTimingFunction); 312 LayerTreeHostAnimationTestAddAnimationWithTimingFunction);
284 313
285 // Ensures that main thread animations have their start times synchronized with 314 // Ensures that main thread animations have their start times synchronized with
286 // impl thread animations. 315 // impl thread animations.
287 class LayerTreeHostAnimationTestSynchronizeAnimationStartTimes 316 class LayerTreeHostAnimationTestSynchronizeAnimationStartTimes
288 : public LayerTreeHostAnimationTest { 317 : public LayerTreeHostAnimationTest {
289 public: 318 public:
290 LayerTreeHostAnimationTestSynchronizeAnimationStartTimes() 319 LayerTreeHostAnimationTestSynchronizeAnimationStartTimes()
291 : main_start_time_(-1.0), 320 : main_start_time_(-1.0),
292 impl_start_time_(-1.0) {} 321 impl_start_time_(-1.0) {}
293 322
323 virtual void SetupTree() OVERRIDE {
324 LayerTreeHostAnimationTest::SetupTree();
325 content_ = FakeContentLayer::Create(&client_);
326 content_->SetBounds(gfx::Size(4, 4));
327 content_->set_layer_animation_delegate(this);
328 layer_tree_host()->root_layer()->AddChild(content_);
329 }
330
294 virtual void BeginTest() OVERRIDE { 331 virtual void BeginTest() OVERRIDE {
295 PostAddAnimationToMainThread(layer_tree_host()->root_layer()); 332 PostAddAnimationToMainThread(content_);
296 } 333 }
297 334
298 virtual void notifyAnimationStarted(double time) OVERRIDE { 335 virtual void notifyAnimationStarted(double time) OVERRIDE {
299 LayerAnimationController* controller = 336 LayerAnimationController* controller =
300 layer_tree_host()->root_layer()->layer_animation_controller(); 337 layer_tree_host()->root_layer()->children()[0]->
338 layer_animation_controller();
301 Animation* animation = 339 Animation* animation =
302 controller->GetAnimation(0, Animation::Opacity); 340 controller->GetAnimation(0, Animation::Opacity);
303 main_start_time_ = animation->start_time(); 341 main_start_time_ = animation->start_time();
304 controller->RemoveAnimation(animation->id()); 342 controller->RemoveAnimation(animation->id());
305 343
306 if (impl_start_time_ > 0.0) 344 if (impl_start_time_ > 0.0)
307 EndTest(); 345 EndTest();
308 } 346 }
309 347
310 virtual void UpdateAnimationState( 348 virtual void UpdateAnimationState(
311 LayerTreeHostImpl* impl_host, 349 LayerTreeHostImpl* impl_host,
312 bool has_unfinished_animation) OVERRIDE { 350 bool has_unfinished_animation) OVERRIDE {
313 LayerAnimationController* controller = 351 LayerAnimationController* controller =
314 impl_host->RootLayer()->layer_animation_controller(); 352 impl_host->active_tree()->root_layer()->children()[0]->
353 layer_animation_controller();
315 Animation* animation = 354 Animation* animation =
316 controller->GetAnimation(0, Animation::Opacity); 355 controller->GetAnimation(0, Animation::Opacity);
317 if (!animation) 356 if (!animation)
318 return; 357 return;
319 358
320 impl_start_time_ = animation->start_time(); 359 impl_start_time_ = animation->start_time();
321 controller->RemoveAnimation(animation->id()); 360 controller->RemoveAnimation(animation->id());
322 361
323 if (main_start_time_ > 0.0) 362 if (main_start_time_ > 0.0)
324 EndTest(); 363 EndTest();
325 } 364 }
326 365
327 virtual void AfterTest() OVERRIDE { 366 virtual void AfterTest() OVERRIDE {
328 EXPECT_FLOAT_EQ(impl_start_time_, main_start_time_); 367 EXPECT_FLOAT_EQ(impl_start_time_, main_start_time_);
329 } 368 }
330 369
331 private: 370 private:
332 double main_start_time_; 371 double main_start_time_;
333 double impl_start_time_; 372 double impl_start_time_;
373 FakeContentLayerClient client_;
374 scoped_refptr<FakeContentLayer> content_;
334 }; 375 };
335 376
336 SINGLE_AND_MULTI_THREAD_TEST_F( 377 SINGLE_AND_MULTI_THREAD_TEST_F(
337 LayerTreeHostAnimationTestSynchronizeAnimationStartTimes); 378 LayerTreeHostAnimationTestSynchronizeAnimationStartTimes);
338 379
339 // Ensures that notify animation finished is called. 380 // Ensures that notify animation finished is called.
340 class LayerTreeHostAnimationTestAnimationFinishedEvents 381 class LayerTreeHostAnimationTestAnimationFinishedEvents
341 : public LayerTreeHostAnimationTest { 382 : public LayerTreeHostAnimationTest {
342 public: 383 public:
343 LayerTreeHostAnimationTestAnimationFinishedEvents() {} 384 LayerTreeHostAnimationTestAnimationFinishedEvents() {}
(...skipping 30 matching lines...) Expand all
374 virtual void SetupTree() OVERRIDE { 415 virtual void SetupTree() OVERRIDE {
375 update_check_layer_->SetOpacity(0.f); 416 update_check_layer_->SetOpacity(0.f);
376 layer_tree_host()->SetRootLayer(update_check_layer_); 417 layer_tree_host()->SetRootLayer(update_check_layer_);
377 LayerTreeHostAnimationTest::SetupTree(); 418 LayerTreeHostAnimationTest::SetupTree();
378 } 419 }
379 420
380 virtual void BeginTest() OVERRIDE { 421 virtual void BeginTest() OVERRIDE {
381 PostAddAnimationToMainThread(update_check_layer_.get()); 422 PostAddAnimationToMainThread(update_check_layer_.get());
382 } 423 }
383 424
384 virtual void CommitCompleteOnThread(LayerTreeHostImpl* tree_impl) OVERRIDE { 425 virtual void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
426 LayerAnimationController* controller_impl =
427 host_impl->active_tree()->root_layer()->layer_animation_controller();
428 Animation* animation_impl =
429 controller_impl->GetAnimation(0, Animation::Opacity);
430 controller_impl->RemoveAnimation(animation_impl->id());
385 EndTest(); 431 EndTest();
386 } 432 }
387 433
388 virtual void AfterTest() OVERRIDE { 434 virtual void AfterTest() OVERRIDE {
389 // Update() should have been called once, proving that the layer was not 435 // Update() should have been called once, proving that the layer was not
390 // skipped. 436 // skipped.
391 EXPECT_EQ(1u, update_check_layer_->update_count()); 437 EXPECT_EQ(1u, update_check_layer_->update_count());
392 438
393 // clear update_check_layer_ so LayerTreeHost dies. 439 // clear update_check_layer_ so LayerTreeHost dies.
394 update_check_layer_ = NULL; 440 update_check_layer_ = NULL;
395 } 441 }
396 442
397 private: 443 private:
398 FakeContentLayerClient client_; 444 FakeContentLayerClient client_;
399 scoped_refptr<FakeContentLayer> update_check_layer_; 445 scoped_refptr<FakeContentLayer> update_check_layer_;
400 }; 446 };
401 447
402 MULTI_THREAD_TEST_F( 448 SINGLE_AND_MULTI_THREAD_TEST_F(
403 LayerTreeHostAnimationTestDoNotSkipLayersWithAnimatedOpacity); 449 LayerTreeHostAnimationTestDoNotSkipLayersWithAnimatedOpacity);
404 450
405 // Layers added to tree with existing active animations should have the 451 // Layers added to tree with existing active animations should have the
406 // animation correctly recognized. 452 // animation correctly recognized.
407 class LayerTreeHostAnimationTestLayerAddedWithAnimation 453 class LayerTreeHostAnimationTestLayerAddedWithAnimation
408 : public LayerTreeHostAnimationTest { 454 : public LayerTreeHostAnimationTest {
409 public: 455 public:
410 LayerTreeHostAnimationTestLayerAddedWithAnimation() {} 456 LayerTreeHostAnimationTestLayerAddedWithAnimation() {}
411 457
412 virtual void BeginTest() OVERRIDE { 458 virtual void BeginTest() OVERRIDE {
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 EXPECT_EQ(1, num_commit_complete_); 576 EXPECT_EQ(1, num_commit_complete_);
531 } 577 }
532 578
533 private: 579 private:
534 int num_commit_complete_; 580 int num_commit_complete_;
535 int num_draw_layers_; 581 int num_draw_layers_;
536 }; 582 };
537 583
538 MULTI_THREAD_TEST_F(LayerTreeHostAnimationTestContinuousAnimate); 584 MULTI_THREAD_TEST_F(LayerTreeHostAnimationTestContinuousAnimate);
539 585
586 // Make sure the main thread can still execute animations when CanDraw() is not
587 // true.
588 class LayerTreeHostAnimationTestRunAnimationWhenNotCanDraw
589 : public LayerTreeHostAnimationTest {
590 public:
591 LayerTreeHostAnimationTestRunAnimationWhenNotCanDraw() : started_times_(0) {}
592
593 virtual void SetupTree() OVERRIDE {
594 LayerTreeHostAnimationTest::SetupTree();
595 content_ = FakeContentLayer::Create(&client_);
596 content_->SetBounds(gfx::Size(4, 4));
597 content_->set_layer_animation_delegate(this);
598 layer_tree_host()->root_layer()->AddChild(content_);
599 }
600
601 virtual void BeginTest() OVERRIDE {
602 layer_tree_host()->SetViewportSize(gfx::Size(), gfx::Size());
603 PostAddAnimationToMainThread(content_);
604 }
605
606 virtual void notifyAnimationStarted(double wall_clock_time) OVERRIDE {
607 started_times_++;
608 }
609
610 virtual void notifyAnimationFinished(double wall_clock_time) OVERRIDE {
611 EXPECT_EQ(1, started_times_);
612 EndTest();
613 }
614
615 virtual void AfterTest() OVERRIDE {}
616
617 private:
618 int started_times_;
619 FakeContentLayerClient client_;
620 scoped_refptr<FakeContentLayer> content_;
621 };
622
623 SINGLE_AND_MULTI_THREAD_TEST_F(
624 LayerTreeHostAnimationTestRunAnimationWhenNotCanDraw);
625
626 // Make sure the main thread can still execute animations when the renderer is
627 // backgrounded.
628 class LayerTreeHostAnimationTestRunAnimationWhenNotVisible
629 : public LayerTreeHostAnimationTest {
630 public:
631 LayerTreeHostAnimationTestRunAnimationWhenNotVisible() : started_times_(0) {}
632
633 virtual void SetupTree() OVERRIDE {
634 LayerTreeHostAnimationTest::SetupTree();
635 content_ = FakeContentLayer::Create(&client_);
636 content_->SetBounds(gfx::Size(4, 4));
637 content_->set_layer_animation_delegate(this);
638 layer_tree_host()->root_layer()->AddChild(content_);
639 }
640
641 virtual void BeginTest() OVERRIDE {
642 visible_ = true;
643 PostAddAnimationToMainThread(content_);
644 }
645
646 virtual void DidCommit() OVERRIDE {
647 visible_ = false;
648 layer_tree_host()->SetVisible(false);
649 }
650
651 virtual void notifyAnimationStarted(double wall_clock_time) OVERRIDE {
652 EXPECT_FALSE(visible_);
653 started_times_++;
654 }
655
656 virtual void notifyAnimationFinished(double wall_clock_time) OVERRIDE {
657 EXPECT_FALSE(visible_);
658 EXPECT_EQ(1, started_times_);
659 EndTest();
660 }
661
662 virtual void AfterTest() OVERRIDE {}
663
664 private:
665 bool visible_;
666 int started_times_;
667 FakeContentLayerClient client_;
668 scoped_refptr<FakeContentLayer> content_;
669 };
670
671 SINGLE_AND_MULTI_THREAD_TEST_F(
672 LayerTreeHostAnimationTestRunAnimationWhenNotVisible);
673
540 } // namespace 674 } // namespace
541 } // namespace cc 675 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698