Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(406)

Side by Side Diff: ppapi/proxy/ppb_graphics_3d_proxy.cc

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

Powered by Google App Engine
This is Rietveld 408576698