[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / content / common / gpu / media / vaapi_video_decode_accelerator.h
blob268439b96bc4dad249401bf99f5570dda48862bc
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.
8 #ifndef CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_DECODE_ACCELERATOR_H_
9 #define CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_DECODE_ACCELERATOR_H_
11 #include <list>
12 #include <map>
13 #include <queue>
14 #include <utility>
15 #include <vector>
17 #include "base/logging.h"
18 #include "base/memory/linked_ptr.h"
19 #include "base/memory/shared_memory.h"
20 #include "base/memory/weak_ptr.h"
21 #include "base/message_loop/message_loop.h"
22 #include "base/synchronization/condition_variable.h"
23 #include "base/synchronization/lock.h"
24 #include "base/threading/thread.h"
25 #include "content/common/content_export.h"
26 #include "content/common/gpu/media/vaapi_wrapper.h"
27 #include "media/base/bitstream_buffer.h"
28 #include "media/video/picture.h"
29 #include "media/video/video_decode_accelerator.h"
31 namespace gfx {
32 class GLImage;
35 namespace content {
37 class AcceleratedVideoDecoder;
38 class VaapiPicture;
40 // Class to provide video decode acceleration for Intel systems with hardware
41 // support for it, and on which libva is available.
42 // Decoding tasks are performed in a separate decoding thread.
44 // Threading/life-cycle: this object is created & destroyed on the GPU
45 // ChildThread. A few methods on it are called on the decoder thread which is
46 // stopped during |this->Destroy()|, so any tasks posted to the decoder thread
47 // can assume |*this| is still alive. See |weak_this_| below for more details.
48 class CONTENT_EXPORT VaapiVideoDecodeAccelerator
49 : public media::VideoDecodeAccelerator {
50 public:
51 class VaapiDecodeSurface;
53 VaapiVideoDecodeAccelerator(
54 const base::Callback<bool(void)>& make_context_current,
55 const base::Callback<void(uint32, uint32, scoped_refptr<gfx::GLImage>)>&
56 bind_image);
57 ~VaapiVideoDecodeAccelerator() override;
59 // media::VideoDecodeAccelerator implementation.
60 bool Initialize(media::VideoCodecProfile profile, Client* client) override;
61 void Decode(const media::BitstreamBuffer& bitstream_buffer) override;
62 void AssignPictureBuffers(
63 const std::vector<media::PictureBuffer>& buffers) override;
64 void ReusePictureBuffer(int32 picture_buffer_id) override;
65 void Flush() override;
66 void Reset() override;
67 void Destroy() override;
68 bool CanDecodeOnIOThread() override;
70 static media::VideoDecodeAccelerator::SupportedProfiles
71 GetSupportedProfiles();
73 private:
74 class VaapiH264Accelerator;
75 class VaapiVP8Accelerator;
77 // Notify the client that an error has occurred and decoding cannot continue.
78 void NotifyError(Error error);
80 // Map the received input buffer into this process' address space and
81 // queue it for decode.
82 void MapAndQueueNewInputBuffer(
83 const media::BitstreamBuffer& bitstream_buffer);
85 // Get a new input buffer from the queue and set it up in decoder. This will
86 // sleep if no input buffers are available. Return true if a new buffer has
87 // been set up, false if an early exit has been requested (due to initiated
88 // reset/flush/destroy).
89 bool GetInputBuffer_Locked();
91 // Signal the client that the current buffer has been read and can be
92 // returned. Will also release the mapping.
93 void ReturnCurrInputBuffer_Locked();
95 // Wait for more surfaces to become available. Return true once they do or
96 // false if an early exit has been requested (due to an initiated
97 // reset/flush/destroy).
98 bool WaitForSurfaces_Locked();
100 // Continue decoding given input buffers and sleep waiting for input/output
101 // as needed. Will exit if a new set of surfaces or reset/flush/destroy
102 // is requested.
103 void DecodeTask();
105 // Scheduled after receiving a flush request and executed after the current
106 // decoding task finishes decoding pending inputs. Makes the decoder return
107 // all remaining output pictures and puts it in an idle state, ready
108 // to resume if needed and schedules a FinishFlush.
109 void FlushTask();
111 // Scheduled by the FlushTask after decoder is flushed to put VAVDA into idle
112 // state and notify the client that flushing has been finished.
113 void FinishFlush();
115 // Scheduled after receiving a reset request and executed after the current
116 // decoding task finishes decoding the current frame. Puts the decoder into
117 // an idle state, ready to resume if needed, discarding decoded but not yet
118 // outputted pictures (decoder keeps ownership of their associated picture
119 // buffers). Schedules a FinishReset afterwards.
120 void ResetTask();
122 // Scheduled by ResetTask after it's done putting VAVDA into an idle state.
123 // Drops remaining input buffers and notifies the client that reset has been
124 // finished.
125 void FinishReset();
127 // Helper for Destroy(), doing all the actual work except for deleting self.
128 void Cleanup();
130 // Get a usable framebuffer configuration for use in binding textures
131 // or return false on failure.
132 bool InitializeFBConfig();
134 // Callback to be executed once we have a |va_surface| to be output and
135 // an available |picture| to use for output.
136 // Puts contents of |va_surface| into given |picture|, releases the
137 // surface and passes the resulting picture to client for output.
138 void OutputPicture(const scoped_refptr<VASurface>& va_surface,
139 int32 input_id,
140 VaapiPicture* picture);
142 // Try to OutputPicture() if we have both a ready surface and picture.
143 void TryOutputSurface();
145 // Called when a VASurface is no longer in use by the decoder or is not being
146 // synced/waiting to be synced to a picture. Returns it to available surfaces
147 // pool.
148 void RecycleVASurfaceID(VASurfaceID va_surface_id);
150 // Initiate wait cycle for surfaces to be released before we release them
151 // and allocate new ones, as requested by the decoder.
152 void InitiateSurfaceSetChange(size_t num_pics, gfx::Size size);
154 // Check if the surfaces have been released or post ourselves for later.
155 void TryFinishSurfaceSetChange();
158 // Below methods are used by accelerator implementations.
160 // Decode of |dec_surface| is ready to be submitted and all codec-specific
161 // settings are set in hardware.
162 bool DecodeSurface(const scoped_refptr<VaapiDecodeSurface>& dec_surface);
164 // |dec_surface| is ready to be outputted once decode is finished.
165 // This can be called before decode is actually done in hardware, and this
166 // method is responsible for maintaining the ordering, i.e. the surfaces have
167 // to be outputted in the same order as SurfaceReady is called.
168 // On Intel, we don't have to explicitly maintain the ordering however, as the
169 // driver will maintain ordering, as well as dependencies, and will process
170 // each submitted command in order, and run each command only if its
171 // dependencies are ready.
172 void SurfaceReady(const scoped_refptr<VaapiDecodeSurface>& dec_surface);
174 // Return a new VaapiDecodeSurface for decoding into, or nullptr if not
175 // available.
176 scoped_refptr<VaapiDecodeSurface> CreateSurface();
179 // Client-provided GL state.
180 base::Callback<bool(void)> make_context_current_;
182 // VAVDA state.
183 enum State {
184 // Initialize() not called yet or failed.
185 kUninitialized,
186 // DecodeTask running.
187 kDecoding,
188 // Resetting, waiting for decoder to finish current task and cleanup.
189 kResetting,
190 // Flushing, waiting for decoder to finish current task and cleanup.
191 kFlushing,
192 // Idle, decoder in state ready to start/resume decoding.
193 kIdle,
194 // Destroying, waiting for the decoder to finish current task.
195 kDestroying,
198 // Protects input buffer and surface queues and state_.
199 base::Lock lock_;
200 State state_;
202 // An input buffer awaiting consumption, provided by the client.
203 struct InputBuffer {
204 InputBuffer();
205 ~InputBuffer();
207 int32 id;
208 size_t size;
209 scoped_ptr<base::SharedMemory> shm;
212 // Queue for incoming input buffers.
213 typedef std::queue<linked_ptr<InputBuffer> > InputBuffers;
214 InputBuffers input_buffers_;
215 // Signalled when input buffers are queued onto the input_buffers_ queue.
216 base::ConditionVariable input_ready_;
218 // Current input buffer at decoder.
219 linked_ptr<InputBuffer> curr_input_buffer_;
221 // Queue for incoming output buffers (texture ids).
222 typedef std::queue<int32> OutputBuffers;
223 OutputBuffers output_buffers_;
225 scoped_ptr<VaapiWrapper> vaapi_wrapper_;
227 typedef std::map<int32, linked_ptr<VaapiPicture>> Pictures;
228 // All allocated Pictures, regardless of their current state.
229 // Pictures are allocated once and destroyed at the end of decode.
230 // Comes after vaapi_wrapper_ to ensure all pictures are destroyed
231 // before vaapi_wrapper_ is destroyed.
232 Pictures pictures_;
234 // Return a VaapiPicture associated with given client-provided id.
235 VaapiPicture* PictureById(int32 picture_buffer_id);
237 // VA Surfaces no longer in use that can be passed back to the decoder for
238 // reuse, once it requests them.
239 std::list<VASurfaceID> available_va_surfaces_;
240 // Signalled when output surfaces are queued onto the available_va_surfaces_
241 // queue.
242 base::ConditionVariable surfaces_available_;
244 // Pending output requests from the decoder. When it indicates that we should
245 // output a surface and we have an available Picture (i.e. texture) ready
246 // to use, we'll execute the callback passing the Picture. The callback
247 // will put the contents of the surface into the picture and return it to
248 // the client, releasing the surface as well.
249 // If we don't have any available Pictures at the time when the decoder
250 // requests output, we'll store the request on pending_output_cbs_ queue for
251 // later and run it once the client gives us more textures
252 // via ReusePictureBuffer().
253 typedef base::Callback<void(VaapiPicture*)> OutputCB;
254 std::queue<OutputCB> pending_output_cbs_;
256 // ChildThread's message loop
257 base::MessageLoop* message_loop_;
259 // WeakPtr<> pointing to |this| for use in posting tasks from the decoder
260 // thread back to the ChildThread. Because the decoder thread is a member of
261 // this class, any task running on the decoder thread is guaranteed that this
262 // object is still alive. As a result, tasks posted from ChildThread to
263 // decoder thread should use base::Unretained(this), and tasks posted from the
264 // decoder thread to the ChildThread should use |weak_this_|.
265 base::WeakPtr<VaapiVideoDecodeAccelerator> weak_this_;
267 // Callback used when creating VASurface objects.
268 VASurface::ReleaseCB va_surface_release_cb_;
270 // To expose client callbacks from VideoDecodeAccelerator.
271 // NOTE: all calls to these objects *MUST* be executed on message_loop_.
272 scoped_ptr<base::WeakPtrFactory<Client> > client_ptr_factory_;
273 base::WeakPtr<Client> client_;
275 // Accelerators come after vaapi_wrapper_ to ensure they are destroyed first.
276 scoped_ptr<VaapiH264Accelerator> h264_accelerator_;
277 scoped_ptr<VaapiVP8Accelerator> vp8_accelerator_;
278 // After *_accelerator_ to ensure correct destruction order.
279 scoped_ptr<AcceleratedVideoDecoder> decoder_;
281 base::Thread decoder_thread_;
282 // Use this to post tasks to |decoder_thread_| instead of
283 // |decoder_thread_.message_loop()| because the latter will be NULL once
284 // |decoder_thread_.Stop()| returns.
285 scoped_refptr<base::SingleThreadTaskRunner> decoder_thread_task_runner_;
287 int num_frames_at_client_;
288 int num_stream_bufs_at_decoder_;
290 // Whether we are waiting for any pending_output_cbs_ to be run before
291 // NotifyingFlushDone.
292 bool finish_flush_pending_;
294 // Decoder requested a new surface set and we are waiting for all the surfaces
295 // to be returned before we can free them.
296 bool awaiting_va_surfaces_recycle_;
298 // Last requested number/resolution of output picture buffers.
299 size_t requested_num_pics_;
300 gfx::Size requested_pic_size_;
302 // Binds the provided GLImage to a givenr client texture ID & texture target
303 // combination in GLES.
304 base::Callback<void(uint32, uint32, scoped_refptr<gfx::GLImage>)> bind_image_;
306 // The WeakPtrFactory for |weak_this_|.
307 base::WeakPtrFactory<VaapiVideoDecodeAccelerator> weak_this_factory_;
309 DISALLOW_COPY_AND_ASSIGN(VaapiVideoDecodeAccelerator);
312 } // namespace content
314 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_DECODE_ACCELERATOR_H_