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 void PpapiCommandBufferProxy::Echo(const base::Closure& callback) {
152 uint32 PpapiCommandBufferProxy::CreateStreamTexture(uint32 texture_id) {
157 uint32 PpapiCommandBufferProxy::InsertSyncPoint() {
158 uint32 sync_point = 0;
159 if (last_state_.error == gpu::error::kNoError) {
160 Send(new PpapiHostMsg_PPBGraphics3D_InsertSyncPoint(
161 ppapi::API_ID_PPB_GRAPHICS_3D, resource_, &sync_point));
166 uint32 PpapiCommandBufferProxy::InsertFutureSyncPoint() {
167 uint32 sync_point = 0;
168 if (last_state_.error == gpu::error::kNoError) {
169 Send(new PpapiHostMsg_PPBGraphics3D_InsertFutureSyncPoint(
170 ppapi::API_ID_PPB_GRAPHICS_3D, resource_, &sync_point));
175 void PpapiCommandBufferProxy::RetireSyncPoint(uint32 sync_point) {
176 if (last_state_.error == gpu::error::kNoError) {
177 Send(new PpapiHostMsg_PPBGraphics3D_RetireSyncPoint(
178 ppapi::API_ID_PPB_GRAPHICS_3D, resource_, sync_point));
182 void PpapiCommandBufferProxy::SignalSyncPoint(uint32 sync_point,
183 const base::Closure& callback) {
187 void PpapiCommandBufferProxy::SignalQuery(uint32 query,
188 const base::Closure& callback) {
192 void PpapiCommandBufferProxy::SetSurfaceVisible(bool visible) {
196 gpu::Capabilities PpapiCommandBufferProxy::GetCapabilities() {
197 // TODO(boliu): Need to implement this to use cc in Pepper. Tracked in
199 return gpu::Capabilities();
202 gfx::GpuMemoryBuffer* PpapiCommandBufferProxy::CreateGpuMemoryBuffer(
205 unsigned internalformat,
212 void PpapiCommandBufferProxy::DestroyGpuMemoryBuffer(int32 id) {
216 bool PpapiCommandBufferProxy::Send(IPC::Message* msg) {
217 DCHECK(last_state_.error == gpu::error::kNoError);
219 if (channel_->Send(msg))
222 last_state_.error = gpu::error::kLostContext;
226 void PpapiCommandBufferProxy::UpdateState(
227 const gpu::CommandBuffer::State& state,
229 // Handle wraparound. It works as long as we don't have more than 2B state
230 // updates in flight across which reordering occurs.
232 if (state.generation - last_state_.generation < 0x80000000U) {
236 last_state_.error = gpu::error::kLostContext;
237 ++last_state_.generation;
241 void PpapiCommandBufferProxy::TryUpdateState() {
242 if (last_state_.error == gpu::error::kNoError)
243 shared_state()->Read(&last_state_);
246 gpu::CommandBufferSharedState* PpapiCommandBufferProxy::shared_state() const {
247 return reinterpret_cast<gpu::CommandBufferSharedState*>(
248 shared_state_shm_->memory());