Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ui/gfx/compositor/layer.h" | 5 #include "ui/gfx/compositor/layer.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "ui/base/animation/animation.h" | |
| 12 #include "ui/gfx/compositor/layer_animator.h" | 11 #include "ui/gfx/compositor/layer_animator.h" |
| 13 #include "ui/gfx/canvas_skia.h" | 12 #include "ui/gfx/canvas_skia.h" |
| 14 #include "ui/gfx/interpolated_transform.h" | 13 #include "ui/gfx/interpolated_transform.h" |
| 15 #include "ui/gfx/point3.h" | 14 #include "ui/gfx/point3.h" |
| 16 | 15 |
| 17 namespace { | 16 namespace { |
| 18 | 17 |
| 19 const float EPSILON = 1e-3f; | 18 const float EPSILON = 1e-3f; |
| 20 | 19 |
| 21 bool IsApproximateMultilpleOf(float value, float base) { | 20 bool IsApproximateMultilpleOf(float value, float base) { |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 99 } | 98 } |
| 100 | 99 |
| 101 bool Layer::Contains(const Layer* other) const { | 100 bool Layer::Contains(const Layer* other) const { |
| 102 for (const Layer* parent = other; parent; parent = parent->parent()) { | 101 for (const Layer* parent = other; parent; parent = parent->parent()) { |
| 103 if (parent == this) | 102 if (parent == this) |
| 104 return true; | 103 return true; |
| 105 } | 104 } |
| 106 return false; | 105 return false; |
| 107 } | 106 } |
| 108 | 107 |
| 109 void Layer::SetAnimation(Animation* animation) { | 108 void Layer::SetAnimator(LayerAnimator* animator) { |
| 110 if (animation) { | 109 animator->SetDelegate(this); |
| 111 if (!animator_.get()) | 110 animator_.reset(animator); |
| 112 animator_.reset(new LayerAnimator(this)); | 111 } |
| 113 animation->Start(); | 112 |
| 114 animator_->SetAnimation(animation); | 113 LayerAnimator* Layer::GetAnimator() { |
| 115 } else { | 114 if (!animator_.get()) { |
|
sky
2011/10/19 00:06:33
no {}
| |
| 116 animator_.reset(); | 115 SetAnimator(LayerAnimator::CreateDefaultAnimator()); |
| 117 } | 116 } |
| 117 return animator_.get(); | |
| 118 } | 118 } |
| 119 | 119 |
| 120 void Layer::SetTransform(const ui::Transform& transform) { | 120 void Layer::SetTransform(const ui::Transform& transform) { |
| 121 StopAnimatingIfNecessary(LayerAnimator::TRANSFORM); | 121 if (animator_.get()) |
| 122 if (animator_.get() && animator_->IsRunning()) { | 122 animator_->SetTransform(transform); |
| 123 animator_->AnimateTransform(transform); | 123 else |
| 124 return; | 124 SetTransformImmediately(transform); |
| 125 } | |
| 126 SetTransformImmediately(transform); | |
| 127 } | 125 } |
| 128 | 126 |
| 129 void Layer::SetBounds(const gfx::Rect& bounds) { | 127 void Layer::SetBounds(const gfx::Rect& bounds) { |
| 130 StopAnimatingIfNecessary(LayerAnimator::LOCATION); | 128 if (animator_.get()) |
| 131 if (animator_.get() && animator_->IsRunning() && | 129 animator_->SetBounds(bounds); |
| 132 bounds.size() == bounds_.size()) { | 130 else |
| 133 animator_->AnimateToPoint(bounds.origin()); | 131 SetBoundsImmediately(bounds); |
| 134 return; | |
| 135 } | |
| 136 SetBoundsImmediately(bounds); | |
| 137 } | 132 } |
| 138 | 133 |
| 139 void Layer::SetOpacity(float opacity) { | 134 void Layer::SetOpacity(float opacity) { |
| 140 StopAnimatingIfNecessary(LayerAnimator::OPACITY); | 135 if (animator_.get()) |
| 141 if (animator_.get() && animator_->IsRunning()) { | 136 animator_->SetOpacity(opacity); |
| 142 animator_->AnimateOpacity(opacity); | 137 else |
| 143 return; | 138 SetOpacityImmediately(opacity); |
| 144 } | |
| 145 SetOpacityImmediately(opacity); | |
| 146 } | 139 } |
| 147 | 140 |
| 148 void Layer::SetVisible(bool visible) { | 141 void Layer::SetVisible(bool visible) { |
| 149 if (visible_ == visible) | 142 if (visible_ == visible) |
| 150 return; | 143 return; |
| 151 | 144 |
| 152 bool was_drawn = IsDrawn(); | 145 bool was_drawn = IsDrawn(); |
| 153 visible_ = visible; | 146 visible_ = visible; |
| 154 bool is_drawn = IsDrawn(); | 147 bool is_drawn = IsDrawn(); |
| 155 if (was_drawn == is_drawn) | 148 if (was_drawn == is_drawn) |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 343 // of any importance, so take the intersection. | 336 // of any importance, so take the intersection. |
| 344 candidate_hole = bounds().Intersect(candidate_hole); | 337 candidate_hole = bounds().Intersect(candidate_hole); |
| 345 | 338 |
| 346 // Ensure we have the largest hole. | 339 // Ensure we have the largest hole. |
| 347 if (candidate_hole.size().GetArea() > hole_rect_.size().GetArea()) | 340 if (candidate_hole.size().GetArea() > hole_rect_.size().GetArea()) |
| 348 hole_rect_ = candidate_hole; | 341 hole_rect_ = candidate_hole; |
| 349 } | 342 } |
| 350 | 343 |
| 351 // Free up texture memory if the hole fills bounds of layer. | 344 // Free up texture memory if the hole fills bounds of layer. |
| 352 if (!ShouldDraw() && !layer_updated_externally_) | 345 if (!ShouldDraw() && !layer_updated_externally_) |
| 353 texture_ = NULL; | 346 DropTextures(); |
|
sky
2011/10/19 00:06:33
Why do we want to do drop all child textures here?
| |
| 354 } | 347 } |
| 355 | 348 |
| 356 bool Layer::IsCompletelyOpaque() const { | 349 bool Layer::IsCompletelyOpaque() const { |
| 357 return fills_bounds_opaquely() && GetCombinedOpacity() == 1.0f; | 350 return fills_bounds_opaquely() && GetCombinedOpacity() == 1.0f; |
| 358 } | 351 } |
| 359 | 352 |
| 360 // static | 353 // static |
| 361 void Layer::PunchHole(const gfx::Rect& rect, | 354 void Layer::PunchHole(const gfx::Rect& rect, |
| 362 const gfx::Rect& region_to_punch_out, | 355 const gfx::Rect& region_to_punch_out, |
| 363 std::vector<gfx::Rect>* sides) { | 356 std::vector<gfx::Rect>* sides) { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 425 const Layer* p = this; | 418 const Layer* p = this; |
| 426 for (; p && p != ancestor; p = p->parent()) { | 419 for (; p && p != ancestor; p = p->parent()) { |
| 427 if (p->transform().HasChange()) | 420 if (p->transform().HasChange()) |
| 428 transform->ConcatTransform(p->transform()); | 421 transform->ConcatTransform(p->transform()); |
| 429 transform->ConcatTranslate(static_cast<float>(p->bounds().x()), | 422 transform->ConcatTranslate(static_cast<float>(p->bounds().x()), |
| 430 static_cast<float>(p->bounds().y())); | 423 static_cast<float>(p->bounds().y())); |
| 431 } | 424 } |
| 432 return p == ancestor; | 425 return p == ancestor; |
| 433 } | 426 } |
| 434 | 427 |
| 435 void Layer::StopAnimatingIfNecessary( | |
| 436 LayerAnimator::AnimationProperty property) { | |
| 437 if (!animator_.get() || !animator_->IsRunning() || | |
| 438 !animator_->got_initial_tick()) { | |
| 439 return; | |
| 440 } | |
| 441 | |
| 442 if (property != LayerAnimator::LOCATION && | |
| 443 animator_->IsAnimating(LayerAnimator::LOCATION)) { | |
| 444 SetBoundsImmediately( | |
| 445 gfx::Rect(animator_->GetTargetPoint(), bounds_.size())); | |
| 446 } | |
| 447 if (property != LayerAnimator::OPACITY && | |
| 448 animator_->IsAnimating(LayerAnimator::OPACITY)) { | |
| 449 SetOpacityImmediately(animator_->GetTargetOpacity()); | |
| 450 } | |
| 451 if (property != LayerAnimator::TRANSFORM && | |
| 452 animator_->IsAnimating(LayerAnimator::TRANSFORM)) { | |
| 453 SetTransformImmediately(animator_->GetTargetTransform()); | |
| 454 } | |
| 455 animator_.reset(); | |
| 456 } | |
| 457 | |
| 458 void Layer::SetBoundsImmediately(const gfx::Rect& bounds) { | 428 void Layer::SetBoundsImmediately(const gfx::Rect& bounds) { |
| 459 bounds_ = bounds; | 429 bounds_ = bounds; |
| 460 | 430 |
| 461 if (parent()) | 431 if (parent()) |
| 462 parent()->RecomputeHole(); | 432 parent()->RecomputeHole(); |
| 463 } | 433 } |
| 464 | 434 |
| 465 void Layer::SetTransformImmediately(const ui::Transform& transform) { | 435 void Layer::SetTransformImmediately(const ui::Transform& transform) { |
| 466 transform_ = transform; | 436 transform_ = transform; |
| 467 | 437 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 484 while (!to_process.empty()) { | 454 while (!to_process.empty()) { |
| 485 Layer* current = to_process.front(); | 455 Layer* current = to_process.front(); |
| 486 to_process.pop(); | 456 to_process.pop(); |
| 487 current->RecomputeHole(); | 457 current->RecomputeHole(); |
| 488 for (size_t i = 0; i < current->children_.size(); ++i) | 458 for (size_t i = 0; i < current->children_.size(); ++i) |
| 489 to_process.push(current->children_.at(i)); | 459 to_process.push(current->children_.at(i)); |
| 490 } | 460 } |
| 491 } | 461 } |
| 492 } | 462 } |
| 493 | 463 |
| 494 void Layer::SetBoundsFromAnimator(const gfx::Rect& bounds) { | 464 void Layer::SetBoundsFromAnimation(const gfx::Rect& bounds) { |
| 495 SetBoundsImmediately(bounds); | 465 SetBoundsImmediately(bounds); |
| 496 } | 466 } |
| 497 | 467 |
| 498 void Layer::SetTransformFromAnimator(const Transform& transform) { | 468 void Layer::SetTransformFromAnimation(const Transform& transform) { |
| 499 SetTransformImmediately(transform); | 469 SetTransformImmediately(transform); |
| 500 } | 470 } |
| 501 | 471 |
| 502 void Layer::SetOpacityFromAnimator(float opacity) { | 472 void Layer::SetOpacityFromAnimation(float opacity) { |
| 503 SetOpacityImmediately(opacity); | 473 SetOpacityImmediately(opacity); |
| 504 } | 474 } |
| 505 | 475 |
| 476 void Layer::ScheduleDrawForAnimation() { | |
| 477 ScheduleDraw(); | |
| 478 } | |
| 479 | |
| 480 const gfx::Rect& Layer::GetBoundsForAnimation() const { | |
| 481 return bounds(); | |
| 482 } | |
| 483 | |
| 484 const Transform& Layer::GetTransformForAnimation() const { | |
| 485 return transform(); | |
| 486 } | |
| 487 | |
| 488 const float Layer::GetOpacityForAnimation() const { | |
| 489 return opacity(); | |
| 490 } | |
| 491 | |
| 506 } // namespace ui | 492 } // namespace ui |
| OLD | NEW |