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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 } | 87 } |
88 virtual ~LayerWithRealCompositorTest() {} | 88 virtual ~LayerWithRealCompositorTest() {} |
89 | 89 |
90 // Overridden from testing::Test: | 90 // Overridden from testing::Test: |
91 virtual void SetUp() OVERRIDE { | 91 virtual void SetUp() OVERRIDE { |
92 bool enable_pixel_output = true; | 92 bool enable_pixel_output = true; |
93 ui::ContextFactory* context_factory = | 93 ui::ContextFactory* context_factory = |
94 InitializeContextFactoryForTests(enable_pixel_output); | 94 InitializeContextFactoryForTests(enable_pixel_output); |
95 | 95 |
96 const gfx::Rect host_bounds(10, 10, 500, 500); | 96 const gfx::Rect host_bounds(10, 10, 500, 500); |
97 compositor_host_.reset( | 97 compositor_host_.reset(TestCompositorHost::Create( |
98 TestCompositorHost::Create(host_bounds, context_factory)); | 98 host_bounds, context_factory)); |
99 compositor_host_->Show(); | 99 compositor_host_->Show(); |
100 } | 100 } |
101 | 101 |
102 virtual void TearDown() OVERRIDE { | 102 virtual void TearDown() OVERRIDE { |
103 compositor_host_.reset(); | 103 compositor_host_.reset(); |
104 TerminateContextFactoryForTests(); | 104 TerminateContextFactoryForTests(); |
105 } | 105 } |
106 | 106 |
107 Compositor* GetCompositor() { return compositor_host_->GetCompositor(); } | 107 Compositor* GetCompositor() { return compositor_host_->GetCompositor(); } |
108 | 108 |
109 Layer* CreateLayer(LayerType type) { | 109 Layer* CreateLayer(LayerType type) { |
110 return new Layer(type); | 110 return new Layer(type); |
111 } | 111 } |
112 | 112 |
113 Layer* CreateColorLayer(SkColor color, const gfx::Rect& bounds) { | 113 Layer* CreateColorLayer(SkColor color, const gfx::Rect& bounds) { |
114 Layer* layer = new ColoredLayer(color); | 114 Layer* layer = new ColoredLayer(color); |
115 layer->SetBounds(bounds); | 115 layer->SetBounds(bounds); |
116 return layer; | 116 return layer; |
117 } | 117 } |
118 | 118 |
119 Layer* CreateNoTextureLayer(const gfx::Rect& bounds) { | 119 Layer* CreateNoTextureLayer(const gfx::Rect& bounds) { |
120 Layer* layer = CreateLayer(LAYER_NOT_DRAWN); | 120 Layer* layer = CreateLayer(LAYER_NOT_DRAWN); |
121 layer->SetBounds(bounds); | 121 layer->SetBounds(bounds); |
122 return layer; | 122 return layer; |
123 } | 123 } |
124 | 124 |
125 void DrawTree(Layer* root) { | 125 void DrawTree(Layer* root) { |
126 GetCompositor()->SetRootLayer(root); | 126 GetCompositor()->SetRootLayer(root); |
127 GetCompositor()->ScheduleDraw(); | 127 GetCompositor()->ScheduleDraw(); |
128 WaitForSwap(); | 128 WaitForDraw(); |
129 } | 129 } |
130 | 130 |
131 bool ReadPixels(SkBitmap* bitmap) { | 131 bool ReadPixels(SkBitmap* bitmap) { |
132 return ReadPixels(bitmap, gfx::Rect(GetCompositor()->size())); | 132 return ReadPixels(bitmap, gfx::Rect(GetCompositor()->size())); |
133 } | 133 } |
134 | 134 |
135 bool ReadPixels(SkBitmap* bitmap, gfx::Rect source_rect) { | 135 bool ReadPixels(SkBitmap* bitmap, gfx::Rect source_rect) { |
136 scoped_refptr<ReadbackHolder> holder(new ReadbackHolder); | 136 scoped_refptr<ReadbackHolder> holder(new ReadbackHolder); |
137 scoped_ptr<cc::CopyOutputRequest> request = | 137 scoped_ptr<cc::CopyOutputRequest> request = |
138 cc::CopyOutputRequest::CreateBitmapRequest( | 138 cc::CopyOutputRequest::CreateBitmapRequest( |
139 base::Bind(&ReadbackHolder::OutputRequestCallback, holder)); | 139 base::Bind(&ReadbackHolder::OutputRequestCallback, holder)); |
140 request->set_area(source_rect); | 140 request->set_area(source_rect); |
141 | 141 |
142 GetCompositor()->root_layer()->RequestCopyOfOutput(request.Pass()); | 142 GetCompositor()->root_layer()->RequestCopyOfOutput(request.Pass()); |
143 | 143 |
144 // Wait for copy response. The copy output request will get committed | 144 // Wait for copy response. This needs to wait as the compositor could |
145 // before the first draw, but may not be part of the first draw's frame. | 145 // be in the middle of a draw right now, and the commit with the |
146 // The second draw will perform the async copy request, post the callback. | 146 // copy output request may not be done on the first draw. |
147 // The second loop finishes before the callback is run, so a third | 147 for (int i = 0; i < 2; i++) { |
148 // loop is needed. | 148 GetCompositor()->ScheduleDraw(); |
149 for (int i = 0; i < 3; i++) { | |
150 GetCompositor()->ScheduleFullRedraw(); | |
151 WaitForDraw(); | 149 WaitForDraw(); |
152 } | 150 } |
153 | 151 |
154 if (holder->completed()) { | 152 if (holder->completed()) { |
155 *bitmap = holder->result(); | 153 *bitmap = holder->result(); |
156 return true; | 154 return true; |
157 } | 155 } |
158 | 156 |
159 // Callback never called. | 157 // Callback never called. |
160 NOTREACHED(); | 158 NOTREACHED(); |
161 return false; | 159 return false; |
162 } | 160 } |
163 | 161 |
164 void WaitForDraw() { | 162 void WaitForDraw() { |
165 ui::DrawWaiterForTest::WaitForCompositingStarted(GetCompositor()); | 163 ui::DrawWaiterForTest::Wait(GetCompositor()); |
166 } | |
167 | |
168 void WaitForSwap() { | |
169 DrawWaiterForTest::WaitForCompositingEnded(GetCompositor()); | |
170 } | 164 } |
171 | 165 |
172 void WaitForCommit() { | 166 void WaitForCommit() { |
173 ui::DrawWaiterForTest::WaitForCommit(GetCompositor()); | 167 ui::DrawWaiterForTest::WaitForCommit(GetCompositor()); |
174 } | 168 } |
175 | 169 |
176 // Invalidates the entire contents of the layer. | 170 // Invalidates the entire contents of the layer. |
177 void SchedulePaintForLayer(Layer* layer) { | 171 void SchedulePaintForLayer(Layer* layer) { |
178 layer->SchedulePaint( | 172 layer->SchedulePaint( |
179 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height())); | 173 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height())); |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height())); | 442 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height())); |
449 } | 443 } |
450 | 444 |
451 // Invokes DrawTree on the compositor. | 445 // Invokes DrawTree on the compositor. |
452 void Draw() { | 446 void Draw() { |
453 compositor()->ScheduleDraw(); | 447 compositor()->ScheduleDraw(); |
454 WaitForDraw(); | 448 WaitForDraw(); |
455 } | 449 } |
456 | 450 |
457 void WaitForDraw() { | 451 void WaitForDraw() { |
458 DrawWaiterForTest::WaitForCompositingStarted(compositor()); | 452 DrawWaiterForTest::Wait(compositor()); |
459 } | 453 } |
460 | 454 |
461 void WaitForCommit() { | 455 void WaitForCommit() { |
462 DrawWaiterForTest::WaitForCommit(compositor()); | 456 DrawWaiterForTest::WaitForCommit(compositor()); |
463 } | 457 } |
464 | 458 |
465 private: | 459 private: |
466 scoped_ptr<TestCompositorHost> compositor_host_; | 460 scoped_ptr<TestCompositorHost> compositor_host_; |
467 | 461 |
468 DISALLOW_COPY_AND_ASSIGN(LayerWithDelegateTest); | 462 DISALLOW_COPY_AND_ASSIGN(LayerWithDelegateTest); |
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
943 | 937 |
944 // ScheduleDraw without any visible change should cause a commit. | 938 // ScheduleDraw without any visible change should cause a commit. |
945 observer.Reset(); | 939 observer.Reset(); |
946 l1->ScheduleDraw(); | 940 l1->ScheduleDraw(); |
947 WaitForCommit(); | 941 WaitForCommit(); |
948 EXPECT_TRUE(observer.committed()); | 942 EXPECT_TRUE(observer.committed()); |
949 | 943 |
950 // Moving, but not resizing, a layer should alert the observers. | 944 // Moving, but not resizing, a layer should alert the observers. |
951 observer.Reset(); | 945 observer.Reset(); |
952 l2->SetBounds(gfx::Rect(0, 0, 350, 350)); | 946 l2->SetBounds(gfx::Rect(0, 0, 350, 350)); |
953 WaitForSwap(); | 947 WaitForDraw(); |
954 EXPECT_TRUE(observer.notified()); | 948 EXPECT_TRUE(observer.notified()); |
955 | 949 |
956 // So should resizing a layer. | 950 // So should resizing a layer. |
957 observer.Reset(); | 951 observer.Reset(); |
958 l2->SetBounds(gfx::Rect(0, 0, 400, 400)); | 952 l2->SetBounds(gfx::Rect(0, 0, 400, 400)); |
959 WaitForSwap(); | 953 WaitForDraw(); |
960 EXPECT_TRUE(observer.notified()); | 954 EXPECT_TRUE(observer.notified()); |
961 | 955 |
962 // Opacity changes should alert the observers. | 956 // Opacity changes should alert the observers. |
963 observer.Reset(); | 957 observer.Reset(); |
964 l2->SetOpacity(0.5f); | 958 l2->SetOpacity(0.5f); |
965 WaitForSwap(); | 959 WaitForDraw(); |
966 EXPECT_TRUE(observer.notified()); | 960 EXPECT_TRUE(observer.notified()); |
967 | 961 |
968 // So should setting the opacity back. | 962 // So should setting the opacity back. |
969 observer.Reset(); | 963 observer.Reset(); |
970 l2->SetOpacity(1.0f); | 964 l2->SetOpacity(1.0f); |
971 WaitForSwap(); | 965 WaitForDraw(); |
972 EXPECT_TRUE(observer.notified()); | 966 EXPECT_TRUE(observer.notified()); |
973 | 967 |
974 // Setting the transform of a layer should alert the observers. | 968 // Setting the transform of a layer should alert the observers. |
975 observer.Reset(); | 969 observer.Reset(); |
976 gfx::Transform transform; | 970 gfx::Transform transform; |
977 transform.Translate(200.0, 200.0); | 971 transform.Translate(200.0, 200.0); |
978 transform.Rotate(90.0); | 972 transform.Rotate(90.0); |
979 transform.Translate(-200.0, -200.0); | 973 transform.Translate(-200.0, -200.0); |
980 l2->SetTransform(transform); | 974 l2->SetTransform(transform); |
981 WaitForSwap(); | 975 WaitForDraw(); |
982 EXPECT_TRUE(observer.notified()); | 976 EXPECT_TRUE(observer.notified()); |
983 | 977 |
984 // A change resulting in an aborted swap buffer should alert the observer | 978 // A change resulting in an aborted swap buffer should alert the observer |
985 // and also signal an abort. | 979 // and also signal an abort. |
986 observer.Reset(); | 980 observer.Reset(); |
987 l2->SetOpacity(0.1f); | 981 l2->SetOpacity(0.1f); |
988 GetCompositor()->DidAbortSwapBuffers(); | 982 GetCompositor()->DidAbortSwapBuffers(); |
989 WaitForSwap(); | 983 WaitForDraw(); |
990 EXPECT_TRUE(observer.notified()); | 984 EXPECT_TRUE(observer.notified()); |
991 EXPECT_TRUE(observer.aborted()); | 985 EXPECT_TRUE(observer.aborted()); |
992 | 986 |
993 GetCompositor()->RemoveObserver(&observer); | 987 GetCompositor()->RemoveObserver(&observer); |
994 | 988 |
995 // Opacity changes should no longer alert the removed observer. | 989 // Opacity changes should no longer alert the removed observer. |
996 observer.Reset(); | 990 observer.Reset(); |
997 l2->SetOpacity(0.5f); | 991 l2->SetOpacity(0.5f); |
998 WaitForSwap(); | 992 WaitForDraw(); |
999 | 993 |
1000 EXPECT_FALSE(observer.notified()); | 994 EXPECT_FALSE(observer.notified()); |
1001 } | 995 } |
1002 | 996 |
1003 // Checks that modifying the hierarchy correctly affects final composite. | 997 // Checks that modifying the hierarchy correctly affects final composite. |
1004 TEST_F(LayerWithRealCompositorTest, ModifyHierarchy) { | 998 TEST_F(LayerWithRealCompositorTest, ModifyHierarchy) { |
1005 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50)); | 999 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50)); |
1006 | 1000 |
1007 // l0 | 1001 // l0 |
1008 // +-l11 | 1002 // +-l11 |
(...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1554 | 1548 |
1555 child->SetAnimator(LayerAnimator::CreateImplicitAnimator()); | 1549 child->SetAnimator(LayerAnimator::CreateImplicitAnimator()); |
1556 child->SetOpacity(0.5f); | 1550 child->SetOpacity(0.5f); |
1557 EXPECT_TRUE(compositor()->layer_animator_collection()->HasActiveAnimators()); | 1551 EXPECT_TRUE(compositor()->layer_animator_collection()->HasActiveAnimators()); |
1558 | 1552 |
1559 child.reset(); | 1553 child.reset(); |
1560 EXPECT_FALSE(compositor()->layer_animator_collection()->HasActiveAnimators()); | 1554 EXPECT_FALSE(compositor()->layer_animator_collection()->HasActiveAnimators()); |
1561 } | 1555 } |
1562 | 1556 |
1563 } // namespace ui | 1557 } // namespace ui |
OLD | NEW |