OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // This file contains an implementation of VideoDecoderAccelerator | 5 // This file contains an implementation of VideoDecoderAccelerator |
6 // that utilizes hardware video decoder present on Intel CPUs. | 6 // that utilizes hardware video decoder present on Intel CPUs. |
7 | 7 |
8 #ifndef CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_DECODE_ACCELERATOR_H_ | 8 #ifndef CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_DECODE_ACCELERATOR_H_ |
9 #define CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_DECODE_ACCELERATOR_H_ | 9 #define CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_DECODE_ACCELERATOR_H_ |
10 | 10 |
11 #include <map> | 11 #include <map> |
12 #include <queue> | 12 #include <queue> |
13 #include <utility> | 13 #include <utility> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/memory/linked_ptr.h" | 17 #include "base/memory/linked_ptr.h" |
18 #include "base/memory/shared_memory.h" | 18 #include "base/memory/shared_memory.h" |
19 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
20 #include "base/message_loop/message_loop.h" | 20 #include "base/message_loop/message_loop.h" |
21 #include "base/synchronization/condition_variable.h" | 21 #include "base/synchronization/condition_variable.h" |
22 #include "base/synchronization/lock.h" | 22 #include "base/synchronization/lock.h" |
| 23 #include "base/threading/non_thread_safe.h" |
23 #include "base/threading/thread.h" | 24 #include "base/threading/thread.h" |
24 #include "content/common/content_export.h" | 25 #include "content/common/content_export.h" |
25 #include "content/common/gpu/media/vaapi_h264_decoder.h" | 26 #include "content/common/gpu/media/vaapi_h264_decoder.h" |
26 #include "content/common/gpu/media/vaapi_wrapper.h" | 27 #include "content/common/gpu/media/vaapi_wrapper.h" |
27 #include "media/base/bitstream_buffer.h" | 28 #include "media/base/bitstream_buffer.h" |
28 #include "media/video/picture.h" | 29 #include "media/video/picture.h" |
29 #include "media/video/video_decode_accelerator.h" | 30 #include "media/video/video_decode_accelerator.h" |
| 31 #include "ui/gl/gl_bindings.h" |
30 | 32 |
31 namespace content { | 33 namespace content { |
32 | 34 |
33 class VaapiPicture; | |
34 | |
35 // Class to provide video decode acceleration for Intel systems with hardware | 35 // Class to provide video decode acceleration for Intel systems with hardware |
36 // support for it, and on which libva is available. | 36 // support for it, and on which libva is available. |
37 // Decoding tasks are performed in a separate decoding thread. | 37 // Decoding tasks are performed in a separate decoding thread. |
38 // | 38 // |
39 // Threading/life-cycle: this object is created & destroyed on the GPU | 39 // Threading/life-cycle: this object is created & destroyed on the GPU |
40 // ChildThread. A few methods on it are called on the decoder thread which is | 40 // ChildThread. A few methods on it are called on the decoder thread which is |
41 // stopped during |this->Destroy()|, so any tasks posted to the decoder thread | 41 // stopped during |this->Destroy()|, so any tasks posted to the decoder thread |
42 // can assume |*this| is still alive. See |weak_this_| below for more details. | 42 // can assume |*this| is still alive. See |weak_this_| below for more details. |
43 class CONTENT_EXPORT VaapiVideoDecodeAccelerator | 43 class CONTENT_EXPORT VaapiVideoDecodeAccelerator |
44 : public media::VideoDecodeAccelerator { | 44 : public media::VideoDecodeAccelerator { |
45 public: | 45 public: |
46 VaapiVideoDecodeAccelerator( | 46 VaapiVideoDecodeAccelerator( |
| 47 Display* x_display, |
47 const base::Callback<bool(void)>& make_context_current); | 48 const base::Callback<bool(void)>& make_context_current); |
48 virtual ~VaapiVideoDecodeAccelerator(); | 49 virtual ~VaapiVideoDecodeAccelerator(); |
49 | 50 |
50 // media::VideoDecodeAccelerator implementation. | 51 // media::VideoDecodeAccelerator implementation. |
51 virtual bool Initialize(media::VideoCodecProfile profile, | 52 virtual bool Initialize(media::VideoCodecProfile profile, |
52 Client* client) override; | 53 Client* client) override; |
53 virtual void Decode(const media::BitstreamBuffer& bitstream_buffer) override; | 54 virtual void Decode(const media::BitstreamBuffer& bitstream_buffer) override; |
54 virtual void AssignPictureBuffers( | 55 virtual void AssignPictureBuffers( |
55 const std::vector<media::PictureBuffer>& buffers) override; | 56 const std::vector<media::PictureBuffer>& buffers) override; |
56 virtual void ReusePictureBuffer(int32 picture_buffer_id) override; | 57 virtual void ReusePictureBuffer(int32 picture_buffer_id) override; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 void Cleanup(); | 116 void Cleanup(); |
116 | 117 |
117 // Get a usable framebuffer configuration for use in binding textures | 118 // Get a usable framebuffer configuration for use in binding textures |
118 // or return false on failure. | 119 // or return false on failure. |
119 bool InitializeFBConfig(); | 120 bool InitializeFBConfig(); |
120 | 121 |
121 // Callback for the decoder to execute when it wants us to output given | 122 // Callback for the decoder to execute when it wants us to output given |
122 // |va_surface|. | 123 // |va_surface|. |
123 void SurfaceReady(int32 input_id, const scoped_refptr<VASurface>& va_surface); | 124 void SurfaceReady(int32 input_id, const scoped_refptr<VASurface>& va_surface); |
124 | 125 |
| 126 // Represents a texture bound to an X Pixmap for output purposes. |
| 127 class TFPPicture; |
| 128 |
125 // Callback to be executed once we have a |va_surface| to be output and | 129 // Callback to be executed once we have a |va_surface| to be output and |
126 // an available |picture| to use for output. | 130 // an available |tfp_picture| to use for output. |
127 // Puts contents of |va_surface| into given |picture|, releases the | 131 // Puts contents of |va_surface| into given |tfp_picture|, releases the |
128 // surface and passes the resulting picture to client for output. | 132 // surface and passes the resulting picture to client for output. |
129 void OutputPicture(const scoped_refptr<VASurface>& va_surface, | 133 void OutputPicture(const scoped_refptr<VASurface>& va_surface, |
130 int32 input_id, | 134 int32 input_id, |
131 VaapiPicture* picture); | 135 TFPPicture* tfp_picture); |
132 | 136 |
133 // Try to OutputPicture() if we have both a ready surface and picture. | 137 // Try to OutputPicture() if we have both a ready surface and picture. |
134 void TryOutputSurface(); | 138 void TryOutputSurface(); |
135 | 139 |
136 // Called when a VASurface is no longer in use by the decoder or is not being | 140 // Called when a VASurface is no longer in use by the decoder or is not being |
137 // synced/waiting to be synced to a picture. Returns it to available surfaces | 141 // synced/waiting to be synced to a picture. Returns it to available surfaces |
138 // pool. | 142 // pool. |
139 void RecycleVASurfaceID(VASurfaceID va_surface_id); | 143 void RecycleVASurfaceID(VASurfaceID va_surface_id); |
140 | 144 |
141 // Initiate wait cycle for surfaces to be released before we release them | 145 // Initiate wait cycle for surfaces to be released before we release them |
142 // and allocate new ones, as requested by the decoder. | 146 // and allocate new ones, as requested by the decoder. |
143 void InitiateSurfaceSetChange(size_t num_pics, gfx::Size size); | 147 void InitiateSurfaceSetChange(size_t num_pics, gfx::Size size); |
144 // Check if the surfaces have been released or post ourselves for later. | 148 // Check if the surfaces have been released or post ourselves for later. |
145 void TryFinishSurfaceSetChange(); | 149 void TryFinishSurfaceSetChange(); |
146 | 150 |
147 // Client-provided GL state. | 151 // Client-provided X/GLX state. |
| 152 Display* x_display_; |
148 base::Callback<bool(void)> make_context_current_; | 153 base::Callback<bool(void)> make_context_current_; |
| 154 GLXFBConfig fb_config_; |
149 | 155 |
150 // VAVDA state. | 156 // VAVDA state. |
151 enum State { | 157 enum State { |
152 // Initialize() not called yet or failed. | 158 // Initialize() not called yet or failed. |
153 kUninitialized, | 159 kUninitialized, |
154 // DecodeTask running. | 160 // DecodeTask running. |
155 kDecoding, | 161 kDecoding, |
156 // Resetting, waiting for decoder to finish current task and cleanup. | 162 // Resetting, waiting for decoder to finish current task and cleanup. |
157 kResetting, | 163 kResetting, |
158 // Flushing, waiting for decoder to finish current task and cleanup. | 164 // Flushing, waiting for decoder to finish current task and cleanup. |
(...skipping 24 matching lines...) Expand all Loading... |
183 // Signalled when input buffers are queued onto the input_buffers_ queue. | 189 // Signalled when input buffers are queued onto the input_buffers_ queue. |
184 base::ConditionVariable input_ready_; | 190 base::ConditionVariable input_ready_; |
185 | 191 |
186 // Current input buffer at decoder. | 192 // Current input buffer at decoder. |
187 linked_ptr<InputBuffer> curr_input_buffer_; | 193 linked_ptr<InputBuffer> curr_input_buffer_; |
188 | 194 |
189 // Queue for incoming output buffers (texture ids). | 195 // Queue for incoming output buffers (texture ids). |
190 typedef std::queue<int32> OutputBuffers; | 196 typedef std::queue<int32> OutputBuffers; |
191 OutputBuffers output_buffers_; | 197 OutputBuffers output_buffers_; |
192 | 198 |
193 typedef std::map<int32, linked_ptr<VaapiPicture>> Pictures; | 199 typedef std::map<int32, linked_ptr<TFPPicture> > TFPPictures; |
194 // All allocated Pictures, regardless of their current state. | 200 // All allocated TFPPictures, regardless of their current state. TFPPictures |
195 // Pictures are allocated once and destroyed at the end of decode. | 201 // are allocated once and destroyed at the end of decode. |
196 Pictures pictures_; | 202 TFPPictures tfp_pictures_; |
197 | 203 |
198 // Return a TFPPicture associated with given client-provided id. | 204 // Return a TFPPicture associated with given client-provided id. |
199 VaapiPicture* PictureById(int32 picture_buffer_id); | 205 TFPPicture* TFPPictureById(int32 picture_buffer_id); |
200 | 206 |
201 // VA Surfaces no longer in use that can be passed back to the decoder for | 207 // VA Surfaces no longer in use that can be passed back to the decoder for |
202 // reuse, once it requests them. | 208 // reuse, once it requests them. |
203 std::list<VASurfaceID> available_va_surfaces_; | 209 std::list<VASurfaceID> available_va_surfaces_; |
204 // Signalled when output surfaces are queued onto the available_va_surfaces_ | 210 // Signalled when output surfaces are queued onto the available_va_surfaces_ |
205 // queue. | 211 // queue. |
206 base::ConditionVariable surfaces_available_; | 212 base::ConditionVariable surfaces_available_; |
207 | 213 |
208 // Pending output requests from the decoder. When it indicates that we should | 214 // Pending output requests from the decoder. When it indicates that we should |
209 // output a surface and we have an available Picture (i.e. texture) ready | 215 // output a surface and we have an available TFPPicture (i.e. texture) ready |
210 // to use, we'll execute the callback passing the Picture. The callback | 216 // to use, we'll execute the callback passing the TFPPicture. The callback |
211 // will put the contents of the surface into the picture and return it to | 217 // will put the contents of the surface into the picture and return it to |
212 // the client, releasing the surface as well. | 218 // the client, releasing the surface as well. |
213 // If we don't have any available Pictures at the time when the decoder | 219 // If we don't have any available TFPPictures at the time when the decoder |
214 // requests output, we'll store the request on pending_output_cbs_ queue for | 220 // requests output, we'll store the request on pending_output_cbs_ queue for |
215 // later and run it once the client gives us more textures | 221 // later and run it once the client gives us more textures |
216 // via ReusePictureBuffer(). | 222 // via ReusePictureBuffer(). |
217 typedef base::Callback<void(VaapiPicture*)> OutputCB; | 223 typedef base::Callback<void(TFPPicture*)> OutputCB; |
218 std::queue<OutputCB> pending_output_cbs_; | 224 std::queue<OutputCB> pending_output_cbs_; |
219 | 225 |
220 // ChildThread's message loop | 226 // ChildThread's message loop |
221 base::MessageLoop* message_loop_; | 227 base::MessageLoop* message_loop_; |
222 | 228 |
223 // WeakPtr<> pointing to |this| for use in posting tasks from the decoder | 229 // WeakPtr<> pointing to |this| for use in posting tasks from the decoder |
224 // thread back to the ChildThread. Because the decoder thread is a member of | 230 // thread back to the ChildThread. Because the decoder thread is a member of |
225 // this class, any task running on the decoder thread is guaranteed that this | 231 // this class, any task running on the decoder thread is guaranteed that this |
226 // object is still alive. As a result, tasks posted from ChildThread to | 232 // object is still alive. As a result, tasks posted from ChildThread to |
227 // decoder thread should use base::Unretained(this), and tasks posted from the | 233 // decoder thread should use base::Unretained(this), and tasks posted from the |
228 // decoder thread to the ChildThread should use |weak_this_|. | 234 // decoder thread to the ChildThread should use |weak_this_|. |
229 base::WeakPtr<VaapiVideoDecodeAccelerator> weak_this_; | 235 base::WeakPtr<VaapiVideoDecodeAccelerator> weak_this_; |
230 | 236 |
231 // Callback used when creating VASurface objects. | 237 // Callback used when creating VASurface objects. |
232 VASurface::ReleaseCB va_surface_release_cb_; | 238 VASurface::ReleaseCB va_surface_release_cb_; |
233 | 239 |
234 // To expose client callbacks from VideoDecodeAccelerator. | 240 // To expose client callbacks from VideoDecodeAccelerator. |
235 // NOTE: all calls to these objects *MUST* be executed on message_loop_. | 241 // NOTE: all calls to these objects *MUST* be executed on message_loop_. |
236 scoped_ptr<base::WeakPtrFactory<Client> > client_ptr_factory_; | 242 scoped_ptr<base::WeakPtrFactory<Client> > client_ptr_factory_; |
237 base::WeakPtr<Client> client_; | 243 base::WeakPtr<Client> client_; |
238 | 244 |
239 scoped_refptr<VaapiWrapper> vaapi_wrapper_; | 245 scoped_ptr<VaapiWrapper> vaapi_wrapper_; |
240 | 246 |
241 // Comes after vaapi_wrapper_ to ensure its destructor is executed before | 247 // Comes after vaapi_wrapper_ to ensure its destructor is executed before |
242 // vaapi_wrapper_ is destroyed. | 248 // vaapi_wrapper_ is destroyed. |
243 scoped_ptr<VaapiH264Decoder> decoder_; | 249 scoped_ptr<VaapiH264Decoder> decoder_; |
244 base::Thread decoder_thread_; | 250 base::Thread decoder_thread_; |
245 // Use this to post tasks to |decoder_thread_| instead of | 251 // Use this to post tasks to |decoder_thread_| instead of |
246 // |decoder_thread_.message_loop()| because the latter will be NULL once | 252 // |decoder_thread_.message_loop()| because the latter will be NULL once |
247 // |decoder_thread_.Stop()| returns. | 253 // |decoder_thread_.Stop()| returns. |
248 scoped_refptr<base::MessageLoopProxy> decoder_thread_proxy_; | 254 scoped_refptr<base::MessageLoopProxy> decoder_thread_proxy_; |
249 | 255 |
(...skipping 14 matching lines...) Expand all Loading... |
264 | 270 |
265 // The WeakPtrFactory for |weak_this_|. | 271 // The WeakPtrFactory for |weak_this_|. |
266 base::WeakPtrFactory<VaapiVideoDecodeAccelerator> weak_this_factory_; | 272 base::WeakPtrFactory<VaapiVideoDecodeAccelerator> weak_this_factory_; |
267 | 273 |
268 DISALLOW_COPY_AND_ASSIGN(VaapiVideoDecodeAccelerator); | 274 DISALLOW_COPY_AND_ASSIGN(VaapiVideoDecodeAccelerator); |
269 }; | 275 }; |
270 | 276 |
271 } // namespace content | 277 } // namespace content |
272 | 278 |
273 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_DECODE_ACCELERATOR_H_ | 279 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_DECODE_ACCELERATOR_H_ |
OLD | NEW |