blob: 912eff9bd163295a17f6bf29a2c291972216a06f [file] [log] [blame]
[email protected]c0dd24c2012-08-30 23:25:271// 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]cc3cfaa2013-03-18 09:05:525#include "cc/layers/texture_layer.h"
[email protected]c0dd24c2012-08-30 23:25:276
[email protected]de44a152013-01-08 15:28:467#include <string>
8
9#include "base/callback.h"
[email protected]681ccff2013-03-18 06:13:5210#include "cc/base/thread.h"
[email protected]97d519fb2013-03-29 02:27:5411#include "cc/layers/texture_layer_client.h"
[email protected]cc3cfaa2013-03-18 09:05:5212#include "cc/layers/texture_layer_impl.h"
[email protected]586d51ed2012-12-07 20:31:4513#include "cc/test/fake_impl_proxy.h"
[email protected]101441ce2012-10-16 01:45:0314#include "cc/test/fake_layer_tree_host_client.h"
[email protected]586d51ed2012-12-07 20:31:4515#include "cc/test/fake_layer_tree_host_impl.h"
[email protected]e216fef02013-03-20 22:56:1016#include "cc/test/layer_tree_test.h"
[email protected]556fd292013-03-18 08:03:0417#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]7f0c53db2012-10-02 00:23:1820#include "testing/gmock/include/gmock/gmock.h"
21#include "testing/gtest/include/gtest/gtest.h"
[email protected]c0dd24c2012-08-30 23:25:2722
[email protected]c0dd24c2012-08-30 23:25:2723using ::testing::Mock;
24using ::testing::_;
25using ::testing::AtLeast;
26using ::testing::AnyNumber;
27
[email protected]ba565742012-11-10 09:29:4828namespace cc {
[email protected]c0dd24c2012-08-30 23:25:2729namespace {
30
[email protected]408b5e22013-03-19 09:48:0931class MockLayerTreeHost : public LayerTreeHost {
[email protected]28571b042013-03-14 07:59:1532 public:
[email protected]bf691c22013-03-26 21:15:0633 explicit MockLayerTreeHost(LayerTreeHostClient* client)
[email protected]408b5e22013-03-19 09:48:0934 : LayerTreeHost(client, LayerTreeSettings()) {
[email protected]28571b042013-03-14 07:59:1535 Initialize(scoped_ptr<Thread>(NULL));
36 }
[email protected]c0dd24c2012-08-30 23:25:2737
[email protected]28571b042013-03-14 07:59:1538 MOCK_METHOD0(AcquireLayerTextures, void());
39 MOCK_METHOD0(SetNeedsCommit, void());
[email protected]c0dd24c2012-08-30 23:25:2740};
41
[email protected]96baf3e2012-10-22 23:09:5542class TextureLayerTest : public testing::Test {
[email protected]28571b042013-03-14 07:59:1543 public:
[email protected]408b5e22013-03-19 09:48:0944 TextureLayerTest()
45 : fake_client_(
46 FakeLayerTreeHostClient(FakeLayerTreeHostClient::DIRECT_3D)),
47 host_impl_(&proxy_) {}
[email protected]0f077a52012-09-08 01:45:2448
[email protected]28571b042013-03-14 07:59:1549 protected:
50 virtual void SetUp() {
[email protected]408b5e22013-03-19 09:48:0951 layer_tree_host_.reset(new MockLayerTreeHost(&fake_client_));
[email protected]28571b042013-03-14 07:59:1552 }
[email protected]c0dd24c2012-08-30 23:25:2753
[email protected]28571b042013-03-14 07:59:1554 virtual void TearDown() {
55 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
56 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber());
57 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
[email protected]c0dd24c2012-08-30 23:25:2758
[email protected]28571b042013-03-14 07:59:1559 layer_tree_host_->SetRootLayer(NULL);
60 layer_tree_host_.reset();
61 }
[email protected]c0dd24c2012-08-30 23:25:2762
[email protected]408b5e22013-03-19 09:48:0963 scoped_ptr<MockLayerTreeHost> layer_tree_host_;
[email protected]28571b042013-03-14 07:59:1564 FakeImplProxy proxy_;
[email protected]408b5e22013-03-19 09:48:0965 FakeLayerTreeHostClient fake_client_;
[email protected]28571b042013-03-14 07:59:1566 FakeLayerTreeHostImpl host_impl_;
[email protected]c0dd24c2012-08-30 23:25:2767};
68
[email protected]28571b042013-03-14 07:59:1569TEST_F(TextureLayerTest, SyncImplWhenChangingTextureId) {
70 scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL);
71 ASSERT_TRUE(test_layer);
[email protected]c0dd24c2012-08-30 23:25:2772
[email protected]28571b042013-03-14 07:59:1573 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber());
74 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
75 layer_tree_host_->SetRootLayer(test_layer);
76 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
77 EXPECT_EQ(test_layer->layer_tree_host(), layer_tree_host_.get());
[email protected]c0dd24c2012-08-30 23:25:2778
[email protected]28571b042013-03-14 07:59:1579 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
80 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
81 test_layer->SetTextureId(1);
82 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]c0dd24c2012-08-30 23:25:2783
[email protected]28571b042013-03-14 07:59:1584 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1));
85 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
86 test_layer->SetTextureId(2);
87 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]c0dd24c2012-08-30 23:25:2788
[email protected]28571b042013-03-14 07:59:1589 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1));
90 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
91 test_layer->SetTextureId(0);
92 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]c0dd24c2012-08-30 23:25:2793}
94
[email protected]28571b042013-03-14 07:59:1595TEST_F(TextureLayerTest, SyncImplWhenDrawing) {
96 gfx::RectF dirty_rect(0.f, 0.f, 1.f, 1.f);
[email protected]031ccfff2012-10-26 00:57:3897
[email protected]28571b042013-03-14 07:59:1598 scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL);
99 ASSERT_TRUE(test_layer);
100 scoped_ptr<TextureLayerImpl> impl_layer;
101 impl_layer = TextureLayerImpl::Create(host_impl_.active_tree(), 1, false);
102 ASSERT_TRUE(impl_layer);
[email protected]031ccfff2012-10-26 00:57:38103
[email protected]28571b042013-03-14 07:59:15104 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber());
105 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
106 layer_tree_host_->SetRootLayer(test_layer);
107 test_layer->SetTextureId(1);
108 test_layer->SetIsDrawable(true);
109 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
110 EXPECT_EQ(test_layer->layer_tree_host(), layer_tree_host_.get());
[email protected]031ccfff2012-10-26 00:57:38111
[email protected]28571b042013-03-14 07:59:15112 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(1);
113 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
114 test_layer->WillModifyTexture();
115 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]031ccfff2012-10-26 00:57:38116
[email protected]28571b042013-03-14 07:59:15117 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
118 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(1);
119 test_layer->SetNeedsDisplayRect(dirty_rect);
120 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]031ccfff2012-10-26 00:57:38121
[email protected]28571b042013-03-14 07:59:15122 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
123 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(1);
124 test_layer->PushPropertiesTo(impl_layer.get()); // fake commit
125 test_layer->SetIsDrawable(false);
126 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]031ccfff2012-10-26 00:57:38127
[email protected]28571b042013-03-14 07:59:15128 // Verify that non-drawable layers don't signal the compositor,
129 // except for the first draw after last commit, which must acquire
130 // the texture.
131 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(1);
132 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
133 test_layer->WillModifyTexture();
134 test_layer->SetNeedsDisplayRect(dirty_rect);
135 test_layer->PushPropertiesTo(impl_layer.get()); // fake commit
136 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]031ccfff2012-10-26 00:57:38137
[email protected]28571b042013-03-14 07:59:15138 // Second draw with layer in non-drawable state: no texture
139 // acquisition.
140 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
141 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
142 test_layer->WillModifyTexture();
143 test_layer->SetNeedsDisplayRect(dirty_rect);
144 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]031ccfff2012-10-26 00:57:38145}
146
[email protected]28571b042013-03-14 07:59:15147TEST_F(TextureLayerTest, SyncImplWhenRemovingFromTree) {
148 scoped_refptr<Layer> root_layer = Layer::Create();
149 ASSERT_TRUE(root_layer);
150 scoped_refptr<Layer> child_layer = Layer::Create();
151 ASSERT_TRUE(child_layer);
152 root_layer->AddChild(child_layer);
153 scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL);
154 ASSERT_TRUE(test_layer);
155 test_layer->SetTextureId(0);
156 child_layer->AddChild(test_layer);
[email protected]c0dd24c2012-08-30 23:25:27157
[email protected]28571b042013-03-14 07:59:15158 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber());
159 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
160 layer_tree_host_->SetRootLayer(root_layer);
161 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]c0dd24c2012-08-30 23:25:27162
[email protected]28571b042013-03-14 07:59:15163 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
164 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
165 test_layer->RemoveFromParent();
166 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]c0dd24c2012-08-30 23:25:27167
[email protected]28571b042013-03-14 07:59:15168 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
169 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
170 child_layer->AddChild(test_layer);
171 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]c0dd24c2012-08-30 23:25:27172
[email protected]28571b042013-03-14 07:59:15173 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
174 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
175 test_layer->SetTextureId(1);
176 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]c0dd24c2012-08-30 23:25:27177
[email protected]28571b042013-03-14 07:59:15178 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1));
179 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
180 test_layer->RemoveFromParent();
181 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]c0dd24c2012-08-30 23:25:27182}
183
[email protected]de44a152013-01-08 15:28:46184class MockMailboxCallback {
[email protected]28571b042013-03-14 07:59:15185 public:
186 MOCK_METHOD2(Release, void(const std::string& mailbox, unsigned sync_point));
[email protected]de44a152013-01-08 15:28:46187};
188
189struct CommonMailboxObjects {
[email protected]28571b042013-03-14 07:59:15190 CommonMailboxObjects()
191 : mailbox_name1_(64, '1'),
192 mailbox_name2_(64, '2'),
193 sync_point1_(1),
194 sync_point2_(2) {
195 release_mailbox1_ = base::Bind(&MockMailboxCallback::Release,
196 base::Unretained(&mock_callback_),
197 mailbox_name1_);
198 release_mailbox2_ = base::Bind(&MockMailboxCallback::Release,
199 base::Unretained(&mock_callback_),
200 mailbox_name2_);
201 gpu::Mailbox m1;
202 m1.SetName(reinterpret_cast<const int8*>(mailbox_name1_.data()));
203 mailbox1_ = TextureMailbox(m1, release_mailbox1_, sync_point1_);
204 gpu::Mailbox m2;
205 m2.SetName(reinterpret_cast<const int8*>(mailbox_name2_.data()));
206 mailbox2_ = TextureMailbox(m2, release_mailbox2_, sync_point2_);
207 }
[email protected]de44a152013-01-08 15:28:46208
[email protected]28571b042013-03-14 07:59:15209 std::string mailbox_name1_;
210 std::string mailbox_name2_;
211 MockMailboxCallback mock_callback_;
212 TextureMailbox::ReleaseCallback release_mailbox1_;
213 TextureMailbox::ReleaseCallback release_mailbox2_;
214 TextureMailbox mailbox1_;
215 TextureMailbox mailbox2_;
216 unsigned sync_point1_;
217 unsigned sync_point2_;
[email protected]de44a152013-01-08 15:28:46218};
219
220class TextureLayerWithMailboxTest : public TextureLayerTest {
[email protected]28571b042013-03-14 07:59:15221 protected:
222 virtual void TearDown() {
223 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
224 EXPECT_CALL(test_data_.mock_callback_,
225 Release(test_data_.mailbox_name1_,
226 test_data_.sync_point1_)).Times(1);
227 TextureLayerTest::TearDown();
228 }
[email protected]de44a152013-01-08 15:28:46229
[email protected]28571b042013-03-14 07:59:15230 CommonMailboxObjects test_data_;
[email protected]de44a152013-01-08 15:28:46231};
232
[email protected]28571b042013-03-14 07:59:15233TEST_F(TextureLayerWithMailboxTest, ReplaceMailboxOnMainThreadBeforeCommit) {
234 scoped_refptr<TextureLayer> test_layer = TextureLayer::CreateForMailbox();
235 ASSERT_TRUE(test_layer);
[email protected]de44a152013-01-08 15:28:46236
[email protected]28571b042013-03-14 07:59:15237 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
238 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
239 layer_tree_host_->SetRootLayer(test_layer);
240 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]de44a152013-01-08 15:28:46241
[email protected]28571b042013-03-14 07:59:15242 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
243 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
244 test_layer->SetTextureMailbox(test_data_.mailbox1_);
245 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]de44a152013-01-08 15:28:46246
[email protected]28571b042013-03-14 07:59:15247 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
248 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
249 EXPECT_CALL(test_data_.mock_callback_,
250 Release(test_data_.mailbox_name1_, test_data_.sync_point1_))
251 .Times(1);
252 test_layer->SetTextureMailbox(test_data_.mailbox2_);
253 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
254 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]de44a152013-01-08 15:28:46255
[email protected]28571b042013-03-14 07:59:15256 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
257 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
258 EXPECT_CALL(test_data_.mock_callback_,
259 Release(test_data_.mailbox_name2_, test_data_.sync_point2_))
260 .Times(1);
261 test_layer->SetTextureMailbox(TextureMailbox());
262 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
263 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]de44a152013-01-08 15:28:46264
[email protected]28571b042013-03-14 07:59:15265 // Test destructor.
266 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
267 test_layer->SetTextureMailbox(test_data_.mailbox1_);
[email protected]de44a152013-01-08 15:28:46268}
269
[email protected]e216fef02013-03-20 22:56:10270class TextureLayerImplWithMailboxThreadedCallback : public LayerTreeTest {
[email protected]28571b042013-03-14 07:59:15271 public:
272 TextureLayerImplWithMailboxThreadedCallback()
273 : callback_count_(0),
274 commit_count_(0) {}
275
276 // Make sure callback is received on main and doesn't block the impl thread.
277 void ReleaseCallback(unsigned sync_point) {
278 EXPECT_EQ(true, proxy()->IsMainThread());
279 ++callback_count_;
280 }
281
282 void SetMailbox(char mailbox_char) {
283 TextureMailbox mailbox(
284 std::string(64, mailbox_char),
285 base::Bind(
286 &TextureLayerImplWithMailboxThreadedCallback::ReleaseCallback,
287 base::Unretained(this)));
288 layer_->SetTextureMailbox(mailbox);
289 }
290
[email protected]e216fef02013-03-20 22:56:10291 virtual void BeginTest() OVERRIDE {
[email protected]28571b042013-03-14 07:59:15292 gfx::Size bounds(100, 100);
293 root_ = Layer::Create();
294 root_->SetAnchorPoint(gfx::PointF());
295 root_->SetBounds(bounds);
296
297 layer_ = TextureLayer::CreateForMailbox();
298 layer_->SetIsDrawable(true);
299 layer_->SetAnchorPoint(gfx::PointF());
300 layer_->SetBounds(bounds);
301
302 root_->AddChild(layer_);
[email protected]e216fef02013-03-20 22:56:10303 layer_tree_host()->SetRootLayer(root_);
304 layer_tree_host()->SetViewportSize(bounds, bounds);
[email protected]28571b042013-03-14 07:59:15305 SetMailbox('1');
306 EXPECT_EQ(0, callback_count_);
307
308 // Case #1: change mailbox before the commit. The old mailbox should be
309 // released immediately.
310 SetMailbox('2');
311 EXPECT_EQ(1, callback_count_);
[email protected]e216fef02013-03-20 22:56:10312 PostSetNeedsCommitToMainThread();
[email protected]28571b042013-03-14 07:59:15313 }
314
[email protected]e216fef02013-03-20 22:56:10315 virtual void DidCommit() OVERRIDE {
[email protected]28571b042013-03-14 07:59:15316 ++commit_count_;
317 switch (commit_count_) {
318 case 1:
319 // Case #2: change mailbox after the commit (and draw), where the
320 // layer draws. The old mailbox should be released during the next
321 // commit.
322 SetMailbox('3');
323 EXPECT_EQ(1, callback_count_);
324 break;
325 case 2:
326 // Old mailbox was released, task was posted, but won't execute
[email protected]ed511b8d2013-03-25 03:29:29327 // until this DidCommit returns.
[email protected]28571b042013-03-14 07:59:15328 // TODO(piman): fix this.
329 EXPECT_EQ(1, callback_count_);
[email protected]e216fef02013-03-20 22:56:10330 layer_tree_host()->SetNeedsCommit();
[email protected]28571b042013-03-14 07:59:15331 break;
332 case 3:
333 EXPECT_EQ(2, callback_count_);
334 // Case #3: change mailbox when the layer doesn't draw. The old
335 // mailbox should be released during the next commit.
336 layer_->SetBounds(gfx::Size());
337 SetMailbox('4');
338 break;
339 case 4:
340 // Old mailbox was released, task was posted, but won't execute
[email protected]ed511b8d2013-03-25 03:29:29341 // until this DidCommit returns.
[email protected]28571b042013-03-14 07:59:15342 // TODO(piman): fix this.
343 EXPECT_EQ(2, callback_count_);
[email protected]e216fef02013-03-20 22:56:10344 layer_tree_host()->SetNeedsCommit();
[email protected]28571b042013-03-14 07:59:15345 break;
346 case 5:
347 EXPECT_EQ(3, callback_count_);
348 // Case #4: release mailbox that was committed but never drawn. The
349 // old mailbox should be released during the next commit.
350 layer_->SetTextureMailbox(TextureMailbox());
351 break;
352 case 6:
353 // Old mailbox was released, task was posted, but won't execute
[email protected]ed511b8d2013-03-25 03:29:29354 // until this DidCommit returns.
[email protected]28571b042013-03-14 07:59:15355 // TODO(piman): fix this.
356 EXPECT_EQ(3, callback_count_);
[email protected]e216fef02013-03-20 22:56:10357 layer_tree_host()->SetNeedsCommit();
[email protected]28571b042013-03-14 07:59:15358 break;
359 case 7:
360 EXPECT_EQ(4, callback_count_);
[email protected]e216fef02013-03-20 22:56:10361 EndTest();
[email protected]28571b042013-03-14 07:59:15362 break;
363 default:
364 NOTREACHED();
365 break;
[email protected]de44a152013-01-08 15:28:46366 }
[email protected]28571b042013-03-14 07:59:15367 }
[email protected]de44a152013-01-08 15:28:46368
[email protected]e216fef02013-03-20 22:56:10369 virtual void AfterTest() OVERRIDE {}
[email protected]de44a152013-01-08 15:28:46370
[email protected]28571b042013-03-14 07:59:15371 private:
372 int callback_count_;
373 int commit_count_;
374 scoped_refptr<Layer> root_;
375 scoped_refptr<TextureLayer> layer_;
[email protected]de44a152013-01-08 15:28:46376};
377
378SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerImplWithMailboxThreadedCallback);
379
380class TextureLayerImplWithMailboxTest : public TextureLayerTest {
[email protected]28571b042013-03-14 07:59:15381 protected:
[email protected]408b5e22013-03-19 09:48:09382 TextureLayerImplWithMailboxTest()
383 : fake_client_(
384 FakeLayerTreeHostClient(FakeLayerTreeHostClient::DIRECT_3D)) {}
385
[email protected]28571b042013-03-14 07:59:15386 virtual void SetUp() {
387 TextureLayerTest::SetUp();
[email protected]408b5e22013-03-19 09:48:09388 layer_tree_host_.reset(new MockLayerTreeHost(&fake_client_));
[email protected]f74945f2013-03-21 17:08:36389 EXPECT_TRUE(host_impl_.InitializeRenderer(CreateFakeOutputSurface()));
[email protected]28571b042013-03-14 07:59:15390 }
[email protected]de44a152013-01-08 15:28:46391
[email protected]28571b042013-03-14 07:59:15392 CommonMailboxObjects test_data_;
[email protected]408b5e22013-03-19 09:48:09393 FakeLayerTreeHostClient fake_client_;
[email protected]de44a152013-01-08 15:28:46394};
395
[email protected]28571b042013-03-14 07:59:15396TEST_F(TextureLayerImplWithMailboxTest, TestImplLayerCallbacks) {
397 host_impl_.CreatePendingTree();
398 scoped_ptr<TextureLayerImpl> pending_layer;
399 pending_layer = TextureLayerImpl::Create(host_impl_.pending_tree(), 1, true);
400 ASSERT_TRUE(pending_layer);
[email protected]de44a152013-01-08 15:28:46401
[email protected]ed511b8d2013-03-25 03:29:29402 scoped_ptr<LayerImpl> active_layer(
[email protected]28571b042013-03-14 07:59:15403 pending_layer->CreateLayerImpl(host_impl_.active_tree()));
[email protected]ed511b8d2013-03-25 03:29:29404 ASSERT_TRUE(active_layer);
[email protected]de44a152013-01-08 15:28:46405
[email protected]1a37a0752013-03-17 21:25:49406 pending_layer->SetTextureMailbox(test_data_.mailbox1_);
[email protected]421e84f2013-02-22 03:27:15407
[email protected]28571b042013-03-14 07:59:15408 // Test multiple commits without an activation.
409 EXPECT_CALL(test_data_.mock_callback_,
410 Release(test_data_.mailbox_name1_, test_data_.sync_point1_))
411 .Times(1);
[email protected]1a37a0752013-03-17 21:25:49412 pending_layer->SetTextureMailbox(test_data_.mailbox2_);
[email protected]28571b042013-03-14 07:59:15413 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]421e84f2013-02-22 03:27:15414
[email protected]28571b042013-03-14 07:59:15415 // Test callback after activation.
[email protected]ed511b8d2013-03-25 03:29:29416 pending_layer->PushPropertiesTo(active_layer.get());
417 active_layer->DidBecomeActive();
[email protected]421e84f2013-02-22 03:27:15418
[email protected]28571b042013-03-14 07:59:15419 EXPECT_CALL(test_data_.mock_callback_, Release(_, _)).Times(0);
[email protected]1a37a0752013-03-17 21:25:49420 pending_layer->SetTextureMailbox(test_data_.mailbox1_);
[email protected]28571b042013-03-14 07:59:15421 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]421e84f2013-02-22 03:27:15422
[email protected]28571b042013-03-14 07:59:15423 EXPECT_CALL(test_data_.mock_callback_, Release(test_data_.mailbox_name2_, _))
424 .Times(1);
[email protected]ed511b8d2013-03-25 03:29:29425 pending_layer->PushPropertiesTo(active_layer.get());
426 active_layer->DidBecomeActive();
[email protected]28571b042013-03-14 07:59:15427 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]de44a152013-01-08 15:28:46428
[email protected]28571b042013-03-14 07:59:15429 // Test resetting the mailbox.
430 EXPECT_CALL(test_data_.mock_callback_, Release(test_data_.mailbox_name1_, _))
431 .Times(1);
[email protected]1a37a0752013-03-17 21:25:49432 pending_layer->SetTextureMailbox(TextureMailbox());
[email protected]ed511b8d2013-03-25 03:29:29433 pending_layer->PushPropertiesTo(active_layer.get());
434 active_layer->DidBecomeActive();
[email protected]28571b042013-03-14 07:59:15435 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]de44a152013-01-08 15:28:46436
[email protected]28571b042013-03-14 07:59:15437 // Test destructor.
438 EXPECT_CALL(test_data_.mock_callback_,
439 Release(test_data_.mailbox_name1_, test_data_.sync_point1_))
440 .Times(1);
[email protected]1a37a0752013-03-17 21:25:49441 pending_layer->SetTextureMailbox(test_data_.mailbox1_);
[email protected]de44a152013-01-08 15:28:46442}
443
[email protected]28571b042013-03-14 07:59:15444TEST_F(TextureLayerImplWithMailboxTest,
445 TestDestructorCallbackOnCreatedResource) {
446 scoped_ptr<TextureLayerImpl> impl_layer;
447 impl_layer = TextureLayerImpl::Create(host_impl_.active_tree(), 1, true);
448 ASSERT_TRUE(impl_layer);
[email protected]de44a152013-01-08 15:28:46449
[email protected]28571b042013-03-14 07:59:15450 EXPECT_CALL(test_data_.mock_callback_, Release(test_data_.mailbox_name1_, _))
451 .Times(1);
[email protected]1a37a0752013-03-17 21:25:49452 impl_layer->SetTextureMailbox(test_data_.mailbox1_);
[email protected]28571b042013-03-14 07:59:15453 impl_layer->WillDraw(host_impl_.active_tree()->resource_provider());
454 impl_layer->DidDraw(host_impl_.active_tree()->resource_provider());
[email protected]1a37a0752013-03-17 21:25:49455 impl_layer->SetTextureMailbox(TextureMailbox());
[email protected]de44a152013-01-08 15:28:46456}
457
[email protected]28571b042013-03-14 07:59:15458TEST_F(TextureLayerImplWithMailboxTest, TestCallbackOnInUseResource) {
459 ResourceProvider* provider = host_impl_.active_tree()->resource_provider();
460 ResourceProvider::ResourceId id =
461 provider->CreateResourceFromTextureMailbox(test_data_.mailbox1_);
462 provider->AllocateForTesting(id);
[email protected]de44a152013-01-08 15:28:46463
[email protected]28571b042013-03-14 07:59:15464 // Transfer some resources to the parent.
465 ResourceProvider::ResourceIdArray resource_ids_to_transfer;
466 resource_ids_to_transfer.push_back(id);
467 TransferableResourceArray list;
468 provider->PrepareSendToParent(resource_ids_to_transfer, &list);
469 EXPECT_TRUE(provider->InUseByConsumer(id));
470 EXPECT_CALL(test_data_.mock_callback_, Release(_, _)).Times(0);
471 provider->DeleteResource(id);
472 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
473 EXPECT_CALL(test_data_.mock_callback_, Release(test_data_.mailbox_name1_, _))
474 .Times(1);
475 provider->ReceiveFromParent(list);
[email protected]de44a152013-01-08 15:28:46476}
477
[email protected]97d519fb2013-03-29 02:27:54478// Check that ClearClient correctly clears the state so that the impl side
479// doesn't try to use a texture that could have been destroyed.
480class TextureLayerClientTest :
481 public LayerTreeTest,
482 public TextureLayerClient {
483 public:
484 TextureLayerClientTest()
485 : context_(NULL),
486 texture_(0),
487 commit_count_(0),
488 expected_used_textures_on_draw_(0),
489 expected_used_textures_on_commit_(0) {}
490
491 virtual scoped_ptr<OutputSurface> CreateOutputSurface() OVERRIDE {
492 scoped_ptr<TestWebGraphicsContext3D> context(
493 TestWebGraphicsContext3D::Create());
494 context_ = context.get();
495 texture_ = context->createTexture();
496 return FakeOutputSurface::Create3d(
497 context.PassAs<WebKit::WebGraphicsContext3D>()).PassAs<OutputSurface>();
498 }
499
500 virtual unsigned PrepareTexture(ResourceUpdateQueue* queue) {
501 return texture_;
502 }
503
504 virtual WebKit::WebGraphicsContext3D* Context3d() {
505 return context_;
506 }
507
508 virtual void SetupTree() OVERRIDE {
509 scoped_refptr<Layer> root = Layer::Create();
510 root->SetBounds(gfx::Size(10, 10));
511 root->SetAnchorPoint(gfx::PointF());
512 root->SetIsDrawable(true);
513
514 texture_layer_ = TextureLayer::Create(this);
515 texture_layer_->SetBounds(gfx::Size(10, 10));
516 texture_layer_->SetAnchorPoint(gfx::PointF());
517 texture_layer_->SetIsDrawable(true);
518 root->AddChild(texture_layer_);
519
520 layer_tree_host()->SetRootLayer(root);
521 LayerTreeTest::SetupTree();
522 {
523 base::AutoLock lock(lock_);
524 expected_used_textures_on_commit_ = 1;
525 }
526 }
527
528 virtual void BeginTest() OVERRIDE {
529 PostSetNeedsCommitToMainThread();
530 }
531
532 virtual void DidCommitAndDrawFrame() OVERRIDE {
533 ++commit_count_;
534 switch (commit_count_) {
535 case 1:
536 texture_layer_->ClearClient();
537 texture_layer_->SetNeedsDisplay();
538 {
539 base::AutoLock lock(lock_);
540 expected_used_textures_on_commit_ = 0;
541 }
542 texture_ = 0;
543 break;
544 case 2:
545 EndTest();
546 break;
547 default:
548 NOTREACHED();
549 break;
550 }
551 }
552
553 virtual void BeginCommitOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
554 base::AutoLock lock(lock_);
555 expected_used_textures_on_draw_ = expected_used_textures_on_commit_;
556 }
557
558 virtual bool PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
559 LayerTreeHostImpl::FrameData* frame_data,
560 bool result) OVERRIDE {
561 context_->ResetUsedTextures();
562 return true;
563 }
564
565 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
566 bool result) OVERRIDE {
567 ASSERT_TRUE(result);
568 EXPECT_EQ(expected_used_textures_on_draw_, context_->NumUsedTextures());
569 }
570
571 virtual void AfterTest() OVERRIDE {}
572
573 private:
574 scoped_refptr<TextureLayer> texture_layer_;
575 TestWebGraphicsContext3D* context_;
576 unsigned texture_;
577 int commit_count_;
578
579 // Used only on thread.
580 unsigned expected_used_textures_on_draw_;
581
582 // Used on either thread, protected by lock_.
583 base::Lock lock_;
584 unsigned expected_used_textures_on_commit_;
585};
586
587SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerClientTest);
588
[email protected]ba565742012-11-10 09:29:48589} // namespace
590} // namespace cc