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 scale_translate; | |
419 scale_translate.matrix().set3x3(device_scale_factor, 0, 0, | |
danakj
2013/02/16 04:49:51
Why do you use temporary gfx::Transforms for scali
ajuma
2013/02/19 16:12:19
Fixed now.
| |
420 0, device_scale_factor, 0, | |
421 0, 0, 1); | |
422 // Start with the inverse matrix of above. | |
423 gfx::Transform cc_transform; | |
424 cc_transform.matrix().set3x3(1.0f / device_scale_factor, 0, 0, | |
425 0, 1.0f / device_scale_factor, 0, | |
426 0, 0, 1); | |
427 cc_transform.ConcatTransform(transform); | |
428 gfx::Transform translate; | |
429 translate.Translate(bounds.x(), bounds.y()); | |
430 cc_transform.ConcatTransform(translate); | |
431 cc_transform.ConcatTransform(scale_translate); | |
432 return cc_transform; | |
433 } | |
434 | |
413 void Layer::SetFillsBoundsOpaquely(bool fills_bounds_opaquely) { | 435 void Layer::SetFillsBoundsOpaquely(bool fills_bounds_opaquely) { |
414 if (fills_bounds_opaquely_ == fills_bounds_opaquely) | 436 if (fills_bounds_opaquely_ == fills_bounds_opaquely) |
415 return; | 437 return; |
416 | 438 |
417 fills_bounds_opaquely_ = fills_bounds_opaquely; | 439 fills_bounds_opaquely_ = fills_bounds_opaquely; |
418 | 440 |
419 cc_layer_->setContentsOpaque(fills_bounds_opaquely); | 441 cc_layer_->setContentsOpaque(fills_bounds_opaquely); |
420 } | 442 } |
421 | 443 |
422 void Layer::SetExternalTexture(Texture* texture) { | 444 void Layer::SetExternalTexture(Texture* texture) { |
(...skipping 19 matching lines...) Expand all Loading... | |
442 old_texture_layer->willModifyTexture(); | 464 old_texture_layer->willModifyTexture(); |
443 content_layer_ = cc::ContentLayer::create(this); | 465 content_layer_ = cc::ContentLayer::create(this); |
444 new_layer = content_layer_.get(); | 466 new_layer = content_layer_.get(); |
445 } | 467 } |
446 if (parent_) { | 468 if (parent_) { |
447 DCHECK(parent_->cc_layer_); | 469 DCHECK(parent_->cc_layer_); |
448 parent_->cc_layer_->replaceChild(cc_layer_, new_layer); | 470 parent_->cc_layer_->replaceChild(cc_layer_, new_layer); |
449 } | 471 } |
450 cc_layer_->removeLayerAnimationEventObserver(this); | 472 cc_layer_->removeLayerAnimationEventObserver(this); |
451 new_layer->setOpacity(cc_layer_->opacity()); | 473 new_layer->setOpacity(cc_layer_->opacity()); |
474 new_layer->setTransform(cc_layer_->transform()); | |
452 cc_layer_= new_layer; | 475 cc_layer_= new_layer; |
453 cc_layer_->addLayerAnimationEventObserver(this); | 476 cc_layer_->addLayerAnimationEventObserver(this); |
454 cc_layer_is_accelerated_ = layer_updated_externally_; | 477 cc_layer_is_accelerated_ = layer_updated_externally_; |
455 for (size_t i = 0; i < children_.size(); ++i) { | 478 for (size_t i = 0; i < children_.size(); ++i) { |
456 DCHECK(children_[i]->cc_layer_); | 479 DCHECK(children_[i]->cc_layer_); |
457 cc_layer_->addChild(children_[i]->cc_layer_); | 480 cc_layer_->addChild(children_[i]->cc_layer_); |
458 } | 481 } |
459 cc_layer_->setAnchorPoint(gfx::PointF()); | 482 cc_layer_->setAnchorPoint(gfx::PointF()); |
460 cc_layer_->setContentsOpaque(fills_bounds_opaquely_); | 483 cc_layer_->setContentsOpaque(fills_bounds_opaquely_); |
461 cc_layer_->setForceRenderSurface(force_render_surface_); | 484 cc_layer_->setForceRenderSurface(force_render_surface_); |
462 cc_layer_->setIsDrawable(IsDrawn()); | 485 cc_layer_->setIsDrawable(IsDrawn()); |
463 RecomputeTransform(); | |
464 } | 486 } |
465 RecomputeDrawsContentAndUVRect(); | 487 RecomputeDrawsContentAndUVRect(); |
466 } | 488 } |
467 | 489 |
468 void Layer::SetColor(SkColor color) { | 490 void Layer::SetColor(SkColor color) { |
469 GetAnimator()->SetColor(color); | 491 GetAnimator()->SetColor(color); |
470 } | 492 } |
471 | 493 |
472 bool Layer::SchedulePaint(const gfx::Rect& invalid_rect) { | 494 bool Layer::SchedulePaint(const gfx::Rect& invalid_rect) { |
473 if (type_ == LAYER_SOLID_COLOR || (!delegate_ && !texture_)) | 495 if (type_ == LAYER_SOLID_COLOR || (!delegate_ && !texture_)) |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
512 if (!delegate_) | 534 if (!delegate_) |
513 return; | 535 return; |
514 delegate_ = NULL; | 536 delegate_ = NULL; |
515 for (size_t i = 0; i < children_.size(); ++i) | 537 for (size_t i = 0; i < children_.size(); ++i) |
516 children_[i]->SuppressPaint(); | 538 children_[i]->SuppressPaint(); |
517 } | 539 } |
518 | 540 |
519 void Layer::OnDeviceScaleFactorChanged(float device_scale_factor) { | 541 void Layer::OnDeviceScaleFactorChanged(float device_scale_factor) { |
520 if (device_scale_factor_ == device_scale_factor) | 542 if (device_scale_factor_ == device_scale_factor) |
521 return; | 543 return; |
544 if (animator_) | |
545 animator_->StopAnimatingProperty(LayerAnimationElement::TRANSFORM); | |
546 gfx::Transform transform = this->transform(); | |
522 device_scale_factor_ = device_scale_factor; | 547 device_scale_factor_ = device_scale_factor; |
523 RecomputeTransform(); | 548 RecomputeCCTransformFromTransform(transform); |
524 RecomputeDrawsContentAndUVRect(); | 549 RecomputeDrawsContentAndUVRect(); |
525 SchedulePaint(gfx::Rect(bounds_.size())); | 550 SchedulePaint(gfx::Rect(bounds_.size())); |
526 if (delegate_) | 551 if (delegate_) |
527 delegate_->OnDeviceScaleFactorChanged(device_scale_factor); | 552 delegate_->OnDeviceScaleFactorChanged(device_scale_factor); |
528 for (size_t i = 0; i < children_.size(); ++i) | 553 for (size_t i = 0; i < children_.size(); ++i) |
529 children_[i]->OnDeviceScaleFactorChanged(device_scale_factor); | 554 children_[i]->OnDeviceScaleFactorChanged(device_scale_factor); |
530 if (layer_mask_) | 555 if (layer_mask_) |
531 layer_mask_->OnDeviceScaleFactorChanged(device_scale_factor); | 556 layer_mask_->OnDeviceScaleFactorChanged(device_scale_factor); |
532 } | 557 } |
533 | 558 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
634 } | 659 } |
635 | 660 |
636 void Layer::SetBoundsImmediately(const gfx::Rect& bounds) { | 661 void Layer::SetBoundsImmediately(const gfx::Rect& bounds) { |
637 if (bounds == bounds_) | 662 if (bounds == bounds_) |
638 return; | 663 return; |
639 | 664 |
640 base::Closure closure; | 665 base::Closure closure; |
641 if (delegate_) | 666 if (delegate_) |
642 closure = delegate_->PrepareForLayerBoundsChange(); | 667 closure = delegate_->PrepareForLayerBoundsChange(); |
643 bool was_move = bounds_.size() == bounds.size(); | 668 bool was_move = bounds_.size() == bounds.size(); |
669 gfx::Transform transform = this->transform(); | |
644 bounds_ = bounds; | 670 bounds_ = bounds; |
645 | 671 |
646 RecomputeTransform(); | 672 RecomputeCCTransformFromTransform(transform); |
647 RecomputeDrawsContentAndUVRect(); | 673 RecomputeDrawsContentAndUVRect(); |
648 if (!closure.is_null()) | 674 if (!closure.is_null()) |
649 closure.Run(); | 675 closure.Run(); |
650 | 676 |
651 if (was_move) { | 677 if (was_move) { |
652 // Don't schedule a draw if we're invisible. We'll schedule one | 678 // Don't schedule a draw if we're invisible. We'll schedule one |
653 // automatically when we get visible. | 679 // automatically when we get visible. |
654 if (IsDrawn()) | 680 if (IsDrawn()) |
655 ScheduleDraw(); | 681 ScheduleDraw(); |
656 } else { | 682 } else { |
657 // Always schedule a paint, even if we're invisible. | 683 // Always schedule a paint, even if we're invisible. |
658 SchedulePaint(gfx::Rect(bounds.size())); | 684 SchedulePaint(gfx::Rect(bounds.size())); |
659 } | 685 } |
660 } | 686 } |
661 | 687 |
662 void Layer::SetTransformImmediately(const gfx::Transform& transform) { | 688 void Layer::SetTransformImmediately(const gfx::Transform& transform) { |
663 transform_ = transform; | 689 RecomputeCCTransformFromTransform(transform); |
664 | |
665 RecomputeTransform(); | |
666 } | 690 } |
667 | 691 |
668 void Layer::SetOpacityImmediately(float opacity) { | 692 void Layer::SetOpacityImmediately(float opacity) { |
669 cc_layer_->setOpacity(opacity); | 693 cc_layer_->setOpacity(opacity); |
670 ScheduleDraw(); | 694 ScheduleDraw(); |
671 } | 695 } |
672 | 696 |
673 void Layer::SetVisibilityImmediately(bool visible) { | 697 void Layer::SetVisibilityImmediately(bool visible) { |
674 if (visible_ == visible) | 698 if (visible_ == visible) |
675 return; | 699 return; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
724 } | 748 } |
725 | 749 |
726 void Layer::ScheduleDrawForAnimation() { | 750 void Layer::ScheduleDrawForAnimation() { |
727 ScheduleDraw(); | 751 ScheduleDraw(); |
728 } | 752 } |
729 | 753 |
730 const gfx::Rect& Layer::GetBoundsForAnimation() const { | 754 const gfx::Rect& Layer::GetBoundsForAnimation() const { |
731 return bounds(); | 755 return bounds(); |
732 } | 756 } |
733 | 757 |
734 const gfx::Transform& Layer::GetTransformForAnimation() const { | 758 gfx::Transform Layer::GetTransformForAnimation() const { |
735 return transform(); | 759 return transform(); |
736 } | 760 } |
737 | 761 |
738 float Layer::GetOpacityForAnimation() const { | 762 float Layer::GetOpacityForAnimation() const { |
739 return opacity(); | 763 return opacity(); |
740 } | 764 } |
741 | 765 |
742 bool Layer::GetVisibilityForAnimation() const { | 766 bool Layer::GetVisibilityForAnimation() const { |
743 return visible(); | 767 return visible(); |
744 } | 768 } |
745 | 769 |
746 float Layer::GetBrightnessForAnimation() const { | 770 float Layer::GetBrightnessForAnimation() const { |
747 return layer_brightness(); | 771 return layer_brightness(); |
748 } | 772 } |
749 | 773 |
750 float Layer::GetGrayscaleForAnimation() const { | 774 float Layer::GetGrayscaleForAnimation() const { |
751 return layer_grayscale(); | 775 return layer_grayscale(); |
752 } | 776 } |
753 | 777 |
754 SkColor Layer::GetColorForAnimation() const { | 778 SkColor Layer::GetColorForAnimation() const { |
755 // WebColor is equivalent to SkColor, per WebColor.h. | 779 // WebColor is equivalent to SkColor, per WebColor.h. |
756 // The NULL check is here since this is invoked regardless of whether we have | 780 // The NULL check is here since this is invoked regardless of whether we have |
757 // been configured as LAYER_SOLID_COLOR. | 781 // been configured as LAYER_SOLID_COLOR. |
758 return solid_color_layer_.get() ? | 782 return solid_color_layer_.get() ? |
759 solid_color_layer_->backgroundColor() : SK_ColorBLACK; | 783 solid_color_layer_->backgroundColor() : SK_ColorBLACK; |
760 } | 784 } |
761 | 785 |
786 float Layer::GetDeviceScaleFactor() const { | |
787 return device_scale_factor_; | |
788 } | |
789 | |
762 void Layer::AddThreadedAnimation(scoped_ptr<cc::Animation> animation) { | 790 void Layer::AddThreadedAnimation(scoped_ptr<cc::Animation> animation) { |
763 DCHECK(cc_layer_); | 791 DCHECK(cc_layer_); |
764 cc_layer_->addAnimation(animation.Pass()); | 792 cc_layer_->addAnimation(animation.Pass()); |
765 } | 793 } |
766 | 794 |
767 void Layer::RemoveThreadedAnimation(int animation_id) { | 795 void Layer::RemoveThreadedAnimation(int animation_id) { |
768 DCHECK(cc_layer_); | 796 DCHECK(cc_layer_); |
769 cc_layer_->removeAnimation(animation_id); | 797 cc_layer_->removeAnimation(animation_id); |
770 } | 798 } |
771 | 799 |
772 void Layer::CreateWebLayer() { | 800 void Layer::CreateWebLayer() { |
773 if (type_ == LAYER_SOLID_COLOR) { | 801 if (type_ == LAYER_SOLID_COLOR) { |
774 solid_color_layer_ = cc::SolidColorLayer::create(); | 802 solid_color_layer_ = cc::SolidColorLayer::create(); |
775 cc_layer_ = solid_color_layer_.get(); | 803 cc_layer_ = solid_color_layer_.get(); |
776 } else { | 804 } else { |
777 content_layer_ = cc::ContentLayer::create(this); | 805 content_layer_ = cc::ContentLayer::create(this); |
778 cc_layer_ = content_layer_.get(); | 806 cc_layer_ = content_layer_.get(); |
779 } | 807 } |
780 cc_layer_is_accelerated_ = false; | 808 cc_layer_is_accelerated_ = false; |
781 cc_layer_->setAnchorPoint(gfx::PointF()); | 809 cc_layer_->setAnchorPoint(gfx::PointF()); |
782 cc_layer_->setContentsOpaque(true); | 810 cc_layer_->setContentsOpaque(true); |
783 cc_layer_->setIsDrawable(type_ != LAYER_NOT_DRAWN); | 811 cc_layer_->setIsDrawable(type_ != LAYER_NOT_DRAWN); |
784 cc_layer_->addLayerAnimationEventObserver(this); | 812 cc_layer_->addLayerAnimationEventObserver(this); |
785 } | 813 } |
786 | 814 |
787 void Layer::RecomputeTransform() { | 815 void Layer::RecomputeCCTransformFromTransform(const gfx::Transform& transform) { |
788 gfx::Transform scale_translate; | 816 cc_layer_->setTransform(ConvertTransformToCCTransform(transform, |
789 scale_translate.matrix().set3x3(device_scale_factor_, 0, 0, | 817 bounds_, |
790 0, device_scale_factor_, 0, | 818 device_scale_factor_)); |
791 0, 0, 1); | 819 } |
820 | |
821 gfx::Transform Layer::transform() const { | |
822 gfx::Transform inverse_scale; | |
823 inverse_scale.matrix().set3x3(1.0f / device_scale_factor_, 0, 0, | |
danakj
2013/02/16 04:49:51
Here you can use Scale() and Translate() as well i
ajuma
2013/02/19 16:12:19
Done.
| |
824 0, 1.0f / device_scale_factor_, 0, | |
825 0, 0, 1); | |
792 // Start with the inverse matrix of above. | 826 // Start with the inverse matrix of above. |
793 gfx::Transform transform; | 827 gfx::Transform transform; |
794 transform.matrix().set3x3(1.0f / device_scale_factor_, 0, 0, | 828 transform.matrix().set3x3(device_scale_factor_, 0, 0, |
795 0, 1.0f / device_scale_factor_, 0, | 829 0, device_scale_factor_, 0, |
796 0, 0, 1); | 830 0, 0, 1); |
797 transform.ConcatTransform(transform_); | 831 transform.ConcatTransform(cc_layer_->transform()); |
798 gfx::Transform translate; | 832 transform.ConcatTransform(inverse_scale); |
799 translate.Translate(bounds_.x(), bounds_.y()); | 833 gfx::Transform inverse_translate; |
800 transform.ConcatTransform(translate); | 834 inverse_translate.Translate(-bounds_.x(), -bounds_.y()); |
801 transform.ConcatTransform(scale_translate); | 835 transform.ConcatTransform(inverse_translate); |
802 cc_layer_->setTransform(transform); | 836 return transform; |
803 } | 837 } |
804 | 838 |
805 void Layer::RecomputeDrawsContentAndUVRect() { | 839 void Layer::RecomputeDrawsContentAndUVRect() { |
806 DCHECK(cc_layer_); | 840 DCHECK(cc_layer_); |
807 if (!cc_layer_is_accelerated_) { | 841 if (!cc_layer_is_accelerated_) { |
808 cc_layer_->setBounds(ConvertSizeToPixel(this, bounds_.size())); | 842 cc_layer_->setBounds(ConvertSizeToPixel(this, bounds_.size())); |
809 } else { | 843 } else { |
810 DCHECK(texture_); | 844 DCHECK(texture_); |
811 | 845 |
812 float texture_scale_factor = 1.0f / texture_->device_scale_factor(); | 846 float texture_scale_factor = 1.0f / texture_->device_scale_factor(); |
813 gfx::Size texture_size = gfx::ToFlooredSize( | 847 gfx::Size texture_size = gfx::ToFlooredSize( |
814 gfx::ScaleSize(texture_->size(), texture_scale_factor)); | 848 gfx::ScaleSize(texture_->size(), texture_scale_factor)); |
815 | 849 |
816 gfx::Size size(std::min(bounds().width(), texture_size.width()), | 850 gfx::Size size(std::min(bounds().width(), texture_size.width()), |
817 std::min(bounds().height(), texture_size.height())); | 851 std::min(bounds().height(), texture_size.height())); |
818 gfx::PointF uv_top_left(0.f, 0.f); | 852 gfx::PointF uv_top_left(0.f, 0.f); |
819 gfx::PointF uv_bottom_right( | 853 gfx::PointF uv_bottom_right( |
820 static_cast<float>(size.width())/texture_size.width(), | 854 static_cast<float>(size.width())/texture_size.width(), |
821 static_cast<float>(size.height())/texture_size.height()); | 855 static_cast<float>(size.height())/texture_size.height()); |
822 texture_layer_->setUV(uv_top_left, uv_bottom_right); | 856 texture_layer_->setUV(uv_top_left, uv_bottom_right); |
823 | 857 |
824 cc_layer_->setBounds(ConvertSizeToPixel(this, size)); | 858 cc_layer_->setBounds(ConvertSizeToPixel(this, size)); |
825 } | 859 } |
826 } | 860 } |
827 | 861 |
828 } // namespace ui | 862 } // namespace ui |
OLD | NEW |