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 "third_party/WebKit/Source/WebKit/chromium/public/WebContentLayer.h" | 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContentLayer.h" |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebExternalTextureLay
er.h" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebExternalTextureLay
er.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFloatPoint.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFloatPoint.h" |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFloatRect.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFloatRect.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" |
16 #include "ui/base/animation/animation.h" | 16 #include "ui/base/animation/animation.h" |
17 #if defined(USE_WEBKIT_COMPOSITOR) | 17 #if defined(USE_WEBKIT_COMPOSITOR) |
18 #include "ui/gfx/compositor/compositor_cc.h" | 18 #include "ui/gfx/compositor/compositor_cc.h" |
19 #endif | 19 #endif |
20 #include "ui/gfx/canvas_skia.h" | 20 #include "ui/gfx/canvas_skia.h" |
21 #include "ui/gfx/compositor/layer_animator.h" | 21 #include "ui/gfx/compositor/layer_animation_manager.h" |
22 #include "ui/gfx/interpolated_transform.h" | 22 #include "ui/gfx/interpolated_transform.h" |
23 #include "ui/gfx/point3.h" | 23 #include "ui/gfx/point3.h" |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 const float EPSILON = 1e-3f; | 27 const float EPSILON = 1e-3f; |
28 | 28 |
29 bool IsApproximateMultilpleOf(float value, float base) { | 29 bool IsApproximateMultilpleOf(float value, float base) { |
30 float remainder = fmod(fabs(value), base); | 30 float remainder = fmod(fabs(value), base); |
31 return remainder < EPSILON || base - remainder < EPSILON; | 31 return remainder < EPSILON || base - remainder < EPSILON; |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 } | 128 } |
129 | 129 |
130 bool Layer::Contains(const Layer* other) const { | 130 bool Layer::Contains(const Layer* other) const { |
131 for (const Layer* parent = other; parent; parent = parent->parent()) { | 131 for (const Layer* parent = other; parent; parent = parent->parent()) { |
132 if (parent == this) | 132 if (parent == this) |
133 return true; | 133 return true; |
134 } | 134 } |
135 return false; | 135 return false; |
136 } | 136 } |
137 | 137 |
138 void Layer::SetAnimator(LayerAnimator* animator) { | 138 void Layer::SetAnimation(Animation* animation) { |
139 if (animator) | 139 if (animation) { |
140 animator->SetDelegate(this); | 140 if (!animator_.get()) |
141 animator_.reset(animator); | 141 animator_.reset(new LayerAnimationManager(this)); |
142 } | 142 animation->Start(); |
143 | 143 animator_->SetAnimation(animation); |
144 LayerAnimator* Layer::GetAnimator() { | 144 } else { |
145 if (!animator_.get()) | 145 animator_.reset(); |
146 SetAnimator(LayerAnimator::CreateDefaultAnimator()); | 146 } |
147 return animator_.get(); | |
148 } | 147 } |
149 | 148 |
150 void Layer::SetTransform(const ui::Transform& transform) { | 149 void Layer::SetTransform(const ui::Transform& transform) { |
151 GetAnimator()->SetTransform(transform); | 150 StopAnimatingIfNecessary(LayerAnimationManager::TRANSFORM); |
152 } | 151 if (animator_.get() && animator_->IsRunning()) { |
153 | 152 animator_->AnimateTransform(transform); |
154 Transform Layer::GetTargetTransform() const { | 153 return; |
155 if (animator_.get() && animator_->is_animating()) | 154 } |
156 return animator_->GetTargetTransform(); | 155 SetTransformImmediately(transform); |
157 return transform_; | |
158 } | 156 } |
159 | 157 |
160 void Layer::SetBounds(const gfx::Rect& bounds) { | 158 void Layer::SetBounds(const gfx::Rect& bounds) { |
161 GetAnimator()->SetBounds(bounds); | 159 StopAnimatingIfNecessary(LayerAnimationManager::LOCATION); |
| 160 if (animator_.get() && animator_->IsRunning() && |
| 161 bounds.size() == bounds_.size()) { |
| 162 animator_->AnimateToPoint(bounds.origin()); |
| 163 return; |
| 164 } |
| 165 SetBoundsImmediately(bounds); |
162 } | 166 } |
163 | 167 |
164 gfx::Rect Layer::GetTargetBounds() const { | 168 gfx::Rect Layer::GetTargetBounds() const { |
165 if (animator_.get() && animator_->is_animating()) | 169 if (animator_.get() && animator_->IsRunning()) |
166 return animator_->GetTargetBounds(); | 170 return gfx::Rect(animator_->GetTargetPoint(), bounds_.size()); |
167 return bounds_; | 171 return bounds_; |
168 } | 172 } |
169 | 173 |
170 void Layer::SetOpacity(float opacity) { | 174 void Layer::SetOpacity(float opacity) { |
171 GetAnimator()->SetOpacity(opacity); | 175 StopAnimatingIfNecessary(LayerAnimationManager::OPACITY); |
172 } | 176 if (animator_.get() && animator_->IsRunning()) { |
173 | 177 animator_->AnimateOpacity(opacity); |
174 float Layer::GetTargetOpacity() const { | 178 return; |
175 if (animator_.get() && animator_->is_animating()) | 179 } |
176 return animator_->GetTargetOpacity(); | 180 SetOpacityImmediately(opacity); |
177 return opacity_; | |
178 } | 181 } |
179 | 182 |
180 void Layer::SetVisible(bool visible) { | 183 void Layer::SetVisible(bool visible) { |
181 if (visible_ == visible) | 184 if (visible_ == visible) |
182 return; | 185 return; |
183 | 186 |
184 bool was_drawn = IsDrawn(); | 187 bool was_drawn = IsDrawn(); |
185 visible_ = visible; | 188 visible_ = visible; |
186 #if defined(USE_WEBKIT_COMPOSITOR) | 189 #if defined(USE_WEBKIT_COMPOSITOR) |
187 // TODO(piman): Expose a visibility flag on WebLayer. | 190 // TODO(piman): Expose a visibility flag on WebLayer. |
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
586 const Layer* p = this; | 589 const Layer* p = this; |
587 for (; p && p != ancestor; p = p->parent()) { | 590 for (; p && p != ancestor; p = p->parent()) { |
588 if (p->transform().HasChange()) | 591 if (p->transform().HasChange()) |
589 transform->ConcatTransform(p->transform()); | 592 transform->ConcatTransform(p->transform()); |
590 transform->ConcatTranslate(static_cast<float>(p->bounds().x()), | 593 transform->ConcatTranslate(static_cast<float>(p->bounds().x()), |
591 static_cast<float>(p->bounds().y())); | 594 static_cast<float>(p->bounds().y())); |
592 } | 595 } |
593 return p == ancestor; | 596 return p == ancestor; |
594 } | 597 } |
595 | 598 |
| 599 void Layer::StopAnimatingIfNecessary( |
| 600 LayerAnimationManager::AnimationProperty property) { |
| 601 if (!animator_.get() || !animator_->IsRunning() || |
| 602 !animator_->got_initial_tick()) { |
| 603 return; |
| 604 } |
| 605 |
| 606 if (property != LayerAnimationManager::LOCATION && |
| 607 animator_->IsAnimating(LayerAnimationManager::LOCATION)) { |
| 608 SetBoundsImmediately( |
| 609 gfx::Rect(animator_->GetTargetPoint(), bounds_.size())); |
| 610 } |
| 611 if (property != LayerAnimationManager::OPACITY && |
| 612 animator_->IsAnimating(LayerAnimationManager::OPACITY)) { |
| 613 SetOpacityImmediately(animator_->GetTargetOpacity()); |
| 614 } |
| 615 if (property != LayerAnimationManager::TRANSFORM && |
| 616 animator_->IsAnimating(LayerAnimationManager::TRANSFORM)) { |
| 617 SetTransformImmediately(animator_->GetTargetTransform()); |
| 618 } |
| 619 animator_.reset(); |
| 620 } |
| 621 |
596 void Layer::SetBoundsImmediately(const gfx::Rect& bounds) { | 622 void Layer::SetBoundsImmediately(const gfx::Rect& bounds) { |
597 bounds_ = bounds; | 623 bounds_ = bounds; |
598 | 624 |
599 SetNeedsToRecomputeHole(); | 625 SetNeedsToRecomputeHole(); |
600 #if defined(USE_WEBKIT_COMPOSITOR) | 626 #if defined(USE_WEBKIT_COMPOSITOR) |
601 web_layer_.setBounds(bounds.size()); | 627 web_layer_.setBounds(bounds.size()); |
602 RecomputeTransform(); | 628 RecomputeTransform(); |
603 RecomputeDrawsContent(); | 629 RecomputeDrawsContent(); |
604 #endif | 630 #endif |
605 } | 631 } |
606 | 632 |
607 void Layer::SetTransformImmediately(const ui::Transform& transform) { | 633 void Layer::SetTransformImmediately(const ui::Transform& transform) { |
608 transform_ = transform; | 634 transform_ = transform; |
609 | 635 |
610 SetNeedsToRecomputeHole(); | 636 SetNeedsToRecomputeHole(); |
611 #if defined(USE_WEBKIT_COMPOSITOR) | 637 #if defined(USE_WEBKIT_COMPOSITOR) |
612 RecomputeTransform(); | 638 RecomputeTransform(); |
613 #endif | 639 #endif |
614 } | 640 } |
615 | 641 |
616 void Layer::SetOpacityImmediately(float opacity) { | 642 void Layer::SetOpacityImmediately(float opacity) { |
617 opacity_ = opacity; | 643 opacity_ = opacity; |
618 SetNeedsToRecomputeHole(); | 644 SetNeedsToRecomputeHole(); |
619 #if defined(USE_WEBKIT_COMPOSITOR) | 645 #if defined(USE_WEBKIT_COMPOSITOR) |
620 if (visible_) | 646 if (visible_) |
621 web_layer_.setOpacity(opacity); | 647 web_layer_.setOpacity(opacity); |
622 #endif | 648 #endif |
623 } | 649 } |
624 | 650 |
625 void Layer::SetBoundsFromAnimation(const gfx::Rect& bounds) { | 651 void Layer::SetBoundsFromAnimator(const gfx::Rect& bounds) { |
626 SetBoundsImmediately(bounds); | 652 SetBoundsImmediately(bounds); |
627 } | 653 } |
628 | 654 |
629 void Layer::SetTransformFromAnimation(const Transform& transform) { | 655 void Layer::SetTransformFromAnimator(const Transform& transform) { |
630 SetTransformImmediately(transform); | 656 SetTransformImmediately(transform); |
631 } | 657 } |
632 | 658 |
633 void Layer::SetOpacityFromAnimation(float opacity) { | 659 void Layer::SetOpacityFromAnimator(float opacity) { |
634 SetOpacityImmediately(opacity); | 660 SetOpacityImmediately(opacity); |
635 } | 661 } |
636 | 662 |
637 void Layer::ScheduleDrawForAnimation() { | |
638 ScheduleDraw(); | |
639 } | |
640 | |
641 const gfx::Rect& Layer::GetBoundsForAnimation() const { | |
642 return bounds(); | |
643 } | |
644 | |
645 const Transform& Layer::GetTransformForAnimation() const { | |
646 return transform(); | |
647 } | |
648 | |
649 float Layer::GetOpacityForAnimation() const { | |
650 return opacity(); | |
651 } | |
652 | |
653 void Layer::OnLayerAnimationEnded(LayerAnimationSequence* sequence) { | |
654 if (delegate_) | |
655 delegate_->OnLayerAnimationEnded(sequence); | |
656 } | |
657 | |
658 #if defined(USE_WEBKIT_COMPOSITOR) | 663 #if defined(USE_WEBKIT_COMPOSITOR) |
659 void Layer::CreateWebLayer() { | 664 void Layer::CreateWebLayer() { |
660 web_layer_ = WebKit::WebContentLayer::create(this, this); | 665 web_layer_ = WebKit::WebContentLayer::create(this, this); |
661 web_layer_.setAnchorPoint(WebKit::WebFloatPoint(0.f, 0.f)); | 666 web_layer_.setAnchorPoint(WebKit::WebFloatPoint(0.f, 0.f)); |
662 web_layer_.setOpaque(true); | 667 web_layer_.setOpaque(true); |
663 web_layer_is_accelerated_ = false; | 668 web_layer_is_accelerated_ = false; |
664 RecomputeDrawsContent(); | 669 RecomputeDrawsContent(); |
665 } | 670 } |
666 | 671 |
667 void Layer::RecomputeTransform() { | 672 void Layer::RecomputeTransform() { |
(...skipping 16 matching lines...) Expand all Loading... |
684 #else | 689 #else |
685 unsigned int texture_id = 0; | 690 unsigned int texture_id = 0; |
686 #endif | 691 #endif |
687 web_layer_.to<WebKit::WebExternalTextureLayer>().setTextureId( | 692 web_layer_.to<WebKit::WebExternalTextureLayer>().setTextureId( |
688 should_draw ? texture_id : 0); | 693 should_draw ? texture_id : 0); |
689 } | 694 } |
690 } | 695 } |
691 #endif | 696 #endif |
692 | 697 |
693 } // namespace ui | 698 } // namespace ui |
OLD | NEW |