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

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: Address Ami's comments. 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
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 // Save decode_id in a ring buffer. The ring buffer is sized to store
155 // decode_id for the maximum picture delay.
156 decode_ids_[num_decodes_ % kMaximumPictureDelay] = decode_id;
157 num_decodes_++;
158 // Wrap to 0 if num_decodes_ overflowed.
Ami GONE FROM CHROMIUM 2014/05/29 02:31:36 ditto
bbudge 2014/05/29 12:07:38 Done.
159 if (num_decodes_ < 0)
160 num_decodes_ = 0;
161
162 if (available_shm_buffers_.empty() ||
163 available_shm_buffers_.back()->shm->mapped_size() < size) {
164 uint32_t shm_id;
165 if (shm_buffers_.size() < kMaximumPendingDecodes) {
166 // Signal the host to create a new shm buffer by passing an index outside
167 // the legal range.
168 shm_id = static_cast<uint32_t>(shm_buffers_.size());
169 } else {
170 // Signal the host to grow a buffer by passing a legal index. Choose the
171 // last available shm buffer for simplicity.
172 shm_id = available_shm_buffers_.back()->shm_id;
173 available_shm_buffers_.pop_back();
174 }
175
176 // Synchronously get shared memory. Use GenericSyncCall so we can get the
177 // reply params, which contain the handle.
178 uint32_t shm_size = 0;
179 IPC::Message reply;
180 ResourceMessageReplyParams reply_params;
181 int32_t result =
182 GenericSyncCall(RENDERER,
183 PpapiHostMsg_VideoDecoder_GetShm(shm_id, size),
184 &reply,
185 &reply_params);
186 if (result != PP_OK)
187 return PP_ERROR_FAILED;
188 if (!UnpackMessage<PpapiPluginMsg_VideoDecoder_GetShmReply>(reply,
189 &shm_size))
190 return PP_ERROR_FAILED;
191 base::SharedMemoryHandle shm_handle = base::SharedMemory::NULLHandle();
192 if (!reply_params.TakeSharedMemoryHandleAtIndex(0, &shm_handle))
193 return PP_ERROR_NOMEMORY;
194 scoped_ptr<base::SharedMemory> shm(
195 new base::SharedMemory(shm_handle, false /* read_only */));
196 scoped_ptr<ShmBuffer> shm_buffer(
197 new ShmBuffer(shm.Pass(), shm_size, shm_id));
198 if (!shm_buffer->addr)
199 return PP_ERROR_NOMEMORY;
200
201 available_shm_buffers_.push_back(shm_buffer.get());
202 if (shm_buffers_.size() < kMaximumPendingDecodes) {
203 shm_buffers_.push_back(shm_buffer.release());
204 } else {
205 // Delete manually since ScopedVector won't delete the existing element if
206 // we just assign it.
207 delete shm_buffers_[shm_id];
208 shm_buffers_[shm_id] = shm_buffer.release();
209 }
210 }
211
212 // At this point we should have shared memory to hold the plugin's buffer.
213 DCHECK(!available_shm_buffers_.empty() &&
214 available_shm_buffers_.back()->shm->mapped_size() >= size);
215
216 ShmBuffer* shm_buffer = available_shm_buffers_.back();
217 available_shm_buffers_.pop_back();
218 memcpy(shm_buffer->addr, buffer, size);
219
220 Call<PpapiPluginMsg_VideoDecoder_DecodeReply>(
221 RENDERER,
222 PpapiHostMsg_VideoDecoder_Decode(shm_buffer->shm_id, size),
223 base::Bind(&VideoDecoderResource::OnPluginMsgDecodeComplete, this));
224
225 // If we have another free buffer, or we can still create new buffers, let
226 // the plugin call Decode again.
227 if (!available_shm_buffers_.empty() ||
228 shm_buffers_.size() < kMaximumPendingDecodes)
229 return PP_OK;
230
231 // All buffers are busy and we can't create more. Delay completion until a
232 // buffer is available.
233 decode_callback_ = callback;
234 return PP_OK_COMPLETIONPENDING;
235 }
236
237 int32_t VideoDecoderResource::GetPicture(
238 PP_VideoPicture* picture,
239 scoped_refptr<TrackedCallback> callback) {
240 if (decoder_last_error_)
241 return decoder_last_error_;
242 if (reset_callback_)
243 return PP_ERROR_FAILED;
244 if (get_picture_callback_)
245 return PP_ERROR_INPROGRESS;
246
247 // If the next picture is ready, return it synchronously.
248 if (!received_pictures_.empty()) {
249 WriteNextPicture(picture);
250 return PP_OK;
251 }
252
253 get_picture_callback_ = callback;
254 get_picture_ = picture;
255 return PP_OK_COMPLETIONPENDING;
256 }
257
258 void VideoDecoderResource::RecyclePicture(const PP_VideoPicture* picture) {
259 if (decoder_last_error_)
260 return;
261 if (reset_callback_)
262 return;
263
264 Post(RENDERER, PpapiHostMsg_VideoDecoder_RecyclePicture(picture->texture_id));
265 }
266
267 int32_t VideoDecoderResource::Flush(scoped_refptr<TrackedCallback> callback) {
268 if (decoder_last_error_)
269 return decoder_last_error_;
270 if (reset_callback_)
271 return PP_ERROR_FAILED;
272 if (flush_callback_)
273 return PP_ERROR_INPROGRESS;
274 flush_callback_ = callback;
275
276 Call<PpapiPluginMsg_VideoDecoder_FlushReply>(
277 RENDERER,
278 PpapiHostMsg_VideoDecoder_Flush(),
279 base::Bind(&VideoDecoderResource::OnPluginMsgFlushComplete, this));
280
281 return PP_OK_COMPLETIONPENDING;
282 }
283
284 int32_t VideoDecoderResource::Reset(scoped_refptr<TrackedCallback> callback) {
285 if (decoder_last_error_)
286 return decoder_last_error_;
287 if (flush_callback_)
288 return PP_ERROR_FAILED;
289 if (reset_callback_)
290 return PP_ERROR_INPROGRESS;
291 reset_callback_ = callback;
292
293 // Cause any pending Decode or GetPicture callbacks to abort after we return,
294 // to avoid reentering the plugin.
295 if (TrackedCallback::IsPending(decode_callback_))
296 decode_callback_->PostAbort();
297 decode_callback_ = NULL;
298 if (TrackedCallback::IsPending(get_picture_callback_))
299 get_picture_callback_->PostAbort();
300 get_picture_callback_ = NULL;
301 Call<PpapiPluginMsg_VideoDecoder_ResetReply>(
302 RENDERER,
303 PpapiHostMsg_VideoDecoder_Reset(),
304 base::Bind(&VideoDecoderResource::OnPluginMsgResetComplete, this));
305
306 return PP_OK_COMPLETIONPENDING;
307 }
308
309 void VideoDecoderResource::OnReplyReceived(
310 const ResourceMessageReplyParams& params,
311 const IPC::Message& msg) {
312 PPAPI_BEGIN_MESSAGE_MAP(VideoDecoderResource, msg)
313 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
314 PpapiPluginMsg_VideoDecoder_RequestTextures, OnPluginMsgRequestTextures)
315 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
316 PpapiPluginMsg_VideoDecoder_PictureReady, OnPluginMsgPictureReady)
317 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
318 PpapiPluginMsg_VideoDecoder_DismissPicture, OnPluginMsgDismissPicture)
319 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
320 PpapiPluginMsg_VideoDecoder_NotifyError, OnPluginMsgNotifyError)
321 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED(
322 PluginResource::OnReplyReceived(params, msg))
323 PPAPI_END_MESSAGE_MAP()
324 }
325
326 void VideoDecoderResource::SetForTest() {
327 testing_ = true;
328 }
329
330 void VideoDecoderResource::OnPluginMsgRequestTextures(
331 const ResourceMessageReplyParams& params,
332 uint32_t num_textures,
333 const PP_Size& size,
334 uint32_t texture_target) {
335 DCHECK(num_textures);
336 std::vector<uint32_t> texture_ids(num_textures);
337 if (gles2_impl_) {
338 gles2_impl_->GenTextures(num_textures, &texture_ids.front());
339 for (uint32_t i = 0; i < num_textures; ++i) {
340 gles2_impl_->ActiveTexture(GL_TEXTURE0);
341 gles2_impl_->BindTexture(texture_target, texture_ids[i]);
342 gles2_impl_->TexParameteri(
343 texture_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
344 gles2_impl_->TexParameteri(
345 texture_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
346 gles2_impl_->TexParameterf(
347 texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
348 gles2_impl_->TexParameterf(
349 texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
350
351 if (texture_target == GL_TEXTURE_2D) {
352 gles2_impl_->TexImage2D(texture_target,
353 0,
354 GL_RGBA,
355 size.width,
356 size.height,
357 0,
358 GL_RGBA,
359 GL_UNSIGNED_BYTE,
360 NULL);
361 }
362
363 textures_.insert(
364 std::make_pair(texture_ids[i], Texture(texture_target, size)));
365 }
366 gles2_impl_->Flush();
367 } else {
368 DCHECK(testing_);
369 // Create some fake texture ids so we can test picture handling.
370 for (uint32_t i = 0; i < num_textures; ++i) {
371 texture_ids[i] = i + 1;
372 textures_.insert(
373 std::make_pair(texture_ids[i], Texture(texture_target, size)));
374 }
375 }
376
377 Post(RENDERER, PpapiHostMsg_VideoDecoder_AssignTextures(size, texture_ids));
378 }
379
380 void VideoDecoderResource::OnPluginMsgPictureReady(
381 const ResourceMessageReplyParams& params,
382 uint32_t decode_id,
383 uint32_t texture_id) {
384 received_pictures_.push(Picture(decode_id, texture_id));
385
386 if (TrackedCallback::IsPending(get_picture_callback_)) {
387 // The plugin may call GetPicture in its callback.
388 scoped_refptr<TrackedCallback> callback;
389 callback.swap(get_picture_callback_);
390 PP_VideoPicture* picture = get_picture_;
391 get_picture_ = NULL;
392 WriteNextPicture(picture);
393 callback->Run(PP_OK);
394 }
395 }
396
397 void VideoDecoderResource::OnPluginMsgDismissPicture(
398 const ResourceMessageReplyParams& params,
399 uint32_t texture_id) {
400 DeleteGLTexture(texture_id);
401 textures_.erase(texture_id);
402 }
403
404 void VideoDecoderResource::OnPluginMsgNotifyError(
405 const ResourceMessageReplyParams& params,
406 int32_t error) {
407 decoder_last_error_ = error;
408 // Cause any pending callbacks to run immediately. Reentrancy isn't a problem,
409 // since the plugin wasn't calling us.
410 RunCallbackWithError(&initialize_callback_);
411 RunCallbackWithError(&decode_callback_);
412 RunCallbackWithError(&get_picture_callback_);
413 RunCallbackWithError(&flush_callback_);
414 RunCallbackWithError(&reset_callback_);
415 }
416
417 void VideoDecoderResource::OnPluginMsgInitializeComplete(
418 const ResourceMessageReplyParams& params) {
419 decoder_last_error_ = params.result();
420 if (decoder_last_error_ == PP_OK)
421 initialized_ = true;
422
423 // Let the plugin call Initialize again from its callback in case of failure.
424 scoped_refptr<TrackedCallback> callback;
425 callback.swap(initialize_callback_);
426 callback->Run(decoder_last_error_);
427 }
428
429 void VideoDecoderResource::OnPluginMsgDecodeComplete(
430 const ResourceMessageReplyParams& params,
431 uint32_t shm_id) {
432 if (shm_id >= shm_buffers_.size()) {
433 NOTREACHED();
434 return;
435 }
436 // Make the shm buffer available.
437 available_shm_buffers_.push_back(shm_buffers_[shm_id]);
438 // If the plugin is waiting, let it call Decode again.
439 if (decode_callback_) {
440 scoped_refptr<TrackedCallback> callback;
441 callback.swap(decode_callback_);
442 callback->Run(PP_OK);
443 }
444 }
445
446 void VideoDecoderResource::OnPluginMsgFlushComplete(
447 const ResourceMessageReplyParams& params) {
448 // All shm buffers should have been made available by now.
449 DCHECK_EQ(shm_buffers_.size(), available_shm_buffers_.size());
450
451 if (get_picture_callback_) {
452 scoped_refptr<TrackedCallback> callback;
453 callback.swap(get_picture_callback_);
454 callback->Abort();
455 }
456
457 scoped_refptr<TrackedCallback> callback;
458 callback.swap(flush_callback_);
459 callback->Run(params.result());
460 }
461
462 void VideoDecoderResource::OnPluginMsgResetComplete(
463 const ResourceMessageReplyParams& params) {
464 // All shm buffers should have been made available by now.
465 DCHECK_EQ(shm_buffers_.size(), available_shm_buffers_.size());
466 scoped_refptr<TrackedCallback> callback;
467 callback.swap(reset_callback_);
468 callback->Run(params.result());
469 }
470
471 void VideoDecoderResource::RunCallbackWithError(
472 scoped_refptr<TrackedCallback>* callback) {
473 if (TrackedCallback::IsPending(*callback)) {
474 scoped_refptr<TrackedCallback> temp;
475 callback->swap(temp);
476 temp->Run(decoder_last_error_);
477 }
478 }
479
480 void VideoDecoderResource::DeleteGLTexture(uint32_t id) {
481 if (gles2_impl_) {
482 gles2_impl_->DeleteTextures(1, &id);
483 gles2_impl_->Flush();
484 }
485 }
486
487 void VideoDecoderResource::WriteNextPicture(PP_VideoPicture* pp_picture) {
488 DCHECK(!received_pictures_.empty());
489 Picture& picture = received_pictures_.front();
490 uint32_t texture_id = picture.texture_id;
491 TextureMap::iterator it = textures_.find(texture_id);
492 DCHECK(it != textures_.end());
493 // The resource and host identify decodes by a unique id, |num_decodes_|. Use
494 // this to get the plugin's id value, which we stored in |decode_ids_|.
495 pp_picture->decode_id = decode_ids_[picture.decode_id % kMaximumPictureDelay];
496 pp_picture->texture_id = texture_id;
497 pp_picture->texture_target = it->second.texture_target;
498 pp_picture->texture_size = it->second.size;
499 received_pictures_.pop();
500 }
501
502 } // namespace proxy
503 } // namespace ppapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698