OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 VaapiWrapper, used by | 5 // This file contains an implementation of VaapiWrapper, used by |
6 // VaapiVideoDecodeAccelerator and VaapiH264Decoder for decode, | 6 // VaapiVideoDecodeAccelerator and VaapiH264Decoder for decode, |
7 // and VaapiVideoEncodeAccelerator for encode, to interface | 7 // and VaapiVideoEncodeAccelerator for encode, to interface |
8 // with libva (VA-API library for hardware video codec). | 8 // with libva (VA-API library for hardware video codec). |
9 | 9 |
10 #ifndef CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ | 10 #ifndef CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ |
11 #define CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ | 11 #define CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ |
12 | 12 |
13 #include <set> | 13 #include <set> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
| 16 #include "base/lazy_instance.h" |
16 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
17 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
18 #include "content/common/content_export.h" | 19 #include "content/common/content_export.h" |
19 #include "content/common/gpu/media/va_surface.h" | 20 #include "content/common/gpu/media/va_surface.h" |
20 #include "media/base/video_decoder_config.h" | 21 #include "media/base/video_decoder_config.h" |
21 #include "media/base/video_frame.h" | 22 #include "media/base/video_frame.h" |
| 23 #include "media/video/video_encode_accelerator.h" |
22 #include "third_party/libva/va/va.h" | 24 #include "third_party/libva/va/va.h" |
23 #include "third_party/libva/va/va_vpp.h" | 25 #include "third_party/libva/va/va_vpp.h" |
24 #include "ui/gfx/geometry/size.h" | 26 #include "ui/gfx/geometry/size.h" |
25 #if defined(USE_X11) | 27 #if defined(USE_X11) |
26 #include "third_party/libva/va/va_x11.h" | 28 #include "third_party/libva/va/va_x11.h" |
27 #endif // USE_X11 | 29 #endif // USE_X11 |
28 | 30 |
29 namespace content { | 31 namespace content { |
30 | 32 |
31 // This class handles VA-API calls and ensures proper locking of VA-API calls | 33 // This class handles VA-API calls and ensures proper locking of VA-API calls |
32 // to libva, the userspace shim to the HW codec driver. libva is not | 34 // to libva, the userspace shim to the HW codec driver. libva is not |
33 // thread-safe, so we have to perform locking ourselves. This class is fully | 35 // thread-safe, so we have to perform locking ourselves. This class is fully |
34 // synchronous and its methods can be called from any thread and may wait on | 36 // synchronous and its methods can be called from any thread and may wait on |
35 // the va_lock_ while other, concurrent calls run. | 37 // the va_lock_ while other, concurrent calls run. |
36 // | 38 // |
37 // This class is responsible for managing VAAPI connection, contexts and state. | 39 // This class is responsible for managing VAAPI connection, contexts and state. |
38 // It is also responsible for managing and freeing VABuffers (not VASurfaces), | 40 // It is also responsible for managing and freeing VABuffers (not VASurfaces), |
39 // which are used to queue parameters and slice data to the HW codec, | 41 // which are used to queue parameters and slice data to the HW codec, |
40 // as well as underlying memory for VASurfaces themselves. | 42 // as well as underlying memory for VASurfaces themselves. |
41 class CONTENT_EXPORT VaapiWrapper { | 43 class CONTENT_EXPORT VaapiWrapper { |
42 public: | 44 public: |
43 enum CodecMode { | 45 enum CodecMode { |
44 kDecode, | 46 kDecode, |
45 kEncode, | 47 kEncode, |
| 48 kCodecModeMax, |
46 }; | 49 }; |
47 | 50 |
48 // Create VaapiWrapper for VAProfile. | 51 // Return an instance of VaapiWrapper initialized for |va_profile| and |
49 // |report_error_to_uma_cb| will be called independently from reporting | 52 // |mode|. |report_error_to_uma_cb| will be called independently from |
50 // errors to clients via method return values. | 53 // reporting errors to clients via method return values. |
51 static scoped_ptr<VaapiWrapper> Create( | 54 static scoped_ptr<VaapiWrapper> Create( |
52 CodecMode mode, | 55 CodecMode mode, |
53 VAProfile profile, | 56 VAProfile va_profile, |
54 const base::Closure& report_error_to_uma_cb); | 57 const base::Closure& report_error_to_uma_cb); |
55 | 58 |
56 // Create VaapiWrapper for VideoCodecProfile. It maps VideoCodecProfile | 59 // Create VaapiWrapper for VideoCodecProfile. It maps VideoCodecProfile |
57 // |profile| to VAProfile. | 60 // |profile| to VAProfile. |
58 // |report_error_to_uma_cb| will be called independently from reporting | 61 // |report_error_to_uma_cb| will be called independently from reporting |
59 // errors to clients via method return values. | 62 // errors to clients via method return values. |
60 static scoped_ptr<VaapiWrapper> CreateForVideoCodec( | 63 static scoped_ptr<VaapiWrapper> CreateForVideoCodec( |
61 CodecMode mode, | 64 CodecMode mode, |
62 media::VideoCodecProfile profile, | 65 media::VideoCodecProfile profile, |
63 const base::Closure& report_error_to_uma_cb); | 66 const base::Closure& report_error_to_uma_cb); |
64 | 67 |
65 // Return the supported encode profiles. | 68 // Return the supported encode profiles. |
66 static std::vector<media::VideoCodecProfile> GetSupportedEncodeProfiles( | 69 static std::vector<media::VideoEncodeAccelerator::SupportedProfile> |
67 const base::Closure& report_error_to_uma_cb); | 70 GetSupportedEncodeProfiles(); |
68 | 71 |
69 ~VaapiWrapper(); | 72 ~VaapiWrapper(); |
70 | 73 |
71 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each | 74 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each |
72 // of size |size|. Returns true when successful, with the created IDs in | 75 // of size |size|. Returns true when successful, with the created IDs in |
73 // |va_surfaces| to be managed and later wrapped in VASurfaces. | 76 // |va_surfaces| to be managed and later wrapped in VASurfaces. |
74 // The client must DestroySurfaces() each time before calling this method | 77 // The client must DestroySurfaces() each time before calling this method |
75 // again to free the allocated surfaces first, but is not required to do so | 78 // again to free the allocated surfaces first, but is not required to do so |
76 // at destruction time, as this will be done automatically from | 79 // at destruction time, as this will be done automatically from |
77 // the destructor. | 80 // the destructor. |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 | 179 |
177 // Blits a VASurface |va_surface_id_src| into another VASurface | 180 // Blits a VASurface |va_surface_id_src| into another VASurface |
178 // |va_surface_id_dest| applying pixel format conversion and scaling | 181 // |va_surface_id_dest| applying pixel format conversion and scaling |
179 // if needed. | 182 // if needed. |
180 bool BlitSurface(VASurfaceID va_surface_id_src, | 183 bool BlitSurface(VASurfaceID va_surface_id_src, |
181 const gfx::Size& src_size, | 184 const gfx::Size& src_size, |
182 VASurfaceID va_surface_id_dest, | 185 VASurfaceID va_surface_id_dest, |
183 const gfx::Size& dest_size); | 186 const gfx::Size& dest_size); |
184 | 187 |
185 private: | 188 private: |
| 189 struct ProfileInfo { |
| 190 VAProfile va_profile; |
| 191 gfx::Size max_resolution; |
| 192 }; |
| 193 |
| 194 class LazyProfileInfos { |
| 195 public: |
| 196 LazyProfileInfos(); |
| 197 ~LazyProfileInfos(); |
| 198 std::vector<ProfileInfo> GetSupportedProfileInfosForCodecMode( |
| 199 CodecMode mode); |
| 200 bool IsProfileSupported(CodecMode mode, VAProfile va_profile); |
| 201 |
| 202 private: |
| 203 std::vector<ProfileInfo> supported_profiles_[kCodecModeMax]; |
| 204 }; |
| 205 |
186 VaapiWrapper(); | 206 VaapiWrapper(); |
187 | 207 |
188 bool Initialize(CodecMode mode, VAProfile va_profile); | 208 bool Initialize(CodecMode mode, VAProfile va_profile); |
189 void Deinitialize(); | 209 void Deinitialize(); |
190 bool VaInitialize(const base::Closure& report_error_to_uma_cb); | 210 bool VaInitialize(const base::Closure& report_error_to_uma_cb); |
191 bool GetSupportedVaProfiles(std::vector<VAProfile>* profiles); | 211 bool GetSupportedVaProfiles(std::vector<VAProfile>* profiles); |
192 bool IsEntrypointSupported(VAProfile va_profile, VAEntrypoint entrypoint); | 212 |
193 bool AreAttribsSupported(VAProfile va_profile, | 213 // Check if |va_profile| supports |entrypoint| or not. |va_lock_| must be |
194 VAEntrypoint entrypoint, | 214 // held on entry. |
195 const std::vector<VAConfigAttrib>& required_attribs); | 215 bool IsEntrypointSupported_Locked(VAProfile va_profile, |
| 216 VAEntrypoint entrypoint); |
| 217 |
| 218 // Return true if |va_profile| for |entrypoint| with |required_attribs| is |
| 219 // supported. |va_lock_| must be held on entry. |
| 220 bool AreAttribsSupported_Locked( |
| 221 VAProfile va_profile, |
| 222 VAEntrypoint entrypoint, |
| 223 const std::vector<VAConfigAttrib>& required_attribs); |
| 224 |
| 225 // Get maximum resolution for |va_profile| and |entrypoint| with |
| 226 // |required_attribs|. If return value is true, |resolution| is the maximum |
| 227 // resolution. |va_lock_| must be held on entry. |
| 228 bool GetMaxResolution_Locked( |
| 229 VAProfile va_profile, |
| 230 VAEntrypoint entrypoint, |
| 231 std::vector<VAConfigAttrib>& required_attribs, |
| 232 gfx::Size* resolution); |
196 | 233 |
197 // Destroys a |va_surface| created using CreateUnownedSurface. | 234 // Destroys a |va_surface| created using CreateUnownedSurface. |
198 void DestroyUnownedSurface(VASurfaceID va_surface_id); | 235 void DestroyUnownedSurface(VASurfaceID va_surface_id); |
199 | 236 |
200 // Initialize the video post processing context with the |size| of | 237 // Initialize the video post processing context with the |size| of |
201 // the input pictures to be processed. | 238 // the input pictures to be processed. |
202 bool InitializeVpp_Locked(); | 239 bool InitializeVpp_Locked(); |
203 | 240 |
204 // Deinitialize the video post processing context. | 241 // Deinitialize the video post processing context. |
205 void DeinitializeVpp(); | 242 void DeinitializeVpp(); |
206 | 243 |
207 // Execute pending job in hardware and destroy pending buffers. Return false | 244 // Execute pending job in hardware and destroy pending buffers. Return false |
208 // if vaapi driver refuses to accept parameter or slice buffers submitted | 245 // if vaapi driver refuses to accept parameter or slice buffers submitted |
209 // by client, or if execution fails in hardware. | 246 // by client, or if execution fails in hardware. |
210 bool Execute(VASurfaceID va_surface_id); | 247 bool Execute(VASurfaceID va_surface_id); |
211 | 248 |
212 // Attempt to set render mode to "render to texture.". Failure is non-fatal. | 249 // Attempt to set render mode to "render to texture.". Failure is non-fatal. |
213 void TryToSetVADisplayAttributeToLocalGPU(); | 250 void TryToSetVADisplayAttributeToLocalGPU(); |
214 | 251 |
| 252 // Get supported profile infos for |mode|. |
| 253 std::vector<ProfileInfo> GetSupportedProfileInfosForCodecModeInternal( |
| 254 CodecMode mode); |
| 255 |
215 // Lazily initialize static data after sandbox is enabled. Return false on | 256 // Lazily initialize static data after sandbox is enabled. Return false on |
216 // init failure. | 257 // init failure. |
217 static bool PostSandboxInitialization(); | 258 static bool PostSandboxInitialization(); |
218 | 259 |
| 260 // Map VideoCodecProfile enum values to VaProfile values. This function |
| 261 // includes a workaround for crbug.com/345569. If va_profile is h264 baseline |
| 262 // and it is not supported, we try constrained baseline. |
| 263 static VAProfile ProfileToVAProfile(media::VideoCodecProfile profile, |
| 264 CodecMode mode); |
| 265 |
219 // Libva is not thread safe, so we have to do locking for it ourselves. | 266 // Libva is not thread safe, so we have to do locking for it ourselves. |
220 // This lock is to be taken for the duration of all VA-API calls and for | 267 // This lock is to be taken for the duration of all VA-API calls and for |
221 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers(). | 268 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers(). |
222 base::Lock va_lock_; | 269 base::Lock va_lock_; |
223 | 270 |
224 // Allocated ids for VASurfaces. | 271 // Allocated ids for VASurfaces. |
225 std::vector<VASurfaceID> va_surface_ids_; | 272 std::vector<VASurfaceID> va_surface_ids_; |
226 | 273 |
227 // The VAAPI version. | 274 // The VAAPI version. |
228 int major_version_, minor_version_; | 275 int major_version_, minor_version_; |
(...skipping 19 matching lines...) Expand all Loading... |
248 // return values from public methods. | 295 // return values from public methods. |
249 base::Closure report_error_to_uma_cb_; | 296 base::Closure report_error_to_uma_cb_; |
250 | 297 |
251 // VPP (Video Post Processing) context, this is used to convert | 298 // VPP (Video Post Processing) context, this is used to convert |
252 // pictures used by the decoder to RGBA pictures usable by GL or the | 299 // pictures used by the decoder to RGBA pictures usable by GL or the |
253 // display hardware. | 300 // display hardware. |
254 VAConfigID va_vpp_config_id_; | 301 VAConfigID va_vpp_config_id_; |
255 VAContextID va_vpp_context_id_; | 302 VAContextID va_vpp_context_id_; |
256 VABufferID va_vpp_buffer_id_; | 303 VABufferID va_vpp_buffer_id_; |
257 | 304 |
| 305 // Singleton variable to store supported profile information for encode and |
| 306 // decode. |
| 307 static base::LazyInstance<LazyProfileInfos> profile_infos_; |
| 308 |
258 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper); | 309 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper); |
259 }; | 310 }; |
260 | 311 |
261 } // namespace content | 312 } // namespace content |
262 | 313 |
263 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ | 314 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ |
OLD | NEW |