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.
5 #include "ppapi/proxy/ppapi_command_buffer_proxy.h"
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"
15 PpapiCommandBufferProxy::PpapiCommandBufferProxy(
16 const ppapi::HostResource
& resource
,
17 ProxyChannel
* channel
)
18 : resource_(resource
),
22 PpapiCommandBufferProxy::~PpapiCommandBufferProxy() {
23 // Delete all the locally cached shared memory objects, closing the handle
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
;
32 void PpapiCommandBufferProxy::ReportChannelError() {
33 if (!channel_error_callback_
.is_null()) {
34 channel_error_callback_
.Run();
35 channel_error_callback_
.Reset();
39 int PpapiCommandBufferProxy::GetRouteID() const {
44 bool PpapiCommandBufferProxy::Echo(const base::Closure
& callback
) {
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).
57 void PpapiCommandBufferProxy::SetChannelErrorCallback(
58 const base::Closure
& callback
) {
59 channel_error_callback_
= callback
;
62 bool PpapiCommandBufferProxy::Initialize() {
63 return Send(new PpapiHostMsg_PPBGraphics3D_InitCommandBuffer(
64 ppapi::API_ID_PPB_GRAPHICS_3D
, resource_
));
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
;
72 if (Send(new PpapiHostMsg_PPBGraphics3D_GetState(
73 ppapi::API_ID_PPB_GRAPHICS_3D
, resource_
, &state
, &success
))) {
74 UpdateState(state
, success
);
81 gpu::CommandBuffer::State
PpapiCommandBufferProxy::GetLastState() {
82 // Note: The locking command buffer wrapper does not take a global lock before
83 // calling this function.
87 void PpapiCommandBufferProxy::Flush(int32 put_offset
) {
88 if (last_state_
.error
!= gpu::error::kNoError
)
91 IPC::Message
* message
= new PpapiHostMsg_PPBGraphics3D_AsyncFlush(
92 ppapi::API_ID_PPB_GRAPHICS_3D
, resource_
, put_offset
);
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);
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
;
107 bool success
= false;
108 if (Send(new PpapiHostMsg_PPBGraphics3D_Flush(
109 ppapi::API_ID_PPB_GRAPHICS_3D
, resource_
, put_offset
,
110 last_known_get
, &state
, &success
))) {
111 UpdateState(state
, success
);
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
));
127 void PpapiCommandBufferProxy::SetGetOffset(int32 get_offset
) {
128 // Not implemented in proxy.
132 gpu::Buffer
PpapiCommandBufferProxy::CreateTransferBuffer(size_t size
,
136 if (last_state_
.error
!= gpu::error::kNoError
)
137 return gpu::Buffer();
139 if (!Send(new PpapiHostMsg_PPBGraphics3D_CreateTransferBuffer(
140 ppapi::API_ID_PPB_GRAPHICS_3D
, resource_
, size
, id
))) {
141 return gpu::Buffer();
145 return gpu::Buffer();
147 return GetTransferBuffer(*id
);
150 void PpapiCommandBufferProxy::DestroyTransferBuffer(int32 id
) {
151 if (last_state_
.error
!= gpu::error::kNoError
)
154 // Remove the transfer buffer from the client side4 cache.
155 TransferBufferMap::iterator it
= transfer_buffers_
.find(id
);
157 if (it
!= transfer_buffers_
.end()) {
158 // Delete the shared memory object, closing the handle in this process.
159 delete it
->second
.shared_memory
;
161 transfer_buffers_
.erase(it
);
164 Send(new PpapiHostMsg_PPBGraphics3D_DestroyTransferBuffer(
165 ppapi::API_ID_PPB_GRAPHICS_3D
, resource_
, id
));
168 gpu::Buffer
PpapiCommandBufferProxy::GetTransferBuffer(int32 id
) {
169 if (last_state_
.error
!= gpu::error::kNoError
)
170 return gpu::Buffer();
172 // Check local cache to see if there is already a client side shared memory
173 // object for this id.
174 TransferBufferMap::iterator it
= transfer_buffers_
.find(id
);
175 if (it
!= transfer_buffers_
.end()) {
179 // Assuming we are in the renderer process, the service is responsible for
180 // duplicating the handle. This might not be true for NaCl.
181 ppapi::proxy::SerializedHandle
handle(
182 ppapi::proxy::SerializedHandle::SHARED_MEMORY
);
183 if (!Send(new PpapiHostMsg_PPBGraphics3D_GetTransferBuffer(
184 ppapi::API_ID_PPB_GRAPHICS_3D
, resource_
, id
, &handle
))) {
185 return gpu::Buffer();
187 if (!handle
.is_shmem())
188 return gpu::Buffer();
190 // Cache the transfer buffer shared memory object client side.
191 scoped_ptr
<base::SharedMemory
> shared_memory(
192 new base::SharedMemory(handle
.shmem(), false));
194 // Map the shared memory on demand.
195 if (!shared_memory
->memory()) {
196 if (!shared_memory
->Map(handle
.size())) {
197 return gpu::Buffer();
202 buffer
.ptr
= shared_memory
->memory();
203 buffer
.size
= handle
.size();
204 buffer
.shared_memory
= shared_memory
.release();
205 transfer_buffers_
[id
] = buffer
;
210 void PpapiCommandBufferProxy::SetToken(int32 token
) {
214 void PpapiCommandBufferProxy::SetParseError(gpu::error::Error error
) {
218 void PpapiCommandBufferProxy::SetContextLostReason(
219 gpu::error::ContextLostReason reason
) {
223 bool PpapiCommandBufferProxy::Send(IPC::Message
* msg
) {
224 DCHECK(last_state_
.error
== gpu::error::kNoError
);
226 if (channel_
->Send(msg
))
229 last_state_
.error
= gpu::error::kLostContext
;
233 void PpapiCommandBufferProxy::UpdateState(
234 const gpu::CommandBuffer::State
& state
,
236 // Handle wraparound. It works as long as we don't have more than 2B state
237 // updates in flight across which reordering occurs.
239 if (state
.generation
- last_state_
.generation
< 0x80000000U
) {
243 last_state_
.error
= gpu::error::kLostContext
;
244 ++last_state_
.generation
;