| OLD | NEW |
| (Empty) |
| 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_RENDERER_GPU_GPU_CHANNEL_HOST_H_ | |
| 6 #define CONTENT_RENDERER_GPU_GPU_CHANNEL_HOST_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/hash_tables.h" | |
| 13 #include "base/memory/ref_counted.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/memory/weak_ptr.h" | |
| 16 #include "base/process_util.h" | |
| 17 #include "base/synchronization/lock.h" | |
| 18 #include "content/common/gpu/gpu_process_launch_causes.h" | |
| 19 #include "content/common/message_router.h" | |
| 20 #include "content/public/common/gpu_info.h" | |
| 21 #include "content/renderer/gpu/gpu_video_decode_accelerator_host.h" | |
| 22 #include "ipc/ipc_channel_handle.h" | |
| 23 #include "ipc/ipc_channel_proxy.h" | |
| 24 #include "ipc/ipc_sync_channel.h" | |
| 25 #include "ui/gfx/gl/gpu_preference.h" | |
| 26 #include "ui/gfx/native_widget_types.h" | |
| 27 #include "ui/gfx/size.h" | |
| 28 | |
| 29 class CommandBufferProxy; | |
| 30 struct GPUCreateCommandBufferConfig; | |
| 31 class GURL; | |
| 32 class TransportTextureService; | |
| 33 class MessageLoop; | |
| 34 | |
| 35 namespace base { | |
| 36 class MessageLoopProxy; | |
| 37 } | |
| 38 | |
| 39 namespace IPC { | |
| 40 class SyncMessageFilter; | |
| 41 } | |
| 42 | |
| 43 struct GpuListenerInfo { | |
| 44 GpuListenerInfo(); | |
| 45 ~GpuListenerInfo(); | |
| 46 | |
| 47 base::WeakPtr<IPC::Channel::Listener> listener; | |
| 48 scoped_refptr<base::MessageLoopProxy> loop; | |
| 49 }; | |
| 50 | |
| 51 class GpuChannelHostFactory { | |
| 52 public: | |
| 53 virtual ~GpuChannelHostFactory(); | |
| 54 static GpuChannelHostFactory* instance() { return instance_; } | |
| 55 | |
| 56 virtual bool IsMainThread() = 0; | |
| 57 virtual bool IsIOThread() = 0; | |
| 58 virtual MessageLoop* GetMainLoop() = 0; | |
| 59 virtual base::MessageLoopProxy* GetIOLoopProxy() = 0; | |
| 60 virtual base::WaitableEvent* GetShutDownEvent() = 0; | |
| 61 virtual scoped_ptr<base::SharedMemory> AllocateSharedMemory(uint32 size) = 0; | |
| 62 virtual int32 CreateViewCommandBuffer( | |
| 63 int32 surface_id, const GPUCreateCommandBufferConfig& init_params) = 0; | |
| 64 virtual GpuChannelHost* EstablishGpuChannelSync( | |
| 65 content::CauseForGpuLaunch) = 0; | |
| 66 | |
| 67 protected: | |
| 68 static void set_instance(GpuChannelHostFactory* instance) { | |
| 69 instance_ = instance; | |
| 70 } | |
| 71 | |
| 72 private: | |
| 73 static GpuChannelHostFactory* instance_; | |
| 74 }; | |
| 75 | |
| 76 // Encapsulates an IPC channel between the renderer and one plugin process. | |
| 77 // On the plugin side there's a corresponding GpuChannel. | |
| 78 class GpuChannelHost : public IPC::Message::Sender, | |
| 79 public base::RefCountedThreadSafe<GpuChannelHost> { | |
| 80 public: | |
| 81 enum State { | |
| 82 // Not yet connected. | |
| 83 kUnconnected, | |
| 84 // Ready to use. | |
| 85 kConnected, | |
| 86 // An error caused the host to become disconnected. Recreate channel to | |
| 87 // reestablish connection. | |
| 88 kLost | |
| 89 }; | |
| 90 | |
| 91 // Called on the render thread | |
| 92 explicit GpuChannelHost(GpuChannelHostFactory* factory); | |
| 93 virtual ~GpuChannelHost(); | |
| 94 | |
| 95 // Connect to GPU process channel. | |
| 96 void Connect(const IPC::ChannelHandle& channel_handle, | |
| 97 base::ProcessHandle renderer_process_for_gpu); | |
| 98 | |
| 99 State state() const { return state_; } | |
| 100 | |
| 101 // Change state to kLost. | |
| 102 void SetStateLost(); | |
| 103 | |
| 104 // The GPU stats reported by the GPU process. | |
| 105 void set_gpu_info(const content::GPUInfo& gpu_info); | |
| 106 const content::GPUInfo& gpu_info() const; | |
| 107 | |
| 108 void OnChannelError(); | |
| 109 | |
| 110 // IPC::Message::Sender implementation: | |
| 111 virtual bool Send(IPC::Message* msg) OVERRIDE; | |
| 112 | |
| 113 // Create and connect to a command buffer in the GPU process. | |
| 114 CommandBufferProxy* CreateViewCommandBuffer( | |
| 115 int32 surface_id, | |
| 116 CommandBufferProxy* share_group, | |
| 117 const std::string& allowed_extensions, | |
| 118 const std::vector<int32>& attribs, | |
| 119 const GURL& active_url, | |
| 120 gfx::GpuPreference gpu_preference); | |
| 121 | |
| 122 // Create and connect to a command buffer in the GPU process. | |
| 123 CommandBufferProxy* CreateOffscreenCommandBuffer( | |
| 124 const gfx::Size& size, | |
| 125 CommandBufferProxy* share_group, | |
| 126 const std::string& allowed_extensions, | |
| 127 const std::vector<int32>& attribs, | |
| 128 const GURL& active_url, | |
| 129 gfx::GpuPreference gpu_preference); | |
| 130 | |
| 131 // Creates a video decoder in the GPU process. | |
| 132 // Returned pointer is owned by the CommandBufferProxy for |route_id|. | |
| 133 GpuVideoDecodeAcceleratorHost* CreateVideoDecoder( | |
| 134 int command_buffer_route_id, | |
| 135 media::VideoDecodeAccelerator::Profile profile, | |
| 136 media::VideoDecodeAccelerator::Client* client); | |
| 137 | |
| 138 // Destroy a command buffer created by this channel. | |
| 139 void DestroyCommandBuffer(CommandBufferProxy* command_buffer); | |
| 140 | |
| 141 // Add a route for the current message loop. | |
| 142 void AddRoute(int route_id, base::WeakPtr<IPC::Channel::Listener> listener); | |
| 143 void RemoveRoute(int route_id); | |
| 144 | |
| 145 // Asks the GPU process whether the creation or destruction of the | |
| 146 // given command buffer on the given GPU will provoke a switch of | |
| 147 // the GPU from integrated to discrete or vice versa. This requires | |
| 148 // all of the GL contexts in the same share group in the GPU process | |
| 149 // to be dropped. | |
| 150 bool WillGpuSwitchOccur(bool is_creating_context, | |
| 151 gfx::GpuPreference gpu_preference); | |
| 152 | |
| 153 // Forcibly close the channel on the GPU process side. This will | |
| 154 // cause all command buffers on this side to soon afterward start | |
| 155 // registering lost contexts. It also has the side effect of setting | |
| 156 // the state on this side to lost. | |
| 157 void ForciblyCloseChannel(); | |
| 158 | |
| 159 GpuChannelHostFactory* factory() const { return factory_; } | |
| 160 | |
| 161 private: | |
| 162 // A filter used internally to route incoming messages from the IO thread | |
| 163 // to the correct message loop. | |
| 164 class MessageFilter : public IPC::ChannelProxy::MessageFilter { | |
| 165 public: | |
| 166 explicit MessageFilter(GpuChannelHost* parent); | |
| 167 virtual ~MessageFilter(); | |
| 168 | |
| 169 void AddRoute(int route_id, | |
| 170 base::WeakPtr<IPC::Channel::Listener> listener, | |
| 171 scoped_refptr<base::MessageLoopProxy> loop); | |
| 172 void RemoveRoute(int route_id); | |
| 173 | |
| 174 // IPC::ChannelProxy::MessageFilter implementation: | |
| 175 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE; | |
| 176 virtual void OnChannelError() OVERRIDE; | |
| 177 | |
| 178 private: | |
| 179 GpuChannelHost* parent_; | |
| 180 | |
| 181 typedef base::hash_map<int, GpuListenerInfo> ListenerMap; | |
| 182 ListenerMap listeners_; | |
| 183 }; | |
| 184 | |
| 185 GpuChannelHostFactory* factory_; | |
| 186 | |
| 187 State state_; | |
| 188 | |
| 189 content::GPUInfo gpu_info_; | |
| 190 | |
| 191 scoped_ptr<IPC::SyncChannel> channel_; | |
| 192 scoped_refptr<MessageFilter> channel_filter_; | |
| 193 | |
| 194 // Used to look up a proxy from its routing id. | |
| 195 typedef base::hash_map<int, CommandBufferProxy*> ProxyMap; | |
| 196 ProxyMap proxies_; | |
| 197 | |
| 198 // A lock to guard against concurrent access to members like the proxies map | |
| 199 // for calls from contexts that may live on the compositor or main thread. | |
| 200 mutable base::Lock context_lock_; | |
| 201 | |
| 202 // A filter for sending messages from thread other than the main thread. | |
| 203 scoped_refptr<IPC::SyncMessageFilter> sync_filter_; | |
| 204 | |
| 205 DISALLOW_COPY_AND_ASSIGN(GpuChannelHost); | |
| 206 }; | |
| 207 | |
| 208 #endif // CONTENT_RENDERER_GPU_GPU_CHANNEL_HOST_H_ | |
| OLD | NEW |