Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
| 3 * Use of this source code is governed by a BSD-style license that can be | |
| 4 * found in the LICENSE file. | |
| 5 */ | |
| 6 | |
| 7 #include "native_client/src/shared/ppapi_proxy/plugin_ppb_graphics_3d.h" | |
| 8 | |
| 9 #include "gpu/command_buffer/client/gles2_implementation.h" | |
| 10 #include "native_client/src/shared/ppapi_proxy/command_buffer_nacl.h" | |
| 11 #include "native_client/src/shared/ppapi_proxy/object_serialize.h" | |
| 12 #include "native_client/src/shared/ppapi_proxy/plugin_callback.h" | |
| 13 #include "native_client/src/shared/ppapi_proxy/plugin_globals.h" | |
| 14 #include "native_client/src/shared/ppapi_proxy/plugin_instance_data.h" | |
| 15 #include "native_client/src/shared/ppapi_proxy/plugin_ppb_core.h" | |
| 16 #include "native_client/src/shared/ppapi_proxy/plugin_ppb_var.h" | |
| 17 #include "native_client/src/shared/ppapi_proxy/plugin_surface_3d.h" | |
| 18 #include "native_client/src/shared/ppapi_proxy/utility.h" | |
| 19 #include "native_client/src/shared/srpc/nacl_srpc.h" | |
| 20 #include "native_client/src/third_party/ppapi/c/pp_completion_callback.h" | |
| 21 #include "native_client/src/third_party/ppapi/c/pp_errors.h" | |
| 22 #include "native_client/src/third_party/ppapi/c/pp_rect.h" | |
| 23 #include "native_client/src/third_party/ppapi/c/pp_var.h" | |
| 24 #include "srpcgen/ppb_rpc.h" | |
| 25 | |
| 26 namespace ppapi_proxy { | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 const int32 kTransferBufferSize = 512 * 1024; | |
| 31 | |
| 32 int32_t GetNumAttribs(const int32_t* attrib_list) { | |
| 33 int num = 0; | |
|
sehr (please use chromium)
2011/08/17 20:44:36
int32_t num.
nfullagar
2011/08/18 00:23:57
Done.
| |
| 34 if (attrib_list) { | |
| 35 // skip over attrib pairs | |
| 36 while (attrib_list[num] != PP_GRAPHICS3DATTRIB_NONE) | |
| 37 num += 2; | |
| 38 // Add one more for PP_GRAPHICS3DATTRIB_NONE. | |
| 39 num += 1; | |
| 40 } | |
| 41 return num; | |
| 42 } | |
| 43 | |
| 44 PP_Resource Create(PP_Instance instance, | |
| 45 PP_Resource share_context, | |
| 46 const int32_t* attrib_list) { | |
| 47 DebugPrintf("PPB_Graphics3D::Create: instance=%"NACL_PRIu32"\n", instance); | |
| 48 PP_Resource graphics3d_id = kInvalidResourceId; | |
| 49 nacl_abi_size_t num_attribs = GetNumAttribs(attrib_list); | |
| 50 NaClSrpcError retval = | |
| 51 PpbGraphics3DRpcClient::PPB_Graphics3DTrusted_CreateRaw( | |
| 52 GetMainSrpcChannel(), | |
| 53 instance, | |
| 54 share_context, | |
| 55 num_attribs, const_cast<int32_t *>(attrib_list), | |
| 56 &graphics3d_id); | |
| 57 if (retval == NACL_SRPC_RESULT_OK) { | |
| 58 scoped_refptr<PluginGraphics3D> graphics_3d = | |
| 59 PluginResource::AdoptAs<PluginGraphics3D>(graphics3d_id); | |
| 60 if (graphics_3d.get()) { | |
| 61 graphics_3d->set_instance_id(instance); | |
| 62 return graphics3d_id; | |
| 63 } | |
| 64 } | |
| 65 return kInvalidResourceId; | |
| 66 } | |
| 67 | |
| 68 PP_Bool IsGraphics3D(PP_Resource resource) { | |
| 69 DebugPrintf("PPB_Graphics3D::IsGraphics3D: resource=%"NACL_PRIu32"\n", | |
| 70 resource); | |
| 71 return PluginResource::GetAs<PluginGraphics3D>(resource).get() | |
| 72 ? PP_TRUE : PP_FALSE; | |
| 73 } | |
| 74 | |
| 75 int32_t GetAttribs(PP_Resource graphics3d_id, | |
| 76 int32_t* attrib_list) { | |
| 77 int32_t pp_error; | |
| 78 nacl_abi_size_t num_attribs = GetNumAttribs(attrib_list); | |
| 79 NaClSrpcError retval = | |
| 80 PpbGraphics3DRpcClient::PPB_Graphics3D_GetAttribs( | |
| 81 GetMainSrpcChannel(), | |
| 82 graphics3d_id, | |
| 83 num_attribs, attrib_list, | |
| 84 &num_attribs, attrib_list, | |
| 85 &pp_error); | |
| 86 if (retval != NACL_SRPC_RESULT_OK) { | |
| 87 return PP_ERROR_BADARGUMENT; | |
| 88 } | |
| 89 return pp_error; | |
| 90 } | |
| 91 | |
| 92 int32_t SetAttribs(PP_Resource graphics3d_id, | |
| 93 int32_t* attrib_list) { | |
| 94 int32_t pp_error; | |
| 95 nacl_abi_size_t num_attribs = GetNumAttribs(attrib_list); | |
| 96 NaClSrpcError retval = | |
| 97 PpbGraphics3DRpcClient::PPB_Graphics3D_SetAttribs( | |
| 98 GetMainSrpcChannel(), | |
| 99 graphics3d_id, | |
| 100 num_attribs, attrib_list, | |
| 101 &pp_error); | |
| 102 if (retval != NACL_SRPC_RESULT_OK) { | |
| 103 return PP_ERROR_BADARGUMENT; | |
| 104 } | |
| 105 return pp_error; | |
| 106 } | |
| 107 | |
| 108 int32_t ResizeBuffers(PP_Resource graphics3d_id, | |
| 109 int32_t width, | |
| 110 int32_t height) { | |
| 111 int32_t pp_error; | |
| 112 NaClSrpcError retval = | |
| 113 PpbGraphics3DRpcClient::PPB_Graphics3D_ResizeBuffers( | |
| 114 GetMainSrpcChannel(), | |
| 115 graphics3d_id, | |
| 116 width, | |
| 117 height, | |
| 118 &pp_error); | |
| 119 if (retval != NACL_SRPC_RESULT_OK) { | |
| 120 return PP_ERROR_BADARGUMENT; | |
| 121 } | |
| 122 return pp_error; | |
| 123 } | |
| 124 | |
| 125 | |
| 126 int32_t SwapBuffs(PP_Resource graphics3d_id, | |
| 127 struct PP_CompletionCallback callback) { | |
| 128 DebugPrintf("PPB_Graphics3D::SwapBuffers: graphics3d_id=%"NACL_PRIu32"\n", | |
| 129 graphics3d_id); | |
| 130 | |
| 131 scoped_refptr<PluginGraphics3D> graphics3d = | |
| 132 PluginResource::GetAs<PluginGraphics3D>(graphics3d_id).get(); | |
| 133 if (!graphics3d.get()) | |
| 134 return MayForceCallback(callback, PP_ERROR_BADRESOURCE); | |
| 135 | |
| 136 return MayForceCallback(callback, | |
| 137 graphics3d->SwapBuffers(graphics3d_id, callback)); | |
| 138 } | |
| 139 | |
| 140 } // namespace | |
| 141 | |
| 142 __thread PP_Resource PluginGraphics3D::cached_graphics3d_id = 0; | |
| 143 __thread gpu::gles2::GLES2Implementation* | |
| 144 PluginGraphics3D::cached_implementation = NULL; | |
| 145 | |
| 146 PluginGraphics3D::PluginGraphics3D() : instance_id_(0) { } | |
| 147 | |
| 148 PluginGraphics3D::~PluginGraphics3D() { | |
| 149 // Invalidate the cache. | |
| 150 cached_graphics3d_id = 0; | |
| 151 cached_implementation = NULL; | |
| 152 } | |
| 153 | |
| 154 // static | |
| 155 gpu::gles2::GLES2Implementation* PluginGraphics3D::implFromResourceSlow( | |
| 156 PP_Resource graphics3d_id) { | |
| 157 DebugPrintf("PluginGraphics3D::implFromResourceSlow: " | |
| 158 "resource=%"NACL_PRIu32"\n", graphics3d_id); | |
| 159 | |
| 160 // For performance reasons, we don't error-check the context, but crash on | |
| 161 // NULL instead. | |
| 162 gpu::gles2::GLES2Implementation* impl = | |
| 163 PluginResource::GetAs<PluginGraphics3D>(graphics3d_id)->impl(); | |
| 164 cached_graphics3d_id = graphics3d_id; | |
| 165 cached_implementation = impl; | |
| 166 return impl; | |
| 167 } | |
| 168 | |
| 169 | |
| 170 bool PluginGraphics3D::InitFromBrowserResource(PP_Resource res) { | |
| 171 DebugPrintf("PluginGraphics3D::InitFromBrowserResource: " | |
| 172 "resource=%"NACL_PRIu32"\n", res); | |
| 173 | |
| 174 // Create and initialize the objects required to issue GLES2 calls. | |
| 175 command_buffer_.reset(new CommandBufferNacl(res, PluginCore::GetInterface())); | |
| 176 command_buffer_->Initialize(kTransferBufferSize); | |
| 177 gles2_helper_.reset(new gpu::gles2::GLES2CmdHelper(command_buffer_.get())); | |
| 178 gpu::Buffer buffer = command_buffer_->GetRingBuffer(); | |
| 179 if (gles2_helper_->Initialize(buffer.size)) { | |
| 180 // Request id -1 to signify 'don't care' | |
| 181 int32 transfer_buffer_id = | |
| 182 command_buffer_->CreateTransferBuffer(kTransferBufferSize, -1); | |
| 183 gpu::Buffer transfer_buffer = | |
| 184 command_buffer_->GetTransferBuffer(transfer_buffer_id); | |
| 185 if (transfer_buffer.ptr) { | |
| 186 gles2_implementation_.reset(new gpu::gles2::GLES2Implementation( | |
| 187 gles2_helper_.get(), | |
| 188 transfer_buffer.size, | |
| 189 transfer_buffer.ptr, | |
| 190 transfer_buffer_id, | |
| 191 false)); | |
| 192 return true; | |
| 193 } | |
| 194 } | |
| 195 return false; | |
| 196 } | |
| 197 | |
| 198 int32_t PluginGraphics3D::SwapBuffers(PP_Resource graphics3d_id, | |
| 199 struct PP_CompletionCallback callback) { | |
| 200 | |
| 201 int32_t callback_id = CompletionCallbackTable::Get()->AddCallback(callback); | |
| 202 if (callback_id == 0) // Just like Chrome, for now disallow blocking calls. | |
| 203 return PP_ERROR_BADARGUMENT; | |
| 204 | |
| 205 int32_t pp_error; | |
| 206 NaClSrpcError retval = | |
| 207 PpbGraphics3DRpcClient::PPB_Graphics3D_SwapBuffers( | |
| 208 GetMainSrpcChannel(), | |
| 209 graphics3d_id, | |
| 210 callback_id, | |
| 211 &pp_error); | |
| 212 if (retval != NACL_SRPC_RESULT_OK) | |
| 213 return PP_ERROR_FAILED; | |
| 214 | |
| 215 if ((PP_OK_COMPLETIONPENDING != pp_error) && (PP_OK != pp_error)) | |
| 216 return pp_error; | |
| 217 | |
| 218 impl()->SwapBuffers(); | |
| 219 return PP_OK_COMPLETIONPENDING; | |
| 220 } | |
| 221 | |
| 222 | |
| 223 // static | |
| 224 const PPB_Graphics3D_Dev* PluginGraphics3D::GetInterface() { | |
| 225 static const PPB_Graphics3D_Dev intf = { | |
| 226 &Create, | |
| 227 &IsGraphics3D, | |
| 228 &GetAttribs, | |
| 229 &SetAttribs, | |
| 230 &ResizeBuffers, | |
| 231 &SwapBuffs, | |
| 232 }; | |
| 233 return &intf; | |
| 234 } | |
| 235 | |
| 236 } // namespace ppapi_proxy | |
| 237 | |
| OLD | NEW |