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

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

Powered by Google App Engine
This is Rietveld 408576698