Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(257)

Side by Side Diff: ppapi/proxy/video_decoder_resource.cc

Issue 270213004: Implement Pepper PPB_VideoDecoder interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Disable some unit tests on Win 64 bit builds. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ppapi/proxy/video_decoder_resource.h ('k') | ppapi/proxy/video_decoder_resource_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "ppapi/proxy/video_decoder_resource.h"
6
7 #include "base/bind.h"
8 #include "gpu/command_buffer/client/gles2_cmd_helper.h"
9 #include "gpu/command_buffer/client/gles2_implementation.h"
10 #include "ipc/ipc_message.h"
11 #include "ppapi/c/pp_errors.h"
12 #include "ppapi/c/ppb_opengles2.h"
13 #include "ppapi/proxy/plugin_dispatcher.h"
14 #include "ppapi/proxy/ppapi_messages.h"
15 #include "ppapi/proxy/ppb_graphics_3d_proxy.h"
16 #include "ppapi/proxy/serialized_handle.h"
17 #include "ppapi/proxy/video_decoder_constants.h"
18 #include "ppapi/shared_impl/ppapi_globals.h"
19 #include "ppapi/shared_impl/ppb_graphics_3d_shared.h"
20 #include "ppapi/shared_impl/proxy_lock.h"
21 #include "ppapi/shared_impl/resource_tracker.h"
22 #include "ppapi/thunk/enter.h"
23
24 using ppapi::thunk::EnterResourceNoLock;
25 using ppapi::thunk::PPB_Graphics3D_API;
26 using ppapi::thunk::PPB_VideoDecoder_API;
27
28 namespace ppapi {
29 namespace proxy {
30
31 VideoDecoderResource::ShmBuffer::ShmBuffer(
32 scoped_ptr<base::SharedMemory> shm_ptr,
33 uint32_t size,
34 uint32_t shm_id)
35 : shm(shm_ptr.Pass()), addr(NULL), shm_id(shm_id) {
36 if (shm->Map(size))
37 addr = shm->memory();
38 }
39
40 VideoDecoderResource::ShmBuffer::~ShmBuffer() {
41 }
42
43 VideoDecoderResource::Texture::Texture(uint32_t texture_target,
44 const PP_Size& size)
45 : texture_target(texture_target), size(size) {
46 }
47
48 VideoDecoderResource::Texture::~Texture() {
49 }
50
51 VideoDecoderResource::Picture::Picture(int32_t decode_id, uint32_t texture_id)
52 : decode_id(decode_id), texture_id(texture_id) {
53 }
54
55 VideoDecoderResource::Picture::~Picture() {
56 }
57
58 VideoDecoderResource::VideoDecoderResource(Connection connection,
59 PP_Instance instance)
60 : PluginResource(connection, instance),
61 num_decodes_(0),
62 get_picture_(NULL),
63 gles2_impl_(NULL),
64 initialized_(false),
65 testing_(false),
66 // Set |decoder_last_error_| to PP_OK after successful initialization.
67 // This makes error checking a little more concise, since we can check
68 // that the decoder has been initialized and hasn't returned an error by
69 // just testing |decoder_last_error_|.
70 decoder_last_error_(PP_ERROR_FAILED) {
71 // Clear the decode_ids_ array.
72 memset(decode_ids_, 0, arraysize(decode_ids_));
73 SendCreate(RENDERER, PpapiHostMsg_VideoDecoder_Create());
74 }
75
76 VideoDecoderResource::~VideoDecoderResource() {
77 // Destroy any textures which haven't been dismissed.
78 TextureMap::iterator it = textures_.begin();
79 for (; it != textures_.end(); ++it)
80 DeleteGLTexture(it->first);
81 }
82
83 PPB_VideoDecoder_API* VideoDecoderResource::AsPPB_VideoDecoder_API() {
84 return this;
85 }
86
87 int32_t VideoDecoderResource::Initialize(
88 PP_Resource graphics_context,
89 PP_VideoProfile profile,
90 PP_Bool allow_software_fallback,
91 scoped_refptr<TrackedCallback> callback) {
92 if (initialized_)
93 return PP_ERROR_FAILED;
94 if (profile < 0 || profile > PP_VIDEOPROFILE_MAX)
95 return PP_ERROR_BADARGUMENT;
96 if (initialize_callback_)
97 return PP_ERROR_INPROGRESS;
98 if (!graphics_context)
99 return PP_ERROR_BADRESOURCE;
100
101 HostResource host_resource;
102 if (!testing_) {
103 // Create a new Graphics3D resource that can create texture resources to
104 // share with the plugin. We can't use the plugin's Graphics3D, since we
105 // create textures on a proxy thread, and would interfere with the plugin.
106 thunk::EnterResourceCreationNoLock enter_create(pp_instance());
107 if (enter_create.failed())
108 return PP_ERROR_FAILED;
109 int32_t attrib_list[] = {PP_GRAPHICS3DATTRIB_NONE};
110 graphics3d_ =
111 ScopedPPResource(ScopedPPResource::PassRef(),
112 enter_create.functions()->CreateGraphics3D(
113 pp_instance(), graphics_context, attrib_list));
114 EnterResourceNoLock<PPB_Graphics3D_API> enter_graphics(graphics3d_.get(),
115 true);
116 if (enter_graphics.failed())
117 return PP_ERROR_BADRESOURCE;
118
119 PPB_Graphics3D_Shared* ppb_graphics3d_shared =
120 static_cast<PPB_Graphics3D_Shared*>(enter_graphics.object());
121 gles2_impl_ = ppb_graphics3d_shared->gles2_impl();
122 host_resource = ppb_graphics3d_shared->host_resource();
123 }
124
125 initialize_callback_ = callback;
126
127 Call<PpapiPluginMsg_VideoDecoder_InitializeReply>(
128 RENDERER,
129 PpapiHostMsg_VideoDecoder_Initialize(
130 host_resource, profile, PP_ToBool(allow_software_fallback)),
131 base::Bind(&VideoDecoderResource::OnPluginMsgInitializeComplete, this));
132
133 return PP_OK_COMPLETIONPENDING;
134 }
135
136 int32_t VideoDecoderResource::Decode(uint32_t decode_id,
137 uint32_t size,
138 const void* buffer,
139 scoped_refptr<TrackedCallback> callback) {
140 if (decoder_last_error_)
141 return decoder_last_error_;
142 if (flush_callback_ || reset_callback_)
143 return PP_ERROR_FAILED;
144 if (decode_callback_)
145 return PP_ERROR_INPROGRESS;
146 if (size > kMaximumBitstreamBufferSize)
147 return PP_ERROR_NOMEMORY;
148
149 // If we allow the plugin to call Decode again, we must have somewhere to
150 // copy their buffer.
151 DCHECK(!available_shm_buffers_.empty() ||
152 shm_buffers_.size() < kMaximumPendingDecodes);
153
154 // Count up, wrapping back to 0 before overflowing.
155 int32_t uid = ++num_decodes_;
156 if (uid == std::numeric_limits<int32_t>::max())
157 num_decodes_ = 0;
158
159 // Save decode_id in a ring buffer. The ring buffer is sized to store
160 // decode_id for the maximum picture delay.
161 decode_ids_[uid % kMaximumPictureDelay] = decode_id;
162
163 if (available_shm_buffers_.empty() ||
164 available_shm_buffers_.back()->shm->mapped_size() < size) {
165 uint32_t shm_id;
166 if (shm_buffers_.size() < kMaximumPendingDecodes) {
167 // Signal the host to create a new shm buffer by passing an index outside
168 // the legal range.
169 shm_id = static_cast<uint32_t>(shm_buffers_.size());
170 } else {
171 // Signal the host to grow a buffer by passing a legal index. Choose the
172 // last available shm buffer for simplicity.
173 shm_id = available_shm_buffers_.back()->shm_id;
174 available_shm_buffers_.pop_back();
175 }
176
177 // Synchronously get shared memory. Use GenericSyncCall so we can get the
178 // reply params, which contain the handle.
179 uint32_t shm_size = 0;
180 IPC::Message reply;
181 ResourceMessageReplyParams reply_params;
182 int32_t result =
183 GenericSyncCall(RENDERER,
184 PpapiHostMsg_VideoDecoder_GetShm(shm_id, size),
185 &reply,
186 &reply_params);
187 if (result != PP_OK)
188 return PP_ERROR_FAILED;
189 if (!UnpackMessage<PpapiPluginMsg_VideoDecoder_GetShmReply>(reply,
190 &shm_size))
191 return PP_ERROR_FAILED;
192 base::SharedMemoryHandle shm_handle = base::SharedMemory::NULLHandle();
193 if (!reply_params.TakeSharedMemoryHandleAtIndex(0, &shm_handle))
194 return PP_ERROR_NOMEMORY;
195 scoped_ptr<base::SharedMemory> shm(
196 new base::SharedMemory(shm_handle, false /* read_only */));
197 scoped_ptr<ShmBuffer> shm_buffer(
198 new ShmBuffer(shm.Pass(), shm_size, shm_id));
199 if (!shm_buffer->addr)
200 return PP_ERROR_NOMEMORY;
201
202 available_shm_buffers_.push_back(shm_buffer.get());
203 if (shm_buffers_.size() < kMaximumPendingDecodes) {
204 shm_buffers_.push_back(shm_buffer.release());
205 } else {
206 // Delete manually since ScopedVector won't delete the existing element if
207 // we just assign it.
208 delete shm_buffers_[shm_id];
209 shm_buffers_[shm_id] = shm_buffer.release();
210 }
211 }
212
213 // At this point we should have shared memory to hold the plugin's buffer.
214 DCHECK(!available_shm_buffers_.empty() &&
215 available_shm_buffers_.back()->shm->mapped_size() >= size);
216
217 ShmBuffer* shm_buffer = available_shm_buffers_.back();
218 available_shm_buffers_.pop_back();
219 memcpy(shm_buffer->addr, buffer, size);
220
221 Call<PpapiPluginMsg_VideoDecoder_DecodeReply>(
222 RENDERER,
223 PpapiHostMsg_VideoDecoder_Decode(shm_buffer->shm_id, size, uid),
224 base::Bind(&VideoDecoderResource::OnPluginMsgDecodeComplete, this));
225
226 // If we have another free buffer, or we can still create new buffers, let
227 // the plugin call Decode again.
228 if (!available_shm_buffers_.empty() ||
229 shm_buffers_.size() < kMaximumPendingDecodes)
230 return PP_OK;
231
232 // All buffers are busy and we can't create more. Delay completion until a
233 // buffer is available.
234 decode_callback_ = callback;
235 return PP_OK_COMPLETIONPENDING;
236 }
237
238 int32_t VideoDecoderResource::GetPicture(
239 PP_VideoPicture* picture,
240 scoped_refptr<TrackedCallback> callback) {
241 if (decoder_last_error_)
242 return decoder_last_error_;
243 if (reset_callback_)
244 return PP_ERROR_FAILED;
245 if (get_picture_callback_)
246 return PP_ERROR_INPROGRESS;
247
248 // If the next picture is ready, return it synchronously.
249 if (!received_pictures_.empty()) {
250 WriteNextPicture(picture);
251 return PP_OK;
252 }
253
254 get_picture_callback_ = callback;
255 get_picture_ = picture;
256 return PP_OK_COMPLETIONPENDING;
257 }
258
259 void VideoDecoderResource::RecyclePicture(const PP_VideoPicture* picture) {
260 if (decoder_last_error_)
261 return;
262 if (reset_callback_)
263 return;
264
265 Post(RENDERER, PpapiHostMsg_VideoDecoder_RecyclePicture(picture->texture_id));
266 }
267
268 int32_t VideoDecoderResource::Flush(scoped_refptr<TrackedCallback> callback) {
269 if (decoder_last_error_)
270 return decoder_last_error_;
271 if (reset_callback_)
272 return PP_ERROR_FAILED;
273 if (flush_callback_)
274 return PP_ERROR_INPROGRESS;
275 flush_callback_ = callback;
276
277 Call<PpapiPluginMsg_VideoDecoder_FlushReply>(
278 RENDERER,
279 PpapiHostMsg_VideoDecoder_Flush(),
280 base::Bind(&VideoDecoderResource::OnPluginMsgFlushComplete, this));
281
282 return PP_OK_COMPLETIONPENDING;
283 }
284
285 int32_t VideoDecoderResource::Reset(scoped_refptr<TrackedCallback> callback) {
286 if (decoder_last_error_)
287 return decoder_last_error_;
288 if (flush_callback_)
289 return PP_ERROR_FAILED;
290 if (reset_callback_)
291 return PP_ERROR_INPROGRESS;
292 reset_callback_ = callback;
293
294 // Cause any pending Decode or GetPicture callbacks to abort after we return,
295 // to avoid reentering the plugin.
296 if (TrackedCallback::IsPending(decode_callback_))
297 decode_callback_->PostAbort();
298 decode_callback_ = NULL;
299 if (TrackedCallback::IsPending(get_picture_callback_))
300 get_picture_callback_->PostAbort();
301 get_picture_callback_ = NULL;
302 Call<PpapiPluginMsg_VideoDecoder_ResetReply>(
303 RENDERER,
304 PpapiHostMsg_VideoDecoder_Reset(),
305 base::Bind(&VideoDecoderResource::OnPluginMsgResetComplete, this));
306
307 return PP_OK_COMPLETIONPENDING;
308 }
309
310 void VideoDecoderResource::OnReplyReceived(
311 const ResourceMessageReplyParams& params,
312 const IPC::Message& msg) {
313 PPAPI_BEGIN_MESSAGE_MAP(VideoDecoderResource, msg)
314 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
315 PpapiPluginMsg_VideoDecoder_RequestTextures, OnPluginMsgRequestTextures)
316 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
317 PpapiPluginMsg_VideoDecoder_PictureReady, OnPluginMsgPictureReady)
318 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
319 PpapiPluginMsg_VideoDecoder_DismissPicture, OnPluginMsgDismissPicture)
320 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
321 PpapiPluginMsg_VideoDecoder_NotifyError, OnPluginMsgNotifyError)
322 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED(
323 PluginResource::OnReplyReceived(params, msg))
324 PPAPI_END_MESSAGE_MAP()
325 }
326
327 void VideoDecoderResource::SetForTest() {
328 testing_ = true;
329 }
330
331 void VideoDecoderResource::OnPluginMsgRequestTextures(
332 const ResourceMessageReplyParams& params,
333 uint32_t num_textures,
334 const PP_Size& size,
335 uint32_t texture_target) {
336 DCHECK(num_textures);
337 std::vector<uint32_t> texture_ids(num_textures);
338 if (gles2_impl_) {
339 gles2_impl_->GenTextures(num_textures, &texture_ids.front());
340 for (uint32_t i = 0; i < num_textures; ++i) {
341 gles2_impl_->ActiveTexture(GL_TEXTURE0);
342 gles2_impl_->BindTexture(texture_target, texture_ids[i]);
343 gles2_impl_->TexParameteri(
344 texture_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
345 gles2_impl_->TexParameteri(
346 texture_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
347 gles2_impl_->TexParameterf(
348 texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
349 gles2_impl_->TexParameterf(
350 texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
351
352 if (texture_target == GL_TEXTURE_2D) {
353 gles2_impl_->TexImage2D(texture_target,
354 0,
355 GL_RGBA,
356 size.width,
357 size.height,
358 0,
359 GL_RGBA,
360 GL_UNSIGNED_BYTE,
361 NULL);
362 }
363
364 textures_.insert(
365 std::make_pair(texture_ids[i], Texture(texture_target, size)));
366 }
367 gles2_impl_->Flush();
368 } else {
369 DCHECK(testing_);
370 // Create some fake texture ids so we can test picture handling.
371 for (uint32_t i = 0; i < num_textures; ++i) {
372 texture_ids[i] = i + 1;
373 textures_.insert(
374 std::make_pair(texture_ids[i], Texture(texture_target, size)));
375 }
376 }
377
378 Post(RENDERER, PpapiHostMsg_VideoDecoder_AssignTextures(size, texture_ids));
379 }
380
381 void VideoDecoderResource::OnPluginMsgPictureReady(
382 const ResourceMessageReplyParams& params,
383 int32_t decode_id,
384 uint32_t texture_id) {
385 received_pictures_.push(Picture(decode_id, texture_id));
386
387 if (TrackedCallback::IsPending(get_picture_callback_)) {
388 // The plugin may call GetPicture in its callback.
389 scoped_refptr<TrackedCallback> callback;
390 callback.swap(get_picture_callback_);
391 PP_VideoPicture* picture = get_picture_;
392 get_picture_ = NULL;
393 WriteNextPicture(picture);
394 callback->Run(PP_OK);
395 }
396 }
397
398 void VideoDecoderResource::OnPluginMsgDismissPicture(
399 const ResourceMessageReplyParams& params,
400 uint32_t texture_id) {
401 DeleteGLTexture(texture_id);
402 textures_.erase(texture_id);
403 }
404
405 void VideoDecoderResource::OnPluginMsgNotifyError(
406 const ResourceMessageReplyParams& params,
407 int32_t error) {
408 decoder_last_error_ = error;
409 // Cause any pending callbacks to run immediately. Reentrancy isn't a problem,
410 // since the plugin wasn't calling us.
411 RunCallbackWithError(&initialize_callback_);
412 RunCallbackWithError(&decode_callback_);
413 RunCallbackWithError(&get_picture_callback_);
414 RunCallbackWithError(&flush_callback_);
415 RunCallbackWithError(&reset_callback_);
416 }
417
418 void VideoDecoderResource::OnPluginMsgInitializeComplete(
419 const ResourceMessageReplyParams& params) {
420 decoder_last_error_ = params.result();
421 if (decoder_last_error_ == PP_OK)
422 initialized_ = true;
423
424 // Let the plugin call Initialize again from its callback in case of failure.
425 scoped_refptr<TrackedCallback> callback;
426 callback.swap(initialize_callback_);
427 callback->Run(decoder_last_error_);
428 }
429
430 void VideoDecoderResource::OnPluginMsgDecodeComplete(
431 const ResourceMessageReplyParams& params,
432 uint32_t shm_id) {
433 if (shm_id >= shm_buffers_.size()) {
434 NOTREACHED();
435 return;
436 }
437 // Make the shm buffer available.
438 available_shm_buffers_.push_back(shm_buffers_[shm_id]);
439 // If the plugin is waiting, let it call Decode again.
440 if (decode_callback_) {
441 scoped_refptr<TrackedCallback> callback;
442 callback.swap(decode_callback_);
443 callback->Run(PP_OK);
444 }
445 }
446
447 void VideoDecoderResource::OnPluginMsgFlushComplete(
448 const ResourceMessageReplyParams& params) {
449 // All shm buffers should have been made available by now.
450 DCHECK_EQ(shm_buffers_.size(), available_shm_buffers_.size());
451
452 if (get_picture_callback_) {
453 scoped_refptr<TrackedCallback> callback;
454 callback.swap(get_picture_callback_);
455 callback->Abort();
456 }
457
458 scoped_refptr<TrackedCallback> callback;
459 callback.swap(flush_callback_);
460 callback->Run(params.result());
461 }
462
463 void VideoDecoderResource::OnPluginMsgResetComplete(
464 const ResourceMessageReplyParams& params) {
465 // All shm buffers should have been made available by now.
466 DCHECK_EQ(shm_buffers_.size(), available_shm_buffers_.size());
467 scoped_refptr<TrackedCallback> callback;
468 callback.swap(reset_callback_);
469 callback->Run(params.result());
470 }
471
472 void VideoDecoderResource::RunCallbackWithError(
473 scoped_refptr<TrackedCallback>* callback) {
474 if (TrackedCallback::IsPending(*callback)) {
475 scoped_refptr<TrackedCallback> temp;
476 callback->swap(temp);
477 temp->Run(decoder_last_error_);
478 }
479 }
480
481 void VideoDecoderResource::DeleteGLTexture(uint32_t id) {
482 if (gles2_impl_) {
483 gles2_impl_->DeleteTextures(1, &id);
484 gles2_impl_->Flush();
485 }
486 }
487
488 void VideoDecoderResource::WriteNextPicture(PP_VideoPicture* pp_picture) {
489 DCHECK(!received_pictures_.empty());
490 Picture& picture = received_pictures_.front();
491 // Internally, we identify decodes by a unique id, which the host returns
492 // to us in the picture. Use this to get the plugin's decode_id.
493 pp_picture->decode_id = decode_ids_[picture.decode_id % kMaximumPictureDelay];
494 pp_picture->texture_id = picture.texture_id;
495 TextureMap::iterator it = textures_.find(picture.texture_id);
496 if (it != textures_.end()) {
497 pp_picture->texture_target = it->second.texture_target;
498 pp_picture->texture_size = it->second.size;
499 } else {
500 NOTREACHED();
501 }
502 received_pictures_.pop();
503 }
504
505 } // namespace proxy
506 } // namespace ppapi
OLDNEW
« no previous file with comments | « ppapi/proxy/video_decoder_resource.h ('k') | ppapi/proxy/video_decoder_resource_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698