blob: 0d383c939603bb6339211afbf05beb8358215aa2 [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]046fa1ac2014-04-01 09:06:4331base::SharedMemoryHandle TransportSHMHandle(Dispatcher* dispatcher,
32 base::SharedMemory* shm) {
[email protected]eeb4e4a2011-07-19 16:22:0633 base::PlatformFile source =
[email protected]046fa1ac2014-04-01 09:06:4334 IPC::PlatformFileForTransitToPlatformFile(shm->handle());
[email protected]eeb4e4a2011-07-19 16:22:0635 // Don't close the handle, it doesn't belong to us.
36 return dispatcher->ShareHandleWithRemote(source, false);
37}
38
[email protected]914f5262013-06-01 00:17:2239gpu::CommandBuffer::State GetErrorState() {
40 gpu::CommandBuffer::State error_state;
41 error_state.error = gpu::error::kGenericError;
[email protected]eeb4e4a2011-07-19 16:22:0642 return error_state;
43}
44
[email protected]eeb4e4a2011-07-19 16:22:0645} // namespace
46
[email protected]eeb4e4a2011-07-19 16:22:0647Graphics3D::Graphics3D(const HostResource& resource)
[email protected]4e46a732013-09-30 18:35:5948 : PPB_Graphics3D_Shared(resource) {
[email protected]eeb4e4a2011-07-19 16:22:0649}
50
51Graphics3D::~Graphics3D() {
[email protected]edccb562013-10-02 00:01:2052 DestroyGLES2Impl();
[email protected]eeb4e4a2011-07-19 16:22:0653}
54
[email protected]9ed07f82012-05-29 21:54:5555bool Graphics3D::Init(gpu::gles2::GLES2Implementation* share_gles2) {
[email protected]7f8b26b2011-08-18 15:41:0156 PluginDispatcher* dispatcher = PluginDispatcher::GetForResource(this);
[email protected]eeb4e4a2011-07-19 16:22:0657 if (!dispatcher)
58 return false;
59
[email protected]aaa11b32012-03-08 12:30:1360 command_buffer_.reset(
61 new PpapiCommandBufferProxy(host_resource(), dispatcher));
[email protected]84b44c552012-10-15 20:58:1762
[email protected]9ed07f82012-05-29 21:54:5563 return CreateGLES2Impl(kCommandBufferSize, kTransferBufferSize,
64 share_gles2);
[email protected]eeb4e4a2011-07-19 16:22:0665}
66
[email protected]503b3a22011-12-12 23:29:4067PP_Bool Graphics3D::SetGetBuffer(int32_t /* transfer_buffer_id */) {
[email protected]eeb4e4a2011-07-19 16:22:0668 return PP_FALSE;
69}
70
[email protected]eeb4e4a2011-07-19 16:22:0671PP_Bool Graphics3D::Flush(int32_t put_offset) {
72 return PP_FALSE;
73}
74
[email protected]046fa1ac2014-04-01 09:06:4375scoped_refptr<gpu::Buffer> Graphics3D::CreateTransferBuffer(
76 uint32_t size,
77 int32_t* id) {
78 *id = -1;
79 return NULL;
[email protected]eeb4e4a2011-07-19 16:22:0680}
81
82PP_Bool Graphics3D::DestroyTransferBuffer(int32_t id) {
83 return PP_FALSE;
84}
85
[email protected]7fe4198b2014-03-18 21:52:3686gpu::CommandBuffer::State Graphics3D::WaitForTokenInRange(int32_t start,
87 int32_t end) {
88 return GetErrorState();
89}
90
91gpu::CommandBuffer::State Graphics3D::WaitForGetOffsetInRange(int32_t start,
92 int32_t end) {
[email protected]eeb4e4a2011-07-19 16:22:0693 return GetErrorState();
94}
95
[email protected]b096d032013-03-08 03:08:0196uint32_t Graphics3D::InsertSyncPoint() {
97 NOTREACHED();
98 return 0;
99}
100
[email protected]7035bc92014-07-01 00:27:22101uint32_t Graphics3D::InsertFutureSyncPoint() {
102 NOTREACHED();
103 return 0;
104}
105
106void Graphics3D::RetireSyncPoint(uint32_t sync_point) {
107 NOTREACHED();
108}
109
[email protected]eeb4e4a2011-07-19 16:22:06110gpu::CommandBuffer* Graphics3D::GetCommandBuffer() {
[email protected]4e46a732013-09-30 18:35:59111 return command_buffer_.get();
[email protected]eeb4e4a2011-07-19 16:22:06112}
113
[email protected]744e0792013-09-27 01:18:35114gpu::GpuControl* Graphics3D::GetGpuControl() {
115 return command_buffer_.get();
116}
117
[email protected]eeb4e4a2011-07-19 16:22:06118int32 Graphics3D::DoSwapBuffers() {
[email protected]561438abd2011-11-24 01:06:23119 gles2_impl()->SwapBuffers();
[email protected]eeb4e4a2011-07-19 16:22:06120 IPC::Message* msg = new PpapiHostMsg_PPBGraphics3D_SwapBuffers(
[email protected]ac4b54d2011-10-20 23:09:28121 API_ID_PPB_GRAPHICS_3D, host_resource());
[email protected]eeb4e4a2011-07-19 16:22:06122 msg->set_unblock(true);
[email protected]7f8b26b2011-08-18 15:41:01123 PluginDispatcher::GetForResource(this)->Send(msg);
[email protected]eeb4e4a2011-07-19 16:22:06124
[email protected]eeb4e4a2011-07-19 16:22:06125 return PP_OK_COMPLETIONPENDING;
126}
127
[email protected]5c966022011-09-13 18:09:37128PPB_Graphics3D_Proxy::PPB_Graphics3D_Proxy(Dispatcher* dispatcher)
129 : InterfaceProxy(dispatcher),
[email protected]a2f53dc2013-04-30 01:06:35130 callback_factory_(this) {
[email protected]eeb4e4a2011-07-19 16:22:06131}
132
133PPB_Graphics3D_Proxy::~PPB_Graphics3D_Proxy() {
134}
135
136// static
[email protected]eeb4e4a2011-07-19 16:22:06137PP_Resource PPB_Graphics3D_Proxy::CreateProxyResource(
138 PP_Instance instance,
[email protected]eeb4e4a2011-07-19 16:22:06139 PP_Resource share_context,
140 const int32_t* attrib_list) {
141 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
142 if (!dispatcher)
143 return PP_ERROR_BADARGUMENT;
144
[email protected]9ed07f82012-05-29 21:54:55145 HostResource share_host;
146 gpu::gles2::GLES2Implementation* share_gles2 = NULL;
147 if (share_context != 0) {
148 EnterResourceNoLock<PPB_Graphics3D_API> enter(share_context, true);
149 if (enter.failed())
150 return PP_ERROR_BADARGUMENT;
151
152 PPB_Graphics3D_Shared* share_graphics =
153 static_cast<PPB_Graphics3D_Shared*>(enter.object());
154 share_host = share_graphics->host_resource();
155 share_gles2 = share_graphics->gles2_impl();
156 }
[email protected]eeb4e4a2011-07-19 16:22:06157
158 std::vector<int32_t> attribs;
159 if (attrib_list) {
160 for (const int32_t* attr = attrib_list;
[email protected]0e50fc4d2011-08-01 15:33:51161 attr[0] != PP_GRAPHICS3DATTRIB_NONE;
162 attr += 2) {
163 attribs.push_back(attr[0]);
164 attribs.push_back(attr[1]);
[email protected]eeb4e4a2011-07-19 16:22:06165 }
[email protected]eeb4e4a2011-07-19 16:22:06166 }
[email protected]0e50fc4d2011-08-01 15:33:51167 attribs.push_back(PP_GRAPHICS3DATTRIB_NONE);
[email protected]eeb4e4a2011-07-19 16:22:06168
169 HostResource result;
170 dispatcher->Send(new PpapiHostMsg_PPBGraphics3D_Create(
[email protected]9ed07f82012-05-29 21:54:55171 API_ID_PPB_GRAPHICS_3D, instance, share_host, attribs, &result));
[email protected]eeb4e4a2011-07-19 16:22:06172 if (result.is_null())
173 return 0;
174
[email protected]51e1aec2011-08-11 04:48:30175 scoped_refptr<Graphics3D> graphics_3d(new Graphics3D(result));
[email protected]9ed07f82012-05-29 21:54:55176 if (!graphics_3d->Init(share_gles2))
[email protected]eeb4e4a2011-07-19 16:22:06177 return 0;
[email protected]7f8b26b2011-08-18 15:41:01178 return graphics_3d->GetReference();
[email protected]eeb4e4a2011-07-19 16:22:06179}
180
181bool PPB_Graphics3D_Proxy::OnMessageReceived(const IPC::Message& msg) {
182 bool handled = true;
183 IPC_BEGIN_MESSAGE_MAP(PPB_Graphics3D_Proxy, msg)
[email protected]11494c5c2012-11-13 02:50:57184#if !defined(OS_NACL)
[email protected]eeb4e4a2011-07-19 16:22:06185 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_Create,
186 OnMsgCreate)
[email protected]503b3a22011-12-12 23:29:40187 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_SetGetBuffer,
188 OnMsgSetGetBuffer)
[email protected]7fe4198b2014-03-18 21:52:36189 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_WaitForTokenInRange,
190 OnMsgWaitForTokenInRange)
191 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_WaitForGetOffsetInRange,
192 OnMsgWaitForGetOffsetInRange)
193 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_AsyncFlush, OnMsgAsyncFlush)
[email protected]eeb4e4a2011-07-19 16:22:06194 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_CreateTransferBuffer,
195 OnMsgCreateTransferBuffer)
196 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_DestroyTransferBuffer,
197 OnMsgDestroyTransferBuffer)
[email protected]eeb4e4a2011-07-19 16:22:06198 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_SwapBuffers,
199 OnMsgSwapBuffers)
[email protected]b096d032013-03-08 03:08:01200 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_InsertSyncPoint,
201 OnMsgInsertSyncPoint)
[email protected]7035bc92014-07-01 00:27:22202 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_InsertFutureSyncPoint,
203 OnMsgInsertFutureSyncPoint)
204 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_RetireSyncPoint,
205 OnMsgRetireSyncPoint)
[email protected]11494c5c2012-11-13 02:50:57206#endif // !defined(OS_NACL)
[email protected]eeb4e4a2011-07-19 16:22:06207
208 IPC_MESSAGE_HANDLER(PpapiMsg_PPBGraphics3D_SwapBuffersACK,
209 OnMsgSwapBuffersACK)
210 IPC_MESSAGE_UNHANDLED(handled = false)
211
212 IPC_END_MESSAGE_MAP()
213 // FIXME(brettw) handle bad messages!
214 return handled;
215}
216
[email protected]11494c5c2012-11-13 02:50:57217#if !defined(OS_NACL)
[email protected]eeb4e4a2011-07-19 16:22:06218void PPB_Graphics3D_Proxy::OnMsgCreate(PP_Instance instance,
[email protected]9ed07f82012-05-29 21:54:55219 HostResource share_context,
[email protected]eeb4e4a2011-07-19 16:22:06220 const std::vector<int32_t>& attribs,
221 HostResource* result) {
[email protected]48c1db72012-12-17 20:56:11222 if (attribs.empty() ||
223 attribs.back() != PP_GRAPHICS3DATTRIB_NONE ||
224 !(attribs.size() & 1))
[email protected]eeb4e4a2011-07-19 16:22:06225 return; // Bad message.
226
[email protected]5c966022011-09-13 18:09:37227 thunk::EnterResourceCreation enter(instance);
[email protected]9ed07f82012-05-29 21:54:55228
[email protected]eeb4e4a2011-07-19 16:22:06229 if (enter.succeeded()) {
230 result->SetHostResource(
[email protected]9ed07f82012-05-29 21:54:55231 instance,
232 enter.functions()->CreateGraphics3DRaw(instance,
233 share_context.host_resource(),
234 &attribs.front()));
[email protected]eeb4e4a2011-07-19 16:22:06235 }
236}
237
[email protected]503b3a22011-12-12 23:29:40238void PPB_Graphics3D_Proxy::OnMsgSetGetBuffer(
239 const HostResource& context,
240 int32 transfer_buffer_id) {
241 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
242 if (enter.succeeded())
243 enter.object()->SetGetBuffer(transfer_buffer_id);
[email protected]eeb4e4a2011-07-19 16:22:06244}
245
[email protected]7fe4198b2014-03-18 21:52:36246void PPB_Graphics3D_Proxy::OnMsgWaitForTokenInRange(
247 const HostResource& context,
248 int32 start,
249 int32 end,
250 gpu::CommandBuffer::State* state,
251 bool* success) {
[email protected]eeb4e4a2011-07-19 16:22:06252 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
[email protected]571b35e82012-05-19 00:04:35253 if (enter.failed()) {
254 *success = false;
[email protected]eeb4e4a2011-07-19 16:22:06255 return;
[email protected]571b35e82012-05-19 00:04:35256 }
[email protected]7fe4198b2014-03-18 21:52:36257 *state = enter.object()->WaitForTokenInRange(start, end);
258 *success = true;
259}
260
261void PPB_Graphics3D_Proxy::OnMsgWaitForGetOffsetInRange(
262 const HostResource& context,
263 int32 start,
264 int32 end,
265 gpu::CommandBuffer::State* state,
266 bool* success) {
267 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
268 if (enter.failed()) {
269 *success = false;
270 return;
271 }
272 *state = enter.object()->WaitForGetOffsetInRange(start, end);
[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]046fa1ac2014-04-01 09:06:43286 int32* id,
287 ppapi::proxy::SerializedHandle* transfer_buffer) {
288 transfer_buffer->set_null_shmem();
[email protected]eeb4e4a2011-07-19 16:22:06289 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
[email protected]046fa1ac2014-04-01 09:06:43290 if (enter.succeeded()) {
291 scoped_refptr<gpu::Buffer> buffer =
292 enter.object()->CreateTransferBuffer(size, id);
293 if (!buffer)
294 return;
[email protected]8a978df2014-04-02 23:54:09295 gpu::SharedMemoryBufferBacking* backing =
296 static_cast<gpu::SharedMemoryBufferBacking*>(buffer->backing());
297 DCHECK(backing && backing->shared_memory());
[email protected]046fa1ac2014-04-01 09:06:43298 transfer_buffer->set_shmem(
[email protected]8a978df2014-04-02 23:54:09299 TransportSHMHandle(dispatcher(), backing->shared_memory()),
[email protected]046fa1ac2014-04-01 09:06:43300 buffer->size());
301 } else {
[email protected]67c80782012-12-21 01:16:52302 *id = -1;
[email protected]046fa1ac2014-04-01 09:06:43303 }
[email protected]eeb4e4a2011-07-19 16:22:06304}
305
306void PPB_Graphics3D_Proxy::OnMsgDestroyTransferBuffer(
307 const HostResource& context,
308 int32 id) {
309 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
310 if (enter.succeeded())
311 enter.object()->DestroyTransferBuffer(id);
312}
313
[email protected]eeb4e4a2011-07-19 16:22:06314void PPB_Graphics3D_Proxy::OnMsgSwapBuffers(const HostResource& context) {
[email protected]79d23c72011-08-08 19:40:40315 EnterHostFromHostResourceForceCallback<PPB_Graphics3D_API> enter(
316 context, callback_factory_,
[email protected]eeb4e4a2011-07-19 16:22:06317 &PPB_Graphics3D_Proxy::SendSwapBuffersACKToPlugin, context);
[email protected]eeb4e4a2011-07-19 16:22:06318 if (enter.succeeded())
[email protected]79d23c72011-08-08 19:40:40319 enter.SetResult(enter.object()->SwapBuffers(enter.callback()));
[email protected]eeb4e4a2011-07-19 16:22:06320}
[email protected]b096d032013-03-08 03:08:01321
322void PPB_Graphics3D_Proxy::OnMsgInsertSyncPoint(const HostResource& context,
323 uint32* sync_point) {
324 *sync_point = 0;
325 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
326 if (enter.succeeded())
327 *sync_point = enter.object()->InsertSyncPoint();
328}
[email protected]7035bc92014-07-01 00:27:22329
330void PPB_Graphics3D_Proxy::OnMsgInsertFutureSyncPoint(
331 const HostResource& context,
332 uint32* sync_point) {
333 *sync_point = 0;
334 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
335 if (enter.succeeded())
336 *sync_point = enter.object()->InsertFutureSyncPoint();
337}
338
339void PPB_Graphics3D_Proxy::OnMsgRetireSyncPoint(const HostResource& context,
340 uint32 sync_point) {
341 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
342 if (enter.succeeded())
343 enter.object()->RetireSyncPoint(sync_point);
344}
[email protected]11494c5c2012-11-13 02:50:57345#endif // !defined(OS_NACL)
[email protected]eeb4e4a2011-07-19 16:22:06346
347void PPB_Graphics3D_Proxy::OnMsgSwapBuffersACK(const HostResource& resource,
348 int32_t pp_error) {
349 EnterPluginFromHostResource<PPB_Graphics3D_API> enter(resource);
350 if (enter.succeeded())
351 static_cast<Graphics3D*>(enter.object())->SwapBuffersACK(pp_error);
352}
353
[email protected]11494c5c2012-11-13 02:50:57354#if !defined(OS_NACL)
[email protected]eeb4e4a2011-07-19 16:22:06355void PPB_Graphics3D_Proxy::SendSwapBuffersACKToPlugin(
356 int32_t result,
357 const HostResource& context) {
358 dispatcher()->Send(new PpapiMsg_PPBGraphics3D_SwapBuffersACK(
[email protected]ac4b54d2011-10-20 23:09:28359 API_ID_PPB_GRAPHICS_3D, context, result));
[email protected]eeb4e4a2011-07-19 16:22:06360}
[email protected]11494c5c2012-11-13 02:50:57361#endif // !defined(OS_NACL)
[email protected]eeb4e4a2011-07-19 16:22:06362
363} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02364} // namespace ppapi
[email protected]eeb4e4a2011-07-19 16:22:06365