[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] | 97d519fb | 2013-03-29 02:27:54 | [diff] [blame] | 10 | #include "cc/layers/texture_layer_client.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] | 06d68d0 | 2013-04-19 18:46:21 | [diff] [blame] | 15 | #include "cc/test/layer_test_common.h" |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 16 | #include "cc/test/layer_tree_test.h" |
[email protected] | 556fd29 | 2013-03-18 08:03:04 | [diff] [blame] | 17 | #include "cc/trees/layer_tree_host.h" |
| 18 | #include "cc/trees/layer_tree_impl.h" |
| 19 | #include "cc/trees/single_thread_proxy.h" |
[email protected] | 0bf5a20 | 2013-07-10 14:50:54 | [diff] [blame] | 20 | #include "gpu/GLES2/gl2extchromium.h" |
[email protected] | 7f0c53db | 2012-10-02 00:23:18 | [diff] [blame] | 21 | #include "testing/gmock/include/gmock/gmock.h" |
| 22 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 23 | |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 24 | using ::testing::Mock; |
| 25 | using ::testing::_; |
| 26 | using ::testing::AtLeast; |
| 27 | using ::testing::AnyNumber; |
| 28 | |
[email protected] | ba56574 | 2012-11-10 09:29:48 | [diff] [blame] | 29 | namespace cc { |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 30 | namespace { |
| 31 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 32 | class MockLayerTreeHost : public LayerTreeHost { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 33 | public: |
[email protected] | bf691c2 | 2013-03-26 21:15:06 | [diff] [blame] | 34 | explicit MockLayerTreeHost(LayerTreeHostClient* client) |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 35 | : LayerTreeHost(client, LayerTreeSettings()) { |
[email protected] | 810d40b7 | 2013-06-20 18:26:15 | [diff] [blame] | 36 | Initialize(NULL); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 37 | } |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 38 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 39 | MOCK_METHOD0(AcquireLayerTextures, void()); |
| 40 | MOCK_METHOD0(SetNeedsCommit, void()); |
[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: |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 45 | TextureLayerTest() |
| 46 | : fake_client_( |
| 47 | FakeLayerTreeHostClient(FakeLayerTreeHostClient::DIRECT_3D)), |
| 48 | host_impl_(&proxy_) {} |
[email protected] | 0f077a5 | 2012-09-08 01:45:24 | [diff] [blame] | 49 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 50 | protected: |
| 51 | virtual void SetUp() { |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 52 | layer_tree_host_.reset(new MockLayerTreeHost(&fake_client_)); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 53 | } |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 54 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 55 | virtual void TearDown() { |
| 56 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 57 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber()); |
| 58 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 59 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 60 | layer_tree_host_->SetRootLayer(NULL); |
| 61 | layer_tree_host_.reset(); |
| 62 | } |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 63 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 64 | scoped_ptr<MockLayerTreeHost> layer_tree_host_; |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 65 | FakeImplProxy proxy_; |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 66 | FakeLayerTreeHostClient fake_client_; |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 67 | FakeLayerTreeHostImpl host_impl_; |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 68 | }; |
| 69 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 70 | TEST_F(TextureLayerTest, SyncImplWhenChangingTextureId) { |
| 71 | scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL); |
[email protected] | 22898ed | 2013-06-01 04:52:30 | [diff] [blame] | 72 | ASSERT_TRUE(test_layer.get()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 73 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 74 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber()); |
| 75 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
| 76 | layer_tree_host_->SetRootLayer(test_layer); |
| 77 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 78 | EXPECT_EQ(test_layer->layer_tree_host(), layer_tree_host_.get()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 79 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 80 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 81 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 82 | test_layer->SetTextureId(1); |
| 83 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 84 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 85 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1)); |
| 86 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 87 | test_layer->SetTextureId(2); |
| 88 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 89 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 90 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1)); |
| 91 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 92 | test_layer->SetTextureId(0); |
| 93 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 96 | TEST_F(TextureLayerTest, SyncImplWhenDrawing) { |
| 97 | gfx::RectF dirty_rect(0.f, 0.f, 1.f, 1.f); |
[email protected] | 031ccfff | 2012-10-26 00:57:38 | [diff] [blame] | 98 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 99 | scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL); |
[email protected] | 22898ed | 2013-06-01 04:52:30 | [diff] [blame] | 100 | ASSERT_TRUE(test_layer.get()); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 101 | scoped_ptr<TextureLayerImpl> impl_layer; |
| 102 | impl_layer = TextureLayerImpl::Create(host_impl_.active_tree(), 1, false); |
| 103 | ASSERT_TRUE(impl_layer); |
[email protected] | 031ccfff | 2012-10-26 00:57:38 | [diff] [blame] | 104 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 105 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber()); |
| 106 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
| 107 | layer_tree_host_->SetRootLayer(test_layer); |
| 108 | test_layer->SetTextureId(1); |
| 109 | test_layer->SetIsDrawable(true); |
| 110 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 111 | EXPECT_EQ(test_layer->layer_tree_host(), layer_tree_host_.get()); |
[email protected] | 031ccfff | 2012-10-26 00:57:38 | [diff] [blame] | 112 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 113 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(1); |
| 114 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0); |
| 115 | test_layer->WillModifyTexture(); |
| 116 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | 031ccfff | 2012-10-26 00:57:38 | [diff] [blame] | 117 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 118 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 119 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(1); |
| 120 | test_layer->SetNeedsDisplayRect(dirty_rect); |
| 121 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | 031ccfff | 2012-10-26 00:57:38 | [diff] [blame] | 122 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 123 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 124 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(1); |
| 125 | test_layer->PushPropertiesTo(impl_layer.get()); // fake commit |
| 126 | test_layer->SetIsDrawable(false); |
| 127 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | 031ccfff | 2012-10-26 00:57:38 | [diff] [blame] | 128 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 129 | // Verify that non-drawable layers don't signal the compositor, |
| 130 | // except for the first draw after last commit, which must acquire |
| 131 | // the texture. |
| 132 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(1); |
| 133 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0); |
| 134 | test_layer->WillModifyTexture(); |
| 135 | test_layer->SetNeedsDisplayRect(dirty_rect); |
| 136 | test_layer->PushPropertiesTo(impl_layer.get()); // fake commit |
| 137 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | 031ccfff | 2012-10-26 00:57:38 | [diff] [blame] | 138 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 139 | // Second draw with layer in non-drawable state: no texture |
| 140 | // acquisition. |
| 141 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 142 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0); |
| 143 | test_layer->WillModifyTexture(); |
| 144 | test_layer->SetNeedsDisplayRect(dirty_rect); |
| 145 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | 031ccfff | 2012-10-26 00:57:38 | [diff] [blame] | 146 | } |
| 147 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 148 | TEST_F(TextureLayerTest, SyncImplWhenRemovingFromTree) { |
| 149 | scoped_refptr<Layer> root_layer = Layer::Create(); |
[email protected] | 22898ed | 2013-06-01 04:52:30 | [diff] [blame] | 150 | ASSERT_TRUE(root_layer.get()); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 151 | scoped_refptr<Layer> child_layer = Layer::Create(); |
[email protected] | 22898ed | 2013-06-01 04:52:30 | [diff] [blame] | 152 | ASSERT_TRUE(child_layer.get()); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 153 | root_layer->AddChild(child_layer); |
| 154 | scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL); |
[email protected] | 22898ed | 2013-06-01 04:52:30 | [diff] [blame] | 155 | ASSERT_TRUE(test_layer.get()); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 156 | test_layer->SetTextureId(0); |
| 157 | child_layer->AddChild(test_layer); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 158 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 159 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber()); |
| 160 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
| 161 | layer_tree_host_->SetRootLayer(root_layer); |
| 162 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 163 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 164 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 165 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 166 | test_layer->RemoveFromParent(); |
| 167 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 168 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 169 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 170 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 171 | child_layer->AddChild(test_layer); |
| 172 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 173 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 174 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 175 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 176 | test_layer->SetTextureId(1); |
| 177 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 178 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 179 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1)); |
| 180 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 181 | test_layer->RemoveFromParent(); |
| 182 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 183 | } |
| 184 | |
[email protected] | 06d68d0 | 2013-04-19 18:46:21 | [diff] [blame] | 185 | TEST_F(TextureLayerTest, CheckPropertyChangeCausesCorrectBehavior) { |
| 186 | scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL); |
| 187 | test_layer->SetLayerTreeHost(layer_tree_host_.get()); |
| 188 | |
| 189 | // Test properties that should call SetNeedsCommit. All properties need to |
| 190 | // be set to new values in order for SetNeedsCommit to be called. |
| 191 | EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFlipped(false)); |
| 192 | EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetUV( |
| 193 | gfx::PointF(0.25f, 0.25f), gfx::PointF(0.75f, 0.75f))); |
| 194 | EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetVertexOpacity( |
| 195 | 0.5f, 0.5f, 0.5f, 0.5f)); |
| 196 | EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetPremultipliedAlpha(false)); |
| 197 | EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTextureId(1)); |
| 198 | |
| 199 | // Calling SetTextureId can call AcquireLayerTextures. |
| 200 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber()); |
| 201 | } |
| 202 | |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 203 | class MockMailboxCallback { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 204 | public: |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 205 | MOCK_METHOD3(Release, void(const std::string& mailbox, |
| 206 | unsigned sync_point, |
| 207 | bool lost_resource)); |
[email protected] | 42f40a5 | 2013-06-08 04:38:51 | [diff] [blame] | 208 | MOCK_METHOD3(Release2, void(base::SharedMemory* shared_memory, |
| 209 | unsigned sync_point, |
| 210 | bool lost_resource)); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | struct CommonMailboxObjects { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 214 | CommonMailboxObjects() |
| 215 | : mailbox_name1_(64, '1'), |
| 216 | mailbox_name2_(64, '2'), |
| 217 | sync_point1_(1), |
[email protected] | 42f40a5 | 2013-06-08 04:38:51 | [diff] [blame] | 218 | sync_point2_(2), |
| 219 | shared_memory_(new base::SharedMemory) { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 220 | release_mailbox1_ = base::Bind(&MockMailboxCallback::Release, |
| 221 | base::Unretained(&mock_callback_), |
| 222 | mailbox_name1_); |
| 223 | release_mailbox2_ = base::Bind(&MockMailboxCallback::Release, |
| 224 | base::Unretained(&mock_callback_), |
| 225 | mailbox_name2_); |
| 226 | gpu::Mailbox m1; |
| 227 | m1.SetName(reinterpret_cast<const int8*>(mailbox_name1_.data())); |
| 228 | mailbox1_ = TextureMailbox(m1, release_mailbox1_, sync_point1_); |
| 229 | gpu::Mailbox m2; |
| 230 | m2.SetName(reinterpret_cast<const int8*>(mailbox_name2_.data())); |
| 231 | mailbox2_ = TextureMailbox(m2, release_mailbox2_, sync_point2_); |
[email protected] | 42f40a5 | 2013-06-08 04:38:51 | [diff] [blame] | 232 | |
| 233 | gfx::Size size(128, 128); |
| 234 | EXPECT_TRUE(shared_memory_->CreateAndMapAnonymous(4 * size.GetArea())); |
| 235 | release_mailbox3_ = base::Bind(&MockMailboxCallback::Release2, |
| 236 | base::Unretained(&mock_callback_), |
| 237 | shared_memory_.get()); |
| 238 | mailbox3_ = TextureMailbox(shared_memory_.get(), size, release_mailbox3_); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 239 | } |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 240 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 241 | std::string mailbox_name1_; |
| 242 | std::string mailbox_name2_; |
| 243 | MockMailboxCallback mock_callback_; |
| 244 | TextureMailbox::ReleaseCallback release_mailbox1_; |
| 245 | TextureMailbox::ReleaseCallback release_mailbox2_; |
[email protected] | 42f40a5 | 2013-06-08 04:38:51 | [diff] [blame] | 246 | TextureMailbox::ReleaseCallback release_mailbox3_; |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 247 | TextureMailbox mailbox1_; |
| 248 | TextureMailbox mailbox2_; |
[email protected] | 42f40a5 | 2013-06-08 04:38:51 | [diff] [blame] | 249 | TextureMailbox mailbox3_; |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 250 | unsigned sync_point1_; |
| 251 | unsigned sync_point2_; |
[email protected] | 42f40a5 | 2013-06-08 04:38:51 | [diff] [blame] | 252 | scoped_ptr<base::SharedMemory> shared_memory_; |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 253 | }; |
| 254 | |
| 255 | class TextureLayerWithMailboxTest : public TextureLayerTest { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 256 | protected: |
| 257 | virtual void TearDown() { |
| 258 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 259 | EXPECT_CALL(test_data_.mock_callback_, |
| 260 | Release(test_data_.mailbox_name1_, |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 261 | test_data_.sync_point1_, |
| 262 | false)).Times(1); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 263 | TextureLayerTest::TearDown(); |
| 264 | } |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 265 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 266 | CommonMailboxObjects test_data_; |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 267 | }; |
| 268 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 269 | TEST_F(TextureLayerWithMailboxTest, ReplaceMailboxOnMainThreadBeforeCommit) { |
[email protected] | e8e4ae23 | 2013-04-12 00:26:01 | [diff] [blame] | 270 | scoped_refptr<TextureLayer> test_layer = TextureLayer::CreateForMailbox(NULL); |
[email protected] | 22898ed | 2013-06-01 04:52:30 | [diff] [blame] | 271 | ASSERT_TRUE(test_layer.get()); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 272 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 273 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 274 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
| 275 | layer_tree_host_->SetRootLayer(test_layer); |
| 276 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 277 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 278 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 279 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 280 | test_layer->SetTextureMailbox(test_data_.mailbox1_); |
| 281 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 282 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 283 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 284 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 285 | EXPECT_CALL(test_data_.mock_callback_, |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 286 | Release(test_data_.mailbox_name1_, |
| 287 | test_data_.sync_point1_, |
| 288 | false)) |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 289 | .Times(1); |
| 290 | test_layer->SetTextureMailbox(test_data_.mailbox2_); |
| 291 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 292 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 293 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 294 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 295 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 296 | EXPECT_CALL(test_data_.mock_callback_, |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 297 | Release(test_data_.mailbox_name2_, |
| 298 | test_data_.sync_point2_, |
| 299 | false)) |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 300 | .Times(1); |
| 301 | test_layer->SetTextureMailbox(TextureMailbox()); |
| 302 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 303 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 304 | |
[email protected] | 42f40a5 | 2013-06-08 04:38:51 | [diff] [blame] | 305 | test_layer->SetTextureMailbox(test_data_.mailbox3_); |
| 306 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 307 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 308 | |
| 309 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 310 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 311 | EXPECT_CALL(test_data_.mock_callback_, |
| 312 | Release2(test_data_.shared_memory_.get(), |
| 313 | 0, false)) |
| 314 | .Times(1); |
| 315 | test_layer->SetTextureMailbox(TextureMailbox()); |
| 316 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 317 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 318 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 319 | // Test destructor. |
| 320 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 321 | test_layer->SetTextureMailbox(test_data_.mailbox1_); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 322 | } |
| 323 | |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 324 | class TextureLayerImplWithMailboxThreadedCallback : public LayerTreeTest { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 325 | public: |
| 326 | TextureLayerImplWithMailboxThreadedCallback() |
| 327 | : callback_count_(0), |
| 328 | commit_count_(0) {} |
| 329 | |
| 330 | // Make sure callback is received on main and doesn't block the impl thread. |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 331 | void ReleaseCallback(unsigned sync_point, bool lost_resource) { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 332 | EXPECT_EQ(true, proxy()->IsMainThread()); |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 333 | EXPECT_FALSE(lost_resource); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 334 | ++callback_count_; |
| 335 | } |
| 336 | |
| 337 | void SetMailbox(char mailbox_char) { |
| 338 | TextureMailbox mailbox( |
| 339 | std::string(64, mailbox_char), |
| 340 | base::Bind( |
| 341 | &TextureLayerImplWithMailboxThreadedCallback::ReleaseCallback, |
| 342 | base::Unretained(this))); |
| 343 | layer_->SetTextureMailbox(mailbox); |
| 344 | } |
| 345 | |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 346 | virtual void BeginTest() OVERRIDE { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 347 | gfx::Size bounds(100, 100); |
| 348 | root_ = Layer::Create(); |
| 349 | root_->SetAnchorPoint(gfx::PointF()); |
| 350 | root_->SetBounds(bounds); |
| 351 | |
[email protected] | e8e4ae23 | 2013-04-12 00:26:01 | [diff] [blame] | 352 | layer_ = TextureLayer::CreateForMailbox(NULL); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 353 | layer_->SetIsDrawable(true); |
| 354 | layer_->SetAnchorPoint(gfx::PointF()); |
| 355 | layer_->SetBounds(bounds); |
| 356 | |
| 357 | root_->AddChild(layer_); |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 358 | layer_tree_host()->SetRootLayer(root_); |
[email protected] | 18ce5970 | 2013-04-09 04:58:40 | [diff] [blame] | 359 | layer_tree_host()->SetViewportSize(bounds); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 360 | SetMailbox('1'); |
| 361 | EXPECT_EQ(0, callback_count_); |
| 362 | |
| 363 | // Case #1: change mailbox before the commit. The old mailbox should be |
| 364 | // released immediately. |
| 365 | SetMailbox('2'); |
| 366 | EXPECT_EQ(1, callback_count_); |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 367 | PostSetNeedsCommitToMainThread(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 368 | } |
| 369 | |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 370 | virtual void DidCommit() OVERRIDE { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 371 | ++commit_count_; |
| 372 | switch (commit_count_) { |
| 373 | case 1: |
| 374 | // Case #2: change mailbox after the commit (and draw), where the |
| 375 | // layer draws. The old mailbox should be released during the next |
| 376 | // commit. |
| 377 | SetMailbox('3'); |
| 378 | EXPECT_EQ(1, callback_count_); |
| 379 | break; |
| 380 | case 2: |
| 381 | // Old mailbox was released, task was posted, but won't execute |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 382 | // until this DidCommit returns. |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 383 | // TODO(piman): fix this. |
| 384 | EXPECT_EQ(1, callback_count_); |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 385 | layer_tree_host()->SetNeedsCommit(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 386 | break; |
| 387 | case 3: |
| 388 | EXPECT_EQ(2, callback_count_); |
| 389 | // Case #3: change mailbox when the layer doesn't draw. The old |
| 390 | // mailbox should be released during the next commit. |
| 391 | layer_->SetBounds(gfx::Size()); |
| 392 | SetMailbox('4'); |
| 393 | break; |
| 394 | case 4: |
| 395 | // Old mailbox was released, task was posted, but won't execute |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 396 | // until this DidCommit returns. |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 397 | // TODO(piman): fix this. |
| 398 | EXPECT_EQ(2, callback_count_); |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 399 | layer_tree_host()->SetNeedsCommit(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 400 | break; |
| 401 | case 5: |
| 402 | EXPECT_EQ(3, callback_count_); |
| 403 | // Case #4: release mailbox that was committed but never drawn. The |
| 404 | // old mailbox should be released during the next commit. |
| 405 | layer_->SetTextureMailbox(TextureMailbox()); |
| 406 | break; |
| 407 | case 6: |
| 408 | // Old mailbox was released, task was posted, but won't execute |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 409 | // until this DidCommit returns. |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 410 | // TODO(piman): fix this. |
| 411 | EXPECT_EQ(3, callback_count_); |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 412 | layer_tree_host()->SetNeedsCommit(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 413 | break; |
| 414 | case 7: |
| 415 | EXPECT_EQ(4, callback_count_); |
[email protected] | 7096acc | 2013-06-18 21:12:43 | [diff] [blame] | 416 | // Restore a mailbox for the next step. |
| 417 | SetMailbox('5'); |
| 418 | break; |
| 419 | case 8: |
| 420 | // Case #5: remove layer from tree. Callback should *not* be called, the |
| 421 | // mailbox is returned to the main thread. |
| 422 | EXPECT_EQ(4, callback_count_); |
| 423 | layer_->RemoveFromParent(); |
| 424 | break; |
| 425 | case 9: |
| 426 | // Mailbox was released to the main thread, task was posted, but won't |
| 427 | // execute until this DidCommit returns. |
| 428 | // TODO(piman): fix this. |
| 429 | EXPECT_EQ(4, callback_count_); |
| 430 | layer_tree_host()->SetNeedsCommit(); |
| 431 | break; |
| 432 | case 10: |
| 433 | EXPECT_EQ(4, callback_count_); |
| 434 | // Resetting the mailbox will call the callback now. |
| 435 | layer_->SetTextureMailbox(TextureMailbox()); |
| 436 | EXPECT_EQ(5, callback_count_); |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 437 | EndTest(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 438 | break; |
| 439 | default: |
| 440 | NOTREACHED(); |
| 441 | break; |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 442 | } |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 443 | } |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 444 | |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 445 | virtual void AfterTest() OVERRIDE {} |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 446 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 447 | private: |
| 448 | int callback_count_; |
| 449 | int commit_count_; |
| 450 | scoped_refptr<Layer> root_; |
| 451 | scoped_refptr<TextureLayer> layer_; |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 452 | }; |
| 453 | |
[email protected] | 4145d17 | 2013-05-10 16:54:36 | [diff] [blame] | 454 | SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F( |
| 455 | TextureLayerImplWithMailboxThreadedCallback); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 456 | |
| 457 | class TextureLayerImplWithMailboxTest : public TextureLayerTest { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 458 | protected: |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 459 | TextureLayerImplWithMailboxTest() |
| 460 | : fake_client_( |
| 461 | FakeLayerTreeHostClient(FakeLayerTreeHostClient::DIRECT_3D)) {} |
| 462 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 463 | virtual void SetUp() { |
| 464 | TextureLayerTest::SetUp(); |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 465 | layer_tree_host_.reset(new MockLayerTreeHost(&fake_client_)); |
[email protected] | f74945f | 2013-03-21 17:08:36 | [diff] [blame] | 466 | EXPECT_TRUE(host_impl_.InitializeRenderer(CreateFakeOutputSurface())); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 467 | } |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 468 | |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 469 | bool WillDraw(TextureLayerImpl* layer, DrawMode mode) { |
| 470 | bool will_draw = layer->WillDraw( |
| 471 | mode, host_impl_.active_tree()->resource_provider()); |
| 472 | if (will_draw) |
| 473 | layer->DidDraw(host_impl_.active_tree()->resource_provider()); |
| 474 | return will_draw; |
| 475 | } |
| 476 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 477 | CommonMailboxObjects test_data_; |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 478 | FakeLayerTreeHostClient fake_client_; |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 479 | }; |
| 480 | |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 481 | // Test conditions for results of TextureLayerImpl::WillDraw under |
| 482 | // different configurations of different mailbox, texture_id, and draw_mode. |
| 483 | TEST_F(TextureLayerImplWithMailboxTest, TestWillDraw) { |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 484 | EXPECT_CALL(test_data_.mock_callback_, |
| 485 | Release(test_data_.mailbox_name1_, |
| 486 | test_data_.sync_point1_, |
| 487 | false)) |
| 488 | .Times(AnyNumber()); |
| 489 | EXPECT_CALL(test_data_.mock_callback_, |
| 490 | Release2(test_data_.shared_memory_.get(), 0, false)) |
| 491 | .Times(AnyNumber()); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 492 | // Hardware mode. |
| 493 | { |
| 494 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 495 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, true); |
| 496 | impl_layer->SetTextureMailbox(test_data_.mailbox1_); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 497 | EXPECT_TRUE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE)); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | { |
| 501 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 502 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, true); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 503 | impl_layer->SetTextureMailbox(TextureMailbox()); |
| 504 | EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE)); |
| 505 | } |
| 506 | |
| 507 | { |
| 508 | // Software resource. |
| 509 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 510 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, true); |
| 511 | impl_layer->SetTextureMailbox(test_data_.mailbox3_); |
| 512 | EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE)); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | { |
| 516 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 517 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, false); |
| 518 | unsigned texture = |
| 519 | host_impl_.output_surface()->context3d()->createTexture(); |
| 520 | impl_layer->set_texture_id(texture); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 521 | EXPECT_TRUE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE)); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | { |
| 525 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 526 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, false); |
| 527 | impl_layer->set_texture_id(0); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 528 | EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE)); |
| 529 | } |
| 530 | |
| 531 | // Software mode. |
| 532 | { |
| 533 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 534 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, true); |
| 535 | impl_layer->SetTextureMailbox(test_data_.mailbox1_); |
| 536 | EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_SOFTWARE)); |
| 537 | } |
| 538 | |
| 539 | { |
| 540 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 541 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, true); |
| 542 | impl_layer->SetTextureMailbox(TextureMailbox()); |
| 543 | EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_SOFTWARE)); |
| 544 | } |
| 545 | |
| 546 | { |
| 547 | // Software resource. |
| 548 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 549 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, true); |
| 550 | impl_layer->SetTextureMailbox(test_data_.mailbox3_); |
| 551 | EXPECT_TRUE(WillDraw(impl_layer.get(), DRAW_MODE_SOFTWARE)); |
| 552 | } |
| 553 | |
| 554 | { |
| 555 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 556 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, false); |
| 557 | unsigned texture = |
| 558 | host_impl_.output_surface()->context3d()->createTexture(); |
| 559 | impl_layer->set_texture_id(texture); |
| 560 | EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_SOFTWARE)); |
| 561 | } |
| 562 | |
| 563 | { |
| 564 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 565 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, false); |
| 566 | impl_layer->set_texture_id(0); |
| 567 | EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_SOFTWARE)); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | // Resourceless software mode. |
| 571 | { |
| 572 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 573 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, true); |
| 574 | impl_layer->SetTextureMailbox(test_data_.mailbox1_); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 575 | EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_RESOURCELESS_SOFTWARE)); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | { |
| 579 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 580 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, false); |
| 581 | unsigned texture = |
| 582 | host_impl_.output_surface()->context3d()->createTexture(); |
| 583 | impl_layer->set_texture_id(texture); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 584 | EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_RESOURCELESS_SOFTWARE)); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 588 | TEST_F(TextureLayerImplWithMailboxTest, TestImplLayerCallbacks) { |
| 589 | host_impl_.CreatePendingTree(); |
| 590 | scoped_ptr<TextureLayerImpl> pending_layer; |
| 591 | pending_layer = TextureLayerImpl::Create(host_impl_.pending_tree(), 1, true); |
| 592 | ASSERT_TRUE(pending_layer); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 593 | |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 594 | scoped_ptr<LayerImpl> active_layer( |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 595 | pending_layer->CreateLayerImpl(host_impl_.active_tree())); |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 596 | ASSERT_TRUE(active_layer); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 597 | |
[email protected] | 1a37a075 | 2013-03-17 21:25:49 | [diff] [blame] | 598 | pending_layer->SetTextureMailbox(test_data_.mailbox1_); |
[email protected] | 421e84f | 2013-02-22 03:27:15 | [diff] [blame] | 599 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 600 | // Test multiple commits without an activation. |
| 601 | EXPECT_CALL(test_data_.mock_callback_, |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 602 | Release(test_data_.mailbox_name1_, |
| 603 | test_data_.sync_point1_, |
| 604 | false)) |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 605 | .Times(1); |
[email protected] | 1a37a075 | 2013-03-17 21:25:49 | [diff] [blame] | 606 | pending_layer->SetTextureMailbox(test_data_.mailbox2_); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 607 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | 421e84f | 2013-02-22 03:27:15 | [diff] [blame] | 608 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 609 | // Test callback after activation. |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 610 | pending_layer->PushPropertiesTo(active_layer.get()); |
| 611 | active_layer->DidBecomeActive(); |
[email protected] | 421e84f | 2013-02-22 03:27:15 | [diff] [blame] | 612 | |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 613 | EXPECT_CALL(test_data_.mock_callback_, Release(_, _, _)).Times(0); |
[email protected] | 1a37a075 | 2013-03-17 21:25:49 | [diff] [blame] | 614 | pending_layer->SetTextureMailbox(test_data_.mailbox1_); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 615 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | 421e84f | 2013-02-22 03:27:15 | [diff] [blame] | 616 | |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 617 | EXPECT_CALL(test_data_.mock_callback_, |
| 618 | Release(test_data_.mailbox_name2_, _, false)) |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 619 | .Times(1); |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 620 | pending_layer->PushPropertiesTo(active_layer.get()); |
| 621 | active_layer->DidBecomeActive(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 622 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 623 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 624 | // Test resetting the mailbox. |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 625 | EXPECT_CALL(test_data_.mock_callback_, |
| 626 | Release(test_data_.mailbox_name1_, _, false)) |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 627 | .Times(1); |
[email protected] | 1a37a075 | 2013-03-17 21:25:49 | [diff] [blame] | 628 | pending_layer->SetTextureMailbox(TextureMailbox()); |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 629 | pending_layer->PushPropertiesTo(active_layer.get()); |
| 630 | active_layer->DidBecomeActive(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 631 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 632 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 633 | // Test destructor. |
| 634 | EXPECT_CALL(test_data_.mock_callback_, |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 635 | Release(test_data_.mailbox_name1_, |
| 636 | test_data_.sync_point1_, |
| 637 | false)) |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 638 | .Times(1); |
[email protected] | 1a37a075 | 2013-03-17 21:25:49 | [diff] [blame] | 639 | pending_layer->SetTextureMailbox(test_data_.mailbox1_); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 640 | } |
| 641 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 642 | TEST_F(TextureLayerImplWithMailboxTest, |
| 643 | TestDestructorCallbackOnCreatedResource) { |
| 644 | scoped_ptr<TextureLayerImpl> impl_layer; |
| 645 | impl_layer = TextureLayerImpl::Create(host_impl_.active_tree(), 1, true); |
| 646 | ASSERT_TRUE(impl_layer); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 647 | |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 648 | EXPECT_CALL(test_data_.mock_callback_, |
| 649 | Release(test_data_.mailbox_name1_, _, false)) |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 650 | .Times(1); |
[email protected] | 1a37a075 | 2013-03-17 21:25:49 | [diff] [blame] | 651 | impl_layer->SetTextureMailbox(test_data_.mailbox1_); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 652 | impl_layer->DidBecomeActive(); |
| 653 | EXPECT_TRUE(impl_layer->WillDraw( |
| 654 | DRAW_MODE_HARDWARE, host_impl_.active_tree()->resource_provider())); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 655 | impl_layer->DidDraw(host_impl_.active_tree()->resource_provider()); |
[email protected] | 1a37a075 | 2013-03-17 21:25:49 | [diff] [blame] | 656 | impl_layer->SetTextureMailbox(TextureMailbox()); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 657 | } |
| 658 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 659 | TEST_F(TextureLayerImplWithMailboxTest, TestCallbackOnInUseResource) { |
| 660 | ResourceProvider* provider = host_impl_.active_tree()->resource_provider(); |
| 661 | ResourceProvider::ResourceId id = |
| 662 | provider->CreateResourceFromTextureMailbox(test_data_.mailbox1_); |
| 663 | provider->AllocateForTesting(id); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 664 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 665 | // Transfer some resources to the parent. |
| 666 | ResourceProvider::ResourceIdArray resource_ids_to_transfer; |
| 667 | resource_ids_to_transfer.push_back(id); |
| 668 | TransferableResourceArray list; |
| 669 | provider->PrepareSendToParent(resource_ids_to_transfer, &list); |
| 670 | EXPECT_TRUE(provider->InUseByConsumer(id)); |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 671 | EXPECT_CALL(test_data_.mock_callback_, Release(_, _, _)).Times(0); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 672 | provider->DeleteResource(id); |
| 673 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 674 | EXPECT_CALL(test_data_.mock_callback_, |
| 675 | Release(test_data_.mailbox_name1_, _, false)) |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 676 | .Times(1); |
| 677 | provider->ReceiveFromParent(list); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 678 | } |
| 679 | |
[email protected] | 97d519fb | 2013-03-29 02:27:54 | [diff] [blame] | 680 | // Check that ClearClient correctly clears the state so that the impl side |
| 681 | // doesn't try to use a texture that could have been destroyed. |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 682 | class TextureLayerClientTest |
| 683 | : public LayerTreeTest, |
| 684 | public TextureLayerClient { |
[email protected] | 97d519fb | 2013-03-29 02:27:54 | [diff] [blame] | 685 | public: |
| 686 | TextureLayerClientTest() |
| 687 | : context_(NULL), |
| 688 | texture_(0), |
| 689 | commit_count_(0), |
| 690 | expected_used_textures_on_draw_(0), |
| 691 | expected_used_textures_on_commit_(0) {} |
| 692 | |
| 693 | virtual scoped_ptr<OutputSurface> CreateOutputSurface() OVERRIDE { |
| 694 | scoped_ptr<TestWebGraphicsContext3D> context( |
| 695 | TestWebGraphicsContext3D::Create()); |
| 696 | context_ = context.get(); |
| 697 | texture_ = context->createTexture(); |
| 698 | return FakeOutputSurface::Create3d( |
| 699 | context.PassAs<WebKit::WebGraphicsContext3D>()).PassAs<OutputSurface>(); |
| 700 | } |
| 701 | |
[email protected] | 171cbb3 | 2013-07-11 03:51:19 | [diff] [blame^] | 702 | virtual unsigned PrepareTexture() OVERRIDE { |
[email protected] | 97d519fb | 2013-03-29 02:27:54 | [diff] [blame] | 703 | return texture_; |
| 704 | } |
| 705 | |
[email protected] | d5d9bc5 | 2013-04-01 23:16:39 | [diff] [blame] | 706 | virtual WebKit::WebGraphicsContext3D* Context3d() OVERRIDE { |
[email protected] | 97d519fb | 2013-03-29 02:27:54 | [diff] [blame] | 707 | return context_; |
| 708 | } |
| 709 | |
[email protected] | 2541d1a | 2013-07-10 07:33:27 | [diff] [blame] | 710 | virtual bool PrepareTextureMailbox( |
| 711 | cc::TextureMailbox* mailbox, bool use_shared_memory) OVERRIDE { |
[email protected] | e8e4ae23 | 2013-04-12 00:26:01 | [diff] [blame] | 712 | return false; |
| 713 | } |
| 714 | |
[email protected] | 97d519fb | 2013-03-29 02:27:54 | [diff] [blame] | 715 | virtual void SetupTree() OVERRIDE { |
| 716 | scoped_refptr<Layer> root = Layer::Create(); |
| 717 | root->SetBounds(gfx::Size(10, 10)); |
| 718 | root->SetAnchorPoint(gfx::PointF()); |
| 719 | root->SetIsDrawable(true); |
| 720 | |
| 721 | texture_layer_ = TextureLayer::Create(this); |
| 722 | texture_layer_->SetBounds(gfx::Size(10, 10)); |
| 723 | texture_layer_->SetAnchorPoint(gfx::PointF()); |
| 724 | texture_layer_->SetIsDrawable(true); |
| 725 | root->AddChild(texture_layer_); |
| 726 | |
| 727 | layer_tree_host()->SetRootLayer(root); |
| 728 | LayerTreeTest::SetupTree(); |
| 729 | { |
| 730 | base::AutoLock lock(lock_); |
| 731 | expected_used_textures_on_commit_ = 1; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | virtual void BeginTest() OVERRIDE { |
| 736 | PostSetNeedsCommitToMainThread(); |
| 737 | } |
| 738 | |
| 739 | virtual void DidCommitAndDrawFrame() OVERRIDE { |
| 740 | ++commit_count_; |
| 741 | switch (commit_count_) { |
| 742 | case 1: |
| 743 | texture_layer_->ClearClient(); |
| 744 | texture_layer_->SetNeedsDisplay(); |
| 745 | { |
| 746 | base::AutoLock lock(lock_); |
| 747 | expected_used_textures_on_commit_ = 0; |
| 748 | } |
| 749 | texture_ = 0; |
| 750 | break; |
| 751 | case 2: |
| 752 | EndTest(); |
| 753 | break; |
| 754 | default: |
| 755 | NOTREACHED(); |
| 756 | break; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | virtual void BeginCommitOnThread(LayerTreeHostImpl* host_impl) OVERRIDE { |
| 761 | base::AutoLock lock(lock_); |
| 762 | expected_used_textures_on_draw_ = expected_used_textures_on_commit_; |
| 763 | } |
| 764 | |
| 765 | virtual bool PrepareToDrawOnThread(LayerTreeHostImpl* host_impl, |
| 766 | LayerTreeHostImpl::FrameData* frame_data, |
| 767 | bool result) OVERRIDE { |
| 768 | context_->ResetUsedTextures(); |
| 769 | return true; |
| 770 | } |
| 771 | |
| 772 | virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, |
| 773 | bool result) OVERRIDE { |
| 774 | ASSERT_TRUE(result); |
| 775 | EXPECT_EQ(expected_used_textures_on_draw_, context_->NumUsedTextures()); |
| 776 | } |
| 777 | |
| 778 | virtual void AfterTest() OVERRIDE {} |
| 779 | |
| 780 | private: |
| 781 | scoped_refptr<TextureLayer> texture_layer_; |
| 782 | TestWebGraphicsContext3D* context_; |
| 783 | unsigned texture_; |
| 784 | int commit_count_; |
| 785 | |
| 786 | // Used only on thread. |
| 787 | unsigned expected_used_textures_on_draw_; |
| 788 | |
| 789 | // Used on either thread, protected by lock_. |
| 790 | base::Lock lock_; |
| 791 | unsigned expected_used_textures_on_commit_; |
| 792 | }; |
| 793 | |
[email protected] | 4145d17 | 2013-05-10 16:54:36 | [diff] [blame] | 794 | // The TextureLayerClient does not use mailboxes, so can't use a delegating |
| 795 | // renderer. |
| 796 | SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TextureLayerClientTest); |
[email protected] | 97d519fb | 2013-03-29 02:27:54 | [diff] [blame] | 797 | |
[email protected] | 0bf5a20 | 2013-07-10 14:50:54 | [diff] [blame] | 798 | // Test recovering from a lost context. |
| 799 | class TextureLayerLostContextTest |
| 800 | : public LayerTreeTest, |
| 801 | public TextureLayerClient { |
| 802 | public: |
| 803 | TextureLayerLostContextTest() |
| 804 | : texture_(0), |
| 805 | draw_count_(0) {} |
| 806 | |
| 807 | virtual scoped_ptr<OutputSurface> CreateOutputSurface() OVERRIDE { |
| 808 | texture_context_ = TestWebGraphicsContext3D::Create(); |
| 809 | texture_ = texture_context_->createTexture(); |
| 810 | scoped_ptr<TestWebGraphicsContext3D> context( |
| 811 | TestWebGraphicsContext3D::Create()); |
| 812 | return FakeOutputSurface::Create3d( |
| 813 | context.PassAs<WebKit::WebGraphicsContext3D>()).PassAs<OutputSurface>(); |
| 814 | } |
| 815 | |
[email protected] | 171cbb3 | 2013-07-11 03:51:19 | [diff] [blame^] | 816 | virtual unsigned PrepareTexture() OVERRIDE { |
[email protected] | 0bf5a20 | 2013-07-10 14:50:54 | [diff] [blame] | 817 | if (draw_count_ == 0) { |
| 818 | texture_context_->loseContextCHROMIUM(GL_GUILTY_CONTEXT_RESET_ARB, |
| 819 | GL_INNOCENT_CONTEXT_RESET_ARB); |
| 820 | } |
| 821 | return texture_; |
| 822 | } |
| 823 | |
| 824 | virtual WebKit::WebGraphicsContext3D* Context3d() OVERRIDE { |
| 825 | return texture_context_.get(); |
| 826 | } |
| 827 | |
| 828 | virtual bool PrepareTextureMailbox( |
| 829 | cc::TextureMailbox* mailbox, bool use_shared_memory) OVERRIDE { |
| 830 | return false; |
| 831 | } |
| 832 | |
| 833 | virtual void SetupTree() OVERRIDE { |
| 834 | scoped_refptr<Layer> root = Layer::Create(); |
| 835 | root->SetBounds(gfx::Size(10, 10)); |
| 836 | root->SetIsDrawable(true); |
| 837 | |
| 838 | texture_layer_ = TextureLayer::Create(this); |
| 839 | texture_layer_->SetBounds(gfx::Size(10, 10)); |
| 840 | texture_layer_->SetIsDrawable(true); |
| 841 | root->AddChild(texture_layer_); |
| 842 | |
| 843 | layer_tree_host()->SetRootLayer(root); |
| 844 | LayerTreeTest::SetupTree(); |
| 845 | } |
| 846 | |
| 847 | virtual void BeginTest() OVERRIDE { |
| 848 | PostSetNeedsCommitToMainThread(); |
| 849 | } |
| 850 | |
| 851 | virtual bool PrepareToDrawOnThread(LayerTreeHostImpl* host_impl, |
| 852 | LayerTreeHostImpl::FrameData* frame_data, |
| 853 | bool result) OVERRIDE { |
| 854 | LayerImpl* root = host_impl->RootLayer(); |
| 855 | TextureLayerImpl* texture_layer = |
| 856 | static_cast<TextureLayerImpl*>(root->children()[0]); |
| 857 | if (++draw_count_ == 1) |
| 858 | EXPECT_EQ(0u, texture_layer->texture_id()); |
| 859 | else |
| 860 | EXPECT_EQ(texture_, texture_layer->texture_id()); |
| 861 | return true; |
| 862 | } |
| 863 | |
| 864 | virtual void DidCommitAndDrawFrame() OVERRIDE { |
| 865 | EndTest(); |
| 866 | } |
| 867 | |
| 868 | virtual void AfterTest() OVERRIDE {} |
| 869 | |
| 870 | private: |
| 871 | scoped_refptr<TextureLayer> texture_layer_; |
| 872 | scoped_ptr<TestWebGraphicsContext3D> texture_context_; |
| 873 | unsigned texture_; |
| 874 | int draw_count_; |
| 875 | }; |
| 876 | |
| 877 | SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TextureLayerLostContextTest); |
| 878 | |
[email protected] | ba56574 | 2012-11-10 09:29:48 | [diff] [blame] | 879 | } // namespace |
| 880 | } // namespace cc |