Upstream version 10.39.225.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(
459         media::Picture(output_id, input_id, gfx::Rect(tfp_picture->size())));
460 }
461
462 void VaapiVideoDecodeAccelerator::TryOutputSurface() {
463   DCHECK_EQ(message_loop_, base::MessageLoop::current());
464
465   // Handle Destroy() arriving while pictures are queued for output.
466   if (!client_)
467     return;
468
469   if (pending_output_cbs_.empty() || output_buffers_.empty())
470     return;
471
472   OutputCB output_cb = pending_output_cbs_.front();
473   pending_output_cbs_.pop();
474
475   TFPPicture* tfp_picture = TFPPictureById(output_buffers_.front());
476   DCHECK(tfp_picture);
477   output_buffers_.pop();
478
479   output_cb.Run(tfp_picture);
480
481   if (finish_flush_pending_ && pending_output_cbs_.empty())
482     FinishFlush();
483 }
484
485 void VaapiVideoDecodeAccelerator::MapAndQueueNewInputBuffer(
486     const media::BitstreamBuffer& bitstream_buffer) {
487   DCHECK_EQ(message_loop_, base::MessageLoop::current());
488   TRACE_EVENT1("Video Decoder", "MapAndQueueNewInputBuffer", "input_id",
489       bitstream_buffer.id());
490
491   DVLOG(4) << "Mapping new input buffer id: " << bitstream_buffer.id()
492            << " size: " << static_cast<int>(bitstream_buffer.size());
493
494   scoped_ptr<base::SharedMemory> shm(
495       new base::SharedMemory(bitstream_buffer.handle(), true));
496   RETURN_AND_NOTIFY_ON_FAILURE(shm->Map(bitstream_buffer.size()),
497       "Failed to map input buffer", UNREADABLE_INPUT,); //NOLINT
498
499   base::AutoLock auto_lock(lock_);
500
501   // Set up a new input buffer and queue it for later.
502   linked_ptr<InputBuffer> input_buffer(new InputBuffer());
503   input_buffer->shm.reset(shm.release());
504   input_buffer->id = bitstream_buffer.id();
505   input_buffer->size = bitstream_buffer.size();
506
507   ++num_stream_bufs_at_decoder_;
508   TRACE_COUNTER1("Video Decoder", "Stream buffers at decoder",
509                  num_stream_bufs_at_decoder_);
510
511   input_buffers_.push(input_buffer);
512   input_ready_.Signal();
513 }
514
515 bool VaapiVideoDecodeAccelerator::GetInputBuffer_Locked() {
516   DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
517   lock_.AssertAcquired();
518
519   if (curr_input_buffer_.get())
520     return true;
521
522   // Will only wait if it is expected that in current state new buffers will
523   // be queued from the client via Decode(). The state can change during wait.
524   while (input_buffers_.empty() && (state_ == kDecoding || state_ == kIdle)) {
525     input_ready_.Wait();
526   }
527
528   // We could have got woken up in a different state or never got to sleep
529   // due to current state; check for that.
530   switch (state_) {
531     case kFlushing:
532       // Here we are only interested in finishing up decoding buffers that are
533       // already queued up. Otherwise will stop decoding.
534       if (input_buffers_.empty())
535         return false;
536       // else fallthrough
537     case kDecoding:
538     case kIdle:
539       DCHECK(!input_buffers_.empty());
540
541       curr_input_buffer_ = input_buffers_.front();
542       input_buffers_.pop();
543
544       DVLOG(4) << "New current bitstream buffer, id: "
545                << curr_input_buffer_->id
546                << " size: " << curr_input_buffer_->size;
547
548       decoder_->SetStream(
549           static_cast<uint8*>(curr_input_buffer_->shm->memory()),
550           curr_input_buffer_->size, curr_input_buffer_->id);
551       return true;
552
553     default:
554       // We got woken up due to being destroyed/reset, ignore any already
555       // queued inputs.
556       return false;
557   }
558 }
559
560 void VaapiVideoDecodeAccelerator::ReturnCurrInputBuffer_Locked() {
561   lock_.AssertAcquired();
562   DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
563   DCHECK(curr_input_buffer_.get());
564
565   int32 id = curr_input_buffer_->id;
566   curr_input_buffer_.reset();
567   DVLOG(4) << "End of input buffer " << id;
568   message_loop_->PostTask(FROM_HERE, base::Bind(
569       &Client::NotifyEndOfBitstreamBuffer, client_, id));
570
571   --num_stream_bufs_at_decoder_;
572   TRACE_COUNTER1("Video Decoder", "Stream buffers at decoder",
573                  num_stream_bufs_at_decoder_);
574 }
575
576 bool VaapiVideoDecodeAccelerator::FeedDecoderWithOutputSurfaces_Locked() {
577   lock_.AssertAcquired();
578   DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
579
580   while (available_va_surfaces_.empty() &&
581         (state_ == kDecoding || state_ == kFlushing || state_ == kIdle)) {
582     surfaces_available_.Wait();
583   }
584
585   if (state_ != kDecoding && state_ != kFlushing && state_ != kIdle)
586     return false;
587
588   while (!available_va_surfaces_.empty()) {
589     scoped_refptr<VASurface> va_surface(
590         new VASurface(available_va_surfaces_.front(), va_surface_release_cb_));
591     available_va_surfaces_.pop_front();
592     decoder_->ReuseSurface(va_surface);
593   }
594
595   return true;
596 }
597
598 void VaapiVideoDecodeAccelerator::DecodeTask() {
599   DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
600   TRACE_EVENT0("Video Decoder", "VAVDA::DecodeTask");
601   base::AutoLock auto_lock(lock_);
602
603   if (state_ != kDecoding)
604     return;
605
606   // Main decode task.
607   DVLOG(4) << "Decode task";
608
609   // Try to decode what stream data is (still) in the decoder until we run out
610   // of it.
611   while (GetInputBuffer_Locked()) {
612     DCHECK(curr_input_buffer_.get());
613
614     VaapiH264Decoder::DecResult res;
615     {
616       // We are OK releasing the lock here, as decoder never calls our methods
617       // directly and we will reacquire the lock before looking at state again.
618       // This is the main decode function of the decoder and while keeping
619       // the lock for its duration would be fine, it would defeat the purpose
620       // of having a separate decoder thread.
621       base::AutoUnlock auto_unlock(lock_);
622       res = decoder_->Decode();
623     }
624
625     switch (res) {
626       case VaapiH264Decoder::kAllocateNewSurfaces:
627         DVLOG(1) << "Decoder requesting a new set of surfaces";
628         message_loop_->PostTask(FROM_HERE, base::Bind(
629             &VaapiVideoDecodeAccelerator::InitiateSurfaceSetChange, weak_this_,
630                 decoder_->GetRequiredNumOfPictures(),
631                 decoder_->GetPicSize()));
632         // We'll get rescheduled once ProvidePictureBuffers() finishes.
633         return;
634
635       case VaapiH264Decoder::kRanOutOfStreamData:
636         ReturnCurrInputBuffer_Locked();
637         break;
638
639       case VaapiH264Decoder::kRanOutOfSurfaces:
640         // No more output buffers in the decoder, try getting more or go to
641         // sleep waiting for them.
642         if (!FeedDecoderWithOutputSurfaces_Locked())
643           return;
644
645         break;
646
647       case VaapiH264Decoder::kDecodeError:
648         RETURN_AND_NOTIFY_ON_FAILURE(false, "Error decoding stream",
649                                      PLATFORM_FAILURE,); //NOLINT
650         return;
651     }
652   }
653 }
654
655 void VaapiVideoDecodeAccelerator::InitiateSurfaceSetChange(size_t num_pics,
656                                                            gfx::Size size) {
657   DCHECK_EQ(message_loop_, base::MessageLoop::current());
658   DCHECK(!awaiting_va_surfaces_recycle_);
659
660   // At this point decoder has stopped running and has already posted onto our
661   // loop any remaining output request callbacks, which executed before we got
662   // here. Some of them might have been pended though, because we might not
663   // have had enough TFPictures to output surfaces to. Initiate a wait cycle,
664   // which will wait for client to return enough PictureBuffers to us, so that
665   // we can finish all pending output callbacks, releasing associated surfaces.
666   DVLOG(1) << "Initiating surface set change";
667   awaiting_va_surfaces_recycle_ = true;
668
669   requested_num_pics_ = num_pics;
670   requested_pic_size_ = size;
671
672   TryFinishSurfaceSetChange();
673 }
674
675 void VaapiVideoDecodeAccelerator::TryFinishSurfaceSetChange() {
676   DCHECK_EQ(message_loop_, base::MessageLoop::current());
677
678   if (!awaiting_va_surfaces_recycle_)
679     return;
680
681   if (!pending_output_cbs_.empty() ||
682       tfp_pictures_.size() != available_va_surfaces_.size()) {
683     // Either:
684     // 1. Not all pending pending output callbacks have been executed yet.
685     // Wait for the client to return enough pictures and retry later.
686     // 2. The above happened and all surface release callbacks have been posted
687     // as the result, but not all have executed yet. Post ourselves after them
688     // to let them release surfaces.
689     DVLOG(2) << "Awaiting pending output/surface release callbacks to finish";
690     message_loop_->PostTask(FROM_HERE, base::Bind(
691         &VaapiVideoDecodeAccelerator::TryFinishSurfaceSetChange, weak_this_));
692     return;
693   }
694
695   // All surfaces released, destroy them and dismiss all PictureBuffers.
696   awaiting_va_surfaces_recycle_ = false;
697   available_va_surfaces_.clear();
698   vaapi_wrapper_->DestroySurfaces();
699
700   for (TFPPictures::iterator iter = tfp_pictures_.begin();
701        iter != tfp_pictures_.end(); ++iter) {
702     DVLOG(2) << "Dismissing picture id: " << iter->first;
703     if (client_)
704       client_->DismissPictureBuffer(iter->first);
705   }
706   tfp_pictures_.clear();
707
708   // And ask for a new set as requested.
709   DVLOG(1) << "Requesting " << requested_num_pics_ << " pictures of size: "
710            << requested_pic_size_.ToString();
711
712   message_loop_->PostTask(FROM_HERE, base::Bind(
713       &Client::ProvidePictureBuffers, client_,
714       requested_num_pics_, requested_pic_size_, GL_TEXTURE_2D));
715 }
716
717 void VaapiVideoDecodeAccelerator::Decode(
718     const media::BitstreamBuffer& bitstream_buffer) {
719   DCHECK_EQ(message_loop_, base::MessageLoop::current());
720
721   TRACE_EVENT1("Video Decoder", "VAVDA::Decode", "Buffer id",
722                bitstream_buffer.id());
723
724   // We got a new input buffer from the client, map it and queue for later use.
725   MapAndQueueNewInputBuffer(bitstream_buffer);
726
727   base::AutoLock auto_lock(lock_);
728   switch (state_) {
729     case kIdle:
730       state_ = kDecoding;
731       decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
732           &VaapiVideoDecodeAccelerator::DecodeTask,
733           base::Unretained(this)));
734       break;
735
736     case kDecoding:
737       // Decoder already running, fallthrough.
738     case kResetting:
739       // When resetting, allow accumulating bitstream buffers, so that
740       // the client can queue after-seek-buffers while we are finishing with
741       // the before-seek one.
742       break;
743
744     default:
745       RETURN_AND_NOTIFY_ON_FAILURE(false,
746           "Decode request from client in invalid state: " << state_,
747           PLATFORM_FAILURE,); //NOLINT
748       break;
749   }
750 }
751
752 void VaapiVideoDecodeAccelerator::RecycleVASurfaceID(
753     VASurfaceID va_surface_id) {
754   DCHECK_EQ(message_loop_, base::MessageLoop::current());
755   base::AutoLock auto_lock(lock_);
756
757   available_va_surfaces_.push_back(va_surface_id);
758   surfaces_available_.Signal();
759 }
760
761 void VaapiVideoDecodeAccelerator::AssignPictureBuffers(
762     const std::vector<media::PictureBuffer>& buffers) {
763   DCHECK_EQ(message_loop_, base::MessageLoop::current());
764
765   base::AutoLock auto_lock(lock_);
766   DCHECK(tfp_pictures_.empty());
767
768   while (!output_buffers_.empty())
769     output_buffers_.pop();
770
771   RETURN_AND_NOTIFY_ON_FAILURE(
772       buffers.size() == requested_num_pics_,
773       "Got an invalid buffers. (Got " << buffers.size() << ", requested "
774       << requested_num_pics_ << ")", INVALID_ARGUMENT,); //NOLINT
775   DCHECK(requested_pic_size_ == buffers[0].size());
776
777   std::vector<VASurfaceID> va_surface_ids;
778   RETURN_AND_NOTIFY_ON_FAILURE(
779       vaapi_wrapper_->CreateSurfaces(requested_pic_size_,
780                                      buffers.size(),
781                                      &va_surface_ids),
782       "Failed creating VA Surfaces", PLATFORM_FAILURE,); //NOLINT
783   DCHECK_EQ(va_surface_ids.size(), buffers.size());
784
785   for (size_t i = 0; i < buffers.size(); ++i) {
786     DVLOG(2) << "Assigning picture id: " << buffers[i].id()
787              << " to texture id: " << buffers[i].texture_id()
788              << " VASurfaceID: " << va_surface_ids[i];
789
790     linked_ptr<TFPPicture> tfp_picture(
791         TFPPicture::Create(make_context_current_,
792                            vaapi_wrapper_.get(),
793                            buffers[i].id(),
794                            buffers[i].texture_id(),
795                            requested_pic_size_));
796
797     RETURN_AND_NOTIFY_ON_FAILURE(
798         tfp_picture.get(), "Failed assigning picture buffer to a texture.",
799         PLATFORM_FAILURE,); //NOLINT
800
801     bool inserted = tfp_pictures_.insert(std::make_pair(
802         buffers[i].id(), tfp_picture)).second;
803     DCHECK(inserted);
804
805     output_buffers_.push(buffers[i].id());
806     available_va_surfaces_.push_back(va_surface_ids[i]);
807     surfaces_available_.Signal();
808   }
809
810   state_ = kDecoding;
811   decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
812       &VaapiVideoDecodeAccelerator::DecodeTask, base::Unretained(this)));
813 }
814
815 void VaapiVideoDecodeAccelerator::ReusePictureBuffer(int32 picture_buffer_id) {
816   DCHECK_EQ(message_loop_, base::MessageLoop::current());
817   TRACE_EVENT1("Video Decoder", "VAVDA::ReusePictureBuffer", "Picture id",
818                picture_buffer_id);
819
820   --num_frames_at_client_;
821   TRACE_COUNTER1("Video Decoder", "Textures at client", num_frames_at_client_);
822
823   output_buffers_.push(picture_buffer_id);
824   TryOutputSurface();
825 }
826
827 void VaapiVideoDecodeAccelerator::FlushTask() {
828   DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
829   DVLOG(1) << "Flush task";
830
831   // First flush all the pictures that haven't been outputted, notifying the
832   // client to output them.
833   bool res = decoder_->Flush();
834   RETURN_AND_NOTIFY_ON_FAILURE(res, "Failed flushing the decoder.",
835                                PLATFORM_FAILURE,); //NOLINT
836
837   // Put the decoder in idle state, ready to resume.
838   decoder_->Reset();
839
840   message_loop_->PostTask(FROM_HERE, base::Bind(
841       &VaapiVideoDecodeAccelerator::FinishFlush, weak_this_));
842 }
843
844 void VaapiVideoDecodeAccelerator::Flush() {
845   DCHECK_EQ(message_loop_, base::MessageLoop::current());
846   DVLOG(1) << "Got flush request";
847
848   base::AutoLock auto_lock(lock_);
849   state_ = kFlushing;
850   // Queue a flush task after all existing decoding tasks to clean up.
851   decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
852       &VaapiVideoDecodeAccelerator::FlushTask, base::Unretained(this)));
853
854   input_ready_.Signal();
855   surfaces_available_.Signal();
856 }
857
858 void VaapiVideoDecodeAccelerator::FinishFlush() {
859   DCHECK_EQ(message_loop_, base::MessageLoop::current());
860
861   finish_flush_pending_ = false;
862
863   base::AutoLock auto_lock(lock_);
864   if (state_ != kFlushing) {
865     DCHECK_EQ(state_, kDestroying);
866     return;  // We could've gotten destroyed already.
867   }
868
869   // Still waiting for textures from client to finish outputting all pending
870   // frames. Try again later.
871   if (!pending_output_cbs_.empty()) {
872     finish_flush_pending_ = true;
873     return;
874   }
875
876   state_ = kIdle;
877
878   message_loop_->PostTask(FROM_HERE, base::Bind(
879       &Client::NotifyFlushDone, client_));
880
881   DVLOG(1) << "Flush finished";
882 }
883
884 void VaapiVideoDecodeAccelerator::ResetTask() {
885   DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
886   DVLOG(1) << "ResetTask";
887
888   // All the decoding tasks from before the reset request from client are done
889   // by now, as this task was scheduled after them and client is expected not
890   // to call Decode() after Reset() and before NotifyResetDone.
891   decoder_->Reset();
892
893   base::AutoLock auto_lock(lock_);
894
895   // Return current input buffer, if present.
896   if (curr_input_buffer_.get())
897     ReturnCurrInputBuffer_Locked();
898
899   // And let client know that we are done with reset.
900   message_loop_->PostTask(FROM_HERE, base::Bind(
901       &VaapiVideoDecodeAccelerator::FinishReset, weak_this_));
902 }
903
904 void VaapiVideoDecodeAccelerator::Reset() {
905   DCHECK_EQ(message_loop_, base::MessageLoop::current());
906   DVLOG(1) << "Got reset request";
907
908   // This will make any new decode tasks exit early.
909   base::AutoLock auto_lock(lock_);
910   state_ = kResetting;
911   finish_flush_pending_ = false;
912
913   // Drop all remaining input buffers, if present.
914   while (!input_buffers_.empty()) {
915     message_loop_->PostTask(FROM_HERE, base::Bind(
916         &Client::NotifyEndOfBitstreamBuffer, client_,
917         input_buffers_.front()->id));
918     input_buffers_.pop();
919   }
920
921   decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
922       &VaapiVideoDecodeAccelerator::ResetTask, base::Unretained(this)));
923
924   input_ready_.Signal();
925   surfaces_available_.Signal();
926 }
927
928 void VaapiVideoDecodeAccelerator::FinishReset() {
929   DCHECK_EQ(message_loop_, base::MessageLoop::current());
930   DVLOG(1) << "FinishReset";
931   base::AutoLock auto_lock(lock_);
932
933   if (state_ != kResetting) {
934     DCHECK(state_ == kDestroying || state_ == kUninitialized) << state_;
935     return;  // We could've gotten destroyed already.
936   }
937
938   // Drop pending outputs.
939   while (!pending_output_cbs_.empty())
940     pending_output_cbs_.pop();
941
942   if (awaiting_va_surfaces_recycle_) {
943     // Decoder requested a new surface set while we were waiting for it to
944     // finish the last DecodeTask, running at the time of Reset().
945     // Let the surface set change finish first before resetting.
946     message_loop_->PostTask(FROM_HERE, base::Bind(
947         &VaapiVideoDecodeAccelerator::FinishReset, weak_this_));
948     return;
949   }
950
951   num_stream_bufs_at_decoder_ = 0;
952   state_ = kIdle;
953
954   message_loop_->PostTask(FROM_HERE, base::Bind(
955       &Client::NotifyResetDone, client_));
956
957   // The client might have given us new buffers via Decode() while we were
958   // resetting and might be waiting for our move, and not call Decode() anymore
959   // until we return something. Post a DecodeTask() so that we won't
960   // sleep forever waiting for Decode() in that case. Having two of them
961   // in the pipe is harmless, the additional one will return as soon as it sees
962   // that we are back in kDecoding state.
963   if (!input_buffers_.empty()) {
964     state_ = kDecoding;
965     decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
966       &VaapiVideoDecodeAccelerator::DecodeTask,
967       base::Unretained(this)));
968   }
969
970   DVLOG(1) << "Reset finished";
971 }
972
973 void VaapiVideoDecodeAccelerator::Cleanup() {
974   DCHECK_EQ(message_loop_, base::MessageLoop::current());
975
976   if (state_ == kUninitialized || state_ == kDestroying)
977     return;
978
979   DVLOG(1) << "Destroying VAVDA";
980   base::AutoLock auto_lock(lock_);
981   state_ = kDestroying;
982
983   client_ptr_factory_.reset();
984   weak_this_factory_.InvalidateWeakPtrs();
985
986   {
987     base::AutoUnlock auto_unlock(lock_);
988     // Post a dummy task to the decoder_thread_ to ensure it is drained.
989     base::WaitableEvent waiter(false, false);
990     decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind(
991         &base::WaitableEvent::Signal, base::Unretained(&waiter)));
992     input_ready_.Signal();
993     surfaces_available_.Signal();
994     waiter.Wait();
995     decoder_thread_.Stop();
996   }
997
998   state_ = kUninitialized;
999 }
1000
1001 void VaapiVideoDecodeAccelerator::Destroy() {
1002   DCHECK_EQ(message_loop_, base::MessageLoop::current());
1003   Cleanup();
1004   delete this;
1005 }
1006
1007 bool VaapiVideoDecodeAccelerator::CanDecodeOnIOThread() {
1008   return false;
1009 }
1010
1011 }  // namespace media