OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/output/output_surface.h" | 5 #include "cc/output/output_surface.h" |
6 #include "cc/output/output_surface_client.h" | 6 #include "cc/output/output_surface_client.h" |
7 #include "cc/test/test_web_graphics_context_3d.h" | 7 #include "cc/test/test_web_graphics_context_3d.h" |
| 8 #include "gpu/GLES2/gl2extchromium.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
9 | 10 |
10 namespace cc { | 11 namespace cc { |
11 namespace { | 12 namespace { |
12 | 13 |
13 class TestOutputSurface : public OutputSurface { | 14 class TestOutputSurface : public OutputSurface { |
14 public: | 15 public: |
15 explicit TestOutputSurface(scoped_ptr<WebKit::WebGraphicsContext3D> context3d) | 16 explicit TestOutputSurface(scoped_ptr<WebKit::WebGraphicsContext3D> context3d) |
16 : OutputSurface(context3d.Pass()) {} | 17 : OutputSurface(context3d.Pass()) {} |
17 | 18 |
18 explicit TestOutputSurface( | 19 explicit TestOutputSurface( |
19 scoped_ptr<cc::SoftwareOutputDevice> software_device) | 20 scoped_ptr<cc::SoftwareOutputDevice> software_device) |
20 : OutputSurface(software_device.Pass()) {} | 21 : OutputSurface(software_device.Pass()) {} |
21 | 22 |
22 TestOutputSurface(scoped_ptr<WebKit::WebGraphicsContext3D> context3d, | 23 TestOutputSurface(scoped_ptr<WebKit::WebGraphicsContext3D> context3d, |
23 scoped_ptr<cc::SoftwareOutputDevice> software_device) | 24 scoped_ptr<cc::SoftwareOutputDevice> software_device) |
24 : OutputSurface(context3d.Pass(), software_device.Pass()) {} | 25 : OutputSurface(context3d.Pass(), software_device.Pass()) {} |
25 | 26 |
26 OutputSurfaceClient* client() { return client_; } | 27 OutputSurfaceClient* client() { return client_; } |
| 28 |
| 29 bool InitializeNewContext3D( |
| 30 scoped_ptr<WebKit::WebGraphicsContext3D> new_context3d) { |
| 31 return InitializeAndSetContext3D(new_context3d.Pass(), |
| 32 scoped_refptr<ContextProvider>()); |
| 33 } |
27 }; | 34 }; |
28 | 35 |
29 class FakeOutputSurfaceClient : public OutputSurfaceClient { | 36 class FakeOutputSurfaceClient : public OutputSurfaceClient { |
30 public: | 37 public: |
| 38 FakeOutputSurfaceClient() |
| 39 : deferred_initialize_result_(true), |
| 40 deferred_initialize_called_(false), |
| 41 did_lose_output_surface_called_(false) {} |
| 42 |
31 virtual bool DeferredInitialize( | 43 virtual bool DeferredInitialize( |
32 scoped_refptr<ContextProvider> offscreen_context_provider) OVERRIDE { | 44 scoped_refptr<ContextProvider> offscreen_context_provider) OVERRIDE { |
33 return true; | 45 deferred_initialize_called_ = true; |
| 46 return deferred_initialize_result_; |
34 } | 47 } |
35 virtual void SetNeedsRedrawRect(gfx::Rect damage_rect) OVERRIDE {} | 48 virtual void SetNeedsRedrawRect(gfx::Rect damage_rect) OVERRIDE {} |
36 virtual void OnVSyncParametersChanged(base::TimeTicks timebase, | 49 virtual void OnVSyncParametersChanged(base::TimeTicks timebase, |
37 base::TimeDelta interval) OVERRIDE {} | 50 base::TimeDelta interval) OVERRIDE {} |
38 virtual void BeginFrame(base::TimeTicks frame_time) OVERRIDE {} | 51 virtual void BeginFrame(base::TimeTicks frame_time) OVERRIDE {} |
39 virtual void OnSendFrameToParentCompositorAck(const CompositorFrameAck& ack) | 52 virtual void OnSendFrameToParentCompositorAck(const CompositorFrameAck& ack) |
40 OVERRIDE {} | 53 OVERRIDE {} |
41 virtual void OnSwapBuffersComplete() OVERRIDE {} | 54 virtual void OnSwapBuffersComplete() OVERRIDE {} |
42 virtual void DidLoseOutputSurface() OVERRIDE {} | 55 virtual void DidLoseOutputSurface() OVERRIDE { |
| 56 did_lose_output_surface_called_ = true; |
| 57 } |
43 virtual void SetExternalDrawConstraints(const gfx::Transform& transform, | 58 virtual void SetExternalDrawConstraints(const gfx::Transform& transform, |
44 gfx::Rect viewport) OVERRIDE {} | 59 gfx::Rect viewport) OVERRIDE {} |
| 60 |
| 61 void set_deferred_initialize_result(bool result) { |
| 62 deferred_initialize_result_ = result; |
| 63 } |
| 64 |
| 65 bool deferred_initialize_called() { |
| 66 return deferred_initialize_called_; |
| 67 } |
| 68 |
| 69 bool did_lose_output_surface_called() { |
| 70 return did_lose_output_surface_called_; |
| 71 } |
| 72 |
| 73 private: |
| 74 bool deferred_initialize_result_; |
| 75 bool deferred_initialize_called_; |
| 76 bool did_lose_output_surface_called_; |
45 }; | 77 }; |
46 | 78 |
47 TEST(OutputSurfaceTest, ClientPointerIndicatesBindToClientSuccess) { | 79 TEST(OutputSurfaceTest, ClientPointerIndicatesBindToClientSuccess) { |
48 scoped_ptr<TestWebGraphicsContext3D> context3d = | 80 scoped_ptr<TestWebGraphicsContext3D> context3d = |
49 TestWebGraphicsContext3D::Create(); | 81 TestWebGraphicsContext3D::Create(); |
50 | 82 |
51 TestOutputSurface output_surface( | 83 TestOutputSurface output_surface( |
52 context3d.PassAs<WebKit::WebGraphicsContext3D>()); | 84 context3d.PassAs<WebKit::WebGraphicsContext3D>()); |
53 EXPECT_EQ(NULL, output_surface.client()); | 85 EXPECT_EQ(NULL, output_surface.client()); |
54 | 86 |
55 FakeOutputSurfaceClient client; | 87 FakeOutputSurfaceClient client; |
56 EXPECT_TRUE(output_surface.BindToClient(&client)); | 88 EXPECT_TRUE(output_surface.BindToClient(&client)); |
57 EXPECT_EQ(&client, output_surface.client()); | 89 EXPECT_EQ(&client, output_surface.client()); |
| 90 EXPECT_FALSE(client.deferred_initialize_called()); |
| 91 |
| 92 // Verify DidLoseOutputSurface callback is hooked up correctly. |
| 93 EXPECT_FALSE(client.did_lose_output_surface_called()); |
| 94 output_surface.context3d()->loseContextCHROMIUM( |
| 95 GL_GUILTY_CONTEXT_RESET_ARB, GL_INNOCENT_CONTEXT_RESET_ARB); |
| 96 EXPECT_TRUE(client.did_lose_output_surface_called()); |
58 } | 97 } |
59 | 98 |
60 TEST(OutputSurfaceTest, ClientPointerIndicatesBindToClientFailure) { | 99 TEST(OutputSurfaceTest, ClientPointerIndicatesBindToClientFailure) { |
61 scoped_ptr<TestWebGraphicsContext3D> context3d = | 100 scoped_ptr<TestWebGraphicsContext3D> context3d = |
62 TestWebGraphicsContext3D::Create(); | 101 TestWebGraphicsContext3D::Create(); |
63 | 102 |
64 // Lose the context so BindToClient fails. | 103 // Lose the context so BindToClient fails. |
65 context3d->set_times_make_current_succeeds(0); | 104 context3d->set_times_make_current_succeeds(0); |
66 | 105 |
67 TestOutputSurface output_surface( | 106 TestOutputSurface output_surface( |
68 context3d.PassAs<WebKit::WebGraphicsContext3D>()); | 107 context3d.PassAs<WebKit::WebGraphicsContext3D>()); |
69 EXPECT_EQ(NULL, output_surface.client()); | 108 EXPECT_EQ(NULL, output_surface.client()); |
70 | 109 |
71 FakeOutputSurfaceClient client; | 110 FakeOutputSurfaceClient client; |
72 EXPECT_FALSE(output_surface.BindToClient(&client)); | 111 EXPECT_FALSE(output_surface.BindToClient(&client)); |
73 EXPECT_EQ(NULL, output_surface.client()); | 112 EXPECT_EQ(NULL, output_surface.client()); |
74 } | 113 } |
75 | 114 |
| 115 class InitializeNewContext3D : public ::testing::Test { |
| 116 public: |
| 117 InitializeNewContext3D() |
| 118 : second_context3d_(TestWebGraphicsContext3D::Create()), |
| 119 output_surface_(TestWebGraphicsContext3D::Create() |
| 120 .PassAs<WebKit::WebGraphicsContext3D>()) {} |
| 121 |
| 122 protected: |
| 123 void BindOutputSurface() { |
| 124 first_context3d_ptr_ = |
| 125 static_cast<TestWebGraphicsContext3D*>(output_surface_.context3d()); |
| 126 EXPECT_TRUE(output_surface_.BindToClient(&client_)); |
| 127 EXPECT_EQ(&client_, output_surface_.client()); |
| 128 } |
| 129 |
| 130 void InitializeNewContextExpectFail() { |
| 131 EXPECT_FALSE(output_surface_.InitializeNewContext3D( |
| 132 second_context3d_.PassAs<WebKit::WebGraphicsContext3D>())); |
| 133 EXPECT_EQ(&client_, output_surface_.client()); |
| 134 |
| 135 // First context3d still in place. |
| 136 ASSERT_TRUE(output_surface_.context3d()); |
| 137 EXPECT_EQ(first_context3d_ptr_, output_surface_.context3d()); |
| 138 EXPECT_FALSE(client_.did_lose_output_surface_called()); |
| 139 output_surface_.context3d()->loseContextCHROMIUM( |
| 140 GL_GUILTY_CONTEXT_RESET_ARB, GL_INNOCENT_CONTEXT_RESET_ARB); |
| 141 EXPECT_TRUE(client_.did_lose_output_surface_called()); |
| 142 } |
| 143 |
| 144 TestWebGraphicsContext3D* first_context3d_ptr_; |
| 145 scoped_ptr<TestWebGraphicsContext3D> second_context3d_; |
| 146 TestOutputSurface output_surface_; |
| 147 FakeOutputSurfaceClient client_; |
| 148 }; |
| 149 |
| 150 TEST_F(InitializeNewContext3D, Success) { |
| 151 BindOutputSurface(); |
| 152 EXPECT_FALSE(client_.deferred_initialize_called()); |
| 153 |
| 154 EXPECT_TRUE(output_surface_.InitializeNewContext3D( |
| 155 second_context3d_.PassAs<WebKit::WebGraphicsContext3D>())); |
| 156 EXPECT_TRUE(client_.deferred_initialize_called()); |
| 157 |
| 158 EXPECT_FALSE(client_.did_lose_output_surface_called()); |
| 159 output_surface_.context3d()->loseContextCHROMIUM( |
| 160 GL_GUILTY_CONTEXT_RESET_ARB, GL_INNOCENT_CONTEXT_RESET_ARB); |
| 161 EXPECT_TRUE(client_.did_lose_output_surface_called()); |
| 162 } |
| 163 |
| 164 TEST_F(InitializeNewContext3D, Context3dMakeCurrentFails) { |
| 165 BindOutputSurface(); |
| 166 second_context3d_->set_times_make_current_succeeds(0); |
| 167 InitializeNewContextExpectFail(); |
| 168 } |
| 169 |
| 170 TEST_F(InitializeNewContext3D, ClientDeferredInitializeFails) { |
| 171 BindOutputSurface(); |
| 172 client_.set_deferred_initialize_result(false); |
| 173 InitializeNewContextExpectFail(); |
| 174 } |
| 175 |
76 } // namespace | 176 } // namespace |
77 } // namespace cc | 177 } // namespace cc |
OLD | NEW |