blob: ffdadf89c21bbf6398aab30001f84c8cfc1d185e [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]97d519fb2013-03-29 02:27:5410#include "cc/layers/texture_layer_client.h"
[email protected]cc3cfaa2013-03-18 09:05:5211#include "cc/layers/texture_layer_impl.h"
[email protected]586d51ed2012-12-07 20:31:4512#include "cc/test/fake_impl_proxy.h"
[email protected]101441ce2012-10-16 01:45:0313#include "cc/test/fake_layer_tree_host_client.h"
[email protected]586d51ed2012-12-07 20:31:4514#include "cc/test/fake_layer_tree_host_impl.h"
[email protected]06d68d02013-04-19 18:46:2115#include "cc/test/layer_test_common.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]0bf5a202013-07-10 14:50:5420#include "gpu/GLES2/gl2extchromium.h"
[email protected]7f0c53db2012-10-02 00:23:1821#include "testing/gmock/include/gmock/gmock.h"
22#include "testing/gtest/include/gtest/gtest.h"
[email protected]c0dd24c2012-08-30 23:25:2723
[email protected]c0dd24c2012-08-30 23:25:2724using ::testing::Mock;
25using ::testing::_;
26using ::testing::AtLeast;
27using ::testing::AnyNumber;
28
[email protected]ba565742012-11-10 09:29:4829namespace cc {
[email protected]c0dd24c2012-08-30 23:25:2730namespace {
31
[email protected]408b5e22013-03-19 09:48:0932class MockLayerTreeHost : public LayerTreeHost {
[email protected]28571b042013-03-14 07:59:1533 public:
[email protected]bf691c22013-03-26 21:15:0634 explicit MockLayerTreeHost(LayerTreeHostClient* client)
[email protected]408b5e22013-03-19 09:48:0935 : LayerTreeHost(client, LayerTreeSettings()) {
[email protected]810d40b72013-06-20 18:26:1536 Initialize(NULL);
[email protected]28571b042013-03-14 07:59:1537 }
[email protected]c0dd24c2012-08-30 23:25:2738
[email protected]28571b042013-03-14 07:59:1539 MOCK_METHOD0(AcquireLayerTextures, void());
40 MOCK_METHOD0(SetNeedsCommit, void());
[email protected]c0dd24c2012-08-30 23:25:2741};
42
[email protected]96baf3e2012-10-22 23:09:5543class TextureLayerTest : public testing::Test {
[email protected]28571b042013-03-14 07:59:1544 public:
[email protected]408b5e22013-03-19 09:48:0945 TextureLayerTest()
46 : fake_client_(
47 FakeLayerTreeHostClient(FakeLayerTreeHostClient::DIRECT_3D)),
48 host_impl_(&proxy_) {}
[email protected]0f077a52012-09-08 01:45:2449
[email protected]28571b042013-03-14 07:59:1550 protected:
51 virtual void SetUp() {
[email protected]408b5e22013-03-19 09:48:0952 layer_tree_host_.reset(new MockLayerTreeHost(&fake_client_));
[email protected]28571b042013-03-14 07:59:1553 }
[email protected]c0dd24c2012-08-30 23:25:2754
[email protected]28571b042013-03-14 07:59:1555 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]c0dd24c2012-08-30 23:25:2759
[email protected]28571b042013-03-14 07:59:1560 layer_tree_host_->SetRootLayer(NULL);
61 layer_tree_host_.reset();
62 }
[email protected]c0dd24c2012-08-30 23:25:2763
[email protected]408b5e22013-03-19 09:48:0964 scoped_ptr<MockLayerTreeHost> layer_tree_host_;
[email protected]28571b042013-03-14 07:59:1565 FakeImplProxy proxy_;
[email protected]408b5e22013-03-19 09:48:0966 FakeLayerTreeHostClient fake_client_;
[email protected]28571b042013-03-14 07:59:1567 FakeLayerTreeHostImpl host_impl_;
[email protected]c0dd24c2012-08-30 23:25:2768};
69
[email protected]28571b042013-03-14 07:59:1570TEST_F(TextureLayerTest, SyncImplWhenChangingTextureId) {
71 scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL);
[email protected]22898ed2013-06-01 04:52:3072 ASSERT_TRUE(test_layer.get());
[email protected]c0dd24c2012-08-30 23:25:2773
[email protected]28571b042013-03-14 07:59:1574 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]c0dd24c2012-08-30 23:25:2779
[email protected]28571b042013-03-14 07:59:1580 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]c0dd24c2012-08-30 23:25:2784
[email protected]28571b042013-03-14 07:59:1585 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]c0dd24c2012-08-30 23:25:2789
[email protected]28571b042013-03-14 07:59:1590 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]c0dd24c2012-08-30 23:25:2794}
95
[email protected]28571b042013-03-14 07:59:1596TEST_F(TextureLayerTest, SyncImplWhenDrawing) {
97 gfx::RectF dirty_rect(0.f, 0.f, 1.f, 1.f);
[email protected]031ccfff2012-10-26 00:57:3898
[email protected]28571b042013-03-14 07:59:1599 scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL);
[email protected]22898ed2013-06-01 04:52:30100 ASSERT_TRUE(test_layer.get());
[email protected]28571b042013-03-14 07:59:15101 scoped_ptr<TextureLayerImpl> impl_layer;
102 impl_layer = TextureLayerImpl::Create(host_impl_.active_tree(), 1, false);
103 ASSERT_TRUE(impl_layer);
[email protected]031ccfff2012-10-26 00:57:38104
[email protected]28571b042013-03-14 07:59:15105 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]031ccfff2012-10-26 00:57:38112
[email protected]28571b042013-03-14 07:59:15113 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]031ccfff2012-10-26 00:57:38117
[email protected]28571b042013-03-14 07:59:15118 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]031ccfff2012-10-26 00:57:38122
[email protected]28571b042013-03-14 07:59:15123 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]031ccfff2012-10-26 00:57:38128
[email protected]28571b042013-03-14 07:59:15129 // 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]031ccfff2012-10-26 00:57:38138
[email protected]28571b042013-03-14 07:59:15139 // 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]031ccfff2012-10-26 00:57:38146}
147
[email protected]28571b042013-03-14 07:59:15148TEST_F(TextureLayerTest, SyncImplWhenRemovingFromTree) {
149 scoped_refptr<Layer> root_layer = Layer::Create();
[email protected]22898ed2013-06-01 04:52:30150 ASSERT_TRUE(root_layer.get());
[email protected]28571b042013-03-14 07:59:15151 scoped_refptr<Layer> child_layer = Layer::Create();
[email protected]22898ed2013-06-01 04:52:30152 ASSERT_TRUE(child_layer.get());
[email protected]28571b042013-03-14 07:59:15153 root_layer->AddChild(child_layer);
154 scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL);
[email protected]22898ed2013-06-01 04:52:30155 ASSERT_TRUE(test_layer.get());
[email protected]28571b042013-03-14 07:59:15156 test_layer->SetTextureId(0);
157 child_layer->AddChild(test_layer);
[email protected]c0dd24c2012-08-30 23:25:27158
[email protected]28571b042013-03-14 07:59:15159 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]c0dd24c2012-08-30 23:25:27163
[email protected]28571b042013-03-14 07:59:15164 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]c0dd24c2012-08-30 23:25:27168
[email protected]28571b042013-03-14 07:59:15169 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]c0dd24c2012-08-30 23:25:27173
[email protected]28571b042013-03-14 07:59:15174 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]c0dd24c2012-08-30 23:25:27178
[email protected]28571b042013-03-14 07:59:15179 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]c0dd24c2012-08-30 23:25:27183}
184
[email protected]06d68d02013-04-19 18:46:21185TEST_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]de44a152013-01-08 15:28:46203class MockMailboxCallback {
[email protected]28571b042013-03-14 07:59:15204 public:
[email protected]7ba3ca72013-04-11 06:37:25205 MOCK_METHOD3(Release, void(const std::string& mailbox,
206 unsigned sync_point,
207 bool lost_resource));
[email protected]42f40a52013-06-08 04:38:51208 MOCK_METHOD3(Release2, void(base::SharedMemory* shared_memory,
209 unsigned sync_point,
210 bool lost_resource));
[email protected]de44a152013-01-08 15:28:46211};
212
213struct CommonMailboxObjects {
[email protected]28571b042013-03-14 07:59:15214 CommonMailboxObjects()
215 : mailbox_name1_(64, '1'),
216 mailbox_name2_(64, '2'),
217 sync_point1_(1),
[email protected]42f40a52013-06-08 04:38:51218 sync_point2_(2),
219 shared_memory_(new base::SharedMemory) {
[email protected]28571b042013-03-14 07:59:15220 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]42f40a52013-06-08 04:38:51232
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]28571b042013-03-14 07:59:15239 }
[email protected]de44a152013-01-08 15:28:46240
[email protected]28571b042013-03-14 07:59:15241 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]42f40a52013-06-08 04:38:51246 TextureMailbox::ReleaseCallback release_mailbox3_;
[email protected]28571b042013-03-14 07:59:15247 TextureMailbox mailbox1_;
248 TextureMailbox mailbox2_;
[email protected]42f40a52013-06-08 04:38:51249 TextureMailbox mailbox3_;
[email protected]28571b042013-03-14 07:59:15250 unsigned sync_point1_;
251 unsigned sync_point2_;
[email protected]42f40a52013-06-08 04:38:51252 scoped_ptr<base::SharedMemory> shared_memory_;
[email protected]de44a152013-01-08 15:28:46253};
254
255class TextureLayerWithMailboxTest : public TextureLayerTest {
[email protected]28571b042013-03-14 07:59:15256 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]7ba3ca72013-04-11 06:37:25261 test_data_.sync_point1_,
262 false)).Times(1);
[email protected]28571b042013-03-14 07:59:15263 TextureLayerTest::TearDown();
264 }
[email protected]de44a152013-01-08 15:28:46265
[email protected]28571b042013-03-14 07:59:15266 CommonMailboxObjects test_data_;
[email protected]de44a152013-01-08 15:28:46267};
268
[email protected]28571b042013-03-14 07:59:15269TEST_F(TextureLayerWithMailboxTest, ReplaceMailboxOnMainThreadBeforeCommit) {
[email protected]e8e4ae232013-04-12 00:26:01270 scoped_refptr<TextureLayer> test_layer = TextureLayer::CreateForMailbox(NULL);
[email protected]22898ed2013-06-01 04:52:30271 ASSERT_TRUE(test_layer.get());
[email protected]de44a152013-01-08 15:28:46272
[email protected]28571b042013-03-14 07:59:15273 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]de44a152013-01-08 15:28:46277
[email protected]28571b042013-03-14 07:59:15278 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]de44a152013-01-08 15:28:46282
[email protected]28571b042013-03-14 07:59:15283 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]7ba3ca72013-04-11 06:37:25286 Release(test_data_.mailbox_name1_,
287 test_data_.sync_point1_,
288 false))
[email protected]28571b042013-03-14 07:59:15289 .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]de44a152013-01-08 15:28:46293
[email protected]28571b042013-03-14 07:59:15294 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]7ba3ca72013-04-11 06:37:25297 Release(test_data_.mailbox_name2_,
298 test_data_.sync_point2_,
299 false))
[email protected]28571b042013-03-14 07:59:15300 .Times(1);
301 test_layer->SetTextureMailbox(TextureMailbox());
302 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
303 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]de44a152013-01-08 15:28:46304
[email protected]42f40a52013-06-08 04:38:51305 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]28571b042013-03-14 07:59:15319 // Test destructor.
320 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
321 test_layer->SetTextureMailbox(test_data_.mailbox1_);
[email protected]de44a152013-01-08 15:28:46322}
323
[email protected]e216fef02013-03-20 22:56:10324class TextureLayerImplWithMailboxThreadedCallback : public LayerTreeTest {
[email protected]28571b042013-03-14 07:59:15325 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]7ba3ca72013-04-11 06:37:25331 void ReleaseCallback(unsigned sync_point, bool lost_resource) {
[email protected]28571b042013-03-14 07:59:15332 EXPECT_EQ(true, proxy()->IsMainThread());
[email protected]7ba3ca72013-04-11 06:37:25333 EXPECT_FALSE(lost_resource);
[email protected]28571b042013-03-14 07:59:15334 ++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]e216fef02013-03-20 22:56:10346 virtual void BeginTest() OVERRIDE {
[email protected]28571b042013-03-14 07:59:15347 gfx::Size bounds(100, 100);
348 root_ = Layer::Create();
349 root_->SetAnchorPoint(gfx::PointF());
350 root_->SetBounds(bounds);
351
[email protected]e8e4ae232013-04-12 00:26:01352 layer_ = TextureLayer::CreateForMailbox(NULL);
[email protected]28571b042013-03-14 07:59:15353 layer_->SetIsDrawable(true);
354 layer_->SetAnchorPoint(gfx::PointF());
355 layer_->SetBounds(bounds);
356
357 root_->AddChild(layer_);
[email protected]e216fef02013-03-20 22:56:10358 layer_tree_host()->SetRootLayer(root_);
[email protected]18ce59702013-04-09 04:58:40359 layer_tree_host()->SetViewportSize(bounds);
[email protected]28571b042013-03-14 07:59:15360 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]e216fef02013-03-20 22:56:10367 PostSetNeedsCommitToMainThread();
[email protected]28571b042013-03-14 07:59:15368 }
369
[email protected]e216fef02013-03-20 22:56:10370 virtual void DidCommit() OVERRIDE {
[email protected]28571b042013-03-14 07:59:15371 ++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]ed511b8d2013-03-25 03:29:29382 // until this DidCommit returns.
[email protected]28571b042013-03-14 07:59:15383 // TODO(piman): fix this.
384 EXPECT_EQ(1, callback_count_);
[email protected]e216fef02013-03-20 22:56:10385 layer_tree_host()->SetNeedsCommit();
[email protected]28571b042013-03-14 07:59:15386 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]ed511b8d2013-03-25 03:29:29396 // until this DidCommit returns.
[email protected]28571b042013-03-14 07:59:15397 // TODO(piman): fix this.
398 EXPECT_EQ(2, callback_count_);
[email protected]e216fef02013-03-20 22:56:10399 layer_tree_host()->SetNeedsCommit();
[email protected]28571b042013-03-14 07:59:15400 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]ed511b8d2013-03-25 03:29:29409 // until this DidCommit returns.
[email protected]28571b042013-03-14 07:59:15410 // TODO(piman): fix this.
411 EXPECT_EQ(3, callback_count_);
[email protected]e216fef02013-03-20 22:56:10412 layer_tree_host()->SetNeedsCommit();
[email protected]28571b042013-03-14 07:59:15413 break;
414 case 7:
415 EXPECT_EQ(4, callback_count_);
[email protected]7096acc2013-06-18 21:12:43416 // 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]e216fef02013-03-20 22:56:10437 EndTest();
[email protected]28571b042013-03-14 07:59:15438 break;
439 default:
440 NOTREACHED();
441 break;
[email protected]de44a152013-01-08 15:28:46442 }
[email protected]28571b042013-03-14 07:59:15443 }
[email protected]de44a152013-01-08 15:28:46444
[email protected]e216fef02013-03-20 22:56:10445 virtual void AfterTest() OVERRIDE {}
[email protected]de44a152013-01-08 15:28:46446
[email protected]28571b042013-03-14 07:59:15447 private:
448 int callback_count_;
449 int commit_count_;
450 scoped_refptr<Layer> root_;
451 scoped_refptr<TextureLayer> layer_;
[email protected]de44a152013-01-08 15:28:46452};
453
[email protected]4145d172013-05-10 16:54:36454SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(
455 TextureLayerImplWithMailboxThreadedCallback);
[email protected]de44a152013-01-08 15:28:46456
457class TextureLayerImplWithMailboxTest : public TextureLayerTest {
[email protected]28571b042013-03-14 07:59:15458 protected:
[email protected]408b5e22013-03-19 09:48:09459 TextureLayerImplWithMailboxTest()
460 : fake_client_(
461 FakeLayerTreeHostClient(FakeLayerTreeHostClient::DIRECT_3D)) {}
462
[email protected]28571b042013-03-14 07:59:15463 virtual void SetUp() {
464 TextureLayerTest::SetUp();
[email protected]408b5e22013-03-19 09:48:09465 layer_tree_host_.reset(new MockLayerTreeHost(&fake_client_));
[email protected]f74945f2013-03-21 17:08:36466 EXPECT_TRUE(host_impl_.InitializeRenderer(CreateFakeOutputSurface()));
[email protected]28571b042013-03-14 07:59:15467 }
[email protected]de44a152013-01-08 15:28:46468
[email protected]0ec335c42013-07-04 06:17:08469 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]28571b042013-03-14 07:59:15477 CommonMailboxObjects test_data_;
[email protected]408b5e22013-03-19 09:48:09478 FakeLayerTreeHostClient fake_client_;
[email protected]de44a152013-01-08 15:28:46479};
480
[email protected]ffbb2212013-06-02 23:47:59481// Test conditions for results of TextureLayerImpl::WillDraw under
482// different configurations of different mailbox, texture_id, and draw_mode.
483TEST_F(TextureLayerImplWithMailboxTest, TestWillDraw) {
[email protected]0ec335c42013-07-04 06:17:08484 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]ffbb2212013-06-02 23:47:59492 // 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]0ec335c42013-07-04 06:17:08497 EXPECT_TRUE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE));
[email protected]ffbb2212013-06-02 23:47:59498 }
499
500 {
501 scoped_ptr<TextureLayerImpl> impl_layer =
502 TextureLayerImpl::Create(host_impl_.active_tree(), 1, true);
[email protected]0ec335c42013-07-04 06:17:08503 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]ffbb2212013-06-02 23:47:59513 }
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]0ec335c42013-07-04 06:17:08521 EXPECT_TRUE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE));
[email protected]ffbb2212013-06-02 23:47:59522 }
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]0ec335c42013-07-04 06:17:08528 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]ffbb2212013-06-02 23:47:59568 }
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]0ec335c42013-07-04 06:17:08575 EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_RESOURCELESS_SOFTWARE));
[email protected]ffbb2212013-06-02 23:47:59576 }
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]0ec335c42013-07-04 06:17:08584 EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_RESOURCELESS_SOFTWARE));
[email protected]ffbb2212013-06-02 23:47:59585 }
586}
587
[email protected]28571b042013-03-14 07:59:15588TEST_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]de44a152013-01-08 15:28:46593
[email protected]ed511b8d2013-03-25 03:29:29594 scoped_ptr<LayerImpl> active_layer(
[email protected]28571b042013-03-14 07:59:15595 pending_layer->CreateLayerImpl(host_impl_.active_tree()));
[email protected]ed511b8d2013-03-25 03:29:29596 ASSERT_TRUE(active_layer);
[email protected]de44a152013-01-08 15:28:46597
[email protected]1a37a0752013-03-17 21:25:49598 pending_layer->SetTextureMailbox(test_data_.mailbox1_);
[email protected]421e84f2013-02-22 03:27:15599
[email protected]28571b042013-03-14 07:59:15600 // Test multiple commits without an activation.
601 EXPECT_CALL(test_data_.mock_callback_,
[email protected]7ba3ca72013-04-11 06:37:25602 Release(test_data_.mailbox_name1_,
603 test_data_.sync_point1_,
604 false))
[email protected]28571b042013-03-14 07:59:15605 .Times(1);
[email protected]1a37a0752013-03-17 21:25:49606 pending_layer->SetTextureMailbox(test_data_.mailbox2_);
[email protected]28571b042013-03-14 07:59:15607 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]421e84f2013-02-22 03:27:15608
[email protected]28571b042013-03-14 07:59:15609 // Test callback after activation.
[email protected]ed511b8d2013-03-25 03:29:29610 pending_layer->PushPropertiesTo(active_layer.get());
611 active_layer->DidBecomeActive();
[email protected]421e84f2013-02-22 03:27:15612
[email protected]7ba3ca72013-04-11 06:37:25613 EXPECT_CALL(test_data_.mock_callback_, Release(_, _, _)).Times(0);
[email protected]1a37a0752013-03-17 21:25:49614 pending_layer->SetTextureMailbox(test_data_.mailbox1_);
[email protected]28571b042013-03-14 07:59:15615 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]421e84f2013-02-22 03:27:15616
[email protected]7ba3ca72013-04-11 06:37:25617 EXPECT_CALL(test_data_.mock_callback_,
618 Release(test_data_.mailbox_name2_, _, false))
[email protected]28571b042013-03-14 07:59:15619 .Times(1);
[email protected]ed511b8d2013-03-25 03:29:29620 pending_layer->PushPropertiesTo(active_layer.get());
621 active_layer->DidBecomeActive();
[email protected]28571b042013-03-14 07:59:15622 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]de44a152013-01-08 15:28:46623
[email protected]28571b042013-03-14 07:59:15624 // Test resetting the mailbox.
[email protected]7ba3ca72013-04-11 06:37:25625 EXPECT_CALL(test_data_.mock_callback_,
626 Release(test_data_.mailbox_name1_, _, false))
[email protected]28571b042013-03-14 07:59:15627 .Times(1);
[email protected]1a37a0752013-03-17 21:25:49628 pending_layer->SetTextureMailbox(TextureMailbox());
[email protected]ed511b8d2013-03-25 03:29:29629 pending_layer->PushPropertiesTo(active_layer.get());
630 active_layer->DidBecomeActive();
[email protected]28571b042013-03-14 07:59:15631 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]de44a152013-01-08 15:28:46632
[email protected]28571b042013-03-14 07:59:15633 // Test destructor.
634 EXPECT_CALL(test_data_.mock_callback_,
[email protected]7ba3ca72013-04-11 06:37:25635 Release(test_data_.mailbox_name1_,
636 test_data_.sync_point1_,
637 false))
[email protected]28571b042013-03-14 07:59:15638 .Times(1);
[email protected]1a37a0752013-03-17 21:25:49639 pending_layer->SetTextureMailbox(test_data_.mailbox1_);
[email protected]de44a152013-01-08 15:28:46640}
641
[email protected]28571b042013-03-14 07:59:15642TEST_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]de44a152013-01-08 15:28:46647
[email protected]7ba3ca72013-04-11 06:37:25648 EXPECT_CALL(test_data_.mock_callback_,
649 Release(test_data_.mailbox_name1_, _, false))
[email protected]28571b042013-03-14 07:59:15650 .Times(1);
[email protected]1a37a0752013-03-17 21:25:49651 impl_layer->SetTextureMailbox(test_data_.mailbox1_);
[email protected]ffbb2212013-06-02 23:47:59652 impl_layer->DidBecomeActive();
653 EXPECT_TRUE(impl_layer->WillDraw(
654 DRAW_MODE_HARDWARE, host_impl_.active_tree()->resource_provider()));
[email protected]28571b042013-03-14 07:59:15655 impl_layer->DidDraw(host_impl_.active_tree()->resource_provider());
[email protected]1a37a0752013-03-17 21:25:49656 impl_layer->SetTextureMailbox(TextureMailbox());
[email protected]de44a152013-01-08 15:28:46657}
658
[email protected]28571b042013-03-14 07:59:15659TEST_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]de44a152013-01-08 15:28:46664
[email protected]28571b042013-03-14 07:59:15665 // 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]7ba3ca72013-04-11 06:37:25671 EXPECT_CALL(test_data_.mock_callback_, Release(_, _, _)).Times(0);
[email protected]28571b042013-03-14 07:59:15672 provider->DeleteResource(id);
673 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]7ba3ca72013-04-11 06:37:25674 EXPECT_CALL(test_data_.mock_callback_,
675 Release(test_data_.mailbox_name1_, _, false))
[email protected]28571b042013-03-14 07:59:15676 .Times(1);
677 provider->ReceiveFromParent(list);
[email protected]de44a152013-01-08 15:28:46678}
679
[email protected]97d519fb2013-03-29 02:27:54680// 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]7ba3ca72013-04-11 06:37:25682class TextureLayerClientTest
683 : public LayerTreeTest,
684 public TextureLayerClient {
[email protected]97d519fb2013-03-29 02:27:54685 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]171cbb32013-07-11 03:51:19702 virtual unsigned PrepareTexture() OVERRIDE {
[email protected]97d519fb2013-03-29 02:27:54703 return texture_;
704 }
705
[email protected]d5d9bc52013-04-01 23:16:39706 virtual WebKit::WebGraphicsContext3D* Context3d() OVERRIDE {
[email protected]97d519fb2013-03-29 02:27:54707 return context_;
708 }
709
[email protected]2541d1a2013-07-10 07:33:27710 virtual bool PrepareTextureMailbox(
711 cc::TextureMailbox* mailbox, bool use_shared_memory) OVERRIDE {
[email protected]e8e4ae232013-04-12 00:26:01712 return false;
713 }
714
[email protected]97d519fb2013-03-29 02:27:54715 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]4145d172013-05-10 16:54:36794// The TextureLayerClient does not use mailboxes, so can't use a delegating
795// renderer.
796SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TextureLayerClientTest);
[email protected]97d519fb2013-03-29 02:27:54797
[email protected]0bf5a202013-07-10 14:50:54798// Test recovering from a lost context.
799class 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]171cbb32013-07-11 03:51:19816 virtual unsigned PrepareTexture() OVERRIDE {
[email protected]0bf5a202013-07-10 14:50:54817 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
877SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TextureLayerLostContextTest);
878
[email protected]ba565742012-11-10 09:29:48879} // namespace
880} // namespace cc