Chromium Code Reviews| 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 // Check if |va_profile| supports |required_attribs| or not. |va_lock_| must | |
|
Pawel Osciak
2015/03/10 03:41:14
Return true if |va_profile| for |entrypoint| with
henryhsu
2015/03/10 05:36:53
Done.
| |
| 219 // 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 of |va_profile|. If return value is true, | |
|
Pawel Osciak
2015/03/10 03:41:14
s/of |va_profile|/for |va_profile| and |entrypoint
henryhsu
2015/03/10 05:36:53
Done.
| |
| 226 // |resolution| is the maximum resolution. |va_lock_| must be held on entry. | |
| 227 bool GetMaxResolution_Locked( | |
| 228 VAProfile va_profile, | |
| 229 VAEntrypoint entrypoint, | |
| 230 std::vector<VAConfigAttrib>& required_attribs, | |
|
wuchengli
2015/03/09 11:14:53
add const
henryhsu
2015/03/10 05:36:53
Done.
wuchengli
2015/03/10 05:55:26
Discussed with Henry. vaCreateConfig needs non-con
| |
| 231 gfx::Size* resolution); | |
| 196 | 232 |
| 197 // Destroys a |va_surface| created using CreateUnownedSurface. | 233 // Destroys a |va_surface| created using CreateUnownedSurface. |
| 198 void DestroyUnownedSurface(VASurfaceID va_surface_id); | 234 void DestroyUnownedSurface(VASurfaceID va_surface_id); |
| 199 | 235 |
| 200 // Initialize the video post processing context with the |size| of | 236 // Initialize the video post processing context with the |size| of |
| 201 // the input pictures to be processed. | 237 // the input pictures to be processed. |
| 202 bool InitializeVpp_Locked(); | 238 bool InitializeVpp_Locked(); |
| 203 | 239 |
| 204 // Deinitialize the video post processing context. | 240 // Deinitialize the video post processing context. |
| 205 void DeinitializeVpp(); | 241 void DeinitializeVpp(); |
| 206 | 242 |
| 207 // Execute pending job in hardware and destroy pending buffers. Return false | 243 // Execute pending job in hardware and destroy pending buffers. Return false |
| 208 // if vaapi driver refuses to accept parameter or slice buffers submitted | 244 // if vaapi driver refuses to accept parameter or slice buffers submitted |
| 209 // by client, or if execution fails in hardware. | 245 // by client, or if execution fails in hardware. |
| 210 bool Execute(VASurfaceID va_surface_id); | 246 bool Execute(VASurfaceID va_surface_id); |
| 211 | 247 |
| 212 // Attempt to set render mode to "render to texture.". Failure is non-fatal. | 248 // Attempt to set render mode to "render to texture.". Failure is non-fatal. |
| 213 void TryToSetVADisplayAttributeToLocalGPU(); | 249 void TryToSetVADisplayAttributeToLocalGPU(); |
| 214 | 250 |
| 251 // Get supported profile infos for |mode|. | |
| 252 std::vector<ProfileInfo> GetSupportedProfileInfosForCodecModeInternal( | |
| 253 CodecMode mode); | |
| 254 | |
| 215 // Lazily initialize static data after sandbox is enabled. Return false on | 255 // Lazily initialize static data after sandbox is enabled. Return false on |
| 216 // init failure. | 256 // init failure. |
| 217 static bool PostSandboxInitialization(); | 257 static bool PostSandboxInitialization(); |
| 218 | 258 |
| 259 // Map VideoCodecProfile enum values to VaProfile values. This function | |
| 260 // includes a workaround for crbug.com/345569. If va_profile is h264 baseline | |
| 261 // and it is not supported, we try constrained baseline. | |
| 262 static VAProfile ProfileToVAProfile(media::VideoCodecProfile profile, | |
| 263 CodecMode mode); | |
| 264 | |
| 219 // Libva is not thread safe, so we have to do locking for it ourselves. | 265 // 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 | 266 // This lock is to be taken for the duration of all VA-API calls and for |
| 221 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers(). | 267 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers(). |
| 222 base::Lock va_lock_; | 268 base::Lock va_lock_; |
| 223 | 269 |
| 224 // Allocated ids for VASurfaces. | 270 // Allocated ids for VASurfaces. |
| 225 std::vector<VASurfaceID> va_surface_ids_; | 271 std::vector<VASurfaceID> va_surface_ids_; |
| 226 | 272 |
| 227 // The VAAPI version. | 273 // The VAAPI version. |
| 228 int major_version_, minor_version_; | 274 int major_version_, minor_version_; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 248 // return values from public methods. | 294 // return values from public methods. |
| 249 base::Closure report_error_to_uma_cb_; | 295 base::Closure report_error_to_uma_cb_; |
| 250 | 296 |
| 251 // VPP (Video Post Processing) context, this is used to convert | 297 // VPP (Video Post Processing) context, this is used to convert |
| 252 // pictures used by the decoder to RGBA pictures usable by GL or the | 298 // pictures used by the decoder to RGBA pictures usable by GL or the |
| 253 // display hardware. | 299 // display hardware. |
| 254 VAConfigID va_vpp_config_id_; | 300 VAConfigID va_vpp_config_id_; |
| 255 VAContextID va_vpp_context_id_; | 301 VAContextID va_vpp_context_id_; |
| 256 VABufferID va_vpp_buffer_id_; | 302 VABufferID va_vpp_buffer_id_; |
| 257 | 303 |
| 304 // Singleton variable to store supported profile information for encode and | |
| 305 // decode. | |
| 306 static base::LazyInstance<LazyProfileInfos> profile_infos_; | |
| 307 | |
| 258 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper); | 308 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper); |
| 259 }; | 309 }; |
| 260 | 310 |
| 261 } // namespace content | 311 } // namespace content |
| 262 | 312 |
| 263 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ | 313 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ |
| OLD | NEW |