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 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_BROWSER_GPU_BROWSER_GPU_CHANNEL_HOST_FACTORY_H_ 6 #define CONTENT_BROWSER_GPU_BROWSER_GPU_CHANNEL_HOST_FACTORY_H_ 7 8 #include <vector> 9 10 #include "base/memory/ref_counted.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "content/common/gpu/client/gpu_channel_host.h" 13 #include "content/common/gpu/client/gpu_memory_buffer_factory_host.h" 14 #include "ipc/message_filter.h" 15 16 namespace content { 17 18 class CONTENT_EXPORT BrowserGpuChannelHostFactory 19 : public GpuChannelHostFactory, 20 public GpuMemoryBufferFactoryHost { 21 public: 22 static void Initialize(bool establish_gpu_channel); 23 static void Terminate(); instance()24 static BrowserGpuChannelHostFactory* instance() { return instance_; } 25 26 // GpuChannelHostFactory implementation. 27 virtual bool IsMainThread() OVERRIDE; 28 virtual base::MessageLoop* GetMainLoop() OVERRIDE; 29 virtual scoped_refptr<base::MessageLoopProxy> GetIOLoopProxy() OVERRIDE; 30 virtual scoped_ptr<base::SharedMemory> AllocateSharedMemory( 31 size_t size) OVERRIDE; 32 virtual CreateCommandBufferResult CreateViewCommandBuffer( 33 int32 surface_id, 34 const GPUCreateCommandBufferConfig& init_params, 35 int32 route_id) OVERRIDE; 36 virtual void CreateImage( 37 gfx::PluginWindowHandle window, 38 int32 image_id, 39 const CreateImageCallback& callback) OVERRIDE; 40 virtual void DeleteImage(int32 image_idu, int32 sync_point) OVERRIDE; 41 virtual scoped_ptr<gfx::GpuMemoryBuffer> AllocateGpuMemoryBuffer( 42 size_t width, 43 size_t height, 44 unsigned internalformat, 45 unsigned usage) OVERRIDE; 46 47 // GpuMemoryBufferFactoryHost implementation. 48 virtual void CreateGpuMemoryBuffer( 49 const gfx::GpuMemoryBufferHandle& handle, 50 const gfx::Size& size, 51 unsigned internalformat, 52 unsigned usage, 53 const CreateGpuMemoryBufferCallback& callback) OVERRIDE; 54 virtual void DestroyGpuMemoryBuffer(const gfx::GpuMemoryBufferHandle& handle, 55 int32 sync_point) OVERRIDE; 56 57 // Specify a task runner and callback to be used for a set of messages. The 58 // callback will be set up on the current GpuProcessHost, identified by 59 // GpuProcessHostId(). 60 virtual void SetHandlerForControlMessages( 61 const uint32* message_ids, 62 size_t num_messages, 63 const base::Callback<void(const IPC::Message&)>& handler, 64 base::TaskRunner* target_task_runner); GpuProcessHostId()65 int GpuProcessHostId() { return gpu_host_id_; } 66 GpuChannelHost* EstablishGpuChannelSync( 67 CauseForGpuLaunch cause_for_gpu_launch); 68 void EstablishGpuChannel(CauseForGpuLaunch cause_for_gpu_launch, 69 const base::Closure& callback); 70 GpuChannelHost* GetGpuChannel(); GetGpuChannelId()71 int GetGpuChannelId() { return gpu_client_id_; } 72 73 // Used to skip GpuChannelHost tests when there can be no GPU process. 74 static bool CanUseForTesting(); 75 76 private: 77 struct CreateRequest; 78 class EstablishRequest; 79 80 BrowserGpuChannelHostFactory(); 81 virtual ~BrowserGpuChannelHostFactory(); 82 83 void GpuChannelEstablished(); 84 void CreateViewCommandBufferOnIO( 85 CreateRequest* request, 86 int32 surface_id, 87 const GPUCreateCommandBufferConfig& init_params); 88 static void CommandBufferCreatedOnIO(CreateRequest* request, 89 CreateCommandBufferResult result); 90 void CreateImageOnIO( 91 gfx::PluginWindowHandle window, 92 int32 image_id, 93 const CreateImageCallback& callback); 94 static void ImageCreatedOnIO( 95 const CreateImageCallback& callback, const gfx::Size size); 96 static void OnImageCreated( 97 const CreateImageCallback& callback, const gfx::Size size); 98 void DeleteImageOnIO(int32 image_id, int32 sync_point); 99 static void AddFilterOnIO(int gpu_host_id, 100 scoped_refptr<IPC::MessageFilter> filter); 101 102 void CreateGpuMemoryBufferOnIO(const gfx::GpuMemoryBufferHandle& handle, 103 const gfx::Size& size, 104 unsigned internalformat, 105 unsigned usage, 106 uint32 request_id); 107 void GpuMemoryBufferCreatedOnIO( 108 uint32 request_id, 109 const gfx::GpuMemoryBufferHandle& handle); 110 void OnGpuMemoryBufferCreated( 111 uint32 request_id, 112 const gfx::GpuMemoryBufferHandle& handle); 113 void DestroyGpuMemoryBufferOnIO(const gfx::GpuMemoryBufferHandle& handle, 114 int32 sync_point); 115 116 const int gpu_client_id_; 117 scoped_ptr<base::WaitableEvent> shutdown_event_; 118 scoped_refptr<GpuChannelHost> gpu_channel_; 119 int gpu_host_id_; 120 scoped_refptr<EstablishRequest> pending_request_; 121 std::vector<base::Closure> established_callbacks_; 122 123 uint32 next_create_gpu_memory_buffer_request_id_; 124 typedef std::map<uint32, CreateGpuMemoryBufferCallback> 125 CreateGpuMemoryBufferCallbackMap; 126 CreateGpuMemoryBufferCallbackMap create_gpu_memory_buffer_requests_; 127 128 static BrowserGpuChannelHostFactory* instance_; 129 130 DISALLOW_COPY_AND_ASSIGN(BrowserGpuChannelHostFactory); 131 }; 132 133 } // namespace content 134 135 #endif // CONTENT_BROWSER_GPU_BROWSER_GPU_CHANNEL_HOST_FACTORY_H_ 136