OLD | NEW |
| (Empty) |
1 // Copyright (c) 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. | |
4 | |
5 #include "content/browser/aura/gpu_process_transport_factory.h" | |
6 | |
7 #include <string> | |
8 | |
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 "cc/output/compositor_frame.h" | |
15 #include "cc/output/output_surface.h" | |
16 #include "content/browser/aura/browser_compositor_output_surface.h" | |
17 #include "content/browser/aura/browser_compositor_output_surface_proxy.h" | |
18 #include "content/browser/aura/gpu_browser_compositor_output_surface.h" | |
19 #include "content/browser/aura/reflector_impl.h" | |
20 #include "content/browser/aura/software_browser_compositor_output_surface.h" | |
21 #include "content/browser/gpu/browser_gpu_channel_host_factory.h" | |
22 #include "content/browser/gpu/gpu_data_manager_impl.h" | |
23 #include "content/browser/gpu/gpu_surface_tracker.h" | |
24 #include "content/browser/renderer_host/render_widget_host_impl.h" | |
25 #include "content/common/gpu/client/context_provider_command_buffer.h" | |
26 #include "content/common/gpu/client/gl_helper.h" | |
27 #include "content/common/gpu/client/gpu_channel_host.h" | |
28 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" | |
29 #include "content/common/gpu/gpu_process_launch_causes.h" | |
30 #include "gpu/GLES2/gl2extchromium.h" | |
31 #include "gpu/command_buffer/client/gles2_interface.h" | |
32 #include "third_party/khronos/GLES2/gl2.h" | |
33 #include "ui/compositor/compositor.h" | |
34 #include "ui/compositor/compositor_switches.h" | |
35 #include "ui/gfx/native_widget_types.h" | |
36 #include "ui/gfx/size.h" | |
37 | |
38 #if defined(OS_WIN) | |
39 #include "content/browser/aura/software_output_device_win.h" | |
40 #include "ui/surface/accelerated_surface_win.h" | |
41 #elif defined(USE_OZONE) | |
42 #include "content/browser/aura/software_output_device_ozone.h" | |
43 #elif defined(USE_X11) | |
44 #include "content/browser/aura/software_output_device_x11.h" | |
45 #endif | |
46 | |
47 using cc::ContextProvider; | |
48 using gpu::gles2::GLES2Interface; | |
49 | |
50 namespace content { | |
51 | |
52 struct GpuProcessTransportFactory::PerCompositorData { | |
53 int surface_id; | |
54 #if defined(OS_WIN) | |
55 scoped_ptr<AcceleratedSurface> accelerated_surface; | |
56 #endif | |
57 scoped_refptr<ReflectorImpl> reflector; | |
58 }; | |
59 | |
60 class OwnedTexture : public ui::Texture, ImageTransportFactoryObserver { | |
61 public: | |
62 OwnedTexture(const scoped_refptr<ContextProvider>& provider, | |
63 const gfx::Size& size, | |
64 float device_scale_factor, | |
65 GLuint texture_id) | |
66 : ui::Texture(true, size, device_scale_factor), | |
67 provider_(provider), | |
68 texture_id_(texture_id) { | |
69 ImageTransportFactory::GetInstance()->AddObserver(this); | |
70 } | |
71 | |
72 // ui::Texture overrides: | |
73 virtual unsigned int PrepareTexture() OVERRIDE { | |
74 // It's possible that we may have lost the context owning our | |
75 // texture but not yet fired the OnLostResources callback, so poll to see if | |
76 // it's still valid. | |
77 if (provider_ && provider_->IsContextLost()) | |
78 texture_id_ = 0u; | |
79 return texture_id_; | |
80 } | |
81 | |
82 // ImageTransportFactory overrides: | |
83 virtual void OnLostResources() OVERRIDE { | |
84 DeleteTexture(); | |
85 provider_ = NULL; | |
86 } | |
87 | |
88 protected: | |
89 virtual ~OwnedTexture() { | |
90 ImageTransportFactory::GetInstance()->RemoveObserver(this); | |
91 DeleteTexture(); | |
92 } | |
93 | |
94 protected: | |
95 void DeleteTexture() { | |
96 if (texture_id_) { | |
97 provider_->ContextGL()->DeleteTextures(1, &texture_id_); | |
98 texture_id_ = 0; | |
99 } | |
100 } | |
101 | |
102 scoped_refptr<cc::ContextProvider> provider_; | |
103 GLuint texture_id_; | |
104 | |
105 private: | |
106 DISALLOW_COPY_AND_ASSIGN(OwnedTexture); | |
107 }; | |
108 | |
109 class ImageTransportClientTexture : public OwnedTexture { | |
110 public: | |
111 ImageTransportClientTexture(const scoped_refptr<ContextProvider>& provider, | |
112 float device_scale_factor, | |
113 GLuint texture_id) | |
114 : OwnedTexture(provider, | |
115 gfx::Size(0, 0), | |
116 device_scale_factor, | |
117 texture_id) {} | |
118 | |
119 virtual void Consume(const std::string& mailbox_name, | |
120 const gfx::Size& new_size) OVERRIDE { | |
121 DCHECK(mailbox_name.size() == GL_MAILBOX_SIZE_CHROMIUM); | |
122 mailbox_name_ = mailbox_name; | |
123 if (mailbox_name.empty()) | |
124 return; | |
125 | |
126 DCHECK(provider_ && texture_id_); | |
127 GLES2Interface* gl = provider_->ContextGL(); | |
128 gl->BindTexture(GL_TEXTURE_2D, texture_id_); | |
129 gl->ConsumeTextureCHROMIUM( | |
130 GL_TEXTURE_2D, reinterpret_cast<const GLbyte*>(mailbox_name.c_str())); | |
131 size_ = new_size; | |
132 gl->ShallowFlushCHROMIUM(); | |
133 } | |
134 | |
135 virtual std::string Produce() OVERRIDE { return mailbox_name_; } | |
136 | |
137 protected: | |
138 virtual ~ImageTransportClientTexture() {} | |
139 | |
140 private: | |
141 std::string mailbox_name_; | |
142 DISALLOW_COPY_AND_ASSIGN(ImageTransportClientTexture); | |
143 }; | |
144 | |
145 GpuProcessTransportFactory::GpuProcessTransportFactory() | |
146 : callback_factory_(this) { | |
147 output_surface_proxy_ = new BrowserCompositorOutputSurfaceProxy( | |
148 &output_surface_map_); | |
149 } | |
150 | |
151 GpuProcessTransportFactory::~GpuProcessTransportFactory() { | |
152 DCHECK(per_compositor_data_.empty()); | |
153 | |
154 // Make sure the lost context callback doesn't try to run during destruction. | |
155 callback_factory_.InvalidateWeakPtrs(); | |
156 } | |
157 | |
158 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> | |
159 GpuProcessTransportFactory::CreateOffscreenCommandBufferContext() { | |
160 return CreateContextCommon(0); | |
161 } | |
162 | |
163 scoped_ptr<cc::SoftwareOutputDevice> CreateSoftwareOutputDevice( | |
164 ui::Compositor* compositor) { | |
165 #if defined(OS_WIN) | |
166 return scoped_ptr<cc::SoftwareOutputDevice>(new SoftwareOutputDeviceWin( | |
167 compositor)); | |
168 #elif defined(USE_OZONE) | |
169 return scoped_ptr<cc::SoftwareOutputDevice>(new SoftwareOutputDeviceOzone( | |
170 compositor)); | |
171 #elif defined(USE_X11) | |
172 return scoped_ptr<cc::SoftwareOutputDevice>(new SoftwareOutputDeviceX11( | |
173 compositor)); | |
174 #endif | |
175 | |
176 NOTREACHED(); | |
177 return scoped_ptr<cc::SoftwareOutputDevice>(); | |
178 } | |
179 | |
180 scoped_ptr<cc::OutputSurface> GpuProcessTransportFactory::CreateOutputSurface( | |
181 ui::Compositor* compositor, bool software_fallback) { | |
182 PerCompositorData* data = per_compositor_data_[compositor]; | |
183 if (!data) | |
184 data = CreatePerCompositorData(compositor); | |
185 | |
186 scoped_refptr<ContextProviderCommandBuffer> context_provider; | |
187 | |
188 // Software fallback does not happen on Chrome OS. | |
189 #if defined(OS_CHROMEOS) | |
190 software_fallback = false; | |
191 #endif | |
192 | |
193 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
194 if (!command_line->HasSwitch(switches::kUIEnableSoftwareCompositing) && | |
195 !software_fallback) { | |
196 context_provider = ContextProviderCommandBuffer::Create( | |
197 GpuProcessTransportFactory::CreateContextCommon(data->surface_id), | |
198 "Compositor"); | |
199 } | |
200 | |
201 UMA_HISTOGRAM_BOOLEAN("Aura.CreatedGpuBrowserCompositor", !!context_provider); | |
202 | |
203 if (!context_provider.get()) { | |
204 if (ui::Compositor::WasInitializedWithThread()) { | |
205 LOG(FATAL) << "Failed to create UI context, but can't use software" | |
206 " compositing with browser threaded compositing. Aborting."; | |
207 } | |
208 | |
209 scoped_ptr<SoftwareBrowserCompositorOutputSurface> surface( | |
210 new SoftwareBrowserCompositorOutputSurface( | |
211 output_surface_proxy_, | |
212 CreateSoftwareOutputDevice(compositor), | |
213 per_compositor_data_[compositor]->surface_id, | |
214 &output_surface_map_, | |
215 base::MessageLoopProxy::current().get(), | |
216 compositor->AsWeakPtr())); | |
217 return surface.PassAs<cc::OutputSurface>(); | |
218 } | |
219 | |
220 scoped_refptr<base::SingleThreadTaskRunner> compositor_thread_task_runner = | |
221 ui::Compositor::GetCompositorMessageLoop(); | |
222 if (!compositor_thread_task_runner.get()) | |
223 compositor_thread_task_runner = base::MessageLoopProxy::current(); | |
224 | |
225 // Here we know the GpuProcessHost has been set up, because we created a | |
226 // context. | |
227 output_surface_proxy_->ConnectToGpuProcessHost( | |
228 compositor_thread_task_runner.get()); | |
229 | |
230 scoped_ptr<BrowserCompositorOutputSurface> surface( | |
231 new GpuBrowserCompositorOutputSurface( | |
232 context_provider, | |
233 per_compositor_data_[compositor]->surface_id, | |
234 &output_surface_map_, | |
235 base::MessageLoopProxy::current().get(), | |
236 compositor->AsWeakPtr())); | |
237 if (data->reflector.get()) { | |
238 data->reflector->CreateSharedTexture(); | |
239 data->reflector->AttachToOutputSurface(surface.get()); | |
240 } | |
241 | |
242 return surface.PassAs<cc::OutputSurface>(); | |
243 } | |
244 | |
245 scoped_refptr<ui::Reflector> GpuProcessTransportFactory::CreateReflector( | |
246 ui::Compositor* source, | |
247 ui::Layer* target) { | |
248 PerCompositorData* data = per_compositor_data_[source]; | |
249 DCHECK(data); | |
250 | |
251 if (data->reflector.get()) | |
252 RemoveObserver(data->reflector.get()); | |
253 | |
254 data->reflector = new ReflectorImpl( | |
255 source, target, &output_surface_map_, data->surface_id); | |
256 AddObserver(data->reflector.get()); | |
257 return data->reflector; | |
258 } | |
259 | |
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 RemoveObserver(reflector_impl); | |
268 data->reflector->Shutdown(); | |
269 data->reflector = NULL; | |
270 } | |
271 | |
272 void GpuProcessTransportFactory::RemoveCompositor(ui::Compositor* compositor) { | |
273 PerCompositorDataMap::iterator it = per_compositor_data_.find(compositor); | |
274 if (it == per_compositor_data_.end()) | |
275 return; | |
276 PerCompositorData* data = it->second; | |
277 DCHECK(data); | |
278 GpuSurfaceTracker::Get()->RemoveSurface(data->surface_id); | |
279 delete data; | |
280 per_compositor_data_.erase(it); | |
281 if (per_compositor_data_.empty()) | |
282 gl_helper_.reset(); | |
283 } | |
284 | |
285 bool GpuProcessTransportFactory::DoesCreateTestContexts() { return false; } | |
286 | |
287 ui::ContextFactory* GpuProcessTransportFactory::AsContextFactory() { | |
288 return this; | |
289 } | |
290 | |
291 gfx::GLSurfaceHandle GpuProcessTransportFactory::GetSharedSurfaceHandle() { | |
292 // TODO(sievers): crbug.com/329737 | |
293 // Creating the context here hurts startup performance. | |
294 // Remove this once all tests are happy. | |
295 SharedMainThreadContextProvider(); | |
296 | |
297 gfx::GLSurfaceHandle handle = gfx::GLSurfaceHandle( | |
298 gfx::kNullPluginWindow, gfx::TEXTURE_TRANSPORT); | |
299 handle.parent_client_id = | |
300 BrowserGpuChannelHostFactory::instance()->GetGpuChannelId(); | |
301 return handle; | |
302 } | |
303 | |
304 scoped_refptr<ui::Texture> GpuProcessTransportFactory::CreateTransportClient( | |
305 float device_scale_factor) { | |
306 scoped_refptr<cc::ContextProvider> provider = | |
307 SharedMainThreadContextProvider(); | |
308 if (!provider.get()) | |
309 return NULL; | |
310 GLuint texture_id = 0; | |
311 provider->ContextGL()->GenTextures(1, &texture_id); | |
312 scoped_refptr<ImageTransportClientTexture> image( | |
313 new ImageTransportClientTexture( | |
314 provider, device_scale_factor, texture_id)); | |
315 return image; | |
316 } | |
317 | |
318 scoped_refptr<ui::Texture> GpuProcessTransportFactory::CreateOwnedTexture( | |
319 const gfx::Size& size, | |
320 float device_scale_factor, | |
321 unsigned int texture_id) { | |
322 scoped_refptr<cc::ContextProvider> provider = | |
323 SharedMainThreadContextProvider(); | |
324 if (!provider.get()) | |
325 return NULL; | |
326 scoped_refptr<OwnedTexture> image(new OwnedTexture( | |
327 provider, size, device_scale_factor, texture_id)); | |
328 return image; | |
329 } | |
330 | |
331 GLHelper* GpuProcessTransportFactory::GetGLHelper() { | |
332 if (!gl_helper_) { | |
333 scoped_refptr<cc::ContextProvider> provider = | |
334 SharedMainThreadContextProvider(); | |
335 if (provider.get()) | |
336 gl_helper_.reset(new GLHelper(provider->ContextGL(), | |
337 provider->ContextSupport())); | |
338 } | |
339 return gl_helper_.get(); | |
340 } | |
341 | |
342 void GpuProcessTransportFactory::AddObserver( | |
343 ImageTransportFactoryObserver* observer) { | |
344 observer_list_.AddObserver(observer); | |
345 } | |
346 | |
347 void GpuProcessTransportFactory::RemoveObserver( | |
348 ImageTransportFactoryObserver* observer) { | |
349 observer_list_.RemoveObserver(observer); | |
350 } | |
351 | |
352 scoped_refptr<cc::ContextProvider> | |
353 GpuProcessTransportFactory::OffscreenCompositorContextProvider() { | |
354 // Don't check for DestroyedOnMainThread() here. We hear about context | |
355 // loss for this context through the lost context callback. If the context | |
356 // is lost, we want to leave this ContextProvider available until the lost | |
357 // context notification is sent to the ImageTransportFactoryObserver clients. | |
358 if (offscreen_compositor_contexts_.get()) | |
359 return offscreen_compositor_contexts_; | |
360 | |
361 offscreen_compositor_contexts_ = ContextProviderCommandBuffer::Create( | |
362 GpuProcessTransportFactory::CreateOffscreenCommandBufferContext(), | |
363 "Compositor-Offscreen"); | |
364 | |
365 return offscreen_compositor_contexts_; | |
366 } | |
367 | |
368 scoped_refptr<cc::ContextProvider> | |
369 GpuProcessTransportFactory::SharedMainThreadContextProvider() { | |
370 if (shared_main_thread_contexts_.get()) | |
371 return shared_main_thread_contexts_; | |
372 | |
373 if (ui::Compositor::WasInitializedWithThread()) { | |
374 // In threaded compositing mode, we have to create our own context for the | |
375 // main thread since the compositor's context will be bound to the | |
376 // compositor thread. | |
377 shared_main_thread_contexts_ = ContextProviderCommandBuffer::Create( | |
378 GpuProcessTransportFactory::CreateOffscreenCommandBufferContext(), | |
379 "Offscreen-MainThread"); | |
380 } else { | |
381 // In single threaded compositing mode, we can just reuse the compositor's | |
382 // shared context. | |
383 shared_main_thread_contexts_ = | |
384 static_cast<ContextProviderCommandBuffer*>( | |
385 OffscreenCompositorContextProvider().get()); | |
386 } | |
387 | |
388 if (shared_main_thread_contexts_) { | |
389 shared_main_thread_contexts_->SetLostContextCallback( | |
390 base::Bind(&GpuProcessTransportFactory:: | |
391 OnLostMainThreadSharedContextInsideCallback, | |
392 callback_factory_.GetWeakPtr())); | |
393 if (!shared_main_thread_contexts_->BindToCurrentThread()) { | |
394 shared_main_thread_contexts_ = NULL; | |
395 offscreen_compositor_contexts_ = NULL; | |
396 } | |
397 } | |
398 return shared_main_thread_contexts_; | |
399 } | |
400 | |
401 GpuProcessTransportFactory::PerCompositorData* | |
402 GpuProcessTransportFactory::CreatePerCompositorData( | |
403 ui::Compositor* compositor) { | |
404 DCHECK(!per_compositor_data_[compositor]); | |
405 | |
406 gfx::AcceleratedWidget widget = compositor->widget(); | |
407 GpuSurfaceTracker* tracker = GpuSurfaceTracker::Get(); | |
408 | |
409 PerCompositorData* data = new PerCompositorData; | |
410 data->surface_id = tracker->AddSurfaceForNativeWidget(widget); | |
411 #if defined(OS_WIN) | |
412 if (GpuDataManagerImpl::GetInstance()->IsUsingAcceleratedSurface()) | |
413 data->accelerated_surface.reset(new AcceleratedSurface(widget)); | |
414 #endif | |
415 tracker->SetSurfaceHandle( | |
416 data->surface_id, | |
417 gfx::GLSurfaceHandle(widget, gfx::NATIVE_DIRECT)); | |
418 | |
419 per_compositor_data_[compositor] = data; | |
420 | |
421 return data; | |
422 } | |
423 | |
424 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> | |
425 GpuProcessTransportFactory::CreateContextCommon(int surface_id) { | |
426 if (!GpuDataManagerImpl::GetInstance()->CanUseGpuBrowserCompositor()) | |
427 return scoped_ptr<WebGraphicsContext3DCommandBufferImpl>(); | |
428 blink::WebGraphicsContext3D::Attributes attrs; | |
429 attrs.shareResources = true; | |
430 attrs.depth = false; | |
431 attrs.stencil = false; | |
432 attrs.antialias = false; | |
433 attrs.noAutomaticFlushes = true; | |
434 CauseForGpuLaunch cause = | |
435 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE; | |
436 scoped_refptr<GpuChannelHost> gpu_channel_host( | |
437 BrowserGpuChannelHostFactory::instance()->EstablishGpuChannelSync(cause)); | |
438 if (!gpu_channel_host) | |
439 return scoped_ptr<WebGraphicsContext3DCommandBufferImpl>(); | |
440 GURL url("chrome://gpu/GpuProcessTransportFactory::CreateContextCommon"); | |
441 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context( | |
442 new WebGraphicsContext3DCommandBufferImpl( | |
443 surface_id, | |
444 url, | |
445 gpu_channel_host.get(), | |
446 attrs, | |
447 false, | |
448 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits())); | |
449 return context.Pass(); | |
450 } | |
451 | |
452 void GpuProcessTransportFactory::OnLostMainThreadSharedContextInsideCallback() { | |
453 base::MessageLoop::current()->PostTask( | |
454 FROM_HERE, | |
455 base::Bind(&GpuProcessTransportFactory::OnLostMainThreadSharedContext, | |
456 callback_factory_.GetWeakPtr())); | |
457 } | |
458 | |
459 void GpuProcessTransportFactory::OnLostMainThreadSharedContext() { | |
460 LOG(ERROR) << "Lost UI shared context."; | |
461 | |
462 // Keep old resources around while we call the observers, but ensure that | |
463 // new resources are created if needed. | |
464 // Kill shared contexts for both threads in tandem so they are always in | |
465 // the same share group. | |
466 scoped_refptr<cc::ContextProvider> lost_offscreen_compositor_contexts = | |
467 offscreen_compositor_contexts_; | |
468 scoped_refptr<cc::ContextProvider> lost_shared_main_thread_contexts = | |
469 shared_main_thread_contexts_; | |
470 offscreen_compositor_contexts_ = NULL; | |
471 shared_main_thread_contexts_ = NULL; | |
472 | |
473 scoped_ptr<GLHelper> lost_gl_helper = gl_helper_.Pass(); | |
474 | |
475 FOR_EACH_OBSERVER(ImageTransportFactoryObserver, | |
476 observer_list_, | |
477 OnLostResources()); | |
478 | |
479 // Kill things that use the shared context before killing the shared context. | |
480 lost_gl_helper.reset(); | |
481 lost_offscreen_compositor_contexts = NULL; | |
482 lost_shared_main_thread_contexts = NULL; | |
483 } | |
484 | |
485 } // namespace content | |
OLD | NEW |