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