OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/plugins/ppapi/ppb_context_3d_impl.h" | 5 #include "webkit/plugins/ppapi/ppb_context_3d_impl.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "gpu/command_buffer/common/command_buffer.h" | 8 #include "base/shared_memory.h" |
9 #include "gpu/command_buffer/client/gles2_cmd_helper.h" | 9 #include "gpu/command_buffer/client/gles2_cmd_helper.h" |
10 #include "gpu/command_buffer/client/gles2_implementation.h" | 10 #include "gpu/command_buffer/client/gles2_implementation.h" |
| 11 #include "gpu/command_buffer/common/command_buffer.h" |
| 12 #include "ppapi/c/dev/ppb_context_3d_trusted_dev.h" |
11 #include "webkit/plugins/ppapi/common.h" | 13 #include "webkit/plugins/ppapi/common.h" |
12 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 14 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
13 #include "webkit/plugins/ppapi/ppb_surface_3d_impl.h" | 15 #include "webkit/plugins/ppapi/ppb_surface_3d_impl.h" |
14 | 16 |
15 namespace webkit { | 17 namespace webkit { |
16 namespace ppapi { | 18 namespace ppapi { |
17 | 19 |
18 namespace { | 20 namespace { |
19 | 21 |
20 // Size of the transfer buffer. | 22 // Size of the transfer buffer. |
21 const int32 kCommandBufferSize = 1024 * 1024; | 23 const int32 kCommandBufferSize = 1024 * 1024; |
22 const int32 kTransferBufferSize = 1024 * 1024; | 24 const int32 kTransferBufferSize = 1024 * 1024; |
23 | 25 |
| 26 bool ShmToHandle(base::SharedMemory* shm, |
| 27 size_t size, |
| 28 int* shm_handle, |
| 29 uint32_t* shm_size) { |
| 30 if (!shm || !shm_handle || !shm_size) |
| 31 return false; |
| 32 #if defined(OS_POSIX) |
| 33 *shm_handle = shm->handle().fd; |
| 34 #elif defined(OS_WIN) |
| 35 *shm_handle = reinterpret_cast<int>(shm->handle()); |
| 36 #else |
| 37 #error "Platform not supported." |
| 38 #endif |
| 39 *shm_size = size; |
| 40 return true; |
| 41 } |
| 42 |
| 43 PP_Context3DTrustedState PPStateFromGPUState(gpu::CommandBuffer::State s) { |
| 44 PP_Context3DTrustedState state = { |
| 45 s.num_entries, |
| 46 s.get_offset, |
| 47 s.put_offset, |
| 48 s.token, |
| 49 static_cast<PPB_Context3DTrustedError>(s.error) |
| 50 }; |
| 51 return state; |
| 52 } |
| 53 |
24 PP_Resource Create(PP_Instance instance_id, | 54 PP_Resource Create(PP_Instance instance_id, |
25 PP_Config3D_Dev config, | 55 PP_Config3D_Dev config, |
26 PP_Resource share_context, | 56 PP_Resource share_context, |
27 const int32_t* attrib_list) { | 57 const int32_t* attrib_list) { |
28 // TODO(alokp): Support shared context. | 58 // TODO(alokp): Support shared context. |
29 DCHECK_EQ(0, share_context); | 59 DCHECK_EQ(0, share_context); |
30 if (share_context != 0) | 60 if (share_context != 0) |
31 return 0; | 61 return 0; |
32 | 62 |
33 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); | 63 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 } | 112 } |
83 | 113 |
84 const PPB_Context3D_Dev ppb_context3d = { | 114 const PPB_Context3D_Dev ppb_context3d = { |
85 &Create, | 115 &Create, |
86 &IsContext3D, | 116 &IsContext3D, |
87 &GetAttrib, | 117 &GetAttrib, |
88 &BindSurfaces, | 118 &BindSurfaces, |
89 &GetBoundSurfaces, | 119 &GetBoundSurfaces, |
90 }; | 120 }; |
91 | 121 |
| 122 |
| 123 PP_Resource CreateRaw(PP_Instance instance_id, |
| 124 PP_Config3D_Dev config, |
| 125 PP_Resource share_context, |
| 126 const int32_t* attrib_list) { |
| 127 // TODO(alokp): Support shared context. |
| 128 DCHECK_EQ(0, share_context); |
| 129 if (share_context != 0) |
| 130 return 0; |
| 131 |
| 132 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); |
| 133 if (!instance) |
| 134 return 0; |
| 135 |
| 136 scoped_refptr<PPB_Context3D_Impl> context( |
| 137 new PPB_Context3D_Impl(instance)); |
| 138 if (!context->InitRaw(config, share_context, attrib_list)) |
| 139 return 0; |
| 140 |
| 141 return context->GetReference(); |
| 142 } |
| 143 |
| 144 PP_Bool Initialize(PP_Resource context_id, int32_t size) { |
| 145 scoped_refptr<PPB_Context3D_Impl> context( |
| 146 Resource::GetAs<PPB_Context3D_Impl>(context_id)); |
| 147 if (!context.get()) |
| 148 return PP_FALSE; |
| 149 return context->command_buffer()->Initialize(size) ? PP_TRUE : PP_FALSE; |
| 150 } |
| 151 |
| 152 PP_Bool GetRingBuffer(PP_Resource context_id, |
| 153 int* shm_handle, |
| 154 uint32_t* shm_size) { |
| 155 scoped_refptr<PPB_Context3D_Impl> context( |
| 156 Resource::GetAs<PPB_Context3D_Impl>(context_id)); |
| 157 if (!context.get()) |
| 158 return PP_FALSE; |
| 159 |
| 160 gpu::Buffer buffer = context->command_buffer()->GetRingBuffer(); |
| 161 |
| 162 return ShmToHandle(buffer.shared_memory, buffer.size, shm_handle, shm_size) |
| 163 ? PP_TRUE : PP_FALSE; |
| 164 } |
| 165 |
| 166 PP_Context3DTrustedState GetState(PP_Resource context_id) { |
| 167 scoped_refptr<PPB_Context3D_Impl> context( |
| 168 Resource::GetAs<PPB_Context3D_Impl>(context_id)); |
| 169 if (!context.get()) { |
| 170 PP_Context3DTrustedState error_state = { 0 }; |
| 171 return error_state; |
| 172 } |
| 173 |
| 174 return PPStateFromGPUState(context->command_buffer()->GetState()); |
| 175 } |
| 176 |
| 177 PP_Bool Flush(PP_Resource context_id, int32_t put_offset) { |
| 178 scoped_refptr<PPB_Context3D_Impl> context( |
| 179 Resource::GetAs<PPB_Context3D_Impl>(context_id)); |
| 180 if (!context.get()) |
| 181 return PP_FALSE; |
| 182 |
| 183 context->command_buffer()->Flush(put_offset); |
| 184 return PP_TRUE; |
| 185 } |
| 186 |
| 187 PP_Context3DTrustedState FlushSync(PP_Resource context_id, int32_t put_offset) { |
| 188 scoped_refptr<PPB_Context3D_Impl> context( |
| 189 Resource::GetAs<PPB_Context3D_Impl>(context_id)); |
| 190 if (!context.get()) { |
| 191 PP_Context3DTrustedState error_state = { 0 }; |
| 192 return error_state; |
| 193 } |
| 194 |
| 195 return PPStateFromGPUState(context->command_buffer()->FlushSync(put_offset)); |
| 196 } |
| 197 |
| 198 int32_t CreateTransferBuffer(PP_Resource context_id, size_t size) { |
| 199 scoped_refptr<PPB_Context3D_Impl> context( |
| 200 Resource::GetAs<PPB_Context3D_Impl>(context_id)); |
| 201 if (!context.get()) |
| 202 return 0; |
| 203 return context->command_buffer()->CreateTransferBuffer(size); |
| 204 } |
| 205 |
| 206 PP_Bool DestroyTransferBuffer(PP_Resource context_id, int32_t id) { |
| 207 scoped_refptr<PPB_Context3D_Impl> context( |
| 208 Resource::GetAs<PPB_Context3D_Impl>(context_id)); |
| 209 if (!context.get()) |
| 210 return PP_FALSE; |
| 211 context->command_buffer()->DestroyTransferBuffer(id); |
| 212 return PP_TRUE; |
| 213 |
| 214 } |
| 215 |
| 216 PP_Bool GetTransferBuffer(PP_Resource context_id, |
| 217 int32_t id, |
| 218 int* shm_handle, |
| 219 uint32_t* shm_size) { |
| 220 scoped_refptr<PPB_Context3D_Impl> context( |
| 221 Resource::GetAs<PPB_Context3D_Impl>(context_id)); |
| 222 if (!context.get()) |
| 223 return PP_FALSE; |
| 224 gpu::Buffer buffer = context->command_buffer()->GetTransferBuffer(id); |
| 225 |
| 226 return ShmToHandle(buffer.shared_memory, buffer.size, shm_handle, shm_size) |
| 227 ? PP_TRUE : PP_FALSE; |
| 228 |
| 229 } |
| 230 |
| 231 |
92 } // namespace | 232 } // namespace |
93 | 233 |
94 PPB_Context3D_Impl::PPB_Context3D_Impl(PluginInstance* instance) | 234 PPB_Context3D_Impl::PPB_Context3D_Impl(PluginInstance* instance) |
95 : Resource(instance), | 235 : Resource(instance), |
96 instance_(instance), | 236 instance_(instance), |
97 transfer_buffer_id_(0), | 237 transfer_buffer_id_(0), |
98 draw_surface_(NULL), | 238 draw_surface_(NULL), |
99 read_surface_(NULL) { | 239 read_surface_(NULL) { |
100 } | 240 } |
101 | 241 |
102 PPB_Context3D_Impl::~PPB_Context3D_Impl() { | 242 PPB_Context3D_Impl::~PPB_Context3D_Impl() { |
103 Destroy(); | 243 Destroy(); |
104 } | 244 } |
105 | 245 |
106 const PPB_Context3D_Dev* PPB_Context3D_Impl::GetInterface() { | 246 const PPB_Context3D_Dev* PPB_Context3D_Impl::GetInterface() { |
107 return &ppb_context3d; | 247 return &ppb_context3d; |
108 } | 248 } |
109 | 249 |
| 250 const PPB_Context3DTrusted_Dev* PPB_Context3D_Impl::GetTrustedInterface() { |
| 251 static const PPB_Context3DTrusted_Dev iface = { |
| 252 &CreateRaw, |
| 253 &Initialize, |
| 254 &GetRingBuffer, |
| 255 &GetState, |
| 256 &Flush, |
| 257 &FlushSync, |
| 258 &CreateTransferBuffer, |
| 259 &DestroyTransferBuffer, |
| 260 &GetTransferBuffer |
| 261 }; |
| 262 return &iface; |
| 263 } |
| 264 |
110 PPB_Context3D_Impl* PPB_Context3D_Impl::AsPPB_Context3D_Impl() { | 265 PPB_Context3D_Impl* PPB_Context3D_Impl::AsPPB_Context3D_Impl() { |
111 return this; | 266 return this; |
112 } | 267 } |
113 | 268 |
114 bool PPB_Context3D_Impl::Init(PP_Config3D_Dev config, | 269 bool PPB_Context3D_Impl::InitRaw(PP_Config3D_Dev config, |
115 PP_Resource share_context, | 270 PP_Resource share_context, |
116 const int32_t* attrib_list) { | 271 const int32_t* attrib_list) { |
117 // Create and initialize the objects required to issue GLES2 calls. | 272 // Create and initialize the objects required to issue GLES2 calls. |
118 platform_context_.reset(instance()->CreateContext3D()); | 273 platform_context_.reset(instance()->CreateContext3D()); |
119 if (!platform_context_.get()) { | 274 if (!platform_context_.get()) { |
120 Destroy(); | 275 Destroy(); |
121 return false; | 276 return false; |
122 } | 277 } |
123 if (!platform_context_->Init()) { | 278 if (!platform_context_->Init()) { |
124 Destroy(); | 279 Destroy(); |
125 return false; | 280 return false; |
126 } | 281 } |
| 282 return true; |
| 283 } |
| 284 |
| 285 bool PPB_Context3D_Impl::Init(PP_Config3D_Dev config, |
| 286 PP_Resource share_context, |
| 287 const int32_t* attrib_list) { |
| 288 if (!InitRaw(config, share_context, attrib_list)) |
| 289 return false; |
| 290 |
| 291 if (!CreateImplementation()) { |
| 292 Destroy(); |
| 293 return false; |
| 294 } |
127 | 295 |
| 296 return true; |
| 297 } |
| 298 |
| 299 bool PPB_Context3D_Impl::CreateImplementation() { |
128 gpu::CommandBuffer* command_buffer = platform_context_->GetCommandBuffer(); | 300 gpu::CommandBuffer* command_buffer = platform_context_->GetCommandBuffer(); |
129 DCHECK(command_buffer); | 301 DCHECK(command_buffer); |
130 | 302 |
131 if (!command_buffer->Initialize(kCommandBufferSize)) { | 303 if (!command_buffer->Initialize(kCommandBufferSize)) |
132 Destroy(); | |
133 return false; | 304 return false; |
134 } | |
135 | 305 |
136 // Create the GLES2 helper, which writes the command buffer protocol. | 306 // Create the GLES2 helper, which writes the command buffer protocol. |
137 helper_.reset(new gpu::gles2::GLES2CmdHelper(command_buffer)); | 307 helper_.reset(new gpu::gles2::GLES2CmdHelper(command_buffer)); |
138 if (!helper_->Initialize(kCommandBufferSize)) { | 308 if (!helper_->Initialize(kCommandBufferSize)) |
139 Destroy(); | |
140 return false; | 309 return false; |
141 } | |
142 | 310 |
143 // Create a transfer buffer used to copy resources between the renderer | 311 // Create a transfer buffer used to copy resources between the renderer |
144 // process and the GPU process. | 312 // process and the GPU process. |
145 transfer_buffer_id_ = | 313 transfer_buffer_id_ = |
146 command_buffer->CreateTransferBuffer(kTransferBufferSize); | 314 command_buffer->CreateTransferBuffer(kTransferBufferSize); |
147 if (transfer_buffer_id_ < 0) { | 315 if (transfer_buffer_id_ < 0) |
148 Destroy(); | |
149 return false; | 316 return false; |
150 } | |
151 | 317 |
152 // Map the buffer into the renderer process's address space. | 318 // Map the buffer into the renderer process's address space. |
153 gpu::Buffer transfer_buffer = | 319 gpu::Buffer transfer_buffer = |
154 command_buffer->GetTransferBuffer(transfer_buffer_id_); | 320 command_buffer->GetTransferBuffer(transfer_buffer_id_); |
155 if (!transfer_buffer.ptr) { | 321 if (!transfer_buffer.ptr) |
156 Destroy(); | |
157 return false; | 322 return false; |
158 } | |
159 | 323 |
160 // Create the object exposing the OpenGL API. | 324 // Create the object exposing the OpenGL API. |
161 gles2_impl_.reset(new gpu::gles2::GLES2Implementation( | 325 gles2_impl_.reset(new gpu::gles2::GLES2Implementation( |
162 helper_.get(), | 326 helper_.get(), |
163 transfer_buffer.size, | 327 transfer_buffer.size, |
164 transfer_buffer.ptr, | 328 transfer_buffer.ptr, |
165 transfer_buffer_id_, | 329 transfer_buffer_id_, |
166 false)); | 330 false)); |
167 | 331 |
168 return true; | 332 return true; |
(...skipping 22 matching lines...) Expand all Loading... |
191 return PP_OK; | 355 return PP_OK; |
192 } | 356 } |
193 | 357 |
194 void PPB_Context3D_Impl::Destroy() { | 358 void PPB_Context3D_Impl::Destroy() { |
195 if (draw_surface_) | 359 if (draw_surface_) |
196 draw_surface_->BindToContext(NULL); | 360 draw_surface_->BindToContext(NULL); |
197 | 361 |
198 gles2_impl_.reset(); | 362 gles2_impl_.reset(); |
199 | 363 |
200 if (platform_context_.get() && transfer_buffer_id_ != 0) { | 364 if (platform_context_.get() && transfer_buffer_id_ != 0) { |
201 gpu::CommandBuffer* command_buffer = platform_context_->GetCommandBuffer(); | 365 command_buffer()->DestroyTransferBuffer(transfer_buffer_id_); |
202 command_buffer->DestroyTransferBuffer(transfer_buffer_id_); | |
203 transfer_buffer_id_ = 0; | 366 transfer_buffer_id_ = 0; |
204 } | 367 } |
205 | 368 |
206 helper_.reset(); | 369 helper_.reset(); |
207 platform_context_.reset(); | 370 platform_context_.reset(); |
208 } | 371 } |
209 | 372 |
| 373 gpu::CommandBuffer *PPB_Context3D_Impl::command_buffer() { |
| 374 return platform_context_->GetCommandBuffer(); |
| 375 } |
| 376 |
210 } // namespace ppapi | 377 } // namespace ppapi |
211 } // namespace webkit | 378 } // namespace webkit |
212 | 379 |
OLD | NEW |