Introduce proximity_auth component.
[chromium-blink-merge.git] / ppapi / proxy / ppapi_command_buffer_proxy.cc
blob6c1093c8d06c06893653dda0b45101538454b45d
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"
13 namespace ppapi {
14 namespace proxy {
16 PpapiCommandBufferProxy::PpapiCommandBufferProxy(
17 const ppapi::HostResource& resource,
18 ProxyChannel* channel)
19 : resource_(resource),
20 channel_(channel) {
23 PpapiCommandBufferProxy::~PpapiCommandBufferProxy() {
24 // gpu::Buffers are no longer referenced, allowing shared memory objects to be
25 // deleted, closing the handle in this process.
28 bool PpapiCommandBufferProxy::Initialize() {
29 return true;
32 gpu::CommandBuffer::State PpapiCommandBufferProxy::GetLastState() {
33 ppapi::ProxyLock::AssertAcquiredDebugOnly();
34 return last_state_;
37 int32 PpapiCommandBufferProxy::GetLastToken() {
38 ppapi::ProxyLock::AssertAcquiredDebugOnly();
39 return last_state_.token;
42 void PpapiCommandBufferProxy::Flush(int32 put_offset) {
43 if (last_state_.error != gpu::error::kNoError)
44 return;
46 IPC::Message* message = new PpapiHostMsg_PPBGraphics3D_AsyncFlush(
47 ppapi::API_ID_PPB_GRAPHICS_3D, resource_, put_offset);
49 // Do not let a synchronous flush hold up this message. If this handler is
50 // deferred until after the synchronous flush completes, it will overwrite the
51 // cached last_state_ with out-of-date data.
52 message->set_unblock(true);
53 Send(message);
56 void PpapiCommandBufferProxy::WaitForTokenInRange(int32 start, int32 end) {
57 if (last_state_.error != gpu::error::kNoError)
58 return;
60 bool success = false;
61 gpu::CommandBuffer::State state;
62 if (Send(new PpapiHostMsg_PPBGraphics3D_WaitForTokenInRange(
63 ppapi::API_ID_PPB_GRAPHICS_3D,
64 resource_,
65 start,
66 end,
67 &state,
68 &success)))
69 UpdateState(state, success);
72 void PpapiCommandBufferProxy::WaitForGetOffsetInRange(int32 start, int32 end) {
73 if (last_state_.error != gpu::error::kNoError)
74 return;
76 bool success = false;
77 gpu::CommandBuffer::State state;
78 if (Send(new PpapiHostMsg_PPBGraphics3D_WaitForGetOffsetInRange(
79 ppapi::API_ID_PPB_GRAPHICS_3D,
80 resource_,
81 start,
82 end,
83 &state,
84 &success)))
85 UpdateState(state, success);
88 void PpapiCommandBufferProxy::SetGetBuffer(int32 transfer_buffer_id) {
89 if (last_state_.error == gpu::error::kNoError) {
90 Send(new PpapiHostMsg_PPBGraphics3D_SetGetBuffer(
91 ppapi::API_ID_PPB_GRAPHICS_3D, resource_, transfer_buffer_id));
95 scoped_refptr<gpu::Buffer> PpapiCommandBufferProxy::CreateTransferBuffer(
96 size_t size,
97 int32* id) {
98 *id = -1;
100 if (last_state_.error != gpu::error::kNoError)
101 return NULL;
103 // Assuming we are in the renderer process, the service is responsible for
104 // duplicating the handle. This might not be true for NaCl.
105 ppapi::proxy::SerializedHandle handle(
106 ppapi::proxy::SerializedHandle::SHARED_MEMORY);
107 if (!Send(new PpapiHostMsg_PPBGraphics3D_CreateTransferBuffer(
108 ppapi::API_ID_PPB_GRAPHICS_3D, resource_, size, id, &handle))) {
109 return NULL;
112 if (*id <= 0 || !handle.is_shmem())
113 return NULL;
115 scoped_ptr<base::SharedMemory> shared_memory(
116 new base::SharedMemory(handle.shmem(), false));
118 // Map the shared memory on demand.
119 if (!shared_memory->memory()) {
120 if (!shared_memory->Map(handle.size())) {
121 *id = -1;
122 return NULL;
126 return gpu::MakeBufferFromSharedMemory(shared_memory.Pass(), handle.size());
129 void PpapiCommandBufferProxy::DestroyTransferBuffer(int32 id) {
130 if (last_state_.error != gpu::error::kNoError)
131 return;
133 Send(new PpapiHostMsg_PPBGraphics3D_DestroyTransferBuffer(
134 ppapi::API_ID_PPB_GRAPHICS_3D, resource_, id));
137 void PpapiCommandBufferProxy::Echo(const base::Closure& callback) {
138 NOTREACHED();
141 uint32 PpapiCommandBufferProxy::CreateStreamTexture(uint32 texture_id) {
142 NOTREACHED();
143 return 0;
146 uint32 PpapiCommandBufferProxy::InsertSyncPoint() {
147 uint32 sync_point = 0;
148 if (last_state_.error == gpu::error::kNoError) {
149 Send(new PpapiHostMsg_PPBGraphics3D_InsertSyncPoint(
150 ppapi::API_ID_PPB_GRAPHICS_3D, resource_, &sync_point));
152 return sync_point;
155 uint32 PpapiCommandBufferProxy::InsertFutureSyncPoint() {
156 uint32 sync_point = 0;
157 if (last_state_.error == gpu::error::kNoError) {
158 Send(new PpapiHostMsg_PPBGraphics3D_InsertFutureSyncPoint(
159 ppapi::API_ID_PPB_GRAPHICS_3D, resource_, &sync_point));
161 return sync_point;
164 void PpapiCommandBufferProxy::RetireSyncPoint(uint32 sync_point) {
165 if (last_state_.error == gpu::error::kNoError) {
166 Send(new PpapiHostMsg_PPBGraphics3D_RetireSyncPoint(
167 ppapi::API_ID_PPB_GRAPHICS_3D, resource_, sync_point));
171 void PpapiCommandBufferProxy::SignalSyncPoint(uint32 sync_point,
172 const base::Closure& callback) {
173 NOTREACHED();
176 void PpapiCommandBufferProxy::SignalQuery(uint32 query,
177 const base::Closure& callback) {
178 NOTREACHED();
181 void PpapiCommandBufferProxy::SetSurfaceVisible(bool visible) {
182 NOTREACHED();
185 gpu::Capabilities PpapiCommandBufferProxy::GetCapabilities() {
186 // TODO(boliu): Need to implement this to use cc in Pepper. Tracked in
187 // crbug.com/325391.
188 return gpu::Capabilities();
191 gfx::GpuMemoryBuffer* PpapiCommandBufferProxy::CreateGpuMemoryBuffer(
192 size_t width,
193 size_t height,
194 unsigned internalformat,
195 unsigned usage,
196 int32* id) {
197 NOTREACHED();
198 return NULL;
201 void PpapiCommandBufferProxy::DestroyGpuMemoryBuffer(int32 id) {
202 NOTREACHED();
205 bool PpapiCommandBufferProxy::Send(IPC::Message* msg) {
206 DCHECK(last_state_.error == gpu::error::kNoError);
208 if (channel_->Send(msg))
209 return true;
211 last_state_.error = gpu::error::kLostContext;
212 return false;
215 void PpapiCommandBufferProxy::UpdateState(
216 const gpu::CommandBuffer::State& state,
217 bool success) {
218 // Handle wraparound. It works as long as we don't have more than 2B state
219 // updates in flight across which reordering occurs.
220 if (success) {
221 if (state.generation - last_state_.generation < 0x80000000U) {
222 last_state_ = state;
224 } else {
225 last_state_.error = gpu::error::kLostContext;
226 ++last_state_.generation;
230 } // namespace proxy
231 } // namespace ppapi