Release the Settings API.
[chromium-blink-merge.git] / ppapi / proxy / ppb_graphics_3d_proxy.cc
blob85692d40569bd2273f1a140461bdf993a78bf26c
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/ppb_graphics_3d_proxy.h"
7 #include "gpu/command_buffer/client/gles2_implementation.h"
8 #include "gpu/command_buffer/common/command_buffer.h"
9 #include "ppapi/c/pp_errors.h"
10 #include "ppapi/proxy/enter_proxy.h"
11 #include "ppapi/proxy/plugin_dispatcher.h"
12 #include "ppapi/proxy/ppapi_command_buffer_proxy.h"
13 #include "ppapi/proxy/ppapi_messages.h"
14 #include "ppapi/shared_impl/ppapi_globals.h"
15 #include "ppapi/thunk/enter.h"
16 #include "ppapi/thunk/resource_creation_api.h"
17 #include "ppapi/thunk/thunk.h"
19 using ppapi::thunk::EnterResourceNoLock;
20 using ppapi::thunk::PPB_Graphics3D_API;
21 using ppapi::thunk::ResourceCreationAPI;
23 namespace ppapi {
24 namespace proxy {
26 namespace {
28 const int32 kCommandBufferSize = 1024 * 1024;
29 const int32 kTransferBufferSize = 1024 * 1024;
31 base::SharedMemoryHandle TransportSHMHandle(Dispatcher* dispatcher,
32 base::SharedMemory* shm) {
33 base::PlatformFile source =
34 IPC::PlatformFileForTransitToPlatformFile(shm->handle());
35 // Don't close the handle, it doesn't belong to us.
36 return dispatcher->ShareHandleWithRemote(source, false);
39 gpu::CommandBuffer::State GetErrorState() {
40 gpu::CommandBuffer::State error_state;
41 error_state.error = gpu::error::kGenericError;
42 return error_state;
45 } // namespace
47 Graphics3D::Graphics3D(const HostResource& resource)
48 : PPB_Graphics3D_Shared(resource) {
51 Graphics3D::~Graphics3D() {
52 DestroyGLES2Impl();
55 bool Graphics3D::Init(gpu::gles2::GLES2Implementation* share_gles2) {
56 PluginDispatcher* dispatcher = PluginDispatcher::GetForResource(this);
57 if (!dispatcher)
58 return false;
60 command_buffer_.reset(
61 new PpapiCommandBufferProxy(host_resource(), dispatcher));
63 return CreateGLES2Impl(kCommandBufferSize, kTransferBufferSize,
64 share_gles2);
67 PP_Bool Graphics3D::SetGetBuffer(int32_t /* transfer_buffer_id */) {
68 return PP_FALSE;
71 gpu::CommandBuffer::State Graphics3D::GetState() {
72 return GetErrorState();
75 PP_Bool Graphics3D::Flush(int32_t put_offset) {
76 return PP_FALSE;
79 scoped_refptr<gpu::Buffer> Graphics3D::CreateTransferBuffer(
80 uint32_t size,
81 int32_t* id) {
82 *id = -1;
83 return NULL;
86 PP_Bool Graphics3D::DestroyTransferBuffer(int32_t id) {
87 return PP_FALSE;
90 gpu::CommandBuffer::State Graphics3D::WaitForTokenInRange(int32_t start,
91 int32_t end) {
92 return GetErrorState();
95 gpu::CommandBuffer::State Graphics3D::WaitForGetOffsetInRange(int32_t start,
96 int32_t end) {
97 return GetErrorState();
100 uint32_t Graphics3D::InsertSyncPoint() {
101 NOTREACHED();
102 return 0;
105 gpu::CommandBuffer* Graphics3D::GetCommandBuffer() {
106 return command_buffer_.get();
109 gpu::GpuControl* Graphics3D::GetGpuControl() {
110 return command_buffer_.get();
113 int32 Graphics3D::DoSwapBuffers() {
114 gles2_impl()->SwapBuffers();
115 IPC::Message* msg = new PpapiHostMsg_PPBGraphics3D_SwapBuffers(
116 API_ID_PPB_GRAPHICS_3D, host_resource());
117 msg->set_unblock(true);
118 PluginDispatcher::GetForResource(this)->Send(msg);
120 return PP_OK_COMPLETIONPENDING;
123 PPB_Graphics3D_Proxy::PPB_Graphics3D_Proxy(Dispatcher* dispatcher)
124 : InterfaceProxy(dispatcher),
125 callback_factory_(this) {
128 PPB_Graphics3D_Proxy::~PPB_Graphics3D_Proxy() {
131 // static
132 PP_Resource PPB_Graphics3D_Proxy::CreateProxyResource(
133 PP_Instance instance,
134 PP_Resource share_context,
135 const int32_t* attrib_list) {
136 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
137 if (!dispatcher)
138 return PP_ERROR_BADARGUMENT;
140 HostResource share_host;
141 gpu::gles2::GLES2Implementation* share_gles2 = NULL;
142 if (share_context != 0) {
143 EnterResourceNoLock<PPB_Graphics3D_API> enter(share_context, true);
144 if (enter.failed())
145 return PP_ERROR_BADARGUMENT;
147 PPB_Graphics3D_Shared* share_graphics =
148 static_cast<PPB_Graphics3D_Shared*>(enter.object());
149 share_host = share_graphics->host_resource();
150 share_gles2 = share_graphics->gles2_impl();
153 std::vector<int32_t> attribs;
154 if (attrib_list) {
155 for (const int32_t* attr = attrib_list;
156 attr[0] != PP_GRAPHICS3DATTRIB_NONE;
157 attr += 2) {
158 attribs.push_back(attr[0]);
159 attribs.push_back(attr[1]);
162 attribs.push_back(PP_GRAPHICS3DATTRIB_NONE);
164 HostResource result;
165 dispatcher->Send(new PpapiHostMsg_PPBGraphics3D_Create(
166 API_ID_PPB_GRAPHICS_3D, instance, share_host, attribs, &result));
167 if (result.is_null())
168 return 0;
170 scoped_refptr<Graphics3D> graphics_3d(new Graphics3D(result));
171 if (!graphics_3d->Init(share_gles2))
172 return 0;
173 return graphics_3d->GetReference();
176 bool PPB_Graphics3D_Proxy::OnMessageReceived(const IPC::Message& msg) {
177 bool handled = true;
178 IPC_BEGIN_MESSAGE_MAP(PPB_Graphics3D_Proxy, msg)
179 #if !defined(OS_NACL)
180 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_Create,
181 OnMsgCreate)
182 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_SetGetBuffer,
183 OnMsgSetGetBuffer)
184 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_GetState,
185 OnMsgGetState)
186 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_WaitForTokenInRange,
187 OnMsgWaitForTokenInRange)
188 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_WaitForGetOffsetInRange,
189 OnMsgWaitForGetOffsetInRange)
190 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_AsyncFlush, OnMsgAsyncFlush)
191 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_CreateTransferBuffer,
192 OnMsgCreateTransferBuffer)
193 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_DestroyTransferBuffer,
194 OnMsgDestroyTransferBuffer)
195 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_SwapBuffers,
196 OnMsgSwapBuffers)
197 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_InsertSyncPoint,
198 OnMsgInsertSyncPoint)
199 #endif // !defined(OS_NACL)
201 IPC_MESSAGE_HANDLER(PpapiMsg_PPBGraphics3D_SwapBuffersACK,
202 OnMsgSwapBuffersACK)
203 IPC_MESSAGE_UNHANDLED(handled = false)
205 IPC_END_MESSAGE_MAP()
206 // FIXME(brettw) handle bad messages!
207 return handled;
210 #if !defined(OS_NACL)
211 void PPB_Graphics3D_Proxy::OnMsgCreate(PP_Instance instance,
212 HostResource share_context,
213 const std::vector<int32_t>& attribs,
214 HostResource* result) {
215 if (attribs.empty() ||
216 attribs.back() != PP_GRAPHICS3DATTRIB_NONE ||
217 !(attribs.size() & 1))
218 return; // Bad message.
220 thunk::EnterResourceCreation enter(instance);
222 if (enter.succeeded()) {
223 result->SetHostResource(
224 instance,
225 enter.functions()->CreateGraphics3DRaw(instance,
226 share_context.host_resource(),
227 &attribs.front()));
231 void PPB_Graphics3D_Proxy::OnMsgSetGetBuffer(
232 const HostResource& context,
233 int32 transfer_buffer_id) {
234 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
235 if (enter.succeeded())
236 enter.object()->SetGetBuffer(transfer_buffer_id);
239 void PPB_Graphics3D_Proxy::OnMsgGetState(const HostResource& context,
240 gpu::CommandBuffer::State* state,
241 bool* success) {
242 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
243 if (enter.failed()) {
244 *success = false;
245 return;
247 *state = enter.object()->GetState();
248 *success = true;
251 void PPB_Graphics3D_Proxy::OnMsgWaitForTokenInRange(
252 const HostResource& context,
253 int32 start,
254 int32 end,
255 gpu::CommandBuffer::State* state,
256 bool* success) {
257 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
258 if (enter.failed()) {
259 *success = false;
260 return;
262 *state = enter.object()->WaitForTokenInRange(start, end);
263 *success = true;
266 void PPB_Graphics3D_Proxy::OnMsgWaitForGetOffsetInRange(
267 const HostResource& context,
268 int32 start,
269 int32 end,
270 gpu::CommandBuffer::State* state,
271 bool* success) {
272 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
273 if (enter.failed()) {
274 *success = false;
275 return;
277 *state = enter.object()->WaitForGetOffsetInRange(start, end);
278 *success = true;
281 void PPB_Graphics3D_Proxy::OnMsgAsyncFlush(const HostResource& context,
282 int32 put_offset) {
283 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
284 if (enter.succeeded())
285 enter.object()->Flush(put_offset);
288 void PPB_Graphics3D_Proxy::OnMsgCreateTransferBuffer(
289 const HostResource& context,
290 uint32 size,
291 int32* id,
292 ppapi::proxy::SerializedHandle* transfer_buffer) {
293 transfer_buffer->set_null_shmem();
294 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
295 if (enter.succeeded()) {
296 scoped_refptr<gpu::Buffer> buffer =
297 enter.object()->CreateTransferBuffer(size, id);
298 if (!buffer)
299 return;
300 gpu::SharedMemoryBufferBacking* backing =
301 static_cast<gpu::SharedMemoryBufferBacking*>(buffer->backing());
302 DCHECK(backing && backing->shared_memory());
303 transfer_buffer->set_shmem(
304 TransportSHMHandle(dispatcher(), backing->shared_memory()),
305 buffer->size());
306 } else {
307 *id = -1;
311 void PPB_Graphics3D_Proxy::OnMsgDestroyTransferBuffer(
312 const HostResource& context,
313 int32 id) {
314 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
315 if (enter.succeeded())
316 enter.object()->DestroyTransferBuffer(id);
319 void PPB_Graphics3D_Proxy::OnMsgSwapBuffers(const HostResource& context) {
320 EnterHostFromHostResourceForceCallback<PPB_Graphics3D_API> enter(
321 context, callback_factory_,
322 &PPB_Graphics3D_Proxy::SendSwapBuffersACKToPlugin, context);
323 if (enter.succeeded())
324 enter.SetResult(enter.object()->SwapBuffers(enter.callback()));
327 void PPB_Graphics3D_Proxy::OnMsgInsertSyncPoint(const HostResource& context,
328 uint32* sync_point) {
329 *sync_point = 0;
330 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
331 if (enter.succeeded())
332 *sync_point = enter.object()->InsertSyncPoint();
334 #endif // !defined(OS_NACL)
336 void PPB_Graphics3D_Proxy::OnMsgSwapBuffersACK(const HostResource& resource,
337 int32_t pp_error) {
338 EnterPluginFromHostResource<PPB_Graphics3D_API> enter(resource);
339 if (enter.succeeded())
340 static_cast<Graphics3D*>(enter.object())->SwapBuffersACK(pp_error);
343 #if !defined(OS_NACL)
344 void PPB_Graphics3D_Proxy::SendSwapBuffersACKToPlugin(
345 int32_t result,
346 const HostResource& context) {
347 dispatcher()->Send(new PpapiMsg_PPBGraphics3D_SwapBuffersACK(
348 API_ID_PPB_GRAPHICS_3D, context, result));
350 #endif // !defined(OS_NACL)
352 } // namespace proxy
353 } // namespace ppapi