OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "cc/layers/texture_layer.h" | 5 #include "cc/layers/texture_layer.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
| 11 #include "base/threading/thread.h" |
| 12 #include "base/time/time.h" |
11 #include "cc/debug/test_web_graphics_context_3d.h" | 13 #include "cc/debug/test_web_graphics_context_3d.h" |
12 #include "cc/layers/texture_layer_client.h" | 14 #include "cc/layers/texture_layer_client.h" |
13 #include "cc/layers/texture_layer_impl.h" | 15 #include "cc/layers/texture_layer_impl.h" |
14 #include "cc/resources/returned_resource.h" | 16 #include "cc/resources/returned_resource.h" |
15 #include "cc/test/fake_impl_proxy.h" | 17 #include "cc/test/fake_impl_proxy.h" |
16 #include "cc/test/fake_layer_tree_host_client.h" | 18 #include "cc/test/fake_layer_tree_host_client.h" |
17 #include "cc/test/fake_layer_tree_host_impl.h" | 19 #include "cc/test/fake_layer_tree_host_impl.h" |
18 #include "cc/test/fake_output_surface.h" | 20 #include "cc/test/fake_output_surface.h" |
19 #include "cc/test/layer_test_common.h" | 21 #include "cc/test/layer_test_common.h" |
20 #include "cc/test/layer_tree_test.h" | 22 #include "cc/test/layer_tree_test.h" |
(...skipping 833 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
854 base::ThreadChecker main_thread_; | 856 base::ThreadChecker main_thread_; |
855 int callback_count_; | 857 int callback_count_; |
856 int commit_count_; | 858 int commit_count_; |
857 scoped_refptr<Layer> root_; | 859 scoped_refptr<Layer> root_; |
858 scoped_refptr<TextureLayer> layer_; | 860 scoped_refptr<TextureLayer> layer_; |
859 }; | 861 }; |
860 | 862 |
861 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F( | 863 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F( |
862 TextureLayerImplWithMailboxThreadedCallback); | 864 TextureLayerImplWithMailboxThreadedCallback); |
863 | 865 |
| 866 |
| 867 class TextureLayerNoMailboxIsActivatedDuringCommit : public LayerTreeTest, |
| 868 public TextureLayerClient { |
| 869 protected: |
| 870 TextureLayerNoMailboxIsActivatedDuringCommit() |
| 871 : wait_thread_("WAIT"), |
| 872 wait_event_(false, false) { |
| 873 wait_thread_.Start(); |
| 874 } |
| 875 |
| 876 virtual void BeginTest() OVERRIDE { |
| 877 activate_count_ = 0; |
| 878 |
| 879 gfx::Size bounds(100, 100); |
| 880 root_ = Layer::Create(); |
| 881 root_->SetAnchorPoint(gfx::PointF()); |
| 882 root_->SetBounds(bounds); |
| 883 |
| 884 layer_ = TextureLayer::Create(this); |
| 885 layer_->SetIsDrawable(true); |
| 886 layer_->SetAnchorPoint(gfx::PointF()); |
| 887 layer_->SetBounds(bounds); |
| 888 |
| 889 root_->AddChild(layer_); |
| 890 layer_tree_host()->SetRootLayer(root_); |
| 891 layer_tree_host()->SetViewportSize(bounds); |
| 892 |
| 893 PostSetNeedsCommitToMainThread(); |
| 894 } |
| 895 |
| 896 // TextureLayerClient implementation. |
| 897 virtual unsigned PrepareTexture() OVERRIDE { |
| 898 return OffscreenContextProviderForMainThread() |
| 899 ->Context3d()->createTexture(); |
| 900 } |
| 901 virtual WebKit::WebGraphicsContext3D* Context3d() OVERRIDE { |
| 902 return OffscreenContextProviderForMainThread()->Context3d(); |
| 903 } |
| 904 virtual bool PrepareTextureMailbox(TextureMailbox* mailbox, |
| 905 bool use_shared_memory) OVERRIDE { |
| 906 return false; |
| 907 } |
| 908 |
| 909 virtual void WillActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE { |
| 910 // Slow down activation so the main thread DidCommit() will run if |
| 911 // not blocked. |
| 912 wait_thread_.message_loop()->PostDelayedTask( |
| 913 FROM_HERE, |
| 914 base::Bind(&base::WaitableEvent::Signal, |
| 915 base::Unretained(&wait_event_)), |
| 916 base::TimeDelta::FromMilliseconds(10)); |
| 917 wait_event_.Wait(); |
| 918 } |
| 919 |
| 920 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE { |
| 921 { |
| 922 base::AutoLock lock(activate_lock_); |
| 923 ++activate_count_; |
| 924 } |
| 925 |
| 926 proxy()->MainThreadTaskRunner()->PostTask( |
| 927 FROM_HERE, |
| 928 base::Bind(&TextureLayerNoMailboxIsActivatedDuringCommit::DidActivate, |
| 929 base::Unretained(this))); |
| 930 } |
| 931 |
| 932 void DidActivate() { |
| 933 base::AutoLock lock(activate_lock_); |
| 934 switch (activate_count_) { |
| 935 case 1: |
| 936 // The first texture has been activated. Invalidate the layer so it |
| 937 // grabs a new texture id from the client. |
| 938 layer_->SetNeedsDisplay(); |
| 939 // So this commit number should complete after the second activate. |
| 940 EXPECT_EQ(1, layer_tree_host()->source_frame_number()); |
| 941 break; |
| 942 case 2: |
| 943 // The second mailbox has been activated. Remove the layer from |
| 944 // the tree to cause another commit/activation. The commit should |
| 945 // finish *after* the layer is removed from the active tree. |
| 946 layer_->RemoveFromParent(); |
| 947 // So this commit number should complete after the third activate. |
| 948 EXPECT_EQ(2, layer_tree_host()->source_frame_number()); |
| 949 } |
| 950 } |
| 951 |
| 952 virtual void DidCommit() OVERRIDE { |
| 953 switch (layer_tree_host()->source_frame_number()) { |
| 954 case 2: { |
| 955 // The activate for the 2nd texture should have happened before now. |
| 956 base::AutoLock lock(activate_lock_); |
| 957 EXPECT_EQ(2, activate_count_); |
| 958 break; |
| 959 } |
| 960 case 3: { |
| 961 // The activate to remove the layer should have happened before now. |
| 962 base::AutoLock lock(activate_lock_); |
| 963 EXPECT_EQ(3, activate_count_); |
| 964 |
| 965 EndTest(); |
| 966 break; |
| 967 } |
| 968 } |
| 969 } |
| 970 |
| 971 |
| 972 virtual void AfterTest() OVERRIDE {} |
| 973 |
| 974 base::Thread wait_thread_; |
| 975 base::WaitableEvent wait_event_; |
| 976 base::Lock activate_lock_; |
| 977 int activate_count_; |
| 978 int activate_commit_; |
| 979 scoped_refptr<Layer> root_; |
| 980 scoped_refptr<TextureLayer> layer_; |
| 981 }; |
| 982 |
| 983 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F( |
| 984 TextureLayerNoMailboxIsActivatedDuringCommit); |
| 985 |
| 986 class TextureLayerMailboxIsActivatedDuringCommit : public LayerTreeTest { |
| 987 protected: |
| 988 TextureLayerMailboxIsActivatedDuringCommit() |
| 989 : wait_thread_("WAIT"), |
| 990 wait_event_(false, false) { |
| 991 wait_thread_.Start(); |
| 992 } |
| 993 |
| 994 static void ReleaseCallback(unsigned sync_point, bool lost_resource) {} |
| 995 |
| 996 void SetMailbox(char mailbox_char) { |
| 997 TextureMailbox mailbox( |
| 998 std::string(64, mailbox_char), |
| 999 base::Bind( |
| 1000 &TextureLayerMailboxIsActivatedDuringCommit::ReleaseCallback)); |
| 1001 layer_->SetTextureMailbox(mailbox); |
| 1002 } |
| 1003 |
| 1004 virtual void BeginTest() OVERRIDE { |
| 1005 activate_count_ = 0; |
| 1006 |
| 1007 gfx::Size bounds(100, 100); |
| 1008 root_ = Layer::Create(); |
| 1009 root_->SetAnchorPoint(gfx::PointF()); |
| 1010 root_->SetBounds(bounds); |
| 1011 |
| 1012 layer_ = TextureLayer::CreateForMailbox(NULL); |
| 1013 layer_->SetIsDrawable(true); |
| 1014 layer_->SetAnchorPoint(gfx::PointF()); |
| 1015 layer_->SetBounds(bounds); |
| 1016 |
| 1017 root_->AddChild(layer_); |
| 1018 layer_tree_host()->SetRootLayer(root_); |
| 1019 layer_tree_host()->SetViewportSize(bounds); |
| 1020 SetMailbox('1'); |
| 1021 |
| 1022 PostSetNeedsCommitToMainThread(); |
| 1023 } |
| 1024 |
| 1025 virtual void WillActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE { |
| 1026 // Slow down activation so the main thread DidCommit() will run if |
| 1027 // not blocked. |
| 1028 wait_thread_.message_loop()->PostDelayedTask( |
| 1029 FROM_HERE, |
| 1030 base::Bind(&base::WaitableEvent::Signal, |
| 1031 base::Unretained(&wait_event_)), |
| 1032 base::TimeDelta::FromMilliseconds(10)); |
| 1033 wait_event_.Wait(); |
| 1034 } |
| 1035 |
| 1036 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE { |
| 1037 { |
| 1038 base::AutoLock lock(activate_lock_); |
| 1039 ++activate_count_; |
| 1040 } |
| 1041 |
| 1042 proxy()->MainThreadTaskRunner()->PostTask( |
| 1043 FROM_HERE, |
| 1044 base::Bind(&TextureLayerMailboxIsActivatedDuringCommit::DidActivate, |
| 1045 base::Unretained(this))); |
| 1046 } |
| 1047 |
| 1048 void DidActivate() { |
| 1049 base::AutoLock lock(activate_lock_); |
| 1050 switch (activate_count_) { |
| 1051 case 1: |
| 1052 // The first mailbox has been activated. Set a new mailbox, and |
| 1053 // expect the next commit to finish *after* it is activated. |
| 1054 SetMailbox('2'); |
| 1055 // So this commit number should complete after the second activate. |
| 1056 EXPECT_EQ(1, layer_tree_host()->source_frame_number()); |
| 1057 break; |
| 1058 case 2: |
| 1059 // The second mailbox has been activated. Remove the layer from |
| 1060 // the tree to cause another commit/activation. The commit should |
| 1061 // finish *after* the layer is removed from the active tree. |
| 1062 layer_->RemoveFromParent(); |
| 1063 // So this commit number should complete after the third activate. |
| 1064 EXPECT_EQ(2, layer_tree_host()->source_frame_number()); |
| 1065 } |
| 1066 } |
| 1067 |
| 1068 virtual void DidCommit() OVERRIDE { |
| 1069 switch (layer_tree_host()->source_frame_number()) { |
| 1070 case 2: { |
| 1071 // The activate for the 2nd mailbox should have happened before now. |
| 1072 base::AutoLock lock(activate_lock_); |
| 1073 EXPECT_EQ(2, activate_count_); |
| 1074 break; |
| 1075 } |
| 1076 case 3: { |
| 1077 // The activate to remove the layer should have happened before now. |
| 1078 base::AutoLock lock(activate_lock_); |
| 1079 EXPECT_EQ(3, activate_count_); |
| 1080 |
| 1081 EndTest(); |
| 1082 break; |
| 1083 } |
| 1084 } |
| 1085 } |
| 1086 |
| 1087 |
| 1088 virtual void AfterTest() OVERRIDE {} |
| 1089 |
| 1090 base::Thread wait_thread_; |
| 1091 base::WaitableEvent wait_event_; |
| 1092 base::Lock activate_lock_; |
| 1093 int activate_count_; |
| 1094 scoped_refptr<Layer> root_; |
| 1095 scoped_refptr<TextureLayer> layer_; |
| 1096 }; |
| 1097 |
| 1098 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F( |
| 1099 TextureLayerMailboxIsActivatedDuringCommit); |
| 1100 |
864 class TextureLayerImplWithMailboxTest : public TextureLayerTest { | 1101 class TextureLayerImplWithMailboxTest : public TextureLayerTest { |
865 protected: | 1102 protected: |
866 TextureLayerImplWithMailboxTest() | 1103 TextureLayerImplWithMailboxTest() |
867 : fake_client_( | 1104 : fake_client_( |
868 FakeLayerTreeHostClient(FakeLayerTreeHostClient::DIRECT_3D)) {} | 1105 FakeLayerTreeHostClient(FakeLayerTreeHostClient::DIRECT_3D)) {} |
869 | 1106 |
870 virtual void SetUp() { | 1107 virtual void SetUp() { |
871 TextureLayerTest::SetUp(); | 1108 TextureLayerTest::SetUp(); |
872 layer_tree_host_.reset(new MockLayerTreeHost(&fake_client_)); | 1109 layer_tree_host_.reset(new MockLayerTreeHost(&fake_client_)); |
873 EXPECT_TRUE(host_impl_.InitializeRenderer(CreateFakeOutputSurface())); | 1110 EXPECT_TRUE(host_impl_.InitializeRenderer(CreateFakeOutputSurface())); |
(...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1431 int callback_count_; | 1668 int callback_count_; |
1432 scoped_refptr<Layer> root_; | 1669 scoped_refptr<Layer> root_; |
1433 scoped_refptr<TextureLayer> layer_; | 1670 scoped_refptr<TextureLayer> layer_; |
1434 }; | 1671 }; |
1435 | 1672 |
1436 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F( | 1673 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F( |
1437 TextureLayerWithMailboxImplThreadDeleted); | 1674 TextureLayerWithMailboxImplThreadDeleted); |
1438 | 1675 |
1439 } // namespace | 1676 } // namespace |
1440 } // namespace cc | 1677 } // namespace cc |
OLD | NEW |