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
3 // found in the LICENSE file.
5 // This file contains an implementation of VaapiWrapper, used by
6 // VaapiVideoDecodeAccelerator and VaapiH264Decoder for decode,
7 // and VaapiVideoEncodeAccelerator for encode, to interface
8 // with libva (VA-API library for hardware video codec).
10 #ifndef CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_
11 #define CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_
16 #include "base/files/file.h"
17 #include "base/lazy_instance.h"
18 #include "base/memory/ref_counted.h"
19 #include "base/synchronization/lock.h"
20 #include "content/common/content_export.h"
21 #include "content/common/gpu/media/va_surface.h"
22 #include "media/base/video_decoder_config.h"
23 #include "media/base/video_frame.h"
24 #include "media/video/video_decode_accelerator.h"
25 #include "media/video/video_encode_accelerator.h"
26 #include "third_party/libva/va/va.h"
27 #include "third_party/libva/va/va_vpp.h"
28 #include "ui/gfx/geometry/size.h"
30 #include "third_party/libva/va/va_x11.h"
35 // This class handles VA-API calls and ensures proper locking of VA-API calls
36 // to libva, the userspace shim to the HW codec driver. libva is not
37 // thread-safe, so we have to perform locking ourselves. This class is fully
38 // synchronous and its methods can be called from any thread and may wait on
39 // the va_lock_ while other, concurrent calls run.
41 // This class is responsible for managing VAAPI connection, contexts and state.
42 // It is also responsible for managing and freeing VABuffers (not VASurfaces),
43 // which are used to queue parameters and slice data to the HW codec,
44 // as well as underlying memory for VASurfaces themselves.
45 class CONTENT_EXPORT VaapiWrapper
{
53 // Return an instance of VaapiWrapper initialized for |va_profile| and
54 // |mode|. |report_error_to_uma_cb| will be called independently from
55 // reporting errors to clients via method return values.
56 static scoped_ptr
<VaapiWrapper
> Create(
59 const base::Closure
& report_error_to_uma_cb
);
61 // Create VaapiWrapper for VideoCodecProfile. It maps VideoCodecProfile
62 // |profile| to VAProfile.
63 // |report_error_to_uma_cb| will be called independently from reporting
64 // errors to clients via method return values.
65 static scoped_ptr
<VaapiWrapper
> CreateForVideoCodec(
67 media::VideoCodecProfile profile
,
68 const base::Closure
& report_error_to_uma_cb
);
70 // Return the supported encode profiles.
71 static media::VideoEncodeAccelerator::SupportedProfiles
72 GetSupportedEncodeProfiles();
74 // Return the supported decode profiles.
75 static media::VideoDecodeAccelerator::SupportedProfiles
76 GetSupportedDecodeProfiles();
80 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each
81 // of size |size|. Returns true when successful, with the created IDs in
82 // |va_surfaces| to be managed and later wrapped in VASurfaces.
83 // The client must DestroySurfaces() each time before calling this method
84 // again to free the allocated surfaces first, but is not required to do so
85 // at destruction time, as this will be done automatically from
87 bool CreateSurfaces(const gfx::Size
& size
,
89 std::vector
<VASurfaceID
>* va_surfaces
);
91 // Free all memory allocated in CreateSurfaces.
92 void DestroySurfaces();
94 // Create a VASurface of |va_format|, |size| and using |va_attribs|
95 // attributes. The ownership of the surface is transferred to the
96 // caller. It differs from surfaces created using CreateSurfaces(),
97 // where VaapiWrapper is the owner of the surfaces.
98 scoped_refptr
<VASurface
> CreateUnownedSurface(
99 unsigned int va_format
,
100 const gfx::Size
& size
,
101 const std::vector
<VASurfaceAttrib
>& va_attribs
);
103 // Submit parameters or slice data of |va_buffer_type|, copying them from
104 // |buffer| of size |size|, into HW codec. The data in |buffer| is no
105 // longer needed and can be freed after this method returns.
106 // Data submitted via this method awaits in the HW codec until
107 // ExecuteAndDestroyPendingBuffers() is called to execute or
108 // DestroyPendingBuffers() is used to cancel a pending job.
109 bool SubmitBuffer(VABufferType va_buffer_type
, size_t size
, void* buffer
);
111 // Submit a VAEncMiscParameterBuffer of given |misc_param_type|, copying its
112 // data from |buffer| of size |size|, into HW codec. The data in |buffer| is
113 // no longer needed and can be freed after this method returns.
114 // Data submitted via this method awaits in the HW codec until
115 // ExecuteAndDestroyPendingBuffers() is called to execute or
116 // DestroyPendingBuffers() is used to cancel a pending job.
117 bool SubmitVAEncMiscParamBuffer(VAEncMiscParameterType misc_param_type
,
121 // Cancel and destroy all buffers queued to the HW codec via SubmitBuffer().
122 // Useful when a pending job is to be cancelled (on reset or error).
123 void DestroyPendingBuffers();
125 // Execute job in hardware on target |va_surface_id| and destroy pending
126 // buffers. Return false if Execute() fails.
127 bool ExecuteAndDestroyPendingBuffers(VASurfaceID va_surface_id
);
130 // Put data from |va_surface_id| into |x_pixmap| of size
131 // |dest_size|, converting/scaling to it.
132 bool PutSurfaceIntoPixmap(VASurfaceID va_surface_id
,
134 gfx::Size dest_size
);
137 // Get a VAImage from a VASurface and map it into memory. The size and format
138 // are derived from the surface. Use GetVaImage() instead if |format| or
139 // |size| are different from surface internal representation. The VAImage
140 // should be released using the ReturnVaImage function. Returns true when
142 bool GetDerivedVaImage(VASurfaceID va_surface_id
, VAImage
* image
, void** mem
);
144 // Get a VAImage from a VASurface |va_surface_id| and map it into memory with
145 // given |format| and |size|. The output is |image| and the mapped memory is
146 // |mem|. If |format| doesn't equal to the internal format, the underlying
147 // implementation will do format conversion if supported. |size| should be
148 // smaller than or equal to the surface. If |size| is smaller, the image will
149 // be cropped. The VAImage should be released using the ReturnVaImage
150 // function. Returns true when successful.
151 bool GetVaImage(VASurfaceID va_surface_id
,
152 VAImageFormat
* format
,
153 const gfx::Size
& size
,
157 // Release the VAImage (and the associated memory mapping) obtained from
158 // GetVaImage() or GetDerivedVaImage().
159 void ReturnVaImage(VAImage
* image
);
161 // Upload contents of |frame| into |va_surface_id| for encode.
162 bool UploadVideoFrameToSurface(const scoped_refptr
<media::VideoFrame
>& frame
,
163 VASurfaceID va_surface_id
);
165 // Create a buffer of |size| bytes to be used as encode output.
166 bool CreateCodedBuffer(size_t size
, VABufferID
* buffer_id
);
168 // Download the contents of the buffer with given |buffer_id| into a buffer of
169 // size |target_size|, pointed to by |target_ptr|. The number of bytes
170 // downloaded will be returned in |coded_data_size|. |sync_surface_id| will
171 // be used as a sync point, i.e. it will have to become idle before starting
172 // the download. |sync_surface_id| should be the source surface passed
173 // to the encode job.
174 bool DownloadAndDestroyCodedBuffer(VABufferID buffer_id
,
175 VASurfaceID sync_surface_id
,
178 size_t* coded_data_size
);
180 // Destroy all previously-allocated (and not yet destroyed) coded buffers.
181 void DestroyCodedBuffers();
183 // Blits a VASurface |va_surface_id_src| into another VASurface
184 // |va_surface_id_dest| applying pixel format conversion and scaling
186 bool BlitSurface(VASurfaceID va_surface_id_src
,
187 const gfx::Size
& src_size
,
188 VASurfaceID va_surface_id_dest
,
189 const gfx::Size
& dest_size
);
191 // Initialize static data before sandbox is enabled.
192 static void PreSandboxInitialization();
196 VAProfile va_profile
;
197 gfx::Size max_resolution
;
200 class LazyProfileInfos
{
204 std::vector
<ProfileInfo
> GetSupportedProfileInfosForCodecMode(
206 bool IsProfileSupported(CodecMode mode
, VAProfile va_profile
);
209 std::vector
<ProfileInfo
> supported_profiles_
[kCodecModeMax
];
212 class VADisplayState
{
217 // |va_lock_| must be held on entry.
218 bool Initialize(VAStatus
* status
);
219 void Deinitialize(VAStatus
* status
);
221 base::Lock
* va_lock() { return &va_lock_
; }
222 VADisplay
va_display() const { return va_display_
; }
224 #if defined(USE_OZONE)
225 void SetDrmFd(base::PlatformFile fd
);
229 friend class base::LazyInstance
<VADisplayState
>;
231 // Returns true if the VAAPI version is less than the specified version.
232 bool VAAPIVersionLessThan(int major
, int minor
);
234 // Protected by |va_lock_|.
237 // Libva is not thread safe, so we have to do locking for it ourselves.
238 // This lock is to be taken for the duration of all VA-API calls and for
239 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers().
242 #if defined(USE_OZONE)
243 // Drm fd used to obtain access to the driver interface by VA.
244 base::ScopedFD drm_fd_
;
247 // The VADisplay handle.
248 VADisplay va_display_
;
250 // The VAAPI version.
251 int major_version_
, minor_version_
;
253 // True if vaInitialize has been called successfully.
254 bool va_initialized_
;
259 bool Initialize(CodecMode mode
, VAProfile va_profile
);
261 bool VaInitialize(const base::Closure
& report_error_to_uma_cb
);
262 bool GetSupportedVaProfiles(std::vector
<VAProfile
>* profiles
);
264 // Check if |va_profile| supports |entrypoint| or not. |va_lock_| must be
266 bool IsEntrypointSupported_Locked(VAProfile va_profile
,
267 VAEntrypoint entrypoint
);
269 // Return true if |va_profile| for |entrypoint| with |required_attribs| is
270 // supported. |va_lock_| must be held on entry.
271 bool AreAttribsSupported_Locked(
272 VAProfile va_profile
,
273 VAEntrypoint entrypoint
,
274 const std::vector
<VAConfigAttrib
>& required_attribs
);
276 // Get maximum resolution for |va_profile| and |entrypoint| with
277 // |required_attribs|. If return value is true, |resolution| is the maximum
278 // resolution. |va_lock_| must be held on entry.
279 bool GetMaxResolution_Locked(
280 VAProfile va_profile
,
281 VAEntrypoint entrypoint
,
282 std::vector
<VAConfigAttrib
>& required_attribs
,
283 gfx::Size
* resolution
);
285 // Destroys a |va_surface| created using CreateUnownedSurface.
286 void DestroyUnownedSurface(VASurfaceID va_surface_id
);
288 // Initialize the video post processing context with the |size| of
289 // the input pictures to be processed.
290 bool InitializeVpp_Locked();
292 // Deinitialize the video post processing context.
293 void DeinitializeVpp();
295 // Execute pending job in hardware and destroy pending buffers. Return false
296 // if vaapi driver refuses to accept parameter or slice buffers submitted
297 // by client, or if execution fails in hardware.
298 bool Execute(VASurfaceID va_surface_id
);
300 // Attempt to set render mode to "render to texture.". Failure is non-fatal.
301 void TryToSetVADisplayAttributeToLocalGPU();
303 // Get supported profile infos for |mode|.
304 std::vector
<ProfileInfo
> GetSupportedProfileInfosForCodecModeInternal(
307 // Lazily initialize static data after sandbox is enabled. Return false on
309 static bool PostSandboxInitialization();
311 // Map VideoCodecProfile enum values to VaProfile values. This function
312 // includes a workaround for crbug.com/345569. If va_profile is h264 baseline
313 // and it is not supported, we try constrained baseline.
314 static VAProfile
ProfileToVAProfile(media::VideoCodecProfile profile
,
317 // Pointer to VADisplayState's member |va_lock_|. Guaranteed to be valid for
318 // the lifetime of VaapiWrapper.
319 base::Lock
* va_lock_
;
321 // Allocated ids for VASurfaces.
322 std::vector
<VASurfaceID
> va_surface_ids_
;
324 // Singleton instance of VADisplayState.
325 static base::LazyInstance
<VADisplayState
> va_display_state_
;
328 // All valid after successful Initialize() and until Deinitialize().
329 VADisplay va_display_
;
330 VAConfigID va_config_id_
;
331 // Created for the current set of va_surface_ids_ in CreateSurfaces() and
332 // valid until DestroySurfaces().
333 VAContextID va_context_id_
;
335 // Data queued up for HW codec, to be committed on next execution.
336 std::vector
<VABufferID
> pending_slice_bufs_
;
337 std::vector
<VABufferID
> pending_va_bufs_
;
339 // Bitstream buffers for encode.
340 std::set
<VABufferID
> coded_buffers_
;
342 // Called to report codec errors to UMA. Errors to clients are reported via
343 // return values from public methods.
344 base::Closure report_error_to_uma_cb_
;
346 // VPP (Video Post Processing) context, this is used to convert
347 // pictures used by the decoder to RGBA pictures usable by GL or the
349 VAConfigID va_vpp_config_id_
;
350 VAContextID va_vpp_context_id_
;
351 VABufferID va_vpp_buffer_id_
;
353 // Singleton variable to store supported profile information for encode and
355 static base::LazyInstance
<LazyProfileInfos
> profile_infos_
;
357 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper
);
360 } // namespace content
362 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_