GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / content / common / gpu / media / vaapi_video_decode_accelerator.h
blob0ef25c55e947ea7f1ddae56dea7416436b80c15e
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 <map>
12 #include <queue>
13 #include <utility>
14 #include <vector>
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/thread.h"
24 #include "content/common/content_export.h"
25 #include "content/common/gpu/media/vaapi_h264_decoder.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 VaapiPicture;
39 // Class to provide video decode acceleration for Intel systems with hardware
40 // support for it, and on which libva is available.
41 // Decoding tasks are performed in a separate decoding thread.
43 // Threading/life-cycle: this object is created & destroyed on the GPU
44 // ChildThread. A few methods on it are called on the decoder thread which is
45 // stopped during |this->Destroy()|, so any tasks posted to the decoder thread
46 // can assume |*this| is still alive. See |weak_this_| below for more details.
47 class CONTENT_EXPORT VaapiVideoDecodeAccelerator
48 : public media::VideoDecodeAccelerator {
49 public:
50 VaapiVideoDecodeAccelerator(
51 const base::Callback<bool(void)>& make_context_current,
52 const base::Callback<void(uint32, uint32, scoped_refptr<gfx::GLImage>)>&
53 bind_image);
54 ~VaapiVideoDecodeAccelerator() override;
56 // media::VideoDecodeAccelerator implementation.
57 bool Initialize(media::VideoCodecProfile profile,
58 Client* client) override;
59 void Decode(const media::BitstreamBuffer& bitstream_buffer) override;
60 void AssignPictureBuffers(
61 const std::vector<media::PictureBuffer>& buffers) override;
62 void ReusePictureBuffer(int32 picture_buffer_id) override;
63 void Flush() override;
64 void Reset() override;
65 void Destroy() override;
66 bool CanDecodeOnIOThread() override;
68 private:
69 // Notify the client that an error has occurred and decoding cannot continue.
70 void NotifyError(Error error);
72 // Map the received input buffer into this process' address space and
73 // queue it for decode.
74 void MapAndQueueNewInputBuffer(
75 const media::BitstreamBuffer& bitstream_buffer);
77 // Get a new input buffer from the queue and set it up in decoder. This will
78 // sleep if no input buffers are available. Return true if a new buffer has
79 // been set up, false if an early exit has been requested (due to initiated
80 // reset/flush/destroy).
81 bool GetInputBuffer_Locked();
83 // Signal the client that the current buffer has been read and can be
84 // returned. Will also release the mapping.
85 void ReturnCurrInputBuffer_Locked();
87 // Pass one or more output buffers to the decoder. This will sleep
88 // if no buffers are available. Return true if buffers have been set up or
89 // false if an early exit has been requested (due to initiated
90 // reset/flush/destroy).
91 bool FeedDecoderWithOutputSurfaces_Locked();
93 // Continue decoding given input buffers and sleep waiting for input/output
94 // as needed. Will exit if a new set of surfaces or reset/flush/destroy
95 // is requested.
96 void DecodeTask();
98 // Scheduled after receiving a flush request and executed after the current
99 // decoding task finishes decoding pending inputs. Makes the decoder return
100 // all remaining output pictures and puts it in an idle state, ready
101 // to resume if needed and schedules a FinishFlush.
102 void FlushTask();
104 // Scheduled by the FlushTask after decoder is flushed to put VAVDA into idle
105 // state and notify the client that flushing has been finished.
106 void FinishFlush();
108 // Scheduled after receiving a reset request and executed after the current
109 // decoding task finishes decoding the current frame. Puts the decoder into
110 // an idle state, ready to resume if needed, discarding decoded but not yet
111 // outputted pictures (decoder keeps ownership of their associated picture
112 // buffers). Schedules a FinishReset afterwards.
113 void ResetTask();
115 // Scheduled by ResetTask after it's done putting VAVDA into an idle state.
116 // Drops remaining input buffers and notifies the client that reset has been
117 // finished.
118 void FinishReset();
120 // Helper for Destroy(), doing all the actual work except for deleting self.
121 void Cleanup();
123 // Get a usable framebuffer configuration for use in binding textures
124 // or return false on failure.
125 bool InitializeFBConfig();
127 // Callback for the decoder to execute when it wants us to output given
128 // |va_surface|.
129 void SurfaceReady(int32 input_id, const scoped_refptr<VASurface>& va_surface);
131 // Callback to be executed once we have a |va_surface| to be output and
132 // an available |picture| to use for output.
133 // Puts contents of |va_surface| into given |picture|, releases the
134 // surface and passes the resulting picture to client for output.
135 void OutputPicture(const scoped_refptr<VASurface>& va_surface,
136 int32 input_id,
137 VaapiPicture* picture);
139 // Try to OutputPicture() if we have both a ready surface and picture.
140 void TryOutputSurface();
142 // Called when a VASurface is no longer in use by the decoder or is not being
143 // synced/waiting to be synced to a picture. Returns it to available surfaces
144 // pool.
145 void RecycleVASurfaceID(VASurfaceID va_surface_id);
147 // Initiate wait cycle for surfaces to be released before we release them
148 // and allocate new ones, as requested by the decoder.
149 void InitiateSurfaceSetChange(size_t num_pics, gfx::Size size);
150 // Check if the surfaces have been released or post ourselves for later.
151 void TryFinishSurfaceSetChange();
153 // Client-provided GL state.
154 base::Callback<bool(void)> make_context_current_;
156 // VAVDA state.
157 enum State {
158 // Initialize() not called yet or failed.
159 kUninitialized,
160 // DecodeTask running.
161 kDecoding,
162 // Resetting, waiting for decoder to finish current task and cleanup.
163 kResetting,
164 // Flushing, waiting for decoder to finish current task and cleanup.
165 kFlushing,
166 // Idle, decoder in state ready to start/resume decoding.
167 kIdle,
168 // Destroying, waiting for the decoder to finish current task.
169 kDestroying,
172 // Protects input buffer and surface queues and state_.
173 base::Lock lock_;
174 State state_;
176 // An input buffer awaiting consumption, provided by the client.
177 struct InputBuffer {
178 InputBuffer();
179 ~InputBuffer();
181 int32 id;
182 size_t size;
183 scoped_ptr<base::SharedMemory> shm;
186 // Queue for incoming input buffers.
187 typedef std::queue<linked_ptr<InputBuffer> > InputBuffers;
188 InputBuffers input_buffers_;
189 // Signalled when input buffers are queued onto the input_buffers_ queue.
190 base::ConditionVariable input_ready_;
192 // Current input buffer at decoder.
193 linked_ptr<InputBuffer> curr_input_buffer_;
195 // Queue for incoming output buffers (texture ids).
196 typedef std::queue<int32> OutputBuffers;
197 OutputBuffers output_buffers_;
199 scoped_ptr<VaapiWrapper> vaapi_wrapper_;
201 typedef std::map<int32, linked_ptr<VaapiPicture>> Pictures;
202 // All allocated Pictures, regardless of their current state.
203 // Pictures are allocated once and destroyed at the end of decode.
204 // Comes after vaapi_wrapper_ to ensure all pictures are destroyed
205 // before vaapi_wrapper_ is destroyed.
206 Pictures pictures_;
208 // Return a VaapiPicture associated with given client-provided id.
209 VaapiPicture* PictureById(int32 picture_buffer_id);
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_;
218 // Pending output requests from the decoder. When it indicates that we should
219 // output a surface and we have an available Picture (i.e. texture) ready
220 // to use, we'll execute the callback passing the Picture. 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 Pictures 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(VaapiPicture*)> OutputCB;
228 std::queue<OutputCB> pending_output_cbs_;
230 // ChildThread's message loop
231 base::MessageLoop* message_loop_;
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_;
241 // Callback used when creating VASurface objects.
242 VASurface::ReleaseCB va_surface_release_cb_;
244 // To expose client callbacks from VideoDecodeAccelerator.
245 // NOTE: all calls to these objects *MUST* be executed on message_loop_.
246 scoped_ptr<base::WeakPtrFactory<Client> > client_ptr_factory_;
247 base::WeakPtr<Client> client_;
249 // Comes after vaapi_wrapper_ to ensure its destructor is executed before
250 // vaapi_wrapper_ is destroyed.
251 scoped_ptr<VaapiH264Decoder> decoder_;
252 base::Thread decoder_thread_;
253 // Use this to post tasks to |decoder_thread_| instead of
254 // |decoder_thread_.message_loop()| because the latter will be NULL once
255 // |decoder_thread_.Stop()| returns.
256 scoped_refptr<base::MessageLoopProxy> decoder_thread_proxy_;
258 int num_frames_at_client_;
259 int num_stream_bufs_at_decoder_;
261 // Whether we are waiting for any pending_output_cbs_ to be run before
262 // NotifyingFlushDone.
263 bool finish_flush_pending_;
265 // Decoder requested a new surface set and we are waiting for all the surfaces
266 // to be returned before we can free them.
267 bool awaiting_va_surfaces_recycle_;
269 // Last requested number/resolution of output picture buffers.
270 size_t requested_num_pics_;
271 gfx::Size requested_pic_size_;
273 // Binds the provided GLImage to a givenr client texture ID & texture target
274 // combination in GLES.
275 base::Callback<void(uint32, uint32, scoped_refptr<gfx::GLImage>)> bind_image_;
277 // The WeakPtrFactory for |weak_this_|.
278 base::WeakPtrFactory<VaapiVideoDecodeAccelerator> weak_this_factory_;
280 DISALLOW_COPY_AND_ASSIGN(VaapiVideoDecodeAccelerator);
283 } // namespace content
285 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_DECODE_ACCELERATOR_H_