| 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/canvas_skia.h" | 13 #include "ui/gfx/canvas_skia.h" |
| 12 #include "ui/gfx/point3.h" | 14 #include "ui/gfx/point3.h" |
| 13 | 15 |
| 14 namespace ui { | 16 namespace ui { |
| 15 | 17 |
| 16 Layer::Layer(Compositor* compositor) | 18 Layer::Layer(Compositor* compositor) |
| 17 : type_(LAYER_HAS_TEXTURE), | 19 : type_(LAYER_HAS_TEXTURE), |
| 18 compositor_(compositor), | 20 compositor_(compositor), |
| 19 parent_(NULL), | 21 parent_(NULL), |
| 20 visible_(true), | 22 visible_(true), |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 } | 81 } |
| 80 | 82 |
| 81 bool Layer::Contains(const Layer* other) const { | 83 bool Layer::Contains(const Layer* other) const { |
| 82 for (const Layer* parent = other; parent; parent = parent->parent()) { | 84 for (const Layer* parent = other; parent; parent = parent->parent()) { |
| 83 if (parent == this) | 85 if (parent == this) |
| 84 return true; | 86 return true; |
| 85 } | 87 } |
| 86 return false; | 88 return false; |
| 87 } | 89 } |
| 88 | 90 |
| 91 void Layer::SetAnimation(Animation* animation) { |
| 92 if (animation) { |
| 93 if (!animator_.get()) |
| 94 animator_.reset(new LayerAnimator(this)); |
| 95 animation->Start(); |
| 96 animator_->SetAnimation(animation); |
| 97 } else { |
| 98 animator_.reset(); |
| 99 } |
| 100 } |
| 101 |
| 89 void Layer::SetTransform(const ui::Transform& transform) { | 102 void Layer::SetTransform(const ui::Transform& transform) { |
| 90 transform_ = transform; | 103 StopAnimatingIfNecessary(LayerAnimator::TRANSFORM); |
| 91 | 104 if (animator_.get() && animator_->IsRunning()) { |
| 92 if (parent() && fills_bounds_opaquely_) | 105 animator_->AnimateTransform(transform); |
| 93 parent()->RecomputeHole(); | 106 return; |
| 107 } |
| 108 SetTransformImmediately(transform); |
| 94 } | 109 } |
| 95 | 110 |
| 96 void Layer::SetBounds(const gfx::Rect& bounds) { | 111 void Layer::SetBounds(const gfx::Rect& bounds) { |
| 97 bounds_ = bounds; | 112 StopAnimatingIfNecessary(LayerAnimator::LOCATION); |
| 113 if (animator_.get() && animator_->IsRunning() && |
| 114 bounds.size() == bounds_.size()) { |
| 115 animator_->AnimateToPoint(bounds.origin()); |
| 116 return; |
| 117 } |
| 118 SetBoundsImmediately(bounds); |
| 119 } |
| 98 | 120 |
| 99 if (parent() && fills_bounds_opaquely_) | 121 void Layer::SetOpacity(float opacity) { |
| 100 parent()->RecomputeHole(); | 122 StopAnimatingIfNecessary(LayerAnimator::OPACITY); |
| 123 if (animator_.get() && animator_->IsRunning()) { |
| 124 animator_->AnimateOpacity(opacity); |
| 125 return; |
| 126 } |
| 127 SetOpacityImmediately(opacity); |
| 101 } | 128 } |
| 102 | 129 |
| 103 void Layer::SetVisible(bool visible) { | 130 void Layer::SetVisible(bool visible) { |
| 104 visible_ = visible; | 131 visible_ = visible; |
| 105 if (!visible_) | 132 if (!visible_) |
| 106 DropTextures(); | 133 DropTextures(); |
| 107 } | 134 } |
| 108 | 135 |
| 109 bool Layer::ShouldDraw() { | 136 bool Layer::ShouldDraw() { |
| 110 return type_ == LAYER_HAS_TEXTURE && GetCombinedOpacity() > 0.0f && | 137 return type_ == LAYER_HAS_TEXTURE && GetCombinedOpacity() > 0.0f && |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 | 255 |
| 229 void Layer::DrawTree() { | 256 void Layer::DrawTree() { |
| 230 if (!visible_) | 257 if (!visible_) |
| 231 return; | 258 return; |
| 232 | 259 |
| 233 Draw(); | 260 Draw(); |
| 234 for (size_t i = 0; i < children_.size(); ++i) | 261 for (size_t i = 0; i < children_.size(); ++i) |
| 235 children_.at(i)->DrawTree(); | 262 children_.at(i)->DrawTree(); |
| 236 } | 263 } |
| 237 | 264 |
| 238 void Layer::SetOpacity(float alpha) { | |
| 239 bool was_opaque = GetCombinedOpacity() == 1.0f; | |
| 240 opacity_ = alpha; | |
| 241 bool is_opaque = GetCombinedOpacity() == 1.0f; | |
| 242 | |
| 243 // If our opacity has changed we need to recompute our hole, our parent's hole | |
| 244 // and the holes of all our descendants. | |
| 245 if (was_opaque != is_opaque) { | |
| 246 if (parent_) | |
| 247 parent_->RecomputeHole(); | |
| 248 std::queue<Layer*> to_process; | |
| 249 to_process.push(this); | |
| 250 while (!to_process.empty()) { | |
| 251 Layer* current = to_process.front(); | |
| 252 to_process.pop(); | |
| 253 current->RecomputeHole(); | |
| 254 for (size_t i = 0; i < current->children_.size(); ++i) | |
| 255 to_process.push(current->children_.at(i)); | |
| 256 } | |
| 257 } | |
| 258 } | |
| 259 | |
| 260 float Layer::GetCombinedOpacity() const { | 265 float Layer::GetCombinedOpacity() const { |
| 261 float opacity = opacity_; | 266 float opacity = opacity_; |
| 262 Layer* current = this->parent_; | 267 Layer* current = this->parent_; |
| 263 while (current) { | 268 while (current) { |
| 264 opacity *= current->opacity_; | 269 opacity *= current->opacity_; |
| 265 current = current->parent_; | 270 current = current->parent_; |
| 266 } | 271 } |
| 267 return opacity; | 272 return opacity; |
| 268 } | 273 } |
| 269 | 274 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 const Layer* p = this; | 349 const Layer* p = this; |
| 345 for (; p && p != ancestor; p = p->parent()) { | 350 for (; p && p != ancestor; p = p->parent()) { |
| 346 if (p->transform().HasChange()) | 351 if (p->transform().HasChange()) |
| 347 transform->ConcatTransform(p->transform()); | 352 transform->ConcatTransform(p->transform()); |
| 348 transform->ConcatTranslate(static_cast<float>(p->bounds().x()), | 353 transform->ConcatTranslate(static_cast<float>(p->bounds().x()), |
| 349 static_cast<float>(p->bounds().y())); | 354 static_cast<float>(p->bounds().y())); |
| 350 } | 355 } |
| 351 return p == ancestor; | 356 return p == ancestor; |
| 352 } | 357 } |
| 353 | 358 |
| 359 void Layer::StopAnimatingIfNecessary( |
| 360 LayerAnimator::AnimationProperty property) { |
| 361 if (!animator_.get() || !animator_->IsRunning() || |
| 362 !animator_->got_initial_tick()) { |
| 363 return; |
| 364 } |
| 365 |
| 366 if (property != LayerAnimator::LOCATION && |
| 367 animator_->IsAnimating(LayerAnimator::LOCATION)) { |
| 368 SetBoundsImmediately( |
| 369 gfx::Rect(animator_->GetTargetPoint(), bounds_.size())); |
| 370 } |
| 371 if (property != LayerAnimator::OPACITY && |
| 372 animator_->IsAnimating(LayerAnimator::OPACITY)) { |
| 373 SetOpacityImmediately(animator_->GetTargetOpacity()); |
| 374 } |
| 375 if (property != LayerAnimator::TRANSFORM && |
| 376 animator_->IsAnimating(LayerAnimator::TRANSFORM)) { |
| 377 SetTransformImmediately(animator_->GetTargetTransform()); |
| 378 } |
| 379 animator_.reset(); |
| 380 } |
| 381 |
| 382 void Layer::SetBoundsImmediately(const gfx::Rect& bounds) { |
| 383 bounds_ = bounds; |
| 384 |
| 385 if (parent() && fills_bounds_opaquely_) |
| 386 parent()->RecomputeHole(); |
| 387 } |
| 388 |
| 389 void Layer::SetTransformImmediately(const ui::Transform& transform) { |
| 390 transform_ = transform; |
| 391 |
| 392 if (parent() && fills_bounds_opaquely_) |
| 393 parent()->RecomputeHole(); |
| 394 } |
| 395 |
| 396 void Layer::SetOpacityImmediately(float opacity) { |
| 397 bool was_opaque = GetCombinedOpacity() == 1.0f; |
| 398 opacity_ = opacity; |
| 399 bool is_opaque = GetCombinedOpacity() == 1.0f; |
| 400 |
| 401 // If our opacity has changed we need to recompute our hole, our parent's hole |
| 402 // and the holes of all our descendants. |
| 403 if (was_opaque != is_opaque) { |
| 404 if (parent_) |
| 405 parent_->RecomputeHole(); |
| 406 std::queue<Layer*> to_process; |
| 407 to_process.push(this); |
| 408 while (!to_process.empty()) { |
| 409 Layer* current = to_process.front(); |
| 410 to_process.pop(); |
| 411 current->RecomputeHole(); |
| 412 for (size_t i = 0; i < current->children_.size(); ++i) |
| 413 to_process.push(current->children_.at(i)); |
| 414 } |
| 415 } |
| 416 } |
| 417 |
| 418 void Layer::SetBoundsFromAnimator(const gfx::Rect& bounds) { |
| 419 SetBoundsImmediately(bounds); |
| 420 } |
| 421 |
| 422 void Layer::SetTransformFromAnimator(const Transform& transform) { |
| 423 SetTransformImmediately(transform); |
| 424 } |
| 425 |
| 426 void Layer::SetOpacityFromAnimator(float opacity) { |
| 427 SetOpacityImmediately(opacity); |
| 428 } |
| 429 |
| 354 } // namespace ui | 430 } // namespace ui |
| OLD | NEW |