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

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

Issue 12212007: cc: Route offscreen context creation for compositor to the browser. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove the GrContextProvider::ScopedContexts guard classes Created 7 years, 10 months 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"
(...skipping 18 matching lines...) Expand all
29 #include "gpu/GLES2/gl2extchromium.h" 29 #include "gpu/GLES2/gl2extchromium.h"
30 #include "gpu/ipc/command_buffer_proxy.h" 30 #include "gpu/ipc/command_buffer_proxy.h"
31 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3 D.h" 31 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3 D.h"
32 #include "third_party/khronos/GLES2/gl2.h" 32 #include "third_party/khronos/GLES2/gl2.h"
33 #include "third_party/khronos/GLES2/gl2ext.h" 33 #include "third_party/khronos/GLES2/gl2ext.h"
34 #include "ui/compositor/compositor.h" 34 #include "ui/compositor/compositor.h"
35 #include "ui/compositor/compositor_setup.h" 35 #include "ui/compositor/compositor_setup.h"
36 #include "ui/compositor/test_web_graphics_context_3d.h" 36 #include "ui/compositor/test_web_graphics_context_3d.h"
37 #include "ui/gfx/native_widget_types.h" 37 #include "ui/gfx/native_widget_types.h"
38 #include "ui/gfx/size.h" 38 #include "ui/gfx/size.h"
39 #include "webkit/gpu/grcontext_for_webgraphicscontext3d.h"
39 40
40 #if defined(OS_WIN) 41 #if defined(OS_WIN)
41 #include "ui/surface/accelerated_surface_win.h" 42 #include "ui/surface/accelerated_surface_win.h"
42 #endif 43 #endif
43 44
44 namespace content { 45 namespace content {
45 namespace { 46 namespace {
46 47
47 ImageTransportFactory* g_factory; 48 ImageTransportFactory* g_factory;
48 49
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 } 417 }
417 418
418 virtual ui::ContextFactory* AsContextFactory() OVERRIDE { 419 virtual ui::ContextFactory* AsContextFactory() OVERRIDE {
419 return this; 420 return this;
420 } 421 }
421 422
422 virtual gfx::GLSurfaceHandle CreateSharedSurfaceHandle() OVERRIDE { 423 virtual gfx::GLSurfaceHandle CreateSharedSurfaceHandle() OVERRIDE {
423 CreateSharedContextLazy(); 424 CreateSharedContextLazy();
424 gfx::GLSurfaceHandle handle = gfx::GLSurfaceHandle( 425 gfx::GLSurfaceHandle handle = gfx::GLSurfaceHandle(
425 gfx::kNullPluginWindow, true); 426 gfx::kNullPluginWindow, true);
426 handle.parent_gpu_process_id = shared_context_->GetGPUProcessID(); 427 handle.parent_gpu_process_id =
427 handle.parent_client_id = shared_context_->GetChannelID(); 428 shared_context_main_thread_->GetGPUProcessID();
429 handle.parent_client_id = shared_context_main_thread_->GetChannelID();
428 430
429 return handle; 431 return handle;
430 } 432 }
431 433
432 virtual void DestroySharedSurfaceHandle( 434 virtual void DestroySharedSurfaceHandle(
433 gfx::GLSurfaceHandle surface) OVERRIDE { 435 gfx::GLSurfaceHandle surface) OVERRIDE {
434 } 436 }
435 437
436 virtual scoped_refptr<ui::Texture> CreateTransportClient( 438 virtual scoped_refptr<ui::Texture> CreateTransportClient(
437 float device_scale_factor) OVERRIDE { 439 float device_scale_factor) OVERRIDE {
438 if (!shared_context_.get()) 440 if (!shared_context_main_thread_.get())
439 return NULL; 441 return NULL;
440 scoped_refptr<ImageTransportClientTexture> image( 442 scoped_refptr<ImageTransportClientTexture> image(
441 new ImageTransportClientTexture(shared_context_.get(), 443 new ImageTransportClientTexture(shared_context_main_thread_.get(),
442 device_scale_factor)); 444 device_scale_factor));
443 return image; 445 return image;
444 } 446 }
445 447
446 virtual scoped_refptr<ui::Texture> CreateOwnedTexture( 448 virtual scoped_refptr<ui::Texture> CreateOwnedTexture(
447 const gfx::Size& size, 449 const gfx::Size& size,
448 float device_scale_factor, 450 float device_scale_factor,
449 unsigned int texture_id) OVERRIDE { 451 unsigned int texture_id) OVERRIDE {
450 if (!shared_context_.get()) 452 if (!shared_context_main_thread_.get())
451 return NULL; 453 return NULL;
452 scoped_refptr<OwnedTexture> image( 454 scoped_refptr<OwnedTexture> image(new OwnedTexture(
453 new OwnedTexture(shared_context_.get(), size, device_scale_factor, 455 shared_context_main_thread_.get(),
454 texture_id)); 456 size,
457 device_scale_factor,
458 texture_id));
455 return image; 459 return image;
456 } 460 }
457 461
458 virtual GLHelper* GetGLHelper() OVERRIDE { 462 virtual GLHelper* GetGLHelper() OVERRIDE {
459 if (!gl_helper_.get()) { 463 if (!gl_helper_.get()) {
460 CreateSharedContextLazy(); 464 CreateSharedContextLazy();
461 WebKit::WebGraphicsContext3D* context_for_thread = 465 WebKit::WebGraphicsContext3D* context_for_thread =
462 CreateOffscreenContext(); 466 CreateOffscreenContext();
463 if (!context_for_thread) 467 if (!context_for_thread)
464 return NULL; 468 return NULL;
465 gl_helper_.reset(new GLHelper(shared_context_.get(), 469 gl_helper_.reset(new GLHelper(shared_context_main_thread_.get(),
466 context_for_thread)); 470 context_for_thread));
467 } 471 }
468 return gl_helper_.get(); 472 return gl_helper_.get();
469 } 473 }
470 474
471 virtual uint32 InsertSyncPoint() OVERRIDE { 475 virtual uint32 InsertSyncPoint() OVERRIDE {
472 if (!shared_context_.get()) 476 if (!shared_context_main_thread_.get())
473 return 0; 477 return 0;
474 return shared_context_->insertSyncPoint(); 478 return shared_context_main_thread_->insertSyncPoint();
475 } 479 }
476 480
477 virtual void AddObserver(ImageTransportFactoryObserver* observer) OVERRIDE { 481 virtual void AddObserver(ImageTransportFactoryObserver* observer) OVERRIDE {
478 observer_list_.AddObserver(observer); 482 observer_list_.AddObserver(observer);
479 } 483 }
480 484
481 virtual void RemoveObserver( 485 virtual void RemoveObserver(
482 ImageTransportFactoryObserver* observer) OVERRIDE { 486 ImageTransportFactoryObserver* observer) OVERRIDE {
483 observer_list_.RemoveObserver(observer); 487 observer_list_.RemoveObserver(observer);
484 } 488 }
485 489
486 // WebGraphicsContextLostCallback implementation, called for the shared 490 // WebGraphicsContextLostCallback implementation, called for the main thread
487 // context. 491 // shared context.
488 virtual void onContextLost() { 492 virtual void onContextLost() {
489 MessageLoop::current()->PostTask( 493 MessageLoop::current()->PostTask(
490 FROM_HERE, 494 FROM_HERE,
491 base::Bind(&GpuProcessTransportFactory::OnLostSharedContext, 495 base::Bind(&GpuProcessTransportFactory::OnLostMainThreadSharedContext,
492 callback_factory_.GetWeakPtr())); 496 callback_factory_.GetWeakPtr()));
493 } 497 }
494 498
495 void OnLostContext(ui::Compositor* compositor) { 499 void OnLostContext(ui::Compositor* compositor) {
496 LOG(ERROR) << "Lost UI compositor context."; 500 LOG(ERROR) << "Lost UI compositor context.";
497 PerCompositorData* data = per_compositor_data_[compositor]; 501 PerCompositorData* data = per_compositor_data_[compositor];
498 DCHECK(data); 502 DCHECK(data);
499 503
500 // Prevent callbacks from other contexts in the same share group from 504 // Prevent callbacks from other contexts in the same share group from
501 // calling us again. 505 // calling us again.
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 factory, 558 factory,
555 swap_client)); 559 swap_client));
556 if (!context->Initialize( 560 if (!context->Initialize(
557 attrs, 561 attrs,
558 false, 562 false,
559 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE)) 563 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE))
560 return NULL; 564 return NULL;
561 return context.release(); 565 return context.release();
562 } 566 }
563 567
568 virtual WebKit::WebGraphicsContext3D* OffscreenContextForMainThread()
569 OVERRIDE {
570 CreateSharedContextLazy();
571 return shared_context_main_thread_.get();
572 }
573
574 virtual WebKit::WebGraphicsContext3D* OffscreenContextForCompositorThread()
575 OVERRIDE {
576 if (shared_context_compositor_thread_)
577 return shared_context_compositor_thread_.get();
578
579 // This context is for another thread. Do not MakeCurrent on this thread,
580 // and any callbacks will not be run on this thread.
581 shared_context_compositor_thread_.reset(CreateOffscreenContext());
582 return shared_context_compositor_thread_.get();
583 }
584
585 class MemoryCallbackProxy :
586 public WebKit::WebGraphicsContext3D::
587 WebGraphicsMemoryAllocationChangedCallbackCHROMIUM {
588 public:
589 MemoryCallbackProxy(
590 WebKit::WebGraphicsContext3D* context3d,
591 webkit::gpu::GrContextForWebGraphicsContext3D* gr_context)
592 : context3d_(context3d),
593 gr_context_(gr_context) {
594 context3d_->setMemoryAllocationChangedCallbackCHROMIUM(this);
595 }
596 virtual ~MemoryCallbackProxy() {
597 context3d_->setMemoryAllocationChangedCallbackCHROMIUM(NULL);
598 }
599
600 private:
601 // WebGraphicsMemoryAllocationChangedCallbackCHROMIUM implementation.
602 virtual void onMemoryAllocationChanged(
603 WebKit::WebGraphicsMemoryAllocation allocation) {
604 gr_context_->SetMemoryLimit(!!allocation.gpuResourceSizeInBytes);
605 }
606
607 WebKit::WebGraphicsContext3D* context3d_;
608 webkit::gpu::GrContextForWebGraphicsContext3D* gr_context_;
609 };
610
611 virtual GrContext* OffscreenGrContextForMainThread() OVERRIDE {
612 if (!grcontext_main_thread_ && shared_context_main_thread_) {
613 grcontext_main_thread_.reset(
614 new webkit::gpu::GrContextForWebGraphicsContext3D(
615 shared_context_main_thread_.get()));
616 memory_callback_proxy_main_thread_.reset(new MemoryCallbackProxy(
617 shared_context_main_thread_.get(), grcontext_main_thread_.get()));
618 }
619 return grcontext_main_thread_->gr_context();
620 }
621
622 virtual GrContext* OffscreenGrContextForCompositorThread()
623 OVERRIDE {
624 if (!grcontext_compositor_thread_ && shared_context_compositor_thread_) {
625 grcontext_compositor_thread_.reset(
626 new webkit::gpu::GrContextForWebGraphicsContext3D(
627 shared_context_compositor_thread_.get()));
628 memory_callback_proxy_compositor_thread_.reset(new MemoryCallbackProxy(
piman 2013/02/13 19:13:31 So, this is called on the main thread, but the Mem
629 shared_context_compositor_thread_.get(),
630 grcontext_compositor_thread_.get()));
631 }
632 return grcontext_compositor_thread_->gr_context();
633 }
634
635 virtual void DestroyOffscreenContext3dForCompositorThread() OVERRIDE {
636 memory_callback_proxy_compositor_thread_.reset();
637 grcontext_compositor_thread_.reset();
638 shared_context_compositor_thread_.reset();
639 }
640
564 void CreateSharedContextLazy() { 641 void CreateSharedContextLazy() {
565 if (shared_context_.get()) 642 if (shared_context_main_thread_.get())
566 return; 643 return;
567 644
568 shared_context_.reset(CreateOffscreenContext()); 645 shared_context_main_thread_.reset(CreateOffscreenContext());
569 if (!shared_context_.get()) { 646 if (!shared_context_main_thread_.get()) {
570 // If we can't recreate contexts, we won't be able to show the UI. Better 647 // If we can't recreate contexts, we won't be able to show the UI. Better
571 // crash at this point. 648 // crash at this point.
572 LOG(FATAL) << "Failed to initialize UI shared context."; 649 LOG(FATAL) << "Failed to initialize UI shared context.";
573 } 650 }
574 if (!shared_context_->makeContextCurrent()) { 651 if (!shared_context_main_thread_->makeContextCurrent()) {
575 // If we can't recreate contexts, we won't be able to show the UI. Better 652 // If we can't recreate contexts, we won't be able to show the UI. Better
576 // crash at this point. 653 // crash at this point.
577 LOG(FATAL) << "Failed to make UI shared context current."; 654 LOG(FATAL) << "Failed to make UI shared context current.";
578 } 655 }
579 shared_context_->setContextLostCallback(this); 656 shared_context_main_thread_->setContextLostCallback(this);
580 } 657 }
581 658
582 void OnLostSharedContext() { 659 void OnLostMainThreadSharedContext() {
660 memory_callback_proxy_main_thread_.reset();
661 grcontext_main_thread_.reset();
662
583 // Keep old resources around while we call the observers, but ensure that 663 // Keep old resources around while we call the observers, but ensure that
584 // new resources are created if needed. 664 // new resources are created if needed.
585 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> old_shared_context( 665 scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
586 shared_context_.release()); 666 old_shared_context_main_thread(shared_context_main_thread_.release());
587 scoped_ptr<GLHelper> old_helper(gl_helper_.release()); 667 scoped_ptr<GLHelper> old_helper(gl_helper_.release());
588 668
589 FOR_EACH_OBSERVER(ImageTransportFactoryObserver, 669 FOR_EACH_OBSERVER(ImageTransportFactoryObserver,
590 observer_list_, 670 observer_list_,
591 OnLostResources()); 671 OnLostResources());
592 } 672 }
593 673
594 typedef std::map<ui::Compositor*, PerCompositorData*> PerCompositorDataMap; 674 typedef std::map<ui::Compositor*, PerCompositorData*> PerCompositorDataMap;
595 PerCompositorDataMap per_compositor_data_; 675 PerCompositorDataMap per_compositor_data_;
596 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> shared_context_; 676 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> shared_context_main_thread_;
677 scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
678 shared_context_compositor_thread_;
679 scoped_ptr<webkit::gpu::GrContextForWebGraphicsContext3D>
680 grcontext_main_thread_;
681 scoped_ptr<webkit::gpu::GrContextForWebGraphicsContext3D>
682 grcontext_compositor_thread_;
683 scoped_ptr<MemoryCallbackProxy> memory_callback_proxy_main_thread_;
684 scoped_ptr<MemoryCallbackProxy> memory_callback_proxy_compositor_thread_;
597 scoped_ptr<GLHelper> gl_helper_; 685 scoped_ptr<GLHelper> gl_helper_;
598 ObserverList<ImageTransportFactoryObserver> observer_list_; 686 ObserverList<ImageTransportFactoryObserver> observer_list_;
599 base::WeakPtrFactory<GpuProcessTransportFactory> callback_factory_; 687 base::WeakPtrFactory<GpuProcessTransportFactory> callback_factory_;
600 scoped_refptr<BrowserCompositorOutputSurfaceProxy> output_surface_proxy_; 688 scoped_refptr<BrowserCompositorOutputSurfaceProxy> output_surface_proxy_;
601 689
602 DISALLOW_COPY_AND_ASSIGN(GpuProcessTransportFactory); 690 DISALLOW_COPY_AND_ASSIGN(GpuProcessTransportFactory);
603 }; 691 };
604 692
605 void CompositorSwapClient::OnLostContext() { 693 void CompositorSwapClient::OnLostContext() {
606 factory_->OnLostContext(compositor_); 694 factory_->OnLostContext(compositor_);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
638 delete g_factory; 726 delete g_factory;
639 g_factory = NULL; 727 g_factory = NULL;
640 } 728 }
641 729
642 // static 730 // static
643 ImageTransportFactory* ImageTransportFactory::GetInstance() { 731 ImageTransportFactory* ImageTransportFactory::GetInstance() {
644 return g_factory; 732 return g_factory;
645 } 733 }
646 734
647 } // namespace content 735 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698