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 PP_Bool
Graphics3D::Flush(int32_t put_offset
) {
75 scoped_refptr
<gpu::Buffer
> Graphics3D::CreateTransferBuffer(
82 PP_Bool
Graphics3D::DestroyTransferBuffer(int32_t id
) {
86 gpu::CommandBuffer::State
Graphics3D::WaitForTokenInRange(int32_t start
,
88 return GetErrorState();
91 gpu::CommandBuffer::State
Graphics3D::WaitForGetOffsetInRange(int32_t start
,
93 return GetErrorState();
96 uint32_t Graphics3D::InsertSyncPoint() {
101 uint32_t Graphics3D::InsertFutureSyncPoint() {
106 void Graphics3D::RetireSyncPoint(uint32_t sync_point
) {
110 gpu::CommandBuffer
* Graphics3D::GetCommandBuffer() {
111 return command_buffer_
.get();
114 gpu::GpuControl
* Graphics3D::GetGpuControl() {
115 return command_buffer_
.get();
118 int32
Graphics3D::DoSwapBuffers() {
119 gles2_impl()->SwapBuffers();
120 IPC::Message
* msg
= new PpapiHostMsg_PPBGraphics3D_SwapBuffers(
121 API_ID_PPB_GRAPHICS_3D
, host_resource());
122 msg
->set_unblock(true);
123 PluginDispatcher::GetForResource(this)->Send(msg
);
125 return PP_OK_COMPLETIONPENDING
;
128 PPB_Graphics3D_Proxy::PPB_Graphics3D_Proxy(Dispatcher
* dispatcher
)
129 : InterfaceProxy(dispatcher
),
130 callback_factory_(this) {
133 PPB_Graphics3D_Proxy::~PPB_Graphics3D_Proxy() {
137 PP_Resource
PPB_Graphics3D_Proxy::CreateProxyResource(
138 PP_Instance instance
,
139 PP_Resource share_context
,
140 const int32_t* attrib_list
) {
141 PluginDispatcher
* dispatcher
= PluginDispatcher::GetForInstance(instance
);
143 return PP_ERROR_BADARGUMENT
;
145 HostResource share_host
;
146 gpu::gles2::GLES2Implementation
* share_gles2
= NULL
;
147 if (share_context
!= 0) {
148 EnterResourceNoLock
<PPB_Graphics3D_API
> enter(share_context
, true);
150 return PP_ERROR_BADARGUMENT
;
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();
158 std::vector
<int32_t> attribs
;
160 for (const int32_t* attr
= attrib_list
;
161 attr
[0] != PP_GRAPHICS3DATTRIB_NONE
;
163 attribs
.push_back(attr
[0]);
164 attribs
.push_back(attr
[1]);
167 attribs
.push_back(PP_GRAPHICS3DATTRIB_NONE
);
170 dispatcher
->Send(new PpapiHostMsg_PPBGraphics3D_Create(
171 API_ID_PPB_GRAPHICS_3D
, instance
, share_host
, attribs
, &result
));
172 if (result
.is_null())
175 scoped_refptr
<Graphics3D
> graphics_3d(new Graphics3D(result
));
176 if (!graphics_3d
->Init(share_gles2
))
178 return graphics_3d
->GetReference();
181 bool PPB_Graphics3D_Proxy::OnMessageReceived(const IPC::Message
& msg
) {
183 IPC_BEGIN_MESSAGE_MAP(PPB_Graphics3D_Proxy
, msg
)
184 #if !defined(OS_NACL)
185 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_Create
,
187 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_SetGetBuffer
,
189 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
)
194 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_CreateTransferBuffer
,
195 OnMsgCreateTransferBuffer
)
196 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_DestroyTransferBuffer
,
197 OnMsgDestroyTransferBuffer
)
198 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_SwapBuffers
,
200 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_InsertSyncPoint
,
201 OnMsgInsertSyncPoint
)
202 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_InsertFutureSyncPoint
,
203 OnMsgInsertFutureSyncPoint
)
204 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_RetireSyncPoint
,
205 OnMsgRetireSyncPoint
)
206 #endif // !defined(OS_NACL)
208 IPC_MESSAGE_HANDLER(PpapiMsg_PPBGraphics3D_SwapBuffersACK
,
210 IPC_MESSAGE_UNHANDLED(handled
= false)
212 IPC_END_MESSAGE_MAP()
213 // FIXME(brettw) handle bad messages!
217 #if !defined(OS_NACL)
218 void PPB_Graphics3D_Proxy::OnMsgCreate(PP_Instance instance
,
219 HostResource share_context
,
220 const std::vector
<int32_t>& attribs
,
221 HostResource
* result
) {
222 if (attribs
.empty() ||
223 attribs
.back() != PP_GRAPHICS3DATTRIB_NONE
||
224 !(attribs
.size() & 1))
225 return; // Bad message.
227 thunk::EnterResourceCreation
enter(instance
);
229 if (enter
.succeeded()) {
230 result
->SetHostResource(
232 enter
.functions()->CreateGraphics3DRaw(instance
,
233 share_context
.host_resource(),
238 void 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
);
246 void PPB_Graphics3D_Proxy::OnMsgWaitForTokenInRange(
247 const HostResource
& context
,
250 gpu::CommandBuffer::State
* state
,
252 EnterHostFromHostResource
<PPB_Graphics3D_API
> enter(context
);
253 if (enter
.failed()) {
257 *state
= enter
.object()->WaitForTokenInRange(start
, end
);
261 void PPB_Graphics3D_Proxy::OnMsgWaitForGetOffsetInRange(
262 const HostResource
& context
,
265 gpu::CommandBuffer::State
* state
,
267 EnterHostFromHostResource
<PPB_Graphics3D_API
> enter(context
);
268 if (enter
.failed()) {
272 *state
= enter
.object()->WaitForGetOffsetInRange(start
, end
);
276 void PPB_Graphics3D_Proxy::OnMsgAsyncFlush(const HostResource
& context
,
278 EnterHostFromHostResource
<PPB_Graphics3D_API
> enter(context
);
279 if (enter
.succeeded())
280 enter
.object()->Flush(put_offset
);
283 void PPB_Graphics3D_Proxy::OnMsgCreateTransferBuffer(
284 const HostResource
& context
,
287 ppapi::proxy::SerializedHandle
* transfer_buffer
) {
288 transfer_buffer
->set_null_shmem();
289 EnterHostFromHostResource
<PPB_Graphics3D_API
> enter(context
);
290 if (enter
.succeeded()) {
291 scoped_refptr
<gpu::Buffer
> buffer
=
292 enter
.object()->CreateTransferBuffer(size
, id
);
295 gpu::SharedMemoryBufferBacking
* backing
=
296 static_cast<gpu::SharedMemoryBufferBacking
*>(buffer
->backing());
297 DCHECK(backing
&& backing
->shared_memory());
298 transfer_buffer
->set_shmem(
299 TransportSHMHandle(dispatcher(), backing
->shared_memory()),
306 void PPB_Graphics3D_Proxy::OnMsgDestroyTransferBuffer(
307 const HostResource
& context
,
309 EnterHostFromHostResource
<PPB_Graphics3D_API
> enter(context
);
310 if (enter
.succeeded())
311 enter
.object()->DestroyTransferBuffer(id
);
314 void PPB_Graphics3D_Proxy::OnMsgSwapBuffers(const HostResource
& context
) {
315 EnterHostFromHostResourceForceCallback
<PPB_Graphics3D_API
> enter(
316 context
, callback_factory_
,
317 &PPB_Graphics3D_Proxy::SendSwapBuffersACKToPlugin
, context
);
318 if (enter
.succeeded())
319 enter
.SetResult(enter
.object()->SwapBuffers(enter
.callback()));
322 void PPB_Graphics3D_Proxy::OnMsgInsertSyncPoint(const HostResource
& context
,
323 uint32
* sync_point
) {
325 EnterHostFromHostResource
<PPB_Graphics3D_API
> enter(context
);
326 if (enter
.succeeded())
327 *sync_point
= enter
.object()->InsertSyncPoint();
330 void PPB_Graphics3D_Proxy::OnMsgInsertFutureSyncPoint(
331 const HostResource
& context
,
332 uint32
* sync_point
) {
334 EnterHostFromHostResource
<PPB_Graphics3D_API
> enter(context
);
335 if (enter
.succeeded())
336 *sync_point
= enter
.object()->InsertFutureSyncPoint();
339 void PPB_Graphics3D_Proxy::OnMsgRetireSyncPoint(const HostResource
& context
,
341 EnterHostFromHostResource
<PPB_Graphics3D_API
> enter(context
);
342 if (enter
.succeeded())
343 enter
.object()->RetireSyncPoint(sync_point
);
345 #endif // !defined(OS_NACL)
347 void PPB_Graphics3D_Proxy::OnMsgSwapBuffersACK(const HostResource
& resource
,
349 EnterPluginFromHostResource
<PPB_Graphics3D_API
> enter(resource
);
350 if (enter
.succeeded())
351 static_cast<Graphics3D
*>(enter
.object())->SwapBuffersACK(pp_error
);
354 #if !defined(OS_NACL)
355 void PPB_Graphics3D_Proxy::SendSwapBuffersACKToPlugin(
357 const HostResource
& context
) {
358 dispatcher()->Send(new PpapiMsg_PPBGraphics3D_SwapBuffersACK(
359 API_ID_PPB_GRAPHICS_3D
, context
, result
));
361 #endif // !defined(OS_NACL)