blob: 02e06f76b8fff4677e5637335a6cf08683fa1d81 [file] [log] [blame]
[email protected]aaa11b32012-03-08 12:30:131// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]eeb4e4a2011-07-19 16:22:062// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ppapi/proxy/ppb_graphics_3d_proxy.h"
6
7#include "gpu/command_buffer/client/gles2_implementation.h"
[email protected]84b44c552012-10-15 20:58:178#include "gpu/command_buffer/common/command_buffer.h"
[email protected]eeb4e4a2011-07-19 16:22:069#include "ppapi/c/pp_errors.h"
10#include "ppapi/proxy/enter_proxy.h"
11#include "ppapi/proxy/plugin_dispatcher.h"
[email protected]aaa11b32012-03-08 12:30:1312#include "ppapi/proxy/ppapi_command_buffer_proxy.h"
[email protected]eeb4e4a2011-07-19 16:22:0613#include "ppapi/proxy/ppapi_messages.h"
[email protected]84b44c552012-10-15 20:58:1714#include "ppapi/shared_impl/ppapi_globals.h"
[email protected]eeb4e4a2011-07-19 16:22:0615#include "ppapi/thunk/enter.h"
16#include "ppapi/thunk/resource_creation_api.h"
17#include "ppapi/thunk/thunk.h"
18
[email protected]eeb4e4a2011-07-19 16:22:0619using ppapi::thunk::EnterResourceNoLock;
20using ppapi::thunk::PPB_Graphics3D_API;
21using ppapi::thunk::ResourceCreationAPI;
22
[email protected]4d2efd22011-08-18 21:58:0223namespace ppapi {
[email protected]eeb4e4a2011-07-19 16:22:0624namespace proxy {
25
26namespace {
[email protected]84b44c552012-10-15 20:58:1727
[email protected]eeb4e4a2011-07-19 16:22:0628const int32 kCommandBufferSize = 1024 * 1024;
29const int32 kTransferBufferSize = 1024 * 1024;
30
[email protected]eeb4e4a2011-07-19 16:22:0631base::SharedMemoryHandle TransportSHMHandleFromInt(Dispatcher* dispatcher,
32 int shm_handle) {
33 // TODO(piman): Change trusted interface to return a PP_FileHandle, those
34 // casts are ugly.
35 base::PlatformFile source =
36#if defined(OS_WIN)
37 reinterpret_cast<HANDLE>(static_cast<intptr_t>(shm_handle));
38#elif defined(OS_POSIX)
39 shm_handle;
40#else
41 #error Not implemented.
42#endif
43 // Don't close the handle, it doesn't belong to us.
44 return dispatcher->ShareHandleWithRemote(source, false);
45}
46
[email protected]914f5262013-06-01 00:17:2247gpu::CommandBuffer::State GetErrorState() {
48 gpu::CommandBuffer::State error_state;
49 error_state.error = gpu::error::kGenericError;
[email protected]eeb4e4a2011-07-19 16:22:0650 return error_state;
51}
52
[email protected]eeb4e4a2011-07-19 16:22:0653} // namespace
54
[email protected]eeb4e4a2011-07-19 16:22:0655Graphics3D::Graphics3D(const HostResource& resource)
[email protected]4e46a732013-09-30 18:35:5956 : PPB_Graphics3D_Shared(resource) {
[email protected]eeb4e4a2011-07-19 16:22:0657}
58
59Graphics3D::~Graphics3D() {
[email protected]edccb562013-10-02 00:01:2060 DestroyGLES2Impl();
[email protected]eeb4e4a2011-07-19 16:22:0661}
62
[email protected]9ed07f82012-05-29 21:54:5563bool Graphics3D::Init(gpu::gles2::GLES2Implementation* share_gles2) {
[email protected]7f8b26b2011-08-18 15:41:0164 PluginDispatcher* dispatcher = PluginDispatcher::GetForResource(this);
[email protected]eeb4e4a2011-07-19 16:22:0665 if (!dispatcher)
66 return false;
67
[email protected]aaa11b32012-03-08 12:30:1368 command_buffer_.reset(
69 new PpapiCommandBufferProxy(host_resource(), dispatcher));
[email protected]84b44c552012-10-15 20:58:1770
[email protected]9ed07f82012-05-29 21:54:5571 return CreateGLES2Impl(kCommandBufferSize, kTransferBufferSize,
72 share_gles2);
[email protected]eeb4e4a2011-07-19 16:22:0673}
74
[email protected]503b3a22011-12-12 23:29:4075PP_Bool Graphics3D::SetGetBuffer(int32_t /* transfer_buffer_id */) {
[email protected]eeb4e4a2011-07-19 16:22:0676 return PP_FALSE;
77}
78
[email protected]914f5262013-06-01 00:17:2279gpu::CommandBuffer::State Graphics3D::GetState() {
[email protected]eeb4e4a2011-07-19 16:22:0680 return GetErrorState();
81}
82
83PP_Bool Graphics3D::Flush(int32_t put_offset) {
84 return PP_FALSE;
85}
86
[email protected]914f5262013-06-01 00:17:2287gpu::CommandBuffer::State Graphics3D::FlushSync(int32_t put_offset) {
[email protected]eeb4e4a2011-07-19 16:22:0688 return GetErrorState();
89}
90
91int32_t Graphics3D::CreateTransferBuffer(uint32_t size) {
92 return PP_FALSE;
93}
94
95PP_Bool Graphics3D::DestroyTransferBuffer(int32_t id) {
96 return PP_FALSE;
97}
98
99PP_Bool Graphics3D::GetTransferBuffer(int32_t id,
100 int* shm_handle,
101 uint32_t* shm_size) {
102 return PP_FALSE;
103}
104
[email protected]914f5262013-06-01 00:17:22105gpu::CommandBuffer::State Graphics3D::FlushSyncFast(int32_t put_offset,
[email protected]eeb4e4a2011-07-19 16:22:06106 int32_t last_known_get) {
107 return GetErrorState();
108}
109
[email protected]b096d032013-03-08 03:08:01110uint32_t Graphics3D::InsertSyncPoint() {
111 NOTREACHED();
112 return 0;
113}
114
[email protected]eeb4e4a2011-07-19 16:22:06115gpu::CommandBuffer* Graphics3D::GetCommandBuffer() {
[email protected]4e46a732013-09-30 18:35:59116 return command_buffer_.get();
[email protected]eeb4e4a2011-07-19 16:22:06117}
118
[email protected]744e0792013-09-27 01:18:35119gpu::GpuControl* Graphics3D::GetGpuControl() {
120 return command_buffer_.get();
121}
122
[email protected]eeb4e4a2011-07-19 16:22:06123int32 Graphics3D::DoSwapBuffers() {
[email protected]561438abd2011-11-24 01:06:23124 gles2_impl()->SwapBuffers();
[email protected]eeb4e4a2011-07-19 16:22:06125 IPC::Message* msg = new PpapiHostMsg_PPBGraphics3D_SwapBuffers(
[email protected]ac4b54d2011-10-20 23:09:28126 API_ID_PPB_GRAPHICS_3D, host_resource());
[email protected]eeb4e4a2011-07-19 16:22:06127 msg->set_unblock(true);
[email protected]7f8b26b2011-08-18 15:41:01128 PluginDispatcher::GetForResource(this)->Send(msg);
[email protected]eeb4e4a2011-07-19 16:22:06129
[email protected]eeb4e4a2011-07-19 16:22:06130 return PP_OK_COMPLETIONPENDING;
131}
132
[email protected]5c966022011-09-13 18:09:37133PPB_Graphics3D_Proxy::PPB_Graphics3D_Proxy(Dispatcher* dispatcher)
134 : InterfaceProxy(dispatcher),
[email protected]a2f53dc2013-04-30 01:06:35135 callback_factory_(this) {
[email protected]eeb4e4a2011-07-19 16:22:06136}
137
138PPB_Graphics3D_Proxy::~PPB_Graphics3D_Proxy() {
139}
140
141// static
[email protected]eeb4e4a2011-07-19 16:22:06142PP_Resource PPB_Graphics3D_Proxy::CreateProxyResource(
143 PP_Instance instance,
[email protected]eeb4e4a2011-07-19 16:22:06144 PP_Resource share_context,
145 const int32_t* attrib_list) {
146 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
147 if (!dispatcher)
148 return PP_ERROR_BADARGUMENT;
149
[email protected]9ed07f82012-05-29 21:54:55150 HostResource share_host;
151 gpu::gles2::GLES2Implementation* share_gles2 = NULL;
152 if (share_context != 0) {
153 EnterResourceNoLock<PPB_Graphics3D_API> enter(share_context, true);
154 if (enter.failed())
155 return PP_ERROR_BADARGUMENT;
156
157 PPB_Graphics3D_Shared* share_graphics =
158 static_cast<PPB_Graphics3D_Shared*>(enter.object());
159 share_host = share_graphics->host_resource();
160 share_gles2 = share_graphics->gles2_impl();
161 }
[email protected]eeb4e4a2011-07-19 16:22:06162
163 std::vector<int32_t> attribs;
164 if (attrib_list) {
165 for (const int32_t* attr = attrib_list;
[email protected]0e50fc4d2011-08-01 15:33:51166 attr[0] != PP_GRAPHICS3DATTRIB_NONE;
167 attr += 2) {
168 attribs.push_back(attr[0]);
169 attribs.push_back(attr[1]);
[email protected]eeb4e4a2011-07-19 16:22:06170 }
[email protected]eeb4e4a2011-07-19 16:22:06171 }
[email protected]0e50fc4d2011-08-01 15:33:51172 attribs.push_back(PP_GRAPHICS3DATTRIB_NONE);
[email protected]eeb4e4a2011-07-19 16:22:06173
174 HostResource result;
175 dispatcher->Send(new PpapiHostMsg_PPBGraphics3D_Create(
[email protected]9ed07f82012-05-29 21:54:55176 API_ID_PPB_GRAPHICS_3D, instance, share_host, attribs, &result));
[email protected]eeb4e4a2011-07-19 16:22:06177 if (result.is_null())
178 return 0;
179
[email protected]51e1aec2011-08-11 04:48:30180 scoped_refptr<Graphics3D> graphics_3d(new Graphics3D(result));
[email protected]9ed07f82012-05-29 21:54:55181 if (!graphics_3d->Init(share_gles2))
[email protected]eeb4e4a2011-07-19 16:22:06182 return 0;
[email protected]7f8b26b2011-08-18 15:41:01183 return graphics_3d->GetReference();
[email protected]eeb4e4a2011-07-19 16:22:06184}
185
186bool PPB_Graphics3D_Proxy::OnMessageReceived(const IPC::Message& msg) {
187 bool handled = true;
188 IPC_BEGIN_MESSAGE_MAP(PPB_Graphics3D_Proxy, msg)
[email protected]11494c5c2012-11-13 02:50:57189#if !defined(OS_NACL)
[email protected]eeb4e4a2011-07-19 16:22:06190 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_Create,
191 OnMsgCreate)
[email protected]503b3a22011-12-12 23:29:40192 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_SetGetBuffer,
193 OnMsgSetGetBuffer)
[email protected]eeb4e4a2011-07-19 16:22:06194 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_GetState,
195 OnMsgGetState)
196 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_Flush,
197 OnMsgFlush)
198 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_AsyncFlush,
199 OnMsgAsyncFlush)
200 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_CreateTransferBuffer,
201 OnMsgCreateTransferBuffer)
202 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_DestroyTransferBuffer,
203 OnMsgDestroyTransferBuffer)
204 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_GetTransferBuffer,
205 OnMsgGetTransferBuffer)
206 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_SwapBuffers,
207 OnMsgSwapBuffers)
[email protected]b096d032013-03-08 03:08:01208 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_InsertSyncPoint,
209 OnMsgInsertSyncPoint)
[email protected]11494c5c2012-11-13 02:50:57210#endif // !defined(OS_NACL)
[email protected]eeb4e4a2011-07-19 16:22:06211
212 IPC_MESSAGE_HANDLER(PpapiMsg_PPBGraphics3D_SwapBuffersACK,
213 OnMsgSwapBuffersACK)
214 IPC_MESSAGE_UNHANDLED(handled = false)
215
216 IPC_END_MESSAGE_MAP()
217 // FIXME(brettw) handle bad messages!
218 return handled;
219}
220
[email protected]11494c5c2012-11-13 02:50:57221#if !defined(OS_NACL)
[email protected]eeb4e4a2011-07-19 16:22:06222void PPB_Graphics3D_Proxy::OnMsgCreate(PP_Instance instance,
[email protected]9ed07f82012-05-29 21:54:55223 HostResource share_context,
[email protected]eeb4e4a2011-07-19 16:22:06224 const std::vector<int32_t>& attribs,
225 HostResource* result) {
[email protected]48c1db72012-12-17 20:56:11226 if (attribs.empty() ||
227 attribs.back() != PP_GRAPHICS3DATTRIB_NONE ||
228 !(attribs.size() & 1))
[email protected]eeb4e4a2011-07-19 16:22:06229 return; // Bad message.
230
[email protected]5c966022011-09-13 18:09:37231 thunk::EnterResourceCreation enter(instance);
[email protected]9ed07f82012-05-29 21:54:55232
[email protected]eeb4e4a2011-07-19 16:22:06233 if (enter.succeeded()) {
234 result->SetHostResource(
[email protected]9ed07f82012-05-29 21:54:55235 instance,
236 enter.functions()->CreateGraphics3DRaw(instance,
237 share_context.host_resource(),
238 &attribs.front()));
[email protected]eeb4e4a2011-07-19 16:22:06239 }
240}
241
[email protected]503b3a22011-12-12 23:29:40242void PPB_Graphics3D_Proxy::OnMsgSetGetBuffer(
243 const HostResource& context,
244 int32 transfer_buffer_id) {
245 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
246 if (enter.succeeded())
247 enter.object()->SetGetBuffer(transfer_buffer_id);
[email protected]eeb4e4a2011-07-19 16:22:06248}
249
250void PPB_Graphics3D_Proxy::OnMsgGetState(const HostResource& context,
[email protected]571b35e82012-05-19 00:04:35251 gpu::CommandBuffer::State* state,
252 bool* success) {
[email protected]eeb4e4a2011-07-19 16:22:06253 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
[email protected]571b35e82012-05-19 00:04:35254 if (enter.failed()) {
255 *success = false;
[email protected]eeb4e4a2011-07-19 16:22:06256 return;
[email protected]571b35e82012-05-19 00:04:35257 }
[email protected]914f5262013-06-01 00:17:22258 *state = enter.object()->GetState();
[email protected]571b35e82012-05-19 00:04:35259 *success = true;
[email protected]eeb4e4a2011-07-19 16:22:06260}
261
262void PPB_Graphics3D_Proxy::OnMsgFlush(const HostResource& context,
263 int32 put_offset,
264 int32 last_known_get,
[email protected]571b35e82012-05-19 00:04:35265 gpu::CommandBuffer::State* state,
266 bool* success) {
[email protected]eeb4e4a2011-07-19 16:22:06267 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
[email protected]571b35e82012-05-19 00:04:35268 if (enter.failed()) {
269 *success = false;
[email protected]eeb4e4a2011-07-19 16:22:06270 return;
[email protected]571b35e82012-05-19 00:04:35271 }
[email protected]914f5262013-06-01 00:17:22272 *state = enter.object()->FlushSyncFast(put_offset, last_known_get);
[email protected]571b35e82012-05-19 00:04:35273 *success = true;
[email protected]eeb4e4a2011-07-19 16:22:06274}
275
276void PPB_Graphics3D_Proxy::OnMsgAsyncFlush(const HostResource& context,
277 int32 put_offset) {
278 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
279 if (enter.succeeded())
280 enter.object()->Flush(put_offset);
281}
282
283void PPB_Graphics3D_Proxy::OnMsgCreateTransferBuffer(
284 const HostResource& context,
[email protected]b31199e2012-12-12 00:48:29285 uint32 size,
[email protected]eeb4e4a2011-07-19 16:22:06286 int32* id) {
287 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
288 if (enter.succeeded())
289 *id = enter.object()->CreateTransferBuffer(size);
290 else
[email protected]67c80782012-12-21 01:16:52291 *id = -1;
[email protected]eeb4e4a2011-07-19 16:22:06292}
293
294void PPB_Graphics3D_Proxy::OnMsgDestroyTransferBuffer(
295 const HostResource& context,
296 int32 id) {
297 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
298 if (enter.succeeded())
299 enter.object()->DestroyTransferBuffer(id);
300}
301
302void PPB_Graphics3D_Proxy::OnMsgGetTransferBuffer(
303 const HostResource& context,
304 int32 id,
[email protected]246fc492012-08-27 20:28:18305 ppapi::proxy::SerializedHandle* transfer_buffer) {
306 transfer_buffer->set_null_shmem();
[email protected]eeb4e4a2011-07-19 16:22:06307
308 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
309 int shm_handle = 0;
310 uint32_t shm_size = 0;
311 if (enter.succeeded() &&
312 enter.object()->GetTransferBuffer(id, &shm_handle, &shm_size)) {
[email protected]246fc492012-08-27 20:28:18313 transfer_buffer->set_shmem(
314 TransportSHMHandleFromInt(dispatcher(), shm_handle),
315 shm_size);
[email protected]eeb4e4a2011-07-19 16:22:06316 }
317}
318
319void PPB_Graphics3D_Proxy::OnMsgSwapBuffers(const HostResource& context) {
[email protected]79d23c72011-08-08 19:40:40320 EnterHostFromHostResourceForceCallback<PPB_Graphics3D_API> enter(
321 context, callback_factory_,
[email protected]eeb4e4a2011-07-19 16:22:06322 &PPB_Graphics3D_Proxy::SendSwapBuffersACKToPlugin, context);
[email protected]eeb4e4a2011-07-19 16:22:06323 if (enter.succeeded())
[email protected]79d23c72011-08-08 19:40:40324 enter.SetResult(enter.object()->SwapBuffers(enter.callback()));
[email protected]eeb4e4a2011-07-19 16:22:06325}
[email protected]b096d032013-03-08 03:08:01326
327void 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();
333}
[email protected]11494c5c2012-11-13 02:50:57334#endif // !defined(OS_NACL)
[email protected]eeb4e4a2011-07-19 16:22:06335
336void 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);
341}
342
[email protected]11494c5c2012-11-13 02:50:57343#if !defined(OS_NACL)
[email protected]eeb4e4a2011-07-19 16:22:06344void PPB_Graphics3D_Proxy::SendSwapBuffersACKToPlugin(
345 int32_t result,
346 const HostResource& context) {
347 dispatcher()->Send(new PpapiMsg_PPBGraphics3D_SwapBuffersACK(
[email protected]ac4b54d2011-10-20 23:09:28348 API_ID_PPB_GRAPHICS_3D, context, result));
[email protected]eeb4e4a2011-07-19 16:22:06349}
[email protected]11494c5c2012-11-13 02:50:57350#endif // !defined(OS_NACL)
[email protected]eeb4e4a2011-07-19 16:22:06351
352} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02353} // namespace ppapi
[email protected]eeb4e4a2011-07-19 16:22:06354