1 // Copyright 2014 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 "content/browser/compositor/gpu_process_transport_factory.h"
10 #include "base/command_line.h"
11 #include "base/location.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/metrics/histogram.h"
14 #include "base/threading/thread.h"
15 #include "cc/output/compositor_frame.h"
16 #include "cc/output/output_surface.h"
17 #include "cc/surfaces/surface_manager.h"
18 #include "content/browser/compositor/browser_compositor_output_surface.h"
19 #include "content/browser/compositor/browser_compositor_output_surface_proxy.h"
20 #include "content/browser/compositor/gpu_browser_compositor_output_surface.h"
21 #include "content/browser/compositor/onscreen_display_client.h"
22 #include "content/browser/compositor/reflector_impl.h"
23 #include "content/browser/compositor/software_browser_compositor_output_surface.h"
24 #include "content/browser/compositor/surface_display_output_surface.h"
25 #include "content/browser/gpu/browser_gpu_channel_host_factory.h"
26 #include "content/browser/gpu/gpu_data_manager_impl.h"
27 #include "content/browser/gpu/gpu_surface_tracker.h"
28 #include "content/browser/renderer_host/render_widget_host_impl.h"
29 #include "content/common/gpu/client/context_provider_command_buffer.h"
30 #include "content/common/gpu/client/gl_helper.h"
31 #include "content/common/gpu/client/gpu_channel_host.h"
32 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
33 #include "content/common/gpu/gpu_process_launch_causes.h"
34 #include "content/common/host_shared_bitmap_manager.h"
35 #include "content/public/common/content_switches.h"
36 #include "gpu/GLES2/gl2extchromium.h"
37 #include "gpu/command_buffer/client/gles2_interface.h"
38 #include "gpu/command_buffer/common/mailbox.h"
39 #include "third_party/khronos/GLES2/gl2.h"
40 #include "ui/compositor/compositor.h"
41 #include "ui/compositor/compositor_constants.h"
42 #include "ui/compositor/compositor_switches.h"
43 #include "ui/gfx/native_widget_types.h"
44 #include "ui/gfx/size.h"
47 #include "content/browser/compositor/software_output_device_win.h"
48 #elif defined(USE_OZONE)
49 #include "content/browser/compositor/overlay_candidate_validator_ozone.h"
50 #include "content/browser/compositor/software_output_device_ozone.h"
51 #include "ui/ozone/public/surface_factory_ozone.h"
52 #elif defined(USE_X11)
53 #include "content/browser/compositor/software_output_device_x11.h"
54 #elif defined(OS_MACOSX)
55 #include "content/browser/compositor/software_output_device_mac.h"
58 using cc::ContextProvider
;
59 using gpu::gles2::GLES2Interface
;
63 struct GpuProcessTransportFactory::PerCompositorData
{
65 scoped_refptr
<ReflectorImpl
> reflector
;
66 scoped_ptr
<OnscreenDisplayClient
> display_client
;
69 GpuProcessTransportFactory::GpuProcessTransportFactory()
70 : callback_factory_(this),
71 next_surface_id_namespace_(1u) {
72 output_surface_proxy_
= new BrowserCompositorOutputSurfaceProxy(
73 &output_surface_map_
);
74 #if defined(OS_CHROMEOS)
75 bool use_thread
= !CommandLine::ForCurrentProcess()->HasSwitch(
76 switches::kUIDisableThreadedCompositing
);
78 bool use_thread
= false;
81 compositor_thread_
.reset(new base::Thread("Browser Compositor"));
82 compositor_thread_
->Start();
84 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
85 switches::kUseSurfaces
)) {
86 surface_manager_
= make_scoped_ptr(new cc::SurfaceManager
);
90 GpuProcessTransportFactory::~GpuProcessTransportFactory() {
91 DCHECK(per_compositor_data_
.empty());
93 // Make sure the lost context callback doesn't try to run during destruction.
94 callback_factory_
.InvalidateWeakPtrs();
97 scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
>
98 GpuProcessTransportFactory::CreateOffscreenCommandBufferContext() {
99 return CreateContextCommon(0);
102 scoped_ptr
<cc::SoftwareOutputDevice
> CreateSoftwareOutputDevice(
103 ui::Compositor
* compositor
) {
105 return scoped_ptr
<cc::SoftwareOutputDevice
>(new SoftwareOutputDeviceWin(
107 #elif defined(USE_OZONE)
108 return scoped_ptr
<cc::SoftwareOutputDevice
>(new SoftwareOutputDeviceOzone(
110 #elif defined(USE_X11)
111 return scoped_ptr
<cc::SoftwareOutputDevice
>(new SoftwareOutputDeviceX11(
113 #elif defined(OS_MACOSX)
114 return scoped_ptr
<cc::SoftwareOutputDevice
>(
115 new SoftwareOutputDeviceMac(compositor
));
118 return scoped_ptr
<cc::SoftwareOutputDevice
>();
122 scoped_ptr
<cc::OverlayCandidateValidator
> CreateOverlayCandidateValidator(
123 gfx::AcceleratedWidget widget
) {
124 #if defined(USE_OZONE)
125 ui::OverlayCandidatesOzone
* overlay_candidates
=
126 ui::SurfaceFactoryOzone::GetInstance()->GetOverlayCandidates(widget
);
127 if (overlay_candidates
&& CommandLine::ForCurrentProcess()->HasSwitch(
128 switches::kEnableHardwareOverlays
)) {
129 return scoped_ptr
<cc::OverlayCandidateValidator
>(
130 new OverlayCandidateValidatorOzone(widget
, overlay_candidates
));
133 return scoped_ptr
<cc::OverlayCandidateValidator
>();
136 scoped_ptr
<cc::OutputSurface
> GpuProcessTransportFactory::CreateOutputSurface(
137 ui::Compositor
* compositor
, bool software_fallback
) {
138 PerCompositorData
* data
= per_compositor_data_
[compositor
];
140 data
= CreatePerCompositorData(compositor
);
142 bool create_software_renderer
= software_fallback
;
143 #if defined(OS_CHROMEOS)
144 // Software fallback does not happen on Chrome OS.
145 create_software_renderer
= false;
146 #elif defined(OS_WIN)
147 if (::GetProp(compositor
->widget(), kForceSoftwareCompositor
)) {
148 if (::RemoveProp(compositor
->widget(), kForceSoftwareCompositor
))
149 create_software_renderer
= true;
153 scoped_refptr
<ContextProviderCommandBuffer
> context_provider
;
155 if (!create_software_renderer
) {
156 context_provider
= ContextProviderCommandBuffer::Create(
157 GpuProcessTransportFactory::CreateContextCommon(data
->surface_id
),
161 UMA_HISTOGRAM_BOOLEAN("Aura.CreatedGpuBrowserCompositor", !!context_provider
);
163 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
164 switches::kUseSurfaces
)) {
165 // This gets a bit confusing. Here we have a ContextProvider configured to
166 // render directly to this widget. We need to make an OnscreenDisplayClient
167 // associated with this context, then return a SurfaceDisplayOutputSurface
168 // set up to draw to the display's surface.
169 cc::SurfaceManager
* manager
= surface_manager_
.get();
170 scoped_ptr
<cc::OutputSurface
> software_surface
;
171 if (!context_provider
) {
173 make_scoped_ptr(new SoftwareBrowserCompositorOutputSurface(
174 output_surface_proxy_
,
175 CreateSoftwareOutputDevice(compositor
),
176 per_compositor_data_
[compositor
]->surface_id
,
177 &output_surface_map_
,
178 compositor
->vsync_manager()));
180 scoped_ptr
<OnscreenDisplayClient
> display_client(new OnscreenDisplayClient(
181 context_provider
, software_surface
.Pass(), manager
));
182 // TODO(jamesr): Need to set up filtering for the
183 // GpuHostMsg_UpdateVSyncParameters message.
185 scoped_refptr
<cc::ContextProvider
> offscreen_context_provider
;
186 if (context_provider
) {
187 offscreen_context_provider
= ContextProviderCommandBuffer::Create(
188 GpuProcessTransportFactory::CreateOffscreenCommandBufferContext(),
189 "Offscreen-Compositor");
191 scoped_ptr
<SurfaceDisplayOutputSurface
> output_surface(
192 new SurfaceDisplayOutputSurface(manager
,
193 next_surface_id_namespace_
++,
194 offscreen_context_provider
));
195 output_surface
->set_display(display_client
->display());
196 data
->display_client
= display_client
.Pass();
197 return output_surface
.PassAs
<cc::OutputSurface
>();
200 if (!context_provider
.get()) {
201 if (compositor_thread_
.get()) {
202 LOG(FATAL
) << "Failed to create UI context, but can't use software"
203 " compositing with browser threaded compositing. Aborting.";
206 scoped_ptr
<SoftwareBrowserCompositorOutputSurface
> surface(
207 new SoftwareBrowserCompositorOutputSurface(
208 output_surface_proxy_
,
209 CreateSoftwareOutputDevice(compositor
),
210 per_compositor_data_
[compositor
]->surface_id
,
211 &output_surface_map_
,
212 compositor
->vsync_manager()));
213 return surface
.PassAs
<cc::OutputSurface
>();
216 scoped_refptr
<base::SingleThreadTaskRunner
> compositor_thread_task_runner
=
217 GetCompositorMessageLoop();
218 if (!compositor_thread_task_runner
.get())
219 compositor_thread_task_runner
= base::MessageLoopProxy::current();
221 // Here we know the GpuProcessHost has been set up, because we created a
223 output_surface_proxy_
->ConnectToGpuProcessHost(
224 compositor_thread_task_runner
.get());
226 scoped_ptr
<BrowserCompositorOutputSurface
> surface(
227 new GpuBrowserCompositorOutputSurface(
229 per_compositor_data_
[compositor
]->surface_id
,
230 &output_surface_map_
,
231 compositor
->vsync_manager(),
232 CreateOverlayCandidateValidator(compositor
->widget())));
233 if (data
->reflector
.get())
234 data
->reflector
->ReattachToOutputSurfaceFromMainThread(surface
.get());
236 return surface
.PassAs
<cc::OutputSurface
>();
239 scoped_refptr
<ui::Reflector
> GpuProcessTransportFactory::CreateReflector(
240 ui::Compositor
* source
,
242 PerCompositorData
* data
= per_compositor_data_
[source
];
245 data
->reflector
= new ReflectorImpl(source
,
247 &output_surface_map_
,
248 GetCompositorMessageLoop(),
250 return data
->reflector
;
253 void GpuProcessTransportFactory::RemoveReflector(
254 scoped_refptr
<ui::Reflector
> reflector
) {
255 ReflectorImpl
* reflector_impl
=
256 static_cast<ReflectorImpl
*>(reflector
.get());
257 PerCompositorData
* data
=
258 per_compositor_data_
[reflector_impl
->mirrored_compositor()];
260 data
->reflector
->Shutdown();
261 data
->reflector
= NULL
;
264 void GpuProcessTransportFactory::RemoveCompositor(ui::Compositor
* compositor
) {
265 PerCompositorDataMap::iterator it
= per_compositor_data_
.find(compositor
);
266 if (it
== per_compositor_data_
.end())
268 PerCompositorData
* data
= it
->second
;
270 GpuSurfaceTracker::Get()->RemoveSurface(data
->surface_id
);
272 per_compositor_data_
.erase(it
);
273 if (per_compositor_data_
.empty()) {
274 // Destroying the GLHelper may cause some async actions to be cancelled,
275 // causing things to request a new GLHelper. Due to crbug.com/176091 the
276 // GLHelper created in this case would be lost/leaked if we just reset()
277 // on the |gl_helper_| variable directly. So instead we call reset() on a
279 scoped_ptr
<GLHelper
> helper
= gl_helper_
.Pass();
281 // If there are any observer left at this point, make sure they clean up
282 // before we destroy the GLHelper.
284 ImageTransportFactoryObserver
, observer_list_
, OnLostResources());
287 DCHECK(!gl_helper_
) << "Destroying the GLHelper should not cause a new "
288 "GLHelper to be created.";
292 bool GpuProcessTransportFactory::DoesCreateTestContexts() { return false; }
294 cc::SharedBitmapManager
* GpuProcessTransportFactory::GetSharedBitmapManager() {
295 return HostSharedBitmapManager::current();
298 ui::ContextFactory
* GpuProcessTransportFactory::GetContextFactory() {
302 base::MessageLoopProxy
* GpuProcessTransportFactory::GetCompositorMessageLoop() {
303 if (!compositor_thread_
)
305 return compositor_thread_
->message_loop_proxy();
308 gfx::GLSurfaceHandle
GpuProcessTransportFactory::GetSharedSurfaceHandle() {
309 gfx::GLSurfaceHandle handle
= gfx::GLSurfaceHandle(
310 gfx::kNullPluginWindow
, gfx::TEXTURE_TRANSPORT
);
311 handle
.parent_client_id
=
312 BrowserGpuChannelHostFactory::instance()->GetGpuChannelId();
316 GLHelper
* GpuProcessTransportFactory::GetGLHelper() {
317 if (!gl_helper_
&& !per_compositor_data_
.empty()) {
318 scoped_refptr
<cc::ContextProvider
> provider
=
319 SharedMainThreadContextProvider();
321 gl_helper_
.reset(new GLHelper(provider
->ContextGL(),
322 provider
->ContextSupport()));
324 return gl_helper_
.get();
327 void GpuProcessTransportFactory::AddObserver(
328 ImageTransportFactoryObserver
* observer
) {
329 observer_list_
.AddObserver(observer
);
332 void GpuProcessTransportFactory::RemoveObserver(
333 ImageTransportFactoryObserver
* observer
) {
334 observer_list_
.RemoveObserver(observer
);
337 #if defined(OS_MACOSX)
338 void GpuProcessTransportFactory::OnSurfaceDisplayed(int surface_id
) {
339 BrowserCompositorOutputSurface
* surface
= output_surface_map_
.Lookup(
342 surface
->OnSurfaceDisplayed();
346 scoped_refptr
<cc::ContextProvider
>
347 GpuProcessTransportFactory::SharedMainThreadContextProvider() {
348 if (shared_main_thread_contexts_
.get())
349 return shared_main_thread_contexts_
;
351 // In threaded compositing mode, we have to create our own context for the
352 // main thread since the compositor's context will be bound to the
353 // compositor thread. When not in threaded mode, we still need a separate
354 // context so that skia and gl_helper don't step on each other.
355 shared_main_thread_contexts_
= ContextProviderCommandBuffer::Create(
356 GpuProcessTransportFactory::CreateOffscreenCommandBufferContext(),
357 "Offscreen-MainThread");
359 if (shared_main_thread_contexts_
) {
360 shared_main_thread_contexts_
->SetLostContextCallback(
361 base::Bind(&GpuProcessTransportFactory::
362 OnLostMainThreadSharedContextInsideCallback
,
363 callback_factory_
.GetWeakPtr()));
364 if (!shared_main_thread_contexts_
->BindToCurrentThread())
365 shared_main_thread_contexts_
= NULL
;
367 return shared_main_thread_contexts_
;
370 GpuProcessTransportFactory::PerCompositorData
*
371 GpuProcessTransportFactory::CreatePerCompositorData(
372 ui::Compositor
* compositor
) {
373 DCHECK(!per_compositor_data_
[compositor
]);
375 gfx::AcceleratedWidget widget
= compositor
->widget();
376 GpuSurfaceTracker
* tracker
= GpuSurfaceTracker::Get();
378 PerCompositorData
* data
= new PerCompositorData
;
379 data
->surface_id
= tracker
->AddSurfaceForNativeWidget(widget
);
380 tracker
->SetSurfaceHandle(
382 gfx::GLSurfaceHandle(widget
, gfx::NATIVE_DIRECT
));
384 per_compositor_data_
[compositor
] = data
;
389 scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
>
390 GpuProcessTransportFactory::CreateContextCommon(int surface_id
) {
391 if (!GpuDataManagerImpl::GetInstance()->CanUseGpuBrowserCompositor())
392 return scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
>();
393 blink::WebGraphicsContext3D::Attributes attrs
;
394 attrs
.shareResources
= true;
396 attrs
.stencil
= false;
397 attrs
.antialias
= false;
398 attrs
.noAutomaticFlushes
= true;
399 bool lose_context_when_out_of_memory
= true;
400 CauseForGpuLaunch cause
=
401 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE
;
402 scoped_refptr
<GpuChannelHost
> gpu_channel_host(
403 BrowserGpuChannelHostFactory::instance()->EstablishGpuChannelSync(cause
));
404 if (!gpu_channel_host
) {
405 LOG(ERROR
) << "Failed to establish GPU channel.";
406 return scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
>();
408 GURL
url("chrome://gpu/GpuProcessTransportFactory::CreateContextCommon");
409 scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
> context(
410 new WebGraphicsContext3DCommandBufferImpl(
413 gpu_channel_host
.get(),
415 lose_context_when_out_of_memory
,
416 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits(),
418 return context
.Pass();
421 void GpuProcessTransportFactory::OnLostMainThreadSharedContextInsideCallback() {
422 base::MessageLoop::current()->PostTask(
424 base::Bind(&GpuProcessTransportFactory::OnLostMainThreadSharedContext
,
425 callback_factory_
.GetWeakPtr()));
428 void GpuProcessTransportFactory::OnLostMainThreadSharedContext() {
429 LOG(ERROR
) << "Lost UI shared context.";
431 // Keep old resources around while we call the observers, but ensure that
432 // new resources are created if needed.
433 // Kill shared contexts for both threads in tandem so they are always in
434 // the same share group.
435 scoped_refptr
<cc::ContextProvider
> lost_shared_main_thread_contexts
=
436 shared_main_thread_contexts_
;
437 shared_main_thread_contexts_
= NULL
;
439 scoped_ptr
<GLHelper
> lost_gl_helper
= gl_helper_
.Pass();
441 FOR_EACH_OBSERVER(ImageTransportFactoryObserver
,
445 // Kill things that use the shared context before killing the shared context.
446 lost_gl_helper
.reset();
447 lost_shared_main_thread_contexts
= NULL
;
450 } // namespace content