blob: 6b78a71b0b513a89c2884a8161e9ce23fb34a427 [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]b04264f92013-09-13 23:37:297#include <algorithm>
[email protected]de44a152013-01-08 15:28:468#include <string>
9
[email protected]b04264f92013-09-13 23:37:2910#include "base/bind.h"
[email protected]de44a152013-01-08 15:28:4611#include "base/callback.h"
[email protected]9794fb32013-08-29 09:49:5912#include "base/synchronization/waitable_event.h"
[email protected]74b43cc2013-08-30 06:29:2713#include "base/threading/thread.h"
14#include "base/time/time.h"
[email protected]b04264f92013-09-13 23:37:2915#include "cc/layers/solid_color_layer.h"
[email protected]97d519fb2013-03-29 02:27:5416#include "cc/layers/texture_layer_client.h"
[email protected]cc3cfaa2013-03-18 09:05:5217#include "cc/layers/texture_layer_impl.h"
[email protected]b04264f92013-09-13 23:37:2918#include "cc/output/compositor_frame_ack.h"
19#include "cc/output/context_provider.h"
[email protected]e00bab022013-08-19 00:42:4520#include "cc/resources/returned_resource.h"
[email protected]586d51ed2012-12-07 20:31:4521#include "cc/test/fake_impl_proxy.h"
[email protected]101441ce2012-10-16 01:45:0322#include "cc/test/fake_layer_tree_host_client.h"
[email protected]586d51ed2012-12-07 20:31:4523#include "cc/test/fake_layer_tree_host_impl.h"
[email protected]199b715e2013-08-13 05:18:3424#include "cc/test/fake_output_surface.h"
[email protected]06d68d02013-04-19 18:46:2125#include "cc/test/layer_test_common.h"
[email protected]e216fef02013-03-20 22:56:1026#include "cc/test/layer_tree_test.h"
[email protected]c2610b9f2013-10-31 06:54:5927#include "cc/test/test_web_graphics_context_3d.h"
[email protected]9794fb32013-08-29 09:49:5928#include "cc/trees/blocking_task_runner.h"
[email protected]556fd292013-03-18 08:03:0429#include "cc/trees/layer_tree_host.h"
30#include "cc/trees/layer_tree_impl.h"
31#include "cc/trees/single_thread_proxy.h"
[email protected]0bf5a202013-07-10 14:50:5432#include "gpu/GLES2/gl2extchromium.h"
[email protected]7f0c53db2012-10-02 00:23:1833#include "testing/gmock/include/gmock/gmock.h"
34#include "testing/gtest/include/gtest/gtest.h"
[email protected]c0dd24c2012-08-30 23:25:2735
[email protected]c0dd24c2012-08-30 23:25:2736using ::testing::Mock;
37using ::testing::_;
38using ::testing::AtLeast;
39using ::testing::AnyNumber;
40
[email protected]ba565742012-11-10 09:29:4841namespace cc {
[email protected]c0dd24c2012-08-30 23:25:2742namespace {
43
[email protected]9ee234342014-01-18 02:42:2644gpu::Mailbox MailboxFromString(const std::string& string) {
45 gpu::Mailbox mailbox;
46 mailbox.SetName(reinterpret_cast<const int8*>(string.data()));
47 return mailbox;
48}
49
[email protected]408b5e22013-03-19 09:48:0950class MockLayerTreeHost : public LayerTreeHost {
[email protected]28571b042013-03-14 07:59:1551 public:
[email protected]943528e2013-11-07 05:01:3252 explicit MockLayerTreeHost(FakeLayerTreeHostClient* client)
[email protected]a7f35682013-10-22 23:05:5753 : LayerTreeHost(client, NULL, LayerTreeSettings()) {
[email protected]943528e2013-11-07 05:01:3254 InitializeSingleThreaded(client);
[email protected]28571b042013-03-14 07:59:1555 }
[email protected]c0dd24c2012-08-30 23:25:2756
[email protected]28571b042013-03-14 07:59:1557 MOCK_METHOD0(AcquireLayerTextures, void());
58 MOCK_METHOD0(SetNeedsCommit, void());
[email protected]3519b872013-07-30 07:17:5059 MOCK_METHOD0(SetNeedsUpdateLayers, void());
[email protected]aeeb3372013-11-05 14:05:5460 MOCK_METHOD0(StartRateLimiter, void());
61 MOCK_METHOD0(StopRateLimiter, void());
[email protected]c0dd24c2012-08-30 23:25:2762};
63
[email protected]31d4df82013-07-18 10:17:2264class TextureLayerTest : public testing::Test {
65 public:
66 TextureLayerTest()
67 : fake_client_(
68 FakeLayerTreeHostClient(FakeLayerTreeHostClient::DIRECT_3D)),
69 host_impl_(&proxy_) {}
70
71 protected:
72 virtual void SetUp() {
73 layer_tree_host_.reset(new MockLayerTreeHost(&fake_client_));
74 }
75
76 virtual void TearDown() {
77 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
78 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber());
79 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
80
81 layer_tree_host_->SetRootLayer(NULL);
82 layer_tree_host_.reset();
83 }
84
85 scoped_ptr<MockLayerTreeHost> layer_tree_host_;
86 FakeImplProxy proxy_;
87 FakeLayerTreeHostClient fake_client_;
88 FakeLayerTreeHostImpl host_impl_;
89};
90
91TEST_F(TextureLayerTest, SyncImplWhenChangingTextureId) {
92 scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL);
93 ASSERT_TRUE(test_layer.get());
94
95 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber());
96 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
97 layer_tree_host_->SetRootLayer(test_layer);
98 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
99 EXPECT_EQ(test_layer->layer_tree_host(), layer_tree_host_.get());
100
101 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
102 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
103 test_layer->SetTextureId(1);
104 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
105
106 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1));
107 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
108 test_layer->SetTextureId(2);
109 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
110
111 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1));
112 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
113 test_layer->SetTextureId(0);
114 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
115}
116
117TEST_F(TextureLayerTest, SyncImplWhenDrawing) {
118 gfx::RectF dirty_rect(0.f, 0.f, 1.f, 1.f);
119
120 scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL);
121 ASSERT_TRUE(test_layer.get());
122 scoped_ptr<TextureLayerImpl> impl_layer;
123 impl_layer = TextureLayerImpl::Create(host_impl_.active_tree(), 1, false);
124 ASSERT_TRUE(impl_layer);
125
126 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber());
127 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
128 layer_tree_host_->SetRootLayer(test_layer);
129 test_layer->SetTextureId(1);
130 test_layer->SetIsDrawable(true);
131 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
132 EXPECT_EQ(test_layer->layer_tree_host(), layer_tree_host_.get());
133
134 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(1);
135 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
136 test_layer->WillModifyTexture();
137 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
138
139 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
[email protected]3519b872013-07-30 07:17:50140 EXPECT_CALL(*layer_tree_host_, SetNeedsUpdateLayers()).Times(1);
[email protected]31d4df82013-07-18 10:17:22141 test_layer->SetNeedsDisplayRect(dirty_rect);
142 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
143
144 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
145 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(1);
146 test_layer->PushPropertiesTo(impl_layer.get()); // fake commit
147 test_layer->SetIsDrawable(false);
148 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
149
150 // Verify that non-drawable layers don't signal the compositor,
151 // except for the first draw after last commit, which must acquire
152 // the texture.
153 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(1);
154 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
155 test_layer->WillModifyTexture();
156 test_layer->SetNeedsDisplayRect(dirty_rect);
157 test_layer->PushPropertiesTo(impl_layer.get()); // fake commit
158 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
159
160 // Second draw with layer in non-drawable state: no texture
161 // acquisition.
162 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
163 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
164 test_layer->WillModifyTexture();
165 test_layer->SetNeedsDisplayRect(dirty_rect);
166 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
167}
168
169TEST_F(TextureLayerTest, SyncImplWhenRemovingFromTree) {
170 scoped_refptr<Layer> root_layer = Layer::Create();
171 ASSERT_TRUE(root_layer.get());
172 scoped_refptr<Layer> child_layer = Layer::Create();
173 ASSERT_TRUE(child_layer.get());
174 root_layer->AddChild(child_layer);
175 scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL);
176 ASSERT_TRUE(test_layer.get());
177 test_layer->SetTextureId(0);
178 child_layer->AddChild(test_layer);
179
180 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber());
181 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
182 layer_tree_host_->SetRootLayer(root_layer);
183 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
184
185 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
186 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
187 test_layer->RemoveFromParent();
188 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
189
190 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
191 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
192 child_layer->AddChild(test_layer);
193 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
194
195 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
196 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
197 test_layer->SetTextureId(1);
198 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
199
200 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1));
201 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
202 test_layer->RemoveFromParent();
203 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
204}
205
206TEST_F(TextureLayerTest, CheckPropertyChangeCausesCorrectBehavior) {
207 scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL);
[email protected]80d42bd2013-08-30 19:13:45208 EXPECT_SET_NEEDS_COMMIT(1, layer_tree_host_->SetRootLayer(test_layer));
[email protected]31d4df82013-07-18 10:17:22209
210 // Test properties that should call SetNeedsCommit. All properties need to
211 // be set to new values in order for SetNeedsCommit to be called.
212 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFlipped(false));
213 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetUV(
214 gfx::PointF(0.25f, 0.25f), gfx::PointF(0.75f, 0.75f)));
215 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetVertexOpacity(
216 0.5f, 0.5f, 0.5f, 0.5f));
217 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetPremultipliedAlpha(false));
218 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBlendBackgroundColor(true));
219 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTextureId(1));
220
221 // Calling SetTextureId can call AcquireLayerTextures.
222 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber());
223}
224
[email protected]1c10e232013-07-31 12:35:43225TEST_F(TextureLayerTest, VisibleContentOpaqueRegion) {
226 const gfx::Size layer_bounds(100, 100);
227 const gfx::Rect layer_rect(layer_bounds);
228 const Region layer_region(layer_rect);
229
230 scoped_refptr<TextureLayer> layer = TextureLayer::Create(NULL);
231 layer->SetBounds(layer_bounds);
232 layer->draw_properties().visible_content_rect = layer_rect;
233 layer->SetBlendBackgroundColor(true);
234
235 // Verify initial conditions.
236 EXPECT_FALSE(layer->contents_opaque());
237 EXPECT_EQ(0u, layer->background_color());
238 EXPECT_EQ(Region().ToString(),
239 layer->VisibleContentOpaqueRegion().ToString());
240
241 // Opaque background.
242 layer->SetBackgroundColor(SK_ColorWHITE);
243 EXPECT_EQ(layer_region.ToString(),
244 layer->VisibleContentOpaqueRegion().ToString());
245
246 // Transparent background.
247 layer->SetBackgroundColor(SkColorSetARGB(100, 255, 255, 255));
248 EXPECT_EQ(Region().ToString(),
249 layer->VisibleContentOpaqueRegion().ToString());
250}
251
[email protected]2f529812013-07-12 01:58:39252class FakeTextureLayerClient : public TextureLayerClient {
253 public:
[email protected]f5931d42013-11-06 19:44:57254 FakeTextureLayerClient() {}
[email protected]2f529812013-07-12 01:58:39255
256 virtual unsigned PrepareTexture() OVERRIDE {
[email protected]31d4df82013-07-18 10:17:22257 return 0;
[email protected]2f529812013-07-12 01:58:39258 }
259
[email protected]9260757f2013-09-17 01:24:16260 virtual bool PrepareTextureMailbox(
261 TextureMailbox* mailbox,
262 scoped_ptr<SingleReleaseCallback>* release_callback,
263 bool use_shared_memory) OVERRIDE {
[email protected]31d4df82013-07-18 10:17:22264 *mailbox = TextureMailbox();
[email protected]9260757f2013-09-17 01:24:16265 *release_callback = scoped_ptr<SingleReleaseCallback>();
[email protected]2f529812013-07-12 01:58:39266 return true;
267 }
268
269 private:
[email protected]2f529812013-07-12 01:58:39270 DISALLOW_COPY_AND_ASSIGN(FakeTextureLayerClient);
271};
272
[email protected]31d4df82013-07-18 10:17:22273TEST_F(TextureLayerTest, RateLimiter) {
274 FakeTextureLayerClient client;
275 scoped_refptr<TextureLayer> test_layer = TextureLayer::CreateForMailbox(
276 &client);
277 test_layer->SetIsDrawable(true);
278 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
279 layer_tree_host_->SetRootLayer(test_layer);
280
281 // Don't rate limit until we invalidate.
[email protected]aeeb3372013-11-05 14:05:54282 EXPECT_CALL(*layer_tree_host_, StartRateLimiter()).Times(0);
[email protected]31d4df82013-07-18 10:17:22283 test_layer->SetRateLimitContext(true);
284 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
285
286 // Do rate limit after we invalidate.
[email protected]aeeb3372013-11-05 14:05:54287 EXPECT_CALL(*layer_tree_host_, StartRateLimiter());
[email protected]31d4df82013-07-18 10:17:22288 test_layer->SetNeedsDisplay();
289 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
290
291 // Stop rate limiter when we don't want it any more.
[email protected]aeeb3372013-11-05 14:05:54292 EXPECT_CALL(*layer_tree_host_, StopRateLimiter());
[email protected]31d4df82013-07-18 10:17:22293 test_layer->SetRateLimitContext(false);
294 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
295
296 // Or we clear the client.
297 test_layer->SetRateLimitContext(true);
[email protected]aeeb3372013-11-05 14:05:54298 EXPECT_CALL(*layer_tree_host_, StopRateLimiter());
[email protected]31d4df82013-07-18 10:17:22299 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
300 test_layer->ClearClient();
301 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
302
303 // Reset to a layer with a client, that started the rate limiter.
304 test_layer = TextureLayer::CreateForMailbox(
305 &client);
306 test_layer->SetIsDrawable(true);
307 test_layer->SetRateLimitContext(true);
308 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
309 layer_tree_host_->SetRootLayer(test_layer);
[email protected]aeeb3372013-11-05 14:05:54310 EXPECT_CALL(*layer_tree_host_, StartRateLimiter()).Times(0);
[email protected]31d4df82013-07-18 10:17:22311 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]aeeb3372013-11-05 14:05:54312 EXPECT_CALL(*layer_tree_host_, StartRateLimiter());
[email protected]31d4df82013-07-18 10:17:22313 test_layer->SetNeedsDisplay();
314 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
315
316 // Stop rate limiter when we're removed from the tree.
[email protected]aeeb3372013-11-05 14:05:54317 EXPECT_CALL(*layer_tree_host_, StopRateLimiter());
[email protected]31d4df82013-07-18 10:17:22318 layer_tree_host_->SetRootLayer(NULL);
319 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
320}
321
[email protected]de44a152013-01-08 15:28:46322class MockMailboxCallback {
[email protected]28571b042013-03-14 07:59:15323 public:
[email protected]9ee234342014-01-18 02:42:26324 MOCK_METHOD3(Release,
325 void(const std::string& mailbox,
326 uint32 sync_point,
327 bool lost_resource));
328 MOCK_METHOD3(Release2,
329 void(base::SharedMemory* shared_memory,
330 uint32 sync_point,
331 bool lost_resource));
[email protected]de44a152013-01-08 15:28:46332};
333
334struct CommonMailboxObjects {
[email protected]28571b042013-03-14 07:59:15335 CommonMailboxObjects()
336 : mailbox_name1_(64, '1'),
337 mailbox_name2_(64, '2'),
338 sync_point1_(1),
[email protected]42f40a52013-06-08 04:38:51339 sync_point2_(2),
340 shared_memory_(new base::SharedMemory) {
[email protected]28571b042013-03-14 07:59:15341 release_mailbox1_ = base::Bind(&MockMailboxCallback::Release,
342 base::Unretained(&mock_callback_),
343 mailbox_name1_);
344 release_mailbox2_ = base::Bind(&MockMailboxCallback::Release,
345 base::Unretained(&mock_callback_),
346 mailbox_name2_);
[email protected]9ee234342014-01-18 02:42:26347 const uint32 arbitrary_target1 = 1;
348 const uint32 arbitrary_target2 = 2;
349 mailbox1_ = TextureMailbox(
350 MailboxFromString(mailbox_name1_), arbitrary_target1, sync_point1_);
351 mailbox2_ = TextureMailbox(
352 MailboxFromString(mailbox_name2_), arbitrary_target2, sync_point2_);
[email protected]42f40a52013-06-08 04:38:51353 gfx::Size size(128, 128);
354 EXPECT_TRUE(shared_memory_->CreateAndMapAnonymous(4 * size.GetArea()));
355 release_mailbox3_ = base::Bind(&MockMailboxCallback::Release2,
356 base::Unretained(&mock_callback_),
357 shared_memory_.get());
[email protected]9260757f2013-09-17 01:24:16358 mailbox3_ = TextureMailbox(shared_memory_.get(), size);
[email protected]28571b042013-03-14 07:59:15359 }
[email protected]de44a152013-01-08 15:28:46360
[email protected]28571b042013-03-14 07:59:15361 std::string mailbox_name1_;
362 std::string mailbox_name2_;
363 MockMailboxCallback mock_callback_;
[email protected]9260757f2013-09-17 01:24:16364 ReleaseCallback release_mailbox1_;
365 ReleaseCallback release_mailbox2_;
366 ReleaseCallback release_mailbox3_;
[email protected]28571b042013-03-14 07:59:15367 TextureMailbox mailbox1_;
368 TextureMailbox mailbox2_;
[email protected]42f40a52013-06-08 04:38:51369 TextureMailbox mailbox3_;
[email protected]9ee234342014-01-18 02:42:26370 uint32 sync_point1_;
371 uint32 sync_point2_;
[email protected]42f40a52013-06-08 04:38:51372 scoped_ptr<base::SharedMemory> shared_memory_;
[email protected]de44a152013-01-08 15:28:46373};
374
[email protected]9ee234342014-01-18 02:42:26375class TestMailboxHolder : public TextureLayer::TextureMailboxHolder {
[email protected]9794fb32013-08-29 09:49:59376 public:
[email protected]9ee234342014-01-18 02:42:26377 using TextureLayer::TextureMailboxHolder::Create;
[email protected]9794fb32013-08-29 09:49:59378
379 protected:
380 virtual ~TestMailboxHolder() {}
381};
382
[email protected]de44a152013-01-08 15:28:46383class TextureLayerWithMailboxTest : public TextureLayerTest {
[email protected]28571b042013-03-14 07:59:15384 protected:
385 virtual void TearDown() {
386 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
387 EXPECT_CALL(test_data_.mock_callback_,
388 Release(test_data_.mailbox_name1_,
[email protected]7ba3ca72013-04-11 06:37:25389 test_data_.sync_point1_,
390 false)).Times(1);
[email protected]28571b042013-03-14 07:59:15391 TextureLayerTest::TearDown();
392 }
[email protected]de44a152013-01-08 15:28:46393
[email protected]28571b042013-03-14 07:59:15394 CommonMailboxObjects test_data_;
[email protected]de44a152013-01-08 15:28:46395};
396
[email protected]28571b042013-03-14 07:59:15397TEST_F(TextureLayerWithMailboxTest, ReplaceMailboxOnMainThreadBeforeCommit) {
[email protected]e8e4ae232013-04-12 00:26:01398 scoped_refptr<TextureLayer> test_layer = TextureLayer::CreateForMailbox(NULL);
[email protected]22898ed2013-06-01 04:52:30399 ASSERT_TRUE(test_layer.get());
[email protected]de44a152013-01-08 15:28:46400
[email protected]28571b042013-03-14 07:59:15401 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
402 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
403 layer_tree_host_->SetRootLayer(test_layer);
404 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]de44a152013-01-08 15:28:46405
[email protected]28571b042013-03-14 07:59:15406 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
407 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
[email protected]9260757f2013-09-17 01:24:16408 test_layer->SetTextureMailbox(
409 test_data_.mailbox1_,
410 SingleReleaseCallback::Create(test_data_.release_mailbox1_));
[email protected]28571b042013-03-14 07:59:15411 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]de44a152013-01-08 15:28:46412
[email protected]28571b042013-03-14 07:59:15413 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
414 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
415 EXPECT_CALL(test_data_.mock_callback_,
[email protected]7ba3ca72013-04-11 06:37:25416 Release(test_data_.mailbox_name1_,
417 test_data_.sync_point1_,
418 false))
[email protected]28571b042013-03-14 07:59:15419 .Times(1);
[email protected]9260757f2013-09-17 01:24:16420 test_layer->SetTextureMailbox(
421 test_data_.mailbox2_,
422 SingleReleaseCallback::Create(test_data_.release_mailbox2_));
[email protected]28571b042013-03-14 07:59:15423 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
424 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]de44a152013-01-08 15:28:46425
[email protected]28571b042013-03-14 07:59:15426 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
427 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
428 EXPECT_CALL(test_data_.mock_callback_,
[email protected]7ba3ca72013-04-11 06:37:25429 Release(test_data_.mailbox_name2_,
430 test_data_.sync_point2_,
431 false))
[email protected]28571b042013-03-14 07:59:15432 .Times(1);
[email protected]9260757f2013-09-17 01:24:16433 test_layer->SetTextureMailbox(TextureMailbox(),
434 scoped_ptr<SingleReleaseCallback>());
[email protected]28571b042013-03-14 07:59:15435 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
436 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]de44a152013-01-08 15:28:46437
[email protected]80d42bd2013-08-30 19:13:45438 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
439 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
[email protected]9260757f2013-09-17 01:24:16440 test_layer->SetTextureMailbox(
441 test_data_.mailbox3_,
442 SingleReleaseCallback::Create(test_data_.release_mailbox3_));
[email protected]42f40a52013-06-08 04:38:51443 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
444 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
445
446 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
447 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
448 EXPECT_CALL(test_data_.mock_callback_,
449 Release2(test_data_.shared_memory_.get(),
450 0, false))
451 .Times(1);
[email protected]9260757f2013-09-17 01:24:16452 test_layer->SetTextureMailbox(TextureMailbox(),
453 scoped_ptr<SingleReleaseCallback>());
[email protected]42f40a52013-06-08 04:38:51454 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
455 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
456
[email protected]28571b042013-03-14 07:59:15457 // Test destructor.
458 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
[email protected]9260757f2013-09-17 01:24:16459 test_layer->SetTextureMailbox(
460 test_data_.mailbox1_,
461 SingleReleaseCallback::Create(test_data_.release_mailbox1_));
[email protected]de44a152013-01-08 15:28:46462}
463
[email protected]9794fb32013-08-29 09:49:59464class TextureLayerMailboxHolderTest : public TextureLayerTest {
465 public:
466 TextureLayerMailboxHolderTest()
467 : main_thread_("MAIN") {
468 main_thread_.Start();
469 }
470
471 void Wait(const base::Thread& thread) {
472 bool manual_reset = false;
473 bool initially_signaled = false;
474 base::WaitableEvent event(manual_reset, initially_signaled);
475 thread.message_loop()->PostTask(
476 FROM_HERE,
477 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event)));
478 event.Wait();
479 }
480
481 void CreateMainRef() {
482 main_ref_ = TestMailboxHolder::Create(
[email protected]9260757f2013-09-17 01:24:16483 test_data_.mailbox1_,
484 SingleReleaseCallback::Create(test_data_.release_mailbox1_)).Pass();
[email protected]9794fb32013-08-29 09:49:59485 }
486
487 void ReleaseMainRef() {
488 main_ref_.reset();
489 }
490
[email protected]9260757f2013-09-17 01:24:16491 void CreateImplRef(scoped_ptr<SingleReleaseCallback>* impl_ref) {
[email protected]9794fb32013-08-29 09:49:59492 *impl_ref = main_ref_->holder()->GetCallbackForImplThread();
493 }
494
495 void CapturePostTasksAndWait(base::WaitableEvent* begin_capture,
496 base::WaitableEvent* wait_for_capture,
497 base::WaitableEvent* stop_capture) {
498 begin_capture->Wait();
499 BlockingTaskRunner::CapturePostTasks capture;
500 wait_for_capture->Signal();
501 stop_capture->Wait();
502 }
503
504 protected:
505 scoped_ptr<TestMailboxHolder::MainThreadReference>
506 main_ref_;
507 base::Thread main_thread_;
508 CommonMailboxObjects test_data_;
509};
510
511TEST_F(TextureLayerMailboxHolderTest, TwoCompositors_BothReleaseThenMain) {
512 scoped_refptr<TextureLayer> test_layer = TextureLayer::CreateForMailbox(NULL);
513 ASSERT_TRUE(test_layer.get());
514
515 main_thread_.message_loop()->PostTask(
516 FROM_HERE,
517 base::Bind(&TextureLayerMailboxHolderTest::CreateMainRef,
518 base::Unretained(this)));
519
520 Wait(main_thread_);
521
522 // The texture layer is attached to compositor1, and passes a reference to its
523 // impl tree.
[email protected]9260757f2013-09-17 01:24:16524 scoped_ptr<SingleReleaseCallback> compositor1;
[email protected]9794fb32013-08-29 09:49:59525 main_thread_.message_loop()->PostTask(
526 FROM_HERE,
527 base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef,
528 base::Unretained(this),
529 &compositor1));
530
531 // Then the texture layer is removed and attached to compositor2, and passes a
532 // reference to its impl tree.
[email protected]9260757f2013-09-17 01:24:16533 scoped_ptr<SingleReleaseCallback> compositor2;
[email protected]9794fb32013-08-29 09:49:59534 main_thread_.message_loop()->PostTask(
535 FROM_HERE,
536 base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef,
537 base::Unretained(this),
538 &compositor2));
539
540 Wait(main_thread_);
541 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
542
543 // The compositors both destroy their impl trees before the main thread layer
544 // is destroyed.
[email protected]9260757f2013-09-17 01:24:16545 compositor1->Run(100, false);
546 compositor2->Run(200, false);
[email protected]9794fb32013-08-29 09:49:59547
548 Wait(main_thread_);
549
550 EXPECT_CALL(test_data_.mock_callback_, Release(_, _, _)).Times(0);
551 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
552
553 // The main thread ref is the last one, so the mailbox is released back to the
554 // embedder, with the last sync point provided by the impl trees.
555 EXPECT_CALL(test_data_.mock_callback_,
556 Release(test_data_.mailbox_name1_, 200, false)).Times(1);
557
558 main_thread_.message_loop()->PostTask(
559 FROM_HERE,
560 base::Bind(&TextureLayerMailboxHolderTest::ReleaseMainRef,
561 base::Unretained(this)));
562 Wait(main_thread_);
563 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
564}
565
566TEST_F(TextureLayerMailboxHolderTest, TwoCompositors_MainReleaseBetween) {
567 scoped_refptr<TextureLayer> test_layer = TextureLayer::CreateForMailbox(NULL);
568 ASSERT_TRUE(test_layer.get());
569
570 main_thread_.message_loop()->PostTask(
571 FROM_HERE,
572 base::Bind(&TextureLayerMailboxHolderTest::CreateMainRef,
573 base::Unretained(this)));
574
575 Wait(main_thread_);
576
577 // The texture layer is attached to compositor1, and passes a reference to its
578 // impl tree.
[email protected]9260757f2013-09-17 01:24:16579 scoped_ptr<SingleReleaseCallback> compositor1;
[email protected]9794fb32013-08-29 09:49:59580 main_thread_.message_loop()->PostTask(
581 FROM_HERE,
582 base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef,
583 base::Unretained(this),
584 &compositor1));
585
586 // Then the texture layer is removed and attached to compositor2, and passes a
587 // reference to its impl tree.
[email protected]9260757f2013-09-17 01:24:16588 scoped_ptr<SingleReleaseCallback> compositor2;
[email protected]9794fb32013-08-29 09:49:59589 main_thread_.message_loop()->PostTask(
590 FROM_HERE,
591 base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef,
592 base::Unretained(this),
593 &compositor2));
594
595 Wait(main_thread_);
596 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
597
598 // One compositor destroys their impl tree.
[email protected]9260757f2013-09-17 01:24:16599 compositor1->Run(100, false);
[email protected]9794fb32013-08-29 09:49:59600
601 // Then the main thread reference is destroyed.
602 main_thread_.message_loop()->PostTask(
603 FROM_HERE,
604 base::Bind(&TextureLayerMailboxHolderTest::ReleaseMainRef,
605 base::Unretained(this)));
606
607 Wait(main_thread_);
608
609 EXPECT_CALL(test_data_.mock_callback_, Release(_, _, _)).Times(0);
610 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
611
612 // The second impl reference is destroyed last, causing the mailbox to be
613 // released back to the embedder with the last sync point from the impl tree.
614 EXPECT_CALL(test_data_.mock_callback_,
615 Release(test_data_.mailbox_name1_, 200, true)).Times(1);
616
[email protected]9260757f2013-09-17 01:24:16617 compositor2->Run(200, true);
[email protected]9794fb32013-08-29 09:49:59618 Wait(main_thread_);
619 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
620}
621
622TEST_F(TextureLayerMailboxHolderTest, TwoCompositors_MainReleasedFirst) {
623 scoped_refptr<TextureLayer> test_layer = TextureLayer::CreateForMailbox(NULL);
624 ASSERT_TRUE(test_layer.get());
625
626 main_thread_.message_loop()->PostTask(
627 FROM_HERE,
628 base::Bind(&TextureLayerMailboxHolderTest::CreateMainRef,
629 base::Unretained(this)));
630
631 Wait(main_thread_);
632
633 // The texture layer is attached to compositor1, and passes a reference to its
634 // impl tree.
[email protected]9260757f2013-09-17 01:24:16635 scoped_ptr<SingleReleaseCallback> compositor1;
[email protected]9794fb32013-08-29 09:49:59636 main_thread_.message_loop()->PostTask(
637 FROM_HERE,
638 base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef,
639 base::Unretained(this),
640 &compositor1));
641
642 // Then the texture layer is removed and attached to compositor2, and passes a
643 // reference to its impl tree.
[email protected]9260757f2013-09-17 01:24:16644 scoped_ptr<SingleReleaseCallback> compositor2;
[email protected]9794fb32013-08-29 09:49:59645 main_thread_.message_loop()->PostTask(
646 FROM_HERE,
647 base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef,
648 base::Unretained(this),
649 &compositor2));
650
651 Wait(main_thread_);
652 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
653
654 // The main thread reference is destroyed first.
655 main_thread_.message_loop()->PostTask(
656 FROM_HERE,
657 base::Bind(&TextureLayerMailboxHolderTest::ReleaseMainRef,
658 base::Unretained(this)));
659
660 // One compositor destroys their impl tree.
[email protected]9260757f2013-09-17 01:24:16661 compositor2->Run(200, false);
[email protected]9794fb32013-08-29 09:49:59662
663 Wait(main_thread_);
664
665 EXPECT_CALL(test_data_.mock_callback_, Release(_, _, _)).Times(0);
666 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
667
668 // The second impl reference is destroyed last, causing the mailbox to be
669 // released back to the embedder with the last sync point from the impl tree.
670 EXPECT_CALL(test_data_.mock_callback_,
671 Release(test_data_.mailbox_name1_, 100, true)).Times(1);
672
[email protected]9260757f2013-09-17 01:24:16673 compositor1->Run(100, true);
[email protected]9794fb32013-08-29 09:49:59674 Wait(main_thread_);
675 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
676}
677
678TEST_F(TextureLayerMailboxHolderTest, TwoCompositors_SecondImplRefShortcut) {
679 scoped_refptr<TextureLayer> test_layer = TextureLayer::CreateForMailbox(NULL);
680 ASSERT_TRUE(test_layer.get());
681
682 main_thread_.message_loop()->PostTask(
683 FROM_HERE,
684 base::Bind(&TextureLayerMailboxHolderTest::CreateMainRef,
685 base::Unretained(this)));
686
687 Wait(main_thread_);
688
689 // The texture layer is attached to compositor1, and passes a reference to its
690 // impl tree.
[email protected]9260757f2013-09-17 01:24:16691 scoped_ptr<SingleReleaseCallback> compositor1;
[email protected]9794fb32013-08-29 09:49:59692 main_thread_.message_loop()->PostTask(
693 FROM_HERE,
694 base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef,
695 base::Unretained(this),
696 &compositor1));
697
698 // Then the texture layer is removed and attached to compositor2, and passes a
699 // reference to its impl tree.
[email protected]9260757f2013-09-17 01:24:16700 scoped_ptr<SingleReleaseCallback> compositor2;
[email protected]9794fb32013-08-29 09:49:59701 main_thread_.message_loop()->PostTask(
702 FROM_HERE,
703 base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef,
704 base::Unretained(this),
705 &compositor2));
706
707 Wait(main_thread_);
708 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
709
710 // The main thread reference is destroyed first.
711 main_thread_.message_loop()->PostTask(
712 FROM_HERE,
713 base::Bind(&TextureLayerMailboxHolderTest::ReleaseMainRef,
714 base::Unretained(this)));
715
716 EXPECT_CALL(test_data_.mock_callback_,
717 Release(test_data_.mailbox_name1_, 200, true)).Times(1);
718
719 bool manual_reset = false;
720 bool initially_signaled = false;
721 base::WaitableEvent begin_capture(manual_reset, initially_signaled);
722 base::WaitableEvent wait_for_capture(manual_reset, initially_signaled);
723 base::WaitableEvent stop_capture(manual_reset, initially_signaled);
724
725 // Post a task to start capturing tasks on the main thread. This will block
726 // the main thread until we signal the |stop_capture| event.
727 main_thread_.message_loop()->PostTask(
728 FROM_HERE,
729 base::Bind(&TextureLayerMailboxHolderTest::CapturePostTasksAndWait,
730 base::Unretained(this),
731 &begin_capture,
732 &wait_for_capture,
733 &stop_capture));
734
735 // Before the main thread capturing starts, one compositor destroys their
736 // impl reference. Since capturing did not start, this gets post-tasked to
737 // the main thread.
[email protected]9260757f2013-09-17 01:24:16738 compositor1->Run(100, false);
[email protected]9794fb32013-08-29 09:49:59739
740 // Start capturing on the main thread.
741 begin_capture.Signal();
742 wait_for_capture.Wait();
743
744 // Meanwhile, the second compositor released its impl reference, but this task
745 // gets shortcutted directly to the main thread. This means the reference is
746 // released before compositor1, whose reference will be released later when
747 // the post-task is serviced. But since it was destroyed _on the impl thread_
748 // last, its sync point values should be used.
[email protected]9260757f2013-09-17 01:24:16749 compositor2->Run(200, true);
[email protected]9794fb32013-08-29 09:49:59750
751 stop_capture.Signal();
752 Wait(main_thread_);
753
754 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
755}
756
[email protected]e216fef02013-03-20 22:56:10757class TextureLayerImplWithMailboxThreadedCallback : public LayerTreeTest {
[email protected]28571b042013-03-14 07:59:15758 public:
759 TextureLayerImplWithMailboxThreadedCallback()
760 : callback_count_(0),
761 commit_count_(0) {}
762
763 // Make sure callback is received on main and doesn't block the impl thread.
[email protected]9ee234342014-01-18 02:42:26764 void ReleaseCallback(uint32 sync_point, bool lost_resource) {
[email protected]9794fb32013-08-29 09:49:59765 EXPECT_EQ(true, main_thread_.CalledOnValidThread());
[email protected]7ba3ca72013-04-11 06:37:25766 EXPECT_FALSE(lost_resource);
[email protected]28571b042013-03-14 07:59:15767 ++callback_count_;
768 }
769
770 void SetMailbox(char mailbox_char) {
[email protected]9794fb32013-08-29 09:49:59771 EXPECT_EQ(true, main_thread_.CalledOnValidThread());
[email protected]9260757f2013-09-17 01:24:16772 scoped_ptr<SingleReleaseCallback> callback = SingleReleaseCallback::Create(
[email protected]28571b042013-03-14 07:59:15773 base::Bind(
774 &TextureLayerImplWithMailboxThreadedCallback::ReleaseCallback,
775 base::Unretained(this)));
[email protected]9ee234342014-01-18 02:42:26776 layer_->SetTextureMailbox(
777 TextureMailbox(
778 MailboxFromString(std::string(64, mailbox_char)), GL_TEXTURE_2D, 0),
779 callback.Pass());
[email protected]28571b042013-03-14 07:59:15780 }
781
[email protected]e216fef02013-03-20 22:56:10782 virtual void BeginTest() OVERRIDE {
[email protected]9794fb32013-08-29 09:49:59783 EXPECT_EQ(true, main_thread_.CalledOnValidThread());
784
[email protected]28571b042013-03-14 07:59:15785 gfx::Size bounds(100, 100);
786 root_ = Layer::Create();
787 root_->SetAnchorPoint(gfx::PointF());
788 root_->SetBounds(bounds);
789
[email protected]e8e4ae232013-04-12 00:26:01790 layer_ = TextureLayer::CreateForMailbox(NULL);
[email protected]28571b042013-03-14 07:59:15791 layer_->SetIsDrawable(true);
792 layer_->SetAnchorPoint(gfx::PointF());
793 layer_->SetBounds(bounds);
794
795 root_->AddChild(layer_);
[email protected]e216fef02013-03-20 22:56:10796 layer_tree_host()->SetRootLayer(root_);
[email protected]18ce59702013-04-09 04:58:40797 layer_tree_host()->SetViewportSize(bounds);
[email protected]28571b042013-03-14 07:59:15798 SetMailbox('1');
799 EXPECT_EQ(0, callback_count_);
800
801 // Case #1: change mailbox before the commit. The old mailbox should be
802 // released immediately.
803 SetMailbox('2');
804 EXPECT_EQ(1, callback_count_);
[email protected]e216fef02013-03-20 22:56:10805 PostSetNeedsCommitToMainThread();
[email protected]28571b042013-03-14 07:59:15806 }
807
[email protected]e216fef02013-03-20 22:56:10808 virtual void DidCommit() OVERRIDE {
[email protected]28571b042013-03-14 07:59:15809 ++commit_count_;
810 switch (commit_count_) {
811 case 1:
812 // Case #2: change mailbox after the commit (and draw), where the
813 // layer draws. The old mailbox should be released during the next
814 // commit.
815 SetMailbox('3');
816 EXPECT_EQ(1, callback_count_);
817 break;
818 case 2:
[email protected]28571b042013-03-14 07:59:15819 EXPECT_EQ(2, callback_count_);
820 // Case #3: change mailbox when the layer doesn't draw. The old
821 // mailbox should be released during the next commit.
822 layer_->SetBounds(gfx::Size());
823 SetMailbox('4');
824 break;
[email protected]9794fb32013-08-29 09:49:59825 case 3:
[email protected]28571b042013-03-14 07:59:15826 EXPECT_EQ(3, callback_count_);
827 // Case #4: release mailbox that was committed but never drawn. The
828 // old mailbox should be released during the next commit.
[email protected]9260757f2013-09-17 01:24:16829 layer_->SetTextureMailbox(TextureMailbox(),
830 scoped_ptr<SingleReleaseCallback>());
[email protected]28571b042013-03-14 07:59:15831 break;
[email protected]9794fb32013-08-29 09:49:59832 case 4:
833 if (layer_tree_host()->settings().impl_side_painting) {
834 // With impl painting, the texture mailbox will still be on the impl
835 // thread when the commit finishes, because the layer is not drawble
836 // when it has no texture mailbox, and thus does not block the commit
837 // on activation. So, we wait for activation.
838 // TODO(danakj): fix this. crbug.com/277953
839 layer_tree_host()->SetNeedsCommit();
840 break;
841 } else {
842 ++commit_count_;
843 }
844 case 5:
[email protected]28571b042013-03-14 07:59:15845 EXPECT_EQ(4, callback_count_);
[email protected]7096acc2013-06-18 21:12:43846 // Restore a mailbox for the next step.
847 SetMailbox('5');
848 break;
[email protected]9794fb32013-08-29 09:49:59849 case 6:
[email protected]7096acc2013-06-18 21:12:43850 // Case #5: remove layer from tree. Callback should *not* be called, the
851 // mailbox is returned to the main thread.
852 EXPECT_EQ(4, callback_count_);
853 layer_->RemoveFromParent();
854 break;
[email protected]9794fb32013-08-29 09:49:59855 case 7:
856 if (layer_tree_host()->settings().impl_side_painting) {
857 // With impl painting, the texture mailbox will still be on the impl
858 // thread when the commit finishes, because the layer is not around to
859 // block the commit on activation anymore. So, we wait for activation.
860 // TODO(danakj): fix this. crbug.com/277953
861 layer_tree_host()->SetNeedsCommit();
862 break;
863 } else {
864 ++commit_count_;
865 }
866 case 8:
[email protected]7096acc2013-06-18 21:12:43867 EXPECT_EQ(4, callback_count_);
868 // Resetting the mailbox will call the callback now.
[email protected]9260757f2013-09-17 01:24:16869 layer_->SetTextureMailbox(TextureMailbox(),
870 scoped_ptr<SingleReleaseCallback>());
[email protected]7096acc2013-06-18 21:12:43871 EXPECT_EQ(5, callback_count_);
[email protected]e216fef02013-03-20 22:56:10872 EndTest();
[email protected]28571b042013-03-14 07:59:15873 break;
874 default:
875 NOTREACHED();
876 break;
[email protected]de44a152013-01-08 15:28:46877 }
[email protected]28571b042013-03-14 07:59:15878 }
[email protected]de44a152013-01-08 15:28:46879
[email protected]e216fef02013-03-20 22:56:10880 virtual void AfterTest() OVERRIDE {}
[email protected]de44a152013-01-08 15:28:46881
[email protected]28571b042013-03-14 07:59:15882 private:
[email protected]9794fb32013-08-29 09:49:59883 base::ThreadChecker main_thread_;
[email protected]28571b042013-03-14 07:59:15884 int callback_count_;
885 int commit_count_;
886 scoped_refptr<Layer> root_;
887 scoped_refptr<TextureLayer> layer_;
[email protected]de44a152013-01-08 15:28:46888};
889
[email protected]4145d172013-05-10 16:54:36890SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(
891 TextureLayerImplWithMailboxThreadedCallback);
[email protected]de44a152013-01-08 15:28:46892
[email protected]74b43cc2013-08-30 06:29:27893
894class TextureLayerNoMailboxIsActivatedDuringCommit : public LayerTreeTest,
895 public TextureLayerClient {
896 protected:
897 TextureLayerNoMailboxIsActivatedDuringCommit()
898 : wait_thread_("WAIT"),
[email protected]f10dc472013-09-27 03:31:59899 wait_event_(false, false),
[email protected]f5931d42013-11-06 19:44:57900 texture_(0u) {
[email protected]74b43cc2013-08-30 06:29:27901 wait_thread_.Start();
902 }
903
904 virtual void BeginTest() OVERRIDE {
905 activate_count_ = 0;
906
907 gfx::Size bounds(100, 100);
908 root_ = Layer::Create();
909 root_->SetAnchorPoint(gfx::PointF());
910 root_->SetBounds(bounds);
911
912 layer_ = TextureLayer::Create(this);
913 layer_->SetIsDrawable(true);
914 layer_->SetAnchorPoint(gfx::PointF());
915 layer_->SetBounds(bounds);
916
917 root_->AddChild(layer_);
918 layer_tree_host()->SetRootLayer(root_);
919 layer_tree_host()->SetViewportSize(bounds);
920
921 PostSetNeedsCommitToMainThread();
922 }
923
[email protected]f5931d42013-11-06 19:44:57924 virtual scoped_ptr<OutputSurface> CreateOutputSurface(bool fallback)
925 OVERRIDE {
926 scoped_refptr<TestContextProvider> provider = TestContextProvider::Create();
927 texture_ = provider->UnboundTestContext3d()->createExternalTexture();
928 return FakeOutputSurface::Create3d(provider).PassAs<OutputSurface>();
929 }
930
[email protected]74b43cc2013-08-30 06:29:27931 // TextureLayerClient implementation.
932 virtual unsigned PrepareTexture() OVERRIDE {
[email protected]f5931d42013-11-06 19:44:57933 return texture_;
[email protected]74b43cc2013-08-30 06:29:27934 }
[email protected]9260757f2013-09-17 01:24:16935 virtual bool PrepareTextureMailbox(
936 TextureMailbox* mailbox,
937 scoped_ptr<SingleReleaseCallback>* release_callback,
938 bool use_shared_memory) OVERRIDE {
[email protected]74b43cc2013-08-30 06:29:27939 return false;
940 }
941
942 virtual void WillActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE {
943 // Slow down activation so the main thread DidCommit() will run if
944 // not blocked.
945 wait_thread_.message_loop()->PostDelayedTask(
946 FROM_HERE,
947 base::Bind(&base::WaitableEvent::Signal,
948 base::Unretained(&wait_event_)),
949 base::TimeDelta::FromMilliseconds(10));
950 wait_event_.Wait();
951
952 base::AutoLock lock(activate_lock_);
953 ++activate_count_;
954 }
955
956 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE {
957 // The main thread is awake now, and will run DidCommit() immediately.
958 // Run DidActivate() afterwards by posting it now.
959 proxy()->MainThreadTaskRunner()->PostTask(
960 FROM_HERE,
961 base::Bind(&TextureLayerNoMailboxIsActivatedDuringCommit::DidActivate,
962 base::Unretained(this)));
963 }
964
965 void DidActivate() {
966 base::AutoLock lock(activate_lock_);
967 switch (activate_count_) {
968 case 1:
969 // The first texture has been activated. Invalidate the layer so it
970 // grabs a new texture id from the client.
971 layer_->SetNeedsDisplay();
972 // So this commit number should complete after the second activate.
973 EXPECT_EQ(1, layer_tree_host()->source_frame_number());
974 break;
975 case 2:
976 // The second mailbox has been activated. Remove the layer from
977 // the tree to cause another commit/activation. The commit should
978 // finish *after* the layer is removed from the active tree.
979 layer_->RemoveFromParent();
980 // So this commit number should complete after the third activate.
981 EXPECT_EQ(2, layer_tree_host()->source_frame_number());
982 break;
983 case 3:
984 EndTest();
985 break;
986 }
987 }
988
989 virtual void DidCommit() OVERRIDE {
990 switch (layer_tree_host()->source_frame_number()) {
991 case 2: {
992 // The activate for the 2nd texture should have happened before now.
993 base::AutoLock lock(activate_lock_);
994 EXPECT_EQ(2, activate_count_);
995 break;
996 }
997 case 3: {
998 // The activate to remove the layer should have happened before now.
999 base::AutoLock lock(activate_lock_);
1000 EXPECT_EQ(3, activate_count_);
1001 break;
1002 }
1003 }
1004 }
1005
1006
1007 virtual void AfterTest() OVERRIDE {}
1008
1009 base::Thread wait_thread_;
1010 base::WaitableEvent wait_event_;
1011 base::Lock activate_lock_;
[email protected]f5931d42013-11-06 19:44:571012 unsigned texture_;
[email protected]74b43cc2013-08-30 06:29:271013 int activate_count_;
[email protected]74b43cc2013-08-30 06:29:271014 scoped_refptr<Layer> root_;
1015 scoped_refptr<TextureLayer> layer_;
1016};
1017
1018SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(
1019 TextureLayerNoMailboxIsActivatedDuringCommit);
1020
1021class TextureLayerMailboxIsActivatedDuringCommit : public LayerTreeTest {
1022 protected:
1023 TextureLayerMailboxIsActivatedDuringCommit()
1024 : wait_thread_("WAIT"),
1025 wait_event_(false, false) {
1026 wait_thread_.Start();
1027 }
1028
[email protected]9ee234342014-01-18 02:42:261029 static void ReleaseCallback(uint32 sync_point, bool lost_resource) {}
[email protected]74b43cc2013-08-30 06:29:271030
1031 void SetMailbox(char mailbox_char) {
[email protected]9260757f2013-09-17 01:24:161032 scoped_ptr<SingleReleaseCallback> callback = SingleReleaseCallback::Create(
[email protected]74b43cc2013-08-30 06:29:271033 base::Bind(
1034 &TextureLayerMailboxIsActivatedDuringCommit::ReleaseCallback));
[email protected]9ee234342014-01-18 02:42:261035 layer_->SetTextureMailbox(
1036 TextureMailbox(
1037 MailboxFromString(std::string(64, mailbox_char)), GL_TEXTURE_2D, 0),
1038 callback.Pass());
[email protected]74b43cc2013-08-30 06:29:271039 }
1040
1041 virtual void BeginTest() OVERRIDE {
1042 activate_count_ = 0;
1043
1044 gfx::Size bounds(100, 100);
1045 root_ = Layer::Create();
1046 root_->SetAnchorPoint(gfx::PointF());
1047 root_->SetBounds(bounds);
1048
1049 layer_ = TextureLayer::CreateForMailbox(NULL);
1050 layer_->SetIsDrawable(true);
1051 layer_->SetAnchorPoint(gfx::PointF());
1052 layer_->SetBounds(bounds);
1053
1054 root_->AddChild(layer_);
1055 layer_tree_host()->SetRootLayer(root_);
1056 layer_tree_host()->SetViewportSize(bounds);
1057 SetMailbox('1');
1058
1059 PostSetNeedsCommitToMainThread();
1060 }
1061
1062 virtual void WillActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE {
1063 // Slow down activation so the main thread DidCommit() will run if
1064 // not blocked.
1065 wait_thread_.message_loop()->PostDelayedTask(
1066 FROM_HERE,
1067 base::Bind(&base::WaitableEvent::Signal,
1068 base::Unretained(&wait_event_)),
1069 base::TimeDelta::FromMilliseconds(10));
1070 wait_event_.Wait();
1071
1072 base::AutoLock lock(activate_lock_);
1073 ++activate_count_;
1074 }
1075
1076 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE {
1077 // The main thread is awake now, and will run DidCommit() immediately.
1078 // Run DidActivate() afterwards by posting it now.
1079 proxy()->MainThreadTaskRunner()->PostTask(
1080 FROM_HERE,
1081 base::Bind(&TextureLayerMailboxIsActivatedDuringCommit::DidActivate,
1082 base::Unretained(this)));
1083 }
1084
1085 void DidActivate() {
1086 base::AutoLock lock(activate_lock_);
1087 switch (activate_count_) {
1088 case 1:
1089 // The first mailbox has been activated. Set a new mailbox, and
1090 // expect the next commit to finish *after* it is activated.
1091 SetMailbox('2');
1092 // So this commit number should complete after the second activate.
1093 EXPECT_EQ(1, layer_tree_host()->source_frame_number());
1094 break;
1095 case 2:
1096 // The second mailbox has been activated. Remove the layer from
1097 // the tree to cause another commit/activation. The commit should
1098 // finish *after* the layer is removed from the active tree.
1099 layer_->RemoveFromParent();
1100 // So this commit number should complete after the third activate.
1101 EXPECT_EQ(2, layer_tree_host()->source_frame_number());
1102 break;
1103 case 3:
1104 EndTest();
1105 break;
1106 }
1107 }
1108
1109 virtual void DidCommit() OVERRIDE {
1110 switch (layer_tree_host()->source_frame_number()) {
1111 case 2: {
1112 // The activate for the 2nd mailbox should have happened before now.
1113 base::AutoLock lock(activate_lock_);
1114 EXPECT_EQ(2, activate_count_);
1115 break;
1116 }
1117 case 3: {
1118 // The activate to remove the layer should have happened before now.
1119 base::AutoLock lock(activate_lock_);
1120 EXPECT_EQ(3, activate_count_);
1121 break;
1122 }
1123 }
1124 }
1125
1126
1127 virtual void AfterTest() OVERRIDE {}
1128
1129 base::Thread wait_thread_;
1130 base::WaitableEvent wait_event_;
1131 base::Lock activate_lock_;
1132 int activate_count_;
1133 scoped_refptr<Layer> root_;
1134 scoped_refptr<TextureLayer> layer_;
1135};
1136
1137SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(
1138 TextureLayerMailboxIsActivatedDuringCommit);
1139
[email protected]de44a152013-01-08 15:28:461140class TextureLayerImplWithMailboxTest : public TextureLayerTest {
[email protected]28571b042013-03-14 07:59:151141 protected:
[email protected]408b5e22013-03-19 09:48:091142 TextureLayerImplWithMailboxTest()
1143 : fake_client_(
1144 FakeLayerTreeHostClient(FakeLayerTreeHostClient::DIRECT_3D)) {}
1145
[email protected]28571b042013-03-14 07:59:151146 virtual void SetUp() {
1147 TextureLayerTest::SetUp();
[email protected]408b5e22013-03-19 09:48:091148 layer_tree_host_.reset(new MockLayerTreeHost(&fake_client_));
[email protected]f74945f2013-03-21 17:08:361149 EXPECT_TRUE(host_impl_.InitializeRenderer(CreateFakeOutputSurface()));
[email protected]28571b042013-03-14 07:59:151150 }
[email protected]de44a152013-01-08 15:28:461151
[email protected]0ec335c42013-07-04 06:17:081152 bool WillDraw(TextureLayerImpl* layer, DrawMode mode) {
1153 bool will_draw = layer->WillDraw(
1154 mode, host_impl_.active_tree()->resource_provider());
1155 if (will_draw)
1156 layer->DidDraw(host_impl_.active_tree()->resource_provider());
1157 return will_draw;
1158 }
1159
[email protected]28571b042013-03-14 07:59:151160 CommonMailboxObjects test_data_;
[email protected]408b5e22013-03-19 09:48:091161 FakeLayerTreeHostClient fake_client_;
[email protected]de44a152013-01-08 15:28:461162};
1163
[email protected]ffbb2212013-06-02 23:47:591164// Test conditions for results of TextureLayerImpl::WillDraw under
1165// different configurations of different mailbox, texture_id, and draw_mode.
1166TEST_F(TextureLayerImplWithMailboxTest, TestWillDraw) {
[email protected]0ec335c42013-07-04 06:17:081167 EXPECT_CALL(test_data_.mock_callback_,
1168 Release(test_data_.mailbox_name1_,
1169 test_data_.sync_point1_,
1170 false))
1171 .Times(AnyNumber());
1172 EXPECT_CALL(test_data_.mock_callback_,
1173 Release2(test_data_.shared_memory_.get(), 0, false))
1174 .Times(AnyNumber());
[email protected]ffbb2212013-06-02 23:47:591175 // Hardware mode.
1176 {
1177 scoped_ptr<TextureLayerImpl> impl_layer =
1178 TextureLayerImpl::Create(host_impl_.active_tree(), 1, true);
[email protected]afc4f262013-10-05 01:14:101179 impl_layer->SetDrawsContent(true);
[email protected]9260757f2013-09-17 01:24:161180 impl_layer->SetTextureMailbox(
1181 test_data_.mailbox1_,
1182 SingleReleaseCallback::Create(test_data_.release_mailbox1_));
[email protected]0ec335c42013-07-04 06:17:081183 EXPECT_TRUE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE));
[email protected]ffbb2212013-06-02 23:47:591184 }
1185
1186 {
1187 scoped_ptr<TextureLayerImpl> impl_layer =
1188 TextureLayerImpl::Create(host_impl_.active_tree(), 1, true);
[email protected]afc4f262013-10-05 01:14:101189 impl_layer->SetDrawsContent(true);
[email protected]9260757f2013-09-17 01:24:161190 impl_layer->SetTextureMailbox(TextureMailbox(),
1191 scoped_ptr<SingleReleaseCallback>());
[email protected]0ec335c42013-07-04 06:17:081192 EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE));
1193 }
1194
1195 {
1196 // Software resource.
1197 scoped_ptr<TextureLayerImpl> impl_layer =
1198 TextureLayerImpl::Create(host_impl_.active_tree(), 1, true);
[email protected]afc4f262013-10-05 01:14:101199 impl_layer->SetDrawsContent(true);
[email protected]9260757f2013-09-17 01:24:161200 impl_layer->SetTextureMailbox(
1201 test_data_.mailbox3_,
1202 SingleReleaseCallback::Create(test_data_.release_mailbox3_));
[email protected]3e44d7a2013-07-30 00:03:101203 EXPECT_TRUE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE));
[email protected]ffbb2212013-06-02 23:47:591204 }
1205
1206 {
1207 scoped_ptr<TextureLayerImpl> impl_layer =
1208 TextureLayerImpl::Create(host_impl_.active_tree(), 1, false);
[email protected]afc4f262013-10-05 01:14:101209 impl_layer->SetDrawsContent(true);
[email protected]0634cdd42013-08-16 00:46:091210 ContextProvider* context_provider =
1211 host_impl_.output_surface()->context_provider();
[email protected]1dff7962014-01-10 12:05:031212 GLuint texture = 0;
1213 context_provider->ContextGL()->GenTextures(1, &texture);
[email protected]ad0250b2014-01-18 03:24:341214 impl_layer->SetTextureId(texture);
[email protected]0ec335c42013-07-04 06:17:081215 EXPECT_TRUE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE));
[email protected]ffbb2212013-06-02 23:47:591216 }
1217
1218 {
1219 scoped_ptr<TextureLayerImpl> impl_layer =
1220 TextureLayerImpl::Create(host_impl_.active_tree(), 1, false);
[email protected]afc4f262013-10-05 01:14:101221 impl_layer->SetDrawsContent(true);
[email protected]ad0250b2014-01-18 03:24:341222 impl_layer->SetTextureId(0);
[email protected]0ec335c42013-07-04 06:17:081223 EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE));
1224 }
1225
1226 // Software mode.
1227 {
1228 scoped_ptr<TextureLayerImpl> impl_layer =
1229 TextureLayerImpl::Create(host_impl_.active_tree(), 1, true);
[email protected]afc4f262013-10-05 01:14:101230 impl_layer->SetDrawsContent(true);
[email protected]9260757f2013-09-17 01:24:161231 impl_layer->SetTextureMailbox(
1232 test_data_.mailbox1_,
1233 SingleReleaseCallback::Create(test_data_.release_mailbox1_));
[email protected]0ec335c42013-07-04 06:17:081234 EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_SOFTWARE));
1235 }
1236
1237 {
1238 scoped_ptr<TextureLayerImpl> impl_layer =
1239 TextureLayerImpl::Create(host_impl_.active_tree(), 1, true);
[email protected]afc4f262013-10-05 01:14:101240 impl_layer->SetDrawsContent(true);
[email protected]9260757f2013-09-17 01:24:161241 impl_layer->SetTextureMailbox(TextureMailbox(),
1242 scoped_ptr<SingleReleaseCallback>());
[email protected]0ec335c42013-07-04 06:17:081243 EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_SOFTWARE));
1244 }
1245
1246 {
1247 // Software resource.
1248 scoped_ptr<TextureLayerImpl> impl_layer =
1249 TextureLayerImpl::Create(host_impl_.active_tree(), 1, true);
[email protected]afc4f262013-10-05 01:14:101250 impl_layer->SetDrawsContent(true);
[email protected]9260757f2013-09-17 01:24:161251 impl_layer->SetTextureMailbox(
1252 test_data_.mailbox3_,
1253 SingleReleaseCallback::Create(test_data_.release_mailbox3_));
[email protected]0ec335c42013-07-04 06:17:081254 EXPECT_TRUE(WillDraw(impl_layer.get(), DRAW_MODE_SOFTWARE));
1255 }
1256
1257 {
1258 scoped_ptr<TextureLayerImpl> impl_layer =
1259 TextureLayerImpl::Create(host_impl_.active_tree(), 1, false);
[email protected]afc4f262013-10-05 01:14:101260 impl_layer->SetDrawsContent(true);
[email protected]0634cdd42013-08-16 00:46:091261 ContextProvider* context_provider =
1262 host_impl_.output_surface()->context_provider();
[email protected]1dff7962014-01-10 12:05:031263 GLuint texture = 0;
1264 context_provider->ContextGL()->GenTextures(1, &texture);
[email protected]ad0250b2014-01-18 03:24:341265 impl_layer->SetTextureId(texture);
[email protected]0ec335c42013-07-04 06:17:081266 EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_SOFTWARE));
1267 }
1268
1269 {
1270 scoped_ptr<TextureLayerImpl> impl_layer =
1271 TextureLayerImpl::Create(host_impl_.active_tree(), 1, false);
[email protected]afc4f262013-10-05 01:14:101272 impl_layer->SetDrawsContent(true);
[email protected]ad0250b2014-01-18 03:24:341273 impl_layer->SetTextureId(0);
[email protected]0ec335c42013-07-04 06:17:081274 EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_SOFTWARE));
[email protected]ffbb2212013-06-02 23:47:591275 }
1276
1277 // Resourceless software mode.
1278 {
1279 scoped_ptr<TextureLayerImpl> impl_layer =
1280 TextureLayerImpl::Create(host_impl_.active_tree(), 1, true);
[email protected]afc4f262013-10-05 01:14:101281 impl_layer->SetDrawsContent(true);
[email protected]9260757f2013-09-17 01:24:161282 impl_layer->SetTextureMailbox(
1283 test_data_.mailbox1_,
1284 SingleReleaseCallback::Create(test_data_.release_mailbox1_));
[email protected]0ec335c42013-07-04 06:17:081285 EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_RESOURCELESS_SOFTWARE));
[email protected]ffbb2212013-06-02 23:47:591286 }
1287
1288 {
1289 scoped_ptr<TextureLayerImpl> impl_layer =
1290 TextureLayerImpl::Create(host_impl_.active_tree(), 1, false);
[email protected]afc4f262013-10-05 01:14:101291 impl_layer->SetDrawsContent(true);
[email protected]0634cdd42013-08-16 00:46:091292 ContextProvider* context_provider =
1293 host_impl_.output_surface()->context_provider();
[email protected]1dff7962014-01-10 12:05:031294 GLuint texture = 0;
1295 context_provider->ContextGL()->GenTextures(1, &texture);
[email protected]ad0250b2014-01-18 03:24:341296 impl_layer->SetTextureId(texture);
[email protected]0ec335c42013-07-04 06:17:081297 EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_RESOURCELESS_SOFTWARE));
[email protected]ffbb2212013-06-02 23:47:591298 }
1299}
1300
[email protected]28571b042013-03-14 07:59:151301TEST_F(TextureLayerImplWithMailboxTest, TestImplLayerCallbacks) {
1302 host_impl_.CreatePendingTree();
1303 scoped_ptr<TextureLayerImpl> pending_layer;
1304 pending_layer = TextureLayerImpl::Create(host_impl_.pending_tree(), 1, true);
1305 ASSERT_TRUE(pending_layer);
[email protected]de44a152013-01-08 15:28:461306
[email protected]ed511b8d2013-03-25 03:29:291307 scoped_ptr<LayerImpl> active_layer(
[email protected]28571b042013-03-14 07:59:151308 pending_layer->CreateLayerImpl(host_impl_.active_tree()));
[email protected]ed511b8d2013-03-25 03:29:291309 ASSERT_TRUE(active_layer);
[email protected]de44a152013-01-08 15:28:461310
[email protected]9260757f2013-09-17 01:24:161311 pending_layer->SetTextureMailbox(
1312 test_data_.mailbox1_,
1313 SingleReleaseCallback::Create(test_data_.release_mailbox1_));
[email protected]421e84f2013-02-22 03:27:151314
[email protected]28571b042013-03-14 07:59:151315 // Test multiple commits without an activation.
1316 EXPECT_CALL(test_data_.mock_callback_,
[email protected]7ba3ca72013-04-11 06:37:251317 Release(test_data_.mailbox_name1_,
1318 test_data_.sync_point1_,
1319 false))
[email protected]28571b042013-03-14 07:59:151320 .Times(1);
[email protected]9260757f2013-09-17 01:24:161321 pending_layer->SetTextureMailbox(
1322 test_data_.mailbox2_,
1323 SingleReleaseCallback::Create(test_data_.release_mailbox2_));
[email protected]28571b042013-03-14 07:59:151324 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]421e84f2013-02-22 03:27:151325
[email protected]28571b042013-03-14 07:59:151326 // Test callback after activation.
[email protected]ed511b8d2013-03-25 03:29:291327 pending_layer->PushPropertiesTo(active_layer.get());
1328 active_layer->DidBecomeActive();
[email protected]421e84f2013-02-22 03:27:151329
[email protected]7ba3ca72013-04-11 06:37:251330 EXPECT_CALL(test_data_.mock_callback_, Release(_, _, _)).Times(0);
[email protected]9260757f2013-09-17 01:24:161331 pending_layer->SetTextureMailbox(
1332 test_data_.mailbox1_,
1333 SingleReleaseCallback::Create(test_data_.release_mailbox1_));
[email protected]28571b042013-03-14 07:59:151334 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]421e84f2013-02-22 03:27:151335
[email protected]7ba3ca72013-04-11 06:37:251336 EXPECT_CALL(test_data_.mock_callback_,
1337 Release(test_data_.mailbox_name2_, _, false))
[email protected]28571b042013-03-14 07:59:151338 .Times(1);
[email protected]ed511b8d2013-03-25 03:29:291339 pending_layer->PushPropertiesTo(active_layer.get());
1340 active_layer->DidBecomeActive();
[email protected]28571b042013-03-14 07:59:151341 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]de44a152013-01-08 15:28:461342
[email protected]28571b042013-03-14 07:59:151343 // Test resetting the mailbox.
[email protected]7ba3ca72013-04-11 06:37:251344 EXPECT_CALL(test_data_.mock_callback_,
1345 Release(test_data_.mailbox_name1_, _, false))
[email protected]28571b042013-03-14 07:59:151346 .Times(1);
[email protected]9260757f2013-09-17 01:24:161347 pending_layer->SetTextureMailbox(TextureMailbox(),
1348 scoped_ptr<SingleReleaseCallback>());
[email protected]ed511b8d2013-03-25 03:29:291349 pending_layer->PushPropertiesTo(active_layer.get());
1350 active_layer->DidBecomeActive();
[email protected]28571b042013-03-14 07:59:151351 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]de44a152013-01-08 15:28:461352
[email protected]28571b042013-03-14 07:59:151353 // Test destructor.
1354 EXPECT_CALL(test_data_.mock_callback_,
[email protected]7ba3ca72013-04-11 06:37:251355 Release(test_data_.mailbox_name1_,
1356 test_data_.sync_point1_,
1357 false))
[email protected]28571b042013-03-14 07:59:151358 .Times(1);
[email protected]9260757f2013-09-17 01:24:161359 pending_layer->SetTextureMailbox(
1360 test_data_.mailbox1_,
1361 SingleReleaseCallback::Create(test_data_.release_mailbox1_));
[email protected]de44a152013-01-08 15:28:461362}
1363
[email protected]28571b042013-03-14 07:59:151364TEST_F(TextureLayerImplWithMailboxTest,
1365 TestDestructorCallbackOnCreatedResource) {
1366 scoped_ptr<TextureLayerImpl> impl_layer;
1367 impl_layer = TextureLayerImpl::Create(host_impl_.active_tree(), 1, true);
1368 ASSERT_TRUE(impl_layer);
[email protected]de44a152013-01-08 15:28:461369
[email protected]7ba3ca72013-04-11 06:37:251370 EXPECT_CALL(test_data_.mock_callback_,
1371 Release(test_data_.mailbox_name1_, _, false))
[email protected]28571b042013-03-14 07:59:151372 .Times(1);
[email protected]9260757f2013-09-17 01:24:161373 impl_layer->SetTextureMailbox(
1374 test_data_.mailbox1_,
1375 SingleReleaseCallback::Create(test_data_.release_mailbox1_));
[email protected]afc4f262013-10-05 01:14:101376 impl_layer->SetDrawsContent(true);
[email protected]ffbb2212013-06-02 23:47:591377 impl_layer->DidBecomeActive();
1378 EXPECT_TRUE(impl_layer->WillDraw(
1379 DRAW_MODE_HARDWARE, host_impl_.active_tree()->resource_provider()));
[email protected]28571b042013-03-14 07:59:151380 impl_layer->DidDraw(host_impl_.active_tree()->resource_provider());
[email protected]9260757f2013-09-17 01:24:161381 impl_layer->SetTextureMailbox(TextureMailbox(),
1382 scoped_ptr<SingleReleaseCallback>());
[email protected]de44a152013-01-08 15:28:461383}
1384
[email protected]28571b042013-03-14 07:59:151385TEST_F(TextureLayerImplWithMailboxTest, TestCallbackOnInUseResource) {
1386 ResourceProvider* provider = host_impl_.active_tree()->resource_provider();
1387 ResourceProvider::ResourceId id =
[email protected]9260757f2013-09-17 01:24:161388 provider->CreateResourceFromTextureMailbox(
1389 test_data_.mailbox1_,
1390 SingleReleaseCallback::Create(test_data_.release_mailbox1_));
[email protected]28571b042013-03-14 07:59:151391 provider->AllocateForTesting(id);
[email protected]de44a152013-01-08 15:28:461392
[email protected]28571b042013-03-14 07:59:151393 // Transfer some resources to the parent.
1394 ResourceProvider::ResourceIdArray resource_ids_to_transfer;
1395 resource_ids_to_transfer.push_back(id);
1396 TransferableResourceArray list;
1397 provider->PrepareSendToParent(resource_ids_to_transfer, &list);
1398 EXPECT_TRUE(provider->InUseByConsumer(id));
[email protected]7ba3ca72013-04-11 06:37:251399 EXPECT_CALL(test_data_.mock_callback_, Release(_, _, _)).Times(0);
[email protected]28571b042013-03-14 07:59:151400 provider->DeleteResource(id);
1401 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]7ba3ca72013-04-11 06:37:251402 EXPECT_CALL(test_data_.mock_callback_,
1403 Release(test_data_.mailbox_name1_, _, false))
[email protected]28571b042013-03-14 07:59:151404 .Times(1);
[email protected]e00bab022013-08-19 00:42:451405 ReturnedResourceArray returned;
1406 TransferableResource::ReturnResources(list, &returned);
1407 provider->ReceiveReturnsFromParent(returned);
[email protected]de44a152013-01-08 15:28:461408}
1409
[email protected]97d519fb2013-03-29 02:27:541410// Check that ClearClient correctly clears the state so that the impl side
1411// doesn't try to use a texture that could have been destroyed.
[email protected]7ba3ca72013-04-11 06:37:251412class TextureLayerClientTest
1413 : public LayerTreeTest,
1414 public TextureLayerClient {
[email protected]97d519fb2013-03-29 02:27:541415 public:
1416 TextureLayerClientTest()
[email protected]f5931d42013-11-06 19:44:571417 : texture_(0),
[email protected]97d519fb2013-03-29 02:27:541418 commit_count_(0),
1419 expected_used_textures_on_draw_(0),
1420 expected_used_textures_on_commit_(0) {}
1421
[email protected]ebc0e1df2013-08-01 02:46:221422 virtual scoped_ptr<OutputSurface> CreateOutputSurface(bool fallback)
1423 OVERRIDE {
[email protected]f5931d42013-11-06 19:44:571424 scoped_refptr<TestContextProvider> provider = TestContextProvider::Create();
1425 texture_ = provider->UnboundTestContext3d()->createExternalTexture();
1426 return FakeOutputSurface::Create3d(provider).PassAs<OutputSurface>();
[email protected]97d519fb2013-03-29 02:27:541427 }
1428
[email protected]f5931d42013-11-06 19:44:571429 virtual unsigned PrepareTexture() OVERRIDE { return texture_; }
[email protected]97d519fb2013-03-29 02:27:541430
[email protected]2541d1a2013-07-10 07:33:271431 virtual bool PrepareTextureMailbox(
[email protected]9260757f2013-09-17 01:24:161432 TextureMailbox* mailbox,
1433 scoped_ptr<SingleReleaseCallback>* release_callback,
1434 bool use_shared_memory) OVERRIDE {
[email protected]e8e4ae232013-04-12 00:26:011435 return false;
1436 }
1437
[email protected]97d519fb2013-03-29 02:27:541438 virtual void SetupTree() OVERRIDE {
1439 scoped_refptr<Layer> root = Layer::Create();
1440 root->SetBounds(gfx::Size(10, 10));
1441 root->SetAnchorPoint(gfx::PointF());
1442 root->SetIsDrawable(true);
1443
1444 texture_layer_ = TextureLayer::Create(this);
1445 texture_layer_->SetBounds(gfx::Size(10, 10));
1446 texture_layer_->SetAnchorPoint(gfx::PointF());
1447 texture_layer_->SetIsDrawable(true);
1448 root->AddChild(texture_layer_);
1449
1450 layer_tree_host()->SetRootLayer(root);
1451 LayerTreeTest::SetupTree();
1452 {
1453 base::AutoLock lock(lock_);
1454 expected_used_textures_on_commit_ = 1;
1455 }
1456 }
1457
1458 virtual void BeginTest() OVERRIDE {
1459 PostSetNeedsCommitToMainThread();
1460 }
1461
1462 virtual void DidCommitAndDrawFrame() OVERRIDE {
1463 ++commit_count_;
1464 switch (commit_count_) {
1465 case 1:
1466 texture_layer_->ClearClient();
1467 texture_layer_->SetNeedsDisplay();
1468 {
1469 base::AutoLock lock(lock_);
1470 expected_used_textures_on_commit_ = 0;
1471 }
[email protected]97d519fb2013-03-29 02:27:541472 break;
1473 case 2:
1474 EndTest();
1475 break;
1476 default:
1477 NOTREACHED();
1478 break;
1479 }
1480 }
1481
1482 virtual void BeginCommitOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1483 base::AutoLock lock(lock_);
1484 expected_used_textures_on_draw_ = expected_used_textures_on_commit_;
1485 }
1486
1487 virtual bool PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
1488 LayerTreeHostImpl::FrameData* frame_data,
1489 bool result) OVERRIDE {
[email protected]f5931d42013-11-06 19:44:571490 ContextForImplThread(host_impl)->ResetUsedTextures();
[email protected]97d519fb2013-03-29 02:27:541491 return true;
1492 }
1493
1494 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1495 bool result) OVERRIDE {
1496 ASSERT_TRUE(result);
[email protected]f5931d42013-11-06 19:44:571497 EXPECT_EQ(expected_used_textures_on_draw_,
1498 ContextForImplThread(host_impl)->NumUsedTextures());
[email protected]97d519fb2013-03-29 02:27:541499 }
1500
1501 virtual void AfterTest() OVERRIDE {}
1502
1503 private:
[email protected]f5931d42013-11-06 19:44:571504 TestWebGraphicsContext3D* ContextForImplThread(LayerTreeHostImpl* host_impl) {
[email protected]1dff7962014-01-10 12:05:031505 return static_cast<TestContextProvider*>(
1506 host_impl->output_surface()->context_provider().get())->TestContext3d();
[email protected]f5931d42013-11-06 19:44:571507 }
1508
[email protected]97d519fb2013-03-29 02:27:541509 scoped_refptr<TextureLayer> texture_layer_;
[email protected]97d519fb2013-03-29 02:27:541510 unsigned texture_;
1511 int commit_count_;
1512
1513 // Used only on thread.
1514 unsigned expected_used_textures_on_draw_;
1515
1516 // Used on either thread, protected by lock_.
1517 base::Lock lock_;
1518 unsigned expected_used_textures_on_commit_;
1519};
1520
[email protected]4145d172013-05-10 16:54:361521// The TextureLayerClient does not use mailboxes, so can't use a delegating
1522// renderer.
1523SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TextureLayerClientTest);
[email protected]97d519fb2013-03-29 02:27:541524
[email protected]b04264f92013-09-13 23:37:291525
1526// Checks that changing a texture in the client for a TextureLayer that's
1527// invisible correctly works without drawing a deleted texture. See
1528// crbug.com/266628
1529class TextureLayerChangeInvisibleTest
1530 : public LayerTreeTest,
1531 public TextureLayerClient {
1532 public:
1533 TextureLayerChangeInvisibleTest()
[email protected]f5931d42013-11-06 19:44:571534 : texture_(0u),
[email protected]b04264f92013-09-13 23:37:291535 prepare_called_(0),
1536 commit_count_(0),
1537 expected_texture_on_draw_(0) {}
1538
[email protected]f5931d42013-11-06 19:44:571539 virtual scoped_ptr<OutputSurface> CreateOutputSurface(bool fallback)
1540 OVERRIDE {
1541 scoped_refptr<TestContextProvider> provider = TestContextProvider::Create();
1542 texture_ = provider->UnboundTestContext3d()->createExternalTexture();
1543 return FakeOutputSurface::Create3d(provider).PassAs<OutputSurface>();
1544 }
1545
[email protected]b04264f92013-09-13 23:37:291546 // TextureLayerClient implementation.
1547 virtual unsigned PrepareTexture() OVERRIDE {
1548 ++prepare_called_;
1549 return texture_;
1550 }
[email protected]b04264f92013-09-13 23:37:291551 virtual bool PrepareTextureMailbox(
[email protected]e51444a2013-12-10 23:05:011552 TextureMailbox* mailbox,
[email protected]9260757f2013-09-17 01:24:161553 scoped_ptr<SingleReleaseCallback>* release_callback,
1554 bool use_shared_memory) OVERRIDE {
[email protected]b04264f92013-09-13 23:37:291555 return false;
1556 }
1557
1558 virtual void SetupTree() OVERRIDE {
1559 scoped_refptr<Layer> root = Layer::Create();
1560 root->SetBounds(gfx::Size(10, 10));
1561 root->SetAnchorPoint(gfx::PointF());
1562 root->SetIsDrawable(true);
1563
1564 solid_layer_ = SolidColorLayer::Create();
1565 solid_layer_->SetBounds(gfx::Size(10, 10));
1566 solid_layer_->SetIsDrawable(true);
1567 solid_layer_->SetBackgroundColor(SK_ColorWHITE);
1568 root->AddChild(solid_layer_);
1569
1570 parent_layer_ = Layer::Create();
1571 parent_layer_->SetBounds(gfx::Size(10, 10));
1572 parent_layer_->SetIsDrawable(true);
1573 root->AddChild(parent_layer_);
1574
1575 texture_layer_ = TextureLayer::Create(this);
1576 texture_layer_->SetBounds(gfx::Size(10, 10));
1577 texture_layer_->SetAnchorPoint(gfx::PointF());
1578 texture_layer_->SetIsDrawable(true);
1579 parent_layer_->AddChild(texture_layer_);
1580
1581 layer_tree_host()->SetRootLayer(root);
1582 LayerTreeTest::SetupTree();
1583 }
1584
1585 virtual void BeginTest() OVERRIDE {
1586 PostSetNeedsCommitToMainThread();
1587 }
1588
1589 virtual void DidCommitAndDrawFrame() OVERRIDE {
1590 ++commit_count_;
1591 switch (commit_count_) {
1592 case 1:
1593 // We should have updated the layer, committing the texture.
1594 EXPECT_EQ(1, prepare_called_);
1595 // Make layer invisible.
1596 parent_layer_->SetOpacity(0.f);
1597 break;
1598 case 2: {
1599 // Layer shouldn't have been updated.
1600 EXPECT_EQ(1, prepare_called_);
[email protected]b04264f92013-09-13 23:37:291601 texture_layer_->SetNeedsDisplay();
1602 // Force a change to make sure we draw a frame.
1603 solid_layer_->SetBackgroundColor(SK_ColorGRAY);
1604 break;
1605 }
1606 case 3:
1607 EXPECT_EQ(1, prepare_called_);
[email protected]b04264f92013-09-13 23:37:291608 // Make layer visible again.
1609 parent_layer_->SetOpacity(1.f);
1610 break;
1611 case 4: {
1612 // Layer should have been updated.
1613 EXPECT_EQ(2, prepare_called_);
1614 texture_layer_->ClearClient();
[email protected]b04264f92013-09-13 23:37:291615 texture_ = 0;
1616 break;
1617 }
1618 case 5:
1619 EndTest();
1620 break;
1621 default:
1622 NOTREACHED();
1623 break;
1624 }
1625 }
1626
1627 virtual void BeginCommitOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1628 ASSERT_TRUE(proxy()->IsMainThreadBlocked());
1629 // This is the only texture that can be drawn this frame.
1630 expected_texture_on_draw_ = texture_;
1631 }
1632
1633 virtual bool PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
1634 LayerTreeHostImpl::FrameData* frame_data,
1635 bool result) OVERRIDE {
1636 ContextForImplThread(host_impl)->ResetUsedTextures();
1637 return true;
1638 }
1639
1640 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1641 bool result) OVERRIDE {
1642 ASSERT_TRUE(result);
1643 TestWebGraphicsContext3D* context = ContextForImplThread(host_impl);
1644 int used_textures = context->NumUsedTextures();
1645 switch (host_impl->active_tree()->source_frame_number()) {
1646 case 0:
1647 EXPECT_EQ(1, used_textures);
1648 EXPECT_TRUE(context->UsedTexture(expected_texture_on_draw_));
1649 break;
1650 case 1:
1651 case 2:
1652 EXPECT_EQ(0, used_textures);
1653 break;
1654 case 3:
1655 EXPECT_EQ(1, used_textures);
1656 EXPECT_TRUE(context->UsedTexture(expected_texture_on_draw_));
1657 break;
1658 default:
1659 break;
1660 }
1661 }
1662
1663 virtual void AfterTest() OVERRIDE {}
1664
1665 private:
1666 TestWebGraphicsContext3D* ContextForImplThread(LayerTreeHostImpl* host_impl) {
[email protected]1dff7962014-01-10 12:05:031667 return static_cast<TestContextProvider*>(
1668 host_impl->output_surface()->context_provider().get())->TestContext3d();
[email protected]b04264f92013-09-13 23:37:291669 }
1670
1671 scoped_refptr<SolidColorLayer> solid_layer_;
1672 scoped_refptr<Layer> parent_layer_;
1673 scoped_refptr<TextureLayer> texture_layer_;
[email protected]b04264f92013-09-13 23:37:291674
1675 // Used on the main thread, and on the impl thread while the main thread is
1676 // blocked.
1677 unsigned texture_;
1678
1679 // Used on the main thread.
[email protected]b04264f92013-09-13 23:37:291680 int prepare_called_;
1681 int commit_count_;
1682
1683 // Used on the compositor thread.
1684 unsigned expected_texture_on_draw_;
1685};
1686
1687// The TextureLayerChangeInvisibleTest does not use mailboxes, so can't use a
1688// delegating renderer.
1689SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TextureLayerChangeInvisibleTest);
1690
[email protected]4bad8b62013-10-24 01:27:291691// Checks that TextureLayer::Update does not cause an extra commit when setting
1692// the texture mailbox.
1693class TextureLayerNoExtraCommitForMailboxTest
1694 : public LayerTreeTest,
1695 public TextureLayerClient {
1696 public:
[email protected]4bad8b62013-10-24 01:27:291697 // TextureLayerClient implementation.
1698 virtual unsigned PrepareTexture() OVERRIDE {
1699 NOTREACHED();
1700 return 0;
1701 }
[email protected]4bad8b62013-10-24 01:27:291702 virtual bool PrepareTextureMailbox(
[email protected]9ee234342014-01-18 02:42:261703 TextureMailbox* texture_mailbox,
[email protected]4bad8b62013-10-24 01:27:291704 scoped_ptr<SingleReleaseCallback>* release_callback,
1705 bool use_shared_memory) OVERRIDE {
[email protected]cce34bd2013-12-02 23:24:451706 if (layer_tree_host()->source_frame_number() == 1) {
[email protected]9ee234342014-01-18 02:42:261707 *texture_mailbox = TextureMailbox();
[email protected]cce34bd2013-12-02 23:24:451708 return true;
1709 }
[email protected]4bad8b62013-10-24 01:27:291710
[email protected]9ee234342014-01-18 02:42:261711 *texture_mailbox = TextureMailbox(
1712 MailboxFromString(std::string(64, '1')), GL_TEXTURE_2D, 0);
[email protected]4bad8b62013-10-24 01:27:291713 *release_callback = SingleReleaseCallback::Create(
1714 base::Bind(&TextureLayerNoExtraCommitForMailboxTest::MailboxReleased,
1715 base::Unretained(this)));
[email protected]4bad8b62013-10-24 01:27:291716 return true;
1717 }
1718
[email protected]9ee234342014-01-18 02:42:261719 void MailboxReleased(uint32 sync_point, bool lost_resource) {
[email protected]cce34bd2013-12-02 23:24:451720 EXPECT_EQ(2, layer_tree_host()->source_frame_number());
1721 EndTest();
[email protected]4bad8b62013-10-24 01:27:291722 }
1723
1724 virtual void SetupTree() OVERRIDE {
1725 scoped_refptr<Layer> root = Layer::Create();
1726 root->SetBounds(gfx::Size(10, 10));
1727 root->SetAnchorPoint(gfx::PointF());
1728 root->SetIsDrawable(true);
1729
1730 solid_layer_ = SolidColorLayer::Create();
1731 solid_layer_->SetBounds(gfx::Size(10, 10));
1732 solid_layer_->SetIsDrawable(true);
1733 solid_layer_->SetBackgroundColor(SK_ColorWHITE);
1734 root->AddChild(solid_layer_);
1735
1736 parent_layer_ = Layer::Create();
1737 parent_layer_->SetBounds(gfx::Size(10, 10));
1738 parent_layer_->SetIsDrawable(true);
1739 root->AddChild(parent_layer_);
1740
1741 texture_layer_ = TextureLayer::CreateForMailbox(this);
1742 texture_layer_->SetBounds(gfx::Size(10, 10));
1743 texture_layer_->SetAnchorPoint(gfx::PointF());
1744 texture_layer_->SetIsDrawable(true);
1745 parent_layer_->AddChild(texture_layer_);
1746
1747 layer_tree_host()->SetRootLayer(root);
1748 LayerTreeTest::SetupTree();
1749 }
1750
1751 virtual void BeginTest() OVERRIDE {
1752 PostSetNeedsCommitToMainThread();
1753 }
1754
[email protected]cce34bd2013-12-02 23:24:451755 virtual void DidCommitAndDrawFrame() OVERRIDE {
[email protected]4bad8b62013-10-24 01:27:291756 switch (layer_tree_host()->source_frame_number()) {
1757 case 1:
[email protected]cce34bd2013-12-02 23:24:451758 EXPECT_FALSE(proxy()->CommitPendingForTesting());
1759 // Invalidate the texture layer to clear the mailbox before
1760 // ending the test.
1761 texture_layer_->SetNeedsDisplay();
1762 break;
1763 case 2:
[email protected]4bad8b62013-10-24 01:27:291764 break;
1765 default:
1766 NOTREACHED();
1767 break;
1768 }
1769 }
1770
[email protected]cce34bd2013-12-02 23:24:451771 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1772 bool result) OVERRIDE {
1773 ASSERT_TRUE(result);
1774 DelegatedFrameData* delegated_frame_data =
1775 output_surface()->last_sent_frame().delegated_frame_data.get();
1776 if (!delegated_frame_data)
1777 return;
1778
1779 // Return all resources immediately.
1780 TransferableResourceArray resources_to_return =
1781 output_surface()->resources_held_by_parent();
1782
1783 CompositorFrameAck ack;
1784 for (size_t i = 0; i < resources_to_return.size(); ++i)
1785 output_surface()->ReturnResource(resources_to_return[i].id, &ack);
1786 host_impl->ReclaimResources(&ack);
1787 host_impl->OnSwapBuffersComplete();
1788 }
1789
[email protected]4bad8b62013-10-24 01:27:291790 virtual void AfterTest() OVERRIDE {}
1791
1792 private:
1793 scoped_refptr<SolidColorLayer> solid_layer_;
1794 scoped_refptr<Layer> parent_layer_;
1795 scoped_refptr<TextureLayer> texture_layer_;
[email protected]4bad8b62013-10-24 01:27:291796};
1797
[email protected]cce34bd2013-12-02 23:24:451798SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerNoExtraCommitForMailboxTest);
[email protected]4bad8b62013-10-24 01:27:291799
[email protected]b04264f92013-09-13 23:37:291800// Checks that changing a mailbox in the client for a TextureLayer that's
1801// invisible correctly works and uses the new mailbox as soon as the layer
1802// becomes visible (and returns the old one).
1803class TextureLayerChangeInvisibleMailboxTest
1804 : public LayerTreeTest,
1805 public TextureLayerClient {
1806 public:
1807 TextureLayerChangeInvisibleMailboxTest()
1808 : mailbox_changed_(true),
1809 mailbox_returned_(0),
1810 prepare_called_(0),
1811 commit_count_(0) {
1812 mailbox_ = MakeMailbox('1');
1813 }
1814
1815 // TextureLayerClient implementation.
1816 virtual unsigned PrepareTexture() OVERRIDE {
1817 NOTREACHED();
1818 return 0;
1819 }
1820
[email protected]b04264f92013-09-13 23:37:291821 virtual bool PrepareTextureMailbox(
[email protected]e51444a2013-12-10 23:05:011822 TextureMailbox* mailbox,
[email protected]9260757f2013-09-17 01:24:161823 scoped_ptr<SingleReleaseCallback>* release_callback,
1824 bool use_shared_memory) OVERRIDE {
[email protected]b04264f92013-09-13 23:37:291825 ++prepare_called_;
1826 if (!mailbox_changed_)
1827 return false;
1828 *mailbox = mailbox_;
[email protected]9260757f2013-09-17 01:24:161829 *release_callback = SingleReleaseCallback::Create(
1830 base::Bind(&TextureLayerChangeInvisibleMailboxTest::MailboxReleased,
1831 base::Unretained(this)));
[email protected]b04264f92013-09-13 23:37:291832 return true;
1833 }
1834
1835 TextureMailbox MakeMailbox(char name) {
[email protected]9ee234342014-01-18 02:42:261836 return TextureMailbox(
1837 MailboxFromString(std::string(64, name)), GL_TEXTURE_2D, 0);
[email protected]b04264f92013-09-13 23:37:291838 }
1839
[email protected]9ee234342014-01-18 02:42:261840 void MailboxReleased(uint32 sync_point, bool lost_resource) {
[email protected]b04264f92013-09-13 23:37:291841 ++mailbox_returned_;
1842 }
1843
1844 virtual void SetupTree() OVERRIDE {
1845 scoped_refptr<Layer> root = Layer::Create();
1846 root->SetBounds(gfx::Size(10, 10));
1847 root->SetAnchorPoint(gfx::PointF());
1848 root->SetIsDrawable(true);
1849
1850 solid_layer_ = SolidColorLayer::Create();
1851 solid_layer_->SetBounds(gfx::Size(10, 10));
1852 solid_layer_->SetIsDrawable(true);
1853 solid_layer_->SetBackgroundColor(SK_ColorWHITE);
1854 root->AddChild(solid_layer_);
1855
1856 parent_layer_ = Layer::Create();
1857 parent_layer_->SetBounds(gfx::Size(10, 10));
1858 parent_layer_->SetIsDrawable(true);
1859 root->AddChild(parent_layer_);
1860
1861 texture_layer_ = TextureLayer::CreateForMailbox(this);
1862 texture_layer_->SetBounds(gfx::Size(10, 10));
1863 texture_layer_->SetAnchorPoint(gfx::PointF());
1864 texture_layer_->SetIsDrawable(true);
1865 parent_layer_->AddChild(texture_layer_);
1866
1867 layer_tree_host()->SetRootLayer(root);
1868 LayerTreeTest::SetupTree();
1869 }
1870
1871 virtual void BeginTest() OVERRIDE {
1872 PostSetNeedsCommitToMainThread();
1873 }
1874
1875 virtual void DidCommitAndDrawFrame() OVERRIDE {
1876 ++commit_count_;
1877 switch (commit_count_) {
1878 case 1:
1879 // We should have updated the layer, committing the texture.
1880 EXPECT_EQ(1, prepare_called_);
1881 // Make layer invisible.
1882 parent_layer_->SetOpacity(0.f);
1883 break;
1884 case 2:
1885 // Layer shouldn't have been updated.
1886 EXPECT_EQ(1, prepare_called_);
1887 // Change the texture.
1888 mailbox_ = MakeMailbox('2');
1889 mailbox_changed_ = true;
1890 texture_layer_->SetNeedsDisplay();
1891 // Force a change to make sure we draw a frame.
1892 solid_layer_->SetBackgroundColor(SK_ColorGRAY);
1893 break;
1894 case 3:
1895 // Layer shouldn't have been updated.
1896 EXPECT_EQ(1, prepare_called_);
1897 // So the old mailbox isn't returned yet.
1898 EXPECT_EQ(0, mailbox_returned_);
1899 // Make layer visible again.
1900 parent_layer_->SetOpacity(1.f);
1901 break;
1902 case 4:
1903 // Layer should have been updated.
1904 EXPECT_EQ(2, prepare_called_);
1905 // So the old mailbox should have been returned already.
1906 EXPECT_EQ(1, mailbox_returned_);
1907 texture_layer_->ClearClient();
1908 break;
1909 case 5:
1910 EXPECT_EQ(2, mailbox_returned_);
1911 EndTest();
1912 break;
1913 default:
1914 NOTREACHED();
1915 break;
1916 }
1917 }
1918
1919 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1920 bool result) OVERRIDE {
1921 ASSERT_TRUE(result);
1922 DelegatedFrameData* delegated_frame_data =
1923 output_surface()->last_sent_frame().delegated_frame_data.get();
1924 if (!delegated_frame_data)
1925 return;
1926
1927 // Return all resources immediately.
1928 TransferableResourceArray resources_to_return =
1929 output_surface()->resources_held_by_parent();
1930
1931 CompositorFrameAck ack;
1932 for (size_t i = 0; i < resources_to_return.size(); ++i)
1933 output_surface()->ReturnResource(resources_to_return[i].id, &ack);
[email protected]a7335e0b2013-09-18 09:34:511934 host_impl->ReclaimResources(&ack);
1935 host_impl->OnSwapBuffersComplete();
[email protected]b04264f92013-09-13 23:37:291936 }
1937
1938 virtual void AfterTest() OVERRIDE {}
1939
1940 private:
1941 scoped_refptr<SolidColorLayer> solid_layer_;
1942 scoped_refptr<Layer> parent_layer_;
1943 scoped_refptr<TextureLayer> texture_layer_;
1944
1945 // Used on the main thread.
1946 bool mailbox_changed_;
1947 TextureMailbox mailbox_;
1948 int mailbox_returned_;
1949 int prepare_called_;
1950 int commit_count_;
1951};
1952
1953SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerChangeInvisibleMailboxTest);
1954
[email protected]0bf5a202013-07-10 14:50:541955// Test recovering from a lost context.
1956class TextureLayerLostContextTest
1957 : public LayerTreeTest,
1958 public TextureLayerClient {
1959 public:
1960 TextureLayerLostContextTest()
[email protected]f5931d42013-11-06 19:44:571961 : context_lost_(false),
[email protected]0bf5a202013-07-10 14:50:541962 draw_count_(0) {}
1963
[email protected]ebc0e1df2013-08-01 02:46:221964 virtual scoped_ptr<OutputSurface> CreateOutputSurface(bool fallback)
1965 OVERRIDE {
[email protected]3f6a906c2013-07-17 01:03:261966 return CreateFakeOutputSurface();
[email protected]0bf5a202013-07-10 14:50:541967 }
1968
[email protected]171cbb32013-07-11 03:51:191969 virtual unsigned PrepareTexture() OVERRIDE {
[email protected]f5931d42013-11-06 19:44:571970 if (draw_count_ == 0)
1971 context_lost_ = true;
1972 if (context_lost_)
1973 return 0u;
1974 return 1u;
[email protected]0bf5a202013-07-10 14:50:541975 }
1976
1977 virtual bool PrepareTextureMailbox(
[email protected]9260757f2013-09-17 01:24:161978 TextureMailbox* mailbox,
1979 scoped_ptr<SingleReleaseCallback>* release_callback,
1980 bool use_shared_memory) OVERRIDE {
[email protected]0bf5a202013-07-10 14:50:541981 return false;
1982 }
1983
1984 virtual void SetupTree() OVERRIDE {
1985 scoped_refptr<Layer> root = Layer::Create();
1986 root->SetBounds(gfx::Size(10, 10));
1987 root->SetIsDrawable(true);
1988
1989 texture_layer_ = TextureLayer::Create(this);
1990 texture_layer_->SetBounds(gfx::Size(10, 10));
1991 texture_layer_->SetIsDrawable(true);
1992 root->AddChild(texture_layer_);
1993
1994 layer_tree_host()->SetRootLayer(root);
1995 LayerTreeTest::SetupTree();
1996 }
1997
1998 virtual void BeginTest() OVERRIDE {
1999 PostSetNeedsCommitToMainThread();
2000 }
2001
2002 virtual bool PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
2003 LayerTreeHostImpl::FrameData* frame_data,
2004 bool result) OVERRIDE {
2005 LayerImpl* root = host_impl->RootLayer();
2006 TextureLayerImpl* texture_layer =
2007 static_cast<TextureLayerImpl*>(root->children()[0]);
2008 if (++draw_count_ == 1)
2009 EXPECT_EQ(0u, texture_layer->texture_id());
2010 else
[email protected]f5931d42013-11-06 19:44:572011 EXPECT_EQ(1u, texture_layer->texture_id());
[email protected]0bf5a202013-07-10 14:50:542012 return true;
2013 }
2014
2015 virtual void DidCommitAndDrawFrame() OVERRIDE {
2016 EndTest();
2017 }
2018
2019 virtual void AfterTest() OVERRIDE {}
2020
2021 private:
2022 scoped_refptr<TextureLayer> texture_layer_;
[email protected]f5931d42013-11-06 19:44:572023 bool context_lost_;
[email protected]0bf5a202013-07-10 14:50:542024 int draw_count_;
2025};
2026
2027SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TextureLayerLostContextTest);
2028
[email protected]9c2bd822013-07-26 12:30:172029class TextureLayerWithMailboxMainThreadDeleted : public LayerTreeTest {
2030 public:
[email protected]9ee234342014-01-18 02:42:262031 void ReleaseCallback(uint32 sync_point, bool lost_resource) {
[email protected]9794fb32013-08-29 09:49:592032 EXPECT_EQ(true, main_thread_.CalledOnValidThread());
[email protected]9c2bd822013-07-26 12:30:172033 EXPECT_FALSE(lost_resource);
2034 ++callback_count_;
2035 EndTest();
2036 }
2037
2038 void SetMailbox(char mailbox_char) {
[email protected]9794fb32013-08-29 09:49:592039 EXPECT_EQ(true, main_thread_.CalledOnValidThread());
[email protected]9260757f2013-09-17 01:24:162040 scoped_ptr<SingleReleaseCallback> callback = SingleReleaseCallback::Create(
[email protected]9c2bd822013-07-26 12:30:172041 base::Bind(
2042 &TextureLayerWithMailboxMainThreadDeleted::ReleaseCallback,
2043 base::Unretained(this)));
[email protected]9ee234342014-01-18 02:42:262044 layer_->SetTextureMailbox(
2045 TextureMailbox(
2046 MailboxFromString(std::string(64, mailbox_char)), GL_TEXTURE_2D, 0),
2047 callback.Pass());
[email protected]9c2bd822013-07-26 12:30:172048 }
2049
2050 virtual void SetupTree() OVERRIDE {
2051 gfx::Size bounds(100, 100);
2052 root_ = Layer::Create();
2053 root_->SetAnchorPoint(gfx::PointF());
2054 root_->SetBounds(bounds);
2055
2056 layer_ = TextureLayer::CreateForMailbox(NULL);
2057 layer_->SetIsDrawable(true);
2058 layer_->SetAnchorPoint(gfx::PointF());
2059 layer_->SetBounds(bounds);
2060
2061 root_->AddChild(layer_);
2062 layer_tree_host()->SetRootLayer(root_);
2063 layer_tree_host()->SetViewportSize(bounds);
2064 }
2065
2066 virtual void BeginTest() OVERRIDE {
[email protected]9794fb32013-08-29 09:49:592067 EXPECT_EQ(true, main_thread_.CalledOnValidThread());
2068
[email protected]9c2bd822013-07-26 12:30:172069 callback_count_ = 0;
2070
2071 // Set the mailbox on the main thread.
2072 SetMailbox('1');
2073 EXPECT_EQ(0, callback_count_);
2074
2075 PostSetNeedsCommitToMainThread();
2076 }
2077
2078 virtual void DidCommitAndDrawFrame() OVERRIDE {
2079 switch (layer_tree_host()->source_frame_number()) {
2080 case 1:
2081 // Delete the TextureLayer on the main thread while the mailbox is in
2082 // the impl tree.
2083 layer_->RemoveFromParent();
2084 layer_ = NULL;
2085 break;
2086 }
2087 }
2088
2089 virtual void AfterTest() OVERRIDE {
2090 EXPECT_EQ(1, callback_count_);
2091 }
2092
2093 private:
[email protected]9794fb32013-08-29 09:49:592094 base::ThreadChecker main_thread_;
[email protected]9c2bd822013-07-26 12:30:172095 int callback_count_;
2096 scoped_refptr<Layer> root_;
2097 scoped_refptr<TextureLayer> layer_;
2098};
2099
2100SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(
2101 TextureLayerWithMailboxMainThreadDeleted);
2102
2103class TextureLayerWithMailboxImplThreadDeleted : public LayerTreeTest {
2104 public:
[email protected]9ee234342014-01-18 02:42:262105 void ReleaseCallback(uint32 sync_point, bool lost_resource) {
[email protected]9794fb32013-08-29 09:49:592106 EXPECT_EQ(true, main_thread_.CalledOnValidThread());
[email protected]9c2bd822013-07-26 12:30:172107 EXPECT_FALSE(lost_resource);
2108 ++callback_count_;
2109 EndTest();
2110 }
2111
2112 void SetMailbox(char mailbox_char) {
[email protected]9794fb32013-08-29 09:49:592113 EXPECT_EQ(true, main_thread_.CalledOnValidThread());
[email protected]9260757f2013-09-17 01:24:162114 scoped_ptr<SingleReleaseCallback> callback = SingleReleaseCallback::Create(
[email protected]9c2bd822013-07-26 12:30:172115 base::Bind(
2116 &TextureLayerWithMailboxImplThreadDeleted::ReleaseCallback,
2117 base::Unretained(this)));
[email protected]9ee234342014-01-18 02:42:262118 layer_->SetTextureMailbox(
2119 TextureMailbox(
2120 MailboxFromString(std::string(64, mailbox_char)), GL_TEXTURE_2D, 0),
2121 callback.Pass());
[email protected]9c2bd822013-07-26 12:30:172122 }
2123
2124 virtual void SetupTree() OVERRIDE {
2125 gfx::Size bounds(100, 100);
2126 root_ = Layer::Create();
2127 root_->SetAnchorPoint(gfx::PointF());
2128 root_->SetBounds(bounds);
2129
2130 layer_ = TextureLayer::CreateForMailbox(NULL);
2131 layer_->SetIsDrawable(true);
2132 layer_->SetAnchorPoint(gfx::PointF());
2133 layer_->SetBounds(bounds);
2134
2135 root_->AddChild(layer_);
2136 layer_tree_host()->SetRootLayer(root_);
2137 layer_tree_host()->SetViewportSize(bounds);
2138 }
2139
2140 virtual void BeginTest() OVERRIDE {
[email protected]9794fb32013-08-29 09:49:592141 EXPECT_EQ(true, main_thread_.CalledOnValidThread());
2142
[email protected]9c2bd822013-07-26 12:30:172143 callback_count_ = 0;
2144
2145 // Set the mailbox on the main thread.
2146 SetMailbox('1');
2147 EXPECT_EQ(0, callback_count_);
2148
2149 PostSetNeedsCommitToMainThread();
2150 }
2151
2152 virtual void DidCommitAndDrawFrame() OVERRIDE {
2153 switch (layer_tree_host()->source_frame_number()) {
2154 case 1:
2155 // Remove the TextureLayer on the main thread while the mailbox is in
2156 // the impl tree, but don't delete the TextureLayer until after the impl
2157 // tree side is deleted.
2158 layer_->RemoveFromParent();
2159 break;
2160 case 2:
2161 layer_ = NULL;
2162 break;
2163 }
2164 }
2165
2166 virtual void AfterTest() OVERRIDE {
2167 EXPECT_EQ(1, callback_count_);
2168 }
2169
2170 private:
[email protected]9794fb32013-08-29 09:49:592171 base::ThreadChecker main_thread_;
[email protected]9c2bd822013-07-26 12:30:172172 int callback_count_;
2173 scoped_refptr<Layer> root_;
2174 scoped_refptr<TextureLayer> layer_;
2175};
2176
2177SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(
2178 TextureLayerWithMailboxImplThreadDeleted);
2179
[email protected]ba565742012-11-10 09:29:482180} // namespace
2181} // namespace cc