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
;
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
;
47 Graphics3D::Graphics3D(const HostResource
& resource
)
48 : PPB_Graphics3D_Shared(resource
) {
51 Graphics3D::~Graphics3D() {
55 bool Graphics3D::Init(gpu::gles2::GLES2Implementation
* share_gles2
) {
56 PluginDispatcher
* dispatcher
= PluginDispatcher::GetForResource(this);
60 command_buffer_
.reset(
61 new PpapiCommandBufferProxy(host_resource(), dispatcher
));
63 return CreateGLES2Impl(kCommandBufferSize
, kTransferBufferSize
,
67 PP_Bool
Graphics3D::SetGetBuffer(int32_t /* transfer_buffer_id */) {
71 gpu::CommandBuffer::State
Graphics3D::GetState() {
72 return GetErrorState();
75 PP_Bool
Graphics3D::Flush(int32_t put_offset
) {
79 scoped_refptr
<gpu::Buffer
> Graphics3D::CreateTransferBuffer(
86 PP_Bool
Graphics3D::DestroyTransferBuffer(int32_t id
) {
90 gpu::CommandBuffer::State
Graphics3D::WaitForTokenInRange(int32_t start
,
92 return GetErrorState();
95 gpu::CommandBuffer::State
Graphics3D::WaitForGetOffsetInRange(int32_t start
,
97 return GetErrorState();
100 uint32_t Graphics3D::InsertSyncPoint() {
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() {
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
);
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);
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
;
155 for (const int32_t* attr
= attrib_list
;
156 attr
[0] != PP_GRAPHICS3DATTRIB_NONE
;
158 attribs
.push_back(attr
[0]);
159 attribs
.push_back(attr
[1]);
162 attribs
.push_back(PP_GRAPHICS3DATTRIB_NONE
);
165 dispatcher
->Send(new PpapiHostMsg_PPBGraphics3D_Create(
166 API_ID_PPB_GRAPHICS_3D
, instance
, share_host
, attribs
, &result
));
167 if (result
.is_null())
170 scoped_refptr
<Graphics3D
> graphics_3d(new Graphics3D(result
));
171 if (!graphics_3d
->Init(share_gles2
))
173 return graphics_3d
->GetReference();
176 bool PPB_Graphics3D_Proxy::OnMessageReceived(const IPC::Message
& msg
) {
178 IPC_BEGIN_MESSAGE_MAP(PPB_Graphics3D_Proxy
, msg
)
179 #if !defined(OS_NACL)
180 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_Create
,
182 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_SetGetBuffer
,
184 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_GetState
,
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
,
197 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_InsertSyncPoint
,
198 OnMsgInsertSyncPoint
)
199 #endif // !defined(OS_NACL)
201 IPC_MESSAGE_HANDLER(PpapiMsg_PPBGraphics3D_SwapBuffersACK
,
203 IPC_MESSAGE_UNHANDLED(handled
= false)
205 IPC_END_MESSAGE_MAP()
206 // FIXME(brettw) handle bad messages!
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(
225 enter
.functions()->CreateGraphics3DRaw(instance
,
226 share_context
.host_resource(),
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
,
242 EnterHostFromHostResource
<PPB_Graphics3D_API
> enter(context
);
243 if (enter
.failed()) {
247 *state
= enter
.object()->GetState();
251 void PPB_Graphics3D_Proxy::OnMsgWaitForTokenInRange(
252 const HostResource
& context
,
255 gpu::CommandBuffer::State
* state
,
257 EnterHostFromHostResource
<PPB_Graphics3D_API
> enter(context
);
258 if (enter
.failed()) {
262 *state
= enter
.object()->WaitForTokenInRange(start
, end
);
266 void PPB_Graphics3D_Proxy::OnMsgWaitForGetOffsetInRange(
267 const HostResource
& context
,
270 gpu::CommandBuffer::State
* state
,
272 EnterHostFromHostResource
<PPB_Graphics3D_API
> enter(context
);
273 if (enter
.failed()) {
277 *state
= enter
.object()->WaitForGetOffsetInRange(start
, end
);
281 void PPB_Graphics3D_Proxy::OnMsgAsyncFlush(const HostResource
& context
,
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
,
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
);
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()),
311 void PPB_Graphics3D_Proxy::OnMsgDestroyTransferBuffer(
312 const HostResource
& context
,
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
) {
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
,
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(
346 const HostResource
& context
) {
347 dispatcher()->Send(new PpapiMsg_PPBGraphics3D_SwapBuffersACK(
348 API_ID_PPB_GRAPHICS_3D
, context
, result
));
350 #endif // !defined(OS_NACL)