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.
5 #include "ppapi/proxy/video_decoder_resource.h"
8 #include "gpu/command_buffer/client/gles2_cmd_helper.h"
9 #include "gpu/command_buffer/client/gles2_implementation.h"
10 #include "gpu/command_buffer/common/mailbox.h"
11 #include "ipc/ipc_message.h"
12 #include "ppapi/c/pp_errors.h"
13 #include "ppapi/c/ppb_opengles2.h"
14 #include "ppapi/proxy/plugin_dispatcher.h"
15 #include "ppapi/proxy/ppapi_messages.h"
16 #include "ppapi/proxy/ppb_graphics_3d_proxy.h"
17 #include "ppapi/proxy/serialized_handle.h"
18 #include "ppapi/proxy/video_decoder_constants.h"
19 #include "ppapi/shared_impl/ppapi_globals.h"
20 #include "ppapi/shared_impl/ppb_graphics_3d_shared.h"
21 #include "ppapi/shared_impl/proxy_lock.h"
22 #include "ppapi/shared_impl/resource_tracker.h"
23 #include "ppapi/thunk/enter.h"
25 using ppapi::thunk::EnterResourceNoLock
;
26 using ppapi::thunk::PPB_Graphics3D_API
;
27 using ppapi::thunk::PPB_VideoDecoder_API
;
32 VideoDecoderResource::ShmBuffer::ShmBuffer(
33 scoped_ptr
<base::SharedMemory
> shm_ptr
,
36 : shm(shm_ptr
.Pass()), addr(NULL
), shm_id(shm_id
) {
41 VideoDecoderResource::ShmBuffer::~ShmBuffer() {
44 VideoDecoderResource::Texture::Texture(uint32_t texture_target
,
46 : texture_target(texture_target
), size(size
) {
49 VideoDecoderResource::Texture::~Texture() {
52 VideoDecoderResource::Picture::Picture(int32_t decode_id
,
54 const PP_Rect
& visible_rect
)
55 : decode_id(decode_id
), texture_id(texture_id
), visible_rect(visible_rect
) {
58 VideoDecoderResource::Picture::~Picture() {
61 VideoDecoderResource::VideoDecoderResource(Connection connection
,
63 : PluginResource(connection
, instance
),
66 get_picture_0_1_(NULL
),
70 // Set |decoder_last_error_| to PP_OK after successful initialization.
71 // This makes error checking a little more concise, since we can check
72 // that the decoder has been initialized and hasn't returned an error by
73 // just testing |decoder_last_error_|.
74 decoder_last_error_(PP_ERROR_FAILED
) {
75 // Clear the decode_ids_ array.
76 memset(decode_ids_
, 0, sizeof(decode_ids_
));
77 SendCreate(RENDERER
, PpapiHostMsg_VideoDecoder_Create());
80 VideoDecoderResource::~VideoDecoderResource() {
81 // Destroy any textures which haven't been dismissed.
82 TextureMap::iterator it
= textures_
.begin();
83 for (; it
!= textures_
.end(); ++it
)
84 DeleteGLTexture(it
->first
);
87 PPB_VideoDecoder_API
* VideoDecoderResource::AsPPB_VideoDecoder_API() {
91 int32_t VideoDecoderResource::Initialize0_1(
92 PP_Resource graphics_context
,
93 PP_VideoProfile profile
,
94 PP_Bool allow_software_fallback
,
95 scoped_refptr
<TrackedCallback
> callback
) {
96 return Initialize(graphics_context
,
98 allow_software_fallback
99 ? PP_HARDWAREACCELERATION_WITHFALLBACK
100 : PP_HARDWAREACCELERATION_ONLY
,
104 int32_t VideoDecoderResource::Initialize(
105 PP_Resource graphics_context
,
106 PP_VideoProfile profile
,
107 PP_HardwareAcceleration acceleration
,
108 scoped_refptr
<TrackedCallback
> callback
) {
110 return PP_ERROR_FAILED
;
111 if (profile
< 0 || profile
> PP_VIDEOPROFILE_MAX
)
112 return PP_ERROR_BADARGUMENT
;
113 if (initialize_callback_
.get())
114 return PP_ERROR_INPROGRESS
;
115 if (!graphics_context
)
116 return PP_ERROR_BADRESOURCE
;
118 HostResource host_resource
;
120 // Create a new Graphics3D resource that can create texture resources to
121 // share with the plugin. We can't use the plugin's Graphics3D, since we
122 // create textures on a proxy thread, and would interfere with the plugin.
123 thunk::EnterResourceCreationNoLock
enter_create(pp_instance());
124 if (enter_create
.failed())
125 return PP_ERROR_FAILED
;
126 int32_t attrib_list
[] = {PP_GRAPHICS3DATTRIB_NONE
};
128 ScopedPPResource(ScopedPPResource::PassRef(),
129 enter_create
.functions()->CreateGraphics3D(
130 pp_instance(), graphics_context
, attrib_list
));
131 EnterResourceNoLock
<PPB_Graphics3D_API
> enter_graphics(graphics3d_
.get(),
133 if (enter_graphics
.failed())
134 return PP_ERROR_BADRESOURCE
;
136 PPB_Graphics3D_Shared
* ppb_graphics3d_shared
=
137 static_cast<PPB_Graphics3D_Shared
*>(enter_graphics
.object());
138 gles2_impl_
= ppb_graphics3d_shared
->gles2_impl();
139 host_resource
= ppb_graphics3d_shared
->host_resource();
142 initialize_callback_
= callback
;
144 Call
<PpapiPluginMsg_VideoDecoder_InitializeReply
>(
146 PpapiHostMsg_VideoDecoder_Initialize(
147 host_resource
, profile
, acceleration
),
148 base::Bind(&VideoDecoderResource::OnPluginMsgInitializeComplete
, this));
150 return PP_OK_COMPLETIONPENDING
;
153 int32_t VideoDecoderResource::Decode(uint32_t decode_id
,
156 scoped_refptr
<TrackedCallback
> callback
) {
157 if (decoder_last_error_
)
158 return decoder_last_error_
;
159 if (flush_callback_
.get() || reset_callback_
.get())
160 return PP_ERROR_FAILED
;
161 if (decode_callback_
.get())
162 return PP_ERROR_INPROGRESS
;
163 if (size
> kMaximumBitstreamBufferSize
)
164 return PP_ERROR_NOMEMORY
;
166 // If we allow the plugin to call Decode again, we must have somewhere to
167 // copy their buffer.
168 DCHECK(!available_shm_buffers_
.empty() ||
169 shm_buffers_
.size() < kMaximumPendingDecodes
);
171 // Count up, wrapping back to 0 before overflowing.
172 int32_t uid
= ++num_decodes_
;
173 if (uid
== std::numeric_limits
<int32_t>::max())
176 // Save decode_id in a ring buffer. The ring buffer is sized to store
177 // decode_id for the maximum picture delay.
178 decode_ids_
[uid
% kMaximumPictureDelay
] = decode_id
;
180 if (available_shm_buffers_
.empty() ||
181 available_shm_buffers_
.back()->shm
->mapped_size() < size
) {
183 if (shm_buffers_
.size() < kMaximumPendingDecodes
) {
184 // Signal the host to create a new shm buffer by passing an index outside
186 shm_id
= static_cast<uint32_t>(shm_buffers_
.size());
188 // Signal the host to grow a buffer by passing a legal index. Choose the
189 // last available shm buffer for simplicity.
190 shm_id
= available_shm_buffers_
.back()->shm_id
;
191 available_shm_buffers_
.pop_back();
194 // Synchronously get shared memory. Use GenericSyncCall so we can get the
195 // reply params, which contain the handle.
196 uint32_t shm_size
= 0;
198 ResourceMessageReplyParams reply_params
;
200 GenericSyncCall(RENDERER
,
201 PpapiHostMsg_VideoDecoder_GetShm(shm_id
, size
),
205 return PP_ERROR_FAILED
;
206 if (!UnpackMessage
<PpapiPluginMsg_VideoDecoder_GetShmReply
>(reply
,
208 return PP_ERROR_FAILED
;
209 base::SharedMemoryHandle shm_handle
= base::SharedMemory::NULLHandle();
210 if (!reply_params
.TakeSharedMemoryHandleAtIndex(0, &shm_handle
))
211 return PP_ERROR_NOMEMORY
;
212 scoped_ptr
<base::SharedMemory
> shm(
213 new base::SharedMemory(shm_handle
, false /* read_only */));
214 scoped_ptr
<ShmBuffer
> shm_buffer(
215 new ShmBuffer(shm
.Pass(), shm_size
, shm_id
));
216 if (!shm_buffer
->addr
)
217 return PP_ERROR_NOMEMORY
;
219 available_shm_buffers_
.push_back(shm_buffer
.get());
220 if (shm_buffers_
.size() < kMaximumPendingDecodes
) {
221 shm_buffers_
.push_back(shm_buffer
.release());
223 // Delete manually since ScopedVector won't delete the existing element if
224 // we just assign it.
225 delete shm_buffers_
[shm_id
];
226 shm_buffers_
[shm_id
] = shm_buffer
.release();
230 // At this point we should have shared memory to hold the plugin's buffer.
231 DCHECK(!available_shm_buffers_
.empty() &&
232 available_shm_buffers_
.back()->shm
->mapped_size() >= size
);
234 ShmBuffer
* shm_buffer
= available_shm_buffers_
.back();
235 available_shm_buffers_
.pop_back();
236 memcpy(shm_buffer
->addr
, buffer
, size
);
238 Call
<PpapiPluginMsg_VideoDecoder_DecodeReply
>(
240 PpapiHostMsg_VideoDecoder_Decode(shm_buffer
->shm_id
, size
, uid
),
241 base::Bind(&VideoDecoderResource::OnPluginMsgDecodeComplete
, this));
243 // If we have another free buffer, or we can still create new buffers, let
244 // the plugin call Decode again.
245 if (!available_shm_buffers_
.empty() ||
246 shm_buffers_
.size() < kMaximumPendingDecodes
)
249 // All buffers are busy and we can't create more. Delay completion until a
250 // buffer is available.
251 decode_callback_
= callback
;
252 return PP_OK_COMPLETIONPENDING
;
255 int32_t VideoDecoderResource::GetPicture0_1(
256 PP_VideoPicture_0_1
* picture
,
257 scoped_refptr
<TrackedCallback
> callback
) {
258 get_picture_0_1_
= picture
;
259 return GetPicture(NULL
, callback
);
262 int32_t VideoDecoderResource::GetPicture(
263 PP_VideoPicture
* picture
,
264 scoped_refptr
<TrackedCallback
> callback
) {
265 if (decoder_last_error_
)
266 return decoder_last_error_
;
267 if (reset_callback_
.get())
268 return PP_ERROR_FAILED
;
269 if (get_picture_callback_
.get())
270 return PP_ERROR_INPROGRESS
;
272 get_picture_
= picture
;
274 // If the next picture is ready, return it synchronously.
275 if (!received_pictures_
.empty()) {
280 get_picture_callback_
= callback
;
282 return PP_OK_COMPLETIONPENDING
;
285 void VideoDecoderResource::RecyclePicture(const PP_VideoPicture
* picture
) {
286 if (decoder_last_error_
)
289 Post(RENDERER
, PpapiHostMsg_VideoDecoder_RecyclePicture(picture
->texture_id
));
292 int32_t VideoDecoderResource::Flush(scoped_refptr
<TrackedCallback
> callback
) {
293 if (decoder_last_error_
)
294 return decoder_last_error_
;
295 if (reset_callback_
.get())
296 return PP_ERROR_FAILED
;
297 if (flush_callback_
.get())
298 return PP_ERROR_INPROGRESS
;
299 flush_callback_
= callback
;
301 Call
<PpapiPluginMsg_VideoDecoder_FlushReply
>(
303 PpapiHostMsg_VideoDecoder_Flush(),
304 base::Bind(&VideoDecoderResource::OnPluginMsgFlushComplete
, this));
306 return PP_OK_COMPLETIONPENDING
;
309 int32_t VideoDecoderResource::Reset(scoped_refptr
<TrackedCallback
> callback
) {
310 if (decoder_last_error_
)
311 return decoder_last_error_
;
312 if (flush_callback_
.get())
313 return PP_ERROR_FAILED
;
314 if (reset_callback_
.get())
315 return PP_ERROR_INPROGRESS
;
316 reset_callback_
= callback
;
318 // Cause any pending Decode or GetPicture callbacks to abort after we return,
319 // to avoid reentering the plugin.
320 if (TrackedCallback::IsPending(decode_callback_
))
321 decode_callback_
->PostAbort();
322 decode_callback_
= NULL
;
323 if (TrackedCallback::IsPending(get_picture_callback_
))
324 get_picture_callback_
->PostAbort();
325 get_picture_callback_
= NULL
;
326 Call
<PpapiPluginMsg_VideoDecoder_ResetReply
>(
328 PpapiHostMsg_VideoDecoder_Reset(),
329 base::Bind(&VideoDecoderResource::OnPluginMsgResetComplete
, this));
331 return PP_OK_COMPLETIONPENDING
;
334 void VideoDecoderResource::OnReplyReceived(
335 const ResourceMessageReplyParams
& params
,
336 const IPC::Message
& msg
) {
337 PPAPI_BEGIN_MESSAGE_MAP(VideoDecoderResource
, msg
)
338 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
339 PpapiPluginMsg_VideoDecoder_RequestTextures
, OnPluginMsgRequestTextures
)
340 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
341 PpapiPluginMsg_VideoDecoder_PictureReady
, OnPluginMsgPictureReady
)
342 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
343 PpapiPluginMsg_VideoDecoder_DismissPicture
, OnPluginMsgDismissPicture
)
344 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
345 PpapiPluginMsg_VideoDecoder_NotifyError
, OnPluginMsgNotifyError
)
346 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED(
347 PluginResource::OnReplyReceived(params
, msg
))
348 PPAPI_END_MESSAGE_MAP()
351 void VideoDecoderResource::SetForTest() {
355 void VideoDecoderResource::OnPluginMsgRequestTextures(
356 const ResourceMessageReplyParams
& params
,
357 uint32_t num_textures
,
359 uint32_t texture_target
,
360 const std::vector
<gpu::Mailbox
>& mailboxes
) {
361 DCHECK(num_textures
);
362 DCHECK(mailboxes
.empty() || mailboxes
.size() == num_textures
);
363 std::vector
<uint32_t> texture_ids(num_textures
);
365 gles2_impl_
->GenTextures(num_textures
, &texture_ids
.front());
366 for (uint32_t i
= 0; i
< num_textures
; ++i
) {
367 gles2_impl_
->ActiveTexture(GL_TEXTURE0
);
368 gles2_impl_
->BindTexture(texture_target
, texture_ids
[i
]);
369 gles2_impl_
->TexParameteri(
370 texture_target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
371 gles2_impl_
->TexParameteri(
372 texture_target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
373 gles2_impl_
->TexParameterf(
374 texture_target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
375 gles2_impl_
->TexParameterf(
376 texture_target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
378 if (texture_target
== GL_TEXTURE_2D
) {
379 gles2_impl_
->TexImage2D(texture_target
,
389 if (!mailboxes
.empty()) {
390 gles2_impl_
->ProduceTextureCHROMIUM(
391 GL_TEXTURE_2D
, reinterpret_cast<const GLbyte
*>(mailboxes
[i
].name
));
395 std::make_pair(texture_ids
[i
], Texture(texture_target
, size
)));
397 gles2_impl_
->Flush();
400 // Create some fake texture ids so we can test picture handling.
401 for (uint32_t i
= 0; i
< num_textures
; ++i
) {
402 texture_ids
[i
] = i
+ 1;
404 std::make_pair(texture_ids
[i
], Texture(texture_target
, size
)));
408 Post(RENDERER
, PpapiHostMsg_VideoDecoder_AssignTextures(size
, texture_ids
));
411 void VideoDecoderResource::OnPluginMsgPictureReady(
412 const ResourceMessageReplyParams
& params
,
415 const PP_Rect
& visible_rect
) {
416 received_pictures_
.push(Picture(decode_id
, texture_id
, visible_rect
));
418 if (TrackedCallback::IsPending(get_picture_callback_
)) {
419 // The plugin may call GetPicture in its callback.
420 scoped_refptr
<TrackedCallback
> callback
;
421 callback
.swap(get_picture_callback_
);
423 callback
->Run(PP_OK
);
427 void VideoDecoderResource::OnPluginMsgDismissPicture(
428 const ResourceMessageReplyParams
& params
,
429 uint32_t texture_id
) {
430 DeleteGLTexture(texture_id
);
431 textures_
.erase(texture_id
);
434 void VideoDecoderResource::OnPluginMsgNotifyError(
435 const ResourceMessageReplyParams
& params
,
437 decoder_last_error_
= error
;
438 // Cause any pending callbacks to run immediately. Reentrancy isn't a problem,
439 // since the plugin wasn't calling us.
440 RunCallbackWithError(&initialize_callback_
);
441 RunCallbackWithError(&decode_callback_
);
442 RunCallbackWithError(&get_picture_callback_
);
443 RunCallbackWithError(&flush_callback_
);
444 RunCallbackWithError(&reset_callback_
);
447 void VideoDecoderResource::OnPluginMsgInitializeComplete(
448 const ResourceMessageReplyParams
& params
) {
449 decoder_last_error_
= params
.result();
450 if (decoder_last_error_
== PP_OK
)
453 // Let the plugin call Initialize again from its callback in case of failure.
454 scoped_refptr
<TrackedCallback
> callback
;
455 callback
.swap(initialize_callback_
);
456 callback
->Run(decoder_last_error_
);
459 void VideoDecoderResource::OnPluginMsgDecodeComplete(
460 const ResourceMessageReplyParams
& params
,
462 if (shm_id
>= shm_buffers_
.size()) {
466 // Make the shm buffer available.
467 available_shm_buffers_
.push_back(shm_buffers_
[shm_id
]);
468 // If the plugin is waiting, let it call Decode again.
469 if (decode_callback_
.get()) {
470 scoped_refptr
<TrackedCallback
> callback
;
471 callback
.swap(decode_callback_
);
472 callback
->Run(PP_OK
);
476 void VideoDecoderResource::OnPluginMsgFlushComplete(
477 const ResourceMessageReplyParams
& params
) {
478 // All shm buffers should have been made available by now.
479 DCHECK_EQ(shm_buffers_
.size(), available_shm_buffers_
.size());
481 if (get_picture_callback_
.get()) {
482 scoped_refptr
<TrackedCallback
> callback
;
483 callback
.swap(get_picture_callback_
);
487 scoped_refptr
<TrackedCallback
> callback
;
488 callback
.swap(flush_callback_
);
489 callback
->Run(params
.result());
492 void VideoDecoderResource::OnPluginMsgResetComplete(
493 const ResourceMessageReplyParams
& params
) {
494 // All shm buffers should have been made available by now.
495 DCHECK_EQ(shm_buffers_
.size(), available_shm_buffers_
.size());
496 // Recycle any pictures which haven't been passed to the plugin.
497 while (!received_pictures_
.empty()) {
498 Post(RENDERER
, PpapiHostMsg_VideoDecoder_RecyclePicture(
499 received_pictures_
.front().texture_id
));
500 received_pictures_
.pop();
503 scoped_refptr
<TrackedCallback
> callback
;
504 callback
.swap(reset_callback_
);
505 callback
->Run(params
.result());
508 void VideoDecoderResource::RunCallbackWithError(
509 scoped_refptr
<TrackedCallback
>* callback
) {
510 if (TrackedCallback::IsPending(*callback
)) {
511 scoped_refptr
<TrackedCallback
> temp
;
512 callback
->swap(temp
);
513 temp
->Run(decoder_last_error_
);
517 void VideoDecoderResource::DeleteGLTexture(uint32_t id
) {
519 gles2_impl_
->DeleteTextures(1, &id
);
520 gles2_impl_
->Flush();
524 void VideoDecoderResource::WriteNextPicture() {
525 DCHECK(!received_pictures_
.empty());
526 Picture
& picture
= received_pictures_
.front();
528 // Internally, we identify decodes by a unique id, which the host returns
529 // to us in the picture. Use this to get the plugin's decode_id.
530 uint32_t decode_id
= decode_ids_
[picture
.decode_id
% kMaximumPictureDelay
];
531 uint32_t texture_id
= picture
.texture_id
;
532 uint32_t texture_target
= 0;
533 PP_Size texture_size
= PP_MakeSize(0, 0);
534 TextureMap::iterator it
= textures_
.find(picture
.texture_id
);
535 if (it
!= textures_
.end()) {
536 texture_target
= it
->second
.texture_target
;
537 texture_size
= it
->second
.size
;
543 DCHECK(!get_picture_0_1_
);
544 get_picture_
->decode_id
= decode_id
;
545 get_picture_
->texture_id
= texture_id
;
546 get_picture_
->texture_target
= texture_target
;
547 get_picture_
->texture_size
= texture_size
;
548 get_picture_
->visible_rect
= picture
.visible_rect
;
551 DCHECK(get_picture_0_1_
);
552 get_picture_0_1_
->decode_id
= decode_id
;
553 get_picture_0_1_
->texture_id
= texture_id
;
554 get_picture_0_1_
->texture_target
= texture_target
;
555 get_picture_0_1_
->texture_size
= texture_size
;
556 get_picture_0_1_
= NULL
;
559 received_pictures_
.pop();