[email protected] | aaa11b3 | 2012-03-08 12:30:13 | [diff] [blame] | 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 | #include "ppapi/proxy/ppapi_command_buffer_proxy.h" |
| 6 | |
| 7 | #include "ppapi/proxy/ppapi_messages.h" |
| 8 | #include "ppapi/proxy/proxy_channel.h" |
| 9 | #include "ppapi/shared_impl/api_id.h" |
| 10 | #include "ppapi/shared_impl/host_resource.h" |
| 11 | |
| 12 | namespace ppapi { |
| 13 | namespace proxy { |
| 14 | |
| 15 | PpapiCommandBufferProxy::PpapiCommandBufferProxy( |
| 16 | const ppapi::HostResource& resource, |
| 17 | ProxyChannel* channel) |
| 18 | : resource_(resource), |
| 19 | channel_(channel) { |
| 20 | } |
| 21 | |
| 22 | PpapiCommandBufferProxy::~PpapiCommandBufferProxy() { |
| 23 | // Delete all the locally cached shared memory objects, closing the handle |
| 24 | // in this process. |
| 25 | for (TransferBufferMap::iterator it = transfer_buffers_.begin(); |
| 26 | it != transfer_buffers_.end(); ++it) { |
| 27 | delete it->second.shared_memory; |
| 28 | it->second.shared_memory = NULL; |
| 29 | } |
| 30 | } |
| 31 | |
[email protected] | 2651ef6e | 2012-03-28 23:47:27 | [diff] [blame] | 32 | void PpapiCommandBufferProxy::ReportChannelError() { |
| 33 | if (!channel_error_callback_.is_null()) { |
| 34 | channel_error_callback_.Run(); |
| 35 | channel_error_callback_.Reset(); |
| 36 | } |
| 37 | } |
| 38 | |
[email protected] | 9e5fffaf | 2012-04-02 23:32:37 | [diff] [blame] | 39 | int PpapiCommandBufferProxy::GetRouteID() const { |
| 40 | NOTIMPLEMENTED(); |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | bool PpapiCommandBufferProxy::Echo(const base::Closure& callback) { |
[email protected] | 9e5fffaf | 2012-04-02 23:32:37 | [diff] [blame] | 45 | return false; |
| 46 | } |
| 47 | |
[email protected] | 9e5fffaf | 2012-04-02 23:32:37 | [diff] [blame] | 48 | bool PpapiCommandBufferProxy::SetParent( |
| 49 | CommandBufferProxy* parent_command_buffer, |
| 50 | uint32 parent_texture_id) { |
| 51 | // TODO(fsamuel): Need a proper implementation of this to support offscreen |
| 52 | // contexts in the guest renderer (WebGL, canvas, etc). |
| 53 | NOTIMPLEMENTED(); |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | void PpapiCommandBufferProxy::SetChannelErrorCallback( |
| 58 | const base::Closure& callback) { |
| 59 | channel_error_callback_ = callback; |
| 60 | } |
| 61 | |
[email protected] | aaa11b3 | 2012-03-08 12:30:13 | [diff] [blame] | 62 | bool PpapiCommandBufferProxy::Initialize() { |
| 63 | return Send(new PpapiHostMsg_PPBGraphics3D_InitCommandBuffer( |
| 64 | ppapi::API_ID_PPB_GRAPHICS_3D, resource_)); |
| 65 | } |
| 66 | |
| 67 | gpu::CommandBuffer::State PpapiCommandBufferProxy::GetState() { |
| 68 | // Send will flag state with lost context if IPC fails. |
| 69 | if (last_state_.error == gpu::error::kNoError) { |
| 70 | gpu::CommandBuffer::State state; |
[email protected] | 571b35e8 | 2012-05-19 00:04:35 | [diff] [blame] | 71 | bool success = false; |
[email protected] | aaa11b3 | 2012-03-08 12:30:13 | [diff] [blame] | 72 | if (Send(new PpapiHostMsg_PPBGraphics3D_GetState( |
[email protected] | 571b35e8 | 2012-05-19 00:04:35 | [diff] [blame] | 73 | ppapi::API_ID_PPB_GRAPHICS_3D, resource_, &state, &success))) { |
| 74 | UpdateState(state, success); |
[email protected] | aaa11b3 | 2012-03-08 12:30:13 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | return last_state_; |
| 79 | } |
| 80 | |
| 81 | gpu::CommandBuffer::State PpapiCommandBufferProxy::GetLastState() { |
[email protected] | 84b44c55 | 2012-10-15 20:58:17 | [diff] [blame] | 82 | // Note: The locking command buffer wrapper does not take a global lock before |
| 83 | // calling this function. |
[email protected] | aaa11b3 | 2012-03-08 12:30:13 | [diff] [blame] | 84 | return last_state_; |
| 85 | } |
| 86 | |
| 87 | void PpapiCommandBufferProxy::Flush(int32 put_offset) { |
| 88 | if (last_state_.error != gpu::error::kNoError) |
| 89 | return; |
| 90 | |
| 91 | IPC::Message* message = new PpapiHostMsg_PPBGraphics3D_AsyncFlush( |
| 92 | ppapi::API_ID_PPB_GRAPHICS_3D, resource_, put_offset); |
| 93 | |
| 94 | // Do not let a synchronous flush hold up this message. If this handler is |
| 95 | // deferred until after the synchronous flush completes, it will overwrite the |
| 96 | // cached last_state_ with out-of-date data. |
| 97 | message->set_unblock(true); |
| 98 | Send(message); |
| 99 | } |
| 100 | |
| 101 | gpu::CommandBuffer::State PpapiCommandBufferProxy::FlushSync(int32 put_offset, |
| 102 | int32 last_known_get) { |
| 103 | if (last_known_get == last_state_.get_offset) { |
| 104 | // Send will flag state with lost context if IPC fails. |
| 105 | if (last_state_.error == gpu::error::kNoError) { |
| 106 | gpu::CommandBuffer::State state; |
[email protected] | 571b35e8 | 2012-05-19 00:04:35 | [diff] [blame] | 107 | bool success = false; |
[email protected] | aaa11b3 | 2012-03-08 12:30:13 | [diff] [blame] | 108 | if (Send(new PpapiHostMsg_PPBGraphics3D_Flush( |
| 109 | ppapi::API_ID_PPB_GRAPHICS_3D, resource_, put_offset, |
[email protected] | 571b35e8 | 2012-05-19 00:04:35 | [diff] [blame] | 110 | last_known_get, &state, &success))) { |
| 111 | UpdateState(state, success); |
[email protected] | aaa11b3 | 2012-03-08 12:30:13 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } else { |
| 115 | Flush(put_offset); |
| 116 | } |
| 117 | return last_state_; |
| 118 | } |
| 119 | |
| 120 | void PpapiCommandBufferProxy::SetGetBuffer(int32 transfer_buffer_id) { |
| 121 | if (last_state_.error == gpu::error::kNoError) { |
| 122 | Send(new PpapiHostMsg_PPBGraphics3D_SetGetBuffer( |
| 123 | ppapi::API_ID_PPB_GRAPHICS_3D, resource_, transfer_buffer_id)); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | void PpapiCommandBufferProxy::SetGetOffset(int32 get_offset) { |
| 128 | // Not implemented in proxy. |
| 129 | NOTREACHED(); |
| 130 | } |
| 131 | |
| 132 | int32 PpapiCommandBufferProxy::CreateTransferBuffer( |
| 133 | size_t size, |
| 134 | int32 id_request) { |
| 135 | if (last_state_.error == gpu::error::kNoError) { |
| 136 | int32 id; |
| 137 | if (Send(new PpapiHostMsg_PPBGraphics3D_CreateTransferBuffer( |
| 138 | ppapi::API_ID_PPB_GRAPHICS_3D, resource_, size, &id))) { |
| 139 | return id; |
| 140 | } |
| 141 | } |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | int32 PpapiCommandBufferProxy::RegisterTransferBuffer( |
| 146 | base::SharedMemory* shared_memory, |
| 147 | size_t size, |
| 148 | int32 id_request) { |
| 149 | // Not implemented in proxy. |
| 150 | NOTREACHED(); |
| 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | void PpapiCommandBufferProxy::DestroyTransferBuffer(int32 id) { |
| 155 | if (last_state_.error != gpu::error::kNoError) |
| 156 | return; |
| 157 | |
| 158 | // Remove the transfer buffer from the client side4 cache. |
| 159 | TransferBufferMap::iterator it = transfer_buffers_.find(id); |
[email protected] | aaa11b3 | 2012-03-08 12:30:13 | [diff] [blame] | 160 | |
[email protected] | 852ab72 | 2012-10-25 21:08:25 | [diff] [blame] | 161 | if (it != transfer_buffers_.end()) { |
| 162 | // Delete the shared memory object, closing the handle in this process. |
| 163 | delete it->second.shared_memory; |
[email protected] | aaa11b3 | 2012-03-08 12:30:13 | [diff] [blame] | 164 | |
[email protected] | 852ab72 | 2012-10-25 21:08:25 | [diff] [blame] | 165 | transfer_buffers_.erase(it); |
| 166 | } |
[email protected] | aaa11b3 | 2012-03-08 12:30:13 | [diff] [blame] | 167 | |
| 168 | Send(new PpapiHostMsg_PPBGraphics3D_DestroyTransferBuffer( |
| 169 | ppapi::API_ID_PPB_GRAPHICS_3D, resource_, id)); |
| 170 | } |
| 171 | |
| 172 | gpu::Buffer PpapiCommandBufferProxy::GetTransferBuffer(int32 id) { |
| 173 | if (last_state_.error != gpu::error::kNoError) |
| 174 | return gpu::Buffer(); |
| 175 | |
| 176 | // Check local cache to see if there is already a client side shared memory |
| 177 | // object for this id. |
| 178 | TransferBufferMap::iterator it = transfer_buffers_.find(id); |
| 179 | if (it != transfer_buffers_.end()) { |
| 180 | return it->second; |
| 181 | } |
| 182 | |
| 183 | // Assuming we are in the renderer process, the service is responsible for |
| 184 | // duplicating the handle. This might not be true for NaCl. |
[email protected] | 246fc49 | 2012-08-27 20:28:18 | [diff] [blame] | 185 | ppapi::proxy::SerializedHandle handle( |
| 186 | ppapi::proxy::SerializedHandle::SHARED_MEMORY); |
[email protected] | aaa11b3 | 2012-03-08 12:30:13 | [diff] [blame] | 187 | if (!Send(new PpapiHostMsg_PPBGraphics3D_GetTransferBuffer( |
[email protected] | 246fc49 | 2012-08-27 20:28:18 | [diff] [blame] | 188 | ppapi::API_ID_PPB_GRAPHICS_3D, resource_, id, &handle))) { |
[email protected] | aaa11b3 | 2012-03-08 12:30:13 | [diff] [blame] | 189 | return gpu::Buffer(); |
| 190 | } |
[email protected] | 246fc49 | 2012-08-27 20:28:18 | [diff] [blame] | 191 | if (!handle.is_shmem()) |
| 192 | return gpu::Buffer(); |
[email protected] | aaa11b3 | 2012-03-08 12:30:13 | [diff] [blame] | 193 | |
| 194 | // Cache the transfer buffer shared memory object client side. |
| 195 | scoped_ptr<base::SharedMemory> shared_memory( |
[email protected] | 246fc49 | 2012-08-27 20:28:18 | [diff] [blame] | 196 | new base::SharedMemory(handle.shmem(), false)); |
[email protected] | aaa11b3 | 2012-03-08 12:30:13 | [diff] [blame] | 197 | |
| 198 | // Map the shared memory on demand. |
| 199 | if (!shared_memory->memory()) { |
[email protected] | 246fc49 | 2012-08-27 20:28:18 | [diff] [blame] | 200 | if (!shared_memory->Map(handle.size())) { |
[email protected] | aaa11b3 | 2012-03-08 12:30:13 | [diff] [blame] | 201 | return gpu::Buffer(); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | gpu::Buffer buffer; |
| 206 | buffer.ptr = shared_memory->memory(); |
[email protected] | 246fc49 | 2012-08-27 20:28:18 | [diff] [blame] | 207 | buffer.size = handle.size(); |
[email protected] | aaa11b3 | 2012-03-08 12:30:13 | [diff] [blame] | 208 | buffer.shared_memory = shared_memory.release(); |
| 209 | transfer_buffers_[id] = buffer; |
| 210 | |
| 211 | return buffer; |
| 212 | } |
| 213 | |
| 214 | void PpapiCommandBufferProxy::SetToken(int32 token) { |
| 215 | NOTREACHED(); |
| 216 | } |
| 217 | |
| 218 | void PpapiCommandBufferProxy::SetParseError(gpu::error::Error error) { |
| 219 | NOTREACHED(); |
| 220 | } |
| 221 | |
| 222 | void PpapiCommandBufferProxy::SetContextLostReason( |
| 223 | gpu::error::ContextLostReason reason) { |
| 224 | NOTREACHED(); |
| 225 | } |
| 226 | |
| 227 | bool PpapiCommandBufferProxy::Send(IPC::Message* msg) { |
| 228 | DCHECK(last_state_.error == gpu::error::kNoError); |
| 229 | |
| 230 | if (channel_->Send(msg)) |
| 231 | return true; |
| 232 | |
| 233 | last_state_.error = gpu::error::kLostContext; |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | void PpapiCommandBufferProxy::UpdateState( |
[email protected] | 571b35e8 | 2012-05-19 00:04:35 | [diff] [blame] | 238 | const gpu::CommandBuffer::State& state, |
| 239 | bool success) { |
[email protected] | aaa11b3 | 2012-03-08 12:30:13 | [diff] [blame] | 240 | // Handle wraparound. It works as long as we don't have more than 2B state |
| 241 | // updates in flight across which reordering occurs. |
[email protected] | 571b35e8 | 2012-05-19 00:04:35 | [diff] [blame] | 242 | if (success) { |
| 243 | if (state.generation - last_state_.generation < 0x80000000U) { |
| 244 | last_state_ = state; |
[email protected] | b44a986 | 2012-05-22 22:08:43 | [diff] [blame] | 245 | } |
| 246 | } else { |
[email protected] | 571b35e8 | 2012-05-19 00:04:35 | [diff] [blame] | 247 | last_state_.error = gpu::error::kLostContext; |
| 248 | ++last_state_.generation; |
| 249 | } |
[email protected] | aaa11b3 | 2012-03-08 12:30:13 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | } // namespace proxy |
| 253 | } // namespace ppapi |