This source file includes following definitions.
- Create
- IsValidGpuMemoryBuffer
- gpu_info_
- Connect
- Send
- CreateViewCommandBuffer
- CreateOffscreenCommandBuffer
- CreateVideoDecoder
- CreateVideoEncoder
- DestroyCommandBuffer
- AddRoute
- RemoveRoute
- ShareToGpuProcess
- ReserveTransferBufferId
- ShareGpuMemoryBufferToGpuProcess
- ReserveGpuMemoryBufferId
- AddRoute
- RemoveRoute
- OnMessageReceived
- OnChannelError
- IsLost
#include "content/common/gpu/client/gpu_channel_host.h"
#include <algorithm>
#include "base/bind.h"
#include "base/debug/trace_event.h"
#include "base/message_loop/message_loop.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/posix/eintr_wrapper.h"
#include "base/threading/thread_restrictions.h"
#include "content/common/gpu/client/command_buffer_proxy_impl.h"
#include "content/common/gpu/gpu_messages.h"
#include "ipc/ipc_sync_message_filter.h"
#include "url/gurl.h"
#if defined(OS_WIN)
#include "content/public/common/sandbox_init.h"
#endif
using base::AutoLock;
using base::MessageLoopProxy;
namespace content {
GpuListenerInfo::GpuListenerInfo() {}
GpuListenerInfo::~GpuListenerInfo() {}
scoped_refptr<GpuChannelHost> GpuChannelHost::Create(
GpuChannelHostFactory* factory,
const gpu::GPUInfo& gpu_info,
const IPC::ChannelHandle& channel_handle,
base::WaitableEvent* shutdown_event) {
DCHECK(factory->IsMainThread());
scoped_refptr<GpuChannelHost> host = new GpuChannelHost(factory, gpu_info);
host->Connect(channel_handle, shutdown_event);
return host;
}
bool GpuChannelHost::IsValidGpuMemoryBuffer(
gfx::GpuMemoryBufferHandle handle) {
switch (handle.type) {
case gfx::SHARED_MEMORY_BUFFER:
#if defined(OS_MACOSX)
case gfx::IO_SURFACE_BUFFER:
#endif
#if defined(OS_ANDROID)
case gfx::SURFACE_TEXTURE_BUFFER:
#endif
return true;
default:
return false;
}
}
GpuChannelHost::GpuChannelHost(GpuChannelHostFactory* factory,
const gpu::GPUInfo& gpu_info)
: factory_(factory),
gpu_info_(gpu_info) {
next_transfer_buffer_id_.GetNext();
next_gpu_memory_buffer_id_.GetNext();
}
void GpuChannelHost::Connect(const IPC::ChannelHandle& channel_handle,
base::WaitableEvent* shutdown_event) {
scoped_refptr<base::MessageLoopProxy> io_loop = factory_->GetIOLoopProxy();
channel_.reset(new IPC::SyncChannel(channel_handle,
IPC::Channel::MODE_CLIENT,
NULL,
io_loop.get(),
true,
shutdown_event));
sync_filter_ = new IPC::SyncMessageFilter(shutdown_event);
channel_->AddFilter(sync_filter_.get());
channel_filter_ = new MessageFilter();
channel_->AddFilter(channel_filter_.get());
}
bool GpuChannelHost::Send(IPC::Message* msg) {
scoped_ptr<IPC::Message> message(msg);
message->set_unblock(false);
if (factory_->IsMainThread()) {
base::ThreadRestrictions::ScopedAllowWait allow_wait;
bool result = channel_->Send(message.release());
if (!result)
DVLOG(1) << "GpuChannelHost::Send failed: Channel::Send failed";
return result;
} else if (base::MessageLoop::current()) {
bool result = sync_filter_->Send(message.release());
if (!result)
DVLOG(1) << "GpuChannelHost::Send failed: SyncMessageFilter::Send failed";
return result;
}
return false;
}
CommandBufferProxyImpl* GpuChannelHost::CreateViewCommandBuffer(
int32 surface_id,
CommandBufferProxyImpl* share_group,
const std::vector<int32>& attribs,
const GURL& active_url,
gfx::GpuPreference gpu_preference) {
TRACE_EVENT1("gpu",
"GpuChannelHost::CreateViewCommandBuffer",
"surface_id",
surface_id);
GPUCreateCommandBufferConfig init_params;
init_params.share_group_id =
share_group ? share_group->GetRouteID() : MSG_ROUTING_NONE;
init_params.attribs = attribs;
init_params.active_url = active_url;
init_params.gpu_preference = gpu_preference;
int32 route_id = factory_->CreateViewCommandBuffer(surface_id, init_params);
if (route_id == MSG_ROUTING_NONE) {
LOG(ERROR) << "GpuChannelHost::CreateViewCommandBuffer failed.";
return NULL;
}
CommandBufferProxyImpl* command_buffer =
new CommandBufferProxyImpl(this, route_id);
AddRoute(route_id, command_buffer->AsWeakPtr());
AutoLock lock(context_lock_);
proxies_[route_id] = command_buffer;
return command_buffer;
}
CommandBufferProxyImpl* GpuChannelHost::CreateOffscreenCommandBuffer(
const gfx::Size& size,
CommandBufferProxyImpl* share_group,
const std::vector<int32>& attribs,
const GURL& active_url,
gfx::GpuPreference gpu_preference) {
TRACE_EVENT0("gpu", "GpuChannelHost::CreateOffscreenCommandBuffer");
GPUCreateCommandBufferConfig init_params;
init_params.share_group_id =
share_group ? share_group->GetRouteID() : MSG_ROUTING_NONE;
init_params.attribs = attribs;
init_params.active_url = active_url;
init_params.gpu_preference = gpu_preference;
int32 route_id;
if (!Send(new GpuChannelMsg_CreateOffscreenCommandBuffer(size,
init_params,
&route_id))) {
LOG(ERROR) << "Failed to send GpuChannelMsg_CreateOffscreenCommandBuffer.";
return NULL;
}
if (route_id == MSG_ROUTING_NONE) {
LOG(ERROR)
<< "GpuChannelMsg_CreateOffscreenCommandBuffer returned failure.";
return NULL;
}
CommandBufferProxyImpl* command_buffer =
new CommandBufferProxyImpl(this, route_id);
AddRoute(route_id, command_buffer->AsWeakPtr());
AutoLock lock(context_lock_);
proxies_[route_id] = command_buffer;
return command_buffer;
}
scoped_ptr<media::VideoDecodeAccelerator> GpuChannelHost::CreateVideoDecoder(
int command_buffer_route_id) {
TRACE_EVENT0("gpu", "GpuChannelHost::CreateVideoDecoder");
AutoLock lock(context_lock_);
ProxyMap::iterator it = proxies_.find(command_buffer_route_id);
DCHECK(it != proxies_.end());
return it->second->CreateVideoDecoder();
}
scoped_ptr<media::VideoEncodeAccelerator> GpuChannelHost::CreateVideoEncoder(
int command_buffer_route_id) {
TRACE_EVENT0("gpu", "GpuChannelHost::CreateVideoEncoder");
AutoLock lock(context_lock_);
ProxyMap::iterator it = proxies_.find(command_buffer_route_id);
DCHECK(it != proxies_.end());
return it->second->CreateVideoEncoder();
}
void GpuChannelHost::DestroyCommandBuffer(
CommandBufferProxyImpl* command_buffer) {
TRACE_EVENT0("gpu", "GpuChannelHost::DestroyCommandBuffer");
int route_id = command_buffer->GetRouteID();
Send(new GpuChannelMsg_DestroyCommandBuffer(route_id));
RemoveRoute(route_id);
AutoLock lock(context_lock_);
proxies_.erase(route_id);
delete command_buffer;
}
void GpuChannelHost::AddRoute(
int route_id, base::WeakPtr<IPC::Listener> listener) {
DCHECK(MessageLoopProxy::current().get());
scoped_refptr<base::MessageLoopProxy> io_loop = factory_->GetIOLoopProxy();
io_loop->PostTask(FROM_HERE,
base::Bind(&GpuChannelHost::MessageFilter::AddRoute,
channel_filter_.get(), route_id, listener,
MessageLoopProxy::current()));
}
void GpuChannelHost::RemoveRoute(int route_id) {
scoped_refptr<base::MessageLoopProxy> io_loop = factory_->GetIOLoopProxy();
io_loop->PostTask(FROM_HERE,
base::Bind(&GpuChannelHost::MessageFilter::RemoveRoute,
channel_filter_.get(), route_id));
}
base::SharedMemoryHandle GpuChannelHost::ShareToGpuProcess(
base::SharedMemoryHandle source_handle) {
if (IsLost())
return base::SharedMemory::NULLHandle();
#if defined(OS_WIN)
base::SharedMemoryHandle target_handle;
if (!BrokerDuplicateHandle(source_handle,
channel_->peer_pid(),
&target_handle,
FILE_GENERIC_READ | FILE_GENERIC_WRITE,
0)) {
return base::SharedMemory::NULLHandle();
}
return target_handle;
#else
int duped_handle = HANDLE_EINTR(dup(source_handle.fd));
if (duped_handle < 0)
return base::SharedMemory::NULLHandle();
return base::FileDescriptor(duped_handle, true);
#endif
}
int32 GpuChannelHost::ReserveTransferBufferId() {
return next_transfer_buffer_id_.GetNext();
}
gfx::GpuMemoryBufferHandle GpuChannelHost::ShareGpuMemoryBufferToGpuProcess(
gfx::GpuMemoryBufferHandle source_handle) {
switch (source_handle.type) {
case gfx::SHARED_MEMORY_BUFFER: {
gfx::GpuMemoryBufferHandle handle;
handle.type = gfx::SHARED_MEMORY_BUFFER;
handle.handle = ShareToGpuProcess(source_handle.handle);
return handle;
}
#if defined(OS_MACOSX)
case gfx::IO_SURFACE_BUFFER:
return source_handle;
#endif
#if defined(OS_ANDROID)
case gfx::SURFACE_TEXTURE_BUFFER:
return source_handle;
#endif
default:
NOTREACHED();
return gfx::GpuMemoryBufferHandle();
}
}
int32 GpuChannelHost::ReserveGpuMemoryBufferId() {
return next_gpu_memory_buffer_id_.GetNext();
}
GpuChannelHost::~GpuChannelHost() {
if (!factory_->IsMainThread())
factory_->GetMainLoop()->DeleteSoon(FROM_HERE, channel_.release());
}
GpuChannelHost::MessageFilter::MessageFilter()
: lost_(false) {
}
GpuChannelHost::MessageFilter::~MessageFilter() {}
void GpuChannelHost::MessageFilter::AddRoute(
int route_id,
base::WeakPtr<IPC::Listener> listener,
scoped_refptr<MessageLoopProxy> loop) {
DCHECK(listeners_.find(route_id) == listeners_.end());
GpuListenerInfo info;
info.listener = listener;
info.loop = loop;
listeners_[route_id] = info;
}
void GpuChannelHost::MessageFilter::RemoveRoute(int route_id) {
ListenerMap::iterator it = listeners_.find(route_id);
if (it != listeners_.end())
listeners_.erase(it);
}
bool GpuChannelHost::MessageFilter::OnMessageReceived(
const IPC::Message& message) {
if (message.is_reply())
return false;
ListenerMap::iterator it = listeners_.find(message.routing_id());
if (it == listeners_.end())
return false;
const GpuListenerInfo& info = it->second;
info.loop->PostTask(
FROM_HERE,
base::Bind(
base::IgnoreResult(&IPC::Listener::OnMessageReceived),
info.listener,
message));
return true;
}
void GpuChannelHost::MessageFilter::OnChannelError() {
{
AutoLock lock(lock_);
lost_ = true;
}
for (ListenerMap::iterator it = listeners_.begin();
it != listeners_.end();
it++) {
const GpuListenerInfo& info = it->second;
info.loop->PostTask(
FROM_HERE,
base::Bind(&IPC::Listener::OnChannelError, info.listener));
}
listeners_.clear();
}
bool GpuChannelHost::MessageFilter::IsLost() const {
AutoLock lock(lock_);
return lost_;
}
}