Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: content/browser/renderer_host/image_transport_factory.cc

Issue 11195011: Send vsync timebase updates to the browser compositor (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased, and added a missing chunk Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/renderer_host/image_transport_factory.h" 5 #include "content/browser/renderer_host/image_transport_factory.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <map> 8 #include <map>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/observer_list.h" 13 #include "base/observer_list.h"
14 #include "base/threading/non_thread_safe.h"
14 #include "content/browser/gpu/browser_gpu_channel_host_factory.h" 15 #include "content/browser/gpu/browser_gpu_channel_host_factory.h"
15 #include "content/browser/gpu/gpu_data_manager_impl.h" 16 #include "content/browser/gpu/gpu_data_manager_impl.h"
17 #include "content/browser/gpu/gpu_process_host.h"
16 #include "content/browser/gpu/gpu_surface_tracker.h" 18 #include "content/browser/gpu/gpu_surface_tracker.h"
17 #include "content/common/gpu/client/gl_helper.h" 19 #include "content/common/gpu/client/gl_helper.h"
18 #include "content/common/gpu/client/gpu_channel_host.h" 20 #include "content/common/gpu/client/gpu_channel_host.h"
19 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" 21 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
22 #include "content/common/gpu/gpu_messages.h"
20 #include "content/common/gpu/gpu_process_launch_causes.h" 23 #include "content/common/gpu/gpu_process_launch_causes.h"
21 #include "content/common/webkitplatformsupport_impl.h" 24 #include "content/common/webkitplatformsupport_impl.h"
22 #include "content/public/common/content_switches.h" 25 #include "content/public/common/content_switches.h"
23 #include "gpu/ipc/command_buffer_proxy.h" 26 #include "gpu/ipc/command_buffer_proxy.h"
27 #include "third_party/WebKit/Source/Platform/chromium/public/WebCompositorOutput Surface.h"
28 #include "third_party/WebKit/Source/Platform/chromium/public/WebCompositorOutput SurfaceClient.h"
24 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebGraphicsC ontext3D.h" 29 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebGraphicsC ontext3D.h"
25 #include "ui/compositor/compositor.h" 30 #include "ui/compositor/compositor.h"
26 #include "ui/compositor/compositor_setup.h" 31 #include "ui/compositor/compositor_setup.h"
27 #include "ui/compositor/test_web_graphics_context_3d.h" 32 #include "ui/compositor/test_web_graphics_context_3d.h"
28 #include "ui/gfx/native_widget_types.h" 33 #include "ui/gfx/native_widget_types.h"
29 #include "ui/gfx/size.h" 34 #include "ui/gfx/size.h"
30 35
31 #if defined(OS_WIN) 36 #if defined(OS_WIN)
32 #include "ui/surface/accelerated_surface_win.h" 37 #include "ui/surface/accelerated_surface_win.h"
33 #endif 38 #endif
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 } 219 }
215 220
216 private: 221 private:
217 void OnLostContext(); 222 void OnLostContext();
218 ui::Compositor* compositor_; 223 ui::Compositor* compositor_;
219 GpuProcessTransportFactory* factory_; 224 GpuProcessTransportFactory* factory_;
220 225
221 DISALLOW_COPY_AND_ASSIGN(CompositorSwapClient); 226 DISALLOW_COPY_AND_ASSIGN(CompositorSwapClient);
222 }; 227 };
223 228
229 class BrowserCompositorOutputSurface;
230
231 // Directs vsync updates to the appropriate BrowserCompositorOutputSurface.
232 class BrowserCompositorOutputSurfaceProxy :
233 public base::RefCountedThreadSafe<BrowserCompositorOutputSurfaceProxy> {
234 public:
235 BrowserCompositorOutputSurfaceProxy()
236 : message_handler_set_(false) {
237 }
238
239 void AddSurface(BrowserCompositorOutputSurface* surface, int surface_id) {
240 if (!message_handler_set_) {
241 uint32 messages_to_filter[] = {GpuHostMsg_UpdateVSyncParameters::ID};
242 BrowserGpuChannelHostFactory::instance()->SetHandlerForControlMessages(
243 messages_to_filter,
244 arraysize(messages_to_filter),
245 base::Bind(&BrowserCompositorOutputSurfaceProxy::OnMessageReceived,
246 this),
247 MessageLoop::current()->message_loop_proxy());
248 message_handler_set_ = true;
249 }
250 surface_map_.AddWithID(surface, surface_id);
251 }
252
253 void RemoveSurface(int surface_id) {
254 surface_map_.Remove(surface_id);
255 }
256
257 private:
258 void OnMessageReceived(const IPC::Message& message) {
259 IPC_BEGIN_MESSAGE_MAP(BrowserCompositorOutputSurfaceProxy, message)
260 IPC_MESSAGE_HANDLER(GpuHostMsg_UpdateVSyncParameters,
261 OnUpdateVSyncParameters);
262 IPC_END_MESSAGE_MAP()
263 }
264
265 void OnUpdateVSyncParameters(int surface_id,
266 base::TimeTicks timebase,
267 base::TimeDelta interval);
268
269 friend class
270 base::RefCountedThreadSafe<BrowserCompositorOutputSurfaceProxy>;
271 ~BrowserCompositorOutputSurfaceProxy() {}
272 IDMap<BrowserCompositorOutputSurface> surface_map_;
273 bool message_handler_set_;
274
275 DISALLOW_COPY_AND_ASSIGN(BrowserCompositorOutputSurfaceProxy);
276 };
277
278
279 // Adapts a WebGraphicsContext3DCommandBufferImpl into a
280 // WebCompositorOutputSurface that also handles vsync parameter updates
281 // arriving from the GPU process.
282 class BrowserCompositorOutputSurface :
283 public WebKit::WebCompositorOutputSurface,
284 public base::NonThreadSafe {
285 public:
286 explicit BrowserCompositorOutputSurface(
287 WebGraphicsContext3DCommandBufferImpl* context,
288 int surface_id,
289 BrowserCompositorOutputSurfaceProxy* output_surface_proxy)
290 : context3D_(context)
291 , surface_id_(surface_id)
piman 2012/10/29 20:36:39 nit: in chrome style, the initializer comas go aft
ajuma 2012/10/30 21:04:04 Done.
292 , client_(NULL)
293 , output_surface_proxy_(output_surface_proxy) {
294 DetachFromThread();
295 }
296
297 virtual ~BrowserCompositorOutputSurface() {
298 DCHECK(CalledOnValidThread());
299 if (!client_)
300 return;
301 output_surface_proxy_->RemoveSurface(surface_id_);
302 }
303
304 virtual bool bindToClient(
305 WebKit::WebCompositorOutputSurfaceClient* client) OVERRIDE {
306 DCHECK(CalledOnValidThread());
307 DCHECK(client);
308 DCHECK(!client_);
309 if (context3D_.get()) {
310 if (!context3D_->makeContextCurrent())
311 return false;
312 }
313
314 client_ = client;
315 output_surface_proxy_->AddSurface(this, surface_id_);
316 return true;
317 }
318
319 virtual const Capabilities& capabilities() const OVERRIDE {
320 DCHECK(CalledOnValidThread());
321 return capabilities_;
322 }
323
324 virtual WebKit::WebGraphicsContext3D* context3D() const OVERRIDE {
325 DCHECK(CalledOnValidThread());
326 return context3D_.get();
327 }
328
329 virtual void sendFrameToParentCompositor(
330 const WebKit::WebCompositorFrame&) OVERRIDE {
331 }
332
333 void OnUpdateVSyncParameters(
334 base::TimeTicks timebase, base::TimeDelta interval) {
335 DCHECK(CalledOnValidThread());
336 DCHECK(client_);
337 double monotonicTimebase = timebase.ToInternalValue() /
338 static_cast<double>(base::Time::kMicrosecondsPerSecond);
339 double intervalInSeconds = interval.ToInternalValue() /
340 static_cast<double>(base::Time::kMicrosecondsPerSecond);
341 client_->onVSyncParametersChanged(monotonicTimebase, intervalInSeconds);
342 }
343
344 private:
345 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context3D_;
346 int surface_id_;
347 Capabilities capabilities_;
348 WebKit::WebCompositorOutputSurfaceClient* client_;
349 scoped_refptr<BrowserCompositorOutputSurfaceProxy> output_surface_proxy_;
350 };
351
352 void BrowserCompositorOutputSurfaceProxy::OnUpdateVSyncParameters(
353 int surface_id, base::TimeTicks timebase, base::TimeDelta interval) {
354 BrowserCompositorOutputSurface* surface = surface_map_.Lookup(surface_id);
355 if (surface)
356 surface->OnUpdateVSyncParameters(timebase, interval);
357 }
358
224 class GpuProcessTransportFactory : 359 class GpuProcessTransportFactory :
225 public ui::ContextFactory, 360 public ui::ContextFactory,
226 public ImageTransportFactory, 361 public ImageTransportFactory,
227 public WebKit::WebGraphicsContext3D::WebGraphicsContextLostCallback { 362 public WebKit::WebGraphicsContext3D::WebGraphicsContextLostCallback {
228 public: 363 public:
229 GpuProcessTransportFactory() 364 GpuProcessTransportFactory()
230 : ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)) { 365 : ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)) {
366 output_surface_proxy_ = new BrowserCompositorOutputSurfaceProxy();
231 } 367 }
232 368
233 virtual ~GpuProcessTransportFactory() { 369 virtual ~GpuProcessTransportFactory() {
234 DCHECK(per_compositor_data_.empty()); 370 DCHECK(per_compositor_data_.empty());
235 } 371 }
236 372
237 virtual WebKit::WebGraphicsContext3D* CreateContext( 373 virtual WebGraphicsContext3DCommandBufferImpl* CreateContext(
238 ui::Compositor* compositor) OVERRIDE { 374 ui::Compositor* compositor) OVERRIDE {
239 PerCompositorData* data = per_compositor_data_[compositor]; 375 PerCompositorData* data = per_compositor_data_[compositor];
240 if (!data) 376 if (!data)
241 data = CreatePerCompositorData(compositor); 377 data = CreatePerCompositorData(compositor);
242 return CreateContextCommon(data->swap_client->AsWeakPtr(), 378 return CreateContextCommon(data->swap_client->AsWeakPtr(),
243 data->surface_id); 379 data->surface_id);
244 } 380 }
245 381
246 virtual WebGraphicsContext3DCommandBufferImpl* CreateOffscreenContext() 382 virtual WebGraphicsContext3DCommandBufferImpl* CreateOffscreenContext()
247 OVERRIDE { 383 OVERRIDE {
248 base::WeakPtr<WebGraphicsContext3DSwapBuffersClient> swap_client; 384 base::WeakPtr<WebGraphicsContext3DSwapBuffersClient> swap_client;
249 return CreateContextCommon(swap_client, 0); 385 return CreateContextCommon(swap_client, 0);
250 } 386 }
251 387
388 virtual WebKit::WebCompositorOutputSurface* CreateOutputSurface(
389 ui::Compositor* compositor) OVERRIDE {
390 WebGraphicsContext3DCommandBufferImpl* context = CreateContext(compositor);
391 return new BrowserCompositorOutputSurface(
392 context,
393 per_compositor_data_[compositor]->surface_id,
394 output_surface_proxy_);
395 }
396
252 virtual void RemoveCompositor(ui::Compositor* compositor) OVERRIDE { 397 virtual void RemoveCompositor(ui::Compositor* compositor) OVERRIDE {
253 PerCompositorDataMap::iterator it = per_compositor_data_.find(compositor); 398 PerCompositorDataMap::iterator it = per_compositor_data_.find(compositor);
254 if (it == per_compositor_data_.end()) 399 if (it == per_compositor_data_.end())
255 return; 400 return;
256 PerCompositorData* data = it->second; 401 PerCompositorData* data = it->second;
257 DCHECK(data); 402 DCHECK(data);
258 GpuSurfaceTracker::Get()->RemoveSurface(data->surface_id); 403 GpuSurfaceTracker::Get()->RemoveSurface(data->surface_id);
259 delete data; 404 delete data;
260 per_compositor_data_.erase(it); 405 per_compositor_data_.erase(it);
261 if (per_compositor_data_.empty()) { 406 if (per_compositor_data_.empty()) {
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 observer_list_, 607 observer_list_,
463 OnLostResources()); 608 OnLostResources());
464 } 609 }
465 610
466 typedef std::map<ui::Compositor*, PerCompositorData*> PerCompositorDataMap; 611 typedef std::map<ui::Compositor*, PerCompositorData*> PerCompositorDataMap;
467 PerCompositorDataMap per_compositor_data_; 612 PerCompositorDataMap per_compositor_data_;
468 scoped_ptr<GLHelper> gl_helper_; 613 scoped_ptr<GLHelper> gl_helper_;
469 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> shared_context_; 614 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> shared_context_;
470 ObserverList<ImageTransportFactoryObserver> observer_list_; 615 ObserverList<ImageTransportFactoryObserver> observer_list_;
471 base::WeakPtrFactory<GpuProcessTransportFactory> callback_factory_; 616 base::WeakPtrFactory<GpuProcessTransportFactory> callback_factory_;
617 scoped_refptr<BrowserCompositorOutputSurfaceProxy> output_surface_proxy_;
472 618
473 DISALLOW_COPY_AND_ASSIGN(GpuProcessTransportFactory); 619 DISALLOW_COPY_AND_ASSIGN(GpuProcessTransportFactory);
474 }; 620 };
475 621
476 void CompositorSwapClient::OnLostContext() { 622 void CompositorSwapClient::OnLostContext() {
477 factory_->OnLostContext(compositor_); 623 factory_->OnLostContext(compositor_);
478 // Note: previous line destroyed this. Don't access members from now on. 624 // Note: previous line destroyed this. Don't access members from now on.
479 } 625 }
480 626
481 WebKit::WebGraphicsContext3D* CreateTestContext() { 627 WebKit::WebGraphicsContext3D* CreateTestContext() {
(...skipping 25 matching lines...) Expand all
507 void ImageTransportFactory::Terminate() { 653 void ImageTransportFactory::Terminate() {
508 ui::ContextFactory::SetInstance(NULL); 654 ui::ContextFactory::SetInstance(NULL);
509 delete g_factory; 655 delete g_factory;
510 g_factory = NULL; 656 g_factory = NULL;
511 } 657 }
512 658
513 // static 659 // static
514 ImageTransportFactory* ImageTransportFactory::GetInstance() { 660 ImageTransportFactory* ImageTransportFactory::GetInstance() {
515 return g_factory; 661 return g_factory;
516 } 662 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698