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.
6 #include "base/debug/trace_event.h"
7 #include "base/logging.h"
8 #include "base/metrics/histogram.h"
9 #include "base/stl_util.h"
10 #include "base/strings/string_util.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "content/common/gpu/gpu_channel.h"
14 #include "content/common/gpu/media/vaapi_video_decode_accelerator.h"
15 #include "media/base/bind_to_current_loop.h"
16 #include "media/video/picture.h"
17 #include "ui/gl/scoped_binders.h"
19 static void ReportToUMA(
20 content::VaapiH264Decoder::VAVDAH264DecoderFailure failure) {
21 UMA_HISTOGRAM_ENUMERATION(
22 "Media.VAVDAH264.DecoderFailure",
24 content::VaapiH264Decoder::VAVDA_H264_DECODER_FAILURES_MAX);
29 #define RETURN_AND_NOTIFY_ON_FAILURE(result, log, error_code, ret) \
33 NotifyError(error_code); \
38 VaapiVideoDecodeAccelerator::InputBuffer::InputBuffer() : id(0), size(0) {
41 VaapiVideoDecodeAccelerator::InputBuffer::~InputBuffer() {
44 void VaapiVideoDecodeAccelerator::NotifyError(Error error) {
45 if (message_loop_ != base::MessageLoop::current()) {
46 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
47 message_loop_->PostTask(FROM_HERE, base::Bind(
48 &VaapiVideoDecodeAccelerator::NotifyError, weak_this_, error));
52 // Post Cleanup() as a task so we don't recursively acquire lock_.
53 message_loop_->PostTask(FROM_HERE, base::Bind(
54 &VaapiVideoDecodeAccelerator::Cleanup, weak_this_));
56 DVLOG(1) << "Notifying of error " << error;
58 client_->NotifyError(error);
59 client_ptr_factory_.reset();
63 // TFPPicture allocates VAImage and binds them to textures passed
64 // in PictureBuffers from clients to them. TFPPictures are created as
65 // a consequence of receiving a set of PictureBuffers from clients and released
66 // at the end of decode (or when a new set of PictureBuffers is required).
68 // TFPPictures are used for output, contents of VASurfaces passed from decoder
69 // are put into the associated vaimage memory and upload to client.
70 class VaapiVideoDecodeAccelerator::TFPPicture : public base::NonThreadSafe {
74 static linked_ptr<TFPPicture> Create(
75 const base::Callback<bool(void)>& make_context_current, //NOLINT
76 wl_display* wl_display,
77 VaapiWrapper* va_wrapper,
78 int32 picture_buffer_id,
82 int32 picture_buffer_id() {
83 return picture_buffer_id_;
90 // Upload vaimage data to texture. Needs to be called every frame.
91 bool Upload(VASurfaceID id);
94 TFPPicture(const base::Callback<bool(void)>& make_context_current, //NOLINT
95 wl_display* wl_display,
96 VaapiWrapper* va_wrapper,
97 int32 picture_buffer_id,
103 base::Callback<bool(void)> make_context_current_; //NOLINT
105 wl_display* wl_display_;
106 VaapiWrapper* va_wrapper_;
108 // Output id for the client.
109 int32 picture_buffer_id_;
115 DISALLOW_COPY_AND_ASSIGN(TFPPicture);
118 VaapiVideoDecodeAccelerator::TFPPicture::TFPPicture(
119 const base::Callback<bool(void)>& make_context_current, //NOLINT
120 wl_display* wl_display,
121 VaapiWrapper* va_wrapper,
122 int32 picture_buffer_id,
125 : make_context_current_(make_context_current),
126 wl_display_(wl_display),
127 va_wrapper_(va_wrapper),
128 picture_buffer_id_(picture_buffer_id),
129 texture_id_(texture_id),
131 DCHECK(!make_context_current_.is_null());
134 linked_ptr<VaapiVideoDecodeAccelerator::TFPPicture>
135 VaapiVideoDecodeAccelerator::TFPPicture::Create(
136 const base::Callback<bool(void)>& make_context_current, //NOLINT
137 wl_display* wl_display,
138 VaapiWrapper* va_wrapper,
139 int32 picture_buffer_id,
142 linked_ptr<TFPPicture> tfp_picture(
143 new TFPPicture(make_context_current, wl_display, va_wrapper,
144 picture_buffer_id, texture_id, size));
146 if (!tfp_picture->Initialize())
152 bool VaapiVideoDecodeAccelerator::TFPPicture::Initialize() {
153 DCHECK(CalledOnValidThread());
154 if (!make_context_current_.Run())
157 if (!va_wrapper_->CreateRGBImage(size_, &va_image_)) {
158 DVLOG(1) << "Failed to create VAImage";
165 VaapiVideoDecodeAccelerator::TFPPicture::~TFPPicture() {
166 DCHECK(CalledOnValidThread());
169 va_wrapper_->DestroyImage(&va_image_);
173 bool VaapiVideoDecodeAccelerator::TFPPicture::Upload(VASurfaceID surface) {
174 DCHECK(CalledOnValidThread());
176 if (!make_context_current_.Run())
179 if (!va_wrapper_->PutSurfaceIntoImage(surface, &va_image_)) {
180 DVLOG(1) << "Failed to put va surface to image";
185 if (!va_wrapper_->MapImage(&va_image_, &buffer)) {
186 DVLOG(1) << "Failed to map VAImage";
190 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_2D, texture_id_);
191 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
192 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
193 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size_.width(), size_.height(),
194 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
196 va_wrapper_->UnmapImage(&va_image_);
201 VaapiVideoDecodeAccelerator::TFPPicture*
202 VaapiVideoDecodeAccelerator::TFPPictureById(int32 picture_buffer_id) {
203 TFPPictures::iterator it = tfp_pictures_.find(picture_buffer_id);
204 if (it == tfp_pictures_.end()) {
205 DVLOG(1) << "Picture id " << picture_buffer_id << " does not exist";
209 return it->second.get();
212 VaapiVideoDecodeAccelerator::VaapiVideoDecodeAccelerator(
213 const base::Callback<bool(void)>& make_context_current) //NOLINT
215 make_context_current_(make_context_current),
216 state_(kUninitialized),
217 input_ready_(&lock_),
218 surfaces_available_(&lock_),
219 message_loop_(base::MessageLoop::current()),
220 decoder_thread_("VaapiDecoderThread"),
221 num_frames_at_client_(0),
222 num_stream_bufs_at_decoder_(0),
223 finish_flush_pending_(false),
224 awaiting_va_surfaces_recycle_(false),
225 requested_num_pics_(0),
226 weak_this_factory_(this) {
227 weak_this_ = weak_this_factory_.GetWeakPtr();
228 va_surface_release_cb_ = media::BindToCurrentLoop(base::Bind(
229 &VaapiVideoDecodeAccelerator::RecycleVASurfaceID, weak_this_));
232 VaapiVideoDecodeAccelerator::~VaapiVideoDecodeAccelerator() {
233 DCHECK_EQ(message_loop_, base::MessageLoop::current());
235 wl_display_flush(wl_display_);
236 wl_display_disconnect(wl_display_);
241 typedef struct wayland_display {
243 wl_compositor* compositor;
245 wl_registry* registry;
249 static void registry_handle_global(
251 wl_registry* registry,
253 const char* interface,
255 wayland_display* display_handle =
256 reinterpret_cast<wayland_display *>(data);
258 if (strcmp(interface, "wl_compositor") == 0) {
259 display_handle->compositor =
260 reinterpret_cast<wl_compositor *>(wl_registry_bind(
261 registry, id, &wl_compositor_interface, 1));
262 } else if (strcmp(interface, "wl_shell") == 0) {
263 display_handle->shell =
264 reinterpret_cast<wl_shell *>(wl_registry_bind(
265 registry, id, &wl_shell_interface, 1));
269 static const struct wl_registry_listener registry_listener = {
270 registry_handle_global,
274 bool VaapiVideoDecodeAccelerator::Initialize(media::VideoCodecProfile profile,
276 DCHECK_EQ(message_loop_, base::MessageLoop::current());
278 client_ptr_factory_.reset(new base::WeakPtrFactory<Client>(client));
279 client_ = client_ptr_factory_->GetWeakPtr();
281 base::AutoLock auto_lock(lock_);
282 DCHECK_EQ(state_, kUninitialized);
283 DVLOG(2) << "Initializing VAVDA, profile: " << profile;
285 if (!make_context_current_.Run())
290 d.display = wl_display_connect(NULL);
293 wl_display_set_user_data(d.display, &d);
294 d.registry = wl_display_get_registry(d.display);
295 wl_registry_add_listener(d.registry, ®istry_listener, &d);
296 wl_display_dispatch(d.display);
297 wl_display_ = d.display;
299 vaapi_wrapper_ = VaapiWrapper::Create(
300 profile, wl_display_,
301 base::Bind(&ReportToUMA, content::VaapiH264Decoder::VAAPI_ERROR));
303 if (!vaapi_wrapper_.get()) {
304 DVLOG(1) << "Failed initializing VAAPI";
309 new VaapiH264Decoder(
310 vaapi_wrapper_.get(),
311 media::BindToCurrentLoop(base::Bind(
312 &VaapiVideoDecodeAccelerator::SurfaceReady, weak_this_)),
313 base::Bind(&ReportToUMA)));
315 CHECK(decoder_thread_.Start());
316 decoder_thread_proxy_ = decoder_thread_.message_loop_proxy();
323 void VaapiVideoDecodeAccelerator::SurfaceReady(
325 const scoped_refptr<VASurface>& va_surface) {
326 DCHECK_EQ(message_loop_, base::MessageLoop::current());
327 DCHECK(!awaiting_va_surfaces_recycle_);
329 // Drop any requests to output if we are resetting or being destroyed.
330 if (state_ == kResetting || state_ == kDestroying)
333 pending_output_cbs_.push(
334 base::Bind(&VaapiVideoDecodeAccelerator::OutputPicture,
335 weak_this_, va_surface, input_id));
340 void VaapiVideoDecodeAccelerator::OutputPicture(
341 const scoped_refptr<VASurface>& va_surface,
343 TFPPicture* tfp_picture) {
344 DCHECK_EQ(message_loop_, base::MessageLoop::current());
346 int32 output_id = tfp_picture->picture_buffer_id();
348 TRACE_EVENT2("Video Decoder", "VAVDA::OutputSurface",
349 "input_id", input_id,
350 "output_id", output_id);
352 DVLOG(3) << "Outputting VASurface " << va_surface->id()
353 << " into pixmap bound to picture buffer id " << output_id;
355 RETURN_AND_NOTIFY_ON_FAILURE(tfp_picture->Upload(va_surface->id()),
356 "Failed to upload VASurface to texture",
357 PLATFORM_FAILURE, ); //NOLINT
359 // Notify the client a picture is ready to be displayed.
360 ++num_frames_at_client_;
361 TRACE_COUNTER1("Video Decoder", "Textures at client", num_frames_at_client_);
362 DVLOG(4) << "Notifying output picture id " << output_id
363 << " for input "<< input_id << " is ready";
365 client_->PictureReady(media::Picture(output_id, input_id));
368 void VaapiVideoDecodeAccelerator::TryOutputSurface() {
369 DCHECK_EQ(message_loop_, base::MessageLoop::current());
371 // Handle Destroy() arriving while pictures are queued for output.
375 if (pending_output_cbs_.empty() || output_buffers_.empty())
378 OutputCB output_cb = pending_output_cbs_.front();
379 pending_output_cbs_.pop();
381 TFPPicture* tfp_picture = TFPPictureById(output_buffers_.front());
383 output_buffers_.pop();
385 output_cb.Run(tfp_picture);
387 if (finish_flush_pending_ && pending_output_cbs_.empty())
391 void VaapiVideoDecodeAccelerator::MapAndQueueNewInputBuffer(
392 const media::BitstreamBuffer& bitstream_buffer) {
393 DCHECK_EQ(message_loop_, base::MessageLoop::current());
394 TRACE_EVENT1("Video Decoder", "MapAndQueueNewInputBuffer", "input_id",
395 bitstream_buffer.id());
397 DVLOG(4) << "Mapping new input buffer id: " << bitstream_buffer.id()
398 << " size: " << static_cast<int>(bitstream_buffer.size());
400 scoped_ptr<base::SharedMemory> shm(
401 new base::SharedMemory(bitstream_buffer.handle(), true));
402 RETURN_AND_NOTIFY_ON_FAILURE(shm->Map(bitstream_buffer.size()),
403 "Failed to map input buffer", UNREADABLE_INPUT,); //NOLINT
405 base::AutoLock auto_lock(lock_);
407 // Set up a new input buffer and queue it for later.
408 linked_ptr<InputBuffer> input_buffer(new InputBuffer());
409 input_buffer->shm.reset(shm.release());
410 input_buffer->id = bitstream_buffer.id();
411 input_buffer->size = bitstream_buffer.size();
413 ++num_stream_bufs_at_decoder_;
414 TRACE_COUNTER1("Video Decoder", "Stream buffers at decoder",
415 num_stream_bufs_at_decoder_);
417 input_buffers_.push(input_buffer);
418 input_ready_.Signal();
421 bool VaapiVideoDecodeAccelerator::GetInputBuffer_Locked() {
422 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
423 lock_.AssertAcquired();
425 if (curr_input_buffer_.get())
428 // Will only wait if it is expected that in current state new buffers will
429 // be queued from the client via Decode(). The state can change during wait.
430 while (input_buffers_.empty() && (state_ == kDecoding || state_ == kIdle)) {
434 // We could have got woken up in a different state or never got to sleep
435 // due to current state; check for that.
438 // Here we are only interested in finishing up decoding buffers that are
439 // already queued up. Otherwise will stop decoding.
440 if (input_buffers_.empty())
445 DCHECK(!input_buffers_.empty());
447 curr_input_buffer_ = input_buffers_.front();
448 input_buffers_.pop();
450 DVLOG(4) << "New current bitstream buffer, id: "
451 << curr_input_buffer_->id
452 << " size: " << curr_input_buffer_->size;
455 static_cast<uint8*>(curr_input_buffer_->shm->memory()),
456 curr_input_buffer_->size, curr_input_buffer_->id);
460 // We got woken up due to being destroyed/reset, ignore any already
466 void VaapiVideoDecodeAccelerator::ReturnCurrInputBuffer_Locked() {
467 lock_.AssertAcquired();
468 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
469 DCHECK(curr_input_buffer_.get());
471 int32 id = curr_input_buffer_->id;
472 curr_input_buffer_.reset();
473 DVLOG(4) << "End of input buffer " << id;
474 message_loop_->PostTask(FROM_HERE, base::Bind(
475 &Client::NotifyEndOfBitstreamBuffer, client_, id));
477 --num_stream_bufs_at_decoder_;
478 TRACE_COUNTER1("Video Decoder", "Stream buffers at decoder",
479 num_stream_bufs_at_decoder_);
482 bool VaapiVideoDecodeAccelerator::FeedDecoderWithOutputSurfaces_Locked() {
483 lock_.AssertAcquired();
484 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
486 while (available_va_surfaces_.empty() &&
487 (state_ == kDecoding || state_ == kFlushing || state_ == kIdle)) {
488 surfaces_available_.Wait();
491 if (state_ != kDecoding && state_ != kFlushing && state_ != kIdle)
494 while (!available_va_surfaces_.empty()) {
495 scoped_refptr<VASurface> va_surface(
496 new VASurface(available_va_surfaces_.front(), va_surface_release_cb_));
497 available_va_surfaces_.pop_front();
498 decoder_->ReuseSurface(va_surface);
504 void VaapiVideoDecodeAccelerator::DecodeTask() {
505 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
506 TRACE_EVENT0("Video Decoder", "VAVDA::DecodeTask");
507 base::AutoLock auto_lock(lock_);
509 if (state_ != kDecoding)
513 DVLOG(4) << "Decode task";
515 // Try to decode what stream data is (still) in the decoder until we run out
517 while (GetInputBuffer_Locked()) {
518 DCHECK(curr_input_buffer_.get());
520 VaapiH264Decoder::DecResult res;
522 // We are OK releasing the lock here, as decoder never calls our methods
523 // directly and we will reacquire the lock before looking at state again.
524 // This is the main decode function of the decoder and while keeping
525 // the lock for its duration would be fine, it would defeat the purpose
526 // of having a separate decoder thread.
527 base::AutoUnlock auto_unlock(lock_);
528 res = decoder_->Decode();
532 case VaapiH264Decoder::kAllocateNewSurfaces:
533 DVLOG(1) << "Decoder requesting a new set of surfaces";
534 message_loop_->PostTask(FROM_HERE, base::Bind(
535 &VaapiVideoDecodeAccelerator::InitiateSurfaceSetChange, weak_this_,
536 decoder_->GetRequiredNumOfPictures(),
537 decoder_->GetPicSize()));
538 // We'll get rescheduled once ProvidePictureBuffers() finishes.
541 case VaapiH264Decoder::kRanOutOfStreamData:
542 ReturnCurrInputBuffer_Locked();
545 case VaapiH264Decoder::kRanOutOfSurfaces:
546 // No more output buffers in the decoder, try getting more or go to
547 // sleep waiting for them.
548 if (!FeedDecoderWithOutputSurfaces_Locked())
553 case VaapiH264Decoder::kDecodeError:
554 RETURN_AND_NOTIFY_ON_FAILURE(false, "Error decoding stream",
555 PLATFORM_FAILURE,); //NOLINT
561 void VaapiVideoDecodeAccelerator::InitiateSurfaceSetChange(size_t num_pics,
563 DCHECK_EQ(message_loop_, base::MessageLoop::current());
564 DCHECK(!awaiting_va_surfaces_recycle_);
566 // At this point decoder has stopped running and has already posted onto our
567 // loop any remaining output request callbacks, which executed before we got
568 // here. Some of them might have been pended though, because we might not
569 // have had enough TFPictures to output surfaces to. Initiate a wait cycle,
570 // which will wait for client to return enough PictureBuffers to us, so that
571 // we can finish all pending output callbacks, releasing associated surfaces.
572 DVLOG(1) << "Initiating surface set change";
573 awaiting_va_surfaces_recycle_ = true;
575 requested_num_pics_ = num_pics;
576 requested_pic_size_ = size;
578 TryFinishSurfaceSetChange();
581 void VaapiVideoDecodeAccelerator::TryFinishSurfaceSetChange() {
582 DCHECK_EQ(message_loop_, base::MessageLoop::current());
584 if (!awaiting_va_surfaces_recycle_)
587 if (!pending_output_cbs_.empty() ||
588 tfp_pictures_.size() != available_va_surfaces_.size()) {
590 // 1. Not all pending pending output callbacks have been executed yet.
591 // Wait for the client to return enough pictures and retry later.
592 // 2. The above happened and all surface release callbacks have been posted
593 // as the result, but not all have executed yet. Post ourselves after them
594 // to let them release surfaces.
595 DVLOG(2) << "Awaiting pending output/surface release callbacks to finish";
596 message_loop_->PostTask(FROM_HERE, base::Bind(
597 &VaapiVideoDecodeAccelerator::TryFinishSurfaceSetChange, weak_this_));
601 // All surfaces released, destroy them and dismiss all PictureBuffers.
602 awaiting_va_surfaces_recycle_ = false;
603 available_va_surfaces_.clear();
604 vaapi_wrapper_->DestroySurfaces();
606 for (TFPPictures::iterator iter = tfp_pictures_.begin();
607 iter != tfp_pictures_.end(); ++iter) {
608 DVLOG(2) << "Dismissing picture id: " << iter->first;
610 client_->DismissPictureBuffer(iter->first);
612 tfp_pictures_.clear();
614 // And ask for a new set as requested.
615 DVLOG(1) << "Requesting " << requested_num_pics_ << " pictures of size: "
616 << requested_pic_size_.ToString();
618 message_loop_->PostTask(FROM_HERE, base::Bind(
619 &Client::ProvidePictureBuffers, client_,
620 requested_num_pics_, requested_pic_size_, GL_TEXTURE_2D));
623 void VaapiVideoDecodeAccelerator::Decode(
624 const media::BitstreamBuffer& bitstream_buffer) {
625 DCHECK_EQ(message_loop_, base::MessageLoop::current());
627 TRACE_EVENT1("Video Decoder", "VAVDA::Decode", "Buffer id",
628 bitstream_buffer.id());
630 // We got a new input buffer from the client, map it and queue for later use.
631 MapAndQueueNewInputBuffer(bitstream_buffer);
633 base::AutoLock auto_lock(lock_);
637 decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
638 &VaapiVideoDecodeAccelerator::DecodeTask,
639 base::Unretained(this)));
643 // Decoder already running, fallthrough.
645 // When resetting, allow accumulating bitstream buffers, so that
646 // the client can queue after-seek-buffers while we are finishing with
647 // the before-seek one.
651 RETURN_AND_NOTIFY_ON_FAILURE(false,
652 "Decode request from client in invalid state: " << state_,
653 PLATFORM_FAILURE,); //NOLINT
658 void VaapiVideoDecodeAccelerator::RecycleVASurfaceID(
659 VASurfaceID va_surface_id) {
660 DCHECK_EQ(message_loop_, base::MessageLoop::current());
661 base::AutoLock auto_lock(lock_);
663 available_va_surfaces_.push_back(va_surface_id);
664 surfaces_available_.Signal();
667 void VaapiVideoDecodeAccelerator::AssignPictureBuffers(
668 const std::vector<media::PictureBuffer>& buffers) {
669 DCHECK_EQ(message_loop_, base::MessageLoop::current());
671 base::AutoLock auto_lock(lock_);
672 DCHECK(tfp_pictures_.empty());
674 while (!output_buffers_.empty())
675 output_buffers_.pop();
677 RETURN_AND_NOTIFY_ON_FAILURE(
678 buffers.size() == requested_num_pics_,
679 "Got an invalid buffers. (Got " << buffers.size() << ", requested "
680 << requested_num_pics_ << ")", INVALID_ARGUMENT,); //NOLINT
681 DCHECK(requested_pic_size_ == buffers[0].size());
683 std::vector<VASurfaceID> va_surface_ids;
684 RETURN_AND_NOTIFY_ON_FAILURE(
685 vaapi_wrapper_->CreateSurfaces(requested_pic_size_,
688 "Failed creating VA Surfaces", PLATFORM_FAILURE,); //NOLINT
689 DCHECK_EQ(va_surface_ids.size(), buffers.size());
691 for (size_t i = 0; i < buffers.size(); ++i) {
692 DVLOG(2) << "Assigning picture id: " << buffers[i].id()
693 << " to texture id: " << buffers[i].texture_id()
694 << " VASurfaceID: " << va_surface_ids[i];
696 linked_ptr<TFPPicture> tfp_picture(
697 TFPPicture::Create(make_context_current_, wl_display_,
698 vaapi_wrapper_.get(), buffers[i].id(),
699 buffers[i].texture_id(), requested_pic_size_));
701 RETURN_AND_NOTIFY_ON_FAILURE(
702 tfp_picture.get(), "Failed assigning picture buffer to a texture.",
703 PLATFORM_FAILURE,); //NOLINT
705 bool inserted = tfp_pictures_.insert(std::make_pair(
706 buffers[i].id(), tfp_picture)).second;
709 output_buffers_.push(buffers[i].id());
710 available_va_surfaces_.push_back(va_surface_ids[i]);
711 surfaces_available_.Signal();
715 decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
716 &VaapiVideoDecodeAccelerator::DecodeTask, base::Unretained(this)));
719 void VaapiVideoDecodeAccelerator::ReusePictureBuffer(int32 picture_buffer_id) {
720 DCHECK_EQ(message_loop_, base::MessageLoop::current());
721 TRACE_EVENT1("Video Decoder", "VAVDA::ReusePictureBuffer", "Picture id",
724 --num_frames_at_client_;
725 TRACE_COUNTER1("Video Decoder", "Textures at client", num_frames_at_client_);
727 output_buffers_.push(picture_buffer_id);
731 void VaapiVideoDecodeAccelerator::FlushTask() {
732 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
733 DVLOG(1) << "Flush task";
735 // First flush all the pictures that haven't been outputted, notifying the
736 // client to output them.
737 bool res = decoder_->Flush();
738 RETURN_AND_NOTIFY_ON_FAILURE(res, "Failed flushing the decoder.",
739 PLATFORM_FAILURE,); //NOLINT
741 // Put the decoder in idle state, ready to resume.
744 message_loop_->PostTask(FROM_HERE, base::Bind(
745 &VaapiVideoDecodeAccelerator::FinishFlush, weak_this_));
748 void VaapiVideoDecodeAccelerator::Flush() {
749 DCHECK_EQ(message_loop_, base::MessageLoop::current());
750 DVLOG(1) << "Got flush request";
752 base::AutoLock auto_lock(lock_);
754 // Queue a flush task after all existing decoding tasks to clean up.
755 decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
756 &VaapiVideoDecodeAccelerator::FlushTask, base::Unretained(this)));
758 input_ready_.Signal();
759 surfaces_available_.Signal();
762 void VaapiVideoDecodeAccelerator::FinishFlush() {
763 DCHECK_EQ(message_loop_, base::MessageLoop::current());
765 finish_flush_pending_ = false;
767 base::AutoLock auto_lock(lock_);
768 if (state_ != kFlushing) {
769 DCHECK_EQ(state_, kDestroying);
770 return; // We could've gotten destroyed already.
773 // Still waiting for textures from client to finish outputting all pending
774 // frames. Try again later.
775 if (!pending_output_cbs_.empty()) {
776 finish_flush_pending_ = true;
782 message_loop_->PostTask(FROM_HERE, base::Bind(
783 &Client::NotifyFlushDone, client_));
785 DVLOG(1) << "Flush finished";
788 void VaapiVideoDecodeAccelerator::ResetTask() {
789 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
790 DVLOG(1) << "ResetTask";
792 // All the decoding tasks from before the reset request from client are done
793 // by now, as this task was scheduled after them and client is expected not
794 // to call Decode() after Reset() and before NotifyResetDone.
797 base::AutoLock auto_lock(lock_);
799 // Return current input buffer, if present.
800 if (curr_input_buffer_.get())
801 ReturnCurrInputBuffer_Locked();
803 // And let client know that we are done with reset.
804 message_loop_->PostTask(FROM_HERE, base::Bind(
805 &VaapiVideoDecodeAccelerator::FinishReset, weak_this_));
808 void VaapiVideoDecodeAccelerator::Reset() {
809 DCHECK_EQ(message_loop_, base::MessageLoop::current());
810 DVLOG(1) << "Got reset request";
812 // This will make any new decode tasks exit early.
813 base::AutoLock auto_lock(lock_);
815 finish_flush_pending_ = false;
817 // Drop all remaining input buffers, if present.
818 while (!input_buffers_.empty()) {
819 message_loop_->PostTask(FROM_HERE, base::Bind(
820 &Client::NotifyEndOfBitstreamBuffer, client_,
821 input_buffers_.front()->id));
822 input_buffers_.pop();
825 decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
826 &VaapiVideoDecodeAccelerator::ResetTask, base::Unretained(this)));
828 input_ready_.Signal();
829 surfaces_available_.Signal();
832 void VaapiVideoDecodeAccelerator::FinishReset() {
833 DCHECK_EQ(message_loop_, base::MessageLoop::current());
834 DVLOG(1) << "FinishReset";
835 base::AutoLock auto_lock(lock_);
837 if (state_ != kResetting) {
838 DCHECK(state_ == kDestroying || state_ == kUninitialized) << state_;
839 return; // We could've gotten destroyed already.
842 // Drop pending outputs.
843 while (!pending_output_cbs_.empty())
844 pending_output_cbs_.pop();
846 if (awaiting_va_surfaces_recycle_) {
847 // Decoder requested a new surface set while we were waiting for it to
848 // finish the last DecodeTask, running at the time of Reset().
849 // Let the surface set change finish first before resetting.
850 message_loop_->PostTask(FROM_HERE, base::Bind(
851 &VaapiVideoDecodeAccelerator::FinishReset, weak_this_));
855 num_stream_bufs_at_decoder_ = 0;
858 message_loop_->PostTask(FROM_HERE, base::Bind(
859 &Client::NotifyResetDone, client_));
861 // The client might have given us new buffers via Decode() while we were
862 // resetting and might be waiting for our move, and not call Decode() anymore
863 // until we return something. Post a DecodeTask() so that we won't
864 // sleep forever waiting for Decode() in that case. Having two of them
865 // in the pipe is harmless, the additional one will return as soon as it sees
866 // that we are back in kDecoding state.
867 if (!input_buffers_.empty()) {
869 decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
870 &VaapiVideoDecodeAccelerator::DecodeTask,
871 base::Unretained(this)));
874 DVLOG(1) << "Reset finished";
877 void VaapiVideoDecodeAccelerator::Cleanup() {
878 DCHECK_EQ(message_loop_, base::MessageLoop::current());
880 if (state_ == kUninitialized || state_ == kDestroying)
883 DVLOG(1) << "Destroying VAVDA";
884 base::AutoLock auto_lock(lock_);
885 state_ = kDestroying;
887 client_ptr_factory_.reset();
888 weak_this_factory_.InvalidateWeakPtrs();
891 base::AutoUnlock auto_unlock(lock_);
892 // Post a dummy task to the decoder_thread_ to ensure it is drained.
893 base::WaitableEvent waiter(false, false);
894 decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
895 &base::WaitableEvent::Signal, base::Unretained(&waiter)));
896 input_ready_.Signal();
897 surfaces_available_.Signal();
899 decoder_thread_.Stop();
902 state_ = kUninitialized;
905 void VaapiVideoDecodeAccelerator::Destroy() {
906 DCHECK_EQ(message_loop_, base::MessageLoop::current());
911 } // namespace content