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 "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"
24 using ppapi::thunk::EnterResourceNoLock
;
25 using ppapi::thunk::PPB_Graphics3D_API
;
26 using ppapi::thunk::PPB_VideoDecoder_API
;
31 VideoDecoderResource::ShmBuffer::ShmBuffer(
32 scoped_ptr
<base::SharedMemory
> shm_ptr
,
35 : shm(shm_ptr
.Pass()), addr(NULL
), shm_id(shm_id
) {
40 VideoDecoderResource::ShmBuffer::~ShmBuffer() {
43 VideoDecoderResource::Texture::Texture(uint32_t texture_target
,
45 : texture_target(texture_target
), size(size
) {
48 VideoDecoderResource::Texture::~Texture() {
51 VideoDecoderResource::Picture::Picture(int32_t decode_id
, uint32_t texture_id
)
52 : decode_id(decode_id
), texture_id(texture_id
) {
55 VideoDecoderResource::Picture::~Picture() {
58 VideoDecoderResource::VideoDecoderResource(Connection connection
,
60 : PluginResource(connection
, instance
),
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());
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
);
83 PPB_VideoDecoder_API
* VideoDecoderResource::AsPPB_VideoDecoder_API() {
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
) {
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
;
101 HostResource host_resource
;
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
};
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(),
116 if (enter_graphics
.failed())
117 return PP_ERROR_BADRESOURCE
;
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();
125 initialize_callback_
= callback
;
127 Call
<PpapiPluginMsg_VideoDecoder_InitializeReply
>(
129 PpapiHostMsg_VideoDecoder_Initialize(
130 host_resource
, profile
, PP_ToBool(allow_software_fallback
)),
131 base::Bind(&VideoDecoderResource::OnPluginMsgInitializeComplete
, this));
133 return PP_OK_COMPLETIONPENDING
;
136 int32_t VideoDecoderResource::Decode(uint32_t decode_id
,
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
;
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
);
154 // Count up, wrapping back to 0 before overflowing.
155 int32_t uid
= ++num_decodes_
;
156 if (uid
== std::numeric_limits
<int32_t>::max())
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
;
163 if (available_shm_buffers_
.empty() ||
164 available_shm_buffers_
.back()->shm
->mapped_size() < size
) {
166 if (shm_buffers_
.size() < kMaximumPendingDecodes
) {
167 // Signal the host to create a new shm buffer by passing an index outside
169 shm_id
= static_cast<uint32_t>(shm_buffers_
.size());
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();
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;
181 ResourceMessageReplyParams reply_params
;
183 GenericSyncCall(RENDERER
,
184 PpapiHostMsg_VideoDecoder_GetShm(shm_id
, size
),
188 return PP_ERROR_FAILED
;
189 if (!UnpackMessage
<PpapiPluginMsg_VideoDecoder_GetShmReply
>(reply
,
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
;
202 available_shm_buffers_
.push_back(shm_buffer
.get());
203 if (shm_buffers_
.size() < kMaximumPendingDecodes
) {
204 shm_buffers_
.push_back(shm_buffer
.release());
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();
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
);
217 ShmBuffer
* shm_buffer
= available_shm_buffers_
.back();
218 available_shm_buffers_
.pop_back();
219 memcpy(shm_buffer
->addr
, buffer
, size
);
221 Call
<PpapiPluginMsg_VideoDecoder_DecodeReply
>(
223 PpapiHostMsg_VideoDecoder_Decode(shm_buffer
->shm_id
, size
, uid
),
224 base::Bind(&VideoDecoderResource::OnPluginMsgDecodeComplete
, this));
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
)
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
;
238 int32_t VideoDecoderResource::GetPicture(
239 PP_VideoPicture
* picture
,
240 scoped_refptr
<TrackedCallback
> callback
) {
241 if (decoder_last_error_
)
242 return decoder_last_error_
;
244 return PP_ERROR_FAILED
;
245 if (get_picture_callback_
)
246 return PP_ERROR_INPROGRESS
;
248 // If the next picture is ready, return it synchronously.
249 if (!received_pictures_
.empty()) {
250 WriteNextPicture(picture
);
254 get_picture_callback_
= callback
;
255 get_picture_
= picture
;
256 return PP_OK_COMPLETIONPENDING
;
259 void VideoDecoderResource::RecyclePicture(const PP_VideoPicture
* picture
) {
260 if (decoder_last_error_
)
265 Post(RENDERER
, PpapiHostMsg_VideoDecoder_RecyclePicture(picture
->texture_id
));
268 int32_t VideoDecoderResource::Flush(scoped_refptr
<TrackedCallback
> callback
) {
269 if (decoder_last_error_
)
270 return decoder_last_error_
;
272 return PP_ERROR_FAILED
;
274 return PP_ERROR_INPROGRESS
;
275 flush_callback_
= callback
;
277 Call
<PpapiPluginMsg_VideoDecoder_FlushReply
>(
279 PpapiHostMsg_VideoDecoder_Flush(),
280 base::Bind(&VideoDecoderResource::OnPluginMsgFlushComplete
, this));
282 return PP_OK_COMPLETIONPENDING
;
285 int32_t VideoDecoderResource::Reset(scoped_refptr
<TrackedCallback
> callback
) {
286 if (decoder_last_error_
)
287 return decoder_last_error_
;
289 return PP_ERROR_FAILED
;
291 return PP_ERROR_INPROGRESS
;
292 reset_callback_
= callback
;
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
>(
304 PpapiHostMsg_VideoDecoder_Reset(),
305 base::Bind(&VideoDecoderResource::OnPluginMsgResetComplete
, this));
307 return PP_OK_COMPLETIONPENDING
;
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()
327 void VideoDecoderResource::SetForTest() {
331 void VideoDecoderResource::OnPluginMsgRequestTextures(
332 const ResourceMessageReplyParams
& params
,
333 uint32_t num_textures
,
335 uint32_t texture_target
) {
336 DCHECK(num_textures
);
337 std::vector
<uint32_t> texture_ids(num_textures
);
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
);
352 if (texture_target
== GL_TEXTURE_2D
) {
353 gles2_impl_
->TexImage2D(texture_target
,
365 std::make_pair(texture_ids
[i
], Texture(texture_target
, size
)));
367 gles2_impl_
->Flush();
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;
374 std::make_pair(texture_ids
[i
], Texture(texture_target
, size
)));
378 Post(RENDERER
, PpapiHostMsg_VideoDecoder_AssignTextures(size
, texture_ids
));
381 void VideoDecoderResource::OnPluginMsgPictureReady(
382 const ResourceMessageReplyParams
& params
,
384 uint32_t texture_id
) {
385 received_pictures_
.push(Picture(decode_id
, texture_id
));
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_
;
393 WriteNextPicture(picture
);
394 callback
->Run(PP_OK
);
398 void VideoDecoderResource::OnPluginMsgDismissPicture(
399 const ResourceMessageReplyParams
& params
,
400 uint32_t texture_id
) {
401 DeleteGLTexture(texture_id
);
402 textures_
.erase(texture_id
);
405 void VideoDecoderResource::OnPluginMsgNotifyError(
406 const ResourceMessageReplyParams
& params
,
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_
);
418 void VideoDecoderResource::OnPluginMsgInitializeComplete(
419 const ResourceMessageReplyParams
& params
) {
420 decoder_last_error_
= params
.result();
421 if (decoder_last_error_
== PP_OK
)
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_
);
430 void VideoDecoderResource::OnPluginMsgDecodeComplete(
431 const ResourceMessageReplyParams
& params
,
433 if (shm_id
>= shm_buffers_
.size()) {
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
);
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());
452 if (get_picture_callback_
) {
453 scoped_refptr
<TrackedCallback
> callback
;
454 callback
.swap(get_picture_callback_
);
458 scoped_refptr
<TrackedCallback
> callback
;
459 callback
.swap(flush_callback_
);
460 callback
->Run(params
.result());
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());
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_
);
481 void VideoDecoderResource::DeleteGLTexture(uint32_t id
) {
483 gles2_impl_
->DeleteTextures(1, &id
);
484 gles2_impl_
->Flush();
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
;
502 received_pictures_
.pop();