1 // Copyright (c) 2014 The Intel Corporation. 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 "ozone/media/vaapi_video_decode_accelerator.h"
10 #include "base/debug/trace_event.h"
11 #include "base/logging.h"
12 #include "base/metrics/histogram.h"
13 #include "base/stl_util.h"
14 #include "base/strings/string_util.h"
15 #include "base/synchronization/waitable_event.h"
16 #include "base/threading/non_thread_safe.h"
17 #include "content/common/gpu/gpu_channel.h"
18 #include "media/base/bind_to_current_loop.h"
19 #include "media/video/picture.h"
20 #include "ui/gl/gl_bindings.h"
21 #include "ui/gl/gl_surface_egl.h"
22 #include "ui/gl/scoped_binders.h"
24 static void ReportToUMA(
25 media::VaapiH264Decoder::VAVDAH264DecoderFailure failure) {
26 UMA_HISTOGRAM_ENUMERATION(
27 "Media.VAVDAH264.DecoderFailure",
29 media::VaapiH264Decoder::VAVDA_H264_DECODER_FAILURES_MAX);
34 #define RETURN_AND_NOTIFY_ON_FAILURE(result, log, error_code, ret) \
38 NotifyError(error_code); \
43 VaapiVideoDecodeAccelerator::InputBuffer::InputBuffer() : id(0), size(0) {
46 VaapiVideoDecodeAccelerator::InputBuffer::~InputBuffer() {
49 void VaapiVideoDecodeAccelerator::NotifyError(Error error) {
50 if (message_loop_ != base::MessageLoop::current()) {
51 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
52 message_loop_->PostTask(FROM_HERE, base::Bind(
53 &VaapiVideoDecodeAccelerator::NotifyError, weak_this_, error));
57 // Post Cleanup() as a task so we don't recursively acquire lock_.
58 message_loop_->PostTask(FROM_HERE, base::Bind(
59 &VaapiVideoDecodeAccelerator::Cleanup, weak_this_));
61 DVLOG(1) << "Notifying of error " << error;
63 client_->NotifyError(error);
64 client_ptr_factory_.reset();
68 // TFPPicture allocates VAImage and binds them to textures passed
69 // in PictureBuffers from clients to them. TFPPictures are created as
70 // a consequence of receiving a set of PictureBuffers from clients and released
71 // at the end of decode (or when a new set of PictureBuffers is required).
73 // TFPPictures are used for output, contents of VASurfaces passed from decoder
74 // are put into the associated vaimage memory and upload to client.
75 class VaapiVideoDecodeAccelerator::TFPPicture : public base::NonThreadSafe {
79 static linked_ptr<TFPPicture> Create(
80 const base::Callback<bool(void)>& make_context_current, //NOLINT
81 VaapiWrapper* va_wrapper,
82 int32 picture_buffer_id,
86 int32 picture_buffer_id() {
87 return picture_buffer_id_;
95 return va_image_.get();
98 // Upload vaimage data to texture. Needs to be called every frame.
99 bool Upload(VASurfaceID id);
101 // Bind EGL image to texture. Needs to be called every frame.
103 bool UpdateEGLImage(VASurfaceID id);
106 TFPPicture(const base::Callback<bool(void)>& make_context_current, //NOLINT
107 VaapiWrapper* va_wrapper,
108 int32 picture_buffer_id,
114 EGLImageKHR CreateEGLImage(
115 EGLDisplay egl_display, VASurfaceID surface, VAImage* va_image);
116 bool DestroyEGLImage(EGLDisplay egl_display, EGLImageKHR egl_image);
118 base::Callback<bool(void)> make_context_current_; //NOLINT
120 VaapiWrapper* va_wrapper_;
122 // Output id for the client.
123 int32 picture_buffer_id_;
127 scoped_ptr<VAImage> va_image_;
128 EGLImageKHR egl_image_;
129 EGLDisplay egl_display_;
131 DISALLOW_COPY_AND_ASSIGN(TFPPicture);
134 VaapiVideoDecodeAccelerator::TFPPicture::TFPPicture(
135 const base::Callback<bool(void)>& make_context_current, //NOLINT
136 VaapiWrapper* va_wrapper,
137 int32 picture_buffer_id,
140 : make_context_current_(make_context_current),
141 va_wrapper_(va_wrapper),
142 picture_buffer_id_(picture_buffer_id),
143 texture_id_(texture_id),
145 va_image_(new VAImage()),
146 egl_display_(gfx::GLSurfaceEGL::GetHardwareDisplay()) {
147 DCHECK(!make_context_current_.is_null());
151 linked_ptr<VaapiVideoDecodeAccelerator::TFPPicture>
152 VaapiVideoDecodeAccelerator::TFPPicture::Create(
153 const base::Callback<bool(void)>& make_context_current, //NOLINT
154 VaapiWrapper* va_wrapper,
155 int32 picture_buffer_id,
158 linked_ptr<TFPPicture> tfp_picture(
159 new TFPPicture(make_context_current, va_wrapper,
160 picture_buffer_id, texture_id, size));
162 if (!tfp_picture->Initialize())
168 bool VaapiVideoDecodeAccelerator::TFPPicture::Initialize() {
169 DCHECK(CalledOnValidThread());
170 if (!make_context_current_.Run())
173 if (!va_wrapper_->CreateRGBImage(size_, va_image_.get())) {
174 DVLOG(1) << "Failed to create VAImage";
181 VaapiVideoDecodeAccelerator::TFPPicture::~TFPPicture() {
182 DCHECK(CalledOnValidThread());
184 if (egl_image_ != EGL_NO_IMAGE_KHR)
185 DestroyEGLImage(egl_display_, egl_image_);
188 va_wrapper_->DestroyImage(va_image_.get());
192 bool VaapiVideoDecodeAccelerator::TFPPicture::UpdateEGLImage(
193 VASurfaceID surface) {
194 DCHECK(CalledOnValidThread());
196 if (!make_context_current_.Run())
199 if (egl_image_ != EGL_NO_IMAGE_KHR)
200 DestroyEGLImage(egl_display_, egl_image_);
202 egl_image_ = CreateEGLImage(egl_display_, surface, va_image_.get());
203 if (egl_image_ == EGL_NO_IMAGE_KHR) {
204 DVLOG(1) << "Failed to create EGL image";
211 bool VaapiVideoDecodeAccelerator::TFPPicture::Upload(VASurfaceID surface) {
212 DCHECK(CalledOnValidThread());
214 if (!make_context_current_.Run())
217 if (!va_wrapper_->PutSurfaceIntoImage(surface, va_image_.get())) {
218 DVLOG(1) << "Failed to put va surface to image";
223 if (!va_wrapper_->MapImage(va_image_.get(), &buffer)) {
224 DVLOG(1) << "Failed to map VAImage";
228 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_2D, texture_id_);
229 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
230 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
232 // See bug https://crosswalk-project.org/jira/browse/XWALK-2265.
233 // The following small piece of code is a workaround for the current VDA
234 // texture output implementation. It can be removed when zero buffer copy
236 unsigned int al = 4 * size_.width();
237 if (al != va_image_->pitches[0]) {
238 // Not aligned phenomenon occurs only in special size video in None-X11.
239 // So re-check RGBA data alignment and realign filled video frame in need.
240 unsigned char* bhandle = static_cast<unsigned char*>(buffer);
241 for (int i = 0; i < size_.height(); i++) {
242 memcpy(bhandle + (i * al), bhandle + (i * (va_image_->pitches[0])), al);
246 glTexImage2D(GL_TEXTURE_2D,
256 va_wrapper_->UnmapImage(va_image_.get());
261 bool VaapiVideoDecodeAccelerator::TFPPicture::Bind() {
262 DCHECK(CalledOnValidThread());
263 if (!make_context_current_.Run())
266 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_2D, texture_id_);
267 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
268 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
269 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, egl_image_);
273 EGLImageKHR VaapiVideoDecodeAccelerator::TFPPicture::CreateEGLImage(
274 EGLDisplay egl_display, VASurfaceID va_surface, VAImage* va_image) {
275 DCHECK(CalledOnValidThread());
278 VABufferInfo buffer_info;
279 if (!va_wrapper_->LockBuffer(va_surface, va_image->buf, &buffer_info)) {
280 DVLOG(1) << "Failed to lock Buffer";
281 return EGL_NO_IMAGE_KHR;
287 EGL_DRM_BUFFER_STRIDE_MESA, 0,
288 EGL_DRM_BUFFER_FORMAT_MESA,
289 EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
290 EGL_DRM_BUFFER_USE_MESA,
291 EGL_DRM_BUFFER_USE_SHARE_MESA,
293 attribs[1] = va_image->width;
294 attribs[3] = va_image->height;
295 attribs[5] = va_image->pitches[0] / 4;
297 EGLImageKHR egl_image = eglCreateImageKHR(
301 (EGLClientBuffer) buffer_info.handle,
305 va_wrapper_->UnlockBuffer(va_surface, va_image->buf, &buffer_info);
311 bool VaapiVideoDecodeAccelerator::TFPPicture::DestroyEGLImage(
312 EGLDisplay egl_display, EGLImageKHR egl_image) {
313 return eglDestroyImageKHR(egl_display, egl_image);
316 VaapiVideoDecodeAccelerator::TFPPicture*
317 VaapiVideoDecodeAccelerator::TFPPictureById(int32 picture_buffer_id) {
318 TFPPictures::iterator it = tfp_pictures_.find(picture_buffer_id);
319 if (it == tfp_pictures_.end()) {
320 DVLOG(1) << "Picture id " << picture_buffer_id << " does not exist";
324 return it->second.get();
327 VaapiVideoDecodeAccelerator::VaapiVideoDecodeAccelerator(
328 const base::Callback<bool(void)>& make_context_current) //NOLINT
329 : make_context_current_(make_context_current),
330 state_(kUninitialized),
331 input_ready_(&lock_),
332 surfaces_available_(&lock_),
333 message_loop_(base::MessageLoop::current()),
334 decoder_thread_("VaapiDecoderThread"),
335 num_frames_at_client_(0),
336 num_stream_bufs_at_decoder_(0),
337 finish_flush_pending_(false),
338 awaiting_va_surfaces_recycle_(false),
339 requested_num_pics_(0),
340 weak_this_factory_(this) {
341 weak_this_ = weak_this_factory_.GetWeakPtr();
342 va_surface_release_cb_ = media::BindToCurrentLoop(base::Bind(
343 &VaapiVideoDecodeAccelerator::RecycleVASurfaceID, weak_this_));
346 VaapiVideoDecodeAccelerator::~VaapiVideoDecodeAccelerator() {
347 DCHECK_EQ(message_loop_, base::MessageLoop::current());
350 bool VaapiVideoDecodeAccelerator::Initialize(media::VideoCodecProfile profile,
352 DCHECK_EQ(message_loop_, base::MessageLoop::current());
354 client_ptr_factory_.reset(new base::WeakPtrFactory<Client>(client));
355 client_ = client_ptr_factory_->GetWeakPtr();
357 base::AutoLock auto_lock(lock_);
358 DCHECK_EQ(state_, kUninitialized);
359 DVLOG(2) << "Initializing VAVDA, profile: " << profile;
361 if (!make_context_current_.Run())
364 vaapi_wrapper_ = VaapiWrapper::Create(
366 gfx::GLSurfaceEGL::GetNativeDisplay(),
367 base::Bind(&ReportToUMA, VaapiH264Decoder::VAAPI_ERROR));
369 if (!vaapi_wrapper_.get()) {
370 DVLOG(1) << "Failed initializing VAAPI";
374 supports_valockBuffer_apis_ = vaapi_wrapper_->SupportsVaLockBufferApis();
376 supports_valockBuffer_apis_ ? "supports" : "doesn't support";
377 LOG(INFO) << "VAAPI " << query << " vaLockBuffer apis";
380 new VaapiH264Decoder(
381 vaapi_wrapper_.get(),
382 media::BindToCurrentLoop(base::Bind(
383 &VaapiVideoDecodeAccelerator::SurfaceReady, weak_this_)),
384 base::Bind(&ReportToUMA)));
386 CHECK(decoder_thread_.Start());
387 decoder_thread_proxy_ = decoder_thread_.message_loop_proxy();
394 void VaapiVideoDecodeAccelerator::SurfaceReady(
396 const scoped_refptr<VASurface>& va_surface) {
397 DCHECK_EQ(message_loop_, base::MessageLoop::current());
398 DCHECK(!awaiting_va_surfaces_recycle_);
400 // Drop any requests to output if we are resetting or being destroyed.
401 if (state_ == kResetting || state_ == kDestroying)
404 pending_output_cbs_.push(
405 base::Bind(&VaapiVideoDecodeAccelerator::OutputPicture,
406 weak_this_, va_surface, input_id));
411 void VaapiVideoDecodeAccelerator::OutputPicture(
412 const scoped_refptr<VASurface>& va_surface,
414 TFPPicture* tfp_picture) {
415 DCHECK_EQ(message_loop_, base::MessageLoop::current());
417 int32 output_id = tfp_picture->picture_buffer_id();
419 TRACE_EVENT2("Video Decoder", "VAVDA::OutputSurface",
420 "input_id", input_id,
421 "output_id", output_id);
423 DVLOG(3) << "Outputting VASurface " << va_surface->id()
424 << " into texture bound to picture buffer id " << output_id;
426 if (supports_valockBuffer_apis_) {
427 RETURN_AND_NOTIFY_ON_FAILURE(
428 vaapi_wrapper_->PutSurfaceIntoImage(
430 tfp_picture->va_image()),
431 "Failed putting surface into vaimage",
432 PLATFORM_FAILURE, ); //NOLINT
434 RETURN_AND_NOTIFY_ON_FAILURE(
435 tfp_picture->UpdateEGLImage(va_surface->id()),
436 "Failed to update egl image per vaimage info",
437 PLATFORM_FAILURE, ); //NOLINT
439 RETURN_AND_NOTIFY_ON_FAILURE(
441 "Failed to bind egl image to texture",
442 PLATFORM_FAILURE, ); //NOLINT
444 RETURN_AND_NOTIFY_ON_FAILURE(
445 tfp_picture->Upload(va_surface->id()),
446 "Failed to upload VASurface to texture",
447 PLATFORM_FAILURE, ); //NOLINT
450 // Notify the client a picture is ready to be displayed.
451 ++num_frames_at_client_;
452 TRACE_COUNTER1("Video Decoder", "Textures at client", num_frames_at_client_);
453 DVLOG(4) << "Notifying output picture id " << output_id
454 << " for input "<< input_id << " is ready";
455 // TODO(posciak): Use visible size from decoder here instead
456 // (crbug.com/402760).
458 client_->PictureReady(media::Picture(output_id, input_id, gfx::Rect(tfp_picture->size())));
461 void VaapiVideoDecodeAccelerator::TryOutputSurface() {
462 DCHECK_EQ(message_loop_, base::MessageLoop::current());
464 // Handle Destroy() arriving while pictures are queued for output.
468 if (pending_output_cbs_.empty() || output_buffers_.empty())
471 OutputCB output_cb = pending_output_cbs_.front();
472 pending_output_cbs_.pop();
474 TFPPicture* tfp_picture = TFPPictureById(output_buffers_.front());
476 output_buffers_.pop();
478 output_cb.Run(tfp_picture);
480 if (finish_flush_pending_ && pending_output_cbs_.empty())
484 void VaapiVideoDecodeAccelerator::MapAndQueueNewInputBuffer(
485 const media::BitstreamBuffer& bitstream_buffer) {
486 DCHECK_EQ(message_loop_, base::MessageLoop::current());
487 TRACE_EVENT1("Video Decoder", "MapAndQueueNewInputBuffer", "input_id",
488 bitstream_buffer.id());
490 DVLOG(4) << "Mapping new input buffer id: " << bitstream_buffer.id()
491 << " size: " << static_cast<int>(bitstream_buffer.size());
493 scoped_ptr<base::SharedMemory> shm(
494 new base::SharedMemory(bitstream_buffer.handle(), true));
495 RETURN_AND_NOTIFY_ON_FAILURE(shm->Map(bitstream_buffer.size()),
496 "Failed to map input buffer", UNREADABLE_INPUT,); //NOLINT
498 base::AutoLock auto_lock(lock_);
500 // Set up a new input buffer and queue it for later.
501 linked_ptr<InputBuffer> input_buffer(new InputBuffer());
502 input_buffer->shm.reset(shm.release());
503 input_buffer->id = bitstream_buffer.id();
504 input_buffer->size = bitstream_buffer.size();
506 ++num_stream_bufs_at_decoder_;
507 TRACE_COUNTER1("Video Decoder", "Stream buffers at decoder",
508 num_stream_bufs_at_decoder_);
510 input_buffers_.push(input_buffer);
511 input_ready_.Signal();
514 bool VaapiVideoDecodeAccelerator::GetInputBuffer_Locked() {
515 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
516 lock_.AssertAcquired();
518 if (curr_input_buffer_.get())
521 // Will only wait if it is expected that in current state new buffers will
522 // be queued from the client via Decode(). The state can change during wait.
523 while (input_buffers_.empty() && (state_ == kDecoding || state_ == kIdle)) {
527 // We could have got woken up in a different state or never got to sleep
528 // due to current state; check for that.
531 // Here we are only interested in finishing up decoding buffers that are
532 // already queued up. Otherwise will stop decoding.
533 if (input_buffers_.empty())
538 DCHECK(!input_buffers_.empty());
540 curr_input_buffer_ = input_buffers_.front();
541 input_buffers_.pop();
543 DVLOG(4) << "New current bitstream buffer, id: "
544 << curr_input_buffer_->id
545 << " size: " << curr_input_buffer_->size;
548 static_cast<uint8*>(curr_input_buffer_->shm->memory()),
549 curr_input_buffer_->size, curr_input_buffer_->id);
553 // We got woken up due to being destroyed/reset, ignore any already
559 void VaapiVideoDecodeAccelerator::ReturnCurrInputBuffer_Locked() {
560 lock_.AssertAcquired();
561 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
562 DCHECK(curr_input_buffer_.get());
564 int32 id = curr_input_buffer_->id;
565 curr_input_buffer_.reset();
566 DVLOG(4) << "End of input buffer " << id;
567 message_loop_->PostTask(FROM_HERE, base::Bind(
568 &Client::NotifyEndOfBitstreamBuffer, client_, id));
570 --num_stream_bufs_at_decoder_;
571 TRACE_COUNTER1("Video Decoder", "Stream buffers at decoder",
572 num_stream_bufs_at_decoder_);
575 bool VaapiVideoDecodeAccelerator::FeedDecoderWithOutputSurfaces_Locked() {
576 lock_.AssertAcquired();
577 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
579 while (available_va_surfaces_.empty() &&
580 (state_ == kDecoding || state_ == kFlushing || state_ == kIdle)) {
581 surfaces_available_.Wait();
584 if (state_ != kDecoding && state_ != kFlushing && state_ != kIdle)
587 while (!available_va_surfaces_.empty()) {
588 scoped_refptr<VASurface> va_surface(
589 new VASurface(available_va_surfaces_.front(), va_surface_release_cb_));
590 available_va_surfaces_.pop_front();
591 decoder_->ReuseSurface(va_surface);
597 void VaapiVideoDecodeAccelerator::DecodeTask() {
598 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
599 TRACE_EVENT0("Video Decoder", "VAVDA::DecodeTask");
600 base::AutoLock auto_lock(lock_);
602 if (state_ != kDecoding)
606 DVLOG(4) << "Decode task";
608 // Try to decode what stream data is (still) in the decoder until we run out
610 while (GetInputBuffer_Locked()) {
611 DCHECK(curr_input_buffer_.get());
613 VaapiH264Decoder::DecResult res;
615 // We are OK releasing the lock here, as decoder never calls our methods
616 // directly and we will reacquire the lock before looking at state again.
617 // This is the main decode function of the decoder and while keeping
618 // the lock for its duration would be fine, it would defeat the purpose
619 // of having a separate decoder thread.
620 base::AutoUnlock auto_unlock(lock_);
621 res = decoder_->Decode();
625 case VaapiH264Decoder::kAllocateNewSurfaces:
626 DVLOG(1) << "Decoder requesting a new set of surfaces";
627 message_loop_->PostTask(FROM_HERE, base::Bind(
628 &VaapiVideoDecodeAccelerator::InitiateSurfaceSetChange, weak_this_,
629 decoder_->GetRequiredNumOfPictures(),
630 decoder_->GetPicSize()));
631 // We'll get rescheduled once ProvidePictureBuffers() finishes.
634 case VaapiH264Decoder::kRanOutOfStreamData:
635 ReturnCurrInputBuffer_Locked();
638 case VaapiH264Decoder::kRanOutOfSurfaces:
639 // No more output buffers in the decoder, try getting more or go to
640 // sleep waiting for them.
641 if (!FeedDecoderWithOutputSurfaces_Locked())
646 case VaapiH264Decoder::kDecodeError:
647 RETURN_AND_NOTIFY_ON_FAILURE(false, "Error decoding stream",
648 PLATFORM_FAILURE,); //NOLINT
654 void VaapiVideoDecodeAccelerator::InitiateSurfaceSetChange(size_t num_pics,
656 DCHECK_EQ(message_loop_, base::MessageLoop::current());
657 DCHECK(!awaiting_va_surfaces_recycle_);
659 // At this point decoder has stopped running and has already posted onto our
660 // loop any remaining output request callbacks, which executed before we got
661 // here. Some of them might have been pended though, because we might not
662 // have had enough TFPictures to output surfaces to. Initiate a wait cycle,
663 // which will wait for client to return enough PictureBuffers to us, so that
664 // we can finish all pending output callbacks, releasing associated surfaces.
665 DVLOG(1) << "Initiating surface set change";
666 awaiting_va_surfaces_recycle_ = true;
668 requested_num_pics_ = num_pics;
669 requested_pic_size_ = size;
671 TryFinishSurfaceSetChange();
674 void VaapiVideoDecodeAccelerator::TryFinishSurfaceSetChange() {
675 DCHECK_EQ(message_loop_, base::MessageLoop::current());
677 if (!awaiting_va_surfaces_recycle_)
680 if (!pending_output_cbs_.empty() ||
681 tfp_pictures_.size() != available_va_surfaces_.size()) {
683 // 1. Not all pending pending output callbacks have been executed yet.
684 // Wait for the client to return enough pictures and retry later.
685 // 2. The above happened and all surface release callbacks have been posted
686 // as the result, but not all have executed yet. Post ourselves after them
687 // to let them release surfaces.
688 DVLOG(2) << "Awaiting pending output/surface release callbacks to finish";
689 message_loop_->PostTask(FROM_HERE, base::Bind(
690 &VaapiVideoDecodeAccelerator::TryFinishSurfaceSetChange, weak_this_));
694 // All surfaces released, destroy them and dismiss all PictureBuffers.
695 awaiting_va_surfaces_recycle_ = false;
696 available_va_surfaces_.clear();
697 vaapi_wrapper_->DestroySurfaces();
699 for (TFPPictures::iterator iter = tfp_pictures_.begin();
700 iter != tfp_pictures_.end(); ++iter) {
701 DVLOG(2) << "Dismissing picture id: " << iter->first;
703 client_->DismissPictureBuffer(iter->first);
705 tfp_pictures_.clear();
707 // And ask for a new set as requested.
708 DVLOG(1) << "Requesting " << requested_num_pics_ << " pictures of size: "
709 << requested_pic_size_.ToString();
711 message_loop_->PostTask(FROM_HERE, base::Bind(
712 &Client::ProvidePictureBuffers, client_,
713 requested_num_pics_, requested_pic_size_, GL_TEXTURE_2D));
716 void VaapiVideoDecodeAccelerator::Decode(
717 const media::BitstreamBuffer& bitstream_buffer) {
718 DCHECK_EQ(message_loop_, base::MessageLoop::current());
720 TRACE_EVENT1("Video Decoder", "VAVDA::Decode", "Buffer id",
721 bitstream_buffer.id());
723 // We got a new input buffer from the client, map it and queue for later use.
724 MapAndQueueNewInputBuffer(bitstream_buffer);
726 base::AutoLock auto_lock(lock_);
730 decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
731 &VaapiVideoDecodeAccelerator::DecodeTask,
732 base::Unretained(this)));
736 // Decoder already running, fallthrough.
738 // When resetting, allow accumulating bitstream buffers, so that
739 // the client can queue after-seek-buffers while we are finishing with
740 // the before-seek one.
744 RETURN_AND_NOTIFY_ON_FAILURE(false,
745 "Decode request from client in invalid state: " << state_,
746 PLATFORM_FAILURE,); //NOLINT
751 void VaapiVideoDecodeAccelerator::RecycleVASurfaceID(
752 VASurfaceID va_surface_id) {
753 DCHECK_EQ(message_loop_, base::MessageLoop::current());
754 base::AutoLock auto_lock(lock_);
756 available_va_surfaces_.push_back(va_surface_id);
757 surfaces_available_.Signal();
760 void VaapiVideoDecodeAccelerator::AssignPictureBuffers(
761 const std::vector<media::PictureBuffer>& buffers) {
762 DCHECK_EQ(message_loop_, base::MessageLoop::current());
764 base::AutoLock auto_lock(lock_);
765 DCHECK(tfp_pictures_.empty());
767 while (!output_buffers_.empty())
768 output_buffers_.pop();
770 RETURN_AND_NOTIFY_ON_FAILURE(
771 buffers.size() == requested_num_pics_,
772 "Got an invalid buffers. (Got " << buffers.size() << ", requested "
773 << requested_num_pics_ << ")", INVALID_ARGUMENT,); //NOLINT
774 DCHECK(requested_pic_size_ == buffers[0].size());
776 std::vector<VASurfaceID> va_surface_ids;
777 RETURN_AND_NOTIFY_ON_FAILURE(
778 vaapi_wrapper_->CreateSurfaces(requested_pic_size_,
781 "Failed creating VA Surfaces", PLATFORM_FAILURE,); //NOLINT
782 DCHECK_EQ(va_surface_ids.size(), buffers.size());
784 for (size_t i = 0; i < buffers.size(); ++i) {
785 DVLOG(2) << "Assigning picture id: " << buffers[i].id()
786 << " to texture id: " << buffers[i].texture_id()
787 << " VASurfaceID: " << va_surface_ids[i];
789 linked_ptr<TFPPicture> tfp_picture(
790 TFPPicture::Create(make_context_current_,
791 vaapi_wrapper_.get(),
793 buffers[i].texture_id(),
794 requested_pic_size_));
796 RETURN_AND_NOTIFY_ON_FAILURE(
797 tfp_picture.get(), "Failed assigning picture buffer to a texture.",
798 PLATFORM_FAILURE,); //NOLINT
800 bool inserted = tfp_pictures_.insert(std::make_pair(
801 buffers[i].id(), tfp_picture)).second;
804 output_buffers_.push(buffers[i].id());
805 available_va_surfaces_.push_back(va_surface_ids[i]);
806 surfaces_available_.Signal();
810 decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
811 &VaapiVideoDecodeAccelerator::DecodeTask, base::Unretained(this)));
814 void VaapiVideoDecodeAccelerator::ReusePictureBuffer(int32 picture_buffer_id) {
815 DCHECK_EQ(message_loop_, base::MessageLoop::current());
816 TRACE_EVENT1("Video Decoder", "VAVDA::ReusePictureBuffer", "Picture id",
819 --num_frames_at_client_;
820 TRACE_COUNTER1("Video Decoder", "Textures at client", num_frames_at_client_);
822 output_buffers_.push(picture_buffer_id);
826 void VaapiVideoDecodeAccelerator::FlushTask() {
827 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
828 DVLOG(1) << "Flush task";
830 // First flush all the pictures that haven't been outputted, notifying the
831 // client to output them.
832 bool res = decoder_->Flush();
833 RETURN_AND_NOTIFY_ON_FAILURE(res, "Failed flushing the decoder.",
834 PLATFORM_FAILURE,); //NOLINT
836 // Put the decoder in idle state, ready to resume.
839 message_loop_->PostTask(FROM_HERE, base::Bind(
840 &VaapiVideoDecodeAccelerator::FinishFlush, weak_this_));
843 void VaapiVideoDecodeAccelerator::Flush() {
844 DCHECK_EQ(message_loop_, base::MessageLoop::current());
845 DVLOG(1) << "Got flush request";
847 base::AutoLock auto_lock(lock_);
849 // Queue a flush task after all existing decoding tasks to clean up.
850 decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
851 &VaapiVideoDecodeAccelerator::FlushTask, base::Unretained(this)));
853 input_ready_.Signal();
854 surfaces_available_.Signal();
857 void VaapiVideoDecodeAccelerator::FinishFlush() {
858 DCHECK_EQ(message_loop_, base::MessageLoop::current());
860 finish_flush_pending_ = false;
862 base::AutoLock auto_lock(lock_);
863 if (state_ != kFlushing) {
864 DCHECK_EQ(state_, kDestroying);
865 return; // We could've gotten destroyed already.
868 // Still waiting for textures from client to finish outputting all pending
869 // frames. Try again later.
870 if (!pending_output_cbs_.empty()) {
871 finish_flush_pending_ = true;
877 message_loop_->PostTask(FROM_HERE, base::Bind(
878 &Client::NotifyFlushDone, client_));
880 DVLOG(1) << "Flush finished";
883 void VaapiVideoDecodeAccelerator::ResetTask() {
884 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
885 DVLOG(1) << "ResetTask";
887 // All the decoding tasks from before the reset request from client are done
888 // by now, as this task was scheduled after them and client is expected not
889 // to call Decode() after Reset() and before NotifyResetDone.
892 base::AutoLock auto_lock(lock_);
894 // Return current input buffer, if present.
895 if (curr_input_buffer_.get())
896 ReturnCurrInputBuffer_Locked();
898 // And let client know that we are done with reset.
899 message_loop_->PostTask(FROM_HERE, base::Bind(
900 &VaapiVideoDecodeAccelerator::FinishReset, weak_this_));
903 void VaapiVideoDecodeAccelerator::Reset() {
904 DCHECK_EQ(message_loop_, base::MessageLoop::current());
905 DVLOG(1) << "Got reset request";
907 // This will make any new decode tasks exit early.
908 base::AutoLock auto_lock(lock_);
910 finish_flush_pending_ = false;
912 // Drop all remaining input buffers, if present.
913 while (!input_buffers_.empty()) {
914 message_loop_->PostTask(FROM_HERE, base::Bind(
915 &Client::NotifyEndOfBitstreamBuffer, client_,
916 input_buffers_.front()->id));
917 input_buffers_.pop();
920 decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
921 &VaapiVideoDecodeAccelerator::ResetTask, base::Unretained(this)));
923 input_ready_.Signal();
924 surfaces_available_.Signal();
927 void VaapiVideoDecodeAccelerator::FinishReset() {
928 DCHECK_EQ(message_loop_, base::MessageLoop::current());
929 DVLOG(1) << "FinishReset";
930 base::AutoLock auto_lock(lock_);
932 if (state_ != kResetting) {
933 DCHECK(state_ == kDestroying || state_ == kUninitialized) << state_;
934 return; // We could've gotten destroyed already.
937 // Drop pending outputs.
938 while (!pending_output_cbs_.empty())
939 pending_output_cbs_.pop();
941 if (awaiting_va_surfaces_recycle_) {
942 // Decoder requested a new surface set while we were waiting for it to
943 // finish the last DecodeTask, running at the time of Reset().
944 // Let the surface set change finish first before resetting.
945 message_loop_->PostTask(FROM_HERE, base::Bind(
946 &VaapiVideoDecodeAccelerator::FinishReset, weak_this_));
950 num_stream_bufs_at_decoder_ = 0;
953 message_loop_->PostTask(FROM_HERE, base::Bind(
954 &Client::NotifyResetDone, client_));
956 // The client might have given us new buffers via Decode() while we were
957 // resetting and might be waiting for our move, and not call Decode() anymore
958 // until we return something. Post a DecodeTask() so that we won't
959 // sleep forever waiting for Decode() in that case. Having two of them
960 // in the pipe is harmless, the additional one will return as soon as it sees
961 // that we are back in kDecoding state.
962 if (!input_buffers_.empty()) {
964 decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
965 &VaapiVideoDecodeAccelerator::DecodeTask,
966 base::Unretained(this)));
969 DVLOG(1) << "Reset finished";
972 void VaapiVideoDecodeAccelerator::Cleanup() {
973 DCHECK_EQ(message_loop_, base::MessageLoop::current());
975 if (state_ == kUninitialized || state_ == kDestroying)
978 DVLOG(1) << "Destroying VAVDA";
979 base::AutoLock auto_lock(lock_);
980 state_ = kDestroying;
982 client_ptr_factory_.reset();
983 weak_this_factory_.InvalidateWeakPtrs();
986 base::AutoUnlock auto_unlock(lock_);
987 // Post a dummy task to the decoder_thread_ to ensure it is drained.
988 base::WaitableEvent waiter(false, false);
989 decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
990 &base::WaitableEvent::Signal, base::Unretained(&waiter)));
991 input_ready_.Signal();
992 surfaces_available_.Signal();
994 decoder_thread_.Stop();
997 state_ = kUninitialized;
1000 void VaapiVideoDecodeAccelerator::Destroy() {
1001 DCHECK_EQ(message_loop_, base::MessageLoop::current());
1006 bool VaapiVideoDecodeAccelerator::CanDecodeOnIOThread() {
1010 } // namespace media