blob: f03e04b76f6f30d05f62d9a61abfa60398f30728 [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
avi02a4d172015-12-21 06:14:367#include <stddef.h>
8#include <stdint.h>
9
[email protected]b04264f92013-09-13 23:37:2910#include <algorithm>
[email protected]de44a152013-01-08 15:28:4611#include <string>
12
[email protected]b04264f92013-09-13 23:37:2913#include "base/bind.h"
[email protected]de44a152013-01-08 15:28:4614#include "base/callback.h"
skyostil0fd1dad2015-04-13 20:11:4815#include "base/location.h"
avi02a4d172015-12-21 06:14:3616#include "base/macros.h"
danakj60bc3bc2016-04-09 00:24:4817#include "base/memory/ptr_util.h"
skyostil0fd1dad2015-04-13 20:11:4818#include "base/single_thread_task_runner.h"
danakj3c3973b2015-08-25 21:50:1819#include "base/synchronization/lock.h"
[email protected]9794fb32013-08-29 09:49:5920#include "base/synchronization/waitable_event.h"
[email protected]74b43cc2013-08-30 06:29:2721#include "base/threading/thread.h"
gab39cb4ed72016-05-11 17:45:1922#include "base/threading/thread_task_runner_handle.h"
[email protected]74b43cc2013-08-30 06:29:2723#include "base/time/time.h"
loysoab32ee72016-06-08 03:33:1824#include "cc/animation/animation_host.h"
[email protected]b04264f92013-09-13 23:37:2925#include "cc/layers/solid_color_layer.h"
[email protected]97d519fb2013-03-29 02:27:5426#include "cc/layers/texture_layer_client.h"
[email protected]cc3cfaa2013-03-18 09:05:5227#include "cc/layers/texture_layer_impl.h"
[email protected]b04264f92013-09-13 23:37:2928#include "cc/output/context_provider.h"
[email protected]e00bab022013-08-19 00:42:4529#include "cc/resources/returned_resource.h"
khushalsagarb64b360d2015-10-21 19:25:1630#include "cc/test/fake_impl_task_runner_provider.h"
[email protected]101441ce2012-10-16 01:45:0331#include "cc/test/fake_layer_tree_host_client.h"
[email protected]586d51ed2012-12-07 20:31:4532#include "cc/test/fake_layer_tree_host_impl.h"
[email protected]199b715e2013-08-13 05:18:3433#include "cc/test/fake_output_surface.h"
[email protected]06d68d02013-04-19 18:46:2134#include "cc/test/layer_test_common.h"
[email protected]e216fef02013-03-20 22:56:1035#include "cc/test/layer_tree_test.h"
danakjffc181a2016-07-22 22:48:4336#include "cc/test/stub_layer_tree_host_single_thread_client.h"
danakj014316e2016-08-04 18:40:2637#include "cc/test/test_delegating_output_surface.h"
reveman34b7a1522015-03-23 20:27:4738#include "cc/test/test_task_graph_runner.h"
[email protected]c2610b9f2013-10-31 06:54:5939#include "cc/test/test_web_graphics_context_3d.h"
[email protected]9794fb32013-08-29 09:49:5940#include "cc/trees/blocking_task_runner.h"
[email protected]556fd292013-03-18 08:03:0441#include "cc/trees/layer_tree_host.h"
42#include "cc/trees/layer_tree_impl.h"
43#include "cc/trees/single_thread_proxy.h"
[email protected]0bf5a202013-07-10 14:50:5444#include "gpu/GLES2/gl2extchromium.h"
[email protected]7f0c53db2012-10-02 00:23:1845#include "testing/gmock/include/gmock/gmock.h"
46#include "testing/gtest/include/gtest/gtest.h"
[email protected]c0dd24c2012-08-30 23:25:2747
[email protected]c0dd24c2012-08-30 23:25:2748using ::testing::Mock;
49using ::testing::_;
50using ::testing::AtLeast;
51using ::testing::AnyNumber;
[email protected]d72d9e02014-04-03 18:40:0952using ::testing::InvokeWithoutArgs;
[email protected]c0dd24c2012-08-30 23:25:2753
[email protected]ba565742012-11-10 09:29:4854namespace cc {
[email protected]c0dd24c2012-08-30 23:25:2755namespace {
56
[email protected]e0a4d732014-02-15 00:23:2657gpu::Mailbox MailboxFromChar(char value) {
[email protected]df41e252014-02-03 23:39:5058 gpu::Mailbox mailbox;
[email protected]e0a4d732014-02-15 00:23:2659 memset(mailbox.name, value, sizeof(mailbox.name));
[email protected]df41e252014-02-03 23:39:5060 return mailbox;
61}
62
dyen398dd0142016-01-21 22:05:5663gpu::SyncToken SyncTokenFromUInt(uint32_t value) {
lukasza2573ce7d2016-02-16 19:17:2264 return gpu::SyncToken(gpu::CommandBufferNamespace::GPU_IO, 0,
65 gpu::CommandBufferId::FromUnsafeValue(0x123), value);
dyen398dd0142016-01-21 22:05:5666}
67
[email protected]408b5e22013-03-19 09:48:0968class MockLayerTreeHost : public LayerTreeHost {
[email protected]28571b042013-03-14 07:59:1569 public:
danakj60bc3bc2016-04-09 00:24:4870 static std::unique_ptr<MockLayerTreeHost> Create(
danakjcf610582015-06-16 22:48:5671 FakeLayerTreeHostClient* client,
72 TaskGraphRunner* task_graph_runner) {
sadrul6780f3da2015-05-11 17:01:5273 LayerTreeHost::InitParams params;
74 params.client = client;
danakjcf610582015-06-16 22:48:5675 params.task_graph_runner = task_graph_runner;
loysoab32ee72016-06-08 03:33:1876 params.animation_host =
77 AnimationHost::CreateForTesting(ThreadInstance::MAIN);
sadrul6780f3da2015-05-11 17:01:5278 LayerTreeSettings settings;
jaydasika853306a2016-07-29 19:28:3479 settings.verify_transform_tree_calculations = true;
80 settings.verify_clip_tree_calculations = true;
sadrul6780f3da2015-05-11 17:01:5281 params.settings = &settings;
danakjffc181a2016-07-22 22:48:4382 return base::WrapUnique(new MockLayerTreeHost(&params));
[email protected]28571b042013-03-14 07:59:1583 }
[email protected]c0dd24c2012-08-30 23:25:2784
[email protected]28571b042013-03-14 07:59:1585 MOCK_METHOD0(SetNeedsCommit, void());
[email protected]3519b872013-07-30 07:17:5086 MOCK_METHOD0(SetNeedsUpdateLayers, void());
[email protected]aeeb3372013-11-05 14:05:5487 MOCK_METHOD0(StartRateLimiter, void());
88 MOCK_METHOD0(StopRateLimiter, void());
sadrul6780f3da2015-05-11 17:01:5289
90 private:
danakjffc181a2016-07-22 22:48:4391 explicit MockLayerTreeHost(LayerTreeHost::InitParams* params)
khushalsagare0a38d42016-01-29 01:15:0692 : LayerTreeHost(params, CompositorMode::SINGLE_THREADED) {
danakjffc181a2016-07-22 22:48:4393 InitializeSingleThreaded(&single_thread_client_,
94 base::ThreadTaskRunnerHandle::Get(), nullptr);
sadrul6780f3da2015-05-11 17:01:5295 }
danakjffc181a2016-07-22 22:48:4396
97 StubLayerTreeHostSingleThreadClient single_thread_client_;
[email protected]c0dd24c2012-08-30 23:25:2798};
99
[email protected]d72d9e02014-04-03 18:40:09100class FakeTextureLayerClient : public TextureLayerClient {
101 public:
[email protected]17e08432014-04-10 00:41:11102 FakeTextureLayerClient() : mailbox_changed_(true) {}
[email protected]d72d9e02014-04-03 18:40:09103
dcheng716bedf2014-10-21 09:51:08104 bool PrepareTextureMailbox(
[email protected]d72d9e02014-04-03 18:40:09105 TextureMailbox* mailbox,
danakj4d0dd802016-08-23 22:10:06106 std::unique_ptr<SingleReleaseCallback>* release_callback) override {
[email protected]d72d9e02014-04-03 18:40:09107 if (!mailbox_changed_)
108 return false;
109
110 *mailbox = mailbox_;
danakja04855a2015-11-18 20:39:10111 *release_callback = std::move(release_callback_);
[email protected]d72d9e02014-04-03 18:40:09112 mailbox_changed_ = false;
113 return true;
114 }
115
[email protected]d72d9e02014-04-03 18:40:09116 void set_mailbox(const TextureMailbox& mailbox,
danakj60bc3bc2016-04-09 00:24:48117 std::unique_ptr<SingleReleaseCallback> release_callback) {
[email protected]d72d9e02014-04-03 18:40:09118 mailbox_ = mailbox;
danakja04855a2015-11-18 20:39:10119 release_callback_ = std::move(release_callback);
[email protected]d72d9e02014-04-03 18:40:09120 mailbox_changed_ = true;
121 }
122
123 private:
[email protected]d72d9e02014-04-03 18:40:09124 TextureMailbox mailbox_;
danakj60bc3bc2016-04-09 00:24:48125 std::unique_ptr<SingleReleaseCallback> release_callback_;
[email protected]d72d9e02014-04-03 18:40:09126 bool mailbox_changed_;
127 DISALLOW_COPY_AND_ASSIGN(FakeTextureLayerClient);
128};
129
130class MockMailboxCallback {
131 public:
132 MOCK_METHOD3(Release,
133 void(const gpu::Mailbox& mailbox,
dyencc16ed4d2015-11-03 20:03:04134 const gpu::SyncToken& sync_token,
[email protected]d72d9e02014-04-03 18:40:09135 bool lost_resource));
136 MOCK_METHOD3(Release2,
jbauman9015c8b2014-12-11 00:49:37137 void(SharedBitmap* shared_bitmap,
dyencc16ed4d2015-11-03 20:03:04138 const gpu::SyncToken& sync_token,
[email protected]d72d9e02014-04-03 18:40:09139 bool lost_resource));
skyostil3976a3f2014-09-04 22:07:23140 MOCK_METHOD4(ReleaseImpl,
141 void(const gpu::Mailbox& mailbox,
dyencc16ed4d2015-11-03 20:03:04142 const gpu::SyncToken& sync_token,
skyostil3976a3f2014-09-04 22:07:23143 bool lost_resource,
144 BlockingTaskRunner* main_thread_task_runner));
145 MOCK_METHOD4(ReleaseImpl2,
jbauman9015c8b2014-12-11 00:49:37146 void(SharedBitmap* shared_bitmap,
dyencc16ed4d2015-11-03 20:03:04147 const gpu::SyncToken& sync_token,
skyostil3976a3f2014-09-04 22:07:23148 bool lost_resource,
149 BlockingTaskRunner* main_thread_task_runner));
[email protected]d72d9e02014-04-03 18:40:09150};
151
152struct CommonMailboxObjects {
jbauman9015c8b2014-12-11 00:49:37153 explicit CommonMailboxObjects(SharedBitmapManager* manager)
[email protected]d72d9e02014-04-03 18:40:09154 : mailbox_name1_(MailboxFromChar('1')),
155 mailbox_name2_(MailboxFromChar('2')),
lukasza2573ce7d2016-02-16 19:17:22156 sync_token1_(gpu::CommandBufferNamespace::GPU_IO,
157 123,
158 gpu::CommandBufferId::FromUnsafeValue(0x234),
159 1),
160 sync_token2_(gpu::CommandBufferNamespace::GPU_IO,
161 123,
162 gpu::CommandBufferId::FromUnsafeValue(0x234),
163 2) {
[email protected]d72d9e02014-04-03 18:40:09164 release_mailbox1_ = base::Bind(&MockMailboxCallback::Release,
165 base::Unretained(&mock_callback_),
166 mailbox_name1_);
167 release_mailbox2_ = base::Bind(&MockMailboxCallback::Release,
168 base::Unretained(&mock_callback_),
169 mailbox_name2_);
skyostil3976a3f2014-09-04 22:07:23170 release_mailbox1_impl_ = base::Bind(&MockMailboxCallback::ReleaseImpl,
171 base::Unretained(&mock_callback_),
172 mailbox_name1_);
173 release_mailbox2_impl_ = base::Bind(&MockMailboxCallback::ReleaseImpl,
174 base::Unretained(&mock_callback_),
175 mailbox_name2_);
avi02a4d172015-12-21 06:14:36176 const uint32_t arbitrary_target1 = GL_TEXTURE_2D;
177 const uint32_t arbitrary_target2 = GL_TEXTURE_EXTERNAL_OES;
dyencc16ed4d2015-11-03 20:03:04178 mailbox1_ = TextureMailbox(mailbox_name1_, sync_token1_, arbitrary_target1);
179 mailbox2_ = TextureMailbox(mailbox_name2_, sync_token2_, arbitrary_target2);
[email protected]d72d9e02014-04-03 18:40:09180 gfx::Size size(128, 128);
jbauman9015c8b2014-12-11 00:49:37181 shared_bitmap_ = manager->AllocateSharedBitmap(size);
182 DCHECK(shared_bitmap_);
183 release_mailbox3_ =
184 base::Bind(&MockMailboxCallback::Release2,
185 base::Unretained(&mock_callback_), shared_bitmap_.get());
186 release_mailbox3_impl_ =
187 base::Bind(&MockMailboxCallback::ReleaseImpl2,
188 base::Unretained(&mock_callback_), shared_bitmap_.get());
189 mailbox3_ = TextureMailbox(shared_bitmap_.get(), size);
[email protected]d72d9e02014-04-03 18:40:09190 }
191
192 gpu::Mailbox mailbox_name1_;
193 gpu::Mailbox mailbox_name2_;
194 MockMailboxCallback mock_callback_;
195 ReleaseCallback release_mailbox1_;
196 ReleaseCallback release_mailbox2_;
197 ReleaseCallback release_mailbox3_;
skyostil3976a3f2014-09-04 22:07:23198 ReleaseCallbackImpl release_mailbox1_impl_;
199 ReleaseCallbackImpl release_mailbox2_impl_;
200 ReleaseCallbackImpl release_mailbox3_impl_;
[email protected]d72d9e02014-04-03 18:40:09201 TextureMailbox mailbox1_;
202 TextureMailbox mailbox2_;
203 TextureMailbox mailbox3_;
dyencc16ed4d2015-11-03 20:03:04204 gpu::SyncToken sync_token1_;
205 gpu::SyncToken sync_token2_;
danakj60bc3bc2016-04-09 00:24:48206 std::unique_ptr<SharedBitmap> shared_bitmap_;
[email protected]d72d9e02014-04-03 18:40:09207};
208
[email protected]31d4df82013-07-18 10:17:22209class TextureLayerTest : public testing::Test {
210 public:
211 TextureLayerTest()
danakj6021ec32016-07-22 22:16:08212 : output_surface_(FakeOutputSurface::CreateDelegating3d()),
khushalsagarb64b360d2015-10-21 19:25:16213 host_impl_(&task_runner_provider_,
214 &shared_bitmap_manager_,
215 &task_graph_runner_),
jbauman9015c8b2014-12-11 00:49:37216 test_data_(&shared_bitmap_manager_) {}
[email protected]31d4df82013-07-18 10:17:22217
218 protected:
dcheng93a52eb2014-12-23 02:14:23219 void SetUp() override {
danakjcf610582015-06-16 22:48:56220 layer_tree_host_ =
221 MockLayerTreeHost::Create(&fake_client_, &task_graph_runner_);
khushalsagar86928f92016-08-17 21:49:05222 layer_tree_ = layer_tree_host_->GetLayerTree();
[email protected]d72d9e02014-04-03 18:40:09223 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
khushalsagar86928f92016-08-17 21:49:05224 layer_tree_->SetViewportSize(gfx::Size(10, 10));
[email protected]d72d9e02014-04-03 18:40:09225 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]31d4df82013-07-18 10:17:22226 }
227
dcheng93a52eb2014-12-23 02:14:23228 void TearDown() override {
[email protected]31d4df82013-07-18 10:17:22229 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]31d4df82013-07-18 10:17:22230 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
231
khushalsagar86928f92016-08-17 21:49:05232 layer_tree_->SetRootLayer(nullptr);
danakjf446a072014-09-27 21:55:48233 layer_tree_host_ = nullptr;
[email protected]31d4df82013-07-18 10:17:22234 }
235
danakj60bc3bc2016-04-09 00:24:48236 std::unique_ptr<MockLayerTreeHost> layer_tree_host_;
khushalsagar86928f92016-08-17 21:49:05237 LayerTree* layer_tree_;
khushalsagarb64b360d2015-10-21 19:25:16238 FakeImplTaskRunnerProvider task_runner_provider_;
[email protected]31d4df82013-07-18 10:17:22239 FakeLayerTreeHostClient fake_client_;
[email protected]4e2eb352014-03-20 17:25:45240 TestSharedBitmapManager shared_bitmap_manager_;
reveman34b7a1522015-03-23 20:27:47241 TestTaskGraphRunner task_graph_runner_;
danakj60bc3bc2016-04-09 00:24:48242 std::unique_ptr<OutputSurface> output_surface_;
[email protected]31d4df82013-07-18 10:17:22243 FakeLayerTreeHostImpl host_impl_;
jbauman9015c8b2014-12-11 00:49:37244 CommonMailboxObjects test_data_;
[email protected]31d4df82013-07-18 10:17:22245};
246
[email protected]31d4df82013-07-18 10:17:22247TEST_F(TextureLayerTest, CheckPropertyChangeCausesCorrectBehavior) {
kulkarni.a4015690f12014-10-10 13:50:06248 scoped_refptr<TextureLayer> test_layer =
loyso0940d412016-03-14 01:30:31249 TextureLayer::CreateForMailbox(nullptr);
khushalsagar86928f92016-08-17 21:49:05250 EXPECT_SET_NEEDS_COMMIT(1, layer_tree_->SetRootLayer(test_layer));
[email protected]31d4df82013-07-18 10:17:22251
252 // Test properties that should call SetNeedsCommit. All properties need to
253 // be set to new values in order for SetNeedsCommit to be called.
254 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFlipped(false));
jackhou10c9af42014-12-04 05:24:44255 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetNearestNeighbor(true));
[email protected]31d4df82013-07-18 10:17:22256 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetUV(
257 gfx::PointF(0.25f, 0.25f), gfx::PointF(0.75f, 0.75f)));
258 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetVertexOpacity(
259 0.5f, 0.5f, 0.5f, 0.5f));
260 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetPremultipliedAlpha(false));
261 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBlendBackgroundColor(true));
[email protected]31d4df82013-07-18 10:17:22262}
263
[email protected]df41e252014-02-03 23:39:50264class TestMailboxHolder : public TextureLayer::TextureMailboxHolder {
[email protected]9794fb32013-08-29 09:49:59265 public:
[email protected]df41e252014-02-03 23:39:50266 using TextureLayer::TextureMailboxHolder::Create;
[email protected]9794fb32013-08-29 09:49:59267
268 protected:
dcheng716bedf2014-10-21 09:51:08269 ~TestMailboxHolder() override {}
[email protected]9794fb32013-08-29 09:49:59270};
271
[email protected]de44a152013-01-08 15:28:46272class TextureLayerWithMailboxTest : public TextureLayerTest {
[email protected]28571b042013-03-14 07:59:15273 protected:
dcheng93a52eb2014-12-23 02:14:23274 void TearDown() override {
[email protected]28571b042013-03-14 07:59:15275 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
dyencc16ed4d2015-11-03 20:03:04276 EXPECT_CALL(
277 test_data_.mock_callback_,
278 Release(test_data_.mailbox_name1_, test_data_.sync_token1_, false))
279 .Times(1);
[email protected]28571b042013-03-14 07:59:15280 TextureLayerTest::TearDown();
281 }
[email protected]de44a152013-01-08 15:28:46282};
283
[email protected]28571b042013-03-14 07:59:15284TEST_F(TextureLayerWithMailboxTest, ReplaceMailboxOnMainThreadBeforeCommit) {
kulkarni.a4015690f12014-10-10 13:50:06285 scoped_refptr<TextureLayer> test_layer =
loyso0940d412016-03-14 01:30:31286 TextureLayer::CreateForMailbox(nullptr);
[email protected]22898ed2013-06-01 04:52:30287 ASSERT_TRUE(test_layer.get());
[email protected]de44a152013-01-08 15:28:46288
[email protected]28571b042013-03-14 07:59:15289 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
khushalsagar86928f92016-08-17 21:49:05290 layer_tree_->SetRootLayer(test_layer);
[email protected]28571b042013-03-14 07:59:15291 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]de44a152013-01-08 15:28:46292
[email protected]28571b042013-03-14 07:59:15293 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
[email protected]9260757f2013-09-17 01:24:16294 test_layer->SetTextureMailbox(
295 test_data_.mailbox1_,
296 SingleReleaseCallback::Create(test_data_.release_mailbox1_));
[email protected]28571b042013-03-14 07:59:15297 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
[email protected]de44a152013-01-08 15:28:46298
[email protected]28571b042013-03-14 07:59:15299 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
dyencc16ed4d2015-11-03 20:03:04300 EXPECT_CALL(
301 test_data_.mock_callback_,
302 Release(test_data_.mailbox_name1_, test_data_.sync_token1_, false))
[email protected]28571b042013-03-14 07:59:15303 .Times(1);
[email protected]9260757f2013-09-17 01:24:16304 test_layer->SetTextureMailbox(
305 test_data_.mailbox2_,
306 SingleReleaseCallback::Create(test_data_.release_mailbox2_));
[email protected]28571b042013-03-14 07:59:15307 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
308 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]de44a152013-01-08 15:28:46309
[email protected]28571b042013-03-14 07:59:15310 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
dyencc16ed4d2015-11-03 20:03:04311 EXPECT_CALL(
312 test_data_.mock_callback_,
313 Release(test_data_.mailbox_name2_, test_data_.sync_token2_, false))
[email protected]28571b042013-03-14 07:59:15314 .Times(1);
danakj968153f32014-10-15 22:52:16315 test_layer->SetTextureMailbox(TextureMailbox(), nullptr);
[email protected]28571b042013-03-14 07:59:15316 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
317 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]de44a152013-01-08 15:28:46318
[email protected]80d42bd2013-08-30 19:13:45319 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
[email protected]9260757f2013-09-17 01:24:16320 test_layer->SetTextureMailbox(
321 test_data_.mailbox3_,
322 SingleReleaseCallback::Create(test_data_.release_mailbox3_));
[email protected]42f40a52013-06-08 04:38:51323 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
324 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
325
[email protected]42f40a52013-06-08 04:38:51326 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
327 EXPECT_CALL(test_data_.mock_callback_,
dyencc16ed4d2015-11-03 20:03:04328 Release2(test_data_.shared_bitmap_.get(), _, false))
329 .Times(1);
danakj968153f32014-10-15 22:52:16330 test_layer->SetTextureMailbox(TextureMailbox(), nullptr);
[email protected]42f40a52013-06-08 04:38:51331 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
332 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
333
[email protected]28571b042013-03-14 07:59:15334 // Test destructor.
335 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
[email protected]9260757f2013-09-17 01:24:16336 test_layer->SetTextureMailbox(
337 test_data_.mailbox1_,
338 SingleReleaseCallback::Create(test_data_.release_mailbox1_));
[email protected]de44a152013-01-08 15:28:46339}
340
[email protected]9794fb32013-08-29 09:49:59341class TextureLayerMailboxHolderTest : public TextureLayerTest {
342 public:
343 TextureLayerMailboxHolderTest()
344 : main_thread_("MAIN") {
345 main_thread_.Start();
fdoray70df5a92016-06-22 21:13:59346 main_thread_.task_runner()->PostTask(
skyostil0fd1dad2015-04-13 20:11:48347 FROM_HERE, base::Bind(&TextureLayerMailboxHolderTest::InitializeOnMain,
348 base::Unretained(this)));
skyostil3976a3f2014-09-04 22:07:23349 Wait(main_thread_);
[email protected]9794fb32013-08-29 09:49:59350 }
351
352 void Wait(const base::Thread& thread) {
gabcca53112016-06-08 20:13:28353 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC,
354 base::WaitableEvent::InitialState::NOT_SIGNALED);
fdoray70df5a92016-06-22 21:13:59355 thread.task_runner()->PostTask(
[email protected]9794fb32013-08-29 09:49:59356 FROM_HERE,
357 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event)));
358 event.Wait();
359 }
360
361 void CreateMainRef() {
362 main_ref_ = TestMailboxHolder::Create(
[email protected]9260757f2013-09-17 01:24:16363 test_data_.mailbox1_,
danakja5a05ba02015-11-20 20:14:21364 SingleReleaseCallback::Create(test_data_.release_mailbox1_));
[email protected]9794fb32013-08-29 09:49:59365 }
366
danakjf446a072014-09-27 21:55:48367 void ReleaseMainRef() { main_ref_ = nullptr; }
[email protected]9794fb32013-08-29 09:49:59368
danakj60bc3bc2016-04-09 00:24:48369 void CreateImplRef(std::unique_ptr<SingleReleaseCallbackImpl>* impl_ref) {
[email protected]9794fb32013-08-29 09:49:59370 *impl_ref = main_ref_->holder()->GetCallbackForImplThread();
371 }
372
373 void CapturePostTasksAndWait(base::WaitableEvent* begin_capture,
374 base::WaitableEvent* wait_for_capture,
375 base::WaitableEvent* stop_capture) {
376 begin_capture->Wait();
skyostil3976a3f2014-09-04 22:07:23377 BlockingTaskRunner::CapturePostTasks capture(
378 main_thread_task_runner_.get());
[email protected]9794fb32013-08-29 09:49:59379 wait_for_capture->Signal();
380 stop_capture->Wait();
381 }
382
383 protected:
skyostil3976a3f2014-09-04 22:07:23384 void InitializeOnMain() {
385 main_thread_task_runner_ =
skyostil0fd1dad2015-04-13 20:11:48386 BlockingTaskRunner::Create(main_thread_.task_runner());
skyostil3976a3f2014-09-04 22:07:23387 }
388
danakj60bc3bc2016-04-09 00:24:48389 std::unique_ptr<TestMailboxHolder::MainThreadReference> main_ref_;
[email protected]9794fb32013-08-29 09:49:59390 base::Thread main_thread_;
danakj60bc3bc2016-04-09 00:24:48391 std::unique_ptr<BlockingTaskRunner> main_thread_task_runner_;
[email protected]9794fb32013-08-29 09:49:59392};
393
394TEST_F(TextureLayerMailboxHolderTest, TwoCompositors_BothReleaseThenMain) {
kulkarni.a4015690f12014-10-10 13:50:06395 scoped_refptr<TextureLayer> test_layer =
loyso0940d412016-03-14 01:30:31396 TextureLayer::CreateForMailbox(nullptr);
[email protected]9794fb32013-08-29 09:49:59397 ASSERT_TRUE(test_layer.get());
398
fdoray70df5a92016-06-22 21:13:59399 main_thread_.task_runner()->PostTask(
skyostil0fd1dad2015-04-13 20:11:48400 FROM_HERE, base::Bind(&TextureLayerMailboxHolderTest::CreateMainRef,
401 base::Unretained(this)));
[email protected]9794fb32013-08-29 09:49:59402
403 Wait(main_thread_);
404
405 // The texture layer is attached to compositor1, and passes a reference to its
406 // impl tree.
danakj60bc3bc2016-04-09 00:24:48407 std::unique_ptr<SingleReleaseCallbackImpl> compositor1;
fdoray70df5a92016-06-22 21:13:59408 main_thread_.task_runner()->PostTask(
skyostil0fd1dad2015-04-13 20:11:48409 FROM_HERE, base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef,
410 base::Unretained(this), &compositor1));
[email protected]9794fb32013-08-29 09:49:59411
412 // Then the texture layer is removed and attached to compositor2, and passes a
413 // reference to its impl tree.
danakj60bc3bc2016-04-09 00:24:48414 std::unique_ptr<SingleReleaseCallbackImpl> compositor2;
fdoray70df5a92016-06-22 21:13:59415 main_thread_.task_runner()->PostTask(
skyostil0fd1dad2015-04-13 20:11:48416 FROM_HERE, base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef,
417 base::Unretained(this), &compositor2));
[email protected]9794fb32013-08-29 09:49:59418
419 Wait(main_thread_);
420 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
421
422 // The compositors both destroy their impl trees before the main thread layer
423 // is destroyed.
dyen398dd0142016-01-21 22:05:56424 compositor1->Run(SyncTokenFromUInt(100), false,
425 main_thread_task_runner_.get());
426 compositor2->Run(SyncTokenFromUInt(200), false,
427 main_thread_task_runner_.get());
[email protected]9794fb32013-08-29 09:49:59428
429 Wait(main_thread_);
430
431 EXPECT_CALL(test_data_.mock_callback_, Release(_, _, _)).Times(0);
432 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
433
434 // The main thread ref is the last one, so the mailbox is released back to the
435 // embedder, with the last sync point provided by the impl trees.
436 EXPECT_CALL(test_data_.mock_callback_,
dyen398dd0142016-01-21 22:05:56437 Release(test_data_.mailbox_name1_, SyncTokenFromUInt(200), false))
dyencc16ed4d2015-11-03 20:03:04438 .Times(1);
[email protected]9794fb32013-08-29 09:49:59439
fdoray70df5a92016-06-22 21:13:59440 main_thread_.task_runner()->PostTask(
skyostil0fd1dad2015-04-13 20:11:48441 FROM_HERE, base::Bind(&TextureLayerMailboxHolderTest::ReleaseMainRef,
442 base::Unretained(this)));
[email protected]9794fb32013-08-29 09:49:59443 Wait(main_thread_);
444 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
445}
446
447TEST_F(TextureLayerMailboxHolderTest, TwoCompositors_MainReleaseBetween) {
kulkarni.a4015690f12014-10-10 13:50:06448 scoped_refptr<TextureLayer> test_layer =
loyso0940d412016-03-14 01:30:31449 TextureLayer::CreateForMailbox(nullptr);
[email protected]9794fb32013-08-29 09:49:59450 ASSERT_TRUE(test_layer.get());
451
fdoray70df5a92016-06-22 21:13:59452 main_thread_.task_runner()->PostTask(
skyostil0fd1dad2015-04-13 20:11:48453 FROM_HERE, base::Bind(&TextureLayerMailboxHolderTest::CreateMainRef,
454 base::Unretained(this)));
[email protected]9794fb32013-08-29 09:49:59455
456 Wait(main_thread_);
457
458 // The texture layer is attached to compositor1, and passes a reference to its
459 // impl tree.
danakj60bc3bc2016-04-09 00:24:48460 std::unique_ptr<SingleReleaseCallbackImpl> compositor1;
fdoray70df5a92016-06-22 21:13:59461 main_thread_.task_runner()->PostTask(
skyostil0fd1dad2015-04-13 20:11:48462 FROM_HERE, base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef,
463 base::Unretained(this), &compositor1));
[email protected]9794fb32013-08-29 09:49:59464
465 // Then the texture layer is removed and attached to compositor2, and passes a
466 // reference to its impl tree.
danakj60bc3bc2016-04-09 00:24:48467 std::unique_ptr<SingleReleaseCallbackImpl> compositor2;
fdoray70df5a92016-06-22 21:13:59468 main_thread_.task_runner()->PostTask(
skyostil0fd1dad2015-04-13 20:11:48469 FROM_HERE, base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef,
470 base::Unretained(this), &compositor2));
[email protected]9794fb32013-08-29 09:49:59471
472 Wait(main_thread_);
473 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
474
475 // One compositor destroys their impl tree.
dyen398dd0142016-01-21 22:05:56476 compositor1->Run(SyncTokenFromUInt(100), false,
477 main_thread_task_runner_.get());
[email protected]9794fb32013-08-29 09:49:59478
479 // Then the main thread reference is destroyed.
fdoray70df5a92016-06-22 21:13:59480 main_thread_.task_runner()->PostTask(
skyostil0fd1dad2015-04-13 20:11:48481 FROM_HERE, base::Bind(&TextureLayerMailboxHolderTest::ReleaseMainRef,
482 base::Unretained(this)));
[email protected]9794fb32013-08-29 09:49:59483
484 Wait(main_thread_);
485
486 EXPECT_CALL(test_data_.mock_callback_, Release(_, _, _)).Times(0);
487 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
488
489 // The second impl reference is destroyed last, causing the mailbox to be
490 // released back to the embedder with the last sync point from the impl tree.
491 EXPECT_CALL(test_data_.mock_callback_,
dyen398dd0142016-01-21 22:05:56492 Release(test_data_.mailbox_name1_, SyncTokenFromUInt(200), true))
dyencc16ed4d2015-11-03 20:03:04493 .Times(1);
[email protected]9794fb32013-08-29 09:49:59494
dyen398dd0142016-01-21 22:05:56495 compositor2->Run(SyncTokenFromUInt(200), true,
496 main_thread_task_runner_.get());
[email protected]9794fb32013-08-29 09:49:59497 Wait(main_thread_);
498 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
499}
500
501TEST_F(TextureLayerMailboxHolderTest, TwoCompositors_MainReleasedFirst) {
kulkarni.a4015690f12014-10-10 13:50:06502 scoped_refptr<TextureLayer> test_layer =
loyso0940d412016-03-14 01:30:31503 TextureLayer::CreateForMailbox(nullptr);
[email protected]9794fb32013-08-29 09:49:59504 ASSERT_TRUE(test_layer.get());
505
fdoray70df5a92016-06-22 21:13:59506 main_thread_.task_runner()->PostTask(
skyostil0fd1dad2015-04-13 20:11:48507 FROM_HERE, base::Bind(&TextureLayerMailboxHolderTest::CreateMainRef,
508 base::Unretained(this)));
[email protected]9794fb32013-08-29 09:49:59509
510 Wait(main_thread_);
511
512 // The texture layer is attached to compositor1, and passes a reference to its
513 // impl tree.
danakj60bc3bc2016-04-09 00:24:48514 std::unique_ptr<SingleReleaseCallbackImpl> compositor1;
fdoray70df5a92016-06-22 21:13:59515 main_thread_.task_runner()->PostTask(
skyostil0fd1dad2015-04-13 20:11:48516 FROM_HERE, base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef,
517 base::Unretained(this), &compositor1));
[email protected]9794fb32013-08-29 09:49:59518
519 // Then the texture layer is removed and attached to compositor2, and passes a
520 // reference to its impl tree.
danakj60bc3bc2016-04-09 00:24:48521 std::unique_ptr<SingleReleaseCallbackImpl> compositor2;
fdoray70df5a92016-06-22 21:13:59522 main_thread_.task_runner()->PostTask(
skyostil0fd1dad2015-04-13 20:11:48523 FROM_HERE, base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef,
524 base::Unretained(this), &compositor2));
[email protected]9794fb32013-08-29 09:49:59525
526 Wait(main_thread_);
527 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
528
529 // The main thread reference is destroyed first.
fdoray70df5a92016-06-22 21:13:59530 main_thread_.task_runner()->PostTask(
skyostil0fd1dad2015-04-13 20:11:48531 FROM_HERE, base::Bind(&TextureLayerMailboxHolderTest::ReleaseMainRef,
532 base::Unretained(this)));
[email protected]9794fb32013-08-29 09:49:59533
534 // One compositor destroys their impl tree.
dyen398dd0142016-01-21 22:05:56535 compositor2->Run(SyncTokenFromUInt(200), false,
536 main_thread_task_runner_.get());
[email protected]9794fb32013-08-29 09:49:59537
538 Wait(main_thread_);
539
540 EXPECT_CALL(test_data_.mock_callback_, Release(_, _, _)).Times(0);
541 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
542
543 // The second impl reference is destroyed last, causing the mailbox to be
544 // released back to the embedder with the last sync point from the impl tree.
545 EXPECT_CALL(test_data_.mock_callback_,
dyen398dd0142016-01-21 22:05:56546 Release(test_data_.mailbox_name1_, SyncTokenFromUInt(100), true))
dyencc16ed4d2015-11-03 20:03:04547 .Times(1);
[email protected]9794fb32013-08-29 09:49:59548
dyen398dd0142016-01-21 22:05:56549 compositor1->Run(SyncTokenFromUInt(100), true,
550 main_thread_task_runner_.get());
[email protected]9794fb32013-08-29 09:49:59551 Wait(main_thread_);
552 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
553}
554
555TEST_F(TextureLayerMailboxHolderTest, TwoCompositors_SecondImplRefShortcut) {
kulkarni.a4015690f12014-10-10 13:50:06556 scoped_refptr<TextureLayer> test_layer =
loyso0940d412016-03-14 01:30:31557 TextureLayer::CreateForMailbox(nullptr);
[email protected]9794fb32013-08-29 09:49:59558 ASSERT_TRUE(test_layer.get());
559
fdoray70df5a92016-06-22 21:13:59560 main_thread_.task_runner()->PostTask(
skyostil0fd1dad2015-04-13 20:11:48561 FROM_HERE, base::Bind(&TextureLayerMailboxHolderTest::CreateMainRef,
562 base::Unretained(this)));
[email protected]9794fb32013-08-29 09:49:59563
564 Wait(main_thread_);
565
566 // The texture layer is attached to compositor1, and passes a reference to its
567 // impl tree.
danakj60bc3bc2016-04-09 00:24:48568 std::unique_ptr<SingleReleaseCallbackImpl> compositor1;
fdoray70df5a92016-06-22 21:13:59569 main_thread_.task_runner()->PostTask(
skyostil0fd1dad2015-04-13 20:11:48570 FROM_HERE, base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef,
571 base::Unretained(this), &compositor1));
[email protected]9794fb32013-08-29 09:49:59572
573 // Then the texture layer is removed and attached to compositor2, and passes a
574 // reference to its impl tree.
danakj60bc3bc2016-04-09 00:24:48575 std::unique_ptr<SingleReleaseCallbackImpl> compositor2;
fdoray70df5a92016-06-22 21:13:59576 main_thread_.task_runner()->PostTask(
skyostil0fd1dad2015-04-13 20:11:48577 FROM_HERE, base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef,
578 base::Unretained(this), &compositor2));
[email protected]9794fb32013-08-29 09:49:59579
580 Wait(main_thread_);
581 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
582
583 // The main thread reference is destroyed first.
fdoray70df5a92016-06-22 21:13:59584 main_thread_.task_runner()->PostTask(
skyostil0fd1dad2015-04-13 20:11:48585 FROM_HERE, base::Bind(&TextureLayerMailboxHolderTest::ReleaseMainRef,
586 base::Unretained(this)));
[email protected]9794fb32013-08-29 09:49:59587
588 EXPECT_CALL(test_data_.mock_callback_,
dyen398dd0142016-01-21 22:05:56589 Release(test_data_.mailbox_name1_, SyncTokenFromUInt(200), true))
dyencc16ed4d2015-11-03 20:03:04590 .Times(1);
[email protected]9794fb32013-08-29 09:49:59591
gabcca53112016-06-08 20:13:28592 base::WaitableEvent begin_capture(
593 base::WaitableEvent::ResetPolicy::AUTOMATIC,
594 base::WaitableEvent::InitialState::NOT_SIGNALED);
595 base::WaitableEvent wait_for_capture(
596 base::WaitableEvent::ResetPolicy::AUTOMATIC,
597 base::WaitableEvent::InitialState::NOT_SIGNALED);
598 base::WaitableEvent stop_capture(
599 base::WaitableEvent::ResetPolicy::AUTOMATIC,
600 base::WaitableEvent::InitialState::NOT_SIGNALED);
[email protected]9794fb32013-08-29 09:49:59601
602 // Post a task to start capturing tasks on the main thread. This will block
603 // the main thread until we signal the |stop_capture| event.
fdoray70df5a92016-06-22 21:13:59604 main_thread_.task_runner()->PostTask(
[email protected]9794fb32013-08-29 09:49:59605 FROM_HERE,
606 base::Bind(&TextureLayerMailboxHolderTest::CapturePostTasksAndWait,
skyostil0fd1dad2015-04-13 20:11:48607 base::Unretained(this), &begin_capture, &wait_for_capture,
[email protected]9794fb32013-08-29 09:49:59608 &stop_capture));
609
610 // Before the main thread capturing starts, one compositor destroys their
611 // impl reference. Since capturing did not start, this gets post-tasked to
612 // the main thread.
dyen398dd0142016-01-21 22:05:56613 compositor1->Run(SyncTokenFromUInt(100), false,
614 main_thread_task_runner_.get());
[email protected]9794fb32013-08-29 09:49:59615
616 // Start capturing on the main thread.
617 begin_capture.Signal();
618 wait_for_capture.Wait();
619
620 // Meanwhile, the second compositor released its impl reference, but this task
621 // gets shortcutted directly to the main thread. This means the reference is
622 // released before compositor1, whose reference will be released later when
623 // the post-task is serviced. But since it was destroyed _on the impl thread_
624 // last, its sync point values should be used.
dyen398dd0142016-01-21 22:05:56625 compositor2->Run(SyncTokenFromUInt(200), true,
626 main_thread_task_runner_.get());
[email protected]9794fb32013-08-29 09:49:59627
628 stop_capture.Signal();
629 Wait(main_thread_);
630
631 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
632}
633
[email protected]e216fef02013-03-20 22:56:10634class TextureLayerImplWithMailboxThreadedCallback : public LayerTreeTest {
[email protected]28571b042013-03-14 07:59:15635 public:
danakj014316e2016-08-04 18:40:26636 TextureLayerImplWithMailboxThreadedCallback() = default;
637
638 std::unique_ptr<TestDelegatingOutputSurface> CreateDelegatingOutputSurface(
639 scoped_refptr<ContextProvider> compositor_context_provider,
640 scoped_refptr<ContextProvider> worker_context_provider) override {
641 bool synchronous_composite =
642 !HasImplThread() &&
643 !layer_tree_host()->settings().single_thread_proxy_scheduler;
644 // Allow relaim resources for this test so that mailboxes in the display
645 // will be returned inside the commit that replaces them.
646 bool force_disable_reclaim_resources = false;
647 return base::MakeUnique<TestDelegatingOutputSurface>(
648 compositor_context_provider, std::move(worker_context_provider),
649 CreateDisplayOutputSurface(compositor_context_provider),
650 shared_bitmap_manager(), gpu_memory_buffer_manager(),
651 layer_tree_host()->settings().renderer_settings, ImplThreadTaskRunner(),
652 synchronous_composite, force_disable_reclaim_resources);
653 }
[email protected]28571b042013-03-14 07:59:15654
655 // Make sure callback is received on main and doesn't block the impl thread.
danakj014316e2016-08-04 18:40:26656 void ReleaseCallback(char mailbox_char,
657 const gpu::SyncToken& sync_token,
658 bool lost_resource) {
[email protected]9794fb32013-08-29 09:49:59659 EXPECT_EQ(true, main_thread_.CalledOnValidThread());
[email protected]7ba3ca72013-04-11 06:37:25660 EXPECT_FALSE(lost_resource);
[email protected]28571b042013-03-14 07:59:15661 ++callback_count_;
662 }
663
664 void SetMailbox(char mailbox_char) {
[email protected]9794fb32013-08-29 09:49:59665 EXPECT_EQ(true, main_thread_.CalledOnValidThread());
danakj60bc3bc2016-04-09 00:24:48666 std::unique_ptr<SingleReleaseCallback> callback =
667 SingleReleaseCallback::Create(base::Bind(
[email protected]28571b042013-03-14 07:59:15668 &TextureLayerImplWithMailboxThreadedCallback::ReleaseCallback,
danakj014316e2016-08-04 18:40:26669 base::Unretained(this), mailbox_char));
dyene5db881b2016-03-01 19:47:03670 layer_->SetTextureMailbox(
671 TextureMailbox(MailboxFromChar(mailbox_char),
672 SyncTokenFromUInt(static_cast<uint32_t>(mailbox_char)),
673 GL_TEXTURE_2D),
674 std::move(callback));
danakj014316e2016-08-04 18:40:26675 // Damage the layer so we send a new frame with the new mailbox to the
676 // Display compositor.
677 layer_->SetNeedsDisplay();
[email protected]28571b042013-03-14 07:59:15678 }
679
dcheng716bedf2014-10-21 09:51:08680 void BeginTest() override {
[email protected]9794fb32013-08-29 09:49:59681 EXPECT_EQ(true, main_thread_.CalledOnValidThread());
682
[email protected]28571b042013-03-14 07:59:15683 gfx::Size bounds(100, 100);
loyso0940d412016-03-14 01:30:31684 root_ = Layer::Create();
[email protected]28571b042013-03-14 07:59:15685 root_->SetBounds(bounds);
686
loyso0940d412016-03-14 01:30:31687 layer_ = TextureLayer::CreateForMailbox(nullptr);
[email protected]28571b042013-03-14 07:59:15688 layer_->SetIsDrawable(true);
[email protected]28571b042013-03-14 07:59:15689 layer_->SetBounds(bounds);
690
691 root_->AddChild(layer_);
khushalsagar86928f92016-08-17 21:49:05692 layer_tree()->SetRootLayer(root_);
693 layer_tree()->SetViewportSize(bounds);
[email protected]28571b042013-03-14 07:59:15694 SetMailbox('1');
695 EXPECT_EQ(0, callback_count_);
696
697 // Case #1: change mailbox before the commit. The old mailbox should be
698 // released immediately.
699 SetMailbox('2');
700 EXPECT_EQ(1, callback_count_);
[email protected]e216fef02013-03-20 22:56:10701 PostSetNeedsCommitToMainThread();
[email protected]28571b042013-03-14 07:59:15702 }
703
dcheng716bedf2014-10-21 09:51:08704 void DidCommit() override {
[email protected]28571b042013-03-14 07:59:15705 ++commit_count_;
706 switch (commit_count_) {
707 case 1:
708 // Case #2: change mailbox after the commit (and draw), where the
709 // layer draws. The old mailbox should be released during the next
710 // commit.
711 SetMailbox('3');
712 EXPECT_EQ(1, callback_count_);
713 break;
714 case 2:
[email protected]28571b042013-03-14 07:59:15715 EXPECT_EQ(2, callback_count_);
716 // Case #3: change mailbox when the layer doesn't draw. The old
717 // mailbox should be released during the next commit.
718 layer_->SetBounds(gfx::Size());
719 SetMailbox('4');
720 break;
[email protected]9794fb32013-08-29 09:49:59721 case 3:
[email protected]28571b042013-03-14 07:59:15722 EXPECT_EQ(3, callback_count_);
723 // Case #4: release mailbox that was committed but never drawn. The
724 // old mailbox should be released during the next commit.
danakj968153f32014-10-15 22:52:16725 layer_->SetTextureMailbox(TextureMailbox(), nullptr);
[email protected]28571b042013-03-14 07:59:15726 break;
[email protected]9794fb32013-08-29 09:49:59727 case 4:
[email protected]28571b042013-03-14 07:59:15728 EXPECT_EQ(4, callback_count_);
[email protected]7096acc2013-06-18 21:12:43729 // Restore a mailbox for the next step.
730 SetMailbox('5');
731 break;
danakj014316e2016-08-04 18:40:26732 case 5:
[email protected]7096acc2013-06-18 21:12:43733 // Case #5: remove layer from tree. Callback should *not* be called, the
734 // mailbox is returned to the main thread.
735 EXPECT_EQ(4, callback_count_);
736 layer_->RemoveFromParent();
737 break;
danakj014316e2016-08-04 18:40:26738 case 6:
[email protected]7096acc2013-06-18 21:12:43739 EXPECT_EQ(4, callback_count_);
740 // Resetting the mailbox will call the callback now.
danakj968153f32014-10-15 22:52:16741 layer_->SetTextureMailbox(TextureMailbox(), nullptr);
[email protected]7096acc2013-06-18 21:12:43742 EXPECT_EQ(5, callback_count_);
[email protected]e216fef02013-03-20 22:56:10743 EndTest();
[email protected]28571b042013-03-14 07:59:15744 break;
745 default:
746 NOTREACHED();
747 break;
[email protected]de44a152013-01-08 15:28:46748 }
[email protected]28571b042013-03-14 07:59:15749 }
[email protected]de44a152013-01-08 15:28:46750
dcheng716bedf2014-10-21 09:51:08751 void AfterTest() override {}
[email protected]de44a152013-01-08 15:28:46752
[email protected]28571b042013-03-14 07:59:15753 private:
[email protected]9794fb32013-08-29 09:49:59754 base::ThreadChecker main_thread_;
danakj014316e2016-08-04 18:40:26755 int callback_count_ = 0;
756 int commit_count_ = 0;
[email protected]28571b042013-03-14 07:59:15757 scoped_refptr<Layer> root_;
758 scoped_refptr<TextureLayer> layer_;
[email protected]de44a152013-01-08 15:28:46759};
760
danakj0943a112016-08-11 00:33:46761SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerImplWithMailboxThreadedCallback);
[email protected]74b43cc2013-08-30 06:29:27762
[email protected]74b43cc2013-08-30 06:29:27763class TextureLayerMailboxIsActivatedDuringCommit : public LayerTreeTest {
764 protected:
[email protected]98ea818e2014-01-24 10:22:08765 TextureLayerMailboxIsActivatedDuringCommit() : activate_count_(0) {}
[email protected]74b43cc2013-08-30 06:29:27766
dyene5db881b2016-03-01 19:47:03767 static void ReleaseCallback(const gpu::SyncToken& original_sync_token,
768 const gpu::SyncToken& release_sync_token,
dyencc16ed4d2015-11-03 20:03:04769 bool lost_resource) {}
[email protected]74b43cc2013-08-30 06:29:27770
771 void SetMailbox(char mailbox_char) {
dyene5db881b2016-03-01 19:47:03772 const gpu::SyncToken sync_token =
773 SyncTokenFromUInt(static_cast<uint32_t>(mailbox_char));
danakj60bc3bc2016-04-09 00:24:48774 std::unique_ptr<SingleReleaseCallback> callback =
775 SingleReleaseCallback::Create(base::Bind(
776 &TextureLayerMailboxIsActivatedDuringCommit::ReleaseCallback,
777 sync_token));
dyencc16ed4d2015-11-03 20:03:04778 layer_->SetTextureMailbox(TextureMailbox(MailboxFromChar(mailbox_char),
dyene5db881b2016-03-01 19:47:03779 sync_token, GL_TEXTURE_2D),
danakja04855a2015-11-18 20:39:10780 std::move(callback));
[email protected]74b43cc2013-08-30 06:29:27781 }
782
dcheng716bedf2014-10-21 09:51:08783 void BeginTest() override {
[email protected]74b43cc2013-08-30 06:29:27784 gfx::Size bounds(100, 100);
loyso0940d412016-03-14 01:30:31785 root_ = Layer::Create();
[email protected]74b43cc2013-08-30 06:29:27786 root_->SetBounds(bounds);
787
loyso0940d412016-03-14 01:30:31788 layer_ = TextureLayer::CreateForMailbox(nullptr);
[email protected]74b43cc2013-08-30 06:29:27789 layer_->SetIsDrawable(true);
[email protected]74b43cc2013-08-30 06:29:27790 layer_->SetBounds(bounds);
791
792 root_->AddChild(layer_);
khushalsagar86928f92016-08-17 21:49:05793 layer_tree()->SetRootLayer(root_);
794 layer_tree()->SetViewportSize(bounds);
[email protected]74b43cc2013-08-30 06:29:27795 SetMailbox('1');
796
797 PostSetNeedsCommitToMainThread();
798 }
799
dcheng716bedf2014-10-21 09:51:08800 void WillActivateTreeOnThread(LayerTreeHostImpl* impl) override {
danakj3c3973b2015-08-25 21:50:18801 base::AutoLock lock(activate_count_lock_);
[email protected]74b43cc2013-08-30 06:29:27802 ++activate_count_;
803 }
804
dcheng716bedf2014-10-21 09:51:08805 void DidCommit() override {
danakj3c3973b2015-08-25 21:50:18806 // The first frame doesn't cause anything to be returned so it does not
807 // need to wait for activation.
808 if (layer_tree_host()->source_frame_number() > 1) {
809 base::AutoLock lock(activate_count_lock_);
810 // The activate happened before commit is done on the main side.
811 EXPECT_EQ(activate_count_, layer_tree_host()->source_frame_number());
812 }
813
[email protected]98ea818e2014-01-24 10:22:08814 switch (layer_tree_host()->source_frame_number()) {
[email protected]74b43cc2013-08-30 06:29:27815 case 1:
816 // The first mailbox has been activated. Set a new mailbox, and
817 // expect the next commit to finish *after* it is activated.
818 SetMailbox('2');
[email protected]74b43cc2013-08-30 06:29:27819 break;
820 case 2:
821 // The second mailbox has been activated. Remove the layer from
822 // the tree to cause another commit/activation. The commit should
823 // finish *after* the layer is removed from the active tree.
824 layer_->RemoveFromParent();
[email protected]74b43cc2013-08-30 06:29:27825 break;
826 case 3:
827 EndTest();
828 break;
829 }
830 }
831
dcheng716bedf2014-10-21 09:51:08832 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
danakj3c3973b2015-08-25 21:50:18833 // The activate didn't happen before commit is done on the impl side (but it
834 // should happen before the main thread is done).
835 EXPECT_EQ(activate_count_, host_impl->sync_tree()->source_frame_number());
[email protected]74b43cc2013-08-30 06:29:27836 }
837
dcheng716bedf2014-10-21 09:51:08838 void AfterTest() override {}
[email protected]74b43cc2013-08-30 06:29:27839
danakj3c3973b2015-08-25 21:50:18840 base::Lock activate_count_lock_;
[email protected]74b43cc2013-08-30 06:29:27841 int activate_count_;
842 scoped_refptr<Layer> root_;
843 scoped_refptr<TextureLayer> layer_;
844};
845
danakj0943a112016-08-11 00:33:46846SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerMailboxIsActivatedDuringCommit);
[email protected]74b43cc2013-08-30 06:29:27847
[email protected]de44a152013-01-08 15:28:46848class TextureLayerImplWithMailboxTest : public TextureLayerTest {
[email protected]28571b042013-03-14 07:59:15849 protected:
dcheng93a52eb2014-12-23 02:14:23850 void SetUp() override {
[email protected]28571b042013-03-14 07:59:15851 TextureLayerTest::SetUp();
danakjcf610582015-06-16 22:48:56852 layer_tree_host_ =
853 MockLayerTreeHost::Create(&fake_client_, &task_graph_runner_);
khushalsagar86928f92016-08-17 21:49:05854 layer_tree_ = layer_tree_host_->GetLayerTree();
sievers71c62dd52015-10-07 01:44:39855 host_impl_.SetVisible(true);
revemand180dfc32015-09-24 00:19:43856 EXPECT_TRUE(host_impl_.InitializeRenderer(output_surface_.get()));
[email protected]28571b042013-03-14 07:59:15857 }
[email protected]de44a152013-01-08 15:28:46858
[email protected]0ec335c42013-07-04 06:17:08859 bool WillDraw(TextureLayerImpl* layer, DrawMode mode) {
860 bool will_draw = layer->WillDraw(
861 mode, host_impl_.active_tree()->resource_provider());
862 if (will_draw)
863 layer->DidDraw(host_impl_.active_tree()->resource_provider());
864 return will_draw;
865 }
866
[email protected]408b5e22013-03-19 09:48:09867 FakeLayerTreeHostClient fake_client_;
[email protected]de44a152013-01-08 15:28:46868};
869
[email protected]ffbb2212013-06-02 23:47:59870// Test conditions for results of TextureLayerImpl::WillDraw under
871// different configurations of different mailbox, texture_id, and draw_mode.
872TEST_F(TextureLayerImplWithMailboxTest, TestWillDraw) {
skyostil3976a3f2014-09-04 22:07:23873 EXPECT_CALL(
874 test_data_.mock_callback_,
dyencc16ed4d2015-11-03 20:03:04875 ReleaseImpl(test_data_.mailbox_name1_, test_data_.sync_token1_, false, _))
[email protected]0ec335c42013-07-04 06:17:08876 .Times(AnyNumber());
dyencc16ed4d2015-11-03 20:03:04877 EXPECT_CALL(
878 test_data_.mock_callback_,
879 ReleaseImpl2(test_data_.shared_bitmap_.get(), gpu::SyncToken(), false, _))
[email protected]0ec335c42013-07-04 06:17:08880 .Times(AnyNumber());
[email protected]ffbb2212013-06-02 23:47:59881 // Hardware mode.
882 {
danakj60bc3bc2016-04-09 00:24:48883 std::unique_ptr<TextureLayerImpl> impl_layer =
[email protected]17e08432014-04-10 00:41:11884 TextureLayerImpl::Create(host_impl_.active_tree(), 1);
[email protected]9260757f2013-09-17 01:24:16885 impl_layer->SetTextureMailbox(
886 test_data_.mailbox1_,
skyostil3976a3f2014-09-04 22:07:23887 SingleReleaseCallbackImpl::Create(test_data_.release_mailbox1_impl_));
[email protected]0ec335c42013-07-04 06:17:08888 EXPECT_TRUE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE));
[email protected]ffbb2212013-06-02 23:47:59889 }
890
891 {
danakj60bc3bc2016-04-09 00:24:48892 std::unique_ptr<TextureLayerImpl> impl_layer =
[email protected]17e08432014-04-10 00:41:11893 TextureLayerImpl::Create(host_impl_.active_tree(), 1);
danakj968153f32014-10-15 22:52:16894 impl_layer->SetTextureMailbox(TextureMailbox(), nullptr);
[email protected]0ec335c42013-07-04 06:17:08895 EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE));
896 }
897
898 {
899 // Software resource.
danakj60bc3bc2016-04-09 00:24:48900 std::unique_ptr<TextureLayerImpl> impl_layer =
[email protected]17e08432014-04-10 00:41:11901 TextureLayerImpl::Create(host_impl_.active_tree(), 1);
[email protected]9260757f2013-09-17 01:24:16902 impl_layer->SetTextureMailbox(
903 test_data_.mailbox3_,
skyostil3976a3f2014-09-04 22:07:23904 SingleReleaseCallbackImpl::Create(test_data_.release_mailbox3_impl_));
[email protected]3e44d7a2013-07-30 00:03:10905 EXPECT_TRUE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE));
[email protected]ffbb2212013-06-02 23:47:59906 }
907
[email protected]0ec335c42013-07-04 06:17:08908 // Software mode.
909 {
danakj60bc3bc2016-04-09 00:24:48910 std::unique_ptr<TextureLayerImpl> impl_layer =
[email protected]17e08432014-04-10 00:41:11911 TextureLayerImpl::Create(host_impl_.active_tree(), 1);
[email protected]9260757f2013-09-17 01:24:16912 impl_layer->SetTextureMailbox(
913 test_data_.mailbox1_,
skyostil3976a3f2014-09-04 22:07:23914 SingleReleaseCallbackImpl::Create(test_data_.release_mailbox1_impl_));
[email protected]0ec335c42013-07-04 06:17:08915 EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_SOFTWARE));
916 }
917
918 {
danakj60bc3bc2016-04-09 00:24:48919 std::unique_ptr<TextureLayerImpl> impl_layer =
[email protected]17e08432014-04-10 00:41:11920 TextureLayerImpl::Create(host_impl_.active_tree(), 1);
danakj968153f32014-10-15 22:52:16921 impl_layer->SetTextureMailbox(TextureMailbox(), nullptr);
[email protected]0ec335c42013-07-04 06:17:08922 EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_SOFTWARE));
923 }
924
925 {
926 // Software resource.
danakj60bc3bc2016-04-09 00:24:48927 std::unique_ptr<TextureLayerImpl> impl_layer =
[email protected]17e08432014-04-10 00:41:11928 TextureLayerImpl::Create(host_impl_.active_tree(), 1);
[email protected]9260757f2013-09-17 01:24:16929 impl_layer->SetTextureMailbox(
930 test_data_.mailbox3_,
skyostil3976a3f2014-09-04 22:07:23931 SingleReleaseCallbackImpl::Create(test_data_.release_mailbox3_impl_));
[email protected]0ec335c42013-07-04 06:17:08932 EXPECT_TRUE(WillDraw(impl_layer.get(), DRAW_MODE_SOFTWARE));
933 }
934
[email protected]ffbb2212013-06-02 23:47:59935 // Resourceless software mode.
936 {
danakj60bc3bc2016-04-09 00:24:48937 std::unique_ptr<TextureLayerImpl> impl_layer =
[email protected]17e08432014-04-10 00:41:11938 TextureLayerImpl::Create(host_impl_.active_tree(), 1);
[email protected]9260757f2013-09-17 01:24:16939 impl_layer->SetTextureMailbox(
940 test_data_.mailbox1_,
skyostil3976a3f2014-09-04 22:07:23941 SingleReleaseCallbackImpl::Create(test_data_.release_mailbox1_impl_));
[email protected]0ec335c42013-07-04 06:17:08942 EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_RESOURCELESS_SOFTWARE));
[email protected]ffbb2212013-06-02 23:47:59943 }
[email protected]ffbb2212013-06-02 23:47:59944}
945
[email protected]28571b042013-03-14 07:59:15946TEST_F(TextureLayerImplWithMailboxTest, TestImplLayerCallbacks) {
947 host_impl_.CreatePendingTree();
danakj60bc3bc2016-04-09 00:24:48948 std::unique_ptr<TextureLayerImpl> pending_layer;
[email protected]17e08432014-04-10 00:41:11949 pending_layer = TextureLayerImpl::Create(host_impl_.pending_tree(), 1);
[email protected]28571b042013-03-14 07:59:15950 ASSERT_TRUE(pending_layer);
[email protected]de44a152013-01-08 15:28:46951
danakj60bc3bc2016-04-09 00:24:48952 std::unique_ptr<LayerImpl> active_layer(
[email protected]28571b042013-03-14 07:59:15953 pending_layer->CreateLayerImpl(host_impl_.active_tree()));
[email protected]ed511b8d2013-03-25 03:29:29954 ASSERT_TRUE(active_layer);
[email protected]de44a152013-01-08 15:28:46955
[email protected]9260757f2013-09-17 01:24:16956 pending_layer->SetTextureMailbox(
957 test_data_.mailbox1_,
skyostil3976a3f2014-09-04 22:07:23958 SingleReleaseCallbackImpl::Create(test_data_.release_mailbox1_impl_));
[email protected]421e84f2013-02-22 03:27:15959
[email protected]28571b042013-03-14 07:59:15960 // Test multiple commits without an activation.
skyostil3976a3f2014-09-04 22:07:23961 EXPECT_CALL(
962 test_data_.mock_callback_,
dyencc16ed4d2015-11-03 20:03:04963 ReleaseImpl(test_data_.mailbox_name1_, test_data_.sync_token1_, false, _))
[email protected]28571b042013-03-14 07:59:15964 .Times(1);
[email protected]9260757f2013-09-17 01:24:16965 pending_layer->SetTextureMailbox(
966 test_data_.mailbox2_,
skyostil3976a3f2014-09-04 22:07:23967 SingleReleaseCallbackImpl::Create(test_data_.release_mailbox2_impl_));
[email protected]28571b042013-03-14 07:59:15968 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]421e84f2013-02-22 03:27:15969
[email protected]28571b042013-03-14 07:59:15970 // Test callback after activation.
[email protected]ed511b8d2013-03-25 03:29:29971 pending_layer->PushPropertiesTo(active_layer.get());
972 active_layer->DidBecomeActive();
[email protected]421e84f2013-02-22 03:27:15973
skyostil3976a3f2014-09-04 22:07:23974 EXPECT_CALL(test_data_.mock_callback_, ReleaseImpl(_, _, _, _)).Times(0);
[email protected]9260757f2013-09-17 01:24:16975 pending_layer->SetTextureMailbox(
976 test_data_.mailbox1_,
skyostil3976a3f2014-09-04 22:07:23977 SingleReleaseCallbackImpl::Create(test_data_.release_mailbox1_impl_));
[email protected]28571b042013-03-14 07:59:15978 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]421e84f2013-02-22 03:27:15979
[email protected]7ba3ca72013-04-11 06:37:25980 EXPECT_CALL(test_data_.mock_callback_,
skyostil3976a3f2014-09-04 22:07:23981 ReleaseImpl(test_data_.mailbox_name2_, _, false, _)).Times(1);
[email protected]ed511b8d2013-03-25 03:29:29982 pending_layer->PushPropertiesTo(active_layer.get());
983 active_layer->DidBecomeActive();
[email protected]28571b042013-03-14 07:59:15984 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]de44a152013-01-08 15:28:46985
[email protected]28571b042013-03-14 07:59:15986 // Test resetting the mailbox.
[email protected]7ba3ca72013-04-11 06:37:25987 EXPECT_CALL(test_data_.mock_callback_,
skyostil3976a3f2014-09-04 22:07:23988 ReleaseImpl(test_data_.mailbox_name1_, _, false, _)).Times(1);
danakj968153f32014-10-15 22:52:16989 pending_layer->SetTextureMailbox(TextureMailbox(), nullptr);
[email protected]ed511b8d2013-03-25 03:29:29990 pending_layer->PushPropertiesTo(active_layer.get());
991 active_layer->DidBecomeActive();
[email protected]28571b042013-03-14 07:59:15992 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]de44a152013-01-08 15:28:46993
[email protected]28571b042013-03-14 07:59:15994 // Test destructor.
skyostil3976a3f2014-09-04 22:07:23995 EXPECT_CALL(
996 test_data_.mock_callback_,
dyencc16ed4d2015-11-03 20:03:04997 ReleaseImpl(test_data_.mailbox_name1_, test_data_.sync_token1_, false, _))
[email protected]28571b042013-03-14 07:59:15998 .Times(1);
[email protected]9260757f2013-09-17 01:24:16999 pending_layer->SetTextureMailbox(
1000 test_data_.mailbox1_,
skyostil3976a3f2014-09-04 22:07:231001 SingleReleaseCallbackImpl::Create(test_data_.release_mailbox1_impl_));
[email protected]de44a152013-01-08 15:28:461002}
1003
[email protected]28571b042013-03-14 07:59:151004TEST_F(TextureLayerImplWithMailboxTest,
1005 TestDestructorCallbackOnCreatedResource) {
danakj60bc3bc2016-04-09 00:24:481006 std::unique_ptr<TextureLayerImpl> impl_layer;
[email protected]17e08432014-04-10 00:41:111007 impl_layer = TextureLayerImpl::Create(host_impl_.active_tree(), 1);
[email protected]28571b042013-03-14 07:59:151008 ASSERT_TRUE(impl_layer);
[email protected]de44a152013-01-08 15:28:461009
[email protected]7ba3ca72013-04-11 06:37:251010 EXPECT_CALL(test_data_.mock_callback_,
skyostil3976a3f2014-09-04 22:07:231011 ReleaseImpl(test_data_.mailbox_name1_, _, false, _)).Times(1);
[email protected]9260757f2013-09-17 01:24:161012 impl_layer->SetTextureMailbox(
1013 test_data_.mailbox1_,
skyostil3976a3f2014-09-04 22:07:231014 SingleReleaseCallbackImpl::Create(test_data_.release_mailbox1_impl_));
[email protected]ffbb2212013-06-02 23:47:591015 impl_layer->DidBecomeActive();
1016 EXPECT_TRUE(impl_layer->WillDraw(
1017 DRAW_MODE_HARDWARE, host_impl_.active_tree()->resource_provider()));
[email protected]28571b042013-03-14 07:59:151018 impl_layer->DidDraw(host_impl_.active_tree()->resource_provider());
danakj968153f32014-10-15 22:52:161019 impl_layer->SetTextureMailbox(TextureMailbox(), nullptr);
[email protected]de44a152013-01-08 15:28:461020}
1021
[email protected]28571b042013-03-14 07:59:151022TEST_F(TextureLayerImplWithMailboxTest, TestCallbackOnInUseResource) {
1023 ResourceProvider* provider = host_impl_.active_tree()->resource_provider();
jbaumanbbd425e2015-05-19 00:33:351024 ResourceId id = provider->CreateResourceFromTextureMailbox(
skyostil3976a3f2014-09-04 22:07:231025 test_data_.mailbox1_,
1026 SingleReleaseCallbackImpl::Create(test_data_.release_mailbox1_impl_));
[email protected]28571b042013-03-14 07:59:151027 provider->AllocateForTesting(id);
[email protected]de44a152013-01-08 15:28:461028
[email protected]28571b042013-03-14 07:59:151029 // Transfer some resources to the parent.
1030 ResourceProvider::ResourceIdArray resource_ids_to_transfer;
1031 resource_ids_to_transfer.push_back(id);
1032 TransferableResourceArray list;
1033 provider->PrepareSendToParent(resource_ids_to_transfer, &list);
1034 EXPECT_TRUE(provider->InUseByConsumer(id));
skyostil3976a3f2014-09-04 22:07:231035 EXPECT_CALL(test_data_.mock_callback_, ReleaseImpl(_, _, _, _)).Times(0);
[email protected]28571b042013-03-14 07:59:151036 provider->DeleteResource(id);
1037 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
[email protected]7ba3ca72013-04-11 06:37:251038 EXPECT_CALL(test_data_.mock_callback_,
skyostil3976a3f2014-09-04 22:07:231039 ReleaseImpl(test_data_.mailbox_name1_, _, false, _)).Times(1);
[email protected]e00bab022013-08-19 00:42:451040 ReturnedResourceArray returned;
1041 TransferableResource::ReturnResources(list, &returned);
1042 provider->ReceiveReturnsFromParent(returned);
[email protected]de44a152013-01-08 15:28:461043}
1044
[email protected]4bad8b62013-10-24 01:27:291045// Checks that TextureLayer::Update does not cause an extra commit when setting
1046// the texture mailbox.
1047class TextureLayerNoExtraCommitForMailboxTest
1048 : public LayerTreeTest,
1049 public TextureLayerClient {
1050 public:
[email protected]4bad8b62013-10-24 01:27:291051 // TextureLayerClient implementation.
dcheng716bedf2014-10-21 09:51:081052 bool PrepareTextureMailbox(
[email protected]df41e252014-02-03 23:39:501053 TextureMailbox* texture_mailbox,
danakj4d0dd802016-08-23 22:10:061054 std::unique_ptr<SingleReleaseCallback>* release_callback) override {
[email protected]cce34bd2013-12-02 23:24:451055 if (layer_tree_host()->source_frame_number() == 1) {
[email protected]9f35bd22014-06-03 15:25:461056 // Once this has been committed, the mailbox will be released.
[email protected]df41e252014-02-03 23:39:501057 *texture_mailbox = TextureMailbox();
[email protected]cce34bd2013-12-02 23:24:451058 return true;
1059 }
[email protected]4bad8b62013-10-24 01:27:291060
dyene5db881b2016-03-01 19:47:031061 *texture_mailbox = TextureMailbox(MailboxFromChar('1'),
1062 SyncTokenFromUInt(0x123), GL_TEXTURE_2D);
[email protected]4bad8b62013-10-24 01:27:291063 *release_callback = SingleReleaseCallback::Create(
1064 base::Bind(&TextureLayerNoExtraCommitForMailboxTest::MailboxReleased,
1065 base::Unretained(this)));
[email protected]4bad8b62013-10-24 01:27:291066 return true;
1067 }
1068
dyencc16ed4d2015-11-03 20:03:041069 void MailboxReleased(const gpu::SyncToken& sync_token, bool lost_resource) {
dyene5db881b2016-03-01 19:47:031070 EXPECT_TRUE(sync_token.HasData());
[email protected]cce34bd2013-12-02 23:24:451071 EndTest();
[email protected]4bad8b62013-10-24 01:27:291072 }
1073
dcheng716bedf2014-10-21 09:51:081074 void SetupTree() override {
loyso0940d412016-03-14 01:30:311075 scoped_refptr<Layer> root = Layer::Create();
[email protected]4bad8b62013-10-24 01:27:291076 root->SetBounds(gfx::Size(10, 10));
[email protected]4bad8b62013-10-24 01:27:291077 root->SetIsDrawable(true);
1078
loyso0940d412016-03-14 01:30:311079 texture_layer_ = TextureLayer::CreateForMailbox(this);
[email protected]4bad8b62013-10-24 01:27:291080 texture_layer_->SetBounds(gfx::Size(10, 10));
[email protected]4bad8b62013-10-24 01:27:291081 texture_layer_->SetIsDrawable(true);
[email protected]0d7fb302014-01-23 21:30:471082 root->AddChild(texture_layer_);
[email protected]4bad8b62013-10-24 01:27:291083
khushalsagar86928f92016-08-17 21:49:051084 layer_tree()->SetRootLayer(root);
[email protected]4bad8b62013-10-24 01:27:291085 LayerTreeTest::SetupTree();
1086 }
1087
dcheng716bedf2014-10-21 09:51:081088 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
[email protected]4bad8b62013-10-24 01:27:291089
dcheng716bedf2014-10-21 09:51:081090 void DidCommitAndDrawFrame() override {
[email protected]4bad8b62013-10-24 01:27:291091 switch (layer_tree_host()->source_frame_number()) {
1092 case 1:
[email protected]4ea293f72014-08-13 03:03:171093 EXPECT_FALSE(proxy()->MainFrameWillHappenForTesting());
[email protected]cce34bd2013-12-02 23:24:451094 // Invalidate the texture layer to clear the mailbox before
1095 // ending the test.
1096 texture_layer_->SetNeedsDisplay();
1097 break;
1098 case 2:
[email protected]4bad8b62013-10-24 01:27:291099 break;
1100 default:
1101 NOTREACHED();
1102 break;
1103 }
1104 }
1105
dcheng716bedf2014-10-21 09:51:081106 void AfterTest() override {}
[email protected]4bad8b62013-10-24 01:27:291107
1108 private:
[email protected]4bad8b62013-10-24 01:27:291109 scoped_refptr<TextureLayer> texture_layer_;
[email protected]4bad8b62013-10-24 01:27:291110};
1111
[email protected]cce34bd2013-12-02 23:24:451112SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerNoExtraCommitForMailboxTest);
[email protected]4bad8b62013-10-24 01:27:291113
[email protected]b04264f92013-09-13 23:37:291114// Checks that changing a mailbox in the client for a TextureLayer that's
1115// invisible correctly works and uses the new mailbox as soon as the layer
1116// becomes visible (and returns the old one).
1117class TextureLayerChangeInvisibleMailboxTest
1118 : public LayerTreeTest,
1119 public TextureLayerClient {
1120 public:
1121 TextureLayerChangeInvisibleMailboxTest()
1122 : mailbox_changed_(true),
1123 mailbox_returned_(0),
1124 prepare_called_(0),
1125 commit_count_(0) {
1126 mailbox_ = MakeMailbox('1');
1127 }
1128
1129 // TextureLayerClient implementation.
dcheng716bedf2014-10-21 09:51:081130 bool PrepareTextureMailbox(
[email protected]e51444a2013-12-10 23:05:011131 TextureMailbox* mailbox,
danakj4d0dd802016-08-23 22:10:061132 std::unique_ptr<SingleReleaseCallback>* release_callback) override {
[email protected]b04264f92013-09-13 23:37:291133 ++prepare_called_;
1134 if (!mailbox_changed_)
1135 return false;
1136 *mailbox = mailbox_;
[email protected]9260757f2013-09-17 01:24:161137 *release_callback = SingleReleaseCallback::Create(
1138 base::Bind(&TextureLayerChangeInvisibleMailboxTest::MailboxReleased,
1139 base::Unretained(this)));
[email protected]b04264f92013-09-13 23:37:291140 return true;
1141 }
1142
1143 TextureMailbox MakeMailbox(char name) {
dyene5db881b2016-03-01 19:47:031144 return TextureMailbox(MailboxFromChar(name),
1145 SyncTokenFromUInt(static_cast<uint32_t>(name)),
dyencc16ed4d2015-11-03 20:03:041146 GL_TEXTURE_2D);
[email protected]b04264f92013-09-13 23:37:291147 }
1148
dyencc16ed4d2015-11-03 20:03:041149 void MailboxReleased(const gpu::SyncToken& sync_token, bool lost_resource) {
dyene5db881b2016-03-01 19:47:031150 EXPECT_TRUE(sync_token.HasData());
[email protected]b04264f92013-09-13 23:37:291151 ++mailbox_returned_;
1152 }
1153
dcheng716bedf2014-10-21 09:51:081154 void SetupTree() override {
loyso0940d412016-03-14 01:30:311155 scoped_refptr<Layer> root = Layer::Create();
[email protected]b04264f92013-09-13 23:37:291156 root->SetBounds(gfx::Size(10, 10));
[email protected]b04264f92013-09-13 23:37:291157 root->SetIsDrawable(true);
1158
loyso0940d412016-03-14 01:30:311159 solid_layer_ = SolidColorLayer::Create();
[email protected]b04264f92013-09-13 23:37:291160 solid_layer_->SetBounds(gfx::Size(10, 10));
1161 solid_layer_->SetIsDrawable(true);
1162 solid_layer_->SetBackgroundColor(SK_ColorWHITE);
1163 root->AddChild(solid_layer_);
1164
loyso0940d412016-03-14 01:30:311165 parent_layer_ = Layer::Create();
[email protected]b04264f92013-09-13 23:37:291166 parent_layer_->SetBounds(gfx::Size(10, 10));
1167 parent_layer_->SetIsDrawable(true);
1168 root->AddChild(parent_layer_);
1169
loyso0940d412016-03-14 01:30:311170 texture_layer_ = TextureLayer::CreateForMailbox(this);
[email protected]b04264f92013-09-13 23:37:291171 texture_layer_->SetBounds(gfx::Size(10, 10));
[email protected]b04264f92013-09-13 23:37:291172 texture_layer_->SetIsDrawable(true);
1173 parent_layer_->AddChild(texture_layer_);
1174
khushalsagar86928f92016-08-17 21:49:051175 layer_tree()->SetRootLayer(root);
[email protected]b04264f92013-09-13 23:37:291176 LayerTreeTest::SetupTree();
1177 }
1178
dcheng716bedf2014-10-21 09:51:081179 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
[email protected]b04264f92013-09-13 23:37:291180
dcheng716bedf2014-10-21 09:51:081181 void DidCommitAndDrawFrame() override {
[email protected]b04264f92013-09-13 23:37:291182 ++commit_count_;
1183 switch (commit_count_) {
1184 case 1:
1185 // We should have updated the layer, committing the texture.
1186 EXPECT_EQ(1, prepare_called_);
1187 // Make layer invisible.
1188 parent_layer_->SetOpacity(0.f);
1189 break;
1190 case 2:
1191 // Layer shouldn't have been updated.
1192 EXPECT_EQ(1, prepare_called_);
1193 // Change the texture.
1194 mailbox_ = MakeMailbox('2');
1195 mailbox_changed_ = true;
1196 texture_layer_->SetNeedsDisplay();
1197 // Force a change to make sure we draw a frame.
1198 solid_layer_->SetBackgroundColor(SK_ColorGRAY);
1199 break;
1200 case 3:
1201 // Layer shouldn't have been updated.
1202 EXPECT_EQ(1, prepare_called_);
1203 // So the old mailbox isn't returned yet.
1204 EXPECT_EQ(0, mailbox_returned_);
1205 // Make layer visible again.
jaydasikacf223762016-05-16 23:02:091206 parent_layer_->SetOpacity(0.9f);
[email protected]b04264f92013-09-13 23:37:291207 break;
1208 case 4:
1209 // Layer should have been updated.
1210 EXPECT_EQ(2, prepare_called_);
1211 // So the old mailbox should have been returned already.
1212 EXPECT_EQ(1, mailbox_returned_);
1213 texture_layer_->ClearClient();
1214 break;
1215 case 5:
1216 EXPECT_EQ(2, mailbox_returned_);
1217 EndTest();
1218 break;
1219 default:
1220 NOTREACHED();
1221 break;
1222 }
1223 }
1224
dcheng716bedf2014-10-21 09:51:081225 void AfterTest() override {}
[email protected]b04264f92013-09-13 23:37:291226
1227 private:
1228 scoped_refptr<SolidColorLayer> solid_layer_;
1229 scoped_refptr<Layer> parent_layer_;
1230 scoped_refptr<TextureLayer> texture_layer_;
1231
1232 // Used on the main thread.
1233 bool mailbox_changed_;
1234 TextureMailbox mailbox_;
1235 int mailbox_returned_;
1236 int prepare_called_;
1237 int commit_count_;
1238};
1239
1240SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerChangeInvisibleMailboxTest);
1241
[email protected]0d7fb302014-01-23 21:30:471242// Test that TextureLayerImpl::ReleaseResources can be called which releases
1243// the mailbox back to TextureLayerClient.
1244class TextureLayerReleaseResourcesBase
1245 : public LayerTreeTest,
1246 public TextureLayerClient {
1247 public:
1248 // TextureLayerClient implementation.
dcheng716bedf2014-10-21 09:51:081249 bool PrepareTextureMailbox(
[email protected]0d7fb302014-01-23 21:30:471250 TextureMailbox* mailbox,
danakj4d0dd802016-08-23 22:10:061251 std::unique_ptr<SingleReleaseCallback>* release_callback) override {
dyene5db881b2016-03-01 19:47:031252 *mailbox = TextureMailbox(MailboxFromChar('1'), SyncTokenFromUInt(1),
1253 GL_TEXTURE_2D);
[email protected]0d7fb302014-01-23 21:30:471254 *release_callback = SingleReleaseCallback::Create(
1255 base::Bind(&TextureLayerReleaseResourcesBase::MailboxReleased,
1256 base::Unretained(this)));
1257 return true;
1258 }
1259
dyencc16ed4d2015-11-03 20:03:041260 void MailboxReleased(const gpu::SyncToken& sync_token, bool lost_resource) {
[email protected]0d7fb302014-01-23 21:30:471261 mailbox_released_ = true;
1262 }
1263
dcheng716bedf2014-10-21 09:51:081264 void SetupTree() override {
[email protected]0d7fb302014-01-23 21:30:471265 LayerTreeTest::SetupTree();
1266
1267 scoped_refptr<TextureLayer> texture_layer =
loyso0940d412016-03-14 01:30:311268 TextureLayer::CreateForMailbox(this);
[email protected]0d7fb302014-01-23 21:30:471269 texture_layer->SetBounds(gfx::Size(10, 10));
[email protected]0d7fb302014-01-23 21:30:471270 texture_layer->SetIsDrawable(true);
1271
khushalsagar86928f92016-08-17 21:49:051272 layer_tree()->root_layer()->AddChild(texture_layer);
jaydasikabd6f15a2016-04-21 19:45:371273 texture_layer_id_ = texture_layer->id();
[email protected]0d7fb302014-01-23 21:30:471274 }
1275
dcheng716bedf2014-10-21 09:51:081276 void BeginTest() override {
[email protected]0d7fb302014-01-23 21:30:471277 mailbox_released_ = false;
1278 PostSetNeedsCommitToMainThread();
1279 }
1280
dcheng716bedf2014-10-21 09:51:081281 void DidCommitAndDrawFrame() override { EndTest(); }
[email protected]0d7fb302014-01-23 21:30:471282
dcheng716bedf2014-10-21 09:51:081283 void AfterTest() override { EXPECT_TRUE(mailbox_released_); }
[email protected]0d7fb302014-01-23 21:30:471284
jaydasikabd6f15a2016-04-21 19:45:371285 protected:
1286 int texture_layer_id_;
1287
[email protected]0d7fb302014-01-23 21:30:471288 private:
1289 bool mailbox_released_;
1290};
1291
1292class TextureLayerReleaseResourcesAfterCommit
1293 : public TextureLayerReleaseResourcesBase {
1294 public:
dcheng716bedf2014-10-21 09:51:081295 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
kulkarni.a4015690f12014-10-10 13:50:061296 LayerTreeImpl* tree = nullptr;
danakj009cdfdf2015-02-17 22:35:141297 tree = host_impl->sync_tree();
jaydasikabd6f15a2016-04-21 19:45:371298 tree->LayerById(texture_layer_id_)->ReleaseResources();
[email protected]0d7fb302014-01-23 21:30:471299 }
1300};
1301
1302SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerReleaseResourcesAfterCommit);
1303
1304class TextureLayerReleaseResourcesAfterActivate
1305 : public TextureLayerReleaseResourcesBase {
1306 public:
dcheng716bedf2014-10-21 09:51:081307 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
jaydasikabd6f15a2016-04-21 19:45:371308 host_impl->active_tree()->LayerById(texture_layer_id_)->ReleaseResources();
[email protected]0d7fb302014-01-23 21:30:471309 }
1310};
1311
1312SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerReleaseResourcesAfterActivate);
1313
[email protected]9c2bd822013-07-26 12:30:171314class TextureLayerWithMailboxMainThreadDeleted : public LayerTreeTest {
1315 public:
dyencc16ed4d2015-11-03 20:03:041316 void ReleaseCallback(const gpu::SyncToken& sync_token, bool lost_resource) {
[email protected]9794fb32013-08-29 09:49:591317 EXPECT_EQ(true, main_thread_.CalledOnValidThread());
[email protected]9c2bd822013-07-26 12:30:171318 EXPECT_FALSE(lost_resource);
1319 ++callback_count_;
1320 EndTest();
1321 }
1322
1323 void SetMailbox(char mailbox_char) {
[email protected]9794fb32013-08-29 09:49:591324 EXPECT_EQ(true, main_thread_.CalledOnValidThread());
danakj60bc3bc2016-04-09 00:24:481325 std::unique_ptr<SingleReleaseCallback> callback =
1326 SingleReleaseCallback::Create(base::Bind(
[email protected]9c2bd822013-07-26 12:30:171327 &TextureLayerWithMailboxMainThreadDeleted::ReleaseCallback,
1328 base::Unretained(this)));
dyene5db881b2016-03-01 19:47:031329 layer_->SetTextureMailbox(
1330 TextureMailbox(MailboxFromChar(mailbox_char),
1331 SyncTokenFromUInt(static_cast<uint32_t>(mailbox_char)),
1332 GL_TEXTURE_2D),
1333 std::move(callback));
[email protected]9c2bd822013-07-26 12:30:171334 }
1335
dcheng716bedf2014-10-21 09:51:081336 void SetupTree() override {
[email protected]9c2bd822013-07-26 12:30:171337 gfx::Size bounds(100, 100);
loyso0940d412016-03-14 01:30:311338 root_ = Layer::Create();
[email protected]9c2bd822013-07-26 12:30:171339 root_->SetBounds(bounds);
1340
loyso0940d412016-03-14 01:30:311341 layer_ = TextureLayer::CreateForMailbox(nullptr);
[email protected]9c2bd822013-07-26 12:30:171342 layer_->SetIsDrawable(true);
[email protected]9c2bd822013-07-26 12:30:171343 layer_->SetBounds(bounds);
1344
1345 root_->AddChild(layer_);
khushalsagar86928f92016-08-17 21:49:051346 layer_tree()->SetRootLayer(root_);
1347 layer_tree()->SetViewportSize(bounds);
[email protected]9c2bd822013-07-26 12:30:171348 }
1349
dcheng716bedf2014-10-21 09:51:081350 void BeginTest() override {
[email protected]9794fb32013-08-29 09:49:591351 EXPECT_EQ(true, main_thread_.CalledOnValidThread());
1352
[email protected]9c2bd822013-07-26 12:30:171353 callback_count_ = 0;
1354
1355 // Set the mailbox on the main thread.
1356 SetMailbox('1');
1357 EXPECT_EQ(0, callback_count_);
1358
1359 PostSetNeedsCommitToMainThread();
1360 }
1361
dcheng716bedf2014-10-21 09:51:081362 void DidCommitAndDrawFrame() override {
[email protected]9c2bd822013-07-26 12:30:171363 switch (layer_tree_host()->source_frame_number()) {
1364 case 1:
1365 // Delete the TextureLayer on the main thread while the mailbox is in
1366 // the impl tree.
1367 layer_->RemoveFromParent();
kulkarni.a4015690f12014-10-10 13:50:061368 layer_ = nullptr;
[email protected]9c2bd822013-07-26 12:30:171369 break;
1370 }
1371 }
1372
dcheng716bedf2014-10-21 09:51:081373 void AfterTest() override { EXPECT_EQ(1, callback_count_); }
[email protected]9c2bd822013-07-26 12:30:171374
1375 private:
[email protected]9794fb32013-08-29 09:49:591376 base::ThreadChecker main_thread_;
[email protected]9c2bd822013-07-26 12:30:171377 int callback_count_;
1378 scoped_refptr<Layer> root_;
1379 scoped_refptr<TextureLayer> layer_;
1380};
1381
danakj0943a112016-08-11 00:33:461382SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerWithMailboxMainThreadDeleted);
[email protected]9c2bd822013-07-26 12:30:171383
1384class TextureLayerWithMailboxImplThreadDeleted : public LayerTreeTest {
1385 public:
dyencc16ed4d2015-11-03 20:03:041386 void ReleaseCallback(const gpu::SyncToken& sync_token, bool lost_resource) {
[email protected]9794fb32013-08-29 09:49:591387 EXPECT_EQ(true, main_thread_.CalledOnValidThread());
[email protected]9c2bd822013-07-26 12:30:171388 EXPECT_FALSE(lost_resource);
1389 ++callback_count_;
1390 EndTest();
1391 }
1392
1393 void SetMailbox(char mailbox_char) {
[email protected]9794fb32013-08-29 09:49:591394 EXPECT_EQ(true, main_thread_.CalledOnValidThread());
danakj60bc3bc2016-04-09 00:24:481395 std::unique_ptr<SingleReleaseCallback> callback =
1396 SingleReleaseCallback::Create(base::Bind(
[email protected]9c2bd822013-07-26 12:30:171397 &TextureLayerWithMailboxImplThreadDeleted::ReleaseCallback,
1398 base::Unretained(this)));
dyene5db881b2016-03-01 19:47:031399 layer_->SetTextureMailbox(
1400 TextureMailbox(MailboxFromChar(mailbox_char),
1401 SyncTokenFromUInt(static_cast<uint32_t>(mailbox_char)),
1402 GL_TEXTURE_2D),
1403 std::move(callback));
[email protected]9c2bd822013-07-26 12:30:171404 }
1405
dcheng716bedf2014-10-21 09:51:081406 void SetupTree() override {
[email protected]9c2bd822013-07-26 12:30:171407 gfx::Size bounds(100, 100);
loyso0940d412016-03-14 01:30:311408 root_ = Layer::Create();
[email protected]9c2bd822013-07-26 12:30:171409 root_->SetBounds(bounds);
1410
loyso0940d412016-03-14 01:30:311411 layer_ = TextureLayer::CreateForMailbox(nullptr);
[email protected]9c2bd822013-07-26 12:30:171412 layer_->SetIsDrawable(true);
[email protected]9c2bd822013-07-26 12:30:171413 layer_->SetBounds(bounds);
1414
1415 root_->AddChild(layer_);
khushalsagar86928f92016-08-17 21:49:051416 layer_tree()->SetRootLayer(root_);
1417 layer_tree()->SetViewportSize(bounds);
[email protected]9c2bd822013-07-26 12:30:171418 }
1419
dcheng716bedf2014-10-21 09:51:081420 void BeginTest() override {
[email protected]9794fb32013-08-29 09:49:591421 EXPECT_EQ(true, main_thread_.CalledOnValidThread());
1422
[email protected]9c2bd822013-07-26 12:30:171423 callback_count_ = 0;
1424
1425 // Set the mailbox on the main thread.
1426 SetMailbox('1');
1427 EXPECT_EQ(0, callback_count_);
1428
1429 PostSetNeedsCommitToMainThread();
1430 }
1431
dcheng716bedf2014-10-21 09:51:081432 void DidCommitAndDrawFrame() override {
[email protected]9c2bd822013-07-26 12:30:171433 switch (layer_tree_host()->source_frame_number()) {
1434 case 1:
1435 // Remove the TextureLayer on the main thread while the mailbox is in
1436 // the impl tree, but don't delete the TextureLayer until after the impl
1437 // tree side is deleted.
1438 layer_->RemoveFromParent();
1439 break;
1440 case 2:
kulkarni.a4015690f12014-10-10 13:50:061441 layer_ = nullptr;
[email protected]9c2bd822013-07-26 12:30:171442 break;
1443 }
1444 }
1445
dcheng716bedf2014-10-21 09:51:081446 void AfterTest() override { EXPECT_EQ(1, callback_count_); }
[email protected]9c2bd822013-07-26 12:30:171447
1448 private:
[email protected]9794fb32013-08-29 09:49:591449 base::ThreadChecker main_thread_;
[email protected]9c2bd822013-07-26 12:30:171450 int callback_count_;
1451 scoped_refptr<Layer> root_;
1452 scoped_refptr<TextureLayer> layer_;
1453};
1454
danakj0943a112016-08-11 00:33:461455SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerWithMailboxImplThreadDeleted);
[email protected]9c2bd822013-07-26 12:30:171456
[email protected]ba565742012-11-10 09:29:481457} // namespace
1458} // namespace cc