OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 #include "base/logging.h" |
| 6 #include "base/process_util.h" |
| 7 #include "chrome/common/command_buffer_messages.h" |
| 8 #include "chrome/common/plugin_messages.h" |
| 9 #include "chrome/renderer/command_buffer_proxy.h" |
| 10 #include "chrome/renderer/plugin_channel_host.h" |
| 11 |
| 12 CommandBufferProxy::CommandBufferProxy( |
| 13 PluginChannelHost* channel, |
| 14 int route_id) |
| 15 : size_(0), |
| 16 channel_(channel), |
| 17 route_id_(route_id) { |
| 18 } |
| 19 |
| 20 CommandBufferProxy::~CommandBufferProxy() { |
| 21 } |
| 22 |
| 23 bool CommandBufferProxy::Send(IPC::Message* msg) { |
| 24 if (channel_) |
| 25 return channel_->Send(msg); |
| 26 |
| 27 // Callee takes ownership of message, regardless of whether Send is |
| 28 // successful. See IPC::Message::Sender. |
| 29 delete msg; |
| 30 return false; |
| 31 } |
| 32 |
| 33 base::SharedMemory* CommandBufferProxy::Initialize(int32 size) { |
| 34 DCHECK(!ring_buffer_.get()); |
| 35 |
| 36 // Initialize the service. Assuming we are in the renderer process, the GPU |
| 37 // process is responsible for duplicating the handle. This might not be true |
| 38 // for NaCl. |
| 39 base::SharedMemoryHandle handle; |
| 40 if (Send(new CommandBufferMsg_Initialize(route_id_, size, &handle)) && |
| 41 base::SharedMemory::IsHandleValid(handle)) { |
| 42 ring_buffer_.reset(new base::SharedMemory(handle, false)); |
| 43 if (ring_buffer_->Map(size * sizeof(int32))) { |
| 44 size_ = size; |
| 45 return ring_buffer_.get(); |
| 46 } |
| 47 |
| 48 ring_buffer_.reset(); |
| 49 } |
| 50 |
| 51 return NULL; |
| 52 } |
| 53 |
| 54 base::SharedMemory* CommandBufferProxy::GetRingBuffer() { |
| 55 // Return locally cached ring buffer. |
| 56 return ring_buffer_.get(); |
| 57 } |
| 58 |
| 59 int32 CommandBufferProxy::GetSize() { |
| 60 // Return locally cached size. |
| 61 return size_; |
| 62 } |
| 63 |
| 64 int32 CommandBufferProxy::SyncOffsets(int32 put_offset) { |
| 65 int32 get_offset; |
| 66 if (Send(new CommandBufferMsg_SyncOffsets(route_id_, |
| 67 put_offset, |
| 68 &get_offset))) |
| 69 return get_offset; |
| 70 |
| 71 return -1; |
| 72 } |
| 73 |
| 74 int32 CommandBufferProxy::GetGetOffset() { |
| 75 int32 get_offset; |
| 76 if (Send(new CommandBufferMsg_GetGetOffset(route_id_, &get_offset))) |
| 77 return get_offset; |
| 78 |
| 79 return -1; |
| 80 } |
| 81 |
| 82 void CommandBufferProxy::SetGetOffset(int32 get_offset) { |
| 83 // Not implemented in proxy. |
| 84 NOTREACHED(); |
| 85 } |
| 86 |
| 87 int32 CommandBufferProxy::GetPutOffset() { |
| 88 int put_offset; |
| 89 if (Send(new CommandBufferMsg_GetPutOffset(route_id_, &put_offset))) |
| 90 return put_offset; |
| 91 |
| 92 return -1; |
| 93 } |
| 94 |
| 95 void CommandBufferProxy::SetPutOffsetChangeCallback(Callback0::Type* callback) { |
| 96 // Not implemented in proxy. |
| 97 NOTREACHED(); |
| 98 } |
| 99 |
| 100 int32 CommandBufferProxy::CreateTransferBuffer(size_t size) { |
| 101 int32 id; |
| 102 if (Send(new CommandBufferMsg_CreateTransferBuffer(route_id_, size, &id))) |
| 103 return id; |
| 104 |
| 105 return -1; |
| 106 } |
| 107 |
| 108 void CommandBufferProxy::DestroyTransferBuffer(int32 id) { |
| 109 // Remove the transfer buffer from the client side4 cache. |
| 110 transfer_buffers_.erase(id); |
| 111 |
| 112 Send(new CommandBufferMsg_DestroyTransferBuffer(route_id_, id)); |
| 113 } |
| 114 |
| 115 base::SharedMemory* CommandBufferProxy::GetTransferBuffer(int32 id) { |
| 116 // Check local cache to see if there is already a client side shared memory |
| 117 // object for this id. |
| 118 TransferBufferMap::iterator it = transfer_buffers_.find(id); |
| 119 if (it != transfer_buffers_.end()) |
| 120 return it->second.get(); |
| 121 |
| 122 // Assuming we are in the renderer process, the service is responsible for |
| 123 // duplicating the handle. This might not be true for NaCl. |
| 124 base::SharedMemoryHandle handle; |
| 125 if (!Send(new CommandBufferMsg_GetTransferBuffer(route_id_, id, &handle))) |
| 126 return NULL; |
| 127 |
| 128 // Cache the transfer buffer shared memory object client side. |
| 129 base::SharedMemory* transfer_buffer = |
| 130 new base::SharedMemory(handle, false, base::GetCurrentProcessHandle()); |
| 131 transfer_buffers_[id].reset(transfer_buffer); |
| 132 |
| 133 return transfer_buffer; |
| 134 } |
| 135 |
| 136 int32 CommandBufferProxy::GetToken() { |
| 137 int32 token; |
| 138 if (Send(new CommandBufferMsg_GetToken(route_id_, &token))) |
| 139 return token; |
| 140 |
| 141 return -1; |
| 142 } |
| 143 |
| 144 void CommandBufferProxy::SetToken(int32 token) { |
| 145 // Not implemented in proxy. |
| 146 NOTREACHED(); |
| 147 } |
| 148 |
| 149 int32 CommandBufferProxy::ResetParseError() { |
| 150 int32 parse_error; |
| 151 if (Send(new CommandBufferMsg_ResetParseError(route_id_, &parse_error))) |
| 152 return parse_error; |
| 153 |
| 154 return -1; |
| 155 } |
| 156 |
| 157 void CommandBufferProxy::SetParseError(int32 parse_error) { |
| 158 // Not implemented in proxy. |
| 159 NOTREACHED(); |
| 160 } |
| 161 |
| 162 bool CommandBufferProxy::GetErrorStatus() { |
| 163 bool status; |
| 164 if (Send(new CommandBufferMsg_GetErrorStatus(route_id_, &status))) |
| 165 return status; |
| 166 |
| 167 return true; |
| 168 } |
| 169 |
| 170 void CommandBufferProxy::RaiseErrorStatus() { |
| 171 // Not implemented in proxy. |
| 172 NOTREACHED(); |
| 173 } |
OLD | NEW |