Remove implicit conversions from scoped_refptr to T* in content/browser/service_worker
[chromium-blink-merge.git] / content / browser / compositor / gpu_process_transport_factory.cc
blob834287cf8ab72d0ae04d03b0314dad9ad8cb7da8
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"
7 #include <string>
9 #include "base/bind.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"
46 #if defined(OS_WIN)
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"
56 #endif
58 using cc::ContextProvider;
59 using gpu::gles2::GLES2Interface;
61 namespace content {
63 struct GpuProcessTransportFactory::PerCompositorData {
64 int surface_id;
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 = !base::CommandLine::ForCurrentProcess()->HasSwitch(
76 switches::kUIDisableThreadedCompositing);
77 #else
78 bool use_thread = false;
79 #endif
80 if (use_thread) {
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) {
104 #if defined(OS_WIN)
105 return scoped_ptr<cc::SoftwareOutputDevice>(new SoftwareOutputDeviceWin(
106 compositor));
107 #elif defined(USE_OZONE)
108 return scoped_ptr<cc::SoftwareOutputDevice>(new SoftwareOutputDeviceOzone(
109 compositor));
110 #elif defined(USE_X11)
111 return scoped_ptr<cc::SoftwareOutputDevice>(new SoftwareOutputDeviceX11(
112 compositor));
113 #elif defined(OS_MACOSX)
114 return scoped_ptr<cc::SoftwareOutputDevice>(
115 new SoftwareOutputDeviceMac(compositor));
116 #else
117 NOTREACHED();
118 return scoped_ptr<cc::SoftwareOutputDevice>();
119 #endif
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 &&
128 base::CommandLine::ForCurrentProcess()->HasSwitch(
129 switches::kEnableHardwareOverlays)) {
130 return scoped_ptr<cc::OverlayCandidateValidator>(
131 new OverlayCandidateValidatorOzone(widget, overlay_candidates));
133 #endif
134 return scoped_ptr<cc::OverlayCandidateValidator>();
137 scoped_ptr<cc::OutputSurface> GpuProcessTransportFactory::CreateOutputSurface(
138 ui::Compositor* compositor, bool software_fallback) {
139 PerCompositorData* data = per_compositor_data_[compositor];
140 if (!data)
141 data = CreatePerCompositorData(compositor);
143 bool create_software_renderer = software_fallback;
144 #if defined(OS_CHROMEOS)
145 // Software fallback does not happen on Chrome OS.
146 create_software_renderer = false;
147 #elif defined(OS_WIN)
148 if (::GetProp(compositor->widget(), kForceSoftwareCompositor)) {
149 if (::RemoveProp(compositor->widget(), kForceSoftwareCompositor))
150 create_software_renderer = true;
152 #endif
154 scoped_refptr<ContextProviderCommandBuffer> context_provider;
156 if (!create_software_renderer) {
157 context_provider = ContextProviderCommandBuffer::Create(
158 GpuProcessTransportFactory::CreateContextCommon(data->surface_id),
159 "Compositor");
162 UMA_HISTOGRAM_BOOLEAN("Aura.CreatedGpuBrowserCompositor",
163 !!context_provider.get());
165 if (context_provider.get()) {
166 scoped_refptr<base::SingleThreadTaskRunner> compositor_thread_task_runner =
167 GetCompositorMessageLoop();
168 if (!compositor_thread_task_runner.get())
169 compositor_thread_task_runner = base::MessageLoopProxy::current();
171 // Here we know the GpuProcessHost has been set up, because we created a
172 // context.
173 output_surface_proxy_->ConnectToGpuProcessHost(
174 compositor_thread_task_runner.get());
177 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
178 switches::kUseSurfaces)) {
179 // This gets a bit confusing. Here we have a ContextProvider configured to
180 // render directly to this widget. We need to make an OnscreenDisplayClient
181 // associated with this context, then return a SurfaceDisplayOutputSurface
182 // set up to draw to the display's surface.
183 cc::SurfaceManager* manager = surface_manager_.get();
184 scoped_ptr<cc::OutputSurface> software_surface;
185 if (!context_provider.get()) {
186 software_surface =
187 make_scoped_ptr(new SoftwareBrowserCompositorOutputSurface(
188 output_surface_proxy_,
189 CreateSoftwareOutputDevice(compositor),
190 per_compositor_data_[compositor]->surface_id,
191 &output_surface_map_,
192 compositor->vsync_manager()));
194 scoped_ptr<OnscreenDisplayClient> display_client(
195 new OnscreenDisplayClient(context_provider,
196 software_surface.Pass(),
197 manager,
198 compositor->task_runner()));
199 // TODO(jamesr): Need to set up filtering for the
200 // GpuHostMsg_UpdateVSyncParameters message.
202 scoped_refptr<cc::ContextProvider> offscreen_context_provider;
203 if (context_provider.get()) {
204 offscreen_context_provider = ContextProviderCommandBuffer::Create(
205 GpuProcessTransportFactory::CreateOffscreenCommandBufferContext(),
206 "Offscreen-Compositor");
208 scoped_ptr<SurfaceDisplayOutputSurface> output_surface(
209 new SurfaceDisplayOutputSurface(manager,
210 next_surface_id_namespace_++,
211 offscreen_context_provider));
212 output_surface->set_display(display_client->display());
213 data->display_client = display_client.Pass();
214 return output_surface.PassAs<cc::OutputSurface>();
217 if (!context_provider.get()) {
218 if (compositor_thread_.get()) {
219 LOG(FATAL) << "Failed to create UI context, but can't use software"
220 " compositing with browser threaded compositing. Aborting.";
223 scoped_ptr<SoftwareBrowserCompositorOutputSurface> surface(
224 new SoftwareBrowserCompositorOutputSurface(
225 output_surface_proxy_,
226 CreateSoftwareOutputDevice(compositor),
227 per_compositor_data_[compositor]->surface_id,
228 &output_surface_map_,
229 compositor->vsync_manager()));
230 return surface.PassAs<cc::OutputSurface>();
233 scoped_ptr<BrowserCompositorOutputSurface> surface(
234 new GpuBrowserCompositorOutputSurface(
235 context_provider,
236 per_compositor_data_[compositor]->surface_id,
237 &output_surface_map_,
238 compositor->vsync_manager(),
239 CreateOverlayCandidateValidator(compositor->widget())));
240 if (data->reflector.get())
241 data->reflector->ReattachToOutputSurfaceFromMainThread(surface.get());
243 return surface.PassAs<cc::OutputSurface>();
246 scoped_refptr<ui::Reflector> GpuProcessTransportFactory::CreateReflector(
247 ui::Compositor* source,
248 ui::Layer* target) {
249 PerCompositorData* data = per_compositor_data_[source];
250 DCHECK(data);
252 data->reflector = new ReflectorImpl(source,
253 target,
254 &output_surface_map_,
255 GetCompositorMessageLoop(),
256 data->surface_id);
257 return data->reflector;
260 void GpuProcessTransportFactory::RemoveReflector(
261 scoped_refptr<ui::Reflector> reflector) {
262 ReflectorImpl* reflector_impl =
263 static_cast<ReflectorImpl*>(reflector.get());
264 PerCompositorData* data =
265 per_compositor_data_[reflector_impl->mirrored_compositor()];
266 DCHECK(data);
267 data->reflector->Shutdown();
268 data->reflector = NULL;
271 void GpuProcessTransportFactory::RemoveCompositor(ui::Compositor* compositor) {
272 PerCompositorDataMap::iterator it = per_compositor_data_.find(compositor);
273 if (it == per_compositor_data_.end())
274 return;
275 PerCompositorData* data = it->second;
276 DCHECK(data);
277 GpuSurfaceTracker::Get()->RemoveSurface(data->surface_id);
278 delete data;
279 per_compositor_data_.erase(it);
280 if (per_compositor_data_.empty()) {
281 // Destroying the GLHelper may cause some async actions to be cancelled,
282 // causing things to request a new GLHelper. Due to crbug.com/176091 the
283 // GLHelper created in this case would be lost/leaked if we just reset()
284 // on the |gl_helper_| variable directly. So instead we call reset() on a
285 // local scoped_ptr.
286 scoped_ptr<GLHelper> helper = gl_helper_.Pass();
288 // If there are any observer left at this point, make sure they clean up
289 // before we destroy the GLHelper.
290 FOR_EACH_OBSERVER(
291 ImageTransportFactoryObserver, observer_list_, OnLostResources());
293 helper.reset();
294 DCHECK(!gl_helper_) << "Destroying the GLHelper should not cause a new "
295 "GLHelper to be created.";
299 bool GpuProcessTransportFactory::DoesCreateTestContexts() { return false; }
301 cc::SharedBitmapManager* GpuProcessTransportFactory::GetSharedBitmapManager() {
302 return HostSharedBitmapManager::current();
305 ui::ContextFactory* GpuProcessTransportFactory::GetContextFactory() {
306 return this;
309 base::MessageLoopProxy* GpuProcessTransportFactory::GetCompositorMessageLoop() {
310 if (!compositor_thread_)
311 return NULL;
312 return compositor_thread_->message_loop_proxy().get();
315 gfx::GLSurfaceHandle GpuProcessTransportFactory::GetSharedSurfaceHandle() {
316 gfx::GLSurfaceHandle handle = gfx::GLSurfaceHandle(
317 gfx::kNullPluginWindow, gfx::TEXTURE_TRANSPORT);
318 handle.parent_client_id =
319 BrowserGpuChannelHostFactory::instance()->GetGpuChannelId();
320 return handle;
323 scoped_ptr<cc::SurfaceIdAllocator>
324 GpuProcessTransportFactory::CreateSurfaceIdAllocator() {
325 return make_scoped_ptr(
326 new cc::SurfaceIdAllocator(next_surface_id_namespace_++));
329 cc::SurfaceManager* GpuProcessTransportFactory::GetSurfaceManager() {
330 return surface_manager_.get();
333 GLHelper* GpuProcessTransportFactory::GetGLHelper() {
334 if (!gl_helper_ && !per_compositor_data_.empty()) {
335 scoped_refptr<cc::ContextProvider> provider =
336 SharedMainThreadContextProvider();
337 if (provider.get())
338 gl_helper_.reset(new GLHelper(provider->ContextGL(),
339 provider->ContextSupport()));
341 return gl_helper_.get();
344 void GpuProcessTransportFactory::AddObserver(
345 ImageTransportFactoryObserver* observer) {
346 observer_list_.AddObserver(observer);
349 void GpuProcessTransportFactory::RemoveObserver(
350 ImageTransportFactoryObserver* observer) {
351 observer_list_.RemoveObserver(observer);
354 #if defined(OS_MACOSX)
355 void GpuProcessTransportFactory::OnSurfaceDisplayed(int surface_id) {
356 BrowserCompositorOutputSurface* surface = output_surface_map_.Lookup(
357 surface_id);
358 if (surface)
359 surface->OnSurfaceDisplayed();
361 #endif
363 scoped_refptr<cc::ContextProvider>
364 GpuProcessTransportFactory::SharedMainThreadContextProvider() {
365 if (shared_main_thread_contexts_.get())
366 return shared_main_thread_contexts_;
368 // In threaded compositing mode, we have to create our own context for the
369 // main thread since the compositor's context will be bound to the
370 // compositor thread. When not in threaded mode, we still need a separate
371 // context so that skia and gl_helper don't step on each other.
372 shared_main_thread_contexts_ = ContextProviderCommandBuffer::Create(
373 GpuProcessTransportFactory::CreateOffscreenCommandBufferContext(),
374 "Offscreen-MainThread");
376 if (shared_main_thread_contexts_.get()) {
377 shared_main_thread_contexts_->SetLostContextCallback(
378 base::Bind(&GpuProcessTransportFactory::
379 OnLostMainThreadSharedContextInsideCallback,
380 callback_factory_.GetWeakPtr()));
381 if (!shared_main_thread_contexts_->BindToCurrentThread())
382 shared_main_thread_contexts_ = NULL;
384 return shared_main_thread_contexts_;
387 GpuProcessTransportFactory::PerCompositorData*
388 GpuProcessTransportFactory::CreatePerCompositorData(
389 ui::Compositor* compositor) {
390 DCHECK(!per_compositor_data_[compositor]);
392 gfx::AcceleratedWidget widget = compositor->widget();
393 GpuSurfaceTracker* tracker = GpuSurfaceTracker::Get();
395 PerCompositorData* data = new PerCompositorData;
396 data->surface_id = tracker->AddSurfaceForNativeWidget(widget);
397 tracker->SetSurfaceHandle(
398 data->surface_id,
399 gfx::GLSurfaceHandle(widget, gfx::NATIVE_DIRECT));
401 per_compositor_data_[compositor] = data;
403 return data;
406 scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
407 GpuProcessTransportFactory::CreateContextCommon(int surface_id) {
408 if (!GpuDataManagerImpl::GetInstance()->CanUseGpuBrowserCompositor())
409 return scoped_ptr<WebGraphicsContext3DCommandBufferImpl>();
410 blink::WebGraphicsContext3D::Attributes attrs;
411 attrs.shareResources = true;
412 attrs.depth = false;
413 attrs.stencil = false;
414 attrs.antialias = false;
415 attrs.noAutomaticFlushes = true;
416 bool lose_context_when_out_of_memory = true;
417 CauseForGpuLaunch cause =
418 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE;
419 scoped_refptr<GpuChannelHost> gpu_channel_host(
420 BrowserGpuChannelHostFactory::instance()->EstablishGpuChannelSync(cause));
421 if (!gpu_channel_host.get()) {
422 LOG(ERROR) << "Failed to establish GPU channel.";
423 return scoped_ptr<WebGraphicsContext3DCommandBufferImpl>();
425 GURL url("chrome://gpu/GpuProcessTransportFactory::CreateContextCommon");
426 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context(
427 new WebGraphicsContext3DCommandBufferImpl(
428 surface_id,
429 url,
430 gpu_channel_host.get(),
431 attrs,
432 lose_context_when_out_of_memory,
433 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits(),
434 NULL));
435 return context.Pass();
438 void GpuProcessTransportFactory::OnLostMainThreadSharedContextInsideCallback() {
439 base::MessageLoop::current()->PostTask(
440 FROM_HERE,
441 base::Bind(&GpuProcessTransportFactory::OnLostMainThreadSharedContext,
442 callback_factory_.GetWeakPtr()));
445 void GpuProcessTransportFactory::OnLostMainThreadSharedContext() {
446 LOG(ERROR) << "Lost UI shared context.";
448 // Keep old resources around while we call the observers, but ensure that
449 // new resources are created if needed.
450 // Kill shared contexts for both threads in tandem so they are always in
451 // the same share group.
452 scoped_refptr<cc::ContextProvider> lost_shared_main_thread_contexts =
453 shared_main_thread_contexts_;
454 shared_main_thread_contexts_ = NULL;
456 scoped_ptr<GLHelper> lost_gl_helper = gl_helper_.Pass();
458 FOR_EACH_OBSERVER(ImageTransportFactoryObserver,
459 observer_list_,
460 OnLostResources());
462 // Kill things that use the shared context before killing the shared context.
463 lost_gl_helper.reset();
464 lost_shared_main_thread_contexts = NULL;
467 } // namespace content