Upstream version 10.38.208.0
[platform/framework/web/crosswalk.git] / src / ozone / media / vaapi_video_decode_accelerator.cc
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.
4
5 #include "ozone/media/vaapi_video_decode_accelerator.h"
6
7 #include <string>
8
9 #include "base/bind.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"
23
24 static void ReportToUMA(
25     media::VaapiH264Decoder::VAVDAH264DecoderFailure failure) {
26   UMA_HISTOGRAM_ENUMERATION(
27       "Media.VAVDAH264.DecoderFailure",
28       failure,
29       media::VaapiH264Decoder::VAVDA_H264_DECODER_FAILURES_MAX);
30 }
31
32 namespace media {
33
34 #define RETURN_AND_NOTIFY_ON_FAILURE(result, log, error_code, ret)  \
35   do {                                                              \
36     if (!(result)) {                                                \
37       DVLOG(1) << log;                                              \
38       NotifyError(error_code);                                      \
39       return ret;                                                   \
40     }                                                               \
41   } while (0)
42
43 VaapiVideoDecodeAccelerator::InputBuffer::InputBuffer() : id(0), size(0) {
44 }
45
46 VaapiVideoDecodeAccelerator::InputBuffer::~InputBuffer() {
47 }
48
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));
54     return;
55   }
56
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_));
60
61   DVLOG(1) << "Notifying of error " << error;
62   if (client_) {
63     client_->NotifyError(error);
64     client_ptr_factory_.reset();
65   }
66 }
67
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).
72 //
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 {
76  public:
77   ~TFPPicture();
78
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,
83       uint32 texture_id,
84       gfx::Size size);
85
86   int32 picture_buffer_id() {
87     return picture_buffer_id_;
88   }
89
90   gfx::Size size() {
91     return size_;
92   }
93
94   VAImage* va_image() {
95     return va_image_.get();
96   }
97
98   // Upload vaimage data to texture. Needs to be called every frame.
99   bool Upload(VASurfaceID id);
100
101   // Bind EGL image to texture. Needs to be called every frame.
102   bool Bind();
103   bool UpdateEGLImage(VASurfaceID id);
104
105  private:
106   TFPPicture(const base::Callback<bool(void)>& make_context_current, //NOLINT
107              VaapiWrapper* va_wrapper,
108              int32 picture_buffer_id,
109              uint32 texture_id,
110              gfx::Size size);
111
112   bool Initialize();
113
114   EGLImageKHR CreateEGLImage(
115       EGLDisplay egl_display, VASurfaceID surface, VAImage* va_image);
116   bool DestroyEGLImage(EGLDisplay egl_display, EGLImageKHR egl_image);
117
118   base::Callback<bool(void)> make_context_current_; //NOLINT
119
120   VaapiWrapper* va_wrapper_;
121
122   // Output id for the client.
123   int32 picture_buffer_id_;
124   uint32 texture_id_;
125
126   gfx::Size size_;
127   scoped_ptr<VAImage> va_image_;
128   EGLImageKHR egl_image_;
129   EGLDisplay egl_display_;
130
131   DISALLOW_COPY_AND_ASSIGN(TFPPicture);
132 };
133
134 VaapiVideoDecodeAccelerator::TFPPicture::TFPPicture(
135     const base::Callback<bool(void)>& make_context_current, //NOLINT
136     VaapiWrapper* va_wrapper,
137     int32 picture_buffer_id,
138     uint32 texture_id,
139     gfx::Size size)
140     : make_context_current_(make_context_current),
141       va_wrapper_(va_wrapper),
142       picture_buffer_id_(picture_buffer_id),
143       texture_id_(texture_id),
144       size_(size),
145       va_image_(new VAImage()),
146       egl_display_(gfx::GLSurfaceEGL::GetHardwareDisplay()) {
147   DCHECK(!make_context_current_.is_null());
148   DCHECK(va_image_);
149 }
150
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,
156     uint32 texture_id,
157     gfx::Size size) {
158   linked_ptr<TFPPicture> tfp_picture(
159       new TFPPicture(make_context_current, va_wrapper,
160                      picture_buffer_id, texture_id, size));
161
162   if (!tfp_picture->Initialize())
163     tfp_picture.reset();
164
165   return tfp_picture;
166 }
167
168 bool VaapiVideoDecodeAccelerator::TFPPicture::Initialize() {
169   DCHECK(CalledOnValidThread());
170   if (!make_context_current_.Run())
171     return false;
172
173   if (!va_wrapper_->CreateRGBImage(size_, va_image_.get())) {
174     DVLOG(1) << "Failed to create VAImage";
175     return false;
176   }
177
178   return true;
179 }
180
181 VaapiVideoDecodeAccelerator::TFPPicture::~TFPPicture() {
182   DCHECK(CalledOnValidThread());
183
184   if (egl_image_ != EGL_NO_IMAGE_KHR)
185     DestroyEGLImage(egl_display_, egl_image_);
186
187   if (va_wrapper_) {
188     va_wrapper_->DestroyImage(va_image_.get());
189   }
190 }
191
192 bool VaapiVideoDecodeAccelerator::TFPPicture::UpdateEGLImage(
193     VASurfaceID surface) {
194   DCHECK(CalledOnValidThread());
195
196   if (!make_context_current_.Run())
197     return false;
198
199   if (egl_image_ != EGL_NO_IMAGE_KHR)
200     DestroyEGLImage(egl_display_, egl_image_);
201
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";
205     return false;
206   }
207
208   return true;
209 }
210
211 bool VaapiVideoDecodeAccelerator::TFPPicture::Upload(VASurfaceID surface) {
212   DCHECK(CalledOnValidThread());
213
214   if (!make_context_current_.Run())
215     return false;
216
217   if (!va_wrapper_->PutSurfaceIntoImage(surface, va_image_.get())) {
218     DVLOG(1) << "Failed to put va surface to image";
219     return false;
220   }
221
222   void* buffer = NULL;
223   if (!va_wrapper_->MapImage(va_image_.get(), &buffer)) {
224     DVLOG(1) << "Failed to map VAImage";
225     return false;
226   }
227
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);
231
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
235   // is implemented.
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);
243     }
244   }
245
246   glTexImage2D(GL_TEXTURE_2D,
247                0,
248                GL_BGRA,
249                size_.width(),
250                size_.height(),
251                0,
252                GL_BGRA,
253                GL_UNSIGNED_BYTE,
254                buffer);
255
256   va_wrapper_->UnmapImage(va_image_.get());
257
258   return true;
259 }
260
261 bool VaapiVideoDecodeAccelerator::TFPPicture::Bind() {
262   DCHECK(CalledOnValidThread());
263   if (!make_context_current_.Run())
264     return false;
265
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_);
270   return true;
271 }
272
273 EGLImageKHR VaapiVideoDecodeAccelerator::TFPPicture::CreateEGLImage(
274     EGLDisplay egl_display, VASurfaceID va_surface, VAImage* va_image) {
275   DCHECK(CalledOnValidThread());
276   DCHECK(va_image);
277
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;
282   }
283
284   EGLint attribs[] = {
285       EGL_WIDTH, 0,
286       EGL_HEIGHT, 0,
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,
292       EGL_NONE };
293   attribs[1] = va_image->width;
294   attribs[3] = va_image->height;
295   attribs[5] = va_image->pitches[0] / 4;
296
297   EGLImageKHR egl_image =  eglCreateImageKHR(
298        egl_display,
299        EGL_NO_CONTEXT,
300        EGL_DRM_BUFFER_MESA,
301        (EGLClientBuffer) buffer_info.handle,
302        attribs);
303
304   if (va_wrapper_) {
305     va_wrapper_->UnlockBuffer(va_surface, va_image->buf, &buffer_info);
306   }
307
308   return egl_image;
309 }
310
311 bool VaapiVideoDecodeAccelerator::TFPPicture::DestroyEGLImage(
312     EGLDisplay egl_display, EGLImageKHR egl_image) {
313   return eglDestroyImageKHR(egl_display, egl_image);
314 }
315
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";
321     return NULL;
322   }
323
324   return it->second.get();
325 }
326
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_));
344 }
345
346 VaapiVideoDecodeAccelerator::~VaapiVideoDecodeAccelerator() {
347   DCHECK_EQ(message_loop_, base::MessageLoop::current());
348 }
349
350 bool VaapiVideoDecodeAccelerator::Initialize(media::VideoCodecProfile profile,
351                                              Client* client) {
352   DCHECK_EQ(message_loop_, base::MessageLoop::current());
353
354   client_ptr_factory_.reset(new base::WeakPtrFactory<Client>(client));
355   client_ = client_ptr_factory_->GetWeakPtr();
356
357   base::AutoLock auto_lock(lock_);
358   DCHECK_EQ(state_, kUninitialized);
359   DVLOG(2) << "Initializing VAVDA, profile: " << profile;
360
361   if (!make_context_current_.Run())
362     return false;
363
364   vaapi_wrapper_ = VaapiWrapper::Create(
365       profile,
366       gfx::GLSurfaceEGL::GetNativeDisplay(),
367       base::Bind(&ReportToUMA, VaapiH264Decoder::VAAPI_ERROR));
368
369   if (!vaapi_wrapper_.get()) {
370     DVLOG(1) << "Failed initializing VAAPI";
371     return false;
372   }
373
374   supports_valockBuffer_apis_ = vaapi_wrapper_->SupportsVaLockBufferApis();
375   std::string query =
376       supports_valockBuffer_apis_ ? "supports" : "doesn't support";
377   LOG(INFO) << "VAAPI " << query << " vaLockBuffer apis";
378
379   decoder_.reset(
380       new VaapiH264Decoder(
381           vaapi_wrapper_.get(),
382           media::BindToCurrentLoop(base::Bind(
383               &VaapiVideoDecodeAccelerator::SurfaceReady, weak_this_)),
384           base::Bind(&ReportToUMA)));
385
386   CHECK(decoder_thread_.Start());
387   decoder_thread_proxy_ = decoder_thread_.message_loop_proxy();
388
389   state_ = kIdle;
390
391   return true;
392 }
393
394 void VaapiVideoDecodeAccelerator::SurfaceReady(
395     int32 input_id,
396     const scoped_refptr<VASurface>& va_surface) {
397   DCHECK_EQ(message_loop_, base::MessageLoop::current());
398   DCHECK(!awaiting_va_surfaces_recycle_);
399
400   // Drop any requests to output if we are resetting or being destroyed.
401   if (state_ == kResetting || state_ == kDestroying)
402     return;
403
404   pending_output_cbs_.push(
405       base::Bind(&VaapiVideoDecodeAccelerator::OutputPicture,
406                  weak_this_, va_surface, input_id));
407
408   TryOutputSurface();
409 }
410
411 void VaapiVideoDecodeAccelerator::OutputPicture(
412     const scoped_refptr<VASurface>& va_surface,
413     int32 input_id,
414     TFPPicture* tfp_picture) {
415   DCHECK_EQ(message_loop_, base::MessageLoop::current());
416
417   int32 output_id  = tfp_picture->picture_buffer_id();
418
419   TRACE_EVENT2("Video Decoder", "VAVDA::OutputSurface",
420                "input_id", input_id,
421                "output_id", output_id);
422
423   DVLOG(3) << "Outputting VASurface " << va_surface->id()
424            << " into texture bound to picture buffer id " << output_id;
425
426   if (supports_valockBuffer_apis_) {
427     RETURN_AND_NOTIFY_ON_FAILURE(
428         vaapi_wrapper_->PutSurfaceIntoImage(
429             va_surface->id(),
430             tfp_picture->va_image()),
431         "Failed putting surface into vaimage",
432         PLATFORM_FAILURE, );  //NOLINT
433
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
438
439     RETURN_AND_NOTIFY_ON_FAILURE(
440         tfp_picture->Bind(),
441         "Failed to bind egl image to texture",
442         PLATFORM_FAILURE, ); //NOLINT
443   } else {
444     RETURN_AND_NOTIFY_ON_FAILURE(
445         tfp_picture->Upload(va_surface->id()),
446         "Failed to upload VASurface to texture",
447         PLATFORM_FAILURE, ); //NOLINT
448   }
449
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).
457   if (client_)
458     client_->PictureReady(media::Picture(output_id, input_id, gfx::Rect(tfp_picture->size())));
459 }
460
461 void VaapiVideoDecodeAccelerator::TryOutputSurface() {
462   DCHECK_EQ(message_loop_, base::MessageLoop::current());
463
464   // Handle Destroy() arriving while pictures are queued for output.
465   if (!client_)
466     return;
467
468   if (pending_output_cbs_.empty() || output_buffers_.empty())
469     return;
470
471   OutputCB output_cb = pending_output_cbs_.front();
472   pending_output_cbs_.pop();
473
474   TFPPicture* tfp_picture = TFPPictureById(output_buffers_.front());
475   DCHECK(tfp_picture);
476   output_buffers_.pop();
477
478   output_cb.Run(tfp_picture);
479
480   if (finish_flush_pending_ && pending_output_cbs_.empty())
481     FinishFlush();
482 }
483
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());
489
490   DVLOG(4) << "Mapping new input buffer id: " << bitstream_buffer.id()
491            << " size: " << static_cast<int>(bitstream_buffer.size());
492
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
497
498   base::AutoLock auto_lock(lock_);
499
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();
505
506   ++num_stream_bufs_at_decoder_;
507   TRACE_COUNTER1("Video Decoder", "Stream buffers at decoder",
508                  num_stream_bufs_at_decoder_);
509
510   input_buffers_.push(input_buffer);
511   input_ready_.Signal();
512 }
513
514 bool VaapiVideoDecodeAccelerator::GetInputBuffer_Locked() {
515   DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
516   lock_.AssertAcquired();
517
518   if (curr_input_buffer_.get())
519     return true;
520
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)) {
524     input_ready_.Wait();
525   }
526
527   // We could have got woken up in a different state or never got to sleep
528   // due to current state; check for that.
529   switch (state_) {
530     case kFlushing:
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())
534         return false;
535       // else fallthrough
536     case kDecoding:
537     case kIdle:
538       DCHECK(!input_buffers_.empty());
539
540       curr_input_buffer_ = input_buffers_.front();
541       input_buffers_.pop();
542
543       DVLOG(4) << "New current bitstream buffer, id: "
544                << curr_input_buffer_->id
545                << " size: " << curr_input_buffer_->size;
546
547       decoder_->SetStream(
548           static_cast<uint8*>(curr_input_buffer_->shm->memory()),
549           curr_input_buffer_->size, curr_input_buffer_->id);
550       return true;
551
552     default:
553       // We got woken up due to being destroyed/reset, ignore any already
554       // queued inputs.
555       return false;
556   }
557 }
558
559 void VaapiVideoDecodeAccelerator::ReturnCurrInputBuffer_Locked() {
560   lock_.AssertAcquired();
561   DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
562   DCHECK(curr_input_buffer_.get());
563
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));
569
570   --num_stream_bufs_at_decoder_;
571   TRACE_COUNTER1("Video Decoder", "Stream buffers at decoder",
572                  num_stream_bufs_at_decoder_);
573 }
574
575 bool VaapiVideoDecodeAccelerator::FeedDecoderWithOutputSurfaces_Locked() {
576   lock_.AssertAcquired();
577   DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
578
579   while (available_va_surfaces_.empty() &&
580         (state_ == kDecoding || state_ == kFlushing || state_ == kIdle)) {
581     surfaces_available_.Wait();
582   }
583
584   if (state_ != kDecoding && state_ != kFlushing && state_ != kIdle)
585     return false;
586
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);
592   }
593
594   return true;
595 }
596
597 void VaapiVideoDecodeAccelerator::DecodeTask() {
598   DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
599   TRACE_EVENT0("Video Decoder", "VAVDA::DecodeTask");
600   base::AutoLock auto_lock(lock_);
601
602   if (state_ != kDecoding)
603     return;
604
605   // Main decode task.
606   DVLOG(4) << "Decode task";
607
608   // Try to decode what stream data is (still) in the decoder until we run out
609   // of it.
610   while (GetInputBuffer_Locked()) {
611     DCHECK(curr_input_buffer_.get());
612
613     VaapiH264Decoder::DecResult res;
614     {
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();
622     }
623
624     switch (res) {
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.
632         return;
633
634       case VaapiH264Decoder::kRanOutOfStreamData:
635         ReturnCurrInputBuffer_Locked();
636         break;
637
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())
642           return;
643
644         break;
645
646       case VaapiH264Decoder::kDecodeError:
647         RETURN_AND_NOTIFY_ON_FAILURE(false, "Error decoding stream",
648                                      PLATFORM_FAILURE,); //NOLINT
649         return;
650     }
651   }
652 }
653
654 void VaapiVideoDecodeAccelerator::InitiateSurfaceSetChange(size_t num_pics,
655                                                            gfx::Size size) {
656   DCHECK_EQ(message_loop_, base::MessageLoop::current());
657   DCHECK(!awaiting_va_surfaces_recycle_);
658
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;
667
668   requested_num_pics_ = num_pics;
669   requested_pic_size_ = size;
670
671   TryFinishSurfaceSetChange();
672 }
673
674 void VaapiVideoDecodeAccelerator::TryFinishSurfaceSetChange() {
675   DCHECK_EQ(message_loop_, base::MessageLoop::current());
676
677   if (!awaiting_va_surfaces_recycle_)
678     return;
679
680   if (!pending_output_cbs_.empty() ||
681       tfp_pictures_.size() != available_va_surfaces_.size()) {
682     // Either:
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_));
691     return;
692   }
693
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();
698
699   for (TFPPictures::iterator iter = tfp_pictures_.begin();
700        iter != tfp_pictures_.end(); ++iter) {
701     DVLOG(2) << "Dismissing picture id: " << iter->first;
702     if (client_)
703       client_->DismissPictureBuffer(iter->first);
704   }
705   tfp_pictures_.clear();
706
707   // And ask for a new set as requested.
708   DVLOG(1) << "Requesting " << requested_num_pics_ << " pictures of size: "
709            << requested_pic_size_.ToString();
710
711   message_loop_->PostTask(FROM_HERE, base::Bind(
712       &Client::ProvidePictureBuffers, client_,
713       requested_num_pics_, requested_pic_size_, GL_TEXTURE_2D));
714 }
715
716 void VaapiVideoDecodeAccelerator::Decode(
717     const media::BitstreamBuffer& bitstream_buffer) {
718   DCHECK_EQ(message_loop_, base::MessageLoop::current());
719
720   TRACE_EVENT1("Video Decoder", "VAVDA::Decode", "Buffer id",
721                bitstream_buffer.id());
722
723   // We got a new input buffer from the client, map it and queue for later use.
724   MapAndQueueNewInputBuffer(bitstream_buffer);
725
726   base::AutoLock auto_lock(lock_);
727   switch (state_) {
728     case kIdle:
729       state_ = kDecoding;
730       decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
731           &VaapiVideoDecodeAccelerator::DecodeTask,
732           base::Unretained(this)));
733       break;
734
735     case kDecoding:
736       // Decoder already running, fallthrough.
737     case kResetting:
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.
741       break;
742
743     default:
744       RETURN_AND_NOTIFY_ON_FAILURE(false,
745           "Decode request from client in invalid state: " << state_,
746           PLATFORM_FAILURE,); //NOLINT
747       break;
748   }
749 }
750
751 void VaapiVideoDecodeAccelerator::RecycleVASurfaceID(
752     VASurfaceID va_surface_id) {
753   DCHECK_EQ(message_loop_, base::MessageLoop::current());
754   base::AutoLock auto_lock(lock_);
755
756   available_va_surfaces_.push_back(va_surface_id);
757   surfaces_available_.Signal();
758 }
759
760 void VaapiVideoDecodeAccelerator::AssignPictureBuffers(
761     const std::vector<media::PictureBuffer>& buffers) {
762   DCHECK_EQ(message_loop_, base::MessageLoop::current());
763
764   base::AutoLock auto_lock(lock_);
765   DCHECK(tfp_pictures_.empty());
766
767   while (!output_buffers_.empty())
768     output_buffers_.pop();
769
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());
775
776   std::vector<VASurfaceID> va_surface_ids;
777   RETURN_AND_NOTIFY_ON_FAILURE(
778       vaapi_wrapper_->CreateSurfaces(requested_pic_size_,
779                                      buffers.size(),
780                                      &va_surface_ids),
781       "Failed creating VA Surfaces", PLATFORM_FAILURE,); //NOLINT
782   DCHECK_EQ(va_surface_ids.size(), buffers.size());
783
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];
788
789     linked_ptr<TFPPicture> tfp_picture(
790         TFPPicture::Create(make_context_current_,
791                            vaapi_wrapper_.get(),
792                            buffers[i].id(),
793                            buffers[i].texture_id(),
794                            requested_pic_size_));
795
796     RETURN_AND_NOTIFY_ON_FAILURE(
797         tfp_picture.get(), "Failed assigning picture buffer to a texture.",
798         PLATFORM_FAILURE,); //NOLINT
799
800     bool inserted = tfp_pictures_.insert(std::make_pair(
801         buffers[i].id(), tfp_picture)).second;
802     DCHECK(inserted);
803
804     output_buffers_.push(buffers[i].id());
805     available_va_surfaces_.push_back(va_surface_ids[i]);
806     surfaces_available_.Signal();
807   }
808
809   state_ = kDecoding;
810   decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
811       &VaapiVideoDecodeAccelerator::DecodeTask, base::Unretained(this)));
812 }
813
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",
817                picture_buffer_id);
818
819   --num_frames_at_client_;
820   TRACE_COUNTER1("Video Decoder", "Textures at client", num_frames_at_client_);
821
822   output_buffers_.push(picture_buffer_id);
823   TryOutputSurface();
824 }
825
826 void VaapiVideoDecodeAccelerator::FlushTask() {
827   DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
828   DVLOG(1) << "Flush task";
829
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
835
836   // Put the decoder in idle state, ready to resume.
837   decoder_->Reset();
838
839   message_loop_->PostTask(FROM_HERE, base::Bind(
840       &VaapiVideoDecodeAccelerator::FinishFlush, weak_this_));
841 }
842
843 void VaapiVideoDecodeAccelerator::Flush() {
844   DCHECK_EQ(message_loop_, base::MessageLoop::current());
845   DVLOG(1) << "Got flush request";
846
847   base::AutoLock auto_lock(lock_);
848   state_ = kFlushing;
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)));
852
853   input_ready_.Signal();
854   surfaces_available_.Signal();
855 }
856
857 void VaapiVideoDecodeAccelerator::FinishFlush() {
858   DCHECK_EQ(message_loop_, base::MessageLoop::current());
859
860   finish_flush_pending_ = false;
861
862   base::AutoLock auto_lock(lock_);
863   if (state_ != kFlushing) {
864     DCHECK_EQ(state_, kDestroying);
865     return;  // We could've gotten destroyed already.
866   }
867
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;
872     return;
873   }
874
875   state_ = kIdle;
876
877   message_loop_->PostTask(FROM_HERE, base::Bind(
878       &Client::NotifyFlushDone, client_));
879
880   DVLOG(1) << "Flush finished";
881 }
882
883 void VaapiVideoDecodeAccelerator::ResetTask() {
884   DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
885   DVLOG(1) << "ResetTask";
886
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.
890   decoder_->Reset();
891
892   base::AutoLock auto_lock(lock_);
893
894   // Return current input buffer, if present.
895   if (curr_input_buffer_.get())
896     ReturnCurrInputBuffer_Locked();
897
898   // And let client know that we are done with reset.
899   message_loop_->PostTask(FROM_HERE, base::Bind(
900       &VaapiVideoDecodeAccelerator::FinishReset, weak_this_));
901 }
902
903 void VaapiVideoDecodeAccelerator::Reset() {
904   DCHECK_EQ(message_loop_, base::MessageLoop::current());
905   DVLOG(1) << "Got reset request";
906
907   // This will make any new decode tasks exit early.
908   base::AutoLock auto_lock(lock_);
909   state_ = kResetting;
910   finish_flush_pending_ = false;
911
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();
918   }
919
920   decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
921       &VaapiVideoDecodeAccelerator::ResetTask, base::Unretained(this)));
922
923   input_ready_.Signal();
924   surfaces_available_.Signal();
925 }
926
927 void VaapiVideoDecodeAccelerator::FinishReset() {
928   DCHECK_EQ(message_loop_, base::MessageLoop::current());
929   DVLOG(1) << "FinishReset";
930   base::AutoLock auto_lock(lock_);
931
932   if (state_ != kResetting) {
933     DCHECK(state_ == kDestroying || state_ == kUninitialized) << state_;
934     return;  // We could've gotten destroyed already.
935   }
936
937   // Drop pending outputs.
938   while (!pending_output_cbs_.empty())
939     pending_output_cbs_.pop();
940
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_));
947     return;
948   }
949
950   num_stream_bufs_at_decoder_ = 0;
951   state_ = kIdle;
952
953   message_loop_->PostTask(FROM_HERE, base::Bind(
954       &Client::NotifyResetDone, client_));
955
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()) {
963     state_ = kDecoding;
964     decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
965       &VaapiVideoDecodeAccelerator::DecodeTask,
966       base::Unretained(this)));
967   }
968
969   DVLOG(1) << "Reset finished";
970 }
971
972 void VaapiVideoDecodeAccelerator::Cleanup() {
973   DCHECK_EQ(message_loop_, base::MessageLoop::current());
974
975   if (state_ == kUninitialized || state_ == kDestroying)
976     return;
977
978   DVLOG(1) << "Destroying VAVDA";
979   base::AutoLock auto_lock(lock_);
980   state_ = kDestroying;
981
982   client_ptr_factory_.reset();
983   weak_this_factory_.InvalidateWeakPtrs();
984
985   {
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();
993     waiter.Wait();
994     decoder_thread_.Stop();
995   }
996
997   state_ = kUninitialized;
998 }
999
1000 void VaapiVideoDecodeAccelerator::Destroy() {
1001   DCHECK_EQ(message_loop_, base::MessageLoop::current());
1002   Cleanup();
1003   delete this;
1004 }
1005
1006 bool VaapiVideoDecodeAccelerator::CanDecodeOnIOThread() {
1007   return false;
1008 }
1009
1010 }  // namespace media