| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 The Native Client 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 // SRPC-abstraction wrappers around PPB_Graphics3D functions. |
| 6 |
| 7 #include <limits> |
| 8 |
| 9 #include "native_client/src/include/nacl_scoped_ptr.h" |
| 10 #include "native_client/src/include/portability.h" |
| 11 #include "native_client/src/shared/ppapi_proxy/browser_callback.h" |
| 12 #include "native_client/src/shared/ppapi_proxy/browser_globals.h" |
| 13 #include "native_client/src/shared/ppapi_proxy/object_serialize.h" |
| 14 #include "native_client/src/shared/ppapi_proxy/plugin_resource.h" |
| 15 #include "native_client/src/shared/ppapi_proxy/utility.h" |
| 16 #include "native_client/src/trusted/desc/nacl_desc_wrapper.h" |
| 17 #include "native_client/src/third_party/ppapi/c/dev/pp_graphics_3d_dev.h" |
| 18 #include "native_client/src/third_party/ppapi/c/dev/ppb_graphics_3d_dev.h" |
| 19 #include "native_client/src/third_party/ppapi/c/dev/ppb_graphics_3d_trusted_dev.
h" |
| 20 #include "native_client/src/third_party/ppapi/c/pp_errors.h" |
| 21 #include "srpcgen/ppb_rpc.h" |
| 22 |
| 23 using ppapi_proxy::DebugPrintf; |
| 24 using ppapi_proxy::SerializeTo; |
| 25 |
| 26 namespace { |
| 27 |
| 28 const int32_t kMaxAllowedBufferSize = 16777216; |
| 29 |
| 30 /// Check that the attribute list is well formed. |
| 31 bool ValidateAttribList(nacl_abi_size_t attrib_list_count, |
| 32 int32_t* attrib_list) { |
| 33 DebugPrintf("ValidateAttribList: count = %d, ptr_null = %d\n", |
| 34 static_cast<int>(attrib_list_count), (attrib_list == NULL) ? 1 : 0); |
| 35 // Zero count lists are ok. |
| 36 if (attrib_list_count == 0) |
| 37 return true; |
| 38 // NULL lists w/ a non-zero count are not allowed. |
| 39 if (attrib_list == NULL) |
| 40 return false; |
| 41 // Must be an odd count, and the last item must be the terminator. |
| 42 return (attrib_list_count & 1) && |
| 43 (attrib_list[attrib_list_count - 1] == PP_GRAPHICS3DATTRIB_NONE); |
| 44 } |
| 45 |
| 46 /// Check that the attribute list is well formed, then copy to output. |
| 47 bool ValidateAndCopyAttribList(nacl_abi_size_t in_attrib_list_count, |
| 48 int32_t* in_attrib_list, |
| 49 nacl_abi_size_t* out_attrib_list_count, |
| 50 int32_t* out_attrib_list) { |
| 51 |
| 52 DebugPrintf("ValidateAndCopyAttribList: in_count = %d, in_ptr_null = %d\n", |
| 53 static_cast<int>(in_attrib_list_count), |
| 54 (in_attrib_list == NULL) ? 1 : 0); |
| 55 DebugPrintf(" out_count = %d, out_ptr_null = %d\n", |
| 56 static_cast<int>(*out_attrib_list_count), |
| 57 (out_attrib_list == NULL) ? 1 : 0); |
| 58 |
| 59 // Attrib lists can both be NULL w/ 0 count. |
| 60 if ((in_attrib_list == NULL) && (out_attrib_list == NULL)) |
| 61 return (in_attrib_list_count == 0) && (*out_attrib_list_count == 0); |
| 62 // Don't allow only one list to be NULL. |
| 63 if ((in_attrib_list == NULL) || (out_attrib_list == NULL)) |
| 64 return false; |
| 65 // Input and output lists must be the same size. |
| 66 if (in_attrib_list_count != *out_attrib_list_count) |
| 67 return false; |
| 68 // Make sure input list is well formed. |
| 69 if (!ValidateAttribList(in_attrib_list_count, in_attrib_list)) |
| 70 return false; |
| 71 // Copy input list to output list. |
| 72 for (nacl_abi_size_t i = 0; i < in_attrib_list_count; ++i) |
| 73 out_attrib_list[i] = in_attrib_list[i]; |
| 74 return true; |
| 75 } |
| 76 |
| 77 } // namespace |
| 78 |
| 79 //@{ |
| 80 /// The following methods are SRPC dispatchers for ppapi/c/ppb_graphics_3d.h. |
| 81 |
| 82 void PpbGraphics3DRpcServer::PPB_Graphics3D_Create( |
| 83 NaClSrpcRpc* rpc, |
| 84 NaClSrpcClosure* done, |
| 85 PP_Instance instance, |
| 86 PP_Resource share_context, |
| 87 nacl_abi_size_t num_attrib_list, int32_t* attrib_list, |
| 88 PP_Resource* graphics3d_id) { |
| 89 DebugPrintf("PpbGraphics3DRpcServer::PPB_Graphics3D_Create(...)\n"); |
| 90 NaClSrpcClosureRunner runner(done); |
| 91 rpc->result = NACL_SRPC_RESULT_APP_ERROR; |
| 92 if (!ValidateAttribList(num_attrib_list, attrib_list)) |
| 93 return; |
| 94 *graphics3d_id = ppapi_proxy::PPBGraphics3DInterface()->Create( |
| 95 instance, share_context, attrib_list); |
| 96 rpc->result = NACL_SRPC_RESULT_OK; |
| 97 } |
| 98 |
| 99 void PpbGraphics3DRpcServer::PPB_Graphics3D_GetAttribs( |
| 100 NaClSrpcRpc* rpc, |
| 101 NaClSrpcClosure* done, |
| 102 PP_Resource graphics3d_id, |
| 103 nacl_abi_size_t in_attrib_list_count, int32_t* in_attrib_list, |
| 104 nacl_abi_size_t* out_attrib_list_count, int32_t* out_attrib_list, |
| 105 int32_t* pp_error) { |
| 106 DebugPrintf("PpbGraphics3DRpcServer::PPB_Graphics3D_GetAttrib(...)\n"); |
| 107 NaClSrpcClosureRunner runner(done); |
| 108 rpc->result = NACL_SRPC_RESULT_APP_ERROR; |
| 109 if (!ValidateAndCopyAttribList(in_attrib_list_count, in_attrib_list, |
| 110 out_attrib_list_count, out_attrib_list)) |
| 111 return; |
| 112 *pp_error = ppapi_proxy::PPBGraphics3DInterface()->GetAttribs( |
| 113 graphics3d_id, out_attrib_list); |
| 114 rpc->result = NACL_SRPC_RESULT_OK; |
| 115 } |
| 116 |
| 117 void PpbGraphics3DRpcServer::PPB_Graphics3D_SetAttribs( |
| 118 NaClSrpcRpc* rpc, |
| 119 NaClSrpcClosure* done, |
| 120 PP_Resource graphics3d_id, |
| 121 nacl_abi_size_t attrib_list_count, int32_t* attrib_list, |
| 122 int32_t* pp_error) { |
| 123 DebugPrintf("PpbGraphics3DRpcServer::PPB_Graphics3D_SetAttrib(...)\n"); |
| 124 NaClSrpcClosureRunner runner(done); |
| 125 rpc->result = NACL_SRPC_RESULT_APP_ERROR; |
| 126 if (!ValidateAttribList(attrib_list_count, attrib_list)) |
| 127 return; |
| 128 *pp_error = ppapi_proxy::PPBGraphics3DInterface()->SetAttribs( |
| 129 graphics3d_id, attrib_list); |
| 130 rpc->result = NACL_SRPC_RESULT_OK; |
| 131 } |
| 132 |
| 133 void PpbGraphics3DRpcServer::PPB_Graphics3D_ResizeBuffers( |
| 134 NaClSrpcRpc* rpc, |
| 135 NaClSrpcClosure* done, |
| 136 PP_Resource graphics3d_id, |
| 137 int32_t width, |
| 138 int32_t height, |
| 139 int32_t* pp_error) { |
| 140 DebugPrintf("PpbGraphics3DRpcServer::PPB_Graphics3D_ResizeBuffers(...)\n"); |
| 141 NaClSrpcClosureRunner runner(done); |
| 142 rpc->result = NACL_SRPC_RESULT_APP_ERROR; |
| 143 *pp_error = ppapi_proxy::PPBGraphics3DInterface()->ResizeBuffers( |
| 144 graphics3d_id, width, height); |
| 145 rpc->result = NACL_SRPC_RESULT_OK; |
| 146 } |
| 147 |
| 148 void PpbGraphics3DRpcServer::PPB_Graphics3D_SwapBuffers( |
| 149 NaClSrpcRpc* rpc, |
| 150 NaClSrpcClosure* done, |
| 151 PP_Resource graphics3d_id, |
| 152 int32_t callback_id, |
| 153 int32_t* pp_error) { |
| 154 DebugPrintf("PpbGraphics3DRpcServer::PPB_Graphics3D_SwapBuffers(...)\n"); |
| 155 NaClSrpcClosureRunner runner(done); |
| 156 rpc->result = NACL_SRPC_RESULT_APP_ERROR; |
| 157 PP_CompletionCallback remote_callback = |
| 158 ppapi_proxy::MakeRemoteCompletionCallback(rpc->channel, callback_id); |
| 159 if (remote_callback.func == NULL) { |
| 160 DebugPrintf(" PPB_Graphics3D_SwapBuffers() FAILED!\n"); |
| 161 return; // Treat this as a generic SRPC error. |
| 162 } |
| 163 *pp_error = ppapi_proxy::PPBGraphics3DInterface()->SwapBuffers( |
| 164 graphics3d_id, remote_callback); |
| 165 if (*pp_error != PP_OK_COMPLETIONPENDING) |
| 166 ppapi_proxy::DeleteRemoteCallbackInfo(remote_callback); |
| 167 DebugPrintf(" PPB_Graphics3D_SwapBuffers: pp_error=%"NACL_PRId32"\n", |
| 168 *pp_error); |
| 169 rpc->result = NACL_SRPC_RESULT_OK; |
| 170 } |
| 171 |
| 172 //@} |
| 173 |
| 174 //@{ |
| 175 /// The following methods are the SRPC dispatchers for |
| 176 /// ppapi/c/ppb_graphics_3d_trusted.h. |
| 177 void PpbGraphics3DRpcServer::PPB_Graphics3DTrusted_CreateRaw( |
| 178 NaClSrpcRpc* rpc, |
| 179 NaClSrpcClosure* done, |
| 180 PP_Instance instance, |
| 181 PP_Resource share_context, |
| 182 nacl_abi_size_t attrib_list_bytes, int32_t* attrib_list, |
| 183 PP_Resource* resource_id) { |
| 184 DebugPrintf("PPB_Graphics3DTrusted_CreateRaw: instance: %"NACL_PRIu32"\n", |
| 185 instance); |
| 186 |
| 187 NaClSrpcClosureRunner runner(done); |
| 188 rpc->result = NACL_SRPC_RESULT_APP_ERROR; |
| 189 if (!ValidateAttribList(attrib_list_bytes, attrib_list)) |
| 190 return; |
| 191 |
| 192 *resource_id = ppapi_proxy::PPBGraphics3DTrustedInterface()->CreateRaw( |
| 193 instance, share_context, attrib_list); |
| 194 rpc->result = NACL_SRPC_RESULT_OK; |
| 195 } |
| 196 |
| 197 void PpbGraphics3DRpcServer::PPB_Graphics3DTrusted_InitCommandBuffer( |
| 198 NaClSrpcRpc* rpc, |
| 199 NaClSrpcClosure* done, |
| 200 PP_Resource resource_id, |
| 201 int32_t size, |
| 202 int32_t* success) { |
| 203 DebugPrintf("PPB_Graphics3DTrusted_InitCommandBuffer(...) resource_id: %d\n", |
| 204 resource_id); |
| 205 NaClSrpcClosureRunner runner(done); |
| 206 rpc->result = NACL_SRPC_RESULT_APP_ERROR; |
| 207 if ((size > kMaxAllowedBufferSize) || (size < 0)) |
| 208 return; |
| 209 *success = ppapi_proxy::PPBGraphics3DTrustedInterface()->InitCommandBuffer( |
| 210 resource_id, size); |
| 211 rpc->result = NACL_SRPC_RESULT_OK; |
| 212 } |
| 213 |
| 214 |
| 215 void PpbGraphics3DRpcServer::PPB_Graphics3DTrusted_GetRingBuffer( |
| 216 NaClSrpcRpc* rpc, |
| 217 NaClSrpcClosure* done, |
| 218 PP_Resource resource_id, |
| 219 NaClSrpcImcDescType* shm_desc, |
| 220 int32_t* shm_size) { |
| 221 DebugPrintf("PPB_Graphics3DTrusted_GetRingBuffer\n"); |
| 222 nacl::DescWrapperFactory factory; |
| 223 nacl::scoped_ptr<nacl::DescWrapper> desc_wrapper; |
| 224 NaClSrpcClosureRunner runner(done); |
| 225 rpc->result = NACL_SRPC_RESULT_APP_ERROR; |
| 226 |
| 227 int native_handle = 0; |
| 228 uint32_t native_size = 0; |
| 229 ppapi_proxy::PPBGraphics3DTrustedInterface()->GetRingBuffer( |
| 230 resource_id, &native_handle, &native_size); |
| 231 desc_wrapper.reset(factory.ImportShmHandle( |
| 232 (NaClHandle)native_handle, native_size)); |
| 233 *shm_desc = desc_wrapper->desc(); |
| 234 *shm_size = native_size; |
| 235 rpc->result = NACL_SRPC_RESULT_OK; |
| 236 } |
| 237 |
| 238 void PpbGraphics3DRpcServer::PPB_Graphics3DTrusted_GetState( |
| 239 NaClSrpcRpc* rpc, |
| 240 NaClSrpcClosure* done, |
| 241 PP_Resource resource_id, |
| 242 nacl_abi_size_t* state_bytes, char* state) { |
| 243 DebugPrintf("PPB_Graphics3DTrusted_GetState\n"); |
| 244 NaClSrpcClosureRunner runner(done); |
| 245 rpc->result = NACL_SRPC_RESULT_APP_ERROR; |
| 246 PP_Graphics3DTrustedState trusted_state = |
| 247 ppapi_proxy::PPBGraphics3DTrustedInterface()->GetState(resource_id); |
| 248 *reinterpret_cast<PP_Graphics3DTrustedState*>(state) = trusted_state; |
| 249 *state_bytes = sizeof(trusted_state); |
| 250 rpc->result = NACL_SRPC_RESULT_OK; |
| 251 } |
| 252 |
| 253 void PpbGraphics3DRpcServer::PPB_Graphics3DTrusted_Flush( |
| 254 NaClSrpcRpc* rpc, |
| 255 NaClSrpcClosure* done, |
| 256 PP_Resource resource_id, |
| 257 int32_t put_offset) { |
| 258 DebugPrintf("PPB_Graphics3DTrusted_Flush(id: %d, put_offset: %d\n", |
| 259 resource_id, put_offset); |
| 260 NaClSrpcClosureRunner runner(done); |
| 261 rpc->result = NACL_SRPC_RESULT_APP_ERROR; |
| 262 ppapi_proxy::PPBGraphics3DTrustedInterface()->Flush(resource_id, put_offset); |
| 263 rpc->result = NACL_SRPC_RESULT_OK; |
| 264 } |
| 265 |
| 266 void PpbGraphics3DRpcServer::PPB_Graphics3DTrusted_FlushSync( |
| 267 NaClSrpcRpc* rpc, |
| 268 NaClSrpcClosure* done, |
| 269 PP_Resource resource_id, |
| 270 int32_t put_offset, |
| 271 nacl_abi_size_t* state_bytes, char* state) { |
| 272 DebugPrintf("PPB_Graphics3DTrusted_FlushSync\n"); |
| 273 NaClSrpcClosureRunner runner(done); |
| 274 rpc->result = NACL_SRPC_RESULT_APP_ERROR; |
| 275 PP_Graphics3DTrustedState trusted_state = |
| 276 ppapi_proxy::PPBGraphics3DTrustedInterface()->FlushSync(resource_id, |
| 277 put_offset); |
| 278 *reinterpret_cast<PP_Graphics3DTrustedState*>(state) = trusted_state; |
| 279 *state_bytes = sizeof(trusted_state); |
| 280 rpc->result = NACL_SRPC_RESULT_OK; |
| 281 |
| 282 } |
| 283 |
| 284 void PpbGraphics3DRpcServer::PPB_Graphics3DTrusted_CreateTransferBuffer( |
| 285 NaClSrpcRpc* rpc, |
| 286 NaClSrpcClosure* done, |
| 287 PP_Resource resource_id, |
| 288 int32_t size, |
| 289 int32_t id_request, |
| 290 int32_t* id) { |
| 291 UNREFERENCED_PARAMETER(id_request); |
| 292 NaClSrpcClosureRunner runner(done); |
| 293 rpc->result = NACL_SRPC_RESULT_APP_ERROR; |
| 294 DebugPrintf("PPB_Graphics3DTrusted_CreateTransferBuffer\n"); |
| 295 if ((size > kMaxAllowedBufferSize) || (size < 0)) |
| 296 return; |
| 297 *id = ppapi_proxy::PPBGraphics3DTrustedInterface()->CreateTransferBuffer( |
| 298 resource_id, size); |
| 299 rpc->result = NACL_SRPC_RESULT_OK; |
| 300 } |
| 301 |
| 302 void PpbGraphics3DRpcServer::PPB_Graphics3DTrusted_DestroyTransferBuffer( |
| 303 NaClSrpcRpc* rpc, |
| 304 NaClSrpcClosure* done, |
| 305 PP_Resource resource_id, |
| 306 int32_t id) { |
| 307 DebugPrintf("PPB_Graphics3DTrusted_DestroyTransferBuffer\n"); |
| 308 NaClSrpcClosureRunner runner(done); |
| 309 rpc->result = NACL_SRPC_RESULT_APP_ERROR; |
| 310 ppapi_proxy::PPBGraphics3DTrustedInterface()->DestroyTransferBuffer( |
| 311 resource_id, id); |
| 312 rpc->result = NACL_SRPC_RESULT_OK; |
| 313 |
| 314 } |
| 315 |
| 316 void PpbGraphics3DRpcServer::PPB_Graphics3DTrusted_GetTransferBuffer( |
| 317 NaClSrpcRpc* rpc, |
| 318 NaClSrpcClosure* done, |
| 319 PP_Resource resource_id, |
| 320 int32_t id, |
| 321 NaClSrpcImcDescType* shm_desc, |
| 322 int32_t* shm_size) { |
| 323 DebugPrintf("PPB_Graphics3DTrusted_GetTransferBuffer\n"); |
| 324 nacl::DescWrapperFactory factory; |
| 325 nacl::scoped_ptr<nacl::DescWrapper> desc_wrapper; |
| 326 NaClSrpcClosureRunner runner(done); |
| 327 rpc->result = NACL_SRPC_RESULT_APP_ERROR; |
| 328 |
| 329 int native_handle = 0; |
| 330 uint32_t native_size = 0; |
| 331 ppapi_proxy::PPBGraphics3DTrustedInterface()-> |
| 332 GetTransferBuffer(resource_id, id, &native_handle, &native_size); |
| 333 desc_wrapper.reset(factory.ImportShmHandle( |
| 334 (NaClHandle)native_handle, native_size)); |
| 335 *shm_desc = desc_wrapper->desc(); |
| 336 *shm_size = native_size; |
| 337 rpc->result = NACL_SRPC_RESULT_OK; |
| 338 |
| 339 } |
| 340 |
| 341 //@} |
| OLD | NEW |