blob: 04f20885659ba15df7b58c9da90342e47336ab93 [file] [log] [blame]
[email protected]eeb4e4a2011-07-19 16:22:061// Copyright (c) 2011 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.
4
5#include "ppapi/proxy/ppb_graphics_3d_proxy.h"
6
7#include "gpu/command_buffer/client/gles2_implementation.h"
8#include "ppapi/c/pp_errors.h"
9#include "ppapi/proxy/enter_proxy.h"
10#include "ppapi/proxy/plugin_dispatcher.h"
11#include "ppapi/proxy/ppapi_messages.h"
12#include "ppapi/thunk/enter.h"
13#include "ppapi/thunk/resource_creation_api.h"
14#include "ppapi/thunk/thunk.h"
15
16using ppapi::thunk::EnterFunctionNoLock;
17using ppapi::thunk::EnterResourceNoLock;
18using ppapi::thunk::PPB_Graphics3D_API;
19using ppapi::thunk::ResourceCreationAPI;
20
21namespace pp {
22namespace proxy {
23
24namespace {
25const int32 kCommandBufferSize = 1024 * 1024;
26const int32 kTransferBufferSize = 1024 * 1024;
27
28class CommandBuffer : public gpu::CommandBuffer {
29 public:
30 CommandBuffer(const HostResource& resource, PluginDispatcher* dispatcher);
31 virtual ~CommandBuffer();
32
33 // gpu::CommandBuffer implementation:
34 virtual bool Initialize(int32 size);
35 virtual bool Initialize(base::SharedMemory* buffer, int32 size);
36 virtual gpu::Buffer GetRingBuffer();
37 virtual State GetState();
[email protected]d58730d2011-07-26 00:00:4138 virtual State GetLastState();
[email protected]eeb4e4a2011-07-19 16:22:0639 virtual void Flush(int32 put_offset);
40 virtual State FlushSync(int32 put_offset, int32 last_known_get);
41 virtual void SetGetOffset(int32 get_offset);
42 virtual int32 CreateTransferBuffer(size_t size, int32 id_request);
43 virtual int32 RegisterTransferBuffer(base::SharedMemory* shared_memory,
44 size_t size,
45 int32 id_request);
46 virtual void DestroyTransferBuffer(int32 id);
47 virtual gpu::Buffer GetTransferBuffer(int32 handle);
48 virtual void SetToken(int32 token);
49 virtual void SetParseError(gpu::error::Error error);
50 virtual void SetContextLostReason(gpu::error::ContextLostReason reason);
51
52 private:
53 bool Send(IPC::Message* msg);
54 void UpdateState(const gpu::CommandBuffer::State& state);
55
56 int32 num_entries_;
57 scoped_ptr<base::SharedMemory> ring_buffer_;
58
59 typedef base::hash_map<int32, gpu::Buffer> TransferBufferMap;
60 TransferBufferMap transfer_buffers_;
61
62 State last_state_;
63
64 HostResource resource_;
65 PluginDispatcher* dispatcher_;
66
67 DISALLOW_COPY_AND_ASSIGN(CommandBuffer);
68};
69
70CommandBuffer::CommandBuffer(const HostResource& resource,
71 PluginDispatcher* dispatcher)
72 : num_entries_(0),
73 resource_(resource),
74 dispatcher_(dispatcher) {
75}
76
77CommandBuffer::~CommandBuffer() {
78 // Delete all the locally cached shared memory objects, closing the handle
79 // in this process.
80 for (TransferBufferMap::iterator it = transfer_buffers_.begin();
81 it != transfer_buffers_.end(); ++it) {
82 delete it->second.shared_memory;
83 it->second.shared_memory = NULL;
84 }
85}
86
87bool CommandBuffer::Initialize(int32 size) {
88 DCHECK(!ring_buffer_.get());
89
90 // Initialize the service. Assuming we are sandboxed, the GPU
91 // process is responsible for duplicating the handle. This might not be true
92 // for NaCl.
93 base::SharedMemoryHandle handle;
94 if (Send(new PpapiHostMsg_PPBGraphics3D_InitCommandBuffer(
95 INTERFACE_ID_PPB_GRAPHICS_3D, resource_, size, &handle)) &&
96 base::SharedMemory::IsHandleValid(handle)) {
97 ring_buffer_.reset(new base::SharedMemory(handle, false));
98 if (ring_buffer_->Map(size)) {
99 num_entries_ = size / sizeof(gpu::CommandBufferEntry);
100 return true;
101 }
102
103 ring_buffer_.reset();
104 }
105
106 return false;
107}
108
109bool CommandBuffer::Initialize(base::SharedMemory* buffer, int32 size) {
110 // Not implemented in proxy.
111 NOTREACHED();
112 return false;
113}
114
115gpu::Buffer CommandBuffer::GetRingBuffer() {
116 // Return locally cached ring buffer.
117 gpu::Buffer buffer;
118 buffer.ptr = ring_buffer_->memory();
119 buffer.size = num_entries_ * sizeof(gpu::CommandBufferEntry);
120 buffer.shared_memory = ring_buffer_.get();
121 return buffer;
122}
123
124gpu::CommandBuffer::State CommandBuffer::GetState() {
125 // Send will flag state with lost context if IPC fails.
126 if (last_state_.error == gpu::error::kNoError) {
127 gpu::CommandBuffer::State state;
128 if (Send(new PpapiHostMsg_PPBGraphics3D_GetState(
129 INTERFACE_ID_PPB_GRAPHICS_3D, resource_, &state)))
130 UpdateState(state);
131 }
132
133 return last_state_;
134}
135
[email protected]d58730d2011-07-26 00:00:41136gpu::CommandBuffer::State CommandBuffer::GetLastState() {
137 return last_state_;
138}
139
[email protected]eeb4e4a2011-07-19 16:22:06140void CommandBuffer::Flush(int32 put_offset) {
141 if (last_state_.error != gpu::error::kNoError)
142 return;
143
144 IPC::Message* message = new PpapiHostMsg_PPBGraphics3D_AsyncFlush(
145 INTERFACE_ID_PPB_GRAPHICS_3D, resource_, put_offset);
146
147 // Do not let a synchronous flush hold up this message. If this handler is
148 // deferred until after the synchronous flush completes, it will overwrite the
149 // cached last_state_ with out-of-date data.
150 message->set_unblock(true);
151 Send(message);
152}
153
154gpu::CommandBuffer::State CommandBuffer::FlushSync(int32 put_offset,
155 int32 last_known_get) {
156 if (last_known_get == last_state_.get_offset) {
157 // Send will flag state with lost context if IPC fails.
158 if (last_state_.error == gpu::error::kNoError) {
159 gpu::CommandBuffer::State state;
160 if (Send(new PpapiHostMsg_PPBGraphics3D_Flush(
161 INTERFACE_ID_PPB_GRAPHICS_3D, resource_, put_offset,
162 last_known_get, &state)))
163 UpdateState(state);
164 }
165 } else {
166 Flush(put_offset);
167 }
168
169 return last_state_;
170}
171
172void CommandBuffer::SetGetOffset(int32 get_offset) {
173 // Not implemented in proxy.
174 NOTREACHED();
175}
176
177int32 CommandBuffer::CreateTransferBuffer(size_t size, int32 id_request) {
178 if (last_state_.error == gpu::error::kNoError) {
179 int32 id;
180 if (Send(new PpapiHostMsg_PPBGraphics3D_CreateTransferBuffer(
181 INTERFACE_ID_PPB_GRAPHICS_3D, resource_, size, &id))) {
182 return id;
183 }
184 }
185
186 return -1;
187}
188
189int32 CommandBuffer::RegisterTransferBuffer(
190 base::SharedMemory* shared_memory,
191 size_t size,
192 int32 id_request) {
193 // Not implemented in proxy.
194 NOTREACHED();
195 return -1;
196}
197
198void CommandBuffer::DestroyTransferBuffer(int32 id) {
199 if (last_state_.error != gpu::error::kNoError)
200 return;
201
202 // Remove the transfer buffer from the client side4 cache.
203 TransferBufferMap::iterator it = transfer_buffers_.find(id);
204 DCHECK(it != transfer_buffers_.end());
205
206 // Delete the shared memory object, closing the handle in this process.
207 delete it->second.shared_memory;
208
209 transfer_buffers_.erase(it);
210
211 Send(new PpapiHostMsg_PPBGraphics3D_DestroyTransferBuffer(
212 INTERFACE_ID_PPB_GRAPHICS_3D, resource_, id));
213}
214
215gpu::Buffer CommandBuffer::GetTransferBuffer(int32 id) {
216 if (last_state_.error != gpu::error::kNoError)
217 return gpu::Buffer();
218
219 // Check local cache to see if there is already a client side shared memory
220 // object for this id.
221 TransferBufferMap::iterator it = transfer_buffers_.find(id);
222 if (it != transfer_buffers_.end()) {
223 return it->second;
224 }
225
226 // Assuming we are in the renderer process, the service is responsible for
227 // duplicating the handle. This might not be true for NaCl.
228 base::SharedMemoryHandle handle;
229 uint32 size;
230 if (!Send(new PpapiHostMsg_PPBGraphics3D_GetTransferBuffer(
231 INTERFACE_ID_PPB_GRAPHICS_3D, resource_, id, &handle, &size))) {
232 return gpu::Buffer();
233 }
234
235 // Cache the transfer buffer shared memory object client side.
236 scoped_ptr<base::SharedMemory> shared_memory(
237 new base::SharedMemory(handle, false));
238
239 // Map the shared memory on demand.
240 if (!shared_memory->memory()) {
241 if (!shared_memory->Map(size)) {
242 return gpu::Buffer();
243 }
244 }
245
246 gpu::Buffer buffer;
247 buffer.ptr = shared_memory->memory();
248 buffer.size = size;
249 buffer.shared_memory = shared_memory.release();
250 transfer_buffers_[id] = buffer;
251
252 return buffer;
253}
254
255void CommandBuffer::SetToken(int32 token) {
256 NOTREACHED();
257}
258
259void CommandBuffer::SetParseError(gpu::error::Error error) {
260 NOTREACHED();
261}
262
263void CommandBuffer::SetContextLostReason(
264 gpu::error::ContextLostReason reason) {
265 NOTREACHED();
266}
267
268bool CommandBuffer::Send(IPC::Message* msg) {
269 DCHECK(last_state_.error == gpu::error::kNoError);
270
271 if (dispatcher_->Send(msg))
272 return true;
273
274 last_state_.error = gpu::error::kLostContext;
275 return false;
276}
277
278void CommandBuffer::UpdateState(const gpu::CommandBuffer::State& state) {
279 // Handle wraparound. It works as long as we don't have more than 2B state
280 // updates in flight across which reordering occurs.
281 if (state.generation - last_state_.generation < 0x80000000U)
282 last_state_ = state;
283}
284
285base::SharedMemoryHandle TransportSHMHandleFromInt(Dispatcher* dispatcher,
286 int shm_handle) {
287 // TODO(piman): Change trusted interface to return a PP_FileHandle, those
288 // casts are ugly.
289 base::PlatformFile source =
290#if defined(OS_WIN)
291 reinterpret_cast<HANDLE>(static_cast<intptr_t>(shm_handle));
292#elif defined(OS_POSIX)
293 shm_handle;
294#else
295 #error Not implemented.
296#endif
297 // Don't close the handle, it doesn't belong to us.
298 return dispatcher->ShareHandleWithRemote(source, false);
299}
300
301PP_Graphics3DTrustedState GetErrorState() {
302 PP_Graphics3DTrustedState error_state = { 0 };
303 error_state.error = PPB_GRAPHICS3D_TRUSTED_ERROR_GENERICERROR;
304 return error_state;
305}
306
307gpu::CommandBuffer::State GPUStateFromPPState(
308 const PP_Graphics3DTrustedState& s) {
309 gpu::CommandBuffer::State state;
310 state.num_entries = s.num_entries;
311 state.get_offset = s.get_offset;
312 state.put_offset = s.put_offset;
313 state.token = s.token;
314 state.error = static_cast<gpu::error::Error>(s.error);
315 state.generation = s.generation;
316 return state;
317}
318
319InterfaceProxy* CreateGraphics3DProxy(Dispatcher* dispatcher,
320 const void* target_interface) {
321 return new PPB_Graphics3D_Proxy(dispatcher, target_interface);
322}
323} // namespace
324
325Graphics3D::Graphics3D(const HostResource& resource)
326 : PluginResource(resource) {
327}
328
329Graphics3D::~Graphics3D() {
330 DestroyGLES2Impl();
331}
332
333bool Graphics3D::Init() {
334 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance());
335 if (!dispatcher)
336 return false;
337
338 command_buffer_.reset(new CommandBuffer(host_resource(), dispatcher));
339
340 return CreateGLES2Impl(kCommandBufferSize, kTransferBufferSize);
341}
342
343PP_Bool Graphics3D::InitCommandBuffer(int32_t size) {
344 return PP_FALSE;
345}
346
347PP_Bool Graphics3D::GetRingBuffer(int* shm_handle, uint32_t* shm_size) {
348 return PP_FALSE;
349}
350
351PP_Graphics3DTrustedState Graphics3D::GetState() {
352 return GetErrorState();
353}
354
355PP_Bool Graphics3D::Flush(int32_t put_offset) {
356 return PP_FALSE;
357}
358
359PP_Graphics3DTrustedState Graphics3D::FlushSync(int32_t put_offset) {
360 return GetErrorState();
361}
362
363int32_t Graphics3D::CreateTransferBuffer(uint32_t size) {
364 return PP_FALSE;
365}
366
367PP_Bool Graphics3D::DestroyTransferBuffer(int32_t id) {
368 return PP_FALSE;
369}
370
371PP_Bool Graphics3D::GetTransferBuffer(int32_t id,
372 int* shm_handle,
373 uint32_t* shm_size) {
374 return PP_FALSE;
375}
376
377PP_Graphics3DTrustedState Graphics3D::FlushSyncFast(int32_t put_offset,
378 int32_t last_known_get) {
379 return GetErrorState();
380}
381
382gpu::CommandBuffer* Graphics3D::GetCommandBuffer() {
383 return command_buffer_.get();
384}
385
386int32 Graphics3D::DoSwapBuffers() {
387 IPC::Message* msg = new PpapiHostMsg_PPBGraphics3D_SwapBuffers(
388 INTERFACE_ID_PPB_GRAPHICS_3D, host_resource());
389 msg->set_unblock(true);
390 GetDispatcher()->Send(msg);
391
392 gles2_impl()->SwapBuffers();
393 return PP_OK_COMPLETIONPENDING;
394}
395
396PPB_Graphics3D_Proxy::PPB_Graphics3D_Proxy(Dispatcher* dispatcher,
397 const void* target_interface)
398 : InterfaceProxy(dispatcher, target_interface) {
399}
400
401PPB_Graphics3D_Proxy::~PPB_Graphics3D_Proxy() {
402}
403
404// static
405const InterfaceProxy::Info* PPB_Graphics3D_Proxy::GetInfo() {
406 static const Info info = {
407 ::ppapi::thunk::GetPPB_Graphics3D_Thunk(),
408 PPB_GRAPHICS_3D_DEV_INTERFACE,
409 INTERFACE_ID_PPB_GRAPHICS_3D,
410 false,
411 &CreateGraphics3DProxy,
412 };
413 return &info;
414}
415
416// static
417PP_Resource PPB_Graphics3D_Proxy::CreateProxyResource(
418 PP_Instance instance,
419 PP_Config3D_Dev config,
420 PP_Resource share_context,
421 const int32_t* attrib_list) {
422 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
423 if (!dispatcher)
424 return PP_ERROR_BADARGUMENT;
425
426 // TODO(alokp): Support shared context.
427 DCHECK_EQ(0, share_context);
428 if (share_context != 0)
429 return 0;
430
431 std::vector<int32_t> attribs;
432 if (attrib_list) {
433 for (const int32_t* attr = attrib_list;
434 *attr != PP_GRAPHICS3DATTRIB_NONE;
435 ++attr) {
436 attribs.push_back(*attr);
437 }
438 attribs.push_back(PP_GRAPHICS3DATTRIB_NONE);
439 }
440
441 HostResource result;
442 dispatcher->Send(new PpapiHostMsg_PPBGraphics3D_Create(
443 INTERFACE_ID_PPB_GRAPHICS_3D, instance, config, attribs, &result));
444 if (result.is_null())
445 return 0;
446
447 linked_ptr<Graphics3D> graphics_3d(new Graphics3D(result));
448 if (!graphics_3d->Init())
449 return 0;
450
451 return PluginResourceTracker::GetInstance()->AddResource(graphics_3d);
452}
453
454bool PPB_Graphics3D_Proxy::OnMessageReceived(const IPC::Message& msg) {
455 bool handled = true;
456 IPC_BEGIN_MESSAGE_MAP(PPB_Graphics3D_Proxy, msg)
457 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_Create,
458 OnMsgCreate)
459 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_InitCommandBuffer,
460 OnMsgInitCommandBuffer)
461 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_GetState,
462 OnMsgGetState)
463 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_Flush,
464 OnMsgFlush)
465 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_AsyncFlush,
466 OnMsgAsyncFlush)
467 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_CreateTransferBuffer,
468 OnMsgCreateTransferBuffer)
469 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_DestroyTransferBuffer,
470 OnMsgDestroyTransferBuffer)
471 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_GetTransferBuffer,
472 OnMsgGetTransferBuffer)
473 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics3D_SwapBuffers,
474 OnMsgSwapBuffers)
475
476 IPC_MESSAGE_HANDLER(PpapiMsg_PPBGraphics3D_SwapBuffersACK,
477 OnMsgSwapBuffersACK)
478 IPC_MESSAGE_UNHANDLED(handled = false)
479
480 IPC_END_MESSAGE_MAP()
481 // FIXME(brettw) handle bad messages!
482 return handled;
483}
484
485void PPB_Graphics3D_Proxy::OnMsgCreate(PP_Instance instance,
486 PP_Config3D_Dev config,
487 const std::vector<int32_t>& attribs,
488 HostResource* result) {
489 if (attribs.empty() || attribs.back() != 0)
490 return; // Bad message.
491
492 EnterFunctionNoLock<ResourceCreationAPI> enter(instance, true);
493 if (enter.succeeded()) {
494 result->SetHostResource(
495 instance,
496 enter.functions()->CreateGraphics3DRaw(instance, config, 0,
497 &attribs.front()));
498 }
499}
500
501void PPB_Graphics3D_Proxy::OnMsgInitCommandBuffer(
502 const HostResource& context,
503 int32 size,
504 base::SharedMemoryHandle* ring_buffer) {
505 *ring_buffer = base::SharedMemory::NULLHandle();
506 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
507 if (enter.failed())
508 return;
509
510 if (!enter.object()->InitCommandBuffer(size))
511 return;
512
513 int shm_handle;
514 uint32_t shm_size;
515 if (!enter.object()->GetRingBuffer(&shm_handle, &shm_size))
516 return;
517 *ring_buffer = TransportSHMHandleFromInt(dispatcher(), shm_handle);
518}
519
520void PPB_Graphics3D_Proxy::OnMsgGetState(const HostResource& context,
521 gpu::CommandBuffer::State* state) {
522 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
523 if (enter.failed())
524 return;
525 PP_Graphics3DTrustedState pp_state = enter.object()->GetState();
526 *state = GPUStateFromPPState(pp_state);
527}
528
529void PPB_Graphics3D_Proxy::OnMsgFlush(const HostResource& context,
530 int32 put_offset,
531 int32 last_known_get,
532 gpu::CommandBuffer::State* state) {
533 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
534 if (enter.failed())
535 return;
536 PP_Graphics3DTrustedState pp_state = enter.object()->FlushSyncFast(
537 put_offset, last_known_get);
538 *state = GPUStateFromPPState(pp_state);
539}
540
541void PPB_Graphics3D_Proxy::OnMsgAsyncFlush(const HostResource& context,
542 int32 put_offset) {
543 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
544 if (enter.succeeded())
545 enter.object()->Flush(put_offset);
546}
547
548void PPB_Graphics3D_Proxy::OnMsgCreateTransferBuffer(
549 const HostResource& context,
550 int32 size,
551 int32* id) {
552 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
553 if (enter.succeeded())
554 *id = enter.object()->CreateTransferBuffer(size);
555 else
556 *id = 0;
557}
558
559void PPB_Graphics3D_Proxy::OnMsgDestroyTransferBuffer(
560 const HostResource& context,
561 int32 id) {
562 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
563 if (enter.succeeded())
564 enter.object()->DestroyTransferBuffer(id);
565}
566
567void PPB_Graphics3D_Proxy::OnMsgGetTransferBuffer(
568 const HostResource& context,
569 int32 id,
570 base::SharedMemoryHandle* transfer_buffer,
571 uint32* size) {
572 *transfer_buffer = base::SharedMemory::NULLHandle();
573 *size = 0;
574
575 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
576 int shm_handle = 0;
577 uint32_t shm_size = 0;
578 if (enter.succeeded() &&
579 enter.object()->GetTransferBuffer(id, &shm_handle, &shm_size)) {
580 *transfer_buffer = TransportSHMHandleFromInt(dispatcher(), shm_handle);
581 *size = shm_size;
582 }
583}
584
585void PPB_Graphics3D_Proxy::OnMsgSwapBuffers(const HostResource& context) {
586 CompletionCallback callback = callback_factory_.NewOptionalCallback(
587 &PPB_Graphics3D_Proxy::SendSwapBuffersACKToPlugin, context);
588
589 EnterHostFromHostResource<PPB_Graphics3D_API> enter(context);
590 int32_t result = PP_ERROR_BADRESOURCE;
591 if (enter.succeeded())
592 result = enter.object()->SwapBuffers(callback.pp_completion_callback());
593 if (result != PP_OK_COMPLETIONPENDING) {
594 // There was some error, so we won't get a flush callback. We need to now
595 // issue the ACK to the plugin hears about the error. This will also clean
596 // up the data associated with the callback.
597 callback.Run(result);
598 }
599}
600
601void PPB_Graphics3D_Proxy::OnMsgSwapBuffersACK(const HostResource& resource,
602 int32_t pp_error) {
603 EnterPluginFromHostResource<PPB_Graphics3D_API> enter(resource);
604 if (enter.succeeded())
605 static_cast<Graphics3D*>(enter.object())->SwapBuffersACK(pp_error);
606}
607
608void PPB_Graphics3D_Proxy::SendSwapBuffersACKToPlugin(
609 int32_t result,
610 const HostResource& context) {
611 dispatcher()->Send(new PpapiMsg_PPBGraphics3D_SwapBuffersACK(
612 INTERFACE_ID_PPB_GRAPHICS_3D, context, result));
613}
614
615} // namespace proxy
616} // namespace pp
617