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

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

Powered by Google App Engine
This is Rietveld 408576698