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_context_3d_impl.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "base/shared_memory.h" | |
10 #include "gpu/command_buffer/client/gles2_cmd_helper.h" | |
11 #include "gpu/command_buffer/client/gles2_implementation.h" | |
12 #include "gpu/command_buffer/common/command_buffer.h" | |
13 #include "ppapi/c/dev/ppb_context_3d_trusted_dev.h" | |
14 #include "ppapi/thunk/enter.h" | |
15 #include "webkit/plugins/ppapi/common.h" | |
16 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | |
17 #include "webkit/plugins/ppapi/ppb_surface_3d_impl.h" | |
18 #include "webkit/plugins/ppapi/resource_helper.h" | |
19 | |
20 using ppapi::thunk::EnterResourceNoLock; | |
21 using ppapi::thunk::PPB_Context3D_API; | |
22 using ppapi::thunk::PPB_Surface3D_API; | |
23 | |
24 namespace webkit { | |
25 namespace ppapi { | |
26 | |
27 namespace { | |
28 | |
29 // Size of the transfer buffer. | |
30 const int32 kCommandBufferSize = 1024 * 1024; | |
31 const int32 kTransferBufferSize = 1024 * 1024; | |
32 | |
33 PP_Bool ShmToHandle(base::SharedMemory* shm, | |
34 size_t size, | |
35 int* shm_handle, | |
36 uint32_t* shm_size) { | |
37 if (!shm || !shm_handle || !shm_size) | |
38 return PP_FALSE; | |
39 #if defined(OS_POSIX) | |
40 *shm_handle = shm->handle().fd; | |
41 #elif defined(OS_WIN) | |
42 *shm_handle = reinterpret_cast<int>(shm->handle()); | |
43 #else | |
44 #error "Platform not supported." | |
45 #endif | |
46 *shm_size = size; | |
47 return PP_TRUE; | |
48 } | |
49 | |
50 PP_Context3DTrustedState GetErrorState() { | |
51 PP_Context3DTrustedState error_state = { 0 }; | |
52 error_state.error = kGenericError; | |
53 return error_state; | |
54 } | |
55 | |
56 PP_Context3DTrustedState PPStateFromGPUState( | |
57 const gpu::CommandBuffer::State& s) { | |
58 PP_Context3DTrustedState state = { | |
59 s.num_entries, | |
60 s.get_offset, | |
61 s.put_offset, | |
62 s.token, | |
63 static_cast<PPB_Context3DTrustedError>(s.error), | |
64 s.generation | |
65 }; | |
66 return state; | |
67 } | |
68 | |
69 } // namespace | |
70 | |
71 PPB_Context3D_Impl::PPB_Context3D_Impl(PP_Instance instance) | |
72 : Resource(instance), | |
73 transfer_buffer_id_(0), | |
74 draw_surface_(NULL), | |
75 read_surface_(NULL), | |
76 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
77 } | |
78 | |
79 PPB_Context3D_Impl::~PPB_Context3D_Impl() { | |
80 Destroy(); | |
81 } | |
82 | |
83 // static | |
84 PP_Resource PPB_Context3D_Impl::Create(PP_Instance instance, | |
85 PP_Config3D_Dev config, | |
86 PP_Resource share_context, | |
87 const int32_t* attrib_list) { | |
88 // TODO(alokp): Support shared context. | |
89 DCHECK_EQ(0, share_context); | |
90 if (share_context != 0) | |
91 return 0; | |
92 | |
93 scoped_refptr<PPB_Context3D_Impl> context(new PPB_Context3D_Impl(instance)); | |
94 if (!context->Init(config, share_context, attrib_list)) | |
95 return 0; | |
96 | |
97 return context->GetReference(); | |
98 } | |
99 | |
100 // static | |
101 PP_Resource PPB_Context3D_Impl::CreateRaw(PP_Instance instance, | |
102 PP_Config3D_Dev config, | |
103 PP_Resource share_context, | |
104 const int32_t* attrib_list) { | |
105 // TODO(alokp): Support shared context. | |
106 DCHECK_EQ(0, share_context); | |
107 if (share_context != 0) | |
108 return 0; | |
109 | |
110 scoped_refptr<PPB_Context3D_Impl> context(new PPB_Context3D_Impl(instance)); | |
111 if (!context->InitRaw(config, share_context, attrib_list)) | |
112 return 0; | |
113 | |
114 return context->GetReference(); | |
115 } | |
116 | |
117 PPB_Context3D_API* PPB_Context3D_Impl::AsPPB_Context3D_API() { | |
118 return this; | |
119 } | |
120 | |
121 int32_t PPB_Context3D_Impl::GetAttrib(int32_t attribute, int32_t* value) { | |
122 // TODO(alokp): Implement me. | |
123 return 0; | |
124 } | |
125 | |
126 int32_t PPB_Context3D_Impl::BindSurfaces(PP_Resource draw, PP_Resource read) { | |
127 EnterResourceNoLock<PPB_Surface3D_API> enter_draw(draw, true); | |
128 if (enter_draw.failed()) | |
129 return PP_ERROR_BADRESOURCE; | |
130 PPB_Surface3D_Impl* new_draw = | |
131 static_cast<PPB_Surface3D_Impl*>(enter_draw.object()); | |
132 | |
133 EnterResourceNoLock<PPB_Surface3D_API> enter_read(read, true); | |
134 if (enter_read.failed()) | |
135 return PP_ERROR_BADRESOURCE; | |
136 PPB_Surface3D_Impl* new_read = | |
137 static_cast<PPB_Surface3D_Impl*>(enter_read.object()); | |
138 return BindSurfacesImpl(new_draw, new_read); | |
139 } | |
140 | |
141 int32_t PPB_Context3D_Impl::BindSurfacesImpl(PPB_Surface3D_Impl* new_draw, | |
142 PPB_Surface3D_Impl* new_read) { | |
143 // TODO(alokp): Support separate draw-read surfaces. | |
144 DCHECK_EQ(new_draw, new_read); | |
145 if (new_draw != new_read) | |
146 return PP_ERROR_NOTSUPPORTED; | |
147 | |
148 if (new_draw == draw_surface_) | |
149 return PP_OK; | |
150 | |
151 if (new_draw && new_draw->context()) | |
152 return PP_ERROR_BADARGUMENT; // Already bound. | |
153 | |
154 if (draw_surface_) | |
155 draw_surface_->BindToContext(NULL); | |
156 if (new_draw && !new_draw->BindToContext(this)) | |
157 return PP_ERROR_NOMEMORY; | |
158 | |
159 draw_surface_ = new_draw; | |
160 read_surface_ = new_read; | |
161 return PP_OK; | |
162 } | |
163 | |
164 int32_t PPB_Context3D_Impl::GetBoundSurfaces(PP_Resource* draw, | |
165 PP_Resource* read) { | |
166 // TODO(alokp): Implement me. | |
167 return 0; | |
168 } | |
169 | |
170 PP_Bool PPB_Context3D_Impl::InitializeTrusted(int32_t size) { | |
171 if (!platform_context_.get()) | |
172 return PP_FALSE; | |
173 return PP_FromBool(platform_context_->GetCommandBuffer()->Initialize(size)); | |
174 } | |
175 | |
176 PP_Bool PPB_Context3D_Impl::GetRingBuffer(int* shm_handle, | |
177 uint32_t* shm_size) { | |
178 if (!platform_context_.get()) | |
179 return PP_FALSE; | |
180 gpu::Buffer buffer = platform_context_->GetCommandBuffer()->GetRingBuffer(); | |
181 return ShmToHandle(buffer.shared_memory, buffer.size, shm_handle, shm_size); | |
182 } | |
183 | |
184 PP_Context3DTrustedState PPB_Context3D_Impl::GetState() { | |
185 if (!platform_context_.get()) | |
186 return GetErrorState(); | |
187 return PPStateFromGPUState(platform_context_->GetCommandBuffer()->GetState()); | |
188 } | |
189 | |
190 PP_Bool PPB_Context3D_Impl::Flush(int32_t put_offset) { | |
191 if (!platform_context_.get()) | |
192 return PP_FALSE; | |
193 platform_context_->GetCommandBuffer()->Flush(put_offset); | |
194 return PP_TRUE; | |
195 } | |
196 | |
197 PP_Context3DTrustedState PPB_Context3D_Impl::FlushSync(int32_t put_offset) { | |
198 if (!platform_context_.get()) | |
199 return GetErrorState(); | |
200 gpu::CommandBuffer::State state = | |
201 platform_context_->GetCommandBuffer()->GetState(); | |
202 return PPStateFromGPUState( | |
203 platform_context_->GetCommandBuffer()->FlushSync(put_offset, | |
204 state.get_offset)); | |
205 } | |
206 | |
207 int32_t PPB_Context3D_Impl::CreateTransferBuffer(uint32_t size) { | |
208 if (!platform_context_.get()) | |
209 return 0; | |
210 return platform_context_->GetCommandBuffer()->CreateTransferBuffer(size, -1); | |
211 } | |
212 | |
213 PP_Bool PPB_Context3D_Impl::DestroyTransferBuffer(int32_t id) { | |
214 if (!platform_context_.get()) | |
215 return PP_FALSE; | |
216 platform_context_->GetCommandBuffer()->DestroyTransferBuffer(id); | |
217 return PP_TRUE; | |
218 } | |
219 | |
220 PP_Bool PPB_Context3D_Impl::GetTransferBuffer(int32_t id, | |
221 int* shm_handle, | |
222 uint32_t* shm_size) { | |
223 if (!platform_context_.get()) | |
224 return PP_FALSE; | |
225 gpu::Buffer buffer = | |
226 platform_context_->GetCommandBuffer()->GetTransferBuffer(id); | |
227 return ShmToHandle(buffer.shared_memory, buffer.size, shm_handle, shm_size); | |
228 } | |
229 | |
230 PP_Context3DTrustedState PPB_Context3D_Impl::FlushSyncFast( | |
231 int32_t put_offset, | |
232 int32_t last_known_get) { | |
233 if (!platform_context_.get()) | |
234 return GetErrorState(); | |
235 return PPStateFromGPUState( | |
236 platform_context_->GetCommandBuffer()->FlushSync(put_offset, | |
237 last_known_get)); | |
238 } | |
239 | |
240 void* PPB_Context3D_Impl::MapTexSubImage2DCHROMIUM(GLenum target, | |
241 GLint level, | |
242 GLint xoffset, | |
243 GLint yoffset, | |
244 GLsizei width, | |
245 GLsizei height, | |
246 GLenum format, | |
247 GLenum type, | |
248 GLenum access) { | |
249 if (!gles2_impl_.get()) | |
250 return NULL; | |
251 return gles2_impl_->MapTexSubImage2DCHROMIUM( | |
252 target, level, xoffset, yoffset, width, height, format, type, access); | |
253 } | |
254 | |
255 void PPB_Context3D_Impl::UnmapTexSubImage2DCHROMIUM(const void* mem) { | |
256 if (gles2_impl_.get()) | |
257 gles2_impl_->UnmapTexSubImage2DCHROMIUM(mem); | |
258 } | |
259 | |
260 gpu::gles2::GLES2Implementation* PPB_Context3D_Impl::GetGLES2Impl() { | |
261 return gles2_impl(); | |
262 } | |
263 | |
264 bool PPB_Context3D_Impl::Init(PP_Config3D_Dev config, | |
265 PP_Resource share_context, | |
266 const int32_t* attrib_list) { | |
267 if (!InitRaw(config, share_context, attrib_list)) | |
268 return false; | |
269 | |
270 if (!CreateImplementation()) { | |
271 Destroy(); | |
272 return false; | |
273 } | |
274 | |
275 return true; | |
276 } | |
277 | |
278 bool PPB_Context3D_Impl::InitRaw(PP_Config3D_Dev config, | |
279 PP_Resource share_context, | |
280 const int32_t* attrib_list) { | |
281 PluginInstance* plugin_instance = ResourceHelper::GetPluginInstance(this); | |
282 if (!plugin_instance) | |
283 return false; | |
284 | |
285 // Create and initialize the objects required to issue GLES2 calls. | |
286 platform_context_.reset(plugin_instance->CreateContext3D()); | |
287 if (!platform_context_.get()) { | |
288 Destroy(); | |
289 return false; | |
290 } | |
291 | |
292 static const int32 kAttribs[] = { | |
293 PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8, | |
294 PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 24, | |
295 PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 8, | |
296 PP_GRAPHICS3DATTRIB_SAMPLES, 0, | |
297 PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, 0, | |
298 PP_GRAPHICS3DATTRIB_HEIGHT, 1, | |
299 PP_GRAPHICS3DATTRIB_WIDTH, 1, | |
300 PP_GRAPHICS3DATTRIB_NONE, | |
301 }; | |
302 if (!platform_context_->Init(kAttribs)) { | |
303 Destroy(); | |
304 return false; | |
305 } | |
306 | |
307 platform_context_->SetContextLostCallback( | |
308 base::Bind(&PPB_Context3D_Impl::OnContextLost, | |
309 weak_ptr_factory_.GetWeakPtr())); | |
310 return true; | |
311 } | |
312 | |
313 bool PPB_Context3D_Impl::CreateImplementation() { | |
314 gpu::CommandBuffer* command_buffer = platform_context_->GetCommandBuffer(); | |
315 DCHECK(command_buffer); | |
316 | |
317 if (!command_buffer->Initialize(kCommandBufferSize)) | |
318 return false; | |
319 | |
320 // Create the GLES2 helper, which writes the command buffer protocol. | |
321 helper_.reset(new gpu::gles2::GLES2CmdHelper(command_buffer)); | |
322 if (!helper_->Initialize(kCommandBufferSize)) | |
323 return false; | |
324 | |
325 // Create a transfer buffer used to copy resources between the renderer | |
326 // process and the GPU process. | |
327 transfer_buffer_id_ = | |
328 command_buffer->CreateTransferBuffer(kTransferBufferSize, -1); | |
329 if (transfer_buffer_id_ < 0) | |
330 return false; | |
331 | |
332 // Map the buffer into the renderer process's address space. | |
333 gpu::Buffer transfer_buffer = | |
334 command_buffer->GetTransferBuffer(transfer_buffer_id_); | |
335 if (!transfer_buffer.ptr) | |
336 return false; | |
337 | |
338 // Create the object exposing the OpenGL API. | |
339 gles2_impl_.reset(new gpu::gles2::GLES2Implementation( | |
340 helper_.get(), | |
341 transfer_buffer.size, | |
342 transfer_buffer.ptr, | |
343 transfer_buffer_id_, | |
344 false, | |
345 true)); | |
346 | |
347 return true; | |
348 } | |
349 | |
350 void PPB_Context3D_Impl::Destroy() { | |
351 if (draw_surface_) | |
352 draw_surface_->BindToContext(NULL); | |
353 | |
354 gles2_impl_.reset(); | |
355 | |
356 if (platform_context_.get() && transfer_buffer_id_ != 0) { | |
357 platform_context_->GetCommandBuffer()->DestroyTransferBuffer( | |
358 transfer_buffer_id_); | |
359 transfer_buffer_id_ = 0; | |
360 } | |
361 | |
362 helper_.reset(); | |
363 platform_context_.reset(); | |
364 } | |
365 | |
366 void PPB_Context3D_Impl::OnContextLost() { | |
367 if (draw_surface_) | |
368 draw_surface_->OnContextLost(); | |
369 if (read_surface_) | |
370 read_surface_->OnContextLost(); | |
371 } | |
372 | |
373 } // namespace ppapi | |
374 } // namespace webkit | |
OLD | NEW |