[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 | |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 7 | #include <algorithm> |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 8 | #include <string> |
| 9 | |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 11 | #include "base/callback.h" |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 12 | #include "base/synchronization/waitable_event.h" |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 13 | #include "base/threading/thread.h" |
| 14 | #include "base/time/time.h" |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 15 | #include "cc/layers/solid_color_layer.h" |
[email protected] | 97d519fb | 2013-03-29 02:27:54 | [diff] [blame] | 16 | #include "cc/layers/texture_layer_client.h" |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 17 | #include "cc/layers/texture_layer_impl.h" |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 18 | #include "cc/output/compositor_frame_ack.h" |
| 19 | #include "cc/output/context_provider.h" |
[email protected] | e00bab02 | 2013-08-19 00:42:45 | [diff] [blame] | 20 | #include "cc/resources/returned_resource.h" |
[email protected] | 586d51ed | 2012-12-07 20:31:45 | [diff] [blame] | 21 | #include "cc/test/fake_impl_proxy.h" |
[email protected] | 101441ce | 2012-10-16 01:45:03 | [diff] [blame] | 22 | #include "cc/test/fake_layer_tree_host_client.h" |
[email protected] | 586d51ed | 2012-12-07 20:31:45 | [diff] [blame] | 23 | #include "cc/test/fake_layer_tree_host_impl.h" |
[email protected] | 199b715e | 2013-08-13 05:18:34 | [diff] [blame] | 24 | #include "cc/test/fake_output_surface.h" |
[email protected] | 06d68d0 | 2013-04-19 18:46:21 | [diff] [blame] | 25 | #include "cc/test/layer_test_common.h" |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 26 | #include "cc/test/layer_tree_test.h" |
[email protected] | c2610b9f | 2013-10-31 06:54:59 | [diff] [blame] | 27 | #include "cc/test/test_web_graphics_context_3d.h" |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 28 | #include "cc/trees/blocking_task_runner.h" |
[email protected] | 556fd29 | 2013-03-18 08:03:04 | [diff] [blame] | 29 | #include "cc/trees/layer_tree_host.h" |
| 30 | #include "cc/trees/layer_tree_impl.h" |
| 31 | #include "cc/trees/single_thread_proxy.h" |
[email protected] | 0bf5a20 | 2013-07-10 14:50:54 | [diff] [blame] | 32 | #include "gpu/GLES2/gl2extchromium.h" |
[email protected] | 7f0c53db | 2012-10-02 00:23:18 | [diff] [blame] | 33 | #include "testing/gmock/include/gmock/gmock.h" |
| 34 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 35 | |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 36 | using ::testing::Mock; |
| 37 | using ::testing::_; |
| 38 | using ::testing::AtLeast; |
| 39 | using ::testing::AnyNumber; |
| 40 | |
[email protected] | ba56574 | 2012-11-10 09:29:48 | [diff] [blame] | 41 | namespace cc { |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 42 | namespace { |
| 43 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 44 | class MockLayerTreeHost : public LayerTreeHost { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 45 | public: |
[email protected] | 943528e | 2013-11-07 05:01:32 | [diff] [blame] | 46 | explicit MockLayerTreeHost(FakeLayerTreeHostClient* client) |
[email protected] | a7f3568 | 2013-10-22 23:05:57 | [diff] [blame] | 47 | : LayerTreeHost(client, NULL, LayerTreeSettings()) { |
[email protected] | 943528e | 2013-11-07 05:01:32 | [diff] [blame] | 48 | InitializeSingleThreaded(client); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 49 | } |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 50 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 51 | MOCK_METHOD0(AcquireLayerTextures, void()); |
| 52 | MOCK_METHOD0(SetNeedsCommit, void()); |
[email protected] | 3519b87 | 2013-07-30 07:17:50 | [diff] [blame] | 53 | MOCK_METHOD0(SetNeedsUpdateLayers, void()); |
[email protected] | aeeb337 | 2013-11-05 14:05:54 | [diff] [blame] | 54 | MOCK_METHOD0(StartRateLimiter, void()); |
| 55 | MOCK_METHOD0(StopRateLimiter, void()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 56 | }; |
| 57 | |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 58 | class TextureLayerTest : public testing::Test { |
| 59 | public: |
| 60 | TextureLayerTest() |
| 61 | : fake_client_( |
| 62 | FakeLayerTreeHostClient(FakeLayerTreeHostClient::DIRECT_3D)), |
| 63 | host_impl_(&proxy_) {} |
| 64 | |
| 65 | protected: |
| 66 | virtual void SetUp() { |
| 67 | layer_tree_host_.reset(new MockLayerTreeHost(&fake_client_)); |
| 68 | } |
| 69 | |
| 70 | virtual void TearDown() { |
| 71 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 72 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber()); |
| 73 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
| 74 | |
| 75 | layer_tree_host_->SetRootLayer(NULL); |
| 76 | layer_tree_host_.reset(); |
| 77 | } |
| 78 | |
| 79 | scoped_ptr<MockLayerTreeHost> layer_tree_host_; |
| 80 | FakeImplProxy proxy_; |
| 81 | FakeLayerTreeHostClient fake_client_; |
| 82 | FakeLayerTreeHostImpl host_impl_; |
| 83 | }; |
| 84 | |
| 85 | TEST_F(TextureLayerTest, SyncImplWhenChangingTextureId) { |
| 86 | scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL); |
| 87 | ASSERT_TRUE(test_layer.get()); |
| 88 | |
| 89 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber()); |
| 90 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
| 91 | layer_tree_host_->SetRootLayer(test_layer); |
| 92 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 93 | EXPECT_EQ(test_layer->layer_tree_host(), layer_tree_host_.get()); |
| 94 | |
| 95 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 96 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 97 | test_layer->SetTextureId(1); |
| 98 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 99 | |
| 100 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1)); |
| 101 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 102 | test_layer->SetTextureId(2); |
| 103 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 104 | |
| 105 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1)); |
| 106 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 107 | test_layer->SetTextureId(0); |
| 108 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 109 | } |
| 110 | |
| 111 | TEST_F(TextureLayerTest, SyncImplWhenDrawing) { |
| 112 | gfx::RectF dirty_rect(0.f, 0.f, 1.f, 1.f); |
| 113 | |
| 114 | scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL); |
| 115 | ASSERT_TRUE(test_layer.get()); |
| 116 | scoped_ptr<TextureLayerImpl> impl_layer; |
| 117 | impl_layer = TextureLayerImpl::Create(host_impl_.active_tree(), 1, false); |
| 118 | ASSERT_TRUE(impl_layer); |
| 119 | |
| 120 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber()); |
| 121 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
| 122 | layer_tree_host_->SetRootLayer(test_layer); |
| 123 | test_layer->SetTextureId(1); |
| 124 | test_layer->SetIsDrawable(true); |
| 125 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 126 | EXPECT_EQ(test_layer->layer_tree_host(), layer_tree_host_.get()); |
| 127 | |
| 128 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(1); |
| 129 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0); |
| 130 | test_layer->WillModifyTexture(); |
| 131 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 132 | |
| 133 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
[email protected] | 3519b87 | 2013-07-30 07:17:50 | [diff] [blame] | 134 | EXPECT_CALL(*layer_tree_host_, SetNeedsUpdateLayers()).Times(1); |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 135 | test_layer->SetNeedsDisplayRect(dirty_rect); |
| 136 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 137 | |
| 138 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 139 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(1); |
| 140 | test_layer->PushPropertiesTo(impl_layer.get()); // fake commit |
| 141 | test_layer->SetIsDrawable(false); |
| 142 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 143 | |
| 144 | // Verify that non-drawable layers don't signal the compositor, |
| 145 | // except for the first draw after last commit, which must acquire |
| 146 | // the texture. |
| 147 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(1); |
| 148 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0); |
| 149 | test_layer->WillModifyTexture(); |
| 150 | test_layer->SetNeedsDisplayRect(dirty_rect); |
| 151 | test_layer->PushPropertiesTo(impl_layer.get()); // fake commit |
| 152 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 153 | |
| 154 | // Second draw with layer in non-drawable state: no texture |
| 155 | // acquisition. |
| 156 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 157 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0); |
| 158 | test_layer->WillModifyTexture(); |
| 159 | test_layer->SetNeedsDisplayRect(dirty_rect); |
| 160 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 161 | } |
| 162 | |
| 163 | TEST_F(TextureLayerTest, SyncImplWhenRemovingFromTree) { |
| 164 | scoped_refptr<Layer> root_layer = Layer::Create(); |
| 165 | ASSERT_TRUE(root_layer.get()); |
| 166 | scoped_refptr<Layer> child_layer = Layer::Create(); |
| 167 | ASSERT_TRUE(child_layer.get()); |
| 168 | root_layer->AddChild(child_layer); |
| 169 | scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL); |
| 170 | ASSERT_TRUE(test_layer.get()); |
| 171 | test_layer->SetTextureId(0); |
| 172 | child_layer->AddChild(test_layer); |
| 173 | |
| 174 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber()); |
| 175 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
| 176 | layer_tree_host_->SetRootLayer(root_layer); |
| 177 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 178 | |
| 179 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 180 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 181 | test_layer->RemoveFromParent(); |
| 182 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 183 | |
| 184 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 185 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 186 | child_layer->AddChild(test_layer); |
| 187 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 188 | |
| 189 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 190 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 191 | test_layer->SetTextureId(1); |
| 192 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 193 | |
| 194 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1)); |
| 195 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 196 | test_layer->RemoveFromParent(); |
| 197 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 198 | } |
| 199 | |
| 200 | TEST_F(TextureLayerTest, CheckPropertyChangeCausesCorrectBehavior) { |
| 201 | scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL); |
[email protected] | 80d42bd | 2013-08-30 19:13:45 | [diff] [blame] | 202 | EXPECT_SET_NEEDS_COMMIT(1, layer_tree_host_->SetRootLayer(test_layer)); |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 203 | |
| 204 | // Test properties that should call SetNeedsCommit. All properties need to |
| 205 | // be set to new values in order for SetNeedsCommit to be called. |
| 206 | EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFlipped(false)); |
| 207 | EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetUV( |
| 208 | gfx::PointF(0.25f, 0.25f), gfx::PointF(0.75f, 0.75f))); |
| 209 | EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetVertexOpacity( |
| 210 | 0.5f, 0.5f, 0.5f, 0.5f)); |
| 211 | EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetPremultipliedAlpha(false)); |
| 212 | EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBlendBackgroundColor(true)); |
| 213 | EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTextureId(1)); |
| 214 | |
| 215 | // Calling SetTextureId can call AcquireLayerTextures. |
| 216 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber()); |
| 217 | } |
| 218 | |
[email protected] | 1c10e23 | 2013-07-31 12:35:43 | [diff] [blame] | 219 | TEST_F(TextureLayerTest, VisibleContentOpaqueRegion) { |
| 220 | const gfx::Size layer_bounds(100, 100); |
| 221 | const gfx::Rect layer_rect(layer_bounds); |
| 222 | const Region layer_region(layer_rect); |
| 223 | |
| 224 | scoped_refptr<TextureLayer> layer = TextureLayer::Create(NULL); |
| 225 | layer->SetBounds(layer_bounds); |
| 226 | layer->draw_properties().visible_content_rect = layer_rect; |
| 227 | layer->SetBlendBackgroundColor(true); |
| 228 | |
| 229 | // Verify initial conditions. |
| 230 | EXPECT_FALSE(layer->contents_opaque()); |
| 231 | EXPECT_EQ(0u, layer->background_color()); |
| 232 | EXPECT_EQ(Region().ToString(), |
| 233 | layer->VisibleContentOpaqueRegion().ToString()); |
| 234 | |
| 235 | // Opaque background. |
| 236 | layer->SetBackgroundColor(SK_ColorWHITE); |
| 237 | EXPECT_EQ(layer_region.ToString(), |
| 238 | layer->VisibleContentOpaqueRegion().ToString()); |
| 239 | |
| 240 | // Transparent background. |
| 241 | layer->SetBackgroundColor(SkColorSetARGB(100, 255, 255, 255)); |
| 242 | EXPECT_EQ(Region().ToString(), |
| 243 | layer->VisibleContentOpaqueRegion().ToString()); |
| 244 | } |
| 245 | |
[email protected] | 2f52981 | 2013-07-12 01:58:39 | [diff] [blame] | 246 | class FakeTextureLayerClient : public TextureLayerClient { |
| 247 | public: |
[email protected] | f5931d4 | 2013-11-06 19:44:57 | [diff] [blame] | 248 | FakeTextureLayerClient() {} |
[email protected] | 2f52981 | 2013-07-12 01:58:39 | [diff] [blame] | 249 | |
| 250 | virtual unsigned PrepareTexture() OVERRIDE { |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 251 | return 0; |
[email protected] | 2f52981 | 2013-07-12 01:58:39 | [diff] [blame] | 252 | } |
| 253 | |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 254 | virtual bool PrepareTextureMailbox( |
| 255 | TextureMailbox* mailbox, |
| 256 | scoped_ptr<SingleReleaseCallback>* release_callback, |
| 257 | bool use_shared_memory) OVERRIDE { |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 258 | *mailbox = TextureMailbox(); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 259 | *release_callback = scoped_ptr<SingleReleaseCallback>(); |
[email protected] | 2f52981 | 2013-07-12 01:58:39 | [diff] [blame] | 260 | return true; |
| 261 | } |
| 262 | |
| 263 | private: |
[email protected] | 2f52981 | 2013-07-12 01:58:39 | [diff] [blame] | 264 | DISALLOW_COPY_AND_ASSIGN(FakeTextureLayerClient); |
| 265 | }; |
| 266 | |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 267 | TEST_F(TextureLayerTest, RateLimiter) { |
| 268 | FakeTextureLayerClient client; |
| 269 | scoped_refptr<TextureLayer> test_layer = TextureLayer::CreateForMailbox( |
| 270 | &client); |
| 271 | test_layer->SetIsDrawable(true); |
| 272 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
| 273 | layer_tree_host_->SetRootLayer(test_layer); |
| 274 | |
| 275 | // Don't rate limit until we invalidate. |
[email protected] | aeeb337 | 2013-11-05 14:05:54 | [diff] [blame] | 276 | EXPECT_CALL(*layer_tree_host_, StartRateLimiter()).Times(0); |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 277 | test_layer->SetRateLimitContext(true); |
| 278 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 279 | |
| 280 | // Do rate limit after we invalidate. |
[email protected] | aeeb337 | 2013-11-05 14:05:54 | [diff] [blame] | 281 | EXPECT_CALL(*layer_tree_host_, StartRateLimiter()); |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 282 | test_layer->SetNeedsDisplay(); |
| 283 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 284 | |
| 285 | // Stop rate limiter when we don't want it any more. |
[email protected] | aeeb337 | 2013-11-05 14:05:54 | [diff] [blame] | 286 | EXPECT_CALL(*layer_tree_host_, StopRateLimiter()); |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 287 | test_layer->SetRateLimitContext(false); |
| 288 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 289 | |
| 290 | // Or we clear the client. |
| 291 | test_layer->SetRateLimitContext(true); |
[email protected] | aeeb337 | 2013-11-05 14:05:54 | [diff] [blame] | 292 | EXPECT_CALL(*layer_tree_host_, StopRateLimiter()); |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 293 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
| 294 | test_layer->ClearClient(); |
| 295 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 296 | |
| 297 | // Reset to a layer with a client, that started the rate limiter. |
| 298 | test_layer = TextureLayer::CreateForMailbox( |
| 299 | &client); |
| 300 | test_layer->SetIsDrawable(true); |
| 301 | test_layer->SetRateLimitContext(true); |
| 302 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
| 303 | layer_tree_host_->SetRootLayer(test_layer); |
[email protected] | aeeb337 | 2013-11-05 14:05:54 | [diff] [blame] | 304 | EXPECT_CALL(*layer_tree_host_, StartRateLimiter()).Times(0); |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 305 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | aeeb337 | 2013-11-05 14:05:54 | [diff] [blame] | 306 | EXPECT_CALL(*layer_tree_host_, StartRateLimiter()); |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 307 | test_layer->SetNeedsDisplay(); |
| 308 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 309 | |
| 310 | // Stop rate limiter when we're removed from the tree. |
[email protected] | aeeb337 | 2013-11-05 14:05:54 | [diff] [blame] | 311 | EXPECT_CALL(*layer_tree_host_, StopRateLimiter()); |
[email protected] | 31d4df8 | 2013-07-18 10:17:22 | [diff] [blame] | 312 | layer_tree_host_->SetRootLayer(NULL); |
| 313 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 314 | } |
| 315 | |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 316 | class MockMailboxCallback { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 317 | public: |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 318 | MOCK_METHOD3(Release, void(const std::string& mailbox, |
| 319 | unsigned sync_point, |
| 320 | bool lost_resource)); |
[email protected] | 42f40a5 | 2013-06-08 04:38:51 | [diff] [blame] | 321 | MOCK_METHOD3(Release2, void(base::SharedMemory* shared_memory, |
| 322 | unsigned sync_point, |
| 323 | bool lost_resource)); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 324 | }; |
| 325 | |
| 326 | struct CommonMailboxObjects { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 327 | CommonMailboxObjects() |
| 328 | : mailbox_name1_(64, '1'), |
| 329 | mailbox_name2_(64, '2'), |
| 330 | sync_point1_(1), |
[email protected] | 42f40a5 | 2013-06-08 04:38:51 | [diff] [blame] | 331 | sync_point2_(2), |
| 332 | shared_memory_(new base::SharedMemory) { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 333 | release_mailbox1_ = base::Bind(&MockMailboxCallback::Release, |
| 334 | base::Unretained(&mock_callback_), |
| 335 | mailbox_name1_); |
| 336 | release_mailbox2_ = base::Bind(&MockMailboxCallback::Release, |
| 337 | base::Unretained(&mock_callback_), |
| 338 | mailbox_name2_); |
| 339 | gpu::Mailbox m1; |
| 340 | m1.SetName(reinterpret_cast<const int8*>(mailbox_name1_.data())); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 341 | mailbox1_ = TextureMailbox(m1, sync_point1_); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 342 | gpu::Mailbox m2; |
| 343 | m2.SetName(reinterpret_cast<const int8*>(mailbox_name2_.data())); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 344 | mailbox2_ = TextureMailbox(m2, sync_point2_); |
[email protected] | 42f40a5 | 2013-06-08 04:38:51 | [diff] [blame] | 345 | |
| 346 | gfx::Size size(128, 128); |
| 347 | EXPECT_TRUE(shared_memory_->CreateAndMapAnonymous(4 * size.GetArea())); |
| 348 | release_mailbox3_ = base::Bind(&MockMailboxCallback::Release2, |
| 349 | base::Unretained(&mock_callback_), |
| 350 | shared_memory_.get()); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 351 | mailbox3_ = TextureMailbox(shared_memory_.get(), size); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 352 | } |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 353 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 354 | std::string mailbox_name1_; |
| 355 | std::string mailbox_name2_; |
| 356 | MockMailboxCallback mock_callback_; |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 357 | ReleaseCallback release_mailbox1_; |
| 358 | ReleaseCallback release_mailbox2_; |
| 359 | ReleaseCallback release_mailbox3_; |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 360 | TextureMailbox mailbox1_; |
| 361 | TextureMailbox mailbox2_; |
[email protected] | 42f40a5 | 2013-06-08 04:38:51 | [diff] [blame] | 362 | TextureMailbox mailbox3_; |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 363 | unsigned sync_point1_; |
| 364 | unsigned sync_point2_; |
[email protected] | 42f40a5 | 2013-06-08 04:38:51 | [diff] [blame] | 365 | scoped_ptr<base::SharedMemory> shared_memory_; |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 366 | }; |
| 367 | |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 368 | class TestMailboxHolder : public TextureLayer::MailboxHolder { |
| 369 | public: |
| 370 | using TextureLayer::MailboxHolder::Create; |
| 371 | |
| 372 | protected: |
| 373 | virtual ~TestMailboxHolder() {} |
| 374 | }; |
| 375 | |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 376 | class TextureLayerWithMailboxTest : public TextureLayerTest { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 377 | protected: |
| 378 | virtual void TearDown() { |
| 379 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 380 | EXPECT_CALL(test_data_.mock_callback_, |
| 381 | Release(test_data_.mailbox_name1_, |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 382 | test_data_.sync_point1_, |
| 383 | false)).Times(1); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 384 | TextureLayerTest::TearDown(); |
| 385 | } |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 386 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 387 | CommonMailboxObjects test_data_; |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 388 | }; |
| 389 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 390 | TEST_F(TextureLayerWithMailboxTest, ReplaceMailboxOnMainThreadBeforeCommit) { |
[email protected] | e8e4ae23 | 2013-04-12 00:26:01 | [diff] [blame] | 391 | scoped_refptr<TextureLayer> test_layer = TextureLayer::CreateForMailbox(NULL); |
[email protected] | 22898ed | 2013-06-01 04:52:30 | [diff] [blame] | 392 | ASSERT_TRUE(test_layer.get()); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 393 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 394 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 395 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
| 396 | layer_tree_host_->SetRootLayer(test_layer); |
| 397 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 398 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 399 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 400 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 401 | test_layer->SetTextureMailbox( |
| 402 | test_data_.mailbox1_, |
| 403 | SingleReleaseCallback::Create(test_data_.release_mailbox1_)); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 404 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 405 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 406 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 407 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 408 | EXPECT_CALL(test_data_.mock_callback_, |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 409 | Release(test_data_.mailbox_name1_, |
| 410 | test_data_.sync_point1_, |
| 411 | false)) |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 412 | .Times(1); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 413 | test_layer->SetTextureMailbox( |
| 414 | test_data_.mailbox2_, |
| 415 | SingleReleaseCallback::Create(test_data_.release_mailbox2_)); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 416 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 417 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 418 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 419 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 420 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 421 | EXPECT_CALL(test_data_.mock_callback_, |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 422 | Release(test_data_.mailbox_name2_, |
| 423 | test_data_.sync_point2_, |
| 424 | false)) |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 425 | .Times(1); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 426 | test_layer->SetTextureMailbox(TextureMailbox(), |
| 427 | scoped_ptr<SingleReleaseCallback>()); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 428 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 429 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 430 | |
[email protected] | 80d42bd | 2013-08-30 19:13:45 | [diff] [blame] | 431 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 432 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 433 | test_layer->SetTextureMailbox( |
| 434 | test_data_.mailbox3_, |
| 435 | SingleReleaseCallback::Create(test_data_.release_mailbox3_)); |
[email protected] | 42f40a5 | 2013-06-08 04:38:51 | [diff] [blame] | 436 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 437 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 438 | |
| 439 | EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
| 440 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
| 441 | EXPECT_CALL(test_data_.mock_callback_, |
| 442 | Release2(test_data_.shared_memory_.get(), |
| 443 | 0, false)) |
| 444 | .Times(1); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 445 | test_layer->SetTextureMailbox(TextureMailbox(), |
| 446 | scoped_ptr<SingleReleaseCallback>()); |
[email protected] | 42f40a5 | 2013-06-08 04:38:51 | [diff] [blame] | 447 | Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
| 448 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 449 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 450 | // Test destructor. |
| 451 | EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 452 | test_layer->SetTextureMailbox( |
| 453 | test_data_.mailbox1_, |
| 454 | SingleReleaseCallback::Create(test_data_.release_mailbox1_)); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 455 | } |
| 456 | |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 457 | class TextureLayerMailboxHolderTest : public TextureLayerTest { |
| 458 | public: |
| 459 | TextureLayerMailboxHolderTest() |
| 460 | : main_thread_("MAIN") { |
| 461 | main_thread_.Start(); |
| 462 | } |
| 463 | |
| 464 | void Wait(const base::Thread& thread) { |
| 465 | bool manual_reset = false; |
| 466 | bool initially_signaled = false; |
| 467 | base::WaitableEvent event(manual_reset, initially_signaled); |
| 468 | thread.message_loop()->PostTask( |
| 469 | FROM_HERE, |
| 470 | base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event))); |
| 471 | event.Wait(); |
| 472 | } |
| 473 | |
| 474 | void CreateMainRef() { |
| 475 | main_ref_ = TestMailboxHolder::Create( |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 476 | test_data_.mailbox1_, |
| 477 | SingleReleaseCallback::Create(test_data_.release_mailbox1_)).Pass(); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | void ReleaseMainRef() { |
| 481 | main_ref_.reset(); |
| 482 | } |
| 483 | |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 484 | void CreateImplRef(scoped_ptr<SingleReleaseCallback>* impl_ref) { |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 485 | *impl_ref = main_ref_->holder()->GetCallbackForImplThread(); |
| 486 | } |
| 487 | |
| 488 | void CapturePostTasksAndWait(base::WaitableEvent* begin_capture, |
| 489 | base::WaitableEvent* wait_for_capture, |
| 490 | base::WaitableEvent* stop_capture) { |
| 491 | begin_capture->Wait(); |
| 492 | BlockingTaskRunner::CapturePostTasks capture; |
| 493 | wait_for_capture->Signal(); |
| 494 | stop_capture->Wait(); |
| 495 | } |
| 496 | |
| 497 | protected: |
| 498 | scoped_ptr<TestMailboxHolder::MainThreadReference> |
| 499 | main_ref_; |
| 500 | base::Thread main_thread_; |
| 501 | CommonMailboxObjects test_data_; |
| 502 | }; |
| 503 | |
| 504 | TEST_F(TextureLayerMailboxHolderTest, TwoCompositors_BothReleaseThenMain) { |
| 505 | scoped_refptr<TextureLayer> test_layer = TextureLayer::CreateForMailbox(NULL); |
| 506 | ASSERT_TRUE(test_layer.get()); |
| 507 | |
| 508 | main_thread_.message_loop()->PostTask( |
| 509 | FROM_HERE, |
| 510 | base::Bind(&TextureLayerMailboxHolderTest::CreateMainRef, |
| 511 | base::Unretained(this))); |
| 512 | |
| 513 | Wait(main_thread_); |
| 514 | |
| 515 | // The texture layer is attached to compositor1, and passes a reference to its |
| 516 | // impl tree. |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 517 | scoped_ptr<SingleReleaseCallback> compositor1; |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 518 | main_thread_.message_loop()->PostTask( |
| 519 | FROM_HERE, |
| 520 | base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef, |
| 521 | base::Unretained(this), |
| 522 | &compositor1)); |
| 523 | |
| 524 | // Then the texture layer is removed and attached to compositor2, and passes a |
| 525 | // reference to its impl tree. |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 526 | scoped_ptr<SingleReleaseCallback> compositor2; |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 527 | main_thread_.message_loop()->PostTask( |
| 528 | FROM_HERE, |
| 529 | base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef, |
| 530 | base::Unretained(this), |
| 531 | &compositor2)); |
| 532 | |
| 533 | Wait(main_thread_); |
| 534 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 535 | |
| 536 | // The compositors both destroy their impl trees before the main thread layer |
| 537 | // is destroyed. |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 538 | compositor1->Run(100, false); |
| 539 | compositor2->Run(200, false); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 540 | |
| 541 | Wait(main_thread_); |
| 542 | |
| 543 | EXPECT_CALL(test_data_.mock_callback_, Release(_, _, _)).Times(0); |
| 544 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 545 | |
| 546 | // The main thread ref is the last one, so the mailbox is released back to the |
| 547 | // embedder, with the last sync point provided by the impl trees. |
| 548 | EXPECT_CALL(test_data_.mock_callback_, |
| 549 | Release(test_data_.mailbox_name1_, 200, false)).Times(1); |
| 550 | |
| 551 | main_thread_.message_loop()->PostTask( |
| 552 | FROM_HERE, |
| 553 | base::Bind(&TextureLayerMailboxHolderTest::ReleaseMainRef, |
| 554 | base::Unretained(this))); |
| 555 | Wait(main_thread_); |
| 556 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 557 | } |
| 558 | |
| 559 | TEST_F(TextureLayerMailboxHolderTest, TwoCompositors_MainReleaseBetween) { |
| 560 | scoped_refptr<TextureLayer> test_layer = TextureLayer::CreateForMailbox(NULL); |
| 561 | ASSERT_TRUE(test_layer.get()); |
| 562 | |
| 563 | main_thread_.message_loop()->PostTask( |
| 564 | FROM_HERE, |
| 565 | base::Bind(&TextureLayerMailboxHolderTest::CreateMainRef, |
| 566 | base::Unretained(this))); |
| 567 | |
| 568 | Wait(main_thread_); |
| 569 | |
| 570 | // The texture layer is attached to compositor1, and passes a reference to its |
| 571 | // impl tree. |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 572 | scoped_ptr<SingleReleaseCallback> compositor1; |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 573 | main_thread_.message_loop()->PostTask( |
| 574 | FROM_HERE, |
| 575 | base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef, |
| 576 | base::Unretained(this), |
| 577 | &compositor1)); |
| 578 | |
| 579 | // Then the texture layer is removed and attached to compositor2, and passes a |
| 580 | // reference to its impl tree. |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 581 | scoped_ptr<SingleReleaseCallback> compositor2; |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 582 | main_thread_.message_loop()->PostTask( |
| 583 | FROM_HERE, |
| 584 | base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef, |
| 585 | base::Unretained(this), |
| 586 | &compositor2)); |
| 587 | |
| 588 | Wait(main_thread_); |
| 589 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 590 | |
| 591 | // One compositor destroys their impl tree. |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 592 | compositor1->Run(100, false); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 593 | |
| 594 | // Then the main thread reference is destroyed. |
| 595 | main_thread_.message_loop()->PostTask( |
| 596 | FROM_HERE, |
| 597 | base::Bind(&TextureLayerMailboxHolderTest::ReleaseMainRef, |
| 598 | base::Unretained(this))); |
| 599 | |
| 600 | Wait(main_thread_); |
| 601 | |
| 602 | EXPECT_CALL(test_data_.mock_callback_, Release(_, _, _)).Times(0); |
| 603 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 604 | |
| 605 | // The second impl reference is destroyed last, causing the mailbox to be |
| 606 | // released back to the embedder with the last sync point from the impl tree. |
| 607 | EXPECT_CALL(test_data_.mock_callback_, |
| 608 | Release(test_data_.mailbox_name1_, 200, true)).Times(1); |
| 609 | |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 610 | compositor2->Run(200, true); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 611 | Wait(main_thread_); |
| 612 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 613 | } |
| 614 | |
| 615 | TEST_F(TextureLayerMailboxHolderTest, TwoCompositors_MainReleasedFirst) { |
| 616 | scoped_refptr<TextureLayer> test_layer = TextureLayer::CreateForMailbox(NULL); |
| 617 | ASSERT_TRUE(test_layer.get()); |
| 618 | |
| 619 | main_thread_.message_loop()->PostTask( |
| 620 | FROM_HERE, |
| 621 | base::Bind(&TextureLayerMailboxHolderTest::CreateMainRef, |
| 622 | base::Unretained(this))); |
| 623 | |
| 624 | Wait(main_thread_); |
| 625 | |
| 626 | // The texture layer is attached to compositor1, and passes a reference to its |
| 627 | // impl tree. |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 628 | scoped_ptr<SingleReleaseCallback> compositor1; |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 629 | main_thread_.message_loop()->PostTask( |
| 630 | FROM_HERE, |
| 631 | base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef, |
| 632 | base::Unretained(this), |
| 633 | &compositor1)); |
| 634 | |
| 635 | // Then the texture layer is removed and attached to compositor2, and passes a |
| 636 | // reference to its impl tree. |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 637 | scoped_ptr<SingleReleaseCallback> compositor2; |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 638 | main_thread_.message_loop()->PostTask( |
| 639 | FROM_HERE, |
| 640 | base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef, |
| 641 | base::Unretained(this), |
| 642 | &compositor2)); |
| 643 | |
| 644 | Wait(main_thread_); |
| 645 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 646 | |
| 647 | // The main thread reference is destroyed first. |
| 648 | main_thread_.message_loop()->PostTask( |
| 649 | FROM_HERE, |
| 650 | base::Bind(&TextureLayerMailboxHolderTest::ReleaseMainRef, |
| 651 | base::Unretained(this))); |
| 652 | |
| 653 | // One compositor destroys their impl tree. |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 654 | compositor2->Run(200, false); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 655 | |
| 656 | Wait(main_thread_); |
| 657 | |
| 658 | EXPECT_CALL(test_data_.mock_callback_, Release(_, _, _)).Times(0); |
| 659 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 660 | |
| 661 | // The second impl reference is destroyed last, causing the mailbox to be |
| 662 | // released back to the embedder with the last sync point from the impl tree. |
| 663 | EXPECT_CALL(test_data_.mock_callback_, |
| 664 | Release(test_data_.mailbox_name1_, 100, true)).Times(1); |
| 665 | |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 666 | compositor1->Run(100, true); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 667 | Wait(main_thread_); |
| 668 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 669 | } |
| 670 | |
| 671 | TEST_F(TextureLayerMailboxHolderTest, TwoCompositors_SecondImplRefShortcut) { |
| 672 | scoped_refptr<TextureLayer> test_layer = TextureLayer::CreateForMailbox(NULL); |
| 673 | ASSERT_TRUE(test_layer.get()); |
| 674 | |
| 675 | main_thread_.message_loop()->PostTask( |
| 676 | FROM_HERE, |
| 677 | base::Bind(&TextureLayerMailboxHolderTest::CreateMainRef, |
| 678 | base::Unretained(this))); |
| 679 | |
| 680 | Wait(main_thread_); |
| 681 | |
| 682 | // The texture layer is attached to compositor1, and passes a reference to its |
| 683 | // impl tree. |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 684 | scoped_ptr<SingleReleaseCallback> compositor1; |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 685 | main_thread_.message_loop()->PostTask( |
| 686 | FROM_HERE, |
| 687 | base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef, |
| 688 | base::Unretained(this), |
| 689 | &compositor1)); |
| 690 | |
| 691 | // Then the texture layer is removed and attached to compositor2, and passes a |
| 692 | // reference to its impl tree. |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 693 | scoped_ptr<SingleReleaseCallback> compositor2; |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 694 | main_thread_.message_loop()->PostTask( |
| 695 | FROM_HERE, |
| 696 | base::Bind(&TextureLayerMailboxHolderTest::CreateImplRef, |
| 697 | base::Unretained(this), |
| 698 | &compositor2)); |
| 699 | |
| 700 | Wait(main_thread_); |
| 701 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 702 | |
| 703 | // The main thread reference is destroyed first. |
| 704 | main_thread_.message_loop()->PostTask( |
| 705 | FROM_HERE, |
| 706 | base::Bind(&TextureLayerMailboxHolderTest::ReleaseMainRef, |
| 707 | base::Unretained(this))); |
| 708 | |
| 709 | EXPECT_CALL(test_data_.mock_callback_, |
| 710 | Release(test_data_.mailbox_name1_, 200, true)).Times(1); |
| 711 | |
| 712 | bool manual_reset = false; |
| 713 | bool initially_signaled = false; |
| 714 | base::WaitableEvent begin_capture(manual_reset, initially_signaled); |
| 715 | base::WaitableEvent wait_for_capture(manual_reset, initially_signaled); |
| 716 | base::WaitableEvent stop_capture(manual_reset, initially_signaled); |
| 717 | |
| 718 | // Post a task to start capturing tasks on the main thread. This will block |
| 719 | // the main thread until we signal the |stop_capture| event. |
| 720 | main_thread_.message_loop()->PostTask( |
| 721 | FROM_HERE, |
| 722 | base::Bind(&TextureLayerMailboxHolderTest::CapturePostTasksAndWait, |
| 723 | base::Unretained(this), |
| 724 | &begin_capture, |
| 725 | &wait_for_capture, |
| 726 | &stop_capture)); |
| 727 | |
| 728 | // Before the main thread capturing starts, one compositor destroys their |
| 729 | // impl reference. Since capturing did not start, this gets post-tasked to |
| 730 | // the main thread. |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 731 | compositor1->Run(100, false); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 732 | |
| 733 | // Start capturing on the main thread. |
| 734 | begin_capture.Signal(); |
| 735 | wait_for_capture.Wait(); |
| 736 | |
| 737 | // Meanwhile, the second compositor released its impl reference, but this task |
| 738 | // gets shortcutted directly to the main thread. This means the reference is |
| 739 | // released before compositor1, whose reference will be released later when |
| 740 | // the post-task is serviced. But since it was destroyed _on the impl thread_ |
| 741 | // last, its sync point values should be used. |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 742 | compositor2->Run(200, true); |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 743 | |
| 744 | stop_capture.Signal(); |
| 745 | Wait(main_thread_); |
| 746 | |
| 747 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
| 748 | } |
| 749 | |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 750 | class TextureLayerImplWithMailboxThreadedCallback : public LayerTreeTest { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 751 | public: |
| 752 | TextureLayerImplWithMailboxThreadedCallback() |
| 753 | : callback_count_(0), |
| 754 | commit_count_(0) {} |
| 755 | |
| 756 | // Make sure callback is received on main and doesn't block the impl thread. |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 757 | void ReleaseCallback(unsigned sync_point, bool lost_resource) { |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 758 | EXPECT_EQ(true, main_thread_.CalledOnValidThread()); |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 759 | EXPECT_FALSE(lost_resource); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 760 | ++callback_count_; |
| 761 | } |
| 762 | |
| 763 | void SetMailbox(char mailbox_char) { |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 764 | EXPECT_EQ(true, main_thread_.CalledOnValidThread()); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 765 | TextureMailbox mailbox(std::string(64, mailbox_char)); |
| 766 | scoped_ptr<SingleReleaseCallback> callback = SingleReleaseCallback::Create( |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 767 | base::Bind( |
| 768 | &TextureLayerImplWithMailboxThreadedCallback::ReleaseCallback, |
| 769 | base::Unretained(this))); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 770 | layer_->SetTextureMailbox(mailbox, callback.Pass()); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 771 | } |
| 772 | |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 773 | virtual void BeginTest() OVERRIDE { |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 774 | EXPECT_EQ(true, main_thread_.CalledOnValidThread()); |
| 775 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 776 | gfx::Size bounds(100, 100); |
| 777 | root_ = Layer::Create(); |
| 778 | root_->SetAnchorPoint(gfx::PointF()); |
| 779 | root_->SetBounds(bounds); |
| 780 | |
[email protected] | e8e4ae23 | 2013-04-12 00:26:01 | [diff] [blame] | 781 | layer_ = TextureLayer::CreateForMailbox(NULL); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 782 | layer_->SetIsDrawable(true); |
| 783 | layer_->SetAnchorPoint(gfx::PointF()); |
| 784 | layer_->SetBounds(bounds); |
| 785 | |
| 786 | root_->AddChild(layer_); |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 787 | layer_tree_host()->SetRootLayer(root_); |
[email protected] | 18ce5970 | 2013-04-09 04:58:40 | [diff] [blame] | 788 | layer_tree_host()->SetViewportSize(bounds); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 789 | SetMailbox('1'); |
| 790 | EXPECT_EQ(0, callback_count_); |
| 791 | |
| 792 | // Case #1: change mailbox before the commit. The old mailbox should be |
| 793 | // released immediately. |
| 794 | SetMailbox('2'); |
| 795 | EXPECT_EQ(1, callback_count_); |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 796 | PostSetNeedsCommitToMainThread(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 797 | } |
| 798 | |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 799 | virtual void DidCommit() OVERRIDE { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 800 | ++commit_count_; |
| 801 | switch (commit_count_) { |
| 802 | case 1: |
| 803 | // Case #2: change mailbox after the commit (and draw), where the |
| 804 | // layer draws. The old mailbox should be released during the next |
| 805 | // commit. |
| 806 | SetMailbox('3'); |
| 807 | EXPECT_EQ(1, callback_count_); |
| 808 | break; |
| 809 | case 2: |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 810 | EXPECT_EQ(2, callback_count_); |
| 811 | // Case #3: change mailbox when the layer doesn't draw. The old |
| 812 | // mailbox should be released during the next commit. |
| 813 | layer_->SetBounds(gfx::Size()); |
| 814 | SetMailbox('4'); |
| 815 | break; |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 816 | case 3: |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 817 | EXPECT_EQ(3, callback_count_); |
| 818 | // Case #4: release mailbox that was committed but never drawn. The |
| 819 | // old mailbox should be released during the next commit. |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 820 | layer_->SetTextureMailbox(TextureMailbox(), |
| 821 | scoped_ptr<SingleReleaseCallback>()); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 822 | break; |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 823 | case 4: |
| 824 | if (layer_tree_host()->settings().impl_side_painting) { |
| 825 | // With impl painting, the texture mailbox will still be on the impl |
| 826 | // thread when the commit finishes, because the layer is not drawble |
| 827 | // when it has no texture mailbox, and thus does not block the commit |
| 828 | // on activation. So, we wait for activation. |
| 829 | // TODO(danakj): fix this. crbug.com/277953 |
| 830 | layer_tree_host()->SetNeedsCommit(); |
| 831 | break; |
| 832 | } else { |
| 833 | ++commit_count_; |
| 834 | } |
| 835 | case 5: |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 836 | EXPECT_EQ(4, callback_count_); |
[email protected] | 7096acc | 2013-06-18 21:12:43 | [diff] [blame] | 837 | // Restore a mailbox for the next step. |
| 838 | SetMailbox('5'); |
| 839 | break; |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 840 | case 6: |
[email protected] | 7096acc | 2013-06-18 21:12:43 | [diff] [blame] | 841 | // Case #5: remove layer from tree. Callback should *not* be called, the |
| 842 | // mailbox is returned to the main thread. |
| 843 | EXPECT_EQ(4, callback_count_); |
| 844 | layer_->RemoveFromParent(); |
| 845 | break; |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 846 | case 7: |
| 847 | if (layer_tree_host()->settings().impl_side_painting) { |
| 848 | // With impl painting, the texture mailbox will still be on the impl |
| 849 | // thread when the commit finishes, because the layer is not around to |
| 850 | // block the commit on activation anymore. So, we wait for activation. |
| 851 | // TODO(danakj): fix this. crbug.com/277953 |
| 852 | layer_tree_host()->SetNeedsCommit(); |
| 853 | break; |
| 854 | } else { |
| 855 | ++commit_count_; |
| 856 | } |
| 857 | case 8: |
[email protected] | 7096acc | 2013-06-18 21:12:43 | [diff] [blame] | 858 | EXPECT_EQ(4, callback_count_); |
| 859 | // Resetting the mailbox will call the callback now. |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 860 | layer_->SetTextureMailbox(TextureMailbox(), |
| 861 | scoped_ptr<SingleReleaseCallback>()); |
[email protected] | 7096acc | 2013-06-18 21:12:43 | [diff] [blame] | 862 | EXPECT_EQ(5, callback_count_); |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 863 | EndTest(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 864 | break; |
| 865 | default: |
| 866 | NOTREACHED(); |
| 867 | break; |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 868 | } |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 869 | } |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 870 | |
[email protected] | e216fef0 | 2013-03-20 22:56:10 | [diff] [blame] | 871 | virtual void AfterTest() OVERRIDE {} |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 872 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 873 | private: |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 874 | base::ThreadChecker main_thread_; |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 875 | int callback_count_; |
| 876 | int commit_count_; |
| 877 | scoped_refptr<Layer> root_; |
| 878 | scoped_refptr<TextureLayer> layer_; |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 879 | }; |
| 880 | |
[email protected] | 4145d17 | 2013-05-10 16:54:36 | [diff] [blame] | 881 | SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F( |
| 882 | TextureLayerImplWithMailboxThreadedCallback); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 883 | |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 884 | |
| 885 | class TextureLayerNoMailboxIsActivatedDuringCommit : public LayerTreeTest, |
| 886 | public TextureLayerClient { |
| 887 | protected: |
| 888 | TextureLayerNoMailboxIsActivatedDuringCommit() |
| 889 | : wait_thread_("WAIT"), |
[email protected] | f10dc47 | 2013-09-27 03:31:59 | [diff] [blame] | 890 | wait_event_(false, false), |
[email protected] | f5931d4 | 2013-11-06 19:44:57 | [diff] [blame] | 891 | texture_(0u) { |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 892 | wait_thread_.Start(); |
| 893 | } |
| 894 | |
| 895 | virtual void BeginTest() OVERRIDE { |
| 896 | activate_count_ = 0; |
| 897 | |
| 898 | gfx::Size bounds(100, 100); |
| 899 | root_ = Layer::Create(); |
| 900 | root_->SetAnchorPoint(gfx::PointF()); |
| 901 | root_->SetBounds(bounds); |
| 902 | |
| 903 | layer_ = TextureLayer::Create(this); |
| 904 | layer_->SetIsDrawable(true); |
| 905 | layer_->SetAnchorPoint(gfx::PointF()); |
| 906 | layer_->SetBounds(bounds); |
| 907 | |
| 908 | root_->AddChild(layer_); |
| 909 | layer_tree_host()->SetRootLayer(root_); |
| 910 | layer_tree_host()->SetViewportSize(bounds); |
| 911 | |
| 912 | PostSetNeedsCommitToMainThread(); |
| 913 | } |
| 914 | |
[email protected] | f5931d4 | 2013-11-06 19:44:57 | [diff] [blame] | 915 | virtual scoped_ptr<OutputSurface> CreateOutputSurface(bool fallback) |
| 916 | OVERRIDE { |
| 917 | scoped_refptr<TestContextProvider> provider = TestContextProvider::Create(); |
| 918 | texture_ = provider->UnboundTestContext3d()->createExternalTexture(); |
| 919 | return FakeOutputSurface::Create3d(provider).PassAs<OutputSurface>(); |
| 920 | } |
| 921 | |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 922 | // TextureLayerClient implementation. |
| 923 | virtual unsigned PrepareTexture() OVERRIDE { |
[email protected] | f5931d4 | 2013-11-06 19:44:57 | [diff] [blame] | 924 | return texture_; |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 925 | } |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 926 | virtual bool PrepareTextureMailbox( |
| 927 | TextureMailbox* mailbox, |
| 928 | scoped_ptr<SingleReleaseCallback>* release_callback, |
| 929 | bool use_shared_memory) OVERRIDE { |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 930 | return false; |
| 931 | } |
| 932 | |
| 933 | virtual void WillActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE { |
| 934 | // Slow down activation so the main thread DidCommit() will run if |
| 935 | // not blocked. |
| 936 | wait_thread_.message_loop()->PostDelayedTask( |
| 937 | FROM_HERE, |
| 938 | base::Bind(&base::WaitableEvent::Signal, |
| 939 | base::Unretained(&wait_event_)), |
| 940 | base::TimeDelta::FromMilliseconds(10)); |
| 941 | wait_event_.Wait(); |
| 942 | |
| 943 | base::AutoLock lock(activate_lock_); |
| 944 | ++activate_count_; |
| 945 | } |
| 946 | |
| 947 | virtual void DidActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE { |
| 948 | // The main thread is awake now, and will run DidCommit() immediately. |
| 949 | // Run DidActivate() afterwards by posting it now. |
| 950 | proxy()->MainThreadTaskRunner()->PostTask( |
| 951 | FROM_HERE, |
| 952 | base::Bind(&TextureLayerNoMailboxIsActivatedDuringCommit::DidActivate, |
| 953 | base::Unretained(this))); |
| 954 | } |
| 955 | |
| 956 | void DidActivate() { |
| 957 | base::AutoLock lock(activate_lock_); |
| 958 | switch (activate_count_) { |
| 959 | case 1: |
| 960 | // The first texture has been activated. Invalidate the layer so it |
| 961 | // grabs a new texture id from the client. |
| 962 | layer_->SetNeedsDisplay(); |
| 963 | // So this commit number should complete after the second activate. |
| 964 | EXPECT_EQ(1, layer_tree_host()->source_frame_number()); |
| 965 | break; |
| 966 | case 2: |
| 967 | // The second mailbox has been activated. Remove the layer from |
| 968 | // the tree to cause another commit/activation. The commit should |
| 969 | // finish *after* the layer is removed from the active tree. |
| 970 | layer_->RemoveFromParent(); |
| 971 | // So this commit number should complete after the third activate. |
| 972 | EXPECT_EQ(2, layer_tree_host()->source_frame_number()); |
| 973 | break; |
| 974 | case 3: |
| 975 | EndTest(); |
| 976 | break; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | virtual void DidCommit() OVERRIDE { |
| 981 | switch (layer_tree_host()->source_frame_number()) { |
| 982 | case 2: { |
| 983 | // The activate for the 2nd texture should have happened before now. |
| 984 | base::AutoLock lock(activate_lock_); |
| 985 | EXPECT_EQ(2, activate_count_); |
| 986 | break; |
| 987 | } |
| 988 | case 3: { |
| 989 | // The activate to remove the layer should have happened before now. |
| 990 | base::AutoLock lock(activate_lock_); |
| 991 | EXPECT_EQ(3, activate_count_); |
| 992 | break; |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | |
| 998 | virtual void AfterTest() OVERRIDE {} |
| 999 | |
| 1000 | base::Thread wait_thread_; |
| 1001 | base::WaitableEvent wait_event_; |
| 1002 | base::Lock activate_lock_; |
[email protected] | f5931d4 | 2013-11-06 19:44:57 | [diff] [blame] | 1003 | unsigned texture_; |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 1004 | int activate_count_; |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 1005 | scoped_refptr<Layer> root_; |
| 1006 | scoped_refptr<TextureLayer> layer_; |
| 1007 | }; |
| 1008 | |
| 1009 | SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F( |
| 1010 | TextureLayerNoMailboxIsActivatedDuringCommit); |
| 1011 | |
| 1012 | class TextureLayerMailboxIsActivatedDuringCommit : public LayerTreeTest { |
| 1013 | protected: |
| 1014 | TextureLayerMailboxIsActivatedDuringCommit() |
| 1015 | : wait_thread_("WAIT"), |
| 1016 | wait_event_(false, false) { |
| 1017 | wait_thread_.Start(); |
| 1018 | } |
| 1019 | |
| 1020 | static void ReleaseCallback(unsigned sync_point, bool lost_resource) {} |
| 1021 | |
| 1022 | void SetMailbox(char mailbox_char) { |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1023 | TextureMailbox mailbox(std::string(64, mailbox_char)); |
| 1024 | scoped_ptr<SingleReleaseCallback> callback = SingleReleaseCallback::Create( |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 1025 | base::Bind( |
| 1026 | &TextureLayerMailboxIsActivatedDuringCommit::ReleaseCallback)); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1027 | layer_->SetTextureMailbox(mailbox, callback.Pass()); |
[email protected] | 74b43cc | 2013-08-30 06:29:27 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | virtual void BeginTest() OVERRIDE { |
| 1031 | activate_count_ = 0; |
| 1032 | |
| 1033 | gfx::Size bounds(100, 100); |
| 1034 | root_ = Layer::Create(); |
| 1035 | root_->SetAnchorPoint(gfx::PointF()); |
| 1036 | root_->SetBounds(bounds); |
| 1037 | |
| 1038 | layer_ = TextureLayer::CreateForMailbox(NULL); |
| 1039 | layer_->SetIsDrawable(true); |
| 1040 | layer_->SetAnchorPoint(gfx::PointF()); |
| 1041 | layer_->SetBounds(bounds); |
| 1042 | |
| 1043 | root_->AddChild(layer_); |
| 1044 | layer_tree_host()->SetRootLayer(root_); |
| 1045 | layer_tree_host()->SetViewportSize(bounds); |
| 1046 | SetMailbox('1'); |
| 1047 | |
| 1048 | PostSetNeedsCommitToMainThread(); |
| 1049 | } |
| 1050 | |
| 1051 | virtual void WillActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE { |
| 1052 | // Slow down activation so the main thread DidCommit() will run if |
| 1053 | // not blocked. |
| 1054 | wait_thread_.message_loop()->PostDelayedTask( |
| 1055 | FROM_HERE, |
| 1056 | base::Bind(&base::WaitableEvent::Signal, |
| 1057 | base::Unretained(&wait_event_)), |
| 1058 | base::TimeDelta::FromMilliseconds(10)); |
| 1059 | wait_event_.Wait(); |
| 1060 | |
| 1061 | base::AutoLock lock(activate_lock_); |
| 1062 | ++activate_count_; |
| 1063 | } |
| 1064 | |
| 1065 | virtual void DidActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE { |
| 1066 | // The main thread is awake now, and will run DidCommit() immediately. |
| 1067 | // Run DidActivate() afterwards by posting it now. |
| 1068 | proxy()->MainThreadTaskRunner()->PostTask( |
| 1069 | FROM_HERE, |
| 1070 | base::Bind(&TextureLayerMailboxIsActivatedDuringCommit::DidActivate, |
| 1071 | base::Unretained(this))); |
| 1072 | } |
| 1073 | |
| 1074 | void DidActivate() { |
| 1075 | base::AutoLock lock(activate_lock_); |
| 1076 | switch (activate_count_) { |
| 1077 | case 1: |
| 1078 | // The first mailbox has been activated. Set a new mailbox, and |
| 1079 | // expect the next commit to finish *after* it is activated. |
| 1080 | SetMailbox('2'); |
| 1081 | // So this commit number should complete after the second activate. |
| 1082 | EXPECT_EQ(1, layer_tree_host()->source_frame_number()); |
| 1083 | break; |
| 1084 | case 2: |
| 1085 | // The second mailbox has been activated. Remove the layer from |
| 1086 | // the tree to cause another commit/activation. The commit should |
| 1087 | // finish *after* the layer is removed from the active tree. |
| 1088 | layer_->RemoveFromParent(); |
| 1089 | // So this commit number should complete after the third activate. |
| 1090 | EXPECT_EQ(2, layer_tree_host()->source_frame_number()); |
| 1091 | break; |
| 1092 | case 3: |
| 1093 | EndTest(); |
| 1094 | break; |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | virtual void DidCommit() OVERRIDE { |
| 1099 | switch (layer_tree_host()->source_frame_number()) { |
| 1100 | case 2: { |
| 1101 | // The activate for the 2nd mailbox should have happened before now. |
| 1102 | base::AutoLock lock(activate_lock_); |
| 1103 | EXPECT_EQ(2, activate_count_); |
| 1104 | break; |
| 1105 | } |
| 1106 | case 3: { |
| 1107 | // The activate to remove the layer should have happened before now. |
| 1108 | base::AutoLock lock(activate_lock_); |
| 1109 | EXPECT_EQ(3, activate_count_); |
| 1110 | break; |
| 1111 | } |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | |
| 1116 | virtual void AfterTest() OVERRIDE {} |
| 1117 | |
| 1118 | base::Thread wait_thread_; |
| 1119 | base::WaitableEvent wait_event_; |
| 1120 | base::Lock activate_lock_; |
| 1121 | int activate_count_; |
| 1122 | scoped_refptr<Layer> root_; |
| 1123 | scoped_refptr<TextureLayer> layer_; |
| 1124 | }; |
| 1125 | |
| 1126 | SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F( |
| 1127 | TextureLayerMailboxIsActivatedDuringCommit); |
| 1128 | |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 1129 | class TextureLayerImplWithMailboxTest : public TextureLayerTest { |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1130 | protected: |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 1131 | TextureLayerImplWithMailboxTest() |
| 1132 | : fake_client_( |
| 1133 | FakeLayerTreeHostClient(FakeLayerTreeHostClient::DIRECT_3D)) {} |
| 1134 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1135 | virtual void SetUp() { |
| 1136 | TextureLayerTest::SetUp(); |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 1137 | layer_tree_host_.reset(new MockLayerTreeHost(&fake_client_)); |
[email protected] | f74945f | 2013-03-21 17:08:36 | [diff] [blame] | 1138 | EXPECT_TRUE(host_impl_.InitializeRenderer(CreateFakeOutputSurface())); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1139 | } |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 1140 | |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 1141 | bool WillDraw(TextureLayerImpl* layer, DrawMode mode) { |
| 1142 | bool will_draw = layer->WillDraw( |
| 1143 | mode, host_impl_.active_tree()->resource_provider()); |
| 1144 | if (will_draw) |
| 1145 | layer->DidDraw(host_impl_.active_tree()->resource_provider()); |
| 1146 | return will_draw; |
| 1147 | } |
| 1148 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1149 | CommonMailboxObjects test_data_; |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 1150 | FakeLayerTreeHostClient fake_client_; |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 1151 | }; |
| 1152 | |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 1153 | // Test conditions for results of TextureLayerImpl::WillDraw under |
| 1154 | // different configurations of different mailbox, texture_id, and draw_mode. |
| 1155 | TEST_F(TextureLayerImplWithMailboxTest, TestWillDraw) { |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 1156 | EXPECT_CALL(test_data_.mock_callback_, |
| 1157 | Release(test_data_.mailbox_name1_, |
| 1158 | test_data_.sync_point1_, |
| 1159 | false)) |
| 1160 | .Times(AnyNumber()); |
| 1161 | EXPECT_CALL(test_data_.mock_callback_, |
| 1162 | Release2(test_data_.shared_memory_.get(), 0, false)) |
| 1163 | .Times(AnyNumber()); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 1164 | // Hardware mode. |
| 1165 | { |
| 1166 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 1167 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, true); |
[email protected] | afc4f26 | 2013-10-05 01:14:10 | [diff] [blame] | 1168 | impl_layer->SetDrawsContent(true); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1169 | impl_layer->SetTextureMailbox( |
| 1170 | test_data_.mailbox1_, |
| 1171 | SingleReleaseCallback::Create(test_data_.release_mailbox1_)); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 1172 | EXPECT_TRUE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE)); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | { |
| 1176 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 1177 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, true); |
[email protected] | afc4f26 | 2013-10-05 01:14:10 | [diff] [blame] | 1178 | impl_layer->SetDrawsContent(true); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1179 | impl_layer->SetTextureMailbox(TextureMailbox(), |
| 1180 | scoped_ptr<SingleReleaseCallback>()); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 1181 | EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE)); |
| 1182 | } |
| 1183 | |
| 1184 | { |
| 1185 | // Software resource. |
| 1186 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 1187 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, true); |
[email protected] | afc4f26 | 2013-10-05 01:14:10 | [diff] [blame] | 1188 | impl_layer->SetDrawsContent(true); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1189 | impl_layer->SetTextureMailbox( |
| 1190 | test_data_.mailbox3_, |
| 1191 | SingleReleaseCallback::Create(test_data_.release_mailbox3_)); |
[email protected] | 3e44d7a | 2013-07-30 00:03:10 | [diff] [blame] | 1192 | EXPECT_TRUE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE)); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | { |
| 1196 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 1197 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, false); |
[email protected] | afc4f26 | 2013-10-05 01:14:10 | [diff] [blame] | 1198 | impl_layer->SetDrawsContent(true); |
[email protected] | 0634cdd4 | 2013-08-16 00:46:09 | [diff] [blame] | 1199 | ContextProvider* context_provider = |
| 1200 | host_impl_.output_surface()->context_provider(); |
[email protected] | 1dff796 | 2014-01-10 12:05:03 | [diff] [blame^] | 1201 | GLuint texture = 0; |
| 1202 | context_provider->ContextGL()->GenTextures(1, &texture); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 1203 | impl_layer->set_texture_id(texture); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 1204 | EXPECT_TRUE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE)); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | { |
| 1208 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 1209 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, false); |
[email protected] | afc4f26 | 2013-10-05 01:14:10 | [diff] [blame] | 1210 | impl_layer->SetDrawsContent(true); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 1211 | impl_layer->set_texture_id(0); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 1212 | EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_HARDWARE)); |
| 1213 | } |
| 1214 | |
| 1215 | // Software mode. |
| 1216 | { |
| 1217 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 1218 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, true); |
[email protected] | afc4f26 | 2013-10-05 01:14:10 | [diff] [blame] | 1219 | impl_layer->SetDrawsContent(true); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1220 | impl_layer->SetTextureMailbox( |
| 1221 | test_data_.mailbox1_, |
| 1222 | SingleReleaseCallback::Create(test_data_.release_mailbox1_)); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 1223 | EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_SOFTWARE)); |
| 1224 | } |
| 1225 | |
| 1226 | { |
| 1227 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 1228 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, true); |
[email protected] | afc4f26 | 2013-10-05 01:14:10 | [diff] [blame] | 1229 | impl_layer->SetDrawsContent(true); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1230 | impl_layer->SetTextureMailbox(TextureMailbox(), |
| 1231 | scoped_ptr<SingleReleaseCallback>()); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 1232 | EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_SOFTWARE)); |
| 1233 | } |
| 1234 | |
| 1235 | { |
| 1236 | // Software resource. |
| 1237 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 1238 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, true); |
[email protected] | afc4f26 | 2013-10-05 01:14:10 | [diff] [blame] | 1239 | impl_layer->SetDrawsContent(true); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1240 | impl_layer->SetTextureMailbox( |
| 1241 | test_data_.mailbox3_, |
| 1242 | SingleReleaseCallback::Create(test_data_.release_mailbox3_)); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 1243 | EXPECT_TRUE(WillDraw(impl_layer.get(), DRAW_MODE_SOFTWARE)); |
| 1244 | } |
| 1245 | |
| 1246 | { |
| 1247 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 1248 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, false); |
[email protected] | afc4f26 | 2013-10-05 01:14:10 | [diff] [blame] | 1249 | impl_layer->SetDrawsContent(true); |
[email protected] | 0634cdd4 | 2013-08-16 00:46:09 | [diff] [blame] | 1250 | ContextProvider* context_provider = |
| 1251 | host_impl_.output_surface()->context_provider(); |
[email protected] | 1dff796 | 2014-01-10 12:05:03 | [diff] [blame^] | 1252 | GLuint texture = 0; |
| 1253 | context_provider->ContextGL()->GenTextures(1, &texture); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 1254 | impl_layer->set_texture_id(texture); |
| 1255 | EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_SOFTWARE)); |
| 1256 | } |
| 1257 | |
| 1258 | { |
| 1259 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 1260 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, false); |
[email protected] | afc4f26 | 2013-10-05 01:14:10 | [diff] [blame] | 1261 | impl_layer->SetDrawsContent(true); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 1262 | impl_layer->set_texture_id(0); |
| 1263 | EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_SOFTWARE)); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | // Resourceless software mode. |
| 1267 | { |
| 1268 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 1269 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, true); |
[email protected] | afc4f26 | 2013-10-05 01:14:10 | [diff] [blame] | 1270 | impl_layer->SetDrawsContent(true); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1271 | impl_layer->SetTextureMailbox( |
| 1272 | test_data_.mailbox1_, |
| 1273 | SingleReleaseCallback::Create(test_data_.release_mailbox1_)); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 1274 | EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_RESOURCELESS_SOFTWARE)); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | { |
| 1278 | scoped_ptr<TextureLayerImpl> impl_layer = |
| 1279 | TextureLayerImpl::Create(host_impl_.active_tree(), 1, false); |
[email protected] | afc4f26 | 2013-10-05 01:14:10 | [diff] [blame] | 1280 | impl_layer->SetDrawsContent(true); |
[email protected] | 0634cdd4 | 2013-08-16 00:46:09 | [diff] [blame] | 1281 | ContextProvider* context_provider = |
| 1282 | host_impl_.output_surface()->context_provider(); |
[email protected] | 1dff796 | 2014-01-10 12:05:03 | [diff] [blame^] | 1283 | GLuint texture = 0; |
| 1284 | context_provider->ContextGL()->GenTextures(1, &texture); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 1285 | impl_layer->set_texture_id(texture); |
[email protected] | 0ec335c4 | 2013-07-04 06:17:08 | [diff] [blame] | 1286 | EXPECT_FALSE(WillDraw(impl_layer.get(), DRAW_MODE_RESOURCELESS_SOFTWARE)); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 1287 | } |
| 1288 | } |
| 1289 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1290 | TEST_F(TextureLayerImplWithMailboxTest, TestImplLayerCallbacks) { |
| 1291 | host_impl_.CreatePendingTree(); |
| 1292 | scoped_ptr<TextureLayerImpl> pending_layer; |
| 1293 | pending_layer = TextureLayerImpl::Create(host_impl_.pending_tree(), 1, true); |
| 1294 | ASSERT_TRUE(pending_layer); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 1295 | |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 1296 | scoped_ptr<LayerImpl> active_layer( |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1297 | pending_layer->CreateLayerImpl(host_impl_.active_tree())); |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 1298 | ASSERT_TRUE(active_layer); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 1299 | |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1300 | pending_layer->SetTextureMailbox( |
| 1301 | test_data_.mailbox1_, |
| 1302 | SingleReleaseCallback::Create(test_data_.release_mailbox1_)); |
[email protected] | 421e84f | 2013-02-22 03:27:15 | [diff] [blame] | 1303 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1304 | // Test multiple commits without an activation. |
| 1305 | EXPECT_CALL(test_data_.mock_callback_, |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 1306 | Release(test_data_.mailbox_name1_, |
| 1307 | test_data_.sync_point1_, |
| 1308 | false)) |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1309 | .Times(1); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1310 | pending_layer->SetTextureMailbox( |
| 1311 | test_data_.mailbox2_, |
| 1312 | SingleReleaseCallback::Create(test_data_.release_mailbox2_)); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1313 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | 421e84f | 2013-02-22 03:27:15 | [diff] [blame] | 1314 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1315 | // Test callback after activation. |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 1316 | pending_layer->PushPropertiesTo(active_layer.get()); |
| 1317 | active_layer->DidBecomeActive(); |
[email protected] | 421e84f | 2013-02-22 03:27:15 | [diff] [blame] | 1318 | |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 1319 | EXPECT_CALL(test_data_.mock_callback_, Release(_, _, _)).Times(0); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1320 | pending_layer->SetTextureMailbox( |
| 1321 | test_data_.mailbox1_, |
| 1322 | SingleReleaseCallback::Create(test_data_.release_mailbox1_)); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1323 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | 421e84f | 2013-02-22 03:27:15 | [diff] [blame] | 1324 | |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 1325 | EXPECT_CALL(test_data_.mock_callback_, |
| 1326 | Release(test_data_.mailbox_name2_, _, false)) |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1327 | .Times(1); |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 1328 | pending_layer->PushPropertiesTo(active_layer.get()); |
| 1329 | active_layer->DidBecomeActive(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1330 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 1331 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1332 | // Test resetting the mailbox. |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 1333 | EXPECT_CALL(test_data_.mock_callback_, |
| 1334 | Release(test_data_.mailbox_name1_, _, false)) |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1335 | .Times(1); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1336 | pending_layer->SetTextureMailbox(TextureMailbox(), |
| 1337 | scoped_ptr<SingleReleaseCallback>()); |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 1338 | pending_layer->PushPropertiesTo(active_layer.get()); |
| 1339 | active_layer->DidBecomeActive(); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1340 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 1341 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1342 | // Test destructor. |
| 1343 | EXPECT_CALL(test_data_.mock_callback_, |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 1344 | Release(test_data_.mailbox_name1_, |
| 1345 | test_data_.sync_point1_, |
| 1346 | false)) |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1347 | .Times(1); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1348 | pending_layer->SetTextureMailbox( |
| 1349 | test_data_.mailbox1_, |
| 1350 | SingleReleaseCallback::Create(test_data_.release_mailbox1_)); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 1351 | } |
| 1352 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1353 | TEST_F(TextureLayerImplWithMailboxTest, |
| 1354 | TestDestructorCallbackOnCreatedResource) { |
| 1355 | scoped_ptr<TextureLayerImpl> impl_layer; |
| 1356 | impl_layer = TextureLayerImpl::Create(host_impl_.active_tree(), 1, true); |
| 1357 | ASSERT_TRUE(impl_layer); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 1358 | |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 1359 | EXPECT_CALL(test_data_.mock_callback_, |
| 1360 | Release(test_data_.mailbox_name1_, _, false)) |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1361 | .Times(1); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1362 | impl_layer->SetTextureMailbox( |
| 1363 | test_data_.mailbox1_, |
| 1364 | SingleReleaseCallback::Create(test_data_.release_mailbox1_)); |
[email protected] | afc4f26 | 2013-10-05 01:14:10 | [diff] [blame] | 1365 | impl_layer->SetDrawsContent(true); |
[email protected] | ffbb221 | 2013-06-02 23:47:59 | [diff] [blame] | 1366 | impl_layer->DidBecomeActive(); |
| 1367 | EXPECT_TRUE(impl_layer->WillDraw( |
| 1368 | DRAW_MODE_HARDWARE, host_impl_.active_tree()->resource_provider())); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1369 | impl_layer->DidDraw(host_impl_.active_tree()->resource_provider()); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1370 | impl_layer->SetTextureMailbox(TextureMailbox(), |
| 1371 | scoped_ptr<SingleReleaseCallback>()); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 1372 | } |
| 1373 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1374 | TEST_F(TextureLayerImplWithMailboxTest, TestCallbackOnInUseResource) { |
| 1375 | ResourceProvider* provider = host_impl_.active_tree()->resource_provider(); |
| 1376 | ResourceProvider::ResourceId id = |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1377 | provider->CreateResourceFromTextureMailbox( |
| 1378 | test_data_.mailbox1_, |
| 1379 | SingleReleaseCallback::Create(test_data_.release_mailbox1_)); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1380 | provider->AllocateForTesting(id); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 1381 | |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1382 | // Transfer some resources to the parent. |
| 1383 | ResourceProvider::ResourceIdArray resource_ids_to_transfer; |
| 1384 | resource_ids_to_transfer.push_back(id); |
| 1385 | TransferableResourceArray list; |
| 1386 | provider->PrepareSendToParent(resource_ids_to_transfer, &list); |
| 1387 | EXPECT_TRUE(provider->InUseByConsumer(id)); |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 1388 | EXPECT_CALL(test_data_.mock_callback_, Release(_, _, _)).Times(0); |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1389 | provider->DeleteResource(id); |
| 1390 | Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 1391 | EXPECT_CALL(test_data_.mock_callback_, |
| 1392 | Release(test_data_.mailbox_name1_, _, false)) |
[email protected] | 28571b04 | 2013-03-14 07:59:15 | [diff] [blame] | 1393 | .Times(1); |
[email protected] | e00bab02 | 2013-08-19 00:42:45 | [diff] [blame] | 1394 | ReturnedResourceArray returned; |
| 1395 | TransferableResource::ReturnResources(list, &returned); |
| 1396 | provider->ReceiveReturnsFromParent(returned); |
[email protected] | de44a15 | 2013-01-08 15:28:46 | [diff] [blame] | 1397 | } |
| 1398 | |
[email protected] | 97d519fb | 2013-03-29 02:27:54 | [diff] [blame] | 1399 | // Check that ClearClient correctly clears the state so that the impl side |
| 1400 | // doesn't try to use a texture that could have been destroyed. |
[email protected] | 7ba3ca7 | 2013-04-11 06:37:25 | [diff] [blame] | 1401 | class TextureLayerClientTest |
| 1402 | : public LayerTreeTest, |
| 1403 | public TextureLayerClient { |
[email protected] | 97d519fb | 2013-03-29 02:27:54 | [diff] [blame] | 1404 | public: |
| 1405 | TextureLayerClientTest() |
[email protected] | f5931d4 | 2013-11-06 19:44:57 | [diff] [blame] | 1406 | : texture_(0), |
[email protected] | 97d519fb | 2013-03-29 02:27:54 | [diff] [blame] | 1407 | commit_count_(0), |
| 1408 | expected_used_textures_on_draw_(0), |
| 1409 | expected_used_textures_on_commit_(0) {} |
| 1410 | |
[email protected] | ebc0e1df | 2013-08-01 02:46:22 | [diff] [blame] | 1411 | virtual scoped_ptr<OutputSurface> CreateOutputSurface(bool fallback) |
| 1412 | OVERRIDE { |
[email protected] | f5931d4 | 2013-11-06 19:44:57 | [diff] [blame] | 1413 | scoped_refptr<TestContextProvider> provider = TestContextProvider::Create(); |
| 1414 | texture_ = provider->UnboundTestContext3d()->createExternalTexture(); |
| 1415 | return FakeOutputSurface::Create3d(provider).PassAs<OutputSurface>(); |
[email protected] | 97d519fb | 2013-03-29 02:27:54 | [diff] [blame] | 1416 | } |
| 1417 | |
[email protected] | f5931d4 | 2013-11-06 19:44:57 | [diff] [blame] | 1418 | virtual unsigned PrepareTexture() OVERRIDE { return texture_; } |
[email protected] | 97d519fb | 2013-03-29 02:27:54 | [diff] [blame] | 1419 | |
[email protected] | 2541d1a | 2013-07-10 07:33:27 | [diff] [blame] | 1420 | virtual bool PrepareTextureMailbox( |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1421 | TextureMailbox* mailbox, |
| 1422 | scoped_ptr<SingleReleaseCallback>* release_callback, |
| 1423 | bool use_shared_memory) OVERRIDE { |
[email protected] | e8e4ae23 | 2013-04-12 00:26:01 | [diff] [blame] | 1424 | return false; |
| 1425 | } |
| 1426 | |
[email protected] | 97d519fb | 2013-03-29 02:27:54 | [diff] [blame] | 1427 | virtual void SetupTree() OVERRIDE { |
| 1428 | scoped_refptr<Layer> root = Layer::Create(); |
| 1429 | root->SetBounds(gfx::Size(10, 10)); |
| 1430 | root->SetAnchorPoint(gfx::PointF()); |
| 1431 | root->SetIsDrawable(true); |
| 1432 | |
| 1433 | texture_layer_ = TextureLayer::Create(this); |
| 1434 | texture_layer_->SetBounds(gfx::Size(10, 10)); |
| 1435 | texture_layer_->SetAnchorPoint(gfx::PointF()); |
| 1436 | texture_layer_->SetIsDrawable(true); |
| 1437 | root->AddChild(texture_layer_); |
| 1438 | |
| 1439 | layer_tree_host()->SetRootLayer(root); |
| 1440 | LayerTreeTest::SetupTree(); |
| 1441 | { |
| 1442 | base::AutoLock lock(lock_); |
| 1443 | expected_used_textures_on_commit_ = 1; |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | virtual void BeginTest() OVERRIDE { |
| 1448 | PostSetNeedsCommitToMainThread(); |
| 1449 | } |
| 1450 | |
| 1451 | virtual void DidCommitAndDrawFrame() OVERRIDE { |
| 1452 | ++commit_count_; |
| 1453 | switch (commit_count_) { |
| 1454 | case 1: |
| 1455 | texture_layer_->ClearClient(); |
| 1456 | texture_layer_->SetNeedsDisplay(); |
| 1457 | { |
| 1458 | base::AutoLock lock(lock_); |
| 1459 | expected_used_textures_on_commit_ = 0; |
| 1460 | } |
[email protected] | 97d519fb | 2013-03-29 02:27:54 | [diff] [blame] | 1461 | break; |
| 1462 | case 2: |
| 1463 | EndTest(); |
| 1464 | break; |
| 1465 | default: |
| 1466 | NOTREACHED(); |
| 1467 | break; |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | virtual void BeginCommitOnThread(LayerTreeHostImpl* host_impl) OVERRIDE { |
| 1472 | base::AutoLock lock(lock_); |
| 1473 | expected_used_textures_on_draw_ = expected_used_textures_on_commit_; |
| 1474 | } |
| 1475 | |
| 1476 | virtual bool PrepareToDrawOnThread(LayerTreeHostImpl* host_impl, |
| 1477 | LayerTreeHostImpl::FrameData* frame_data, |
| 1478 | bool result) OVERRIDE { |
[email protected] | f5931d4 | 2013-11-06 19:44:57 | [diff] [blame] | 1479 | ContextForImplThread(host_impl)->ResetUsedTextures(); |
[email protected] | 97d519fb | 2013-03-29 02:27:54 | [diff] [blame] | 1480 | return true; |
| 1481 | } |
| 1482 | |
| 1483 | virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, |
| 1484 | bool result) OVERRIDE { |
| 1485 | ASSERT_TRUE(result); |
[email protected] | f5931d4 | 2013-11-06 19:44:57 | [diff] [blame] | 1486 | EXPECT_EQ(expected_used_textures_on_draw_, |
| 1487 | ContextForImplThread(host_impl)->NumUsedTextures()); |
[email protected] | 97d519fb | 2013-03-29 02:27:54 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | virtual void AfterTest() OVERRIDE {} |
| 1491 | |
| 1492 | private: |
[email protected] | f5931d4 | 2013-11-06 19:44:57 | [diff] [blame] | 1493 | TestWebGraphicsContext3D* ContextForImplThread(LayerTreeHostImpl* host_impl) { |
[email protected] | 1dff796 | 2014-01-10 12:05:03 | [diff] [blame^] | 1494 | return static_cast<TestContextProvider*>( |
| 1495 | host_impl->output_surface()->context_provider().get())->TestContext3d(); |
[email protected] | f5931d4 | 2013-11-06 19:44:57 | [diff] [blame] | 1496 | } |
| 1497 | |
[email protected] | 97d519fb | 2013-03-29 02:27:54 | [diff] [blame] | 1498 | scoped_refptr<TextureLayer> texture_layer_; |
[email protected] | 97d519fb | 2013-03-29 02:27:54 | [diff] [blame] | 1499 | unsigned texture_; |
| 1500 | int commit_count_; |
| 1501 | |
| 1502 | // Used only on thread. |
| 1503 | unsigned expected_used_textures_on_draw_; |
| 1504 | |
| 1505 | // Used on either thread, protected by lock_. |
| 1506 | base::Lock lock_; |
| 1507 | unsigned expected_used_textures_on_commit_; |
| 1508 | }; |
| 1509 | |
[email protected] | 4145d17 | 2013-05-10 16:54:36 | [diff] [blame] | 1510 | // The TextureLayerClient does not use mailboxes, so can't use a delegating |
| 1511 | // renderer. |
| 1512 | SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TextureLayerClientTest); |
[email protected] | 97d519fb | 2013-03-29 02:27:54 | [diff] [blame] | 1513 | |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1514 | |
| 1515 | // Checks that changing a texture in the client for a TextureLayer that's |
| 1516 | // invisible correctly works without drawing a deleted texture. See |
| 1517 | // crbug.com/266628 |
| 1518 | class TextureLayerChangeInvisibleTest |
| 1519 | : public LayerTreeTest, |
| 1520 | public TextureLayerClient { |
| 1521 | public: |
| 1522 | TextureLayerChangeInvisibleTest() |
[email protected] | f5931d4 | 2013-11-06 19:44:57 | [diff] [blame] | 1523 | : texture_(0u), |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1524 | prepare_called_(0), |
| 1525 | commit_count_(0), |
| 1526 | expected_texture_on_draw_(0) {} |
| 1527 | |
[email protected] | f5931d4 | 2013-11-06 19:44:57 | [diff] [blame] | 1528 | virtual scoped_ptr<OutputSurface> CreateOutputSurface(bool fallback) |
| 1529 | OVERRIDE { |
| 1530 | scoped_refptr<TestContextProvider> provider = TestContextProvider::Create(); |
| 1531 | texture_ = provider->UnboundTestContext3d()->createExternalTexture(); |
| 1532 | return FakeOutputSurface::Create3d(provider).PassAs<OutputSurface>(); |
| 1533 | } |
| 1534 | |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1535 | // TextureLayerClient implementation. |
| 1536 | virtual unsigned PrepareTexture() OVERRIDE { |
| 1537 | ++prepare_called_; |
| 1538 | return texture_; |
| 1539 | } |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1540 | virtual bool PrepareTextureMailbox( |
[email protected] | e51444a | 2013-12-10 23:05:01 | [diff] [blame] | 1541 | TextureMailbox* mailbox, |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1542 | scoped_ptr<SingleReleaseCallback>* release_callback, |
| 1543 | bool use_shared_memory) OVERRIDE { |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1544 | return false; |
| 1545 | } |
| 1546 | |
| 1547 | virtual void SetupTree() OVERRIDE { |
| 1548 | scoped_refptr<Layer> root = Layer::Create(); |
| 1549 | root->SetBounds(gfx::Size(10, 10)); |
| 1550 | root->SetAnchorPoint(gfx::PointF()); |
| 1551 | root->SetIsDrawable(true); |
| 1552 | |
| 1553 | solid_layer_ = SolidColorLayer::Create(); |
| 1554 | solid_layer_->SetBounds(gfx::Size(10, 10)); |
| 1555 | solid_layer_->SetIsDrawable(true); |
| 1556 | solid_layer_->SetBackgroundColor(SK_ColorWHITE); |
| 1557 | root->AddChild(solid_layer_); |
| 1558 | |
| 1559 | parent_layer_ = Layer::Create(); |
| 1560 | parent_layer_->SetBounds(gfx::Size(10, 10)); |
| 1561 | parent_layer_->SetIsDrawable(true); |
| 1562 | root->AddChild(parent_layer_); |
| 1563 | |
| 1564 | texture_layer_ = TextureLayer::Create(this); |
| 1565 | texture_layer_->SetBounds(gfx::Size(10, 10)); |
| 1566 | texture_layer_->SetAnchorPoint(gfx::PointF()); |
| 1567 | texture_layer_->SetIsDrawable(true); |
| 1568 | parent_layer_->AddChild(texture_layer_); |
| 1569 | |
| 1570 | layer_tree_host()->SetRootLayer(root); |
| 1571 | LayerTreeTest::SetupTree(); |
| 1572 | } |
| 1573 | |
| 1574 | virtual void BeginTest() OVERRIDE { |
| 1575 | PostSetNeedsCommitToMainThread(); |
| 1576 | } |
| 1577 | |
| 1578 | virtual void DidCommitAndDrawFrame() OVERRIDE { |
| 1579 | ++commit_count_; |
| 1580 | switch (commit_count_) { |
| 1581 | case 1: |
| 1582 | // We should have updated the layer, committing the texture. |
| 1583 | EXPECT_EQ(1, prepare_called_); |
| 1584 | // Make layer invisible. |
| 1585 | parent_layer_->SetOpacity(0.f); |
| 1586 | break; |
| 1587 | case 2: { |
| 1588 | // Layer shouldn't have been updated. |
| 1589 | EXPECT_EQ(1, prepare_called_); |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1590 | texture_layer_->SetNeedsDisplay(); |
| 1591 | // Force a change to make sure we draw a frame. |
| 1592 | solid_layer_->SetBackgroundColor(SK_ColorGRAY); |
| 1593 | break; |
| 1594 | } |
| 1595 | case 3: |
| 1596 | EXPECT_EQ(1, prepare_called_); |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1597 | // Make layer visible again. |
| 1598 | parent_layer_->SetOpacity(1.f); |
| 1599 | break; |
| 1600 | case 4: { |
| 1601 | // Layer should have been updated. |
| 1602 | EXPECT_EQ(2, prepare_called_); |
| 1603 | texture_layer_->ClearClient(); |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1604 | texture_ = 0; |
| 1605 | break; |
| 1606 | } |
| 1607 | case 5: |
| 1608 | EndTest(); |
| 1609 | break; |
| 1610 | default: |
| 1611 | NOTREACHED(); |
| 1612 | break; |
| 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | virtual void BeginCommitOnThread(LayerTreeHostImpl* host_impl) OVERRIDE { |
| 1617 | ASSERT_TRUE(proxy()->IsMainThreadBlocked()); |
| 1618 | // This is the only texture that can be drawn this frame. |
| 1619 | expected_texture_on_draw_ = texture_; |
| 1620 | } |
| 1621 | |
| 1622 | virtual bool PrepareToDrawOnThread(LayerTreeHostImpl* host_impl, |
| 1623 | LayerTreeHostImpl::FrameData* frame_data, |
| 1624 | bool result) OVERRIDE { |
| 1625 | ContextForImplThread(host_impl)->ResetUsedTextures(); |
| 1626 | return true; |
| 1627 | } |
| 1628 | |
| 1629 | virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, |
| 1630 | bool result) OVERRIDE { |
| 1631 | ASSERT_TRUE(result); |
| 1632 | TestWebGraphicsContext3D* context = ContextForImplThread(host_impl); |
| 1633 | int used_textures = context->NumUsedTextures(); |
| 1634 | switch (host_impl->active_tree()->source_frame_number()) { |
| 1635 | case 0: |
| 1636 | EXPECT_EQ(1, used_textures); |
| 1637 | EXPECT_TRUE(context->UsedTexture(expected_texture_on_draw_)); |
| 1638 | break; |
| 1639 | case 1: |
| 1640 | case 2: |
| 1641 | EXPECT_EQ(0, used_textures); |
| 1642 | break; |
| 1643 | case 3: |
| 1644 | EXPECT_EQ(1, used_textures); |
| 1645 | EXPECT_TRUE(context->UsedTexture(expected_texture_on_draw_)); |
| 1646 | break; |
| 1647 | default: |
| 1648 | break; |
| 1649 | } |
| 1650 | } |
| 1651 | |
| 1652 | virtual void AfterTest() OVERRIDE {} |
| 1653 | |
| 1654 | private: |
| 1655 | TestWebGraphicsContext3D* ContextForImplThread(LayerTreeHostImpl* host_impl) { |
[email protected] | 1dff796 | 2014-01-10 12:05:03 | [diff] [blame^] | 1656 | return static_cast<TestContextProvider*>( |
| 1657 | host_impl->output_surface()->context_provider().get())->TestContext3d(); |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | scoped_refptr<SolidColorLayer> solid_layer_; |
| 1661 | scoped_refptr<Layer> parent_layer_; |
| 1662 | scoped_refptr<TextureLayer> texture_layer_; |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1663 | |
| 1664 | // Used on the main thread, and on the impl thread while the main thread is |
| 1665 | // blocked. |
| 1666 | unsigned texture_; |
| 1667 | |
| 1668 | // Used on the main thread. |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1669 | int prepare_called_; |
| 1670 | int commit_count_; |
| 1671 | |
| 1672 | // Used on the compositor thread. |
| 1673 | unsigned expected_texture_on_draw_; |
| 1674 | }; |
| 1675 | |
| 1676 | // The TextureLayerChangeInvisibleTest does not use mailboxes, so can't use a |
| 1677 | // delegating renderer. |
| 1678 | SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TextureLayerChangeInvisibleTest); |
| 1679 | |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1680 | // Checks that TextureLayer::Update does not cause an extra commit when setting |
| 1681 | // the texture mailbox. |
| 1682 | class TextureLayerNoExtraCommitForMailboxTest |
| 1683 | : public LayerTreeTest, |
| 1684 | public TextureLayerClient { |
| 1685 | public: |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1686 | // TextureLayerClient implementation. |
| 1687 | virtual unsigned PrepareTexture() OVERRIDE { |
| 1688 | NOTREACHED(); |
| 1689 | return 0; |
| 1690 | } |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1691 | virtual bool PrepareTextureMailbox( |
[email protected] | cce34bd | 2013-12-02 23:24:45 | [diff] [blame] | 1692 | TextureMailbox* mailbox, |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1693 | scoped_ptr<SingleReleaseCallback>* release_callback, |
| 1694 | bool use_shared_memory) OVERRIDE { |
[email protected] | cce34bd | 2013-12-02 23:24:45 | [diff] [blame] | 1695 | if (layer_tree_host()->source_frame_number() == 1) { |
| 1696 | *mailbox = TextureMailbox(); |
| 1697 | return true; |
| 1698 | } |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1699 | |
[email protected] | cce34bd | 2013-12-02 23:24:45 | [diff] [blame] | 1700 | *mailbox = TextureMailbox(std::string(64, '1')); |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1701 | *release_callback = SingleReleaseCallback::Create( |
| 1702 | base::Bind(&TextureLayerNoExtraCommitForMailboxTest::MailboxReleased, |
| 1703 | base::Unretained(this))); |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1704 | return true; |
| 1705 | } |
| 1706 | |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1707 | void MailboxReleased(unsigned sync_point, bool lost_resource) { |
[email protected] | cce34bd | 2013-12-02 23:24:45 | [diff] [blame] | 1708 | EXPECT_EQ(2, layer_tree_host()->source_frame_number()); |
| 1709 | EndTest(); |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | virtual void SetupTree() OVERRIDE { |
| 1713 | scoped_refptr<Layer> root = Layer::Create(); |
| 1714 | root->SetBounds(gfx::Size(10, 10)); |
| 1715 | root->SetAnchorPoint(gfx::PointF()); |
| 1716 | root->SetIsDrawable(true); |
| 1717 | |
| 1718 | solid_layer_ = SolidColorLayer::Create(); |
| 1719 | solid_layer_->SetBounds(gfx::Size(10, 10)); |
| 1720 | solid_layer_->SetIsDrawable(true); |
| 1721 | solid_layer_->SetBackgroundColor(SK_ColorWHITE); |
| 1722 | root->AddChild(solid_layer_); |
| 1723 | |
| 1724 | parent_layer_ = Layer::Create(); |
| 1725 | parent_layer_->SetBounds(gfx::Size(10, 10)); |
| 1726 | parent_layer_->SetIsDrawable(true); |
| 1727 | root->AddChild(parent_layer_); |
| 1728 | |
| 1729 | texture_layer_ = TextureLayer::CreateForMailbox(this); |
| 1730 | texture_layer_->SetBounds(gfx::Size(10, 10)); |
| 1731 | texture_layer_->SetAnchorPoint(gfx::PointF()); |
| 1732 | texture_layer_->SetIsDrawable(true); |
| 1733 | parent_layer_->AddChild(texture_layer_); |
| 1734 | |
| 1735 | layer_tree_host()->SetRootLayer(root); |
| 1736 | LayerTreeTest::SetupTree(); |
| 1737 | } |
| 1738 | |
| 1739 | virtual void BeginTest() OVERRIDE { |
| 1740 | PostSetNeedsCommitToMainThread(); |
| 1741 | } |
| 1742 | |
[email protected] | cce34bd | 2013-12-02 23:24:45 | [diff] [blame] | 1743 | virtual void DidCommitAndDrawFrame() OVERRIDE { |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1744 | switch (layer_tree_host()->source_frame_number()) { |
| 1745 | case 1: |
[email protected] | cce34bd | 2013-12-02 23:24:45 | [diff] [blame] | 1746 | EXPECT_FALSE(proxy()->CommitPendingForTesting()); |
| 1747 | // Invalidate the texture layer to clear the mailbox before |
| 1748 | // ending the test. |
| 1749 | texture_layer_->SetNeedsDisplay(); |
| 1750 | break; |
| 1751 | case 2: |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1752 | break; |
| 1753 | default: |
| 1754 | NOTREACHED(); |
| 1755 | break; |
| 1756 | } |
| 1757 | } |
| 1758 | |
[email protected] | cce34bd | 2013-12-02 23:24:45 | [diff] [blame] | 1759 | virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, |
| 1760 | bool result) OVERRIDE { |
| 1761 | ASSERT_TRUE(result); |
| 1762 | DelegatedFrameData* delegated_frame_data = |
| 1763 | output_surface()->last_sent_frame().delegated_frame_data.get(); |
| 1764 | if (!delegated_frame_data) |
| 1765 | return; |
| 1766 | |
| 1767 | // Return all resources immediately. |
| 1768 | TransferableResourceArray resources_to_return = |
| 1769 | output_surface()->resources_held_by_parent(); |
| 1770 | |
| 1771 | CompositorFrameAck ack; |
| 1772 | for (size_t i = 0; i < resources_to_return.size(); ++i) |
| 1773 | output_surface()->ReturnResource(resources_to_return[i].id, &ack); |
| 1774 | host_impl->ReclaimResources(&ack); |
| 1775 | host_impl->OnSwapBuffersComplete(); |
| 1776 | } |
| 1777 | |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1778 | virtual void AfterTest() OVERRIDE {} |
| 1779 | |
| 1780 | private: |
| 1781 | scoped_refptr<SolidColorLayer> solid_layer_; |
| 1782 | scoped_refptr<Layer> parent_layer_; |
| 1783 | scoped_refptr<TextureLayer> texture_layer_; |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1784 | }; |
| 1785 | |
[email protected] | cce34bd | 2013-12-02 23:24:45 | [diff] [blame] | 1786 | SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerNoExtraCommitForMailboxTest); |
[email protected] | 4bad8b6 | 2013-10-24 01:27:29 | [diff] [blame] | 1787 | |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1788 | // Checks that changing a mailbox in the client for a TextureLayer that's |
| 1789 | // invisible correctly works and uses the new mailbox as soon as the layer |
| 1790 | // becomes visible (and returns the old one). |
| 1791 | class TextureLayerChangeInvisibleMailboxTest |
| 1792 | : public LayerTreeTest, |
| 1793 | public TextureLayerClient { |
| 1794 | public: |
| 1795 | TextureLayerChangeInvisibleMailboxTest() |
| 1796 | : mailbox_changed_(true), |
| 1797 | mailbox_returned_(0), |
| 1798 | prepare_called_(0), |
| 1799 | commit_count_(0) { |
| 1800 | mailbox_ = MakeMailbox('1'); |
| 1801 | } |
| 1802 | |
| 1803 | // TextureLayerClient implementation. |
| 1804 | virtual unsigned PrepareTexture() OVERRIDE { |
| 1805 | NOTREACHED(); |
| 1806 | return 0; |
| 1807 | } |
| 1808 | |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1809 | virtual bool PrepareTextureMailbox( |
[email protected] | e51444a | 2013-12-10 23:05:01 | [diff] [blame] | 1810 | TextureMailbox* mailbox, |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1811 | scoped_ptr<SingleReleaseCallback>* release_callback, |
| 1812 | bool use_shared_memory) OVERRIDE { |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1813 | ++prepare_called_; |
| 1814 | if (!mailbox_changed_) |
| 1815 | return false; |
| 1816 | *mailbox = mailbox_; |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1817 | *release_callback = SingleReleaseCallback::Create( |
| 1818 | base::Bind(&TextureLayerChangeInvisibleMailboxTest::MailboxReleased, |
| 1819 | base::Unretained(this))); |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1820 | return true; |
| 1821 | } |
| 1822 | |
| 1823 | TextureMailbox MakeMailbox(char name) { |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1824 | return TextureMailbox(std::string(64, name)); |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | void MailboxReleased(unsigned sync_point, bool lost_resource) { |
| 1828 | ++mailbox_returned_; |
| 1829 | } |
| 1830 | |
| 1831 | virtual void SetupTree() OVERRIDE { |
| 1832 | scoped_refptr<Layer> root = Layer::Create(); |
| 1833 | root->SetBounds(gfx::Size(10, 10)); |
| 1834 | root->SetAnchorPoint(gfx::PointF()); |
| 1835 | root->SetIsDrawable(true); |
| 1836 | |
| 1837 | solid_layer_ = SolidColorLayer::Create(); |
| 1838 | solid_layer_->SetBounds(gfx::Size(10, 10)); |
| 1839 | solid_layer_->SetIsDrawable(true); |
| 1840 | solid_layer_->SetBackgroundColor(SK_ColorWHITE); |
| 1841 | root->AddChild(solid_layer_); |
| 1842 | |
| 1843 | parent_layer_ = Layer::Create(); |
| 1844 | parent_layer_->SetBounds(gfx::Size(10, 10)); |
| 1845 | parent_layer_->SetIsDrawable(true); |
| 1846 | root->AddChild(parent_layer_); |
| 1847 | |
| 1848 | texture_layer_ = TextureLayer::CreateForMailbox(this); |
| 1849 | texture_layer_->SetBounds(gfx::Size(10, 10)); |
| 1850 | texture_layer_->SetAnchorPoint(gfx::PointF()); |
| 1851 | texture_layer_->SetIsDrawable(true); |
| 1852 | parent_layer_->AddChild(texture_layer_); |
| 1853 | |
| 1854 | layer_tree_host()->SetRootLayer(root); |
| 1855 | LayerTreeTest::SetupTree(); |
| 1856 | } |
| 1857 | |
| 1858 | virtual void BeginTest() OVERRIDE { |
| 1859 | PostSetNeedsCommitToMainThread(); |
| 1860 | } |
| 1861 | |
| 1862 | virtual void DidCommitAndDrawFrame() OVERRIDE { |
| 1863 | ++commit_count_; |
| 1864 | switch (commit_count_) { |
| 1865 | case 1: |
| 1866 | // We should have updated the layer, committing the texture. |
| 1867 | EXPECT_EQ(1, prepare_called_); |
| 1868 | // Make layer invisible. |
| 1869 | parent_layer_->SetOpacity(0.f); |
| 1870 | break; |
| 1871 | case 2: |
| 1872 | // Layer shouldn't have been updated. |
| 1873 | EXPECT_EQ(1, prepare_called_); |
| 1874 | // Change the texture. |
| 1875 | mailbox_ = MakeMailbox('2'); |
| 1876 | mailbox_changed_ = true; |
| 1877 | texture_layer_->SetNeedsDisplay(); |
| 1878 | // Force a change to make sure we draw a frame. |
| 1879 | solid_layer_->SetBackgroundColor(SK_ColorGRAY); |
| 1880 | break; |
| 1881 | case 3: |
| 1882 | // Layer shouldn't have been updated. |
| 1883 | EXPECT_EQ(1, prepare_called_); |
| 1884 | // So the old mailbox isn't returned yet. |
| 1885 | EXPECT_EQ(0, mailbox_returned_); |
| 1886 | // Make layer visible again. |
| 1887 | parent_layer_->SetOpacity(1.f); |
| 1888 | break; |
| 1889 | case 4: |
| 1890 | // Layer should have been updated. |
| 1891 | EXPECT_EQ(2, prepare_called_); |
| 1892 | // So the old mailbox should have been returned already. |
| 1893 | EXPECT_EQ(1, mailbox_returned_); |
| 1894 | texture_layer_->ClearClient(); |
| 1895 | break; |
| 1896 | case 5: |
| 1897 | EXPECT_EQ(2, mailbox_returned_); |
| 1898 | EndTest(); |
| 1899 | break; |
| 1900 | default: |
| 1901 | NOTREACHED(); |
| 1902 | break; |
| 1903 | } |
| 1904 | } |
| 1905 | |
| 1906 | virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, |
| 1907 | bool result) OVERRIDE { |
| 1908 | ASSERT_TRUE(result); |
| 1909 | DelegatedFrameData* delegated_frame_data = |
| 1910 | output_surface()->last_sent_frame().delegated_frame_data.get(); |
| 1911 | if (!delegated_frame_data) |
| 1912 | return; |
| 1913 | |
| 1914 | // Return all resources immediately. |
| 1915 | TransferableResourceArray resources_to_return = |
| 1916 | output_surface()->resources_held_by_parent(); |
| 1917 | |
| 1918 | CompositorFrameAck ack; |
| 1919 | for (size_t i = 0; i < resources_to_return.size(); ++i) |
| 1920 | output_surface()->ReturnResource(resources_to_return[i].id, &ack); |
[email protected] | a7335e0b | 2013-09-18 09:34:51 | [diff] [blame] | 1921 | host_impl->ReclaimResources(&ack); |
| 1922 | host_impl->OnSwapBuffersComplete(); |
[email protected] | b04264f9 | 2013-09-13 23:37:29 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | virtual void AfterTest() OVERRIDE {} |
| 1926 | |
| 1927 | private: |
| 1928 | scoped_refptr<SolidColorLayer> solid_layer_; |
| 1929 | scoped_refptr<Layer> parent_layer_; |
| 1930 | scoped_refptr<TextureLayer> texture_layer_; |
| 1931 | |
| 1932 | // Used on the main thread. |
| 1933 | bool mailbox_changed_; |
| 1934 | TextureMailbox mailbox_; |
| 1935 | int mailbox_returned_; |
| 1936 | int prepare_called_; |
| 1937 | int commit_count_; |
| 1938 | }; |
| 1939 | |
| 1940 | SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerChangeInvisibleMailboxTest); |
| 1941 | |
[email protected] | 0bf5a20 | 2013-07-10 14:50:54 | [diff] [blame] | 1942 | // Test recovering from a lost context. |
| 1943 | class TextureLayerLostContextTest |
| 1944 | : public LayerTreeTest, |
| 1945 | public TextureLayerClient { |
| 1946 | public: |
| 1947 | TextureLayerLostContextTest() |
[email protected] | f5931d4 | 2013-11-06 19:44:57 | [diff] [blame] | 1948 | : context_lost_(false), |
[email protected] | 0bf5a20 | 2013-07-10 14:50:54 | [diff] [blame] | 1949 | draw_count_(0) {} |
| 1950 | |
[email protected] | ebc0e1df | 2013-08-01 02:46:22 | [diff] [blame] | 1951 | virtual scoped_ptr<OutputSurface> CreateOutputSurface(bool fallback) |
| 1952 | OVERRIDE { |
[email protected] | 3f6a906c | 2013-07-17 01:03:26 | [diff] [blame] | 1953 | return CreateFakeOutputSurface(); |
[email protected] | 0bf5a20 | 2013-07-10 14:50:54 | [diff] [blame] | 1954 | } |
| 1955 | |
[email protected] | 171cbb3 | 2013-07-11 03:51:19 | [diff] [blame] | 1956 | virtual unsigned PrepareTexture() OVERRIDE { |
[email protected] | f5931d4 | 2013-11-06 19:44:57 | [diff] [blame] | 1957 | if (draw_count_ == 0) |
| 1958 | context_lost_ = true; |
| 1959 | if (context_lost_) |
| 1960 | return 0u; |
| 1961 | return 1u; |
[email protected] | 0bf5a20 | 2013-07-10 14:50:54 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | virtual bool PrepareTextureMailbox( |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 1965 | TextureMailbox* mailbox, |
| 1966 | scoped_ptr<SingleReleaseCallback>* release_callback, |
| 1967 | bool use_shared_memory) OVERRIDE { |
[email protected] | 0bf5a20 | 2013-07-10 14:50:54 | [diff] [blame] | 1968 | return false; |
| 1969 | } |
| 1970 | |
| 1971 | virtual void SetupTree() OVERRIDE { |
| 1972 | scoped_refptr<Layer> root = Layer::Create(); |
| 1973 | root->SetBounds(gfx::Size(10, 10)); |
| 1974 | root->SetIsDrawable(true); |
| 1975 | |
| 1976 | texture_layer_ = TextureLayer::Create(this); |
| 1977 | texture_layer_->SetBounds(gfx::Size(10, 10)); |
| 1978 | texture_layer_->SetIsDrawable(true); |
| 1979 | root->AddChild(texture_layer_); |
| 1980 | |
| 1981 | layer_tree_host()->SetRootLayer(root); |
| 1982 | LayerTreeTest::SetupTree(); |
| 1983 | } |
| 1984 | |
| 1985 | virtual void BeginTest() OVERRIDE { |
| 1986 | PostSetNeedsCommitToMainThread(); |
| 1987 | } |
| 1988 | |
| 1989 | virtual bool PrepareToDrawOnThread(LayerTreeHostImpl* host_impl, |
| 1990 | LayerTreeHostImpl::FrameData* frame_data, |
| 1991 | bool result) OVERRIDE { |
| 1992 | LayerImpl* root = host_impl->RootLayer(); |
| 1993 | TextureLayerImpl* texture_layer = |
| 1994 | static_cast<TextureLayerImpl*>(root->children()[0]); |
| 1995 | if (++draw_count_ == 1) |
| 1996 | EXPECT_EQ(0u, texture_layer->texture_id()); |
| 1997 | else |
[email protected] | f5931d4 | 2013-11-06 19:44:57 | [diff] [blame] | 1998 | EXPECT_EQ(1u, texture_layer->texture_id()); |
[email protected] | 0bf5a20 | 2013-07-10 14:50:54 | [diff] [blame] | 1999 | return true; |
| 2000 | } |
| 2001 | |
| 2002 | virtual void DidCommitAndDrawFrame() OVERRIDE { |
| 2003 | EndTest(); |
| 2004 | } |
| 2005 | |
| 2006 | virtual void AfterTest() OVERRIDE {} |
| 2007 | |
| 2008 | private: |
| 2009 | scoped_refptr<TextureLayer> texture_layer_; |
[email protected] | f5931d4 | 2013-11-06 19:44:57 | [diff] [blame] | 2010 | bool context_lost_; |
[email protected] | 0bf5a20 | 2013-07-10 14:50:54 | [diff] [blame] | 2011 | int draw_count_; |
| 2012 | }; |
| 2013 | |
| 2014 | SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TextureLayerLostContextTest); |
| 2015 | |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 2016 | class TextureLayerWithMailboxMainThreadDeleted : public LayerTreeTest { |
| 2017 | public: |
| 2018 | void ReleaseCallback(unsigned sync_point, bool lost_resource) { |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 2019 | EXPECT_EQ(true, main_thread_.CalledOnValidThread()); |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 2020 | EXPECT_FALSE(lost_resource); |
| 2021 | ++callback_count_; |
| 2022 | EndTest(); |
| 2023 | } |
| 2024 | |
| 2025 | void SetMailbox(char mailbox_char) { |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 2026 | EXPECT_EQ(true, main_thread_.CalledOnValidThread()); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 2027 | TextureMailbox mailbox(std::string(64, mailbox_char)); |
| 2028 | scoped_ptr<SingleReleaseCallback> callback = SingleReleaseCallback::Create( |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 2029 | base::Bind( |
| 2030 | &TextureLayerWithMailboxMainThreadDeleted::ReleaseCallback, |
| 2031 | base::Unretained(this))); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 2032 | layer_->SetTextureMailbox(mailbox, callback.Pass()); |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | virtual void SetupTree() OVERRIDE { |
| 2036 | gfx::Size bounds(100, 100); |
| 2037 | root_ = Layer::Create(); |
| 2038 | root_->SetAnchorPoint(gfx::PointF()); |
| 2039 | root_->SetBounds(bounds); |
| 2040 | |
| 2041 | layer_ = TextureLayer::CreateForMailbox(NULL); |
| 2042 | layer_->SetIsDrawable(true); |
| 2043 | layer_->SetAnchorPoint(gfx::PointF()); |
| 2044 | layer_->SetBounds(bounds); |
| 2045 | |
| 2046 | root_->AddChild(layer_); |
| 2047 | layer_tree_host()->SetRootLayer(root_); |
| 2048 | layer_tree_host()->SetViewportSize(bounds); |
| 2049 | } |
| 2050 | |
| 2051 | virtual void BeginTest() OVERRIDE { |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 2052 | EXPECT_EQ(true, main_thread_.CalledOnValidThread()); |
| 2053 | |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 2054 | callback_count_ = 0; |
| 2055 | |
| 2056 | // Set the mailbox on the main thread. |
| 2057 | SetMailbox('1'); |
| 2058 | EXPECT_EQ(0, callback_count_); |
| 2059 | |
| 2060 | PostSetNeedsCommitToMainThread(); |
| 2061 | } |
| 2062 | |
| 2063 | virtual void DidCommitAndDrawFrame() OVERRIDE { |
| 2064 | switch (layer_tree_host()->source_frame_number()) { |
| 2065 | case 1: |
| 2066 | // Delete the TextureLayer on the main thread while the mailbox is in |
| 2067 | // the impl tree. |
| 2068 | layer_->RemoveFromParent(); |
| 2069 | layer_ = NULL; |
| 2070 | break; |
| 2071 | } |
| 2072 | } |
| 2073 | |
| 2074 | virtual void AfterTest() OVERRIDE { |
| 2075 | EXPECT_EQ(1, callback_count_); |
| 2076 | } |
| 2077 | |
| 2078 | private: |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 2079 | base::ThreadChecker main_thread_; |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 2080 | int callback_count_; |
| 2081 | scoped_refptr<Layer> root_; |
| 2082 | scoped_refptr<TextureLayer> layer_; |
| 2083 | }; |
| 2084 | |
| 2085 | SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F( |
| 2086 | TextureLayerWithMailboxMainThreadDeleted); |
| 2087 | |
| 2088 | class TextureLayerWithMailboxImplThreadDeleted : public LayerTreeTest { |
| 2089 | public: |
| 2090 | void ReleaseCallback(unsigned sync_point, bool lost_resource) { |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 2091 | EXPECT_EQ(true, main_thread_.CalledOnValidThread()); |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 2092 | EXPECT_FALSE(lost_resource); |
| 2093 | ++callback_count_; |
| 2094 | EndTest(); |
| 2095 | } |
| 2096 | |
| 2097 | void SetMailbox(char mailbox_char) { |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 2098 | EXPECT_EQ(true, main_thread_.CalledOnValidThread()); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 2099 | TextureMailbox mailbox(std::string(64, mailbox_char)); |
| 2100 | scoped_ptr<SingleReleaseCallback> callback = SingleReleaseCallback::Create( |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 2101 | base::Bind( |
| 2102 | &TextureLayerWithMailboxImplThreadDeleted::ReleaseCallback, |
| 2103 | base::Unretained(this))); |
[email protected] | 9260757f | 2013-09-17 01:24:16 | [diff] [blame] | 2104 | layer_->SetTextureMailbox(mailbox, callback.Pass()); |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 2105 | } |
| 2106 | |
| 2107 | virtual void SetupTree() OVERRIDE { |
| 2108 | gfx::Size bounds(100, 100); |
| 2109 | root_ = Layer::Create(); |
| 2110 | root_->SetAnchorPoint(gfx::PointF()); |
| 2111 | root_->SetBounds(bounds); |
| 2112 | |
| 2113 | layer_ = TextureLayer::CreateForMailbox(NULL); |
| 2114 | layer_->SetIsDrawable(true); |
| 2115 | layer_->SetAnchorPoint(gfx::PointF()); |
| 2116 | layer_->SetBounds(bounds); |
| 2117 | |
| 2118 | root_->AddChild(layer_); |
| 2119 | layer_tree_host()->SetRootLayer(root_); |
| 2120 | layer_tree_host()->SetViewportSize(bounds); |
| 2121 | } |
| 2122 | |
| 2123 | virtual void BeginTest() OVERRIDE { |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 2124 | EXPECT_EQ(true, main_thread_.CalledOnValidThread()); |
| 2125 | |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 2126 | callback_count_ = 0; |
| 2127 | |
| 2128 | // Set the mailbox on the main thread. |
| 2129 | SetMailbox('1'); |
| 2130 | EXPECT_EQ(0, callback_count_); |
| 2131 | |
| 2132 | PostSetNeedsCommitToMainThread(); |
| 2133 | } |
| 2134 | |
| 2135 | virtual void DidCommitAndDrawFrame() OVERRIDE { |
| 2136 | switch (layer_tree_host()->source_frame_number()) { |
| 2137 | case 1: |
| 2138 | // Remove the TextureLayer on the main thread while the mailbox is in |
| 2139 | // the impl tree, but don't delete the TextureLayer until after the impl |
| 2140 | // tree side is deleted. |
| 2141 | layer_->RemoveFromParent(); |
| 2142 | break; |
| 2143 | case 2: |
| 2144 | layer_ = NULL; |
| 2145 | break; |
| 2146 | } |
| 2147 | } |
| 2148 | |
| 2149 | virtual void AfterTest() OVERRIDE { |
| 2150 | EXPECT_EQ(1, callback_count_); |
| 2151 | } |
| 2152 | |
| 2153 | private: |
[email protected] | 9794fb3 | 2013-08-29 09:49:59 | [diff] [blame] | 2154 | base::ThreadChecker main_thread_; |
[email protected] | 9c2bd82 | 2013-07-26 12:30:17 | [diff] [blame] | 2155 | int callback_count_; |
| 2156 | scoped_refptr<Layer> root_; |
| 2157 | scoped_refptr<TextureLayer> layer_; |
| 2158 | }; |
| 2159 | |
| 2160 | SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F( |
| 2161 | TextureLayerWithMailboxImplThreadDeleted); |
| 2162 | |
[email protected] | ba56574 | 2012-11-10 09:29:48 | [diff] [blame] | 2163 | } // namespace |
| 2164 | } // namespace cc |