[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 5 | #include "cc/layers/texture_layer.h" |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 6 | |
avi | 02a4d17 | 2015-12-21 06:14:36 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 10 | #include <algorithm> |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 11 | #include <string> |
| 12 | |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 13 | #include "base/bind.h" |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 14 | #include "base/callback.h" |
skyostil | 0fd1dad | 2015-04-13 20:11:48 | [diff] [blame] | 15 | #include "base/location.h" |
avi | 02a4d17 | 2015-12-21 06:14:36 | [diff] [blame] | 16 | #include "base/macros.h" |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 17 | #include "base/memory/ptr_util.h" |
skyostil | 0fd1dad | 2015-04-13 20:11:48 | [diff] [blame] | 18 | #include "base/single_thread_task_runner.h" |
danakj | 3c3973b | 2015-08-25 21:50:18 | [diff] [blame] | 19 | #include "base/synchronization/lock.h" |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 20 | #include "base/synchronization/waitable_event.h" |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 21 | #include "base/threading/thread.h" |
gab | 39cb4ed7 | 2016-05-11 17:45:19 | [diff] [blame] | 22 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 23 | #include "base/time/time.h" |
loyso | ab32ee7 | 2016-06-08 03:33:18 | [diff] [blame] | 24 | #include "cc/animation/animation_host.h" |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 25 | #include "cc/layers/solid_color_layer.h" |
[email protected] | 97d519fb | 2013-03-29 02:27:54 | [diff] [blame] | 26 | #include "cc/layers/texture_layer_client.h" |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 27 | #include "cc/layers/texture_layer_impl.h" |
[email protected] | e00bab02 | 2013-08-19 00:42:45 | [diff] [blame] | 28 | #include "cc/resources/returned_resource.h" |
khushalsagar | b64b360d | 2015-10-21 19:25:16 | [diff] [blame] | 29 | #include "cc/test/fake_impl_task_runner_provider.h" |
danakj | c7afae5 | 2017-06-20 21:12:41 | [diff] [blame] | 30 | #include "cc/test/fake_layer_tree_frame_sink.h" |
[email protected] | 101441ce | 2012-10-16 01:45:03 | [diff] [blame] | 31 | #include "cc/test/fake_layer_tree_host_client.h" |
[email protected] | 586d51ed | 2012-12-07 20:31:45 | [diff] [blame] | 32 | #include "cc/test/fake_layer_tree_host_impl.h" |
[email protected] | 06d68d0 | 2013-04-19 18:46:21 | [diff] [blame] | 33 | #include "cc/test/layer_test_common.h" |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 34 | #include "cc/test/layer_tree_test.h" |
danakj | ffc181a | 2016-07-22 22:48:43 | [diff] [blame] | 35 | #include "cc/test/stub_layer_tree_host_single_thread_client.h" |
reveman | 34b7a152 | 2015-03-23 20:27:47 | [diff] [blame] | 36 | #include "cc/test/test_task_graph_runner.h" |
[email protected] | c2610b9f | 2013-10-31 06:54:59 | [diff] [blame] | 37 | #include "cc/test/test_web_graphics_context_3d.h" |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 38 | #include "cc/trees/blocking_task_runner.h" |
khushalsagar | e0e4486e | 2017-01-25 03:15:03 | [diff] [blame] | 39 | #include "cc/trees/layer_tree_host.h" |
[email protected] | 556fd29 | 2013-03-18 08:03:04 | [diff] [blame] | 40 | #include "cc/trees/layer_tree_impl.h" |
| 41 | #include "cc/trees/single_thread_proxy.h" |
Xu Xing | 3254916 | 2017-07-17 22:25:43 | [diff] [blame] | 42 | #include "components/viz/common/gpu/context_provider.h" |
danakj | c391f33 | 2017-07-12 20:45:52 | [diff] [blame] | 43 | #include "components/viz/test/test_layer_tree_frame_sink.h" |
[email protected] | 0bf5a20 | 2013-07-10 14:50:54 | [diff] [blame] | 44 | #include "gpu/GLES2/gl2extchromium.h" |
[email protected] | 7f0c53db | 2012-10-02 00:23:18 | [diff] [blame] | 45 | #include "testing/gmock/include/gmock/gmock.h" |
| 46 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 47 | |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 48 | using ::testing::Mock; |
| 49 | using ::testing::_; |
| 50 | using ::testing::AtLeast; |
| 51 | using ::testing::AnyNumber; |
[email protected] | d72d9e0 | 2014-04-03 18:40:09 | [diff] [blame] | 52 | using ::testing::InvokeWithoutArgs; |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 53 | |
[email protected] | ba56574 | 2012-11-10 09:29:48 | [diff] [blame] | 54 | namespace cc { |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 55 | namespace { |
| 56 | |
[email protected] | e0a4d73 | 2014-02-15 00:23:26 | [diff] [blame] | 57 | gpu::Mailbox MailboxFromChar(char value) { |
[email protected] | df41e25 | 2014-02-03 23:39:50 | [diff] [blame] | 58 | gpu::Mailbox mailbox; |
[email protected] | e0a4d73 | 2014-02-15 00:23:26 | [diff] [blame] | 59 | memset(mailbox.name, value, sizeof(mailbox.name)); |
[email protected] | df41e25 | 2014-02-03 23:39:50 | [diff] [blame] | 60 | return mailbox; |
| 61 | } |
| 62 | |
dyen | 398dd014 | 2016-01-21 22:05:56 | [diff] [blame] | 63 | gpu::SyncToken SyncTokenFromUInt(uint32_t value) { |
lukasza | 2573ce7d | 2016-02-16 19:17:22 | [diff] [blame] | 64 | return gpu::SyncToken(gpu::CommandBufferNamespace::GPU_IO, 0, |
| 65 | gpu::CommandBufferId::FromUnsafeValue(0x123), value); |
dyen | 398dd014 | 2016-01-21 22:05:56 | [diff] [blame] | 66 | } |
| 67 | |
khushalsagar | e0e4486e | 2017-01-25 03:15:03 | [diff] [blame] | 68 | class MockLayerTreeHost : public LayerTreeHost { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 69 | public: |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 70 | static std::unique_ptr<MockLayerTreeHost> Create( |
danakj | cf61058 | 2015-06-16 22:48:56 | [diff] [blame] | 71 | FakeLayerTreeHostClient* client, |
loyso | 2cb3f32f | 2016-11-08 07:08:34 | [diff] [blame] | 72 | TaskGraphRunner* task_graph_runner, |
| 73 | MutatorHost* mutator_host) { |
khushalsagar | e0e4486e | 2017-01-25 03:15:03 | [diff] [blame] | 74 | LayerTreeHost::InitParams params; |
sadrul | 6780f3da | 2015-05-11 17:01:52 | [diff] [blame] | 75 | params.client = client; |
danakj | cf61058 | 2015-06-16 22:48:56 | [diff] [blame] | 76 | params.task_graph_runner = task_graph_runner; |
loyso | 2cb3f32f | 2016-11-08 07:08:34 | [diff] [blame] | 77 | params.mutator_host = mutator_host; |
sadrul | 6780f3da | 2015-05-11 17:01:52 | [diff] [blame] | 78 | LayerTreeSettings settings; |
| 79 | params.settings = &settings; |
danakj | ffc181a | 2016-07-22 22:48:43 | [diff] [blame] | 80 | return base::WrapUnique(new MockLayerTreeHost(¶ms)); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 81 | } |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 82 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 83 | MOCK_METHOD0(SetNeedsCommit, void()); |
[email protected] | 3519b87 | 2013-07-30 07:17:50 | [diff] [blame] | 84 | MOCK_METHOD0(SetNeedsUpdateLayers, void()); |
[email protected] | aeeb337 | 2013-11-05 14:05:54 | [diff] [blame] | 85 | MOCK_METHOD0(StartRateLimiter, void()); |
| 86 | MOCK_METHOD0(StopRateLimiter, void()); |
sadrul | 6780f3da | 2015-05-11 17:01:52 | [diff] [blame] | 87 | |
| 88 | private: |
khushalsagar | e0e4486e | 2017-01-25 03:15:03 | [diff] [blame] | 89 | explicit MockLayerTreeHost(LayerTreeHost::InitParams* params) |
| 90 | : LayerTreeHost(params, CompositorMode::SINGLE_THREADED) { |
danakj | ffc181a | 2016-07-22 22:48:43 | [diff] [blame] | 91 | InitializeSingleThreaded(&single_thread_client_, |
enne | 2b0ad68 | 2016-09-21 01:44:47 | [diff] [blame] | 92 | base::ThreadTaskRunnerHandle::Get()); |
sadrul | 6780f3da | 2015-05-11 17:01:52 | [diff] [blame] | 93 | } |
danakj | ffc181a | 2016-07-22 22:48:43 | [diff] [blame] | 94 | |
| 95 | StubLayerTreeHostSingleThreadClient single_thread_client_; |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 96 | }; |
| 97 | |
[email protected] | d72d9e0 | 2014-04-03 18:40:09 | [diff] [blame] | 98 | class FakeTextureLayerClient : public TextureLayerClient { |
| 99 | public: |
[email protected] | 17e0843 | 2014-04-10 00:41:11 | [diff] [blame] | 100 | FakeTextureLayerClient() : mailbox_changed_(true) {} |
[email protected] | d72d9e0 | 2014-04-03 18:40:09 | [diff] [blame] | 101 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 102 | bool PrepareTextureMailbox( |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 103 | viz::TextureMailbox* mailbox, |
danakj | 4d0dd80 | 2016-08-23 22:10:06 | [diff] [blame] | 104 | std::unique_ptr<SingleReleaseCallback>* release_callback) override { |
[email protected] | d72d9e0 | 2014-04-03 18:40:09 | [diff] [blame] | 105 | if (!mailbox_changed_) |
| 106 | return false; |
| 107 | |
| 108 | *mailbox = mailbox_; |
danakj | a04855a | 2015-11-18 20:39:10 | [diff] [blame] | 109 | *release_callback = std::move(release_callback_); |
[email protected] | d72d9e0 | 2014-04-03 18:40:09 | [diff] [blame] | 110 | mailbox_changed_ = false; |
| 111 | return true; |
| 112 | } |
| 113 | |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 114 | void set_mailbox(const viz::TextureMailbox& mailbox, |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 115 | std::unique_ptr<SingleReleaseCallback> release_callback) { |
[email protected] | d72d9e0 | 2014-04-03 18:40:09 | [diff] [blame] | 116 | mailbox_ = mailbox; |
danakj | a04855a | 2015-11-18 20:39:10 | [diff] [blame] | 117 | release_callback_ = std::move(release_callback); |
[email protected] | d72d9e0 | 2014-04-03 18:40:09 | [diff] [blame] | 118 | mailbox_changed_ = true; |
| 119 | } |
| 120 | |
| 121 | private: |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 122 | viz::TextureMailbox mailbox_; |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 123 | std::unique_ptr<SingleReleaseCallback> release_callback_; |
[email protected] | d72d9e0 | 2014-04-03 18:40:09 | [diff] [blame] | 124 | bool mailbox_changed_; |
| 125 | DISALLOW_COPY_AND_ASSIGN(FakeTextureLayerClient); |
| 126 | }; |
| 127 | |
| 128 | class MockMailboxCallback { |
| 129 | public: |
| 130 | MOCK_METHOD3(Release, |
| 131 | void(const gpu::Mailbox& mailbox, |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 132 | const gpu::SyncToken& sync_token, |
[email protected] | d72d9e0 | 2014-04-03 18:40:09 | [diff] [blame] | 133 | bool lost_resource)); |
| 134 | MOCK_METHOD3(Release2, |
Fady Samuel | 4b5f986 | 2017-07-11 05:27:20 | [diff] [blame] | 135 | void(viz::SharedBitmap* shared_bitmap, |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 136 | const gpu::SyncToken& sync_token, |
[email protected] | d72d9e0 | 2014-04-03 18:40:09 | [diff] [blame] | 137 | bool lost_resource)); |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 138 | MOCK_METHOD4(ReleaseImpl, |
| 139 | void(const gpu::Mailbox& mailbox, |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 140 | const gpu::SyncToken& sync_token, |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 141 | bool lost_resource, |
| 142 | BlockingTaskRunner* main_thread_task_runner)); |
| 143 | MOCK_METHOD4(ReleaseImpl2, |
Fady Samuel | 4b5f986 | 2017-07-11 05:27:20 | [diff] [blame] | 144 | void(viz::SharedBitmap* shared_bitmap, |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 145 | const gpu::SyncToken& sync_token, |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 146 | bool lost_resource, |
| 147 | BlockingTaskRunner* main_thread_task_runner)); |
[email protected] | d72d9e0 | 2014-04-03 18:40:09 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | struct CommonMailboxObjects { |
Fady Samuel | 4b5f986 | 2017-07-11 05:27:20 | [diff] [blame] | 151 | explicit CommonMailboxObjects(viz::SharedBitmapManager* manager) |
[email protected] | d72d9e0 | 2014-04-03 18:40:09 | [diff] [blame] | 152 | : mailbox_name1_(MailboxFromChar('1')), |
| 153 | mailbox_name2_(MailboxFromChar('2')), |
lukasza | 2573ce7d | 2016-02-16 19:17:22 | [diff] [blame] | 154 | sync_token1_(gpu::CommandBufferNamespace::GPU_IO, |
| 155 | 123, |
| 156 | gpu::CommandBufferId::FromUnsafeValue(0x234), |
| 157 | 1), |
| 158 | sync_token2_(gpu::CommandBufferNamespace::GPU_IO, |
| 159 | 123, |
| 160 | gpu::CommandBufferId::FromUnsafeValue(0x234), |
| 161 | 2) { |
[email protected] | d72d9e0 | 2014-04-03 18:40:09 | [diff] [blame] | 162 | release_mailbox1_ = base::Bind(&MockMailboxCallback::Release, |
| 163 | base::Unretained(&mock_callback_), |
| 164 | mailbox_name1_); |
| 165 | release_mailbox2_ = base::Bind(&MockMailboxCallback::Release, |
| 166 | base::Unretained(&mock_callback_), |
| 167 | mailbox_name2_); |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 168 | release_mailbox1_impl_ = base::Bind(&MockMailboxCallback::ReleaseImpl, |
| 169 | base::Unretained(&mock_callback_), |
| 170 | mailbox_name1_); |
| 171 | release_mailbox2_impl_ = base::Bind(&MockMailboxCallback::ReleaseImpl, |
| 172 | base::Unretained(&mock_callback_), |
| 173 | mailbox_name2_); |
avi | 02a4d17 | 2015-12-21 06:14:36 | [diff] [blame] | 174 | const uint32_t arbitrary_target1 = GL_TEXTURE_2D; |
| 175 | const uint32_t arbitrary_target2 = GL_TEXTURE_EXTERNAL_OES; |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 176 | mailbox1_ = |
| 177 | viz::TextureMailbox(mailbox_name1_, sync_token1_, arbitrary_target1); |
| 178 | mailbox2_ = |
| 179 | viz::TextureMailbox(mailbox_name2_, sync_token2_, arbitrary_target2); |
[email protected] | d72d9e0 | 2014-04-03 18:40:09 | [diff] [blame] | 180 | gfx::Size size(128, 128); |
jbauman | 9015c8b | 2014-12-11 00:49:37 | [diff] [blame] | 181 | 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()); |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 189 | mailbox3_ = viz::TextureMailbox(shared_bitmap_.get(), size); |
[email protected] | d72d9e0 | 2014-04-03 18:40:09 | [diff] [blame] | 190 | } |
| 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_; |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 198 | ReleaseCallbackImpl release_mailbox1_impl_; |
| 199 | ReleaseCallbackImpl release_mailbox2_impl_; |
| 200 | ReleaseCallbackImpl release_mailbox3_impl_; |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 201 | viz::TextureMailbox mailbox1_; |
| 202 | viz::TextureMailbox mailbox2_; |
| 203 | viz::TextureMailbox mailbox3_; |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 204 | gpu::SyncToken sync_token1_; |
| 205 | gpu::SyncToken sync_token2_; |
Fady Samuel | 4b5f986 | 2017-07-11 05:27:20 | [diff] [blame] | 206 | std::unique_ptr<viz::SharedBitmap> shared_bitmap_; |
[email protected] | d72d9e0 | 2014-04-03 18:40:09 | [diff] [blame] | 207 | }; |
| 208 | |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 209 | class TextureLayerTest : public testing::Test { |
| 210 | public: |
| 211 | TextureLayerTest() |
danakj | c7afae5 | 2017-06-20 21:12:41 | [diff] [blame] | 212 | : layer_tree_frame_sink_(FakeLayerTreeFrameSink::Create3d()), |
| 213 | host_impl_(&task_runner_provider_, &task_graph_runner_), |
jbauman | 9015c8b | 2014-12-11 00:49:37 | [diff] [blame] | 214 | test_data_(&shared_bitmap_manager_) {} |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 215 | |
| 216 | protected: |
dcheng | 93a52eb | 2014-12-23 02:14:23 | [diff] [blame] | 217 | void SetUp() override { |
loyso | 2cb3f32f | 2016-11-08 07:08:34 | [diff] [blame] | 218 | animation_host_ = AnimationHost::CreateForTesting(ThreadInstance::MAIN); |
| 219 | layer_tree_host_ = MockLayerTreeHost::Create( |
| 220 | &fake_client_, &task_graph_runner_, animation_host_.get()); |
[email protected] | d72d9e0 | 2014-04-03 18:40:09 | [diff] [blame] | 221 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
khushalsagar | b69ba945 | 2017-01-27 22:20:07 | [diff] [blame] | 222 | layer_tree_host_->SetViewportSize(gfx::Size(10, 10)); |
[email protected] | d72d9e0 | 2014-04-03 18:40:09 | [diff] [blame] | 223 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 224 | } |
| 225 | |
dcheng | 93a52eb | 2014-12-23 02:14:23 | [diff] [blame] | 226 | void TearDown() override { |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 227 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 228 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
| 229 | |
loyso | 2cb3f32f | 2016-11-08 07:08:34 | [diff] [blame] | 230 | animation_host_->SetMutatorHostClient(nullptr); |
khushalsagar | b69ba945 | 2017-01-27 22:20:07 | [diff] [blame] | 231 | layer_tree_host_->SetRootLayer(nullptr); |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 232 | layer_tree_host_ = nullptr; |
loyso | 2cb3f32f | 2016-11-08 07:08:34 | [diff] [blame] | 233 | animation_host_ = nullptr; |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 234 | } |
| 235 | |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 236 | std::unique_ptr<MockLayerTreeHost> layer_tree_host_; |
loyso | 2cb3f32f | 2016-11-08 07:08:34 | [diff] [blame] | 237 | std::unique_ptr<AnimationHost> animation_host_; |
khushalsagar | b64b360d | 2015-10-21 19:25:16 | [diff] [blame] | 238 | FakeImplTaskRunnerProvider task_runner_provider_; |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 239 | FakeLayerTreeHostClient fake_client_; |
[email protected] | 4e2eb35 | 2014-03-20 17:25:45 | [diff] [blame] | 240 | TestSharedBitmapManager shared_bitmap_manager_; |
reveman | 34b7a152 | 2015-03-23 20:27:47 | [diff] [blame] | 241 | TestTaskGraphRunner task_graph_runner_; |
danakj | c7afae5 | 2017-06-20 21:12:41 | [diff] [blame] | 242 | std::unique_ptr<LayerTreeFrameSink> layer_tree_frame_sink_; |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 243 | FakeLayerTreeHostImpl host_impl_; |
jbauman | 9015c8b | 2014-12-11 00:49:37 | [diff] [blame] | 244 | CommonMailboxObjects test_data_; |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 245 | }; |
| 246 | |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 247 | TEST_F(TextureLayerTest, CheckPropertyChangeCausesCorrectBehavior) { |
kulkarni.a | 4015690f1 | 2014-10-10 13:50:06 | [diff] [blame] | 248 | scoped_refptr<TextureLayer> test_layer = |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 249 | TextureLayer::CreateForMailbox(nullptr); |
khushalsagar | b69ba945 | 2017-01-27 22:20:07 | [diff] [blame] | 250 | EXPECT_SET_NEEDS_COMMIT(1, layer_tree_host_->SetRootLayer(test_layer)); |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 251 | |
| 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)); |
jackhou | 10c9af4 | 2014-12-04 05:24:44 | [diff] [blame] | 255 | EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetNearestNeighbor(true)); |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 256 | 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] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 262 | } |
| 263 | |
[email protected] | df41e25 | 2014-02-03 23:39:50 | [diff] [blame] | 264 | class TestMailboxHolder : public TextureLayer::TextureMailboxHolder { |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 265 | public: |
[email protected] | df41e25 | 2014-02-03 23:39:50 | [diff] [blame] | 266 | using TextureLayer::TextureMailboxHolder::Create; |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 267 | |
| 268 | protected: |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 269 | ~TestMailboxHolder() override {} |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 270 | }; |
| 271 | |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 272 | class TextureLayerWithMailboxTest : public TextureLayerTest { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 273 | protected: |
dcheng | 93a52eb | 2014-12-23 02:14:23 | [diff] [blame] | 274 | void TearDown() override { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 275 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 276 | EXPECT_CALL( |
| 277 | test_data_.mock_callback_, |
| 278 | Release(test_data_.mailbox_name1_, test_data_.sync_token1_, false)) |
| 279 | .Times(1); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 280 | TextureLayerTest::TearDown(); |
| 281 | } |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 282 | }; |
| 283 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 284 | TEST_F(TextureLayerWithMailboxTest, ReplaceMailboxOnMainThreadBeforeCommit) { |
kulkarni.a | 4015690f1 | 2014-10-10 13:50:06 | [diff] [blame] | 285 | scoped_refptr<TextureLayer> test_layer = |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 286 | TextureLayer::CreateForMailbox(nullptr); |
[email protected] | 22898ed | 2013-06-01 04:52:30 | [diff] [blame] | 287 | ASSERT_TRUE(test_layer.get()); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 288 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 289 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
khushalsagar | b69ba945 | 2017-01-27 22:20:07 | [diff] [blame] | 290 | layer_tree_host_->SetRootLayer(test_layer); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 291 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 292 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 293 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 294 | test_layer->SetTextureMailbox( |
| 295 | test_data_.mailbox1_, |
| 296 | SingleReleaseCallback::Create(test_data_.release_mailbox1_)); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 297 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 298 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 299 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 300 | EXPECT_CALL( |
| 301 | test_data_.mock_callback_, |
| 302 | Release(test_data_.mailbox_name1_, test_data_.sync_token1_, false)) |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 303 | .Times(1); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 304 | test_layer->SetTextureMailbox( |
| 305 | test_data_.mailbox2_, |
| 306 | SingleReleaseCallback::Create(test_data_.release_mailbox2_)); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 307 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 308 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 309 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 310 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 311 | EXPECT_CALL( |
| 312 | test_data_.mock_callback_, |
| 313 | Release(test_data_.mailbox_name2_, test_data_.sync_token2_, false)) |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 314 | .Times(1); |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 315 | test_layer->SetTextureMailbox(viz::TextureMailbox(), nullptr); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 316 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 317 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 318 | |
[email protected] | 80d42bd | 2013-08-30 19:13:45 | [diff] [blame] | 319 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 320 | test_layer->SetTextureMailbox( |
| 321 | test_data_.mailbox3_, |
| 322 | SingleReleaseCallback::Create(test_data_.release_mailbox3_)); |
[email protected] | 42f40a5 | 2013-06-08 04:38:51 | [diff] [blame] | 323 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 324 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 325 | |
[email protected] | 42f40a5 | 2013-06-08 04:38:51 | [diff] [blame] | 326 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 327 | EXPECT_CALL(test_data_.mock_callback_, |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 328 | Release2(test_data_.shared_bitmap_.get(), _, false)) |
| 329 | .Times(1); |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 330 | test_layer->SetTextureMailbox(viz::TextureMailbox(), nullptr); |
[email protected] | 42f40a5 | 2013-06-08 04:38:51 | [diff] [blame] | 331 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 332 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 333 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 334 | // Test destructor. |
| 335 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 336 | test_layer->SetTextureMailbox( |
| 337 | test_data_.mailbox1_, |
| 338 | SingleReleaseCallback::Create(test_data_.release_mailbox1_)); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 339 | } |
| 340 | |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 341 | class TextureLayerMailboxHolderTest : public TextureLayerTest { |
| 342 | public: |
| 343 | TextureLayerMailboxHolderTest() |
| 344 | : main_thread_("MAIN") { |
| 345 | main_thread_.Start(); |
fdoray | 70df5a9 | 2016-06-22 21:13:59 | [diff] [blame] | 346 | main_thread_.task_runner()->PostTask( |
tzik | 4604bb5 | 2017-04-13 21:50:22 | [diff] [blame] | 347 | FROM_HERE, |
| 348 | base::BindOnce(&TextureLayerMailboxHolderTest::InitializeOnMain, |
| 349 | base::Unretained(this))); |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 350 | Wait(main_thread_); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | void Wait(const base::Thread& thread) { |
gab | cca5311 | 2016-06-08 20:13:28 | [diff] [blame] | 354 | base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 355 | base::WaitableEvent::InitialState::NOT_SIGNALED); |
fdoray | 70df5a9 | 2016-06-22 21:13:59 | [diff] [blame] | 356 | thread.task_runner()->PostTask( |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 357 | FROM_HERE, |
tzik | 4604bb5 | 2017-04-13 21:50:22 | [diff] [blame] | 358 | base::BindOnce(&base::WaitableEvent::Signal, base::Unretained(&event))); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 359 | event.Wait(); |
| 360 | } |
| 361 | |
| 362 | void CreateMainRef() { |
| 363 | main_ref_ = TestMailboxHolder::Create( |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 364 | test_data_.mailbox1_, |
danakj | a5a05ba0 | 2015-11-20 20:14:21 | [diff] [blame] | 365 | SingleReleaseCallback::Create(test_data_.release_mailbox1_)); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 366 | } |
| 367 | |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 368 | void ReleaseMainRef() { main_ref_ = nullptr; } |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 369 | |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 370 | void CreateImplRef(std::unique_ptr<SingleReleaseCallbackImpl>* impl_ref) { |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 371 | *impl_ref = main_ref_->holder()->GetCallbackForImplThread(); |
| 372 | } |
| 373 | |
| 374 | void CapturePostTasksAndWait(base::WaitableEvent* begin_capture, |
| 375 | base::WaitableEvent* wait_for_capture, |
| 376 | base::WaitableEvent* stop_capture) { |
| 377 | begin_capture->Wait(); |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 378 | BlockingTaskRunner::CapturePostTasks capture( |
| 379 | main_thread_task_runner_.get()); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 380 | wait_for_capture->Signal(); |
| 381 | stop_capture->Wait(); |
| 382 | } |
| 383 | |
| 384 | protected: |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 385 | void InitializeOnMain() { |
| 386 | main_thread_task_runner_ = |
skyostil | 0fd1dad | 2015-04-13 20:11:48 | [diff] [blame] | 387 | BlockingTaskRunner::Create(main_thread_.task_runner()); |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 388 | } |
| 389 | |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 390 | std::unique_ptr<TestMailboxHolder::MainThreadReference> main_ref_; |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 391 | base::Thread main_thread_; |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 392 | std::unique_ptr<BlockingTaskRunner> main_thread_task_runner_; |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 393 | }; |
| 394 | |
| 395 | TEST_F(TextureLayerMailboxHolderTest, TwoCompositors_BothReleaseThenMain) { |
kulkarni.a | 4015690f1 | 2014-10-10 13:50:06 | [diff] [blame] | 396 | scoped_refptr<TextureLayer> test_layer = |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 397 | TextureLayer::CreateForMailbox(nullptr); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 398 | ASSERT_TRUE(test_layer.get()); |
| 399 | |
fdoray | 70df5a9 | 2016-06-22 21:13:59 | [diff] [blame] | 400 | main_thread_.task_runner()->PostTask( |
tzik | 4604bb5 | 2017-04-13 21:50:22 | [diff] [blame] | 401 | FROM_HERE, base::BindOnce(&TextureLayerMailboxHolderTest::CreateMainRef, |
| 402 | base::Unretained(this))); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 403 | |
| 404 | Wait(main_thread_); |
| 405 | |
| 406 | // The texture layer is attached to compositor1, and passes a reference to its |
| 407 | // impl tree. |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 408 | std::unique_ptr<SingleReleaseCallbackImpl> compositor1; |
fdoray | 70df5a9 | 2016-06-22 21:13:59 | [diff] [blame] | 409 | main_thread_.task_runner()->PostTask( |
tzik | 4604bb5 | 2017-04-13 21:50:22 | [diff] [blame] | 410 | FROM_HERE, base::BindOnce(&TextureLayerMailboxHolderTest::CreateImplRef, |
| 411 | base::Unretained(this), &compositor1)); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 412 | |
| 413 | // Then the texture layer is removed and attached to compositor2, and passes a |
| 414 | // reference to its impl tree. |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 415 | std::unique_ptr<SingleReleaseCallbackImpl> compositor2; |
fdoray | 70df5a9 | 2016-06-22 21:13:59 | [diff] [blame] | 416 | main_thread_.task_runner()->PostTask( |
tzik | 4604bb5 | 2017-04-13 21:50:22 | [diff] [blame] | 417 | FROM_HERE, base::BindOnce(&TextureLayerMailboxHolderTest::CreateImplRef, |
| 418 | base::Unretained(this), &compositor2)); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 419 | |
| 420 | Wait(main_thread_); |
| 421 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 422 | |
| 423 | // The compositors both destroy their impl trees before the main thread layer |
| 424 | // is destroyed. |
dyen | 398dd014 | 2016-01-21 22:05:56 | [diff] [blame] | 425 | compositor1->Run(SyncTokenFromUInt(100), false, |
| 426 | main_thread_task_runner_.get()); |
| 427 | compositor2->Run(SyncTokenFromUInt(200), false, |
| 428 | main_thread_task_runner_.get()); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 429 | |
| 430 | Wait(main_thread_); |
| 431 | |
| 432 | EXPECT_CALL(test_data_.mock_callback_, Release(_, _, _)).Times(0); |
| 433 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 434 | |
| 435 | // The main thread ref is the last one, so the mailbox is released back to the |
| 436 | // embedder, with the last sync point provided by the impl trees. |
| 437 | EXPECT_CALL(test_data_.mock_callback_, |
dyen | 398dd014 | 2016-01-21 22:05:56 | [diff] [blame] | 438 | Release(test_data_.mailbox_name1_, SyncTokenFromUInt(200), false)) |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 439 | .Times(1); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 440 | |
fdoray | 70df5a9 | 2016-06-22 21:13:59 | [diff] [blame] | 441 | main_thread_.task_runner()->PostTask( |
tzik | 4604bb5 | 2017-04-13 21:50:22 | [diff] [blame] | 442 | FROM_HERE, base::BindOnce(&TextureLayerMailboxHolderTest::ReleaseMainRef, |
| 443 | base::Unretained(this))); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 444 | Wait(main_thread_); |
| 445 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 446 | } |
| 447 | |
| 448 | TEST_F(TextureLayerMailboxHolderTest, TwoCompositors_MainReleaseBetween) { |
kulkarni.a | 4015690f1 | 2014-10-10 13:50:06 | [diff] [blame] | 449 | scoped_refptr<TextureLayer> test_layer = |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 450 | TextureLayer::CreateForMailbox(nullptr); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 451 | ASSERT_TRUE(test_layer.get()); |
| 452 | |
fdoray | 70df5a9 | 2016-06-22 21:13:59 | [diff] [blame] | 453 | main_thread_.task_runner()->PostTask( |
tzik | 4604bb5 | 2017-04-13 21:50:22 | [diff] [blame] | 454 | FROM_HERE, base::BindOnce(&TextureLayerMailboxHolderTest::CreateMainRef, |
| 455 | base::Unretained(this))); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 456 | |
| 457 | Wait(main_thread_); |
| 458 | |
| 459 | // The texture layer is attached to compositor1, and passes a reference to its |
| 460 | // impl tree. |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 461 | std::unique_ptr<SingleReleaseCallbackImpl> compositor1; |
fdoray | 70df5a9 | 2016-06-22 21:13:59 | [diff] [blame] | 462 | main_thread_.task_runner()->PostTask( |
tzik | 4604bb5 | 2017-04-13 21:50:22 | [diff] [blame] | 463 | FROM_HERE, base::BindOnce(&TextureLayerMailboxHolderTest::CreateImplRef, |
| 464 | base::Unretained(this), &compositor1)); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 465 | |
| 466 | // Then the texture layer is removed and attached to compositor2, and passes a |
| 467 | // reference to its impl tree. |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 468 | std::unique_ptr<SingleReleaseCallbackImpl> compositor2; |
fdoray | 70df5a9 | 2016-06-22 21:13:59 | [diff] [blame] | 469 | main_thread_.task_runner()->PostTask( |
tzik | 4604bb5 | 2017-04-13 21:50:22 | [diff] [blame] | 470 | FROM_HERE, base::BindOnce(&TextureLayerMailboxHolderTest::CreateImplRef, |
| 471 | base::Unretained(this), &compositor2)); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 472 | |
| 473 | Wait(main_thread_); |
| 474 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 475 | |
| 476 | // One compositor destroys their impl tree. |
dyen | 398dd014 | 2016-01-21 22:05:56 | [diff] [blame] | 477 | compositor1->Run(SyncTokenFromUInt(100), false, |
| 478 | main_thread_task_runner_.get()); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 479 | |
| 480 | // Then the main thread reference is destroyed. |
fdoray | 70df5a9 | 2016-06-22 21:13:59 | [diff] [blame] | 481 | main_thread_.task_runner()->PostTask( |
tzik | 4604bb5 | 2017-04-13 21:50:22 | [diff] [blame] | 482 | FROM_HERE, base::BindOnce(&TextureLayerMailboxHolderTest::ReleaseMainRef, |
| 483 | base::Unretained(this))); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 484 | |
| 485 | Wait(main_thread_); |
| 486 | |
| 487 | EXPECT_CALL(test_data_.mock_callback_, Release(_, _, _)).Times(0); |
| 488 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 489 | |
| 490 | // The second impl reference is destroyed last, causing the mailbox to be |
| 491 | // released back to the embedder with the last sync point from the impl tree. |
| 492 | EXPECT_CALL(test_data_.mock_callback_, |
dyen | 398dd014 | 2016-01-21 22:05:56 | [diff] [blame] | 493 | Release(test_data_.mailbox_name1_, SyncTokenFromUInt(200), true)) |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 494 | .Times(1); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 495 | |
dyen | 398dd014 | 2016-01-21 22:05:56 | [diff] [blame] | 496 | compositor2->Run(SyncTokenFromUInt(200), true, |
| 497 | main_thread_task_runner_.get()); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 498 | Wait(main_thread_); |
| 499 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 500 | } |
| 501 | |
| 502 | TEST_F(TextureLayerMailboxHolderTest, TwoCompositors_MainReleasedFirst) { |
kulkarni.a | 4015690f1 | 2014-10-10 13:50:06 | [diff] [blame] | 503 | scoped_refptr<TextureLayer> test_layer = |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 504 | TextureLayer::CreateForMailbox(nullptr); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 505 | ASSERT_TRUE(test_layer.get()); |
| 506 | |
fdoray | 70df5a9 | 2016-06-22 21:13:59 | [diff] [blame] | 507 | main_thread_.task_runner()->PostTask( |
tzik | 4604bb5 | 2017-04-13 21:50:22 | [diff] [blame] | 508 | FROM_HERE, base::BindOnce(&TextureLayerMailboxHolderTest::CreateMainRef, |
| 509 | base::Unretained(this))); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 510 | |
| 511 | Wait(main_thread_); |
| 512 | |
| 513 | // The texture layer is attached to compositor1, and passes a reference to its |
| 514 | // impl tree. |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 515 | std::unique_ptr<SingleReleaseCallbackImpl> compositor1; |
fdoray | 70df5a9 | 2016-06-22 21:13:59 | [diff] [blame] | 516 | main_thread_.task_runner()->PostTask( |
tzik | 4604bb5 | 2017-04-13 21:50:22 | [diff] [blame] | 517 | FROM_HERE, base::BindOnce(&TextureLayerMailboxHolderTest::CreateImplRef, |
| 518 | base::Unretained(this), &compositor1)); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 519 | |
| 520 | // Then the texture layer is removed and attached to compositor2, and passes a |
| 521 | // reference to its impl tree. |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 522 | std::unique_ptr<SingleReleaseCallbackImpl> compositor2; |
fdoray | 70df5a9 | 2016-06-22 21:13:59 | [diff] [blame] | 523 | main_thread_.task_runner()->PostTask( |
tzik | 4604bb5 | 2017-04-13 21:50:22 | [diff] [blame] | 524 | FROM_HERE, base::BindOnce(&TextureLayerMailboxHolderTest::CreateImplRef, |
| 525 | base::Unretained(this), &compositor2)); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 526 | |
| 527 | Wait(main_thread_); |
| 528 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 529 | |
| 530 | // The main thread reference is destroyed first. |
fdoray | 70df5a9 | 2016-06-22 21:13:59 | [diff] [blame] | 531 | main_thread_.task_runner()->PostTask( |
tzik | 4604bb5 | 2017-04-13 21:50:22 | [diff] [blame] | 532 | FROM_HERE, base::BindOnce(&TextureLayerMailboxHolderTest::ReleaseMainRef, |
| 533 | base::Unretained(this))); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 534 | |
| 535 | // One compositor destroys their impl tree. |
dyen | 398dd014 | 2016-01-21 22:05:56 | [diff] [blame] | 536 | compositor2->Run(SyncTokenFromUInt(200), false, |
| 537 | main_thread_task_runner_.get()); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 538 | |
| 539 | Wait(main_thread_); |
| 540 | |
| 541 | EXPECT_CALL(test_data_.mock_callback_, Release(_, _, _)).Times(0); |
| 542 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 543 | |
| 544 | // The second impl reference is destroyed last, causing the mailbox to be |
| 545 | // released back to the embedder with the last sync point from the impl tree. |
| 546 | EXPECT_CALL(test_data_.mock_callback_, |
dyen | 398dd014 | 2016-01-21 22:05:56 | [diff] [blame] | 547 | Release(test_data_.mailbox_name1_, SyncTokenFromUInt(100), true)) |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 548 | .Times(1); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 549 | |
dyen | 398dd014 | 2016-01-21 22:05:56 | [diff] [blame] | 550 | compositor1->Run(SyncTokenFromUInt(100), true, |
| 551 | main_thread_task_runner_.get()); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 552 | Wait(main_thread_); |
| 553 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 554 | } |
| 555 | |
| 556 | TEST_F(TextureLayerMailboxHolderTest, TwoCompositors_SecondImplRefShortcut) { |
kulkarni.a | 4015690f1 | 2014-10-10 13:50:06 | [diff] [blame] | 557 | scoped_refptr<TextureLayer> test_layer = |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 558 | TextureLayer::CreateForMailbox(nullptr); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 559 | ASSERT_TRUE(test_layer.get()); |
| 560 | |
fdoray | 70df5a9 | 2016-06-22 21:13:59 | [diff] [blame] | 561 | main_thread_.task_runner()->PostTask( |
tzik | 4604bb5 | 2017-04-13 21:50:22 | [diff] [blame] | 562 | FROM_HERE, base::BindOnce(&TextureLayerMailboxHolderTest::CreateMainRef, |
| 563 | base::Unretained(this))); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 564 | |
| 565 | Wait(main_thread_); |
| 566 | |
| 567 | // The texture layer is attached to compositor1, and passes a reference to its |
| 568 | // impl tree. |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 569 | std::unique_ptr<SingleReleaseCallbackImpl> compositor1; |
fdoray | 70df5a9 | 2016-06-22 21:13:59 | [diff] [blame] | 570 | main_thread_.task_runner()->PostTask( |
tzik | 4604bb5 | 2017-04-13 21:50:22 | [diff] [blame] | 571 | FROM_HERE, base::BindOnce(&TextureLayerMailboxHolderTest::CreateImplRef, |
| 572 | base::Unretained(this), &compositor1)); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 573 | |
| 574 | // Then the texture layer is removed and attached to compositor2, and passes a |
| 575 | // reference to its impl tree. |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 576 | std::unique_ptr<SingleReleaseCallbackImpl> compositor2; |
fdoray | 70df5a9 | 2016-06-22 21:13:59 | [diff] [blame] | 577 | main_thread_.task_runner()->PostTask( |
tzik | 4604bb5 | 2017-04-13 21:50:22 | [diff] [blame] | 578 | FROM_HERE, base::BindOnce(&TextureLayerMailboxHolderTest::CreateImplRef, |
| 579 | base::Unretained(this), &compositor2)); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 580 | |
| 581 | Wait(main_thread_); |
| 582 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 583 | |
| 584 | // The main thread reference is destroyed first. |
fdoray | 70df5a9 | 2016-06-22 21:13:59 | [diff] [blame] | 585 | main_thread_.task_runner()->PostTask( |
tzik | 4604bb5 | 2017-04-13 21:50:22 | [diff] [blame] | 586 | FROM_HERE, base::BindOnce(&TextureLayerMailboxHolderTest::ReleaseMainRef, |
| 587 | base::Unretained(this))); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 588 | |
| 589 | EXPECT_CALL(test_data_.mock_callback_, |
dyen | 398dd014 | 2016-01-21 22:05:56 | [diff] [blame] | 590 | Release(test_data_.mailbox_name1_, SyncTokenFromUInt(200), true)) |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 591 | .Times(1); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 592 | |
gab | cca5311 | 2016-06-08 20:13:28 | [diff] [blame] | 593 | base::WaitableEvent begin_capture( |
| 594 | base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 595 | base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 596 | base::WaitableEvent wait_for_capture( |
| 597 | base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 598 | base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 599 | base::WaitableEvent stop_capture( |
| 600 | base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 601 | base::WaitableEvent::InitialState::NOT_SIGNALED); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 602 | |
| 603 | // Post a task to start capturing tasks on the main thread. This will block |
| 604 | // the main thread until we signal the |stop_capture| event. |
fdoray | 70df5a9 | 2016-06-22 21:13:59 | [diff] [blame] | 605 | main_thread_.task_runner()->PostTask( |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 606 | FROM_HERE, |
tzik | 4604bb5 | 2017-04-13 21:50:22 | [diff] [blame] | 607 | base::BindOnce(&TextureLayerMailboxHolderTest::CapturePostTasksAndWait, |
| 608 | base::Unretained(this), &begin_capture, &wait_for_capture, |
| 609 | &stop_capture)); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 610 | |
| 611 | // Before the main thread capturing starts, one compositor destroys their |
| 612 | // impl reference. Since capturing did not start, this gets post-tasked to |
| 613 | // the main thread. |
dyen | 398dd014 | 2016-01-21 22:05:56 | [diff] [blame] | 614 | compositor1->Run(SyncTokenFromUInt(100), false, |
| 615 | main_thread_task_runner_.get()); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 616 | |
| 617 | // Start capturing on the main thread. |
| 618 | begin_capture.Signal(); |
| 619 | wait_for_capture.Wait(); |
| 620 | |
| 621 | // Meanwhile, the second compositor released its impl reference, but this task |
| 622 | // gets shortcutted directly to the main thread. This means the reference is |
| 623 | // released before compositor1, whose reference will be released later when |
| 624 | // the post-task is serviced. But since it was destroyed _on the impl thread_ |
| 625 | // last, its sync point values should be used. |
dyen | 398dd014 | 2016-01-21 22:05:56 | [diff] [blame] | 626 | compositor2->Run(SyncTokenFromUInt(200), true, |
| 627 | main_thread_task_runner_.get()); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 628 | |
| 629 | stop_capture.Signal(); |
| 630 | Wait(main_thread_); |
| 631 | |
| 632 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 633 | } |
| 634 | |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 635 | class TextureLayerImplWithMailboxThreadedCallback : public LayerTreeTest { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 636 | public: |
danakj | 014316e | 2016-08-04 18:40:26 | [diff] [blame] | 637 | TextureLayerImplWithMailboxThreadedCallback() = default; |
| 638 | |
danakj | c391f33 | 2017-07-12 20:45:52 | [diff] [blame] | 639 | std::unique_ptr<viz::TestLayerTreeFrameSink> CreateLayerTreeFrameSink( |
Alex Zhang | bcce8b13 | 2017-07-20 20:17:24 | [diff] [blame^] | 640 | const viz::RendererSettings& renderer_settings, |
Alex Zhang | 903cb6a | 2017-06-12 20:31:37 | [diff] [blame] | 641 | double refresh_rate, |
Xu Xing | 3254916 | 2017-07-17 22:25:43 | [diff] [blame] | 642 | scoped_refptr<viz::ContextProvider> compositor_context_provider, |
| 643 | scoped_refptr<viz::ContextProvider> worker_context_provider) override { |
staraz | 8867742 | 2017-05-26 13:53:03 | [diff] [blame] | 644 | constexpr bool disable_display_vsync = false; |
danakj | 014316e | 2016-08-04 18:40:26 | [diff] [blame] | 645 | bool synchronous_composite = |
| 646 | !HasImplThread() && |
khushalsagar | cebe494 | 2016-09-07 23:27:01 | [diff] [blame] | 647 | !layer_tree_host()->GetSettings().single_thread_proxy_scheduler; |
danakj | c391f33 | 2017-07-12 20:45:52 | [diff] [blame] | 648 | return base::MakeUnique<viz::TestLayerTreeFrameSink>( |
danakj | 014316e | 2016-08-04 18:40:26 | [diff] [blame] | 649 | compositor_context_provider, std::move(worker_context_provider), |
staraz | 8867742 | 2017-05-26 13:53:03 | [diff] [blame] | 650 | shared_bitmap_manager(), gpu_memory_buffer_manager(), renderer_settings, |
Alex Zhang | 903cb6a | 2017-06-12 20:31:37 | [diff] [blame] | 651 | ImplThreadTaskRunner(), synchronous_composite, disable_display_vsync, |
| 652 | refresh_rate); |
danakj | 014316e | 2016-08-04 18:40:26 | [diff] [blame] | 653 | } |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 654 | |
ericrk | ad592381 | 2017-01-25 23:26:12 | [diff] [blame] | 655 | void AdvanceTestCase() { |
| 656 | ++test_case_; |
| 657 | switch (test_case_) { |
| 658 | case 1: |
| 659 | // Case #1: change mailbox before the commit. The old mailbox should be |
| 660 | // released immediately. |
| 661 | SetMailbox('2'); |
| 662 | EXPECT_EQ(1, callback_count_); |
| 663 | PostSetNeedsCommitToMainThread(); |
| 664 | |
| 665 | // Case 2 does not rely on callbacks to advance. |
| 666 | pending_callback_ = false; |
| 667 | break; |
| 668 | case 2: |
| 669 | // Case #2: change mailbox after the commit (and draw), where the |
| 670 | // layer draws. The old mailbox should be released during the next |
| 671 | // commit. |
| 672 | SetMailbox('3'); |
| 673 | EXPECT_EQ(1, callback_count_); |
| 674 | |
| 675 | // Cases 3-5 rely on a callback to advance. |
| 676 | pending_callback_ = true; |
| 677 | break; |
| 678 | case 3: |
| 679 | EXPECT_EQ(2, callback_count_); |
| 680 | // Case #3: change mailbox when the layer doesn't draw. The old |
| 681 | // mailbox should be released during the next commit. |
| 682 | layer_->SetBounds(gfx::Size()); |
| 683 | SetMailbox('4'); |
| 684 | break; |
| 685 | case 4: |
| 686 | EXPECT_EQ(3, callback_count_); |
| 687 | // Case #4: release mailbox that was committed but never drawn. The |
| 688 | // old mailbox should be released during the next commit. |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 689 | layer_->SetTextureMailbox(viz::TextureMailbox(), nullptr); |
ericrk | ad592381 | 2017-01-25 23:26:12 | [diff] [blame] | 690 | break; |
| 691 | case 5: |
| 692 | EXPECT_EQ(4, callback_count_); |
| 693 | // Restore a mailbox for the next step. |
| 694 | SetMailbox('5'); |
| 695 | |
| 696 | // Cases 6 and 7 do not rely on callbacks to advance. |
| 697 | pending_callback_ = false; |
| 698 | break; |
| 699 | case 6: |
| 700 | // Case #5: remove layer from tree. Callback should *not* be called, the |
| 701 | // mailbox is returned to the main thread. |
| 702 | EXPECT_EQ(4, callback_count_); |
| 703 | layer_->RemoveFromParent(); |
| 704 | break; |
| 705 | case 7: |
| 706 | EXPECT_EQ(4, callback_count_); |
| 707 | // Resetting the mailbox will call the callback now. |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 708 | layer_->SetTextureMailbox(viz::TextureMailbox(), nullptr); |
ericrk | ad592381 | 2017-01-25 23:26:12 | [diff] [blame] | 709 | EXPECT_EQ(5, callback_count_); |
| 710 | EndTest(); |
| 711 | break; |
| 712 | default: |
| 713 | NOTREACHED(); |
| 714 | break; |
| 715 | } |
| 716 | } |
| 717 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 718 | // Make sure callback is received on main and doesn't block the impl thread. |
danakj | 014316e | 2016-08-04 18:40:26 | [diff] [blame] | 719 | void ReleaseCallback(char mailbox_char, |
| 720 | const gpu::SyncToken& sync_token, |
| 721 | bool lost_resource) { |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 722 | EXPECT_EQ(true, main_thread_.CalledOnValidThread()); |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 723 | EXPECT_FALSE(lost_resource); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 724 | ++callback_count_; |
ericrk | ad592381 | 2017-01-25 23:26:12 | [diff] [blame] | 725 | |
| 726 | // If we are waiting on a callback, advance now. |
| 727 | if (pending_callback_) |
| 728 | AdvanceTestCase(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | void SetMailbox(char mailbox_char) { |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 732 | EXPECT_EQ(true, main_thread_.CalledOnValidThread()); |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 733 | std::unique_ptr<SingleReleaseCallback> callback = |
| 734 | SingleReleaseCallback::Create(base::Bind( |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 735 | &TextureLayerImplWithMailboxThreadedCallback::ReleaseCallback, |
danakj | 014316e | 2016-08-04 18:40:26 | [diff] [blame] | 736 | base::Unretained(this), mailbox_char)); |
dyen | e5db881b | 2016-03-01 19:47:03 | [diff] [blame] | 737 | layer_->SetTextureMailbox( |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 738 | viz::TextureMailbox( |
| 739 | MailboxFromChar(mailbox_char), |
| 740 | SyncTokenFromUInt(static_cast<uint32_t>(mailbox_char)), |
| 741 | GL_TEXTURE_2D), |
dyen | e5db881b | 2016-03-01 19:47:03 | [diff] [blame] | 742 | std::move(callback)); |
danakj | 014316e | 2016-08-04 18:40:26 | [diff] [blame] | 743 | // Damage the layer so we send a new frame with the new mailbox to the |
| 744 | // Display compositor. |
| 745 | layer_->SetNeedsDisplay(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 746 | } |
| 747 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 748 | void BeginTest() override { |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 749 | EXPECT_EQ(true, main_thread_.CalledOnValidThread()); |
| 750 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 751 | gfx::Size bounds(100, 100); |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 752 | root_ = Layer::Create(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 753 | root_->SetBounds(bounds); |
| 754 | |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 755 | layer_ = TextureLayer::CreateForMailbox(nullptr); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 756 | layer_->SetIsDrawable(true); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 757 | layer_->SetBounds(bounds); |
| 758 | |
| 759 | root_->AddChild(layer_); |
khushalsagar | b69ba945 | 2017-01-27 22:20:07 | [diff] [blame] | 760 | layer_tree_host()->SetRootLayer(root_); |
| 761 | layer_tree_host()->SetViewportSize(bounds); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 762 | SetMailbox('1'); |
| 763 | EXPECT_EQ(0, callback_count_); |
| 764 | |
ericrk | ad592381 | 2017-01-25 23:26:12 | [diff] [blame] | 765 | // Setup is complete - advance to test case 1. |
| 766 | AdvanceTestCase(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 767 | } |
| 768 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 769 | void DidCommit() override { |
ericrk | ad592381 | 2017-01-25 23:26:12 | [diff] [blame] | 770 | // If we are not waiting on a callback, advance now. |
| 771 | if (!pending_callback_) |
| 772 | AdvanceTestCase(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 773 | } |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 774 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 775 | void AfterTest() override {} |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 776 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 777 | private: |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 778 | base::ThreadChecker main_thread_; |
danakj | 014316e | 2016-08-04 18:40:26 | [diff] [blame] | 779 | int callback_count_ = 0; |
ericrk | ad592381 | 2017-01-25 23:26:12 | [diff] [blame] | 780 | int test_case_ = 0; |
| 781 | // Whether we are waiting on a callback to advance the test case. |
| 782 | bool pending_callback_ = false; |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 783 | scoped_refptr<Layer> root_; |
| 784 | scoped_refptr<TextureLayer> layer_; |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 785 | }; |
| 786 | |
danakj | 0943a11 | 2016-08-11 00:33:46 | [diff] [blame] | 787 | SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerImplWithMailboxThreadedCallback); |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 788 | |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 789 | class TextureLayerMailboxIsActivatedDuringCommit : public LayerTreeTest { |
| 790 | protected: |
samans | 0834fe04 | 2017-03-03 17:03:22 | [diff] [blame] | 791 | void ReleaseCallback(const gpu::SyncToken& original_sync_token, |
| 792 | const gpu::SyncToken& release_sync_token, |
| 793 | bool lost_resource) { |
| 794 | released_count_++; |
| 795 | switch (released_count_) { |
| 796 | case 1: |
| 797 | break; |
| 798 | case 2: |
| 799 | EXPECT_EQ(3, layer_tree_host()->SourceFrameNumber()); |
| 800 | EndTest(); |
| 801 | break; |
| 802 | default: |
| 803 | NOTREACHED(); |
| 804 | } |
| 805 | } |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 806 | |
| 807 | void SetMailbox(char mailbox_char) { |
dyen | e5db881b | 2016-03-01 19:47:03 | [diff] [blame] | 808 | const gpu::SyncToken sync_token = |
| 809 | SyncTokenFromUInt(static_cast<uint32_t>(mailbox_char)); |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 810 | std::unique_ptr<SingleReleaseCallback> callback = |
| 811 | SingleReleaseCallback::Create(base::Bind( |
| 812 | &TextureLayerMailboxIsActivatedDuringCommit::ReleaseCallback, |
samans | 0834fe04 | 2017-03-03 17:03:22 | [diff] [blame] | 813 | base::Unretained(this), sync_token)); |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 814 | layer_->SetTextureMailbox(viz::TextureMailbox(MailboxFromChar(mailbox_char), |
| 815 | sync_token, GL_TEXTURE_2D), |
danakj | a04855a | 2015-11-18 20:39:10 | [diff] [blame] | 816 | std::move(callback)); |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 817 | } |
| 818 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 819 | void BeginTest() override { |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 820 | gfx::Size bounds(100, 100); |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 821 | root_ = Layer::Create(); |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 822 | root_->SetBounds(bounds); |
| 823 | |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 824 | layer_ = TextureLayer::CreateForMailbox(nullptr); |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 825 | layer_->SetIsDrawable(true); |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 826 | layer_->SetBounds(bounds); |
| 827 | |
| 828 | root_->AddChild(layer_); |
khushalsagar | b69ba945 | 2017-01-27 22:20:07 | [diff] [blame] | 829 | layer_tree_host()->SetRootLayer(root_); |
| 830 | layer_tree_host()->SetViewportSize(bounds); |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 831 | SetMailbox('1'); |
| 832 | |
| 833 | PostSetNeedsCommitToMainThread(); |
| 834 | } |
| 835 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 836 | void WillActivateTreeOnThread(LayerTreeHostImpl* impl) override { |
danakj | 3c3973b | 2015-08-25 21:50:18 | [diff] [blame] | 837 | base::AutoLock lock(activate_count_lock_); |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 838 | ++activate_count_; |
| 839 | } |
| 840 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 841 | void DidCommit() override { |
danakj | 3c3973b | 2015-08-25 21:50:18 | [diff] [blame] | 842 | // The first frame doesn't cause anything to be returned so it does not |
| 843 | // need to wait for activation. |
khushalsagar | cebe494 | 2016-09-07 23:27:01 | [diff] [blame] | 844 | if (layer_tree_host()->SourceFrameNumber() > 1) { |
danakj | 3c3973b | 2015-08-25 21:50:18 | [diff] [blame] | 845 | base::AutoLock lock(activate_count_lock_); |
| 846 | // The activate happened before commit is done on the main side. |
khushalsagar | cebe494 | 2016-09-07 23:27:01 | [diff] [blame] | 847 | EXPECT_EQ(activate_count_, layer_tree_host()->SourceFrameNumber()); |
danakj | 3c3973b | 2015-08-25 21:50:18 | [diff] [blame] | 848 | } |
| 849 | |
khushalsagar | cebe494 | 2016-09-07 23:27:01 | [diff] [blame] | 850 | switch (layer_tree_host()->SourceFrameNumber()) { |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 851 | case 1: |
| 852 | // The first mailbox has been activated. Set a new mailbox, and |
| 853 | // expect the next commit to finish *after* it is activated. |
| 854 | SetMailbox('2'); |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 855 | break; |
| 856 | case 2: |
| 857 | // The second mailbox has been activated. Remove the layer from |
| 858 | // the tree to cause another commit/activation. The commit should |
| 859 | // finish *after* the layer is removed from the active tree. |
| 860 | layer_->RemoveFromParent(); |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 861 | break; |
| 862 | case 3: |
samans | 0834fe04 | 2017-03-03 17:03:22 | [diff] [blame] | 863 | // This ensures all texture mailboxes are released before the end of the |
| 864 | // test. |
| 865 | layer_->ClearClient(); |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 866 | break; |
samans | 0834fe04 | 2017-03-03 17:03:22 | [diff] [blame] | 867 | default: |
| 868 | NOTREACHED(); |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 872 | void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override { |
danakj | 3c3973b | 2015-08-25 21:50:18 | [diff] [blame] | 873 | // The activate didn't happen before commit is done on the impl side (but it |
| 874 | // should happen before the main thread is done). |
| 875 | EXPECT_EQ(activate_count_, host_impl->sync_tree()->source_frame_number()); |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 876 | } |
| 877 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 878 | void AfterTest() override {} |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 879 | |
danakj | 3c3973b | 2015-08-25 21:50:18 | [diff] [blame] | 880 | base::Lock activate_count_lock_; |
samans | 0834fe04 | 2017-03-03 17:03:22 | [diff] [blame] | 881 | int activate_count_ = 0; |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 882 | scoped_refptr<Layer> root_; |
| 883 | scoped_refptr<TextureLayer> layer_; |
samans | 0834fe04 | 2017-03-03 17:03:22 | [diff] [blame] | 884 | int released_count_ = 0; |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 885 | }; |
| 886 | |
danakj | 0943a11 | 2016-08-11 00:33:46 | [diff] [blame] | 887 | SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerMailboxIsActivatedDuringCommit); |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 888 | |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 889 | class TextureLayerImplWithMailboxTest : public TextureLayerTest { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 890 | protected: |
dcheng | 93a52eb | 2014-12-23 02:14:23 | [diff] [blame] | 891 | void SetUp() override { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 892 | TextureLayerTest::SetUp(); |
loyso | 2cb3f32f | 2016-11-08 07:08:34 | [diff] [blame] | 893 | layer_tree_host_ = MockLayerTreeHost::Create( |
| 894 | &fake_client_, &task_graph_runner_, animation_host_.get()); |
sievers | 71c62dd5 | 2015-10-07 01:44:39 | [diff] [blame] | 895 | host_impl_.SetVisible(true); |
danakj | c7afae5 | 2017-06-20 21:12:41 | [diff] [blame] | 896 | EXPECT_TRUE(host_impl_.InitializeRenderer(layer_tree_frame_sink_.get())); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 897 | } |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 898 | |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 899 | bool WillDraw(TextureLayerImpl* layer, DrawMode mode) { |
| 900 | bool will_draw = layer->WillDraw( |
| 901 | mode, host_impl_.active_tree()->resource_provider()); |
| 902 | if (will_draw) |
| 903 | layer->DidDraw(host_impl_.active_tree()->resource_provider()); |
| 904 | return will_draw; |
| 905 | } |
| 906 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 907 | FakeLayerTreeHostClient fake_client_; |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 908 | }; |
| 909 | |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 910 | // Test conditions for results of TextureLayerImpl::WillDraw under |
| 911 | // different configurations of different mailbox, texture_id, and draw_mode. |
| 912 | TEST_F(TextureLayerImplWithMailboxTest, TestWillDraw) { |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 913 | EXPECT_CALL( |
| 914 | test_data_.mock_callback_, |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 915 | ReleaseImpl(test_data_.mailbox_name1_, test_data_.sync_token1_, false, _)) |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 916 | .Times(AnyNumber()); |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 917 | EXPECT_CALL( |
| 918 | test_data_.mock_callback_, |
| 919 | ReleaseImpl2(test_data_.shared_bitmap_.get(), gpu::SyncToken(), false, _)) |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 920 | .Times(AnyNumber()); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 921 | // Hardware mode. |
| 922 | { |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 923 | std::unique_ptr<TextureLayerImpl> impl_layer = |
[email protected] | 17e0843 | 2014-04-10 00:41:11 | [diff] [blame] | 924 | TextureLayerImpl::Create(host_impl_.active_tree(), 1); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 925 | impl_layer->SetTextureMailbox( |
| 926 | test_data_.mailbox1_, |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 927 | SingleReleaseCallbackImpl::Create(test_data_.release_mailbox1_impl_)); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 928 | EXPECT_TRUE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE)); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | { |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 932 | std::unique_ptr<TextureLayerImpl> impl_layer = |
[email protected] | 17e0843 | 2014-04-10 00:41:11 | [diff] [blame] | 933 | TextureLayerImpl::Create(host_impl_.active_tree(), 1); |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 934 | impl_layer->SetTextureMailbox(viz::TextureMailbox(), nullptr); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 935 | EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE)); |
| 936 | } |
| 937 | |
| 938 | { |
| 939 | // Software resource. |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 940 | std::unique_ptr<TextureLayerImpl> impl_layer = |
[email protected] | 17e0843 | 2014-04-10 00:41:11 | [diff] [blame] | 941 | TextureLayerImpl::Create(host_impl_.active_tree(), 1); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 942 | impl_layer->SetTextureMailbox( |
| 943 | test_data_.mailbox3_, |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 944 | SingleReleaseCallbackImpl::Create(test_data_.release_mailbox3_impl_)); |
[email protected] | 3e44d7a | 2013-07-30 00:03:10 | [diff] [blame] | 945 | EXPECT_TRUE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE)); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 946 | } |
| 947 | |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 948 | // Software mode. |
| 949 | { |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 950 | std::unique_ptr<TextureLayerImpl> impl_layer = |
[email protected] | 17e0843 | 2014-04-10 00:41:11 | [diff] [blame] | 951 | TextureLayerImpl::Create(host_impl_.active_tree(), 1); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 952 | impl_layer->SetTextureMailbox( |
| 953 | test_data_.mailbox1_, |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 954 | SingleReleaseCallbackImpl::Create(test_data_.release_mailbox1_impl_)); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 955 | EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_SOFTWARE)); |
| 956 | } |
| 957 | |
| 958 | { |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 959 | std::unique_ptr<TextureLayerImpl> impl_layer = |
[email protected] | 17e0843 | 2014-04-10 00:41:11 | [diff] [blame] | 960 | TextureLayerImpl::Create(host_impl_.active_tree(), 1); |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 961 | impl_layer->SetTextureMailbox(viz::TextureMailbox(), nullptr); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 962 | EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_SOFTWARE)); |
| 963 | } |
| 964 | |
| 965 | { |
| 966 | // Software resource. |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 967 | std::unique_ptr<TextureLayerImpl> impl_layer = |
[email protected] | 17e0843 | 2014-04-10 00:41:11 | [diff] [blame] | 968 | TextureLayerImpl::Create(host_impl_.active_tree(), 1); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 969 | impl_layer->SetTextureMailbox( |
| 970 | test_data_.mailbox3_, |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 971 | SingleReleaseCallbackImpl::Create(test_data_.release_mailbox3_impl_)); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 972 | EXPECT_TRUE(WillDraw(impl_layer.get(), DRAW_MODE_SOFTWARE)); |
| 973 | } |
| 974 | |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 975 | // Resourceless software mode. |
| 976 | { |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 977 | std::unique_ptr<TextureLayerImpl> impl_layer = |
[email protected] | 17e0843 | 2014-04-10 00:41:11 | [diff] [blame] | 978 | TextureLayerImpl::Create(host_impl_.active_tree(), 1); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 979 | impl_layer->SetTextureMailbox( |
| 980 | test_data_.mailbox1_, |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 981 | SingleReleaseCallbackImpl::Create(test_data_.release_mailbox1_impl_)); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 982 | EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_RESOURCELESS_SOFTWARE)); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 983 | } |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 984 | } |
| 985 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 986 | TEST_F(TextureLayerImplWithMailboxTest, TestImplLayerCallbacks) { |
| 987 | host_impl_.CreatePendingTree(); |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 988 | std::unique_ptr<TextureLayerImpl> pending_layer; |
[email protected] | 17e0843 | 2014-04-10 00:41:11 | [diff] [blame] | 989 | pending_layer = TextureLayerImpl::Create(host_impl_.pending_tree(), 1); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 990 | ASSERT_TRUE(pending_layer); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 991 | |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 992 | std::unique_ptr<LayerImpl> active_layer( |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 993 | pending_layer->CreateLayerImpl(host_impl_.active_tree())); |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 994 | ASSERT_TRUE(active_layer); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 995 | |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 996 | pending_layer->SetTextureMailbox( |
| 997 | test_data_.mailbox1_, |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 998 | SingleReleaseCallbackImpl::Create(test_data_.release_mailbox1_impl_)); |
[email protected] | 421e84f | 2013-02-22 03:27:15 | [diff] [blame] | 999 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1000 | // Test multiple commits without an activation. |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 1001 | EXPECT_CALL( |
| 1002 | test_data_.mock_callback_, |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 1003 | ReleaseImpl(test_data_.mailbox_name1_, test_data_.sync_token1_, false, _)) |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1004 | .Times(1); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1005 | pending_layer->SetTextureMailbox( |
| 1006 | test_data_.mailbox2_, |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 1007 | SingleReleaseCallbackImpl::Create(test_data_.release_mailbox2_impl_)); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1008 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | 421e84f | 2013-02-22 03:27:15 | [diff] [blame] | 1009 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1010 | // Test callback after activation. |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 1011 | pending_layer->PushPropertiesTo(active_layer.get()); |
| 1012 | active_layer->DidBecomeActive(); |
[email protected] | 421e84f | 2013-02-22 03:27:15 | [diff] [blame] | 1013 | |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 1014 | EXPECT_CALL(test_data_.mock_callback_, ReleaseImpl(_, _, _, _)).Times(0); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1015 | pending_layer->SetTextureMailbox( |
| 1016 | test_data_.mailbox1_, |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 1017 | SingleReleaseCallbackImpl::Create(test_data_.release_mailbox1_impl_)); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1018 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | 421e84f | 2013-02-22 03:27:15 | [diff] [blame] | 1019 | |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 1020 | EXPECT_CALL(test_data_.mock_callback_, |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 1021 | ReleaseImpl(test_data_.mailbox_name2_, _, false, _)).Times(1); |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 1022 | pending_layer->PushPropertiesTo(active_layer.get()); |
| 1023 | active_layer->DidBecomeActive(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1024 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 1025 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1026 | // Test resetting the mailbox. |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 1027 | EXPECT_CALL(test_data_.mock_callback_, |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 1028 | ReleaseImpl(test_data_.mailbox_name1_, _, false, _)).Times(1); |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 1029 | pending_layer->SetTextureMailbox(viz::TextureMailbox(), nullptr); |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 1030 | pending_layer->PushPropertiesTo(active_layer.get()); |
| 1031 | active_layer->DidBecomeActive(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1032 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 1033 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1034 | // Test destructor. |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 1035 | EXPECT_CALL( |
| 1036 | test_data_.mock_callback_, |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 1037 | ReleaseImpl(test_data_.mailbox_name1_, test_data_.sync_token1_, false, _)) |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1038 | .Times(1); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1039 | pending_layer->SetTextureMailbox( |
| 1040 | test_data_.mailbox1_, |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 1041 | SingleReleaseCallbackImpl::Create(test_data_.release_mailbox1_impl_)); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 1042 | } |
| 1043 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1044 | TEST_F(TextureLayerImplWithMailboxTest, |
| 1045 | TestDestructorCallbackOnCreatedResource) { |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 1046 | std::unique_ptr<TextureLayerImpl> impl_layer; |
[email protected] | 17e0843 | 2014-04-10 00:41:11 | [diff] [blame] | 1047 | impl_layer = TextureLayerImpl::Create(host_impl_.active_tree(), 1); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1048 | ASSERT_TRUE(impl_layer); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 1049 | |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 1050 | EXPECT_CALL(test_data_.mock_callback_, |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 1051 | ReleaseImpl(test_data_.mailbox_name1_, _, false, _)).Times(1); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1052 | impl_layer->SetTextureMailbox( |
| 1053 | test_data_.mailbox1_, |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 1054 | SingleReleaseCallbackImpl::Create(test_data_.release_mailbox1_impl_)); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 1055 | impl_layer->DidBecomeActive(); |
| 1056 | EXPECT_TRUE(impl_layer->WillDraw( |
| 1057 | DRAW_MODE_HARDWARE, host_impl_.active_tree()->resource_provider())); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1058 | impl_layer->DidDraw(host_impl_.active_tree()->resource_provider()); |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 1059 | impl_layer->SetTextureMailbox(viz::TextureMailbox(), nullptr); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 1060 | } |
| 1061 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1062 | TEST_F(TextureLayerImplWithMailboxTest, TestCallbackOnInUseResource) { |
| 1063 | ResourceProvider* provider = host_impl_.active_tree()->resource_provider(); |
jbauman | bbd425e | 2015-05-19 00:33:35 | [diff] [blame] | 1064 | ResourceId id = provider->CreateResourceFromTextureMailbox( |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 1065 | test_data_.mailbox1_, |
| 1066 | SingleReleaseCallbackImpl::Create(test_data_.release_mailbox1_impl_)); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1067 | provider->AllocateForTesting(id); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 1068 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1069 | // Transfer some resources to the parent. |
| 1070 | ResourceProvider::ResourceIdArray resource_ids_to_transfer; |
| 1071 | resource_ids_to_transfer.push_back(id); |
danakj | 30464ba1 | 2017-06-29 21:26:45 | [diff] [blame] | 1072 | std::vector<TransferableResource> list; |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1073 | provider->PrepareSendToParent(resource_ids_to_transfer, &list); |
| 1074 | EXPECT_TRUE(provider->InUseByConsumer(id)); |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 1075 | EXPECT_CALL(test_data_.mock_callback_, ReleaseImpl(_, _, _, _)).Times(0); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1076 | provider->DeleteResource(id); |
| 1077 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 1078 | EXPECT_CALL(test_data_.mock_callback_, |
skyostil | 3976a3f | 2014-09-04 22:07:23 | [diff] [blame] | 1079 | ReleaseImpl(test_data_.mailbox_name1_, _, false, _)).Times(1); |
danakj | 30464ba1 | 2017-06-29 21:26:45 | [diff] [blame] | 1080 | std::vector<ReturnedResource> returned = |
| 1081 | TransferableResource::ReturnResources(list); |
[email protected] | e00bab02 | 2013-08-19 00:42:45 | [diff] [blame] | 1082 | provider->ReceiveReturnsFromParent(returned); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 1083 | } |
| 1084 | |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1085 | // Checks that TextureLayer::Update does not cause an extra commit when setting |
| 1086 | // the texture mailbox. |
| 1087 | class TextureLayerNoExtraCommitForMailboxTest |
| 1088 | : public LayerTreeTest, |
| 1089 | public TextureLayerClient { |
| 1090 | public: |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1091 | // TextureLayerClient implementation. |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1092 | bool PrepareTextureMailbox( |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 1093 | viz::TextureMailbox* texture_mailbox, |
danakj | 4d0dd80 | 2016-08-23 22:10:06 | [diff] [blame] | 1094 | std::unique_ptr<SingleReleaseCallback>* release_callback) override { |
khushalsagar | cebe494 | 2016-09-07 23:27:01 | [diff] [blame] | 1095 | if (layer_tree_host()->SourceFrameNumber() == 1) { |
[email protected] | 9f35bd2 | 2014-06-03 15:25:46 | [diff] [blame] | 1096 | // Once this has been committed, the mailbox will be released. |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 1097 | *texture_mailbox = viz::TextureMailbox(); |
[email protected] | cce34bd | 2013-12-02 23:24:45 | [diff] [blame] | 1098 | return true; |
| 1099 | } |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1100 | |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 1101 | *texture_mailbox = viz::TextureMailbox( |
| 1102 | MailboxFromChar('1'), SyncTokenFromUInt(0x123), GL_TEXTURE_2D); |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1103 | *release_callback = SingleReleaseCallback::Create( |
| 1104 | base::Bind(&TextureLayerNoExtraCommitForMailboxTest::MailboxReleased, |
| 1105 | base::Unretained(this))); |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1106 | return true; |
| 1107 | } |
| 1108 | |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 1109 | void MailboxReleased(const gpu::SyncToken& sync_token, bool lost_resource) { |
dyen | e5db881b | 2016-03-01 19:47:03 | [diff] [blame] | 1110 | EXPECT_TRUE(sync_token.HasData()); |
[email protected] | cce34bd | 2013-12-02 23:24:45 | [diff] [blame] | 1111 | EndTest(); |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1112 | } |
| 1113 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1114 | void SetupTree() override { |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 1115 | scoped_refptr<Layer> root = Layer::Create(); |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1116 | root->SetBounds(gfx::Size(10, 10)); |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1117 | root->SetIsDrawable(true); |
| 1118 | |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 1119 | texture_layer_ = TextureLayer::CreateForMailbox(this); |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1120 | texture_layer_->SetBounds(gfx::Size(10, 10)); |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1121 | texture_layer_->SetIsDrawable(true); |
[email protected] | 0d7fb30 | 2014-01-23 21:30:47 | [diff] [blame] | 1122 | root->AddChild(texture_layer_); |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1123 | |
khushalsagar | b69ba945 | 2017-01-27 22:20:07 | [diff] [blame] | 1124 | layer_tree_host()->SetRootLayer(root); |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1125 | LayerTreeTest::SetupTree(); |
| 1126 | } |
| 1127 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1128 | void BeginTest() override { PostSetNeedsCommitToMainThread(); } |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1129 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1130 | void DidCommitAndDrawFrame() override { |
khushalsagar | cebe494 | 2016-09-07 23:27:01 | [diff] [blame] | 1131 | switch (layer_tree_host()->SourceFrameNumber()) { |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1132 | case 1: |
[email protected] | 4ea293f7 | 2014-08-13 03:03:17 | [diff] [blame] | 1133 | EXPECT_FALSE(proxy()->MainFrameWillHappenForTesting()); |
[email protected] | cce34bd | 2013-12-02 23:24:45 | [diff] [blame] | 1134 | // Invalidate the texture layer to clear the mailbox before |
| 1135 | // ending the test. |
| 1136 | texture_layer_->SetNeedsDisplay(); |
| 1137 | break; |
| 1138 | case 2: |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1139 | break; |
| 1140 | default: |
| 1141 | NOTREACHED(); |
| 1142 | break; |
| 1143 | } |
| 1144 | } |
| 1145 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1146 | void AfterTest() override {} |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1147 | |
| 1148 | private: |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1149 | scoped_refptr<TextureLayer> texture_layer_; |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1150 | }; |
| 1151 | |
[email protected] | cce34bd | 2013-12-02 23:24:45 | [diff] [blame] | 1152 | SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerNoExtraCommitForMailboxTest); |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1153 | |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1154 | // Checks that changing a mailbox in the client for a TextureLayer that's |
| 1155 | // invisible correctly works and uses the new mailbox as soon as the layer |
| 1156 | // becomes visible (and returns the old one). |
| 1157 | class TextureLayerChangeInvisibleMailboxTest |
| 1158 | : public LayerTreeTest, |
| 1159 | public TextureLayerClient { |
| 1160 | public: |
| 1161 | TextureLayerChangeInvisibleMailboxTest() |
| 1162 | : mailbox_changed_(true), |
| 1163 | mailbox_returned_(0), |
| 1164 | prepare_called_(0), |
| 1165 | commit_count_(0) { |
| 1166 | mailbox_ = MakeMailbox('1'); |
| 1167 | } |
| 1168 | |
| 1169 | // TextureLayerClient implementation. |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1170 | bool PrepareTextureMailbox( |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 1171 | viz::TextureMailbox* mailbox, |
danakj | 4d0dd80 | 2016-08-23 22:10:06 | [diff] [blame] | 1172 | std::unique_ptr<SingleReleaseCallback>* release_callback) override { |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1173 | ++prepare_called_; |
| 1174 | if (!mailbox_changed_) |
| 1175 | return false; |
| 1176 | *mailbox = mailbox_; |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1177 | *release_callback = SingleReleaseCallback::Create( |
| 1178 | base::Bind(&TextureLayerChangeInvisibleMailboxTest::MailboxReleased, |
| 1179 | base::Unretained(this))); |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1180 | return true; |
| 1181 | } |
| 1182 | |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 1183 | viz::TextureMailbox MakeMailbox(char name) { |
| 1184 | return viz::TextureMailbox(MailboxFromChar(name), |
| 1185 | SyncTokenFromUInt(static_cast<uint32_t>(name)), |
| 1186 | GL_TEXTURE_2D); |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1187 | } |
| 1188 | |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 1189 | void MailboxReleased(const gpu::SyncToken& sync_token, bool lost_resource) { |
dyen | e5db881b | 2016-03-01 19:47:03 | [diff] [blame] | 1190 | EXPECT_TRUE(sync_token.HasData()); |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1191 | ++mailbox_returned_; |
| 1192 | } |
| 1193 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1194 | void SetupTree() override { |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 1195 | scoped_refptr<Layer> root = Layer::Create(); |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1196 | root->SetBounds(gfx::Size(10, 10)); |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1197 | root->SetIsDrawable(true); |
| 1198 | |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 1199 | solid_layer_ = SolidColorLayer::Create(); |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1200 | solid_layer_->SetBounds(gfx::Size(10, 10)); |
| 1201 | solid_layer_->SetIsDrawable(true); |
| 1202 | solid_layer_->SetBackgroundColor(SK_ColorWHITE); |
| 1203 | root->AddChild(solid_layer_); |
| 1204 | |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 1205 | parent_layer_ = Layer::Create(); |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1206 | parent_layer_->SetBounds(gfx::Size(10, 10)); |
| 1207 | parent_layer_->SetIsDrawable(true); |
| 1208 | root->AddChild(parent_layer_); |
| 1209 | |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 1210 | texture_layer_ = TextureLayer::CreateForMailbox(this); |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1211 | texture_layer_->SetBounds(gfx::Size(10, 10)); |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1212 | texture_layer_->SetIsDrawable(true); |
| 1213 | parent_layer_->AddChild(texture_layer_); |
| 1214 | |
khushalsagar | b69ba945 | 2017-01-27 22:20:07 | [diff] [blame] | 1215 | layer_tree_host()->SetRootLayer(root); |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1216 | LayerTreeTest::SetupTree(); |
| 1217 | } |
| 1218 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1219 | void BeginTest() override { PostSetNeedsCommitToMainThread(); } |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1220 | |
samans | 44b6dfc | 2017-04-19 16:50:53 | [diff] [blame] | 1221 | void DidReceiveCompositorFrameAck() override { |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1222 | ++commit_count_; |
| 1223 | switch (commit_count_) { |
| 1224 | case 1: |
| 1225 | // We should have updated the layer, committing the texture. |
| 1226 | EXPECT_EQ(1, prepare_called_); |
| 1227 | // Make layer invisible. |
| 1228 | parent_layer_->SetOpacity(0.f); |
| 1229 | break; |
| 1230 | case 2: |
| 1231 | // Layer shouldn't have been updated. |
| 1232 | EXPECT_EQ(1, prepare_called_); |
| 1233 | // Change the texture. |
| 1234 | mailbox_ = MakeMailbox('2'); |
| 1235 | mailbox_changed_ = true; |
| 1236 | texture_layer_->SetNeedsDisplay(); |
| 1237 | // Force a change to make sure we draw a frame. |
| 1238 | solid_layer_->SetBackgroundColor(SK_ColorGRAY); |
| 1239 | break; |
| 1240 | case 3: |
| 1241 | // Layer shouldn't have been updated. |
| 1242 | EXPECT_EQ(1, prepare_called_); |
| 1243 | // So the old mailbox isn't returned yet. |
| 1244 | EXPECT_EQ(0, mailbox_returned_); |
| 1245 | // Make layer visible again. |
jaydasika | cf22376 | 2016-05-16 23:02:09 | [diff] [blame] | 1246 | parent_layer_->SetOpacity(0.9f); |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1247 | break; |
| 1248 | case 4: |
| 1249 | // Layer should have been updated. |
| 1250 | EXPECT_EQ(2, prepare_called_); |
| 1251 | // So the old mailbox should have been returned already. |
| 1252 | EXPECT_EQ(1, mailbox_returned_); |
| 1253 | texture_layer_->ClearClient(); |
| 1254 | break; |
| 1255 | case 5: |
samans | 44b6dfc | 2017-04-19 16:50:53 | [diff] [blame] | 1256 | EXPECT_EQ(2, mailbox_returned_); |
| 1257 | EndTest(); |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1258 | break; |
| 1259 | default: |
| 1260 | NOTREACHED(); |
| 1261 | break; |
| 1262 | } |
| 1263 | } |
| 1264 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1265 | void AfterTest() override {} |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1266 | |
| 1267 | private: |
| 1268 | scoped_refptr<SolidColorLayer> solid_layer_; |
| 1269 | scoped_refptr<Layer> parent_layer_; |
| 1270 | scoped_refptr<TextureLayer> texture_layer_; |
| 1271 | |
| 1272 | // Used on the main thread. |
| 1273 | bool mailbox_changed_; |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 1274 | viz::TextureMailbox mailbox_; |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1275 | int mailbox_returned_; |
| 1276 | int prepare_called_; |
| 1277 | int commit_count_; |
| 1278 | }; |
| 1279 | |
samans | 44b6dfc | 2017-04-19 16:50:53 | [diff] [blame] | 1280 | SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerChangeInvisibleMailboxTest); |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1281 | |
[email protected] | 0d7fb30 | 2014-01-23 21:30:47 | [diff] [blame] | 1282 | // Test that TextureLayerImpl::ReleaseResources can be called which releases |
| 1283 | // the mailbox back to TextureLayerClient. |
| 1284 | class TextureLayerReleaseResourcesBase |
| 1285 | : public LayerTreeTest, |
| 1286 | public TextureLayerClient { |
| 1287 | public: |
| 1288 | // TextureLayerClient implementation. |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1289 | bool PrepareTextureMailbox( |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 1290 | viz::TextureMailbox* mailbox, |
danakj | 4d0dd80 | 2016-08-23 22:10:06 | [diff] [blame] | 1291 | std::unique_ptr<SingleReleaseCallback>* release_callback) override { |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 1292 | *mailbox = viz::TextureMailbox(MailboxFromChar('1'), SyncTokenFromUInt(1), |
| 1293 | GL_TEXTURE_2D); |
[email protected] | 0d7fb30 | 2014-01-23 21:30:47 | [diff] [blame] | 1294 | *release_callback = SingleReleaseCallback::Create( |
| 1295 | base::Bind(&TextureLayerReleaseResourcesBase::MailboxReleased, |
| 1296 | base::Unretained(this))); |
| 1297 | return true; |
| 1298 | } |
| 1299 | |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 1300 | void MailboxReleased(const gpu::SyncToken& sync_token, bool lost_resource) { |
[email protected] | 0d7fb30 | 2014-01-23 21:30:47 | [diff] [blame] | 1301 | mailbox_released_ = true; |
| 1302 | } |
| 1303 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1304 | void SetupTree() override { |
[email protected] | 0d7fb30 | 2014-01-23 21:30:47 | [diff] [blame] | 1305 | LayerTreeTest::SetupTree(); |
| 1306 | |
| 1307 | scoped_refptr<TextureLayer> texture_layer = |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 1308 | TextureLayer::CreateForMailbox(this); |
[email protected] | 0d7fb30 | 2014-01-23 21:30:47 | [diff] [blame] | 1309 | texture_layer->SetBounds(gfx::Size(10, 10)); |
[email protected] | 0d7fb30 | 2014-01-23 21:30:47 | [diff] [blame] | 1310 | texture_layer->SetIsDrawable(true); |
| 1311 | |
khushalsagar | b69ba945 | 2017-01-27 22:20:07 | [diff] [blame] | 1312 | layer_tree_host()->root_layer()->AddChild(texture_layer); |
jaydasika | bd6f15a | 2016-04-21 19:45:37 | [diff] [blame] | 1313 | texture_layer_id_ = texture_layer->id(); |
[email protected] | 0d7fb30 | 2014-01-23 21:30:47 | [diff] [blame] | 1314 | } |
| 1315 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1316 | void BeginTest() override { |
[email protected] | 0d7fb30 | 2014-01-23 21:30:47 | [diff] [blame] | 1317 | mailbox_released_ = false; |
| 1318 | PostSetNeedsCommitToMainThread(); |
| 1319 | } |
| 1320 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1321 | void DidCommitAndDrawFrame() override { EndTest(); } |
[email protected] | 0d7fb30 | 2014-01-23 21:30:47 | [diff] [blame] | 1322 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1323 | void AfterTest() override { EXPECT_TRUE(mailbox_released_); } |
[email protected] | 0d7fb30 | 2014-01-23 21:30:47 | [diff] [blame] | 1324 | |
jaydasika | bd6f15a | 2016-04-21 19:45:37 | [diff] [blame] | 1325 | protected: |
| 1326 | int texture_layer_id_; |
| 1327 | |
[email protected] | 0d7fb30 | 2014-01-23 21:30:47 | [diff] [blame] | 1328 | private: |
| 1329 | bool mailbox_released_; |
| 1330 | }; |
| 1331 | |
| 1332 | class TextureLayerReleaseResourcesAfterCommit |
| 1333 | : public TextureLayerReleaseResourcesBase { |
| 1334 | public: |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1335 | void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override { |
kulkarni.a | 4015690f1 | 2014-10-10 13:50:06 | [diff] [blame] | 1336 | LayerTreeImpl* tree = nullptr; |
danakj | 009cdfdf | 2015-02-17 22:35:14 | [diff] [blame] | 1337 | tree = host_impl->sync_tree(); |
jaydasika | bd6f15a | 2016-04-21 19:45:37 | [diff] [blame] | 1338 | tree->LayerById(texture_layer_id_)->ReleaseResources(); |
[email protected] | 0d7fb30 | 2014-01-23 21:30:47 | [diff] [blame] | 1339 | } |
| 1340 | }; |
| 1341 | |
| 1342 | SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerReleaseResourcesAfterCommit); |
| 1343 | |
| 1344 | class TextureLayerReleaseResourcesAfterActivate |
| 1345 | : public TextureLayerReleaseResourcesBase { |
| 1346 | public: |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1347 | void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override { |
jaydasika | bd6f15a | 2016-04-21 19:45:37 | [diff] [blame] | 1348 | host_impl->active_tree()->LayerById(texture_layer_id_)->ReleaseResources(); |
[email protected] | 0d7fb30 | 2014-01-23 21:30:47 | [diff] [blame] | 1349 | } |
| 1350 | }; |
| 1351 | |
| 1352 | SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerReleaseResourcesAfterActivate); |
| 1353 | |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1354 | class TextureLayerWithMailboxMainThreadDeleted : public LayerTreeTest { |
| 1355 | public: |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 1356 | void ReleaseCallback(const gpu::SyncToken& sync_token, bool lost_resource) { |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 1357 | EXPECT_EQ(true, main_thread_.CalledOnValidThread()); |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1358 | EXPECT_FALSE(lost_resource); |
| 1359 | ++callback_count_; |
| 1360 | EndTest(); |
| 1361 | } |
| 1362 | |
| 1363 | void SetMailbox(char mailbox_char) { |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 1364 | EXPECT_EQ(true, main_thread_.CalledOnValidThread()); |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 1365 | std::unique_ptr<SingleReleaseCallback> callback = |
| 1366 | SingleReleaseCallback::Create(base::Bind( |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1367 | &TextureLayerWithMailboxMainThreadDeleted::ReleaseCallback, |
| 1368 | base::Unretained(this))); |
dyen | e5db881b | 2016-03-01 19:47:03 | [diff] [blame] | 1369 | layer_->SetTextureMailbox( |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 1370 | viz::TextureMailbox( |
| 1371 | MailboxFromChar(mailbox_char), |
| 1372 | SyncTokenFromUInt(static_cast<uint32_t>(mailbox_char)), |
| 1373 | GL_TEXTURE_2D), |
dyen | e5db881b | 2016-03-01 19:47:03 | [diff] [blame] | 1374 | std::move(callback)); |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1375 | } |
| 1376 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1377 | void SetupTree() override { |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1378 | gfx::Size bounds(100, 100); |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 1379 | root_ = Layer::Create(); |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1380 | root_->SetBounds(bounds); |
| 1381 | |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 1382 | layer_ = TextureLayer::CreateForMailbox(nullptr); |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1383 | layer_->SetIsDrawable(true); |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1384 | layer_->SetBounds(bounds); |
| 1385 | |
| 1386 | root_->AddChild(layer_); |
khushalsagar | b69ba945 | 2017-01-27 22:20:07 | [diff] [blame] | 1387 | layer_tree_host()->SetRootLayer(root_); |
| 1388 | layer_tree_host()->SetViewportSize(bounds); |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1389 | } |
| 1390 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1391 | void BeginTest() override { |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 1392 | EXPECT_EQ(true, main_thread_.CalledOnValidThread()); |
| 1393 | |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1394 | callback_count_ = 0; |
| 1395 | |
| 1396 | // Set the mailbox on the main thread. |
| 1397 | SetMailbox('1'); |
| 1398 | EXPECT_EQ(0, callback_count_); |
| 1399 | |
| 1400 | PostSetNeedsCommitToMainThread(); |
| 1401 | } |
| 1402 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1403 | void DidCommitAndDrawFrame() override { |
khushalsagar | cebe494 | 2016-09-07 23:27:01 | [diff] [blame] | 1404 | switch (layer_tree_host()->SourceFrameNumber()) { |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1405 | case 1: |
| 1406 | // Delete the TextureLayer on the main thread while the mailbox is in |
| 1407 | // the impl tree. |
| 1408 | layer_->RemoveFromParent(); |
kulkarni.a | 4015690f1 | 2014-10-10 13:50:06 | [diff] [blame] | 1409 | layer_ = nullptr; |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1410 | break; |
| 1411 | } |
| 1412 | } |
| 1413 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1414 | void AfterTest() override { EXPECT_EQ(1, callback_count_); } |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1415 | |
| 1416 | private: |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 1417 | base::ThreadChecker main_thread_; |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1418 | int callback_count_; |
| 1419 | scoped_refptr<Layer> root_; |
| 1420 | scoped_refptr<TextureLayer> layer_; |
| 1421 | }; |
| 1422 | |
danakj | 0943a11 | 2016-08-11 00:33:46 | [diff] [blame] | 1423 | SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerWithMailboxMainThreadDeleted); |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1424 | |
| 1425 | class TextureLayerWithMailboxImplThreadDeleted : public LayerTreeTest { |
| 1426 | public: |
dyen | cc16ed4d | 2015-11-03 20:03:04 | [diff] [blame] | 1427 | void ReleaseCallback(const gpu::SyncToken& sync_token, bool lost_resource) { |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 1428 | EXPECT_EQ(true, main_thread_.CalledOnValidThread()); |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1429 | EXPECT_FALSE(lost_resource); |
| 1430 | ++callback_count_; |
| 1431 | EndTest(); |
| 1432 | } |
| 1433 | |
| 1434 | void SetMailbox(char mailbox_char) { |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 1435 | EXPECT_EQ(true, main_thread_.CalledOnValidThread()); |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 1436 | std::unique_ptr<SingleReleaseCallback> callback = |
| 1437 | SingleReleaseCallback::Create(base::Bind( |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1438 | &TextureLayerWithMailboxImplThreadDeleted::ReleaseCallback, |
| 1439 | base::Unretained(this))); |
dyen | e5db881b | 2016-03-01 19:47:03 | [diff] [blame] | 1440 | layer_->SetTextureMailbox( |
Fady Samuel | 2a725ce5 | 2017-07-11 22:30:00 | [diff] [blame] | 1441 | viz::TextureMailbox( |
| 1442 | MailboxFromChar(mailbox_char), |
| 1443 | SyncTokenFromUInt(static_cast<uint32_t>(mailbox_char)), |
| 1444 | GL_TEXTURE_2D), |
dyen | e5db881b | 2016-03-01 19:47:03 | [diff] [blame] | 1445 | std::move(callback)); |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1446 | } |
| 1447 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1448 | void SetupTree() override { |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1449 | gfx::Size bounds(100, 100); |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 1450 | root_ = Layer::Create(); |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1451 | root_->SetBounds(bounds); |
| 1452 | |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 1453 | layer_ = TextureLayer::CreateForMailbox(nullptr); |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1454 | layer_->SetIsDrawable(true); |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1455 | layer_->SetBounds(bounds); |
| 1456 | |
| 1457 | root_->AddChild(layer_); |
khushalsagar | b69ba945 | 2017-01-27 22:20:07 | [diff] [blame] | 1458 | layer_tree_host()->SetRootLayer(root_); |
| 1459 | layer_tree_host()->SetViewportSize(bounds); |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1460 | } |
| 1461 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1462 | void BeginTest() override { |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 1463 | EXPECT_EQ(true, main_thread_.CalledOnValidThread()); |
| 1464 | |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1465 | callback_count_ = 0; |
| 1466 | |
| 1467 | // Set the mailbox on the main thread. |
| 1468 | SetMailbox('1'); |
| 1469 | EXPECT_EQ(0, callback_count_); |
| 1470 | |
| 1471 | PostSetNeedsCommitToMainThread(); |
| 1472 | } |
| 1473 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1474 | void DidCommitAndDrawFrame() override { |
khushalsagar | cebe494 | 2016-09-07 23:27:01 | [diff] [blame] | 1475 | switch (layer_tree_host()->SourceFrameNumber()) { |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1476 | case 1: |
| 1477 | // Remove the TextureLayer on the main thread while the mailbox is in |
| 1478 | // the impl tree, but don't delete the TextureLayer until after the impl |
| 1479 | // tree side is deleted. |
| 1480 | layer_->RemoveFromParent(); |
| 1481 | break; |
| 1482 | case 2: |
kulkarni.a | 4015690f1 | 2014-10-10 13:50:06 | [diff] [blame] | 1483 | layer_ = nullptr; |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1484 | break; |
| 1485 | } |
| 1486 | } |
| 1487 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 1488 | void AfterTest() override { EXPECT_EQ(1, callback_count_); } |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1489 | |
| 1490 | private: |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 1491 | base::ThreadChecker main_thread_; |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1492 | int callback_count_; |
| 1493 | scoped_refptr<Layer> root_; |
| 1494 | scoped_refptr<TextureLayer> layer_; |
| 1495 | }; |
| 1496 | |
danakj | 0943a11 | 2016-08-11 00:33:46 | [diff] [blame] | 1497 | SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerWithMailboxImplThreadDeleted); |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 1498 | |
[email protected] | ba56574 | 2012-11-10 09:29:48 | [diff] [blame] | 1499 | } // namespace |
| 1500 | } // namespace cc |