Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / ppapi / proxy / ppapi_command_buffer_proxy.cc
blobe9a8a35083c9878062704b3e3da13151717a2d0f
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;
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;
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 void PpapiCommandBufferProxy::SignalSyncPoint(uint32 sync_point,
156 const base::Closure& callback) {
157 NOTREACHED();
160 void PpapiCommandBufferProxy::SignalQuery(uint32 query,
161 const base::Closure& callback) {
162 NOTREACHED();
165 void PpapiCommandBufferProxy::SetSurfaceVisible(bool visible) {
166 NOTREACHED();
169 void PpapiCommandBufferProxy::SendManagedMemoryStats(
170 const gpu::ManagedMemoryStats& stats) {
171 NOTREACHED();
174 gpu::Capabilities PpapiCommandBufferProxy::GetCapabilities() {
175 // TODO(boliu): Need to implement this to use cc in Pepper. Tracked in
176 // crbug.com/325391.
177 return gpu::Capabilities();
180 gfx::GpuMemoryBuffer* PpapiCommandBufferProxy::CreateGpuMemoryBuffer(
181 size_t width,
182 size_t height,
183 unsigned internalformat,
184 int32* id) {
185 NOTREACHED();
186 return NULL;
189 void PpapiCommandBufferProxy::DestroyGpuMemoryBuffer(int32 id) {
190 NOTREACHED();
193 bool PpapiCommandBufferProxy::Send(IPC::Message* msg) {
194 DCHECK(last_state_.error == gpu::error::kNoError);
196 if (channel_->Send(msg))
197 return true;
199 last_state_.error = gpu::error::kLostContext;
200 return false;
203 void PpapiCommandBufferProxy::UpdateState(
204 const gpu::CommandBuffer::State& state,
205 bool success) {
206 // Handle wraparound. It works as long as we don't have more than 2B state
207 // updates in flight across which reordering occurs.
208 if (success) {
209 if (state.generation - last_state_.generation < 0x80000000U) {
210 last_state_ = state;
212 } else {
213 last_state_.error = gpu::error::kLostContext;
214 ++last_state_.generation;
218 } // namespace proxy
219 } // namespace ppapi