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
3 // found in the LICENSE file.
5 #include "base/command_line.h"
6 #include "base/run_loop.h"
7 #include "content/browser/gpu/browser_gpu_channel_host_factory.h"
8 #include "content/browser/gpu/gpu_process_host_ui_shim.h"
9 #include "content/common/gpu/client/context_provider_command_buffer.h"
10 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
11 #include "content/common/gpu/gpu_process_launch_causes.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/gpu_data_manager.h"
14 #include "content/public/common/content_switches.h"
15 #include "content/test/content_browser_test.h"
16 #include "ui/gl/gl_switches.h"
17 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
21 using content::WebGraphicsContext3DCommandBufferImpl
;
23 class ContextTestBase
: public content::ContentBrowserTest
{
25 virtual void SetUpOnMainThread() OVERRIDE
{
26 if (!content::BrowserGpuChannelHostFactory::CanUseForTesting())
29 if (!content::BrowserGpuChannelHostFactory::instance())
30 content::BrowserGpuChannelHostFactory::Initialize(true);
32 content::BrowserGpuChannelHostFactory
* factory
=
33 content::BrowserGpuChannelHostFactory::instance();
35 scoped_refptr
<content::GpuChannelHost
> gpu_channel_host(
36 factory
->EstablishGpuChannelSync(
38 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE
));
40 WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
41 gpu_channel_host
.get(),
42 blink::WebGraphicsContext3D::Attributes(),
44 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits()));
45 CHECK(context_
.get());
46 context_
->makeContextCurrent();
47 context_support_
= context_
->GetContextSupport();
48 ContentBrowserTest::SetUpOnMainThread();
51 virtual void TearDownOnMainThread() OVERRIDE
{
52 // Must delete the context first.
54 ContentBrowserTest::TearDownOnMainThread();
58 scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
> context_
;
59 gpu::ContextSupport
* context_support_
;
64 // Include the shared tests.
65 #define CONTEXT_TEST_F IN_PROC_BROWSER_TEST_F
66 #include "content/common/gpu/client/gpu_context_tests.h"
70 class BrowserGpuChannelHostFactoryTest
: public ContextTestBase
{
72 virtual void SetUpOnMainThread() OVERRIDE
{
73 if (!content::BrowserGpuChannelHostFactory::CanUseForTesting())
76 // Start all tests without a gpu channel so that the tests exercise a
77 // consistent codepath.
78 if (!content::BrowserGpuChannelHostFactory::instance())
79 content::BrowserGpuChannelHostFactory::Initialize(false);
83 ContentBrowserTest::SetUpOnMainThread();
86 virtual void TearDownOnMainThread() OVERRIDE
{
87 ContextTestBase::TearDownOnMainThread();
90 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
91 // Start all tests without a gpu channel so that the tests exercise a
92 // consistent codepath.
93 command_line
->AppendSwitch(switches::kDisableGpuProcessPrelaunch
);
96 void OnContextLost(const base::Closure callback
, int* counter
) {
102 BrowserGpuChannelHostFactory
* GetFactory() {
103 return BrowserGpuChannelHostFactory::instance();
106 bool IsChannelEstablished() {
107 return GetFactory()->GetGpuChannel() != NULL
;
110 void EstablishAndWait() {
111 base::RunLoop run_loop
;
112 GetFactory()->EstablishGpuChannel(
113 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE
,
114 run_loop
.QuitClosure());
118 GpuChannelHost
* GetGpuChannel() {
119 return GetFactory()->GetGpuChannel();
122 static void Signal(bool *event
) {
123 CHECK_EQ(*event
, false);
127 scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
> CreateContext() {
128 return make_scoped_ptr(
129 WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
131 blink::WebGraphicsContext3D::Attributes(),
133 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits()));
137 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest
, Basic
) {
141 DCHECK(!IsChannelEstablished());
143 EXPECT_TRUE(GetGpuChannel() != NULL
);
146 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest
,
147 EstablishAndTerminate
) {
151 DCHECK(!IsChannelEstablished());
152 base::RunLoop run_loop
;
153 GetFactory()->EstablishGpuChannel(
154 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE
,
155 run_loop
.QuitClosure());
156 GetFactory()->Terminate();
158 // The callback should still trigger.
162 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest
, AlreadyEstablished
) {
166 DCHECK(!IsChannelEstablished());
167 scoped_refptr
<GpuChannelHost
> gpu_channel
=
168 GetFactory()->EstablishGpuChannelSync(
169 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE
);
171 // Expect established callback immediately.
173 GetFactory()->EstablishGpuChannel(
174 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE
,
175 base::Bind(&BrowserGpuChannelHostFactoryTest::Signal
, &event
));
177 EXPECT_EQ(gpu_channel
, GetGpuChannel());
180 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest
, CrashAndRecover
) {
184 DCHECK(!IsChannelEstablished());
186 scoped_refptr
<GpuChannelHost
> host
= GetGpuChannel();
188 scoped_refptr
<ContextProviderCommandBuffer
> provider
=
189 ContextProviderCommandBuffer::Create(CreateContext(),
190 "BrowserGpuChannelHostFactoryTest");
191 base::RunLoop run_loop
;
193 provider
->SetLostContextCallback(
194 base::Bind(&BrowserGpuChannelHostFactoryTest::OnContextLost
,
195 base::Unretained(this), run_loop
.QuitClosure(), &counter
));
196 EXPECT_TRUE(provider
->BindToCurrentThread());
197 GpuProcessHostUIShim
* shim
=
198 GpuProcessHostUIShim::FromID(GetFactory()->GpuProcessHostId());
199 EXPECT_TRUE(shim
!= NULL
);
200 shim
->SimulateCrash();
203 EXPECT_EQ(1, counter
);
204 EXPECT_FALSE(IsChannelEstablished());
206 EXPECT_TRUE(IsChannelEstablished());
209 } // namespace content