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"
11 #include "ppapi/shared_impl/proxy_lock.h"
16 PpapiCommandBufferProxy::PpapiCommandBufferProxy(
17 const ppapi::HostResource
& resource
,
18 ProxyChannel
* channel
,
19 const SerializedHandle
& shared_state
)
20 : resource_(resource
),
22 shared_state_shm_
.reset(
23 new base::SharedMemory(shared_state
.shmem(), false));
24 shared_state_shm_
->Map(shared_state
.size());
27 PpapiCommandBufferProxy::~PpapiCommandBufferProxy() {
28 // gpu::Buffers are no longer referenced, allowing shared memory objects to be
29 // deleted, closing the handle in this process.
32 bool PpapiCommandBufferProxy::Initialize() {
36 gpu::CommandBuffer::State
PpapiCommandBufferProxy::GetLastState() {
37 ppapi::ProxyLock::AssertAcquiredDebugOnly();
41 int32
PpapiCommandBufferProxy::GetLastToken() {
42 ppapi::ProxyLock::AssertAcquiredDebugOnly();
44 return last_state_
.token
;
47 void PpapiCommandBufferProxy::Flush(int32 put_offset
) {
48 if (last_state_
.error
!= gpu::error::kNoError
)
51 IPC::Message
* message
= new PpapiHostMsg_PPBGraphics3D_AsyncFlush(
52 ppapi::API_ID_PPB_GRAPHICS_3D
, resource_
, put_offset
);
54 // Do not let a synchronous flush hold up this message. If this handler is
55 // deferred until after the synchronous flush completes, it will overwrite the
56 // cached last_state_ with out-of-date data.
57 message
->set_unblock(true);
61 void PpapiCommandBufferProxy::WaitForTokenInRange(int32 start
, int32 end
) {
63 if (!InRange(start
, end
, last_state_
.token
) &&
64 last_state_
.error
== gpu::error::kNoError
) {
66 gpu::CommandBuffer::State state
;
67 if (Send(new PpapiHostMsg_PPBGraphics3D_WaitForTokenInRange(
68 ppapi::API_ID_PPB_GRAPHICS_3D
,
74 UpdateState(state
, success
);
76 DCHECK(InRange(start
, end
, last_state_
.token
) ||
77 last_state_
.error
!= gpu::error::kNoError
);
80 void PpapiCommandBufferProxy::WaitForGetOffsetInRange(int32 start
, int32 end
) {
82 if (!InRange(start
, end
, last_state_
.get_offset
) &&
83 last_state_
.error
== gpu::error::kNoError
) {
85 gpu::CommandBuffer::State state
;
86 if (Send(new PpapiHostMsg_PPBGraphics3D_WaitForGetOffsetInRange(
87 ppapi::API_ID_PPB_GRAPHICS_3D
,
93 UpdateState(state
, success
);
95 DCHECK(InRange(start
, end
, last_state_
.get_offset
) ||
96 last_state_
.error
!= gpu::error::kNoError
);
99 void PpapiCommandBufferProxy::SetGetBuffer(int32 transfer_buffer_id
) {
100 if (last_state_
.error
== gpu::error::kNoError
) {
101 Send(new PpapiHostMsg_PPBGraphics3D_SetGetBuffer(
102 ppapi::API_ID_PPB_GRAPHICS_3D
, resource_
, transfer_buffer_id
));
106 scoped_refptr
<gpu::Buffer
> PpapiCommandBufferProxy::CreateTransferBuffer(
111 if (last_state_
.error
!= gpu::error::kNoError
)
114 // Assuming we are in the renderer process, the service is responsible for
115 // duplicating the handle. This might not be true for NaCl.
116 ppapi::proxy::SerializedHandle
handle(
117 ppapi::proxy::SerializedHandle::SHARED_MEMORY
);
118 if (!Send(new PpapiHostMsg_PPBGraphics3D_CreateTransferBuffer(
119 ppapi::API_ID_PPB_GRAPHICS_3D
, resource_
, size
, id
, &handle
))) {
123 if (*id
<= 0 || !handle
.is_shmem())
126 scoped_ptr
<base::SharedMemory
> shared_memory(
127 new base::SharedMemory(handle
.shmem(), false));
129 // Map the shared memory on demand.
130 if (!shared_memory
->memory()) {
131 if (!shared_memory
->Map(handle
.size())) {
137 return gpu::MakeBufferFromSharedMemory(shared_memory
.Pass(), handle
.size());
140 void PpapiCommandBufferProxy::DestroyTransferBuffer(int32 id
) {
141 if (last_state_
.error
!= gpu::error::kNoError
)
144 Send(new PpapiHostMsg_PPBGraphics3D_DestroyTransferBuffer(
145 ppapi::API_ID_PPB_GRAPHICS_3D
, resource_
, id
));
148 uint32
PpapiCommandBufferProxy::CreateStreamTexture(uint32 texture_id
) {
153 uint32
PpapiCommandBufferProxy::InsertSyncPoint() {
154 uint32 sync_point
= 0;
155 if (last_state_
.error
== gpu::error::kNoError
) {
156 Send(new PpapiHostMsg_PPBGraphics3D_InsertSyncPoint(
157 ppapi::API_ID_PPB_GRAPHICS_3D
, resource_
, &sync_point
));
162 uint32
PpapiCommandBufferProxy::InsertFutureSyncPoint() {
163 uint32 sync_point
= 0;
164 if (last_state_
.error
== gpu::error::kNoError
) {
165 Send(new PpapiHostMsg_PPBGraphics3D_InsertFutureSyncPoint(
166 ppapi::API_ID_PPB_GRAPHICS_3D
, resource_
, &sync_point
));
171 void PpapiCommandBufferProxy::RetireSyncPoint(uint32 sync_point
) {
172 if (last_state_
.error
== gpu::error::kNoError
) {
173 Send(new PpapiHostMsg_PPBGraphics3D_RetireSyncPoint(
174 ppapi::API_ID_PPB_GRAPHICS_3D
, resource_
, sync_point
));
178 void PpapiCommandBufferProxy::SignalSyncPoint(uint32 sync_point
,
179 const base::Closure
& callback
) {
183 void PpapiCommandBufferProxy::SignalQuery(uint32 query
,
184 const base::Closure
& callback
) {
188 void PpapiCommandBufferProxy::SetSurfaceVisible(bool visible
) {
192 gpu::Capabilities
PpapiCommandBufferProxy::GetCapabilities() {
193 // TODO(boliu): Need to implement this to use cc in Pepper. Tracked in
195 return gpu::Capabilities();
198 int32
PpapiCommandBufferProxy::CreateImage(ClientBuffer buffer
,
201 unsigned internalformat
) {
206 void PpapiCommandBufferProxy::DestroyImage(int32 id
) {
210 int32
PpapiCommandBufferProxy::CreateGpuMemoryBufferImage(
213 unsigned internalformat
,
219 bool PpapiCommandBufferProxy::Send(IPC::Message
* msg
) {
220 DCHECK(last_state_
.error
== gpu::error::kNoError
);
222 if (channel_
->Send(msg
))
225 last_state_
.error
= gpu::error::kLostContext
;
229 void PpapiCommandBufferProxy::UpdateState(
230 const gpu::CommandBuffer::State
& state
,
232 // Handle wraparound. It works as long as we don't have more than 2B state
233 // updates in flight across which reordering occurs.
235 if (state
.generation
- last_state_
.generation
< 0x80000000U
) {
239 last_state_
.error
= gpu::error::kLostContext
;
240 ++last_state_
.generation
;
244 void PpapiCommandBufferProxy::TryUpdateState() {
245 if (last_state_
.error
== gpu::error::kNoError
)
246 shared_state()->Read(&last_state_
);
249 gpu::CommandBufferSharedState
* PpapiCommandBufferProxy::shared_state() const {
250 return reinterpret_cast<gpu::CommandBufferSharedState
*>(
251 shared_state_shm_
->memory());