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 OnContextLost(const base::Closure callback
, int* counter
) {
100 BrowserGpuChannelHostFactory
* GetFactory() {
101 return BrowserGpuChannelHostFactory::instance();
104 bool IsChannelEstablished() {
105 return GetFactory()->GetGpuChannel() != NULL
;
108 void EstablishAndWait() {
109 base::RunLoop run_loop
;
110 GetFactory()->EstablishGpuChannel(kInitCause
, run_loop
.QuitClosure());
114 GpuChannelHost
* GetGpuChannel() {
115 return GetFactory()->GetGpuChannel();
118 static void Signal(bool *event
) {
119 CHECK_EQ(*event
, false);
123 scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
> CreateContext() {
124 bool lose_context_when_out_of_memory
= false;
125 return make_scoped_ptr(
126 WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
128 blink::WebGraphicsContext3D::Attributes(),
129 lose_context_when_out_of_memory
,
131 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits(),
136 // Fails since UI Compositor establishes a GpuChannel.
137 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest
, DISABLED_Basic
) {
138 DCHECK(!IsChannelEstablished());
140 EXPECT_TRUE(GetGpuChannel() != NULL
);
143 // Fails since UI Compositor establishes a GpuChannel.
144 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest
,
145 DISABLED_EstablishAndTerminate
) {
146 DCHECK(!IsChannelEstablished());
147 base::RunLoop run_loop
;
148 GetFactory()->EstablishGpuChannel(kInitCause
, run_loop
.QuitClosure());
149 GetFactory()->Terminate();
151 // The callback should still trigger.
155 #if !defined(OS_ANDROID)
156 // Fails since UI Compositor establishes a GpuChannel.
157 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest
,
158 DISABLED_AlreadyEstablished
) {
159 DCHECK(!IsChannelEstablished());
160 scoped_refptr
<GpuChannelHost
> gpu_channel
=
161 GetFactory()->EstablishGpuChannelSync(kInitCause
);
163 // Expect established callback immediately.
165 GetFactory()->EstablishGpuChannel(
167 base::Bind(&BrowserGpuChannelHostFactoryTest::Signal
, &event
));
169 EXPECT_EQ(gpu_channel
.get(), GetGpuChannel());
173 // Fails since UI Compositor establishes a GpuChannel.
174 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest
,
175 DISABLED_CrashAndRecover
) {
176 DCHECK(!IsChannelEstablished());
178 scoped_refptr
<GpuChannelHost
> host
= GetGpuChannel();
180 scoped_refptr
<ContextProviderCommandBuffer
> provider
=
181 ContextProviderCommandBuffer::Create(CreateContext(),
182 "BrowserGpuChannelHostFactoryTest");
183 base::RunLoop run_loop
;
185 provider
->SetLostContextCallback(
186 base::Bind(&BrowserGpuChannelHostFactoryTest::OnContextLost
,
187 base::Unretained(this), run_loop
.QuitClosure(), &counter
));
188 EXPECT_TRUE(provider
->BindToCurrentThread());
189 GpuProcessHostUIShim
* shim
=
190 GpuProcessHostUIShim::FromID(GetFactory()->GpuProcessHostId());
191 EXPECT_TRUE(shim
!= NULL
);
192 shim
->SimulateCrash();
195 EXPECT_EQ(1, counter
);
196 EXPECT_FALSE(IsChannelEstablished());
198 EXPECT_TRUE(IsChannelEstablished());
201 } // namespace content