| 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/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 | 119 |
| 120 bool ReadPixels(SkBitmap* bitmap) { | 120 bool ReadPixels(SkBitmap* bitmap) { |
| 121 return GetCompositor()->ReadPixels(bitmap, | 121 return GetCompositor()->ReadPixels(bitmap, |
| 122 gfx::Rect(GetCompositor()->size())); | 122 gfx::Rect(GetCompositor()->size())); |
| 123 } | 123 } |
| 124 | 124 |
| 125 void WaitForDraw() { | 125 void WaitForDraw() { |
| 126 ui::DrawWaiterForTest::Wait(GetCompositor()); | 126 ui::DrawWaiterForTest::Wait(GetCompositor()); |
| 127 } | 127 } |
| 128 | 128 |
| 129 void WaitForCommit() { |
| 130 ui::DrawWaiterForTest::WaitForCommit(GetCompositor()); |
| 131 } |
| 132 |
| 129 // Invalidates the entire contents of the layer. | 133 // Invalidates the entire contents of the layer. |
| 130 void SchedulePaintForLayer(Layer* layer) { | 134 void SchedulePaintForLayer(Layer* layer) { |
| 131 layer->SchedulePaint( | 135 layer->SchedulePaint( |
| 132 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height())); | 136 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height())); |
| 133 } | 137 } |
| 134 | 138 |
| 135 const base::FilePath& test_data_directory() const { | 139 const base::FilePath& test_data_directory() const { |
| 136 return test_data_directory_; | 140 return test_data_directory_; |
| 137 } | 141 } |
| 138 | 142 |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 return base::Closure(); | 251 return base::Closure(); |
| 248 } | 252 } |
| 249 | 253 |
| 250 DISALLOW_COPY_AND_ASSIGN(NullLayerDelegate); | 254 DISALLOW_COPY_AND_ASSIGN(NullLayerDelegate); |
| 251 }; | 255 }; |
| 252 | 256 |
| 253 // Remembers if it has been notified. | 257 // Remembers if it has been notified. |
| 254 class TestCompositorObserver : public CompositorObserver { | 258 class TestCompositorObserver : public CompositorObserver { |
| 255 public: | 259 public: |
| 256 TestCompositorObserver() | 260 TestCompositorObserver() |
| 257 : started_(false), ended_(false), aborted_(false) {} | 261 : committed_(false), started_(false), ended_(false), aborted_(false) {} |
| 258 | 262 |
| 263 bool committed() const { return committed_; } |
| 259 bool notified() const { return started_ && ended_; } | 264 bool notified() const { return started_ && ended_; } |
| 260 bool aborted() const { return aborted_; } | 265 bool aborted() const { return aborted_; } |
| 261 | 266 |
| 262 void Reset() { | 267 void Reset() { |
| 268 committed_ = false; |
| 263 started_ = false; | 269 started_ = false; |
| 264 ended_ = false; | 270 ended_ = false; |
| 265 aborted_ = false; | 271 aborted_ = false; |
| 266 } | 272 } |
| 267 | 273 |
| 268 private: | 274 private: |
| 269 virtual void OnCompositingDidCommit(Compositor* compositor) OVERRIDE { | 275 virtual void OnCompositingDidCommit(Compositor* compositor) OVERRIDE { |
| 276 committed_ = true; |
| 270 } | 277 } |
| 271 | 278 |
| 272 virtual void OnCompositingStarted(Compositor* compositor, | 279 virtual void OnCompositingStarted(Compositor* compositor, |
| 273 base::TimeTicks start_time) OVERRIDE { | 280 base::TimeTicks start_time) OVERRIDE { |
| 274 started_ = true; | 281 started_ = true; |
| 275 } | 282 } |
| 276 | 283 |
| 277 virtual void OnCompositingEnded(Compositor* compositor) OVERRIDE { | 284 virtual void OnCompositingEnded(Compositor* compositor) OVERRIDE { |
| 278 ended_ = true; | 285 ended_ = true; |
| 279 } | 286 } |
| 280 | 287 |
| 281 virtual void OnCompositingAborted(Compositor* compositor) OVERRIDE { | 288 virtual void OnCompositingAborted(Compositor* compositor) OVERRIDE { |
| 282 aborted_ = true; | 289 aborted_ = true; |
| 283 } | 290 } |
| 284 | 291 |
| 285 virtual void OnCompositingLockStateChanged(Compositor* compositor) OVERRIDE { | 292 virtual void OnCompositingLockStateChanged(Compositor* compositor) OVERRIDE { |
| 286 } | 293 } |
| 287 | 294 |
| 288 virtual void OnUpdateVSyncParameters(Compositor* compositor, | 295 virtual void OnUpdateVSyncParameters(Compositor* compositor, |
| 289 base::TimeTicks timebase, | 296 base::TimeTicks timebase, |
| 290 base::TimeDelta interval) OVERRIDE { | 297 base::TimeDelta interval) OVERRIDE { |
| 291 } | 298 } |
| 292 | 299 |
| 300 bool committed_; |
| 293 bool started_; | 301 bool started_; |
| 294 bool ended_; | 302 bool ended_; |
| 295 bool aborted_; | 303 bool aborted_; |
| 296 | 304 |
| 297 DISALLOW_COPY_AND_ASSIGN(TestCompositorObserver); | 305 DISALLOW_COPY_AND_ASSIGN(TestCompositorObserver); |
| 298 }; | 306 }; |
| 299 | 307 |
| 300 } // namespace | 308 } // namespace |
| 301 | 309 |
| 302 #if defined(OS_WIN) | 310 #if defined(OS_WIN) |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 // Invokes DrawTree on the compositor. | 419 // Invokes DrawTree on the compositor. |
| 412 void Draw() { | 420 void Draw() { |
| 413 compositor()->ScheduleDraw(); | 421 compositor()->ScheduleDraw(); |
| 414 WaitForDraw(); | 422 WaitForDraw(); |
| 415 } | 423 } |
| 416 | 424 |
| 417 void WaitForDraw() { | 425 void WaitForDraw() { |
| 418 DrawWaiterForTest::Wait(compositor()); | 426 DrawWaiterForTest::Wait(compositor()); |
| 419 } | 427 } |
| 420 | 428 |
| 429 void WaitForCommit() { |
| 430 DrawWaiterForTest::WaitForCommit(compositor()); |
| 431 } |
| 432 |
| 421 // CompositorDelegate overrides. | 433 // CompositorDelegate overrides. |
| 422 virtual void ScheduleDraw() OVERRIDE { | 434 virtual void ScheduleDraw() OVERRIDE { |
| 423 DCHECK(!ui::Compositor::WasInitializedWithThread()); | 435 DCHECK(!ui::Compositor::WasInitializedWithThread()); |
| 424 if (compositor_) { | 436 if (compositor_) { |
| 425 base::MessageLoop::current()->PostTask( | 437 base::MessageLoop::current()->PostTask( |
| 426 FROM_HERE, base::Bind(&Compositor::Draw, compositor_->AsWeakPtr())); | 438 FROM_HERE, base::Bind(&Compositor::Draw, compositor_->AsWeakPtr())); |
| 427 } | 439 } |
| 428 } | 440 } |
| 429 | 441 |
| 430 private: | 442 private: |
| (...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 812 gfx::Rect(10, 10, 350, 350))); | 824 gfx::Rect(10, 10, 350, 350))); |
| 813 l1->Add(l2.get()); | 825 l1->Add(l2.get()); |
| 814 TestCompositorObserver observer; | 826 TestCompositorObserver observer; |
| 815 GetCompositor()->AddObserver(&observer); | 827 GetCompositor()->AddObserver(&observer); |
| 816 | 828 |
| 817 // Explicitly called DrawTree should cause the observers to be notified. | 829 // Explicitly called DrawTree should cause the observers to be notified. |
| 818 // NOTE: this call to DrawTree sets l1 to be the compositor's root layer. | 830 // NOTE: this call to DrawTree sets l1 to be the compositor's root layer. |
| 819 DrawTree(l1.get()); | 831 DrawTree(l1.get()); |
| 820 EXPECT_TRUE(observer.notified()); | 832 EXPECT_TRUE(observer.notified()); |
| 821 | 833 |
| 822 // As should scheduling a draw and waiting. | 834 // ScheduleDraw without any visible change should cause a commit. |
| 823 observer.Reset(); | 835 observer.Reset(); |
| 824 l1->ScheduleDraw(); | 836 l1->ScheduleDraw(); |
| 825 WaitForDraw(); | 837 WaitForCommit(); |
| 826 EXPECT_TRUE(observer.notified()); | 838 EXPECT_TRUE(observer.committed()); |
| 827 | 839 |
| 828 // Moving, but not resizing, a layer should alert the observers. | 840 // Moving, but not resizing, a layer should alert the observers. |
| 829 observer.Reset(); | 841 observer.Reset(); |
| 830 l2->SetBounds(gfx::Rect(0, 0, 350, 350)); | 842 l2->SetBounds(gfx::Rect(0, 0, 350, 350)); |
| 831 WaitForDraw(); | 843 WaitForDraw(); |
| 832 EXPECT_TRUE(observer.notified()); | 844 EXPECT_TRUE(observer.notified()); |
| 833 | 845 |
| 834 // So should resizing a layer. | 846 // So should resizing a layer. |
| 835 observer.Reset(); | 847 observer.Reset(); |
| 836 l2->SetBounds(gfx::Rect(0, 0, 400, 400)); | 848 l2->SetBounds(gfx::Rect(0, 0, 400, 400)); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 910 // WritePNGFile(bitmap, ref_img1); | 922 // WritePNGFile(bitmap, ref_img1); |
| 911 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true))); | 923 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true))); |
| 912 | 924 |
| 913 l0->StackAtTop(l11.get()); | 925 l0->StackAtTop(l11.get()); |
| 914 DrawTree(l0.get()); | 926 DrawTree(l0.get()); |
| 915 ASSERT_TRUE(ReadPixels(&bitmap)); | 927 ASSERT_TRUE(ReadPixels(&bitmap)); |
| 916 ASSERT_FALSE(bitmap.empty()); | 928 ASSERT_FALSE(bitmap.empty()); |
| 917 // WritePNGFile(bitmap, ref_img2); | 929 // WritePNGFile(bitmap, ref_img2); |
| 918 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true))); | 930 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true))); |
| 919 | 931 |
| 920 // l11 is already at the front, should have no effect. | 932 // should restore to original configuration |
| 933 l0->StackAbove(l12.get(), l11.get()); |
| 934 DrawTree(l0.get()); |
| 935 ASSERT_TRUE(ReadPixels(&bitmap)); |
| 936 ASSERT_FALSE(bitmap.empty()); |
| 937 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true))); |
| 938 |
| 939 // l11 back to front |
| 921 l0->StackAtTop(l11.get()); | 940 l0->StackAtTop(l11.get()); |
| 922 DrawTree(l0.get()); | 941 DrawTree(l0.get()); |
| 923 ASSERT_TRUE(ReadPixels(&bitmap)); | 942 ASSERT_TRUE(ReadPixels(&bitmap)); |
| 924 ASSERT_FALSE(bitmap.empty()); | 943 ASSERT_FALSE(bitmap.empty()); |
| 925 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true))); | |
| 926 | |
| 927 // l11 is already at the front, should have no effect. | |
| 928 l0->StackAbove(l11.get(), l12.get()); | |
| 929 DrawTree(l0.get()); | |
| 930 ASSERT_TRUE(ReadPixels(&bitmap)); | |
| 931 ASSERT_FALSE(bitmap.empty()); | |
| 932 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true))); | 944 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true))); |
| 933 | 945 |
| 934 // should restore to original configuration | 946 // should restore to original configuration |
| 935 l0->StackAbove(l12.get(), l11.get()); | 947 l0->StackAbove(l12.get(), l11.get()); |
| 936 DrawTree(l0.get()); | 948 DrawTree(l0.get()); |
| 937 ASSERT_TRUE(ReadPixels(&bitmap)); | 949 ASSERT_TRUE(ReadPixels(&bitmap)); |
| 938 ASSERT_FALSE(bitmap.empty()); | 950 ASSERT_FALSE(bitmap.empty()); |
| 939 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true))); | 951 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true))); |
| 952 |
| 953 // l11 back to front |
| 954 l0->StackAbove(l11.get(), l12.get()); |
| 955 DrawTree(l0.get()); |
| 956 ASSERT_TRUE(ReadPixels(&bitmap)); |
| 957 ASSERT_FALSE(bitmap.empty()); |
| 958 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true))); |
| 940 } | 959 } |
| 941 | 960 |
| 942 // Opacity is rendered correctly. | 961 // Opacity is rendered correctly. |
| 943 // Checks that modifying the hierarchy correctly affects final composite. | 962 // Checks that modifying the hierarchy correctly affects final composite. |
| 944 TEST_F(LayerWithRealCompositorTest, MAYBE_Opacity) { | 963 TEST_F(LayerWithRealCompositorTest, MAYBE_Opacity) { |
| 945 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50)); | 964 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50)); |
| 946 | 965 |
| 947 // l0 | 966 // l0 |
| 948 // +-l11 | 967 // +-l11 |
| 949 scoped_ptr<Layer> l0(CreateColorLayer(SK_ColorRED, | 968 scoped_ptr<Layer> l0(CreateColorLayer(SK_ColorRED, |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1026 SchedulePaintLayerDelegate child_delegate; | 1045 SchedulePaintLayerDelegate child_delegate; |
| 1027 scoped_ptr<Layer> child(CreateColorLayer(SK_ColorBLUE, | 1046 scoped_ptr<Layer> child(CreateColorLayer(SK_ColorBLUE, |
| 1028 gfx::Rect(0, 0, 200, 200))); | 1047 gfx::Rect(0, 0, 200, 200))); |
| 1029 child_delegate.set_layer(child.get()); | 1048 child_delegate.set_layer(child.get()); |
| 1030 | 1049 |
| 1031 root->Add(child.get()); | 1050 root->Add(child.get()); |
| 1032 | 1051 |
| 1033 SchedulePaintForLayer(root.get()); | 1052 SchedulePaintForLayer(root.get()); |
| 1034 DrawTree(root.get()); | 1053 DrawTree(root.get()); |
| 1035 child->SchedulePaint(gfx::Rect(0, 0, 20, 20)); | 1054 child->SchedulePaint(gfx::Rect(0, 0, 20, 20)); |
| 1036 child_delegate.GetPaintCountAndClear(); | 1055 EXPECT_EQ(1, child_delegate.GetPaintCountAndClear()); |
| 1037 | 1056 |
| 1038 // Set a rect so that when OnPaintLayer() is invoked SchedulePaint is invoked | 1057 // Set a rect so that when OnPaintLayer() is invoked SchedulePaint is invoked |
| 1039 // again. | 1058 // again. |
| 1040 child_delegate.SetSchedulePaintRect(gfx::Rect(10, 10, 30, 30)); | 1059 child_delegate.SetSchedulePaintRect(gfx::Rect(10, 10, 30, 30)); |
| 1041 WaitForDraw(); | 1060 WaitForCommit(); |
| 1042 // |child| should have been painted once. | |
| 1043 EXPECT_EQ(1, child_delegate.GetPaintCountAndClear()); | 1061 EXPECT_EQ(1, child_delegate.GetPaintCountAndClear()); |
| 1044 | 1062 |
| 1045 // Because SchedulePaint() was invoked from OnPaintLayer() |child| should | 1063 // Because SchedulePaint() was invoked from OnPaintLayer() |child| should |
| 1046 // still need to be painted. | 1064 // still need to be painted. |
| 1047 WaitForDraw(); | 1065 WaitForCommit(); |
| 1048 EXPECT_EQ(1, child_delegate.GetPaintCountAndClear()); | 1066 EXPECT_EQ(1, child_delegate.GetPaintCountAndClear()); |
| 1049 EXPECT_TRUE(child_delegate.last_clip_rect().Contains( | 1067 EXPECT_TRUE(child_delegate.last_clip_rect().Contains( |
| 1050 gfx::Rect(10, 10, 30, 30))); | 1068 gfx::Rect(10, 10, 30, 30))); |
| 1051 } | 1069 } |
| 1052 | 1070 |
| 1053 TEST_F(LayerWithRealCompositorTest, MAYBE_ScaleUpDown) { | 1071 TEST_F(LayerWithRealCompositorTest, MAYBE_ScaleUpDown) { |
| 1054 scoped_ptr<Layer> root(CreateColorLayer(SK_ColorWHITE, | 1072 scoped_ptr<Layer> root(CreateColorLayer(SK_ColorWHITE, |
| 1055 gfx::Rect(10, 20, 200, 220))); | 1073 gfx::Rect(10, 20, 200, 220))); |
| 1056 TestLayerDelegate root_delegate; | 1074 TestLayerDelegate root_delegate; |
| 1057 root_delegate.AddColor(SK_ColorWHITE); | 1075 root_delegate.AddColor(SK_ColorWHITE); |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1220 DrawTree(root.get()); | 1238 DrawTree(root.get()); |
| 1221 | 1239 |
| 1222 // Reset into invisible state. | 1240 // Reset into invisible state. |
| 1223 child->SetVisible(false); | 1241 child->SetVisible(false); |
| 1224 DrawTree(root.get()); | 1242 DrawTree(root.get()); |
| 1225 delegate.Reset(); | 1243 delegate.Reset(); |
| 1226 | 1244 |
| 1227 // Move layer. | 1245 // Move layer. |
| 1228 child->SetBounds(gfx::Rect(200, 200, 500, 500)); | 1246 child->SetBounds(gfx::Rect(200, 200, 500, 500)); |
| 1229 child->SetVisible(true); | 1247 child->SetVisible(true); |
| 1230 WaitForDraw(); | |
| 1231 DrawTree(root.get()); | 1248 DrawTree(root.get()); |
| 1232 EXPECT_FALSE(delegate.painted()); | 1249 EXPECT_FALSE(delegate.painted()); |
| 1233 | 1250 |
| 1234 // Reset into invisible state. | 1251 // Reset into invisible state. |
| 1235 child->SetVisible(false); | 1252 child->SetVisible(false); |
| 1236 DrawTree(root.get()); | 1253 DrawTree(root.get()); |
| 1237 delegate.Reset(); | 1254 delegate.Reset(); |
| 1238 | 1255 |
| 1239 // Resize layer. | 1256 // Resize layer. |
| 1240 child->SetBounds(gfx::Rect(200, 200, 400, 400)); | 1257 child->SetBounds(gfx::Rect(200, 200, 400, 400)); |
| 1241 child->SetVisible(true); | 1258 child->SetVisible(true); |
| 1242 WaitForDraw(); | |
| 1243 DrawTree(root.get()); | 1259 DrawTree(root.get()); |
| 1244 EXPECT_TRUE(delegate.painted()); | 1260 EXPECT_TRUE(delegate.painted()); |
| 1245 } | 1261 } |
| 1246 | 1262 |
| 1247 static scoped_ptr<cc::DelegatedFrameData> MakeFrameData(gfx::Size size) { | 1263 static scoped_ptr<cc::DelegatedFrameData> MakeFrameData(gfx::Size size) { |
| 1248 scoped_ptr<cc::DelegatedFrameData> frame_data(new cc::DelegatedFrameData); | 1264 scoped_ptr<cc::DelegatedFrameData> frame_data(new cc::DelegatedFrameData); |
| 1249 scoped_ptr<cc::RenderPass> render_pass(cc::RenderPass::Create()); | 1265 scoped_ptr<cc::RenderPass> render_pass(cc::RenderPass::Create()); |
| 1250 render_pass->SetNew(cc::RenderPass::Id(1, 1), | 1266 render_pass->SetNew(cc::RenderPass::Id(1, 1), |
| 1251 gfx::Rect(size), | 1267 gfx::Rect(size), |
| 1252 gfx::RectF(), | 1268 gfx::RectF(), |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1359 l1->SetOpacity(0.5f); | 1375 l1->SetOpacity(0.5f); |
| 1360 | 1376 |
| 1361 // Change l1's cc::Layer. | 1377 // Change l1's cc::Layer. |
| 1362 l1->SwitchCCLayerForTest(); | 1378 l1->SwitchCCLayerForTest(); |
| 1363 | 1379 |
| 1364 // Ensure that the opacity animation completed. | 1380 // Ensure that the opacity animation completed. |
| 1365 EXPECT_FLOAT_EQ(l1->opacity(), 0.5f); | 1381 EXPECT_FLOAT_EQ(l1->opacity(), 0.5f); |
| 1366 } | 1382 } |
| 1367 | 1383 |
| 1368 } // namespace ui | 1384 } // namespace ui |
| OLD | NEW |