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/gpu_data_manager.h"
13 #include "content/public/common/content_switches.h"
14 #include "content/public/test/content_browser_test.h"
15 #include "gpu/blink/webgraphicscontext3d_in_process_command_buffer_impl.h"
16 #include "ui/gl/gl_switches.h"
20 using content::WebGraphicsContext3DCommandBufferImpl
;
22 const content::CauseForGpuLaunch kInitCause
=
24 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE
;
26 class ContextTestBase
: public content::ContentBrowserTest
{
28 void SetUpOnMainThread() override
{
29 if (!content::BrowserGpuChannelHostFactory::CanUseForTesting())
32 if (!content::BrowserGpuChannelHostFactory::instance())
33 content::BrowserGpuChannelHostFactory::Initialize(true);
35 content::BrowserGpuChannelHostFactory
* factory
=
36 content::BrowserGpuChannelHostFactory::instance();
38 bool lose_context_when_out_of_memory
= false;
39 base::RunLoop run_loop
;
40 factory
->EstablishGpuChannel(kInitCause
, run_loop
.QuitClosure());
42 scoped_refptr
<content::GpuChannelHost
>
43 gpu_channel_host(factory
->GetGpuChannel());
44 DCHECK(gpu_channel_host
.get());
46 WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
47 gpu_channel_host
.get(),
48 blink::WebGraphicsContext3D::Attributes(),
49 lose_context_when_out_of_memory
,
51 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits(),
53 CHECK(context_
.get());
54 context_
->InitializeOnCurrentThread();
55 context_support_
= context_
->GetContextSupport();
56 ContentBrowserTest::SetUpOnMainThread();
59 void TearDownOnMainThread() override
{
60 // Must delete the context first.
62 ContentBrowserTest::TearDownOnMainThread();
66 scoped_ptr
<content::WebGraphicsContext3DCommandBufferImpl
> context_
;
67 gpu::ContextSupport
* context_support_
;
72 // Include the shared tests.
73 #define CONTEXT_TEST_F IN_PROC_BROWSER_TEST_F
74 #include "content/common/gpu/client/gpu_context_tests.h"
78 class BrowserGpuChannelHostFactoryTest
: public ContentBrowserTest
{
80 void SetUpOnMainThread() override
{
81 if (!BrowserGpuChannelHostFactory::CanUseForTesting())
84 // Start all tests without a gpu channel so that the tests exercise a
85 // consistent codepath.
86 if (!BrowserGpuChannelHostFactory::instance())
87 BrowserGpuChannelHostFactory::Initialize(false);
91 ContentBrowserTest::SetUpOnMainThread();
94 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
95 // Start all tests without a gpu channel so that the tests exercise a
96 // consistent codepath.
97 command_line
->AppendSwitch(switches::kDisableGpuEarlyInit
);
100 void OnContextLost(const base::Closure callback
, int* counter
) {
106 BrowserGpuChannelHostFactory
* GetFactory() {
107 return BrowserGpuChannelHostFactory::instance();
110 bool IsChannelEstablished() {
111 return GetFactory()->GetGpuChannel() != NULL
;
114 void EstablishAndWait() {
115 base::RunLoop run_loop
;
116 GetFactory()->EstablishGpuChannel(kInitCause
, run_loop
.QuitClosure());
120 GpuChannelHost
* GetGpuChannel() {
121 return GetFactory()->GetGpuChannel();
124 static void Signal(bool *event
) {
125 CHECK_EQ(*event
, false);
129 scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
> CreateContext() {
130 bool lose_context_when_out_of_memory
= false;
131 return make_scoped_ptr(
132 WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
134 blink::WebGraphicsContext3D::Attributes(),
135 lose_context_when_out_of_memory
,
137 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits(),
142 // Test fails on Chromeos + Mac, flaky on Windows because UI Compositor
143 // establishes a GPU channel.
144 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
145 #define MAYBE_Basic Basic
147 #define MAYBE_Basic DISABLED_Basic
149 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest
, MAYBE_Basic
) {
150 DCHECK(!IsChannelEstablished());
152 EXPECT_TRUE(GetGpuChannel() != NULL
);
155 // Test fails on Chromeos + Mac, flaky on Windows because UI Compositor
156 // establishes a GPU channel.
157 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
158 #define MAYBE_EstablishAndTerminate EstablishAndTerminate
160 #define MAYBE_EstablishAndTerminate DISABLED_EstablishAndTerminate
162 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest
,
163 MAYBE_EstablishAndTerminate
) {
164 DCHECK(!IsChannelEstablished());
165 base::RunLoop run_loop
;
166 GetFactory()->EstablishGpuChannel(kInitCause
, run_loop
.QuitClosure());
167 GetFactory()->Terminate();
169 // The callback should still trigger.
173 #if !defined(OS_ANDROID)
174 // Test fails on Chromeos + Mac, flaky on Windows because UI Compositor
175 // establishes a GPU channel.
176 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
177 #define MAYBE_AlreadyEstablished AlreadyEstablished
179 #define MAYBE_AlreadyEstablished DISABLED_AlreadyEstablished
181 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest
,
182 MAYBE_AlreadyEstablished
) {
183 DCHECK(!IsChannelEstablished());
184 scoped_refptr
<GpuChannelHost
> gpu_channel
=
185 GetFactory()->EstablishGpuChannelSync(kInitCause
);
187 // Expect established callback immediately.
189 GetFactory()->EstablishGpuChannel(
191 base::Bind(&BrowserGpuChannelHostFactoryTest::Signal
, &event
));
193 EXPECT_EQ(gpu_channel
.get(), GetGpuChannel());
197 // Test fails on Chromeos + Mac, flaky on Windows because UI Compositor
198 // establishes a GPU channel.
199 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
200 #define MAYBE_CrashAndRecover
202 #define MAYBE_CrashAndRecover DISABLED_CrashAndRecover
204 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest
,
205 MAYBE_CrashAndRecover
) {
206 DCHECK(!IsChannelEstablished());
208 scoped_refptr
<GpuChannelHost
> host
= GetGpuChannel();
210 scoped_refptr
<ContextProviderCommandBuffer
> provider
=
211 ContextProviderCommandBuffer::Create(CreateContext(),
212 OFFSCREEN_CONTEXT_FOR_TESTING
);
213 base::RunLoop run_loop
;
215 provider
->SetLostContextCallback(
216 base::Bind(&BrowserGpuChannelHostFactoryTest::OnContextLost
,
217 base::Unretained(this), run_loop
.QuitClosure(), &counter
));
218 EXPECT_TRUE(provider
->BindToCurrentThread());
219 GpuProcessHostUIShim
* shim
=
220 GpuProcessHostUIShim::FromID(GetFactory()->GpuProcessHostId());
221 EXPECT_TRUE(shim
!= NULL
);
222 shim
->SimulateCrash();
225 EXPECT_EQ(1, counter
);
226 EXPECT_FALSE(IsChannelEstablished());
228 EXPECT_TRUE(IsChannelEstablished());
231 } // namespace content