- add sources.
[platform/framework/web/crosswalk.git] / src / content / common / gpu / media / vaapi_video_decode_accelerator.h
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // This file contains an implementation of VideoDecoderAccelerator
6 // that utilizes hardware video decoder present on Intel CPUs.
7
8 #ifndef CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_DECODE_ACCELERATOR_H_
9 #define CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_DECODE_ACCELERATOR_H_
10
11 #include <map>
12 #include <queue>
13 #include <utility>
14 #include <vector>
15
16 #include "base/logging.h"
17 #include "base/memory/linked_ptr.h"
18 #include "base/memory/shared_memory.h"
19 #include "base/memory/weak_ptr.h"
20 #include "base/message_loop/message_loop.h"
21 #include "base/synchronization/condition_variable.h"
22 #include "base/synchronization/lock.h"
23 #include "base/threading/non_thread_safe.h"
24 #include "base/threading/thread.h"
25 #include "content/common/content_export.h"
26 #include "content/common/gpu/media/vaapi_h264_decoder.h"
27 #include "content/common/gpu/media/vaapi_wrapper.h"
28 #include "content/common/gpu/media/video_decode_accelerator_impl.h"
29 #include "media/base/bitstream_buffer.h"
30 #include "media/video/picture.h"
31 #include "media/video/video_decode_accelerator.h"
32 #include "ui/gl/gl_bindings.h"
33
34 namespace content {
35
36 // Class to provide video decode acceleration for Intel systems with hardware
37 // support for it, and on which libva is available.
38 // Decoding tasks are performed in a separate decoding thread.
39 //
40 // Threading/life-cycle: this object is created & destroyed on the GPU
41 // ChildThread.  A few methods on it are called on the decoder thread which is
42 // stopped during |this->Destroy()|, so any tasks posted to the decoder thread
43 // can assume |*this| is still alive.  See |weak_this_| below for more details.
44 class CONTENT_EXPORT VaapiVideoDecodeAccelerator
45     : public VideoDecodeAcceleratorImpl {
46  public:
47   VaapiVideoDecodeAccelerator(
48       Display* x_display, GLXContext glx_context,
49       Client* client,
50       const base::Callback<bool(void)>& make_context_current);
51   virtual ~VaapiVideoDecodeAccelerator();
52
53   // media::VideoDecodeAccelerator implementation.
54   virtual bool Initialize(media::VideoCodecProfile profile) OVERRIDE;
55   virtual void Decode(const media::BitstreamBuffer& bitstream_buffer) OVERRIDE;
56   virtual void AssignPictureBuffers(
57       const std::vector<media::PictureBuffer>& buffers) OVERRIDE;
58   virtual void ReusePictureBuffer(int32 picture_buffer_id) OVERRIDE;
59   virtual void Flush() OVERRIDE;
60   virtual void Reset() OVERRIDE;
61   virtual void Destroy() OVERRIDE;
62
63 private:
64   // Notify the client that |output_id| is ready for displaying.
65   void NotifyPictureReady(int32 input_id, int32 output_id);
66
67   // Notify the client that an error has occurred and decoding cannot continue.
68   void NotifyError(Error error);
69
70   // Map the received input buffer into this process' address space and
71   // queue it for decode.
72   void MapAndQueueNewInputBuffer(
73       const media::BitstreamBuffer& bitstream_buffer);
74
75   // Get a new input buffer from the queue and set it up in decoder. This will
76   // sleep if no input buffers are available. Return true if a new buffer has
77   // been set up, false if an early exit has been requested (due to initiated
78   // reset/flush/destroy).
79   bool GetInputBuffer_Locked();
80
81   // Signal the client that the current buffer has been read and can be
82   // returned. Will also release the mapping.
83   void ReturnCurrInputBuffer_Locked();
84
85   // Pass one or more output buffers to the decoder. This will sleep
86   // if no buffers are available. Return true if buffers have been set up or
87   // false if an early exit has been requested (due to initiated
88   // reset/flush/destroy).
89   bool FeedDecoderWithOutputSurfaces_Locked();
90
91   // Continue decoding given input buffers and sleep waiting for input/output
92   // as needed. Will exit if a new set of surfaces or reset/flush/destroy
93   // is requested.
94   void DecodeTask();
95
96   // Scheduled after receiving a flush request and executed after the current
97   // decoding task finishes decoding pending inputs. Makes the decoder return
98   // all remaining output pictures and puts it in an idle state, ready
99   // to resume if needed and schedules a FinishFlush.
100   void FlushTask();
101
102   // Scheduled by the FlushTask after decoder is flushed to put VAVDA into idle
103   // state and notify the client that flushing has been finished.
104   void FinishFlush();
105
106   // Scheduled after receiving a reset request and executed after the current
107   // decoding task finishes decoding the current frame. Puts the decoder into
108   // an idle state, ready to resume if needed, discarding decoded but not yet
109   // outputted pictures (decoder keeps ownership of their associated picture
110   // buffers). Schedules a FinishReset afterwards.
111   void ResetTask();
112
113   // Scheduled by ResetTask after it's done putting VAVDA into an idle state.
114   // Drops remaining input buffers and notifies the client that reset has been
115   // finished.
116   void FinishReset();
117
118   // Helper for Destroy(), doing all the actual work except for deleting self.
119   void Cleanup();
120
121   // Get a usable framebuffer configuration for use in binding textures
122   // or return false on failure.
123   bool InitializeFBConfig();
124
125   // Callback for the decoder to execute when it wants us to output given
126   // |va_surface|.
127   void SurfaceReady(int32 input_id, const scoped_refptr<VASurface>& va_surface);
128
129   // Represents a texture bound to an X Pixmap for output purposes.
130   class TFPPicture;
131
132   // Callback to be executed once we have a |va_surface| to be output and
133   // an available |tfp_picture| to use for output.
134   // Puts contents of |va_surface| into given |tfp_picture|, releases the
135   // surface and passes the resulting picture to client for output.
136   void OutputPicture(const scoped_refptr<VASurface>& va_surface,
137                      int32 input_id,
138                      TFPPicture* tfp_picture);
139
140   // Try to OutputPicture() if we have both a ready surface and picture.
141   void TryOutputSurface();
142
143   // Called when a VASurface is no longer in use by the decoder or is not being
144   // synced/waiting to be synced to a picture. Returns it to available surfaces
145   // pool.
146   void RecycleVASurfaceID(VASurfaceID va_surface_id);
147
148   // Initiate wait cycle for surfaces to be released before we release them
149   // and allocate new ones, as requested by the decoder.
150   void InitiateSurfaceSetChange(size_t num_pics, gfx::Size size);
151   // Check if the surfaces have been released or post ourselves for later.
152   void TryFinishSurfaceSetChange();
153
154   // Client-provided X/GLX state.
155   Display* x_display_;
156   GLXContext glx_context_;
157   base::Callback<bool(void)> make_context_current_;
158   GLXFBConfig fb_config_;
159
160   // VAVDA state.
161   enum State {
162     // Initialize() not called yet or failed.
163     kUninitialized,
164     // DecodeTask running.
165     kDecoding,
166     // Resetting, waiting for decoder to finish current task and cleanup.
167     kResetting,
168     // Flushing, waiting for decoder to finish current task and cleanup.
169     kFlushing,
170     // Idle, decoder in state ready to start/resume decoding.
171     kIdle,
172     // Destroying, waiting for the decoder to finish current task.
173     kDestroying,
174   };
175
176   // Protects input buffer and surface queues and state_.
177   base::Lock lock_;
178   State state_;
179
180   // An input buffer awaiting consumption, provided by the client.
181   struct InputBuffer {
182     InputBuffer();
183     ~InputBuffer();
184
185     int32 id;
186     size_t size;
187     scoped_ptr<base::SharedMemory> shm;
188   };
189
190   // Queue for incoming input buffers.
191   typedef std::queue<linked_ptr<InputBuffer> > InputBuffers;
192   InputBuffers input_buffers_;
193   // Signalled when input buffers are queued onto the input_buffers_ queue.
194   base::ConditionVariable input_ready_;
195
196   // Current input buffer at decoder.
197   linked_ptr<InputBuffer> curr_input_buffer_;
198
199   // Queue for incoming output buffers (texture ids).
200   typedef std::queue<int32> OutputBuffers;
201   OutputBuffers output_buffers_;
202
203   typedef std::map<int32, linked_ptr<TFPPicture> > TFPPictures;
204   // All allocated TFPPictures, regardless of their current state. TFPPictures
205   // are allocated once and destroyed at the end of decode.
206   TFPPictures tfp_pictures_;
207
208   // Return a TFPPicture associated with given client-provided id.
209   TFPPicture* TFPPictureById(int32 picture_buffer_id);
210
211   // VA Surfaces no longer in use that can be passed back to the decoder for
212   // reuse, once it requests them.
213   std::list<VASurfaceID> available_va_surfaces_;
214   // Signalled when output surfaces are queued onto the available_va_surfaces_
215   // queue.
216   base::ConditionVariable surfaces_available_;
217
218   // Pending output requests from the decoder. When it indicates that we should
219   // output a surface and we have an available TFPPicture (i.e. texture) ready
220   // to use, we'll execute the callback passing the TFPPicture. The callback
221   // will put the contents of the surface into the picture and return it to
222   // the client, releasing the surface as well.
223   // If we don't have any available TFPPictures at the time when the decoder
224   // requests output, we'll store the request on pending_output_cbs_ queue for
225   // later and run it once the client gives us more textures
226   // via ReusePictureBuffer().
227   typedef base::Callback<void(TFPPicture*)> OutputCB;
228   std::queue<OutputCB> pending_output_cbs_;
229
230   // ChildThread's message loop
231   base::MessageLoop* message_loop_;
232
233   // WeakPtr<> pointing to |this| for use in posting tasks from the decoder
234   // thread back to the ChildThread.  Because the decoder thread is a member of
235   // this class, any task running on the decoder thread is guaranteed that this
236   // object is still alive.  As a result, tasks posted from ChildThread to
237   // decoder thread should use base::Unretained(this), and tasks posted from the
238   // decoder thread to the ChildThread should use |weak_this_|.
239   base::WeakPtr<VaapiVideoDecodeAccelerator> weak_this_;
240
241   // To expose client callbacks from VideoDecodeAccelerator.
242   // NOTE: all calls to these objects *MUST* be executed on message_loop_.
243   base::WeakPtrFactory<Client> client_ptr_factory_;
244   base::WeakPtr<Client> client_;
245
246   scoped_ptr<VaapiWrapper> vaapi_wrapper_;
247
248   // Comes after vaapi_wrapper_ to ensure its destructor is executed before
249   // vaapi_wrapper_ is destroyed.
250   scoped_ptr<VaapiH264Decoder> decoder_;
251   base::Thread decoder_thread_;
252
253   int num_frames_at_client_;
254   int num_stream_bufs_at_decoder_;
255
256   // Whether we are waiting for any pending_output_cbs_ to be run before
257   // NotifyingFlushDone.
258   bool finish_flush_pending_;
259
260   // Decoder requested a new surface set and we are waiting for all the surfaces
261   // to be returned before we can free them.
262   bool awaiting_va_surfaces_recycle_;
263
264   // Last requested number/resolution of output picture buffers.
265   size_t requested_num_pics_;
266   gfx::Size requested_pic_size_;
267
268   DISALLOW_COPY_AND_ASSIGN(VaapiVideoDecodeAccelerator);
269 };
270
271 }  // namespace content
272
273 #endif  // CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_DECODE_ACCELERATOR_H_