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