OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "ui/compositor/layer.h" | 5 #include "ui/compositor/layer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/debug/trace_event.h" | 10 #include "base/debug/trace_event.h" |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
193 | 193 |
194 void Layer::SetTransform(const gfx::Transform& transform) { | 194 void Layer::SetTransform(const gfx::Transform& transform) { |
195 GetAnimator()->SetTransform(transform); | 195 GetAnimator()->SetTransform(transform); |
196 } | 196 } |
197 | 197 |
198 gfx::Transform Layer::GetTargetTransform() const { | 198 gfx::Transform Layer::GetTargetTransform() const { |
199 if (animator_.get() && animator_->IsAnimatingProperty( | 199 if (animator_.get() && animator_->IsAnimatingProperty( |
200 LayerAnimationElement::TRANSFORM)) { | 200 LayerAnimationElement::TRANSFORM)) { |
201 return animator_->GetTargetTransform(); | 201 return animator_->GetTargetTransform(); |
202 } | 202 } |
203 return transform_; | 203 return transform(); |
204 } | 204 } |
205 | 205 |
206 void Layer::SetBounds(const gfx::Rect& bounds) { | 206 void Layer::SetBounds(const gfx::Rect& bounds) { |
207 GetAnimator()->SetBounds(bounds); | 207 GetAnimator()->SetBounds(bounds); |
208 } | 208 } |
209 | 209 |
210 gfx::Rect Layer::GetTargetBounds() const { | 210 gfx::Rect Layer::GetTargetBounds() const { |
211 if (animator_.get() && animator_->IsAnimatingProperty( | 211 if (animator_.get() && animator_->IsAnimatingProperty( |
212 LayerAnimationElement::BOUNDS)) { | 212 LayerAnimationElement::BOUNDS)) { |
213 return animator_->GetTargetBounds(); | 213 return animator_->GetTargetBounds(); |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
403 | 403 |
404 const Layer* root_layer = GetRoot(source); | 404 const Layer* root_layer = GetRoot(source); |
405 CHECK_EQ(root_layer, GetRoot(target)); | 405 CHECK_EQ(root_layer, GetRoot(target)); |
406 | 406 |
407 if (source != root_layer) | 407 if (source != root_layer) |
408 source->ConvertPointForAncestor(root_layer, point); | 408 source->ConvertPointForAncestor(root_layer, point); |
409 if (target != root_layer) | 409 if (target != root_layer) |
410 target->ConvertPointFromAncestor(root_layer, point); | 410 target->ConvertPointFromAncestor(root_layer, point); |
411 } | 411 } |
412 | 412 |
413 // static | |
414 gfx::Transform Layer::ConvertTransformToCCTransform( | |
415 const gfx::Transform& transform, | |
416 const gfx::Rect& bounds, | |
417 float device_scale_factor) { | |
418 gfx::Transform cc_transform; | |
419 cc_transform.Scale(device_scale_factor, device_scale_factor); | |
420 cc_transform.Translate(bounds.x(), bounds.y()); | |
421 cc_transform.PreconcatTransform(transform); | |
422 cc_transform.Scale(1.0f / device_scale_factor, 1.0f / device_scale_factor); | |
423 return cc_transform; | |
424 } | |
425 | |
413 void Layer::SetFillsBoundsOpaquely(bool fills_bounds_opaquely) { | 426 void Layer::SetFillsBoundsOpaquely(bool fills_bounds_opaquely) { |
414 if (fills_bounds_opaquely_ == fills_bounds_opaquely) | 427 if (fills_bounds_opaquely_ == fills_bounds_opaquely) |
415 return; | 428 return; |
416 | 429 |
417 fills_bounds_opaquely_ = fills_bounds_opaquely; | 430 fills_bounds_opaquely_ = fills_bounds_opaquely; |
418 | 431 |
419 cc_layer_->setContentsOpaque(fills_bounds_opaquely); | 432 cc_layer_->setContentsOpaque(fills_bounds_opaquely); |
420 } | 433 } |
421 | 434 |
422 void Layer::SetExternalTexture(Texture* texture) { | 435 void Layer::SetExternalTexture(Texture* texture) { |
(...skipping 19 matching lines...) Expand all Loading... | |
442 old_texture_layer->willModifyTexture(); | 455 old_texture_layer->willModifyTexture(); |
443 content_layer_ = cc::ContentLayer::create(this); | 456 content_layer_ = cc::ContentLayer::create(this); |
444 new_layer = content_layer_.get(); | 457 new_layer = content_layer_.get(); |
445 } | 458 } |
446 if (parent_) { | 459 if (parent_) { |
447 DCHECK(parent_->cc_layer_); | 460 DCHECK(parent_->cc_layer_); |
448 parent_->cc_layer_->replaceChild(cc_layer_, new_layer); | 461 parent_->cc_layer_->replaceChild(cc_layer_, new_layer); |
449 } | 462 } |
450 cc_layer_->removeLayerAnimationEventObserver(this); | 463 cc_layer_->removeLayerAnimationEventObserver(this); |
451 new_layer->setOpacity(cc_layer_->opacity()); | 464 new_layer->setOpacity(cc_layer_->opacity()); |
465 new_layer->setTransform(cc_layer_->transform()); | |
452 cc_layer_= new_layer; | 466 cc_layer_= new_layer; |
453 cc_layer_->addLayerAnimationEventObserver(this); | 467 cc_layer_->addLayerAnimationEventObserver(this); |
454 cc_layer_is_accelerated_ = layer_updated_externally_; | 468 cc_layer_is_accelerated_ = layer_updated_externally_; |
455 for (size_t i = 0; i < children_.size(); ++i) { | 469 for (size_t i = 0; i < children_.size(); ++i) { |
456 DCHECK(children_[i]->cc_layer_); | 470 DCHECK(children_[i]->cc_layer_); |
457 cc_layer_->addChild(children_[i]->cc_layer_); | 471 cc_layer_->addChild(children_[i]->cc_layer_); |
458 } | 472 } |
459 cc_layer_->setAnchorPoint(gfx::PointF()); | 473 cc_layer_->setAnchorPoint(gfx::PointF()); |
460 cc_layer_->setContentsOpaque(fills_bounds_opaquely_); | 474 cc_layer_->setContentsOpaque(fills_bounds_opaquely_); |
461 cc_layer_->setForceRenderSurface(force_render_surface_); | 475 cc_layer_->setForceRenderSurface(force_render_surface_); |
462 cc_layer_->setIsDrawable(IsDrawn()); | 476 cc_layer_->setIsDrawable(IsDrawn()); |
463 RecomputeTransform(); | |
464 } | 477 } |
465 RecomputeDrawsContentAndUVRect(); | 478 RecomputeDrawsContentAndUVRect(); |
466 } | 479 } |
467 | 480 |
468 void Layer::SetColor(SkColor color) { | 481 void Layer::SetColor(SkColor color) { |
469 GetAnimator()->SetColor(color); | 482 GetAnimator()->SetColor(color); |
470 } | 483 } |
471 | 484 |
472 bool Layer::SchedulePaint(const gfx::Rect& invalid_rect) { | 485 bool Layer::SchedulePaint(const gfx::Rect& invalid_rect) { |
473 if (type_ == LAYER_SOLID_COLOR || (!delegate_ && !texture_)) | 486 if (type_ == LAYER_SOLID_COLOR || (!delegate_ && !texture_)) |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
512 if (!delegate_) | 525 if (!delegate_) |
513 return; | 526 return; |
514 delegate_ = NULL; | 527 delegate_ = NULL; |
515 for (size_t i = 0; i < children_.size(); ++i) | 528 for (size_t i = 0; i < children_.size(); ++i) |
516 children_[i]->SuppressPaint(); | 529 children_[i]->SuppressPaint(); |
517 } | 530 } |
518 | 531 |
519 void Layer::OnDeviceScaleFactorChanged(float device_scale_factor) { | 532 void Layer::OnDeviceScaleFactorChanged(float device_scale_factor) { |
520 if (device_scale_factor_ == device_scale_factor) | 533 if (device_scale_factor_ == device_scale_factor) |
521 return; | 534 return; |
535 if (animator_) | |
536 animator_->StopAnimatingProperty(LayerAnimationElement::TRANSFORM); | |
537 gfx::Transform transform = this->transform(); | |
522 device_scale_factor_ = device_scale_factor; | 538 device_scale_factor_ = device_scale_factor; |
523 RecomputeTransform(); | 539 RecomputeCCTransformFromTransform(transform); |
524 RecomputeDrawsContentAndUVRect(); | 540 RecomputeDrawsContentAndUVRect(); |
525 SchedulePaint(gfx::Rect(bounds_.size())); | 541 SchedulePaint(gfx::Rect(bounds_.size())); |
526 if (delegate_) | 542 if (delegate_) |
527 delegate_->OnDeviceScaleFactorChanged(device_scale_factor); | 543 delegate_->OnDeviceScaleFactorChanged(device_scale_factor); |
528 for (size_t i = 0; i < children_.size(); ++i) | 544 for (size_t i = 0; i < children_.size(); ++i) |
529 children_[i]->OnDeviceScaleFactorChanged(device_scale_factor); | 545 children_[i]->OnDeviceScaleFactorChanged(device_scale_factor); |
530 if (layer_mask_) | 546 if (layer_mask_) |
531 layer_mask_->OnDeviceScaleFactorChanged(device_scale_factor); | 547 layer_mask_->OnDeviceScaleFactorChanged(device_scale_factor); |
532 } | 548 } |
533 | 549 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
634 } | 650 } |
635 | 651 |
636 void Layer::SetBoundsImmediately(const gfx::Rect& bounds) { | 652 void Layer::SetBoundsImmediately(const gfx::Rect& bounds) { |
637 if (bounds == bounds_) | 653 if (bounds == bounds_) |
638 return; | 654 return; |
639 | 655 |
640 base::Closure closure; | 656 base::Closure closure; |
641 if (delegate_) | 657 if (delegate_) |
642 closure = delegate_->PrepareForLayerBoundsChange(); | 658 closure = delegate_->PrepareForLayerBoundsChange(); |
643 bool was_move = bounds_.size() == bounds.size(); | 659 bool was_move = bounds_.size() == bounds.size(); |
660 gfx::Transform transform = this->transform(); | |
644 bounds_ = bounds; | 661 bounds_ = bounds; |
645 | 662 |
646 RecomputeTransform(); | 663 RecomputeCCTransformFromTransform(transform); |
647 RecomputeDrawsContentAndUVRect(); | 664 RecomputeDrawsContentAndUVRect(); |
648 if (!closure.is_null()) | 665 if (!closure.is_null()) |
649 closure.Run(); | 666 closure.Run(); |
650 | 667 |
651 if (was_move) { | 668 if (was_move) { |
652 // Don't schedule a draw if we're invisible. We'll schedule one | 669 // Don't schedule a draw if we're invisible. We'll schedule one |
653 // automatically when we get visible. | 670 // automatically when we get visible. |
654 if (IsDrawn()) | 671 if (IsDrawn()) |
655 ScheduleDraw(); | 672 ScheduleDraw(); |
656 } else { | 673 } else { |
657 // Always schedule a paint, even if we're invisible. | 674 // Always schedule a paint, even if we're invisible. |
658 SchedulePaint(gfx::Rect(bounds.size())); | 675 SchedulePaint(gfx::Rect(bounds.size())); |
659 } | 676 } |
660 } | 677 } |
661 | 678 |
662 void Layer::SetTransformImmediately(const gfx::Transform& transform) { | 679 void Layer::SetTransformImmediately(const gfx::Transform& transform) { |
663 transform_ = transform; | 680 RecomputeCCTransformFromTransform(transform); |
664 | |
665 RecomputeTransform(); | |
666 } | 681 } |
667 | 682 |
668 void Layer::SetOpacityImmediately(float opacity) { | 683 void Layer::SetOpacityImmediately(float opacity) { |
669 cc_layer_->setOpacity(opacity); | 684 cc_layer_->setOpacity(opacity); |
670 ScheduleDraw(); | 685 ScheduleDraw(); |
671 } | 686 } |
672 | 687 |
673 void Layer::SetVisibilityImmediately(bool visible) { | 688 void Layer::SetVisibilityImmediately(bool visible) { |
674 if (visible_ == visible) | 689 if (visible_ == visible) |
675 return; | 690 return; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
724 } | 739 } |
725 | 740 |
726 void Layer::ScheduleDrawForAnimation() { | 741 void Layer::ScheduleDrawForAnimation() { |
727 ScheduleDraw(); | 742 ScheduleDraw(); |
728 } | 743 } |
729 | 744 |
730 const gfx::Rect& Layer::GetBoundsForAnimation() const { | 745 const gfx::Rect& Layer::GetBoundsForAnimation() const { |
731 return bounds(); | 746 return bounds(); |
732 } | 747 } |
733 | 748 |
734 const gfx::Transform& Layer::GetTransformForAnimation() const { | 749 gfx::Transform Layer::GetTransformForAnimation() const { |
735 return transform(); | 750 return transform(); |
736 } | 751 } |
737 | 752 |
738 float Layer::GetOpacityForAnimation() const { | 753 float Layer::GetOpacityForAnimation() const { |
739 return opacity(); | 754 return opacity(); |
740 } | 755 } |
741 | 756 |
742 bool Layer::GetVisibilityForAnimation() const { | 757 bool Layer::GetVisibilityForAnimation() const { |
743 return visible(); | 758 return visible(); |
744 } | 759 } |
745 | 760 |
746 float Layer::GetBrightnessForAnimation() const { | 761 float Layer::GetBrightnessForAnimation() const { |
747 return layer_brightness(); | 762 return layer_brightness(); |
748 } | 763 } |
749 | 764 |
750 float Layer::GetGrayscaleForAnimation() const { | 765 float Layer::GetGrayscaleForAnimation() const { |
751 return layer_grayscale(); | 766 return layer_grayscale(); |
752 } | 767 } |
753 | 768 |
754 SkColor Layer::GetColorForAnimation() const { | 769 SkColor Layer::GetColorForAnimation() const { |
755 // WebColor is equivalent to SkColor, per WebColor.h. | 770 // WebColor is equivalent to SkColor, per WebColor.h. |
756 // The NULL check is here since this is invoked regardless of whether we have | 771 // The NULL check is here since this is invoked regardless of whether we have |
757 // been configured as LAYER_SOLID_COLOR. | 772 // been configured as LAYER_SOLID_COLOR. |
758 return solid_color_layer_.get() ? | 773 return solid_color_layer_.get() ? |
759 solid_color_layer_->backgroundColor() : SK_ColorBLACK; | 774 solid_color_layer_->backgroundColor() : SK_ColorBLACK; |
760 } | 775 } |
761 | 776 |
777 float Layer::GetDeviceScaleFactor() const { | |
778 return device_scale_factor_; | |
779 } | |
780 | |
762 void Layer::AddThreadedAnimation(scoped_ptr<cc::Animation> animation) { | 781 void Layer::AddThreadedAnimation(scoped_ptr<cc::Animation> animation) { |
763 DCHECK(cc_layer_); | 782 DCHECK(cc_layer_); |
764 cc_layer_->addAnimation(animation.Pass()); | 783 cc_layer_->addAnimation(animation.Pass()); |
765 } | 784 } |
766 | 785 |
767 void Layer::RemoveThreadedAnimation(int animation_id) { | 786 void Layer::RemoveThreadedAnimation(int animation_id) { |
768 DCHECK(cc_layer_); | 787 DCHECK(cc_layer_); |
769 cc_layer_->removeAnimation(animation_id); | 788 cc_layer_->removeAnimation(animation_id); |
770 } | 789 } |
771 | 790 |
772 void Layer::CreateWebLayer() { | 791 void Layer::CreateWebLayer() { |
773 if (type_ == LAYER_SOLID_COLOR) { | 792 if (type_ == LAYER_SOLID_COLOR) { |
774 solid_color_layer_ = cc::SolidColorLayer::create(); | 793 solid_color_layer_ = cc::SolidColorLayer::create(); |
775 cc_layer_ = solid_color_layer_.get(); | 794 cc_layer_ = solid_color_layer_.get(); |
776 } else { | 795 } else { |
777 content_layer_ = cc::ContentLayer::create(this); | 796 content_layer_ = cc::ContentLayer::create(this); |
778 cc_layer_ = content_layer_.get(); | 797 cc_layer_ = content_layer_.get(); |
779 } | 798 } |
780 cc_layer_is_accelerated_ = false; | 799 cc_layer_is_accelerated_ = false; |
781 cc_layer_->setAnchorPoint(gfx::PointF()); | 800 cc_layer_->setAnchorPoint(gfx::PointF()); |
782 cc_layer_->setContentsOpaque(true); | 801 cc_layer_->setContentsOpaque(true); |
783 cc_layer_->setIsDrawable(type_ != LAYER_NOT_DRAWN); | 802 cc_layer_->setIsDrawable(type_ != LAYER_NOT_DRAWN); |
784 cc_layer_->addLayerAnimationEventObserver(this); | 803 cc_layer_->addLayerAnimationEventObserver(this); |
785 } | 804 } |
786 | 805 |
787 void Layer::RecomputeTransform() { | 806 void Layer::RecomputeCCTransformFromTransform(const gfx::Transform& transform) { |
788 gfx::Transform scale_translate; | 807 cc_layer_->setTransform(ConvertTransformToCCTransform(transform, |
789 scale_translate.matrix().set3x3(device_scale_factor_, 0, 0, | 808 bounds_, |
790 0, device_scale_factor_, 0, | 809 device_scale_factor_)); |
791 0, 0, 1); | 810 } |
792 // Start with the inverse matrix of above. | 811 |
812 gfx::Transform Layer::transform() const { | |
793 gfx::Transform transform; | 813 gfx::Transform transform; |
794 transform.matrix().set3x3(1.0f / device_scale_factor_, 0, 0, | 814 transform.Translate(-bounds_.x(), -bounds_.y()); |
danakj
2013/02/19 16:19:31
This ordering is different from the inverse case.
| |
795 0, 1.0f / device_scale_factor_, 0, | 815 transform.Scale(1.0f / device_scale_factor_, 1.0f / device_scale_factor_); |
796 0, 0, 1); | 816 transform.PreconcatTransform(cc_layer_->transform()); |
797 transform.ConcatTransform(transform_); | 817 transform.Scale(device_scale_factor_, device_scale_factor_); |
798 gfx::Transform translate; | 818 return transform; |
799 translate.Translate(bounds_.x(), bounds_.y()); | |
800 transform.ConcatTransform(translate); | |
801 transform.ConcatTransform(scale_translate); | |
802 cc_layer_->setTransform(transform); | |
803 } | 819 } |
804 | 820 |
805 void Layer::RecomputeDrawsContentAndUVRect() { | 821 void Layer::RecomputeDrawsContentAndUVRect() { |
806 DCHECK(cc_layer_); | 822 DCHECK(cc_layer_); |
807 if (!cc_layer_is_accelerated_) { | 823 if (!cc_layer_is_accelerated_) { |
808 cc_layer_->setBounds(ConvertSizeToPixel(this, bounds_.size())); | 824 cc_layer_->setBounds(ConvertSizeToPixel(this, bounds_.size())); |
809 } else { | 825 } else { |
810 DCHECK(texture_); | 826 DCHECK(texture_); |
811 | 827 |
812 float texture_scale_factor = 1.0f / texture_->device_scale_factor(); | 828 float texture_scale_factor = 1.0f / texture_->device_scale_factor(); |
813 gfx::Size texture_size = gfx::ToFlooredSize( | 829 gfx::Size texture_size = gfx::ToFlooredSize( |
814 gfx::ScaleSize(texture_->size(), texture_scale_factor)); | 830 gfx::ScaleSize(texture_->size(), texture_scale_factor)); |
815 | 831 |
816 gfx::Size size(std::min(bounds().width(), texture_size.width()), | 832 gfx::Size size(std::min(bounds().width(), texture_size.width()), |
817 std::min(bounds().height(), texture_size.height())); | 833 std::min(bounds().height(), texture_size.height())); |
818 gfx::PointF uv_top_left(0.f, 0.f); | 834 gfx::PointF uv_top_left(0.f, 0.f); |
819 gfx::PointF uv_bottom_right( | 835 gfx::PointF uv_bottom_right( |
820 static_cast<float>(size.width())/texture_size.width(), | 836 static_cast<float>(size.width())/texture_size.width(), |
821 static_cast<float>(size.height())/texture_size.height()); | 837 static_cast<float>(size.height())/texture_size.height()); |
822 texture_layer_->setUV(uv_top_left, uv_bottom_right); | 838 texture_layer_->setUV(uv_top_left, uv_bottom_right); |
823 | 839 |
824 cc_layer_->setBounds(ConvertSizeToPixel(this, size)); | 840 cc_layer_->setBounds(ConvertSizeToPixel(this, size)); |
825 } | 841 } |
826 } | 842 } |
827 | 843 |
828 } // namespace ui | 844 } // namespace ui |
OLD | NEW |