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