[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 5 | #include "cc/layers/texture_layer.h" |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 6 | |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 7 | #include <string> |
| 8 | |
| 9 | #include "base/callback.h" |
[email protected] | 681ccff | 2013-03-18 06:13:52 | [diff] [blame] | 10 | #include "cc/base/thread.h" |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 11 | #include "cc/layers/texture_layer_impl.h" |
[email protected] | 586d51ed | 2012-12-07 20:31:45 | [diff] [blame] | 12 | #include "cc/test/fake_impl_proxy.h" |
[email protected] | 101441ce | 2012-10-16 01:45:03 | [diff] [blame] | 13 | #include "cc/test/fake_layer_tree_host_client.h" |
[email protected] | 586d51ed | 2012-12-07 20:31:45 | [diff] [blame] | 14 | #include "cc/test/fake_layer_tree_host_impl.h" |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 15 | #include "cc/test/layer_tree_test.h" |
[email protected] | 556fd29 | 2013-03-18 08:03:04 | [diff] [blame] | 16 | #include "cc/trees/layer_tree_host.h" |
| 17 | #include "cc/trees/layer_tree_impl.h" |
| 18 | #include "cc/trees/single_thread_proxy.h" |
[email protected] | 7f0c53db | 2012-10-02 00:23:18 | [diff] [blame] | 19 | #include "testing/gmock/include/gmock/gmock.h" |
| 20 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 21 | |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 22 | using ::testing::Mock; |
| 23 | using ::testing::_; |
| 24 | using ::testing::AtLeast; |
| 25 | using ::testing::AnyNumber; |
| 26 | |
[email protected] | ba56574 | 2012-11-10 09:29:48 | [diff] [blame] | 27 | namespace cc { |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 28 | namespace { |
| 29 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 30 | class MockLayerTreeHost : public LayerTreeHost { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 31 | public: |
[email protected] | bf691c2 | 2013-03-26 21:15:06 | [diff] [blame^] | 32 | explicit MockLayerTreeHost(LayerTreeHostClient* client) |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 33 | : LayerTreeHost(client, LayerTreeSettings()) { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 34 | Initialize(scoped_ptr<Thread>(NULL)); |
| 35 | } |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 36 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 37 | MOCK_METHOD0(AcquireLayerTextures, void()); |
| 38 | MOCK_METHOD0(SetNeedsCommit, void()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 39 | }; |
| 40 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 41 | class TextureLayerTest : public testing::Test { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 42 | public: |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 43 | TextureLayerTest() |
| 44 | : fake_client_( |
| 45 | FakeLayerTreeHostClient(FakeLayerTreeHostClient::DIRECT_3D)), |
| 46 | host_impl_(&proxy_) {} |
[email protected] | 0f077a5 | 2012-09-08 01:45:24 | [diff] [blame] | 47 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 48 | protected: |
| 49 | virtual void SetUp() { |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 50 | layer_tree_host_.reset(new MockLayerTreeHost(&fake_client_)); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 51 | } |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 52 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 53 | virtual void TearDown() { |
| 54 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 55 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber()); |
| 56 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 57 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 58 | layer_tree_host_->SetRootLayer(NULL); |
| 59 | layer_tree_host_.reset(); |
| 60 | } |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 61 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 62 | scoped_ptr<MockLayerTreeHost> layer_tree_host_; |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 63 | FakeImplProxy proxy_; |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 64 | FakeLayerTreeHostClient fake_client_; |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 65 | FakeLayerTreeHostImpl host_impl_; |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 66 | }; |
| 67 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 68 | TEST_F(TextureLayerTest, SyncImplWhenChangingTextureId) { |
| 69 | scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL); |
| 70 | ASSERT_TRUE(test_layer); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 71 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 72 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber()); |
| 73 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
| 74 | layer_tree_host_->SetRootLayer(test_layer); |
| 75 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 76 | EXPECT_EQ(test_layer->layer_tree_host(), layer_tree_host_.get()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 77 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 78 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 79 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 80 | test_layer->SetTextureId(1); |
| 81 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 82 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 83 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1)); |
| 84 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 85 | test_layer->SetTextureId(2); |
| 86 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 87 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 88 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1)); |
| 89 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 90 | test_layer->SetTextureId(0); |
| 91 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 92 | } |
| 93 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 94 | TEST_F(TextureLayerTest, SyncImplWhenDrawing) { |
| 95 | gfx::RectF dirty_rect(0.f, 0.f, 1.f, 1.f); |
[email protected] | 031ccfff | 2012-10-26 00:57:38 | [diff] [blame] | 96 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 97 | scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL); |
| 98 | ASSERT_TRUE(test_layer); |
| 99 | scoped_ptr<TextureLayerImpl> impl_layer; |
| 100 | impl_layer = TextureLayerImpl::Create(host_impl_.active_tree(), 1, false); |
| 101 | ASSERT_TRUE(impl_layer); |
[email protected] | 031ccfff | 2012-10-26 00:57:38 | [diff] [blame] | 102 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 103 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber()); |
| 104 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
| 105 | layer_tree_host_->SetRootLayer(test_layer); |
| 106 | test_layer->SetTextureId(1); |
| 107 | test_layer->SetIsDrawable(true); |
| 108 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 109 | EXPECT_EQ(test_layer->layer_tree_host(), layer_tree_host_.get()); |
[email protected] | 031ccfff | 2012-10-26 00:57:38 | [diff] [blame] | 110 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 111 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(1); |
| 112 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0); |
| 113 | test_layer->WillModifyTexture(); |
| 114 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | 031ccfff | 2012-10-26 00:57:38 | [diff] [blame] | 115 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 116 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 117 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(1); |
| 118 | test_layer->SetNeedsDisplayRect(dirty_rect); |
| 119 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | 031ccfff | 2012-10-26 00:57:38 | [diff] [blame] | 120 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 121 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 122 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(1); |
| 123 | test_layer->PushPropertiesTo(impl_layer.get()); // fake commit |
| 124 | test_layer->SetIsDrawable(false); |
| 125 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | 031ccfff | 2012-10-26 00:57:38 | [diff] [blame] | 126 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 127 | // Verify that non-drawable layers don't signal the compositor, |
| 128 | // except for the first draw after last commit, which must acquire |
| 129 | // the texture. |
| 130 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(1); |
| 131 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0); |
| 132 | test_layer->WillModifyTexture(); |
| 133 | test_layer->SetNeedsDisplayRect(dirty_rect); |
| 134 | test_layer->PushPropertiesTo(impl_layer.get()); // fake commit |
| 135 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | 031ccfff | 2012-10-26 00:57:38 | [diff] [blame] | 136 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 137 | // Second draw with layer in non-drawable state: no texture |
| 138 | // acquisition. |
| 139 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 140 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0); |
| 141 | test_layer->WillModifyTexture(); |
| 142 | test_layer->SetNeedsDisplayRect(dirty_rect); |
| 143 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | 031ccfff | 2012-10-26 00:57:38 | [diff] [blame] | 144 | } |
| 145 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 146 | TEST_F(TextureLayerTest, SyncImplWhenRemovingFromTree) { |
| 147 | scoped_refptr<Layer> root_layer = Layer::Create(); |
| 148 | ASSERT_TRUE(root_layer); |
| 149 | scoped_refptr<Layer> child_layer = Layer::Create(); |
| 150 | ASSERT_TRUE(child_layer); |
| 151 | root_layer->AddChild(child_layer); |
| 152 | scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL); |
| 153 | ASSERT_TRUE(test_layer); |
| 154 | test_layer->SetTextureId(0); |
| 155 | child_layer->AddChild(test_layer); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 156 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 157 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber()); |
| 158 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
| 159 | layer_tree_host_->SetRootLayer(root_layer); |
| 160 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 161 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 162 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 163 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 164 | test_layer->RemoveFromParent(); |
| 165 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 166 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 167 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 168 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 169 | child_layer->AddChild(test_layer); |
| 170 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 171 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 172 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 173 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 174 | test_layer->SetTextureId(1); |
| 175 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 176 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 177 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1)); |
| 178 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 179 | test_layer->RemoveFromParent(); |
| 180 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 181 | } |
| 182 | |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 183 | class MockMailboxCallback { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 184 | public: |
| 185 | MOCK_METHOD2(Release, void(const std::string& mailbox, unsigned sync_point)); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | struct CommonMailboxObjects { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 189 | CommonMailboxObjects() |
| 190 | : mailbox_name1_(64, '1'), |
| 191 | mailbox_name2_(64, '2'), |
| 192 | sync_point1_(1), |
| 193 | sync_point2_(2) { |
| 194 | release_mailbox1_ = base::Bind(&MockMailboxCallback::Release, |
| 195 | base::Unretained(&mock_callback_), |
| 196 | mailbox_name1_); |
| 197 | release_mailbox2_ = base::Bind(&MockMailboxCallback::Release, |
| 198 | base::Unretained(&mock_callback_), |
| 199 | mailbox_name2_); |
| 200 | gpu::Mailbox m1; |
| 201 | m1.SetName(reinterpret_cast<const int8*>(mailbox_name1_.data())); |
| 202 | mailbox1_ = TextureMailbox(m1, release_mailbox1_, sync_point1_); |
| 203 | gpu::Mailbox m2; |
| 204 | m2.SetName(reinterpret_cast<const int8*>(mailbox_name2_.data())); |
| 205 | mailbox2_ = TextureMailbox(m2, release_mailbox2_, sync_point2_); |
| 206 | } |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 207 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 208 | std::string mailbox_name1_; |
| 209 | std::string mailbox_name2_; |
| 210 | MockMailboxCallback mock_callback_; |
| 211 | TextureMailbox::ReleaseCallback release_mailbox1_; |
| 212 | TextureMailbox::ReleaseCallback release_mailbox2_; |
| 213 | TextureMailbox mailbox1_; |
| 214 | TextureMailbox mailbox2_; |
| 215 | unsigned sync_point1_; |
| 216 | unsigned sync_point2_; |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | class TextureLayerWithMailboxTest : public TextureLayerTest { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 220 | protected: |
| 221 | virtual void TearDown() { |
| 222 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 223 | EXPECT_CALL(test_data_.mock_callback_, |
| 224 | Release(test_data_.mailbox_name1_, |
| 225 | test_data_.sync_point1_)).Times(1); |
| 226 | TextureLayerTest::TearDown(); |
| 227 | } |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 228 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 229 | CommonMailboxObjects test_data_; |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 230 | }; |
| 231 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 232 | TEST_F(TextureLayerWithMailboxTest, ReplaceMailboxOnMainThreadBeforeCommit) { |
| 233 | scoped_refptr<TextureLayer> test_layer = TextureLayer::CreateForMailbox(); |
| 234 | ASSERT_TRUE(test_layer); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 235 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 236 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 237 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
| 238 | layer_tree_host_->SetRootLayer(test_layer); |
| 239 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 240 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 241 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 242 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 243 | test_layer->SetTextureMailbox(test_data_.mailbox1_); |
| 244 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 245 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 246 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 247 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 248 | EXPECT_CALL(test_data_.mock_callback_, |
| 249 | Release(test_data_.mailbox_name1_, test_data_.sync_point1_)) |
| 250 | .Times(1); |
| 251 | test_layer->SetTextureMailbox(test_data_.mailbox2_); |
| 252 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 253 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 254 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 255 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 256 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 257 | EXPECT_CALL(test_data_.mock_callback_, |
| 258 | Release(test_data_.mailbox_name2_, test_data_.sync_point2_)) |
| 259 | .Times(1); |
| 260 | test_layer->SetTextureMailbox(TextureMailbox()); |
| 261 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 262 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 263 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 264 | // Test destructor. |
| 265 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 266 | test_layer->SetTextureMailbox(test_data_.mailbox1_); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 267 | } |
| 268 | |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 269 | class TextureLayerImplWithMailboxThreadedCallback : public LayerTreeTest { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 270 | public: |
| 271 | TextureLayerImplWithMailboxThreadedCallback() |
| 272 | : callback_count_(0), |
| 273 | commit_count_(0) {} |
| 274 | |
| 275 | // Make sure callback is received on main and doesn't block the impl thread. |
| 276 | void ReleaseCallback(unsigned sync_point) { |
| 277 | EXPECT_EQ(true, proxy()->IsMainThread()); |
| 278 | ++callback_count_; |
| 279 | } |
| 280 | |
| 281 | void SetMailbox(char mailbox_char) { |
| 282 | TextureMailbox mailbox( |
| 283 | std::string(64, mailbox_char), |
| 284 | base::Bind( |
| 285 | &TextureLayerImplWithMailboxThreadedCallback::ReleaseCallback, |
| 286 | base::Unretained(this))); |
| 287 | layer_->SetTextureMailbox(mailbox); |
| 288 | } |
| 289 | |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 290 | virtual void BeginTest() OVERRIDE { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 291 | gfx::Size bounds(100, 100); |
| 292 | root_ = Layer::Create(); |
| 293 | root_->SetAnchorPoint(gfx::PointF()); |
| 294 | root_->SetBounds(bounds); |
| 295 | |
| 296 | layer_ = TextureLayer::CreateForMailbox(); |
| 297 | layer_->SetIsDrawable(true); |
| 298 | layer_->SetAnchorPoint(gfx::PointF()); |
| 299 | layer_->SetBounds(bounds); |
| 300 | |
| 301 | root_->AddChild(layer_); |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 302 | layer_tree_host()->SetRootLayer(root_); |
| 303 | layer_tree_host()->SetViewportSize(bounds, bounds); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 304 | SetMailbox('1'); |
| 305 | EXPECT_EQ(0, callback_count_); |
| 306 | |
| 307 | // Case #1: change mailbox before the commit. The old mailbox should be |
| 308 | // released immediately. |
| 309 | SetMailbox('2'); |
| 310 | EXPECT_EQ(1, callback_count_); |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 311 | PostSetNeedsCommitToMainThread(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 312 | } |
| 313 | |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 314 | virtual void DidCommit() OVERRIDE { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 315 | ++commit_count_; |
| 316 | switch (commit_count_) { |
| 317 | case 1: |
| 318 | // Case #2: change mailbox after the commit (and draw), where the |
| 319 | // layer draws. The old mailbox should be released during the next |
| 320 | // commit. |
| 321 | SetMailbox('3'); |
| 322 | EXPECT_EQ(1, callback_count_); |
| 323 | break; |
| 324 | case 2: |
| 325 | // Old mailbox was released, task was posted, but won't execute |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 326 | // until this DidCommit returns. |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 327 | // TODO(piman): fix this. |
| 328 | EXPECT_EQ(1, callback_count_); |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 329 | layer_tree_host()->SetNeedsCommit(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 330 | break; |
| 331 | case 3: |
| 332 | EXPECT_EQ(2, callback_count_); |
| 333 | // Case #3: change mailbox when the layer doesn't draw. The old |
| 334 | // mailbox should be released during the next commit. |
| 335 | layer_->SetBounds(gfx::Size()); |
| 336 | SetMailbox('4'); |
| 337 | break; |
| 338 | case 4: |
| 339 | // Old mailbox was released, task was posted, but won't execute |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 340 | // until this DidCommit returns. |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 341 | // TODO(piman): fix this. |
| 342 | EXPECT_EQ(2, callback_count_); |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 343 | layer_tree_host()->SetNeedsCommit(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 344 | break; |
| 345 | case 5: |
| 346 | EXPECT_EQ(3, callback_count_); |
| 347 | // Case #4: release mailbox that was committed but never drawn. The |
| 348 | // old mailbox should be released during the next commit. |
| 349 | layer_->SetTextureMailbox(TextureMailbox()); |
| 350 | break; |
| 351 | case 6: |
| 352 | // Old mailbox was released, task was posted, but won't execute |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 353 | // until this DidCommit returns. |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 354 | // TODO(piman): fix this. |
| 355 | EXPECT_EQ(3, callback_count_); |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 356 | layer_tree_host()->SetNeedsCommit(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 357 | break; |
| 358 | case 7: |
| 359 | EXPECT_EQ(4, callback_count_); |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 360 | EndTest(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 361 | break; |
| 362 | default: |
| 363 | NOTREACHED(); |
| 364 | break; |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 365 | } |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 366 | } |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 367 | |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 368 | virtual void AfterTest() OVERRIDE {} |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 369 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 370 | private: |
| 371 | int callback_count_; |
| 372 | int commit_count_; |
| 373 | scoped_refptr<Layer> root_; |
| 374 | scoped_refptr<TextureLayer> layer_; |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 375 | }; |
| 376 | |
| 377 | SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerImplWithMailboxThreadedCallback); |
| 378 | |
| 379 | class TextureLayerImplWithMailboxTest : public TextureLayerTest { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 380 | protected: |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 381 | TextureLayerImplWithMailboxTest() |
| 382 | : fake_client_( |
| 383 | FakeLayerTreeHostClient(FakeLayerTreeHostClient::DIRECT_3D)) {} |
| 384 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 385 | virtual void SetUp() { |
| 386 | TextureLayerTest::SetUp(); |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 387 | layer_tree_host_.reset(new MockLayerTreeHost(&fake_client_)); |
[email protected] | f74945f | 2013-03-21 17:08:36 | [diff] [blame] | 388 | EXPECT_TRUE(host_impl_.InitializeRenderer(CreateFakeOutputSurface())); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 389 | } |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 390 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 391 | CommonMailboxObjects test_data_; |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 392 | FakeLayerTreeHostClient fake_client_; |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 393 | }; |
| 394 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 395 | TEST_F(TextureLayerImplWithMailboxTest, TestImplLayerCallbacks) { |
| 396 | host_impl_.CreatePendingTree(); |
| 397 | scoped_ptr<TextureLayerImpl> pending_layer; |
| 398 | pending_layer = TextureLayerImpl::Create(host_impl_.pending_tree(), 1, true); |
| 399 | ASSERT_TRUE(pending_layer); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 400 | |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 401 | scoped_ptr<LayerImpl> active_layer( |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 402 | pending_layer->CreateLayerImpl(host_impl_.active_tree())); |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 403 | ASSERT_TRUE(active_layer); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 404 | |
[email protected] | 1a37a075 | 2013-03-17 21:25:49 | [diff] [blame] | 405 | pending_layer->SetTextureMailbox(test_data_.mailbox1_); |
[email protected] | 421e84f | 2013-02-22 03:27:15 | [diff] [blame] | 406 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 407 | // Test multiple commits without an activation. |
| 408 | EXPECT_CALL(test_data_.mock_callback_, |
| 409 | Release(test_data_.mailbox_name1_, test_data_.sync_point1_)) |
| 410 | .Times(1); |
[email protected] | 1a37a075 | 2013-03-17 21:25:49 | [diff] [blame] | 411 | pending_layer->SetTextureMailbox(test_data_.mailbox2_); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 412 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | 421e84f | 2013-02-22 03:27:15 | [diff] [blame] | 413 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 414 | // Test callback after activation. |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 415 | pending_layer->PushPropertiesTo(active_layer.get()); |
| 416 | active_layer->DidBecomeActive(); |
[email protected] | 421e84f | 2013-02-22 03:27:15 | [diff] [blame] | 417 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 418 | EXPECT_CALL(test_data_.mock_callback_, Release(_, _)).Times(0); |
[email protected] | 1a37a075 | 2013-03-17 21:25:49 | [diff] [blame] | 419 | pending_layer->SetTextureMailbox(test_data_.mailbox1_); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 420 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | 421e84f | 2013-02-22 03:27:15 | [diff] [blame] | 421 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 422 | EXPECT_CALL(test_data_.mock_callback_, Release(test_data_.mailbox_name2_, _)) |
| 423 | .Times(1); |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 424 | pending_layer->PushPropertiesTo(active_layer.get()); |
| 425 | active_layer->DidBecomeActive(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 426 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 427 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 428 | // Test resetting the mailbox. |
| 429 | EXPECT_CALL(test_data_.mock_callback_, Release(test_data_.mailbox_name1_, _)) |
| 430 | .Times(1); |
[email protected] | 1a37a075 | 2013-03-17 21:25:49 | [diff] [blame] | 431 | pending_layer->SetTextureMailbox(TextureMailbox()); |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 432 | pending_layer->PushPropertiesTo(active_layer.get()); |
| 433 | active_layer->DidBecomeActive(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 434 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 435 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 436 | // Test destructor. |
| 437 | EXPECT_CALL(test_data_.mock_callback_, |
| 438 | Release(test_data_.mailbox_name1_, test_data_.sync_point1_)) |
| 439 | .Times(1); |
[email protected] | 1a37a075 | 2013-03-17 21:25:49 | [diff] [blame] | 440 | pending_layer->SetTextureMailbox(test_data_.mailbox1_); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 441 | } |
| 442 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 443 | TEST_F(TextureLayerImplWithMailboxTest, |
| 444 | TestDestructorCallbackOnCreatedResource) { |
| 445 | scoped_ptr<TextureLayerImpl> impl_layer; |
| 446 | impl_layer = TextureLayerImpl::Create(host_impl_.active_tree(), 1, true); |
| 447 | ASSERT_TRUE(impl_layer); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 448 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 449 | EXPECT_CALL(test_data_.mock_callback_, Release(test_data_.mailbox_name1_, _)) |
| 450 | .Times(1); |
[email protected] | 1a37a075 | 2013-03-17 21:25:49 | [diff] [blame] | 451 | impl_layer->SetTextureMailbox(test_data_.mailbox1_); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 452 | impl_layer->WillDraw(host_impl_.active_tree()->resource_provider()); |
| 453 | impl_layer->DidDraw(host_impl_.active_tree()->resource_provider()); |
[email protected] | 1a37a075 | 2013-03-17 21:25:49 | [diff] [blame] | 454 | impl_layer->SetTextureMailbox(TextureMailbox()); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 455 | } |
| 456 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 457 | TEST_F(TextureLayerImplWithMailboxTest, TestCallbackOnInUseResource) { |
| 458 | ResourceProvider* provider = host_impl_.active_tree()->resource_provider(); |
| 459 | ResourceProvider::ResourceId id = |
| 460 | provider->CreateResourceFromTextureMailbox(test_data_.mailbox1_); |
| 461 | provider->AllocateForTesting(id); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 462 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 463 | // Transfer some resources to the parent. |
| 464 | ResourceProvider::ResourceIdArray resource_ids_to_transfer; |
| 465 | resource_ids_to_transfer.push_back(id); |
| 466 | TransferableResourceArray list; |
| 467 | provider->PrepareSendToParent(resource_ids_to_transfer, &list); |
| 468 | EXPECT_TRUE(provider->InUseByConsumer(id)); |
| 469 | EXPECT_CALL(test_data_.mock_callback_, Release(_, _)).Times(0); |
| 470 | provider->DeleteResource(id); |
| 471 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 472 | EXPECT_CALL(test_data_.mock_callback_, Release(test_data_.mailbox_name1_, _)) |
| 473 | .Times(1); |
| 474 | provider->ReceiveFromParent(list); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 475 | } |
| 476 | |
[email protected] | ba56574 | 2012-11-10 09:29:48 | [diff] [blame] | 477 | } // namespace |
| 478 | } // namespace cc |