Change DtmfSenderHandler to handle events on the signaling thread.
[chromium-blink-merge.git] / ppapi / proxy / ppapi_command_buffer_proxy.cc
blob7a2783a20064415baa55dc52cf01cfcc2a25b07d
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 const SerializedHandle& shared_state)
20 : resource_(resource),
21 channel_(channel) {
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() {
33 return true;
36 gpu::CommandBuffer::State PpapiCommandBufferProxy::GetLastState() {
37 ppapi::ProxyLock::AssertAcquiredDebugOnly();
38 return last_state_;
41 int32 PpapiCommandBufferProxy::GetLastToken() {
42 ppapi::ProxyLock::AssertAcquiredDebugOnly();
43 TryUpdateState();
44 return last_state_.token;
47 void PpapiCommandBufferProxy::Flush(int32 put_offset) {
48 if (last_state_.error != gpu::error::kNoError)
49 return;
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);
58 Send(message);
61 void PpapiCommandBufferProxy::WaitForTokenInRange(int32 start, int32 end) {
62 TryUpdateState();
63 if (!InRange(start, end, last_state_.token) &&
64 last_state_.error == gpu::error::kNoError) {
65 bool success = false;
66 gpu::CommandBuffer::State state;
67 if (Send(new PpapiHostMsg_PPBGraphics3D_WaitForTokenInRange(
68 ppapi::API_ID_PPB_GRAPHICS_3D,
69 resource_,
70 start,
71 end,
72 &state,
73 &success)))
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) {
81 TryUpdateState();
82 if (!InRange(start, end, last_state_.get_offset) &&
83 last_state_.error == gpu::error::kNoError) {
84 bool success = false;
85 gpu::CommandBuffer::State state;
86 if (Send(new PpapiHostMsg_PPBGraphics3D_WaitForGetOffsetInRange(
87 ppapi::API_ID_PPB_GRAPHICS_3D,
88 resource_,
89 start,
90 end,
91 &state,
92 &success)))
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(
107 size_t size,
108 int32* id) {
109 *id = -1;
111 if (last_state_.error != gpu::error::kNoError)
112 return NULL;
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))) {
120 return NULL;
123 if (*id <= 0 || !handle.is_shmem())
124 return NULL;
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())) {
132 *id = -1;
133 return NULL;
137 return gpu::MakeBufferFromSharedMemory(shared_memory.Pass(), handle.size());
140 void PpapiCommandBufferProxy::DestroyTransferBuffer(int32 id) {
141 if (last_state_.error != gpu::error::kNoError)
142 return;
144 Send(new PpapiHostMsg_PPBGraphics3D_DestroyTransferBuffer(
145 ppapi::API_ID_PPB_GRAPHICS_3D, resource_, id));
148 uint32 PpapiCommandBufferProxy::CreateStreamTexture(uint32 texture_id) {
149 NOTREACHED();
150 return 0;
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));
159 return 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));
168 return 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) {
180 NOTREACHED();
183 void PpapiCommandBufferProxy::SignalQuery(uint32 query,
184 const base::Closure& callback) {
185 NOTREACHED();
188 void PpapiCommandBufferProxy::SetSurfaceVisible(bool visible) {
189 NOTREACHED();
192 gpu::Capabilities PpapiCommandBufferProxy::GetCapabilities() {
193 // TODO(boliu): Need to implement this to use cc in Pepper. Tracked in
194 // crbug.com/325391.
195 return gpu::Capabilities();
198 int32 PpapiCommandBufferProxy::CreateImage(ClientBuffer buffer,
199 size_t width,
200 size_t height,
201 unsigned internalformat) {
202 NOTREACHED();
203 return -1;
206 void PpapiCommandBufferProxy::DestroyImage(int32 id) {
207 NOTREACHED();
210 int32 PpapiCommandBufferProxy::CreateGpuMemoryBufferImage(
211 size_t width,
212 size_t height,
213 unsigned internalformat,
214 unsigned usage) {
215 NOTREACHED();
216 return -1;
219 bool PpapiCommandBufferProxy::Send(IPC::Message* msg) {
220 DCHECK(last_state_.error == gpu::error::kNoError);
222 if (channel_->Send(msg))
223 return true;
225 last_state_.error = gpu::error::kLostContext;
226 return false;
229 void PpapiCommandBufferProxy::UpdateState(
230 const gpu::CommandBuffer::State& state,
231 bool success) {
232 // Handle wraparound. It works as long as we don't have more than 2B state
233 // updates in flight across which reordering occurs.
234 if (success) {
235 if (state.generation - last_state_.generation < 0x80000000U) {
236 last_state_ = state;
238 } else {
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());
254 } // namespace proxy
255 } // namespace ppapi