OLD | NEW |
(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 "webkit/plugins/ppapi/ppb_graphics_3d_impl.h" |
| 6 |
| 7 #include "gpu/command_buffer/common/command_buffer.h" |
| 8 #include "ppapi/c/dev/ppb_context_3d_trusted_dev.h" |
| 9 |
| 10 namespace webkit { |
| 11 namespace ppapi { |
| 12 |
| 13 namespace { |
| 14 |
| 15 bool ShmToHandle(base::SharedMemory* shm, |
| 16 size_t size, |
| 17 int* shm_handle, |
| 18 uint32_t* shm_size) { |
| 19 if (!shm || !shm_handle || !shm_size) |
| 20 return false; |
| 21 #if defined(OS_POSIX) |
| 22 *shm_handle = shm->handle().fd; |
| 23 #elif defined(OS_WIN) |
| 24 *shm_handle = reinterpret_cast<int>(shm->handle()); |
| 25 #else |
| 26 #error "Platform not supported." |
| 27 #endif |
| 28 *shm_size = size; |
| 29 return true; |
| 30 } |
| 31 |
| 32 PP_Context3DTrustedState PPStateFromGPUState(gpu::CommandBuffer::State s) { |
| 33 PP_Context3DTrustedState state = { |
| 34 s.num_entries, |
| 35 s.get_offset, |
| 36 s.put_offset, |
| 37 s.token, |
| 38 static_cast<PPB_Context3DTrustedError>(s.error) |
| 39 }; |
| 40 return state; |
| 41 } |
| 42 |
| 43 PP_Resource CreateRaw(PP_Instance instance_id, |
| 44 PP_Config3D_Dev config, |
| 45 PP_Resource share_context, |
| 46 const int32_t* attrib_list) { |
| 47 // TODO(alokp): Support shared context. |
| 48 DCHECK_EQ(0, share_context); |
| 49 if (share_context != 0) |
| 50 return 0; |
| 51 |
| 52 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); |
| 53 if (!instance) |
| 54 return 0; |
| 55 |
| 56 scoped_refptr<PPB_Graphics3D_Impl> context( |
| 57 new PPB_Graphics3D_Impl(instance)); |
| 58 if (!context->InitRaw(config, share_context, attrib_list)) |
| 59 return 0; |
| 60 |
| 61 return context->GetReference(); |
| 62 } |
| 63 |
| 64 PP_Bool Initialize(PP_Resource context_id, int32_t size) { |
| 65 scoped_refptr<PPB_Graphics3D_Impl> context( |
| 66 Resource::GetAs<PPB_Graphics3D_Impl>(context_id)); |
| 67 if (!context.get() || !context->GetCommandBuffer()) |
| 68 return PP_FALSE; |
| 69 |
| 70 return context->GetCommandBuffer()->Initialize(size) ? PP_TRUE : PP_FALSE; |
| 71 } |
| 72 |
| 73 PP_Bool GetRingBuffer(PP_Resource context_id, |
| 74 int* shm_handle, |
| 75 uint32_t* shm_size) { |
| 76 scoped_refptr<PPB_Graphics3D_Impl> context( |
| 77 Resource::GetAs<PPB_Graphics3D_Impl>(context_id)); |
| 78 if (!context.get() || !context->GetCommandBuffer()) |
| 79 return PP_FALSE; |
| 80 |
| 81 gpu::Buffer buffer = context->GetCommandBuffer()->GetRingBuffer(); |
| 82 return ShmToHandle(buffer.shared_memory, buffer.size, shm_handle, shm_size) |
| 83 ? PP_TRUE : PP_FALSE; |
| 84 } |
| 85 |
| 86 PP_Context3DTrustedState GetState(PP_Resource context_id) { |
| 87 scoped_refptr<PPB_Graphics3D_Impl> context( |
| 88 Resource::GetAs<PPB_Graphics3D_Impl>(context_id)); |
| 89 if (!context.get() || !context->GetCommandBuffer()) { |
| 90 PP_Context3DTrustedState error_state = { 0 }; |
| 91 return error_state; |
| 92 } |
| 93 |
| 94 return PPStateFromGPUState(context->GetCommandBuffer()->GetState()); |
| 95 } |
| 96 |
| 97 PP_Bool Flush(PP_Resource context_id, int32_t put_offset) { |
| 98 scoped_refptr<PPB_Graphics3D_Impl> context( |
| 99 Resource::GetAs<PPB_Graphics3D_Impl>(context_id)); |
| 100 if (!context.get() || !context->GetCommandBuffer()) |
| 101 return PP_FALSE; |
| 102 |
| 103 context->GetCommandBuffer()->Flush(put_offset); |
| 104 return PP_TRUE; |
| 105 } |
| 106 |
| 107 PP_Context3DTrustedState FlushSync(PP_Resource context_id, int32_t put_offset) { |
| 108 scoped_refptr<PPB_Graphics3D_Impl> context( |
| 109 Resource::GetAs<PPB_Graphics3D_Impl>(context_id)); |
| 110 if (!context.get() || !context->GetCommandBuffer()) { |
| 111 PP_Context3DTrustedState error_state = { 0 }; |
| 112 return error_state; |
| 113 } |
| 114 |
| 115 return PPStateFromGPUState( |
| 116 context->GetCommandBuffer()->FlushSync(put_offset)); |
| 117 } |
| 118 |
| 119 int32_t CreateTransferBuffer(PP_Resource context_id, uint32_t size) { |
| 120 scoped_refptr<PPB_Graphics3D_Impl> context( |
| 121 Resource::GetAs<PPB_Graphics3D_Impl>(context_id)); |
| 122 if (!context.get() || !context->GetCommandBuffer()) |
| 123 return 0; |
| 124 |
| 125 return context->GetCommandBuffer()->CreateTransferBuffer(size); |
| 126 } |
| 127 |
| 128 PP_Bool DestroyTransferBuffer(PP_Resource context_id, int32_t id) { |
| 129 scoped_refptr<PPB_Graphics3D_Impl> context( |
| 130 Resource::GetAs<PPB_Graphics3D_Impl>(context_id)); |
| 131 if (!context.get() || !context->GetCommandBuffer()) |
| 132 return PP_FALSE; |
| 133 |
| 134 context->GetCommandBuffer()->DestroyTransferBuffer(id); |
| 135 return PP_TRUE; |
| 136 } |
| 137 |
| 138 PP_Bool GetTransferBuffer(PP_Resource context_id, |
| 139 int32_t id, |
| 140 int* shm_handle, |
| 141 uint32_t* shm_size) { |
| 142 scoped_refptr<PPB_Graphics3D_Impl> context( |
| 143 Resource::GetAs<PPB_Graphics3D_Impl>(context_id)); |
| 144 if (!context.get() || !context->GetCommandBuffer()) |
| 145 return PP_FALSE; |
| 146 |
| 147 gpu::Buffer buffer = context->GetCommandBuffer()->GetTransferBuffer(id); |
| 148 return ShmToHandle(buffer.shared_memory, buffer.size, shm_handle, shm_size) |
| 149 ? PP_TRUE : PP_FALSE; |
| 150 } |
| 151 |
| 152 } // namespace |
| 153 |
| 154 // static |
| 155 const PPB_Context3DTrusted_Dev* PPB_Graphics3D_Impl::GetTrustedInterface() { |
| 156 static const PPB_Context3DTrusted_Dev ppb_context3d_trusted = { |
| 157 &CreateRaw, |
| 158 &Initialize, |
| 159 &GetRingBuffer, |
| 160 &GetState, |
| 161 &Flush, |
| 162 &FlushSync, |
| 163 &CreateTransferBuffer, |
| 164 &DestroyTransferBuffer, |
| 165 &GetTransferBuffer |
| 166 }; |
| 167 return &ppb_context3d_trusted; |
| 168 } |
| 169 |
| 170 } // namespace ppapi |
| 171 } // namespace webkit |
| 172 |
OLD | NEW |