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/memory/ref_counted.h"
17 #include "base/synchronization/lock.h"
18 #include "content/common/content_export.h"
19 #include "content/common/gpu/media/va_surface.h"
20 #include "media/base/video_decoder_config.h"
21 #include "media/base/video_frame.h"
22 #include "third_party/libva/va/va.h"
23 #include "third_party/libva/va/va_vpp.h"
24 #include "ui/gfx/geometry/size.h"
26 #include "third_party/libva/va/va_x11.h"
31 // 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
33 // 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
35 // the va_lock_ while other, concurrent calls run.
37 // This class is responsible for managing VAAPI connection, contexts and state.
38 // 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,
40 // as well as underlying memory for VASurfaces themselves.
41 class CONTENT_EXPORT VaapiWrapper
{
48 // |report_error_to_uma_cb| will be called independently from reporting
49 // errors to clients via method return values.
50 static scoped_ptr
<VaapiWrapper
> Create(
52 media::VideoCodecProfile profile
,
53 const base::Closure
& report_error_to_uma_cb
);
55 // Return the supported encode profiles.
56 static std::vector
<media::VideoCodecProfile
> GetSupportedEncodeProfiles(
57 const base::Closure
& report_error_to_uma_cb
);
61 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each
62 // of size |size|. Returns true when successful, with the created IDs in
63 // |va_surfaces| to be managed and later wrapped in VASurfaces.
64 // The client must DestroySurfaces() each time before calling this method
65 // again to free the allocated surfaces first, but is not required to do so
66 // at destruction time, as this will be done automatically from
68 bool CreateSurfaces(const gfx::Size
& size
,
70 std::vector
<VASurfaceID
>* va_surfaces
);
72 // Free all memory allocated in CreateSurfaces.
73 void DestroySurfaces();
75 // Create a VASurface of |va_format|, |size| and using |va_attribs|
76 // attributes. The ownership of the surface is transferred to the
77 // caller. It differs from surfaces created using CreateSurfaces(),
78 // where VaapiWrapper is the owner of the surfaces.
79 scoped_refptr
<VASurface
> CreateUnownedSurface(
80 unsigned int va_format
,
81 const gfx::Size
& size
,
82 const std::vector
<VASurfaceAttrib
>& va_attribs
);
84 // Submit parameters or slice data of |va_buffer_type|, copying them from
85 // |buffer| of size |size|, into HW codec. The data in |buffer| is no
86 // longer needed and can be freed after this method returns.
87 // Data submitted via this method awaits in the HW codec until
88 // ExecuteAndDestroyPendingBuffers() is called to execute or
89 // DestroyPendingBuffers() is used to cancel a pending job.
90 bool SubmitBuffer(VABufferType va_buffer_type
, size_t size
, void* buffer
);
92 // Submit a VAEncMiscParameterBuffer of given |misc_param_type|, copying its
93 // data from |buffer| of size |size|, into HW codec. The data in |buffer| is
94 // no longer needed and can be freed after this method returns.
95 // Data submitted via this method awaits in the HW codec until
96 // ExecuteAndDestroyPendingBuffers() is called to execute or
97 // DestroyPendingBuffers() is used to cancel a pending job.
98 bool SubmitVAEncMiscParamBuffer(VAEncMiscParameterType misc_param_type
,
102 // Cancel and destroy all buffers queued to the HW codec via SubmitBuffer().
103 // Useful when a pending job is to be cancelled (on reset or error).
104 void DestroyPendingBuffers();
106 // Execute job in hardware on target |va_surface_id| and destroy pending
107 // buffers. Return false if Execute() fails.
108 bool ExecuteAndDestroyPendingBuffers(VASurfaceID va_surface_id
);
111 // Put data from |va_surface_id| into |x_pixmap| of size
112 // |dest_size|, converting/scaling to it.
113 bool PutSurfaceIntoPixmap(VASurfaceID va_surface_id
,
115 gfx::Size dest_size
);
118 // Returns true if the VAAPI version is less than the specified version.
119 bool VAAPIVersionLessThan(int major
, int minor
);
121 // Get a VAImage from a VASurface and map it into memory. The VAImage should
122 // be released using the ReturnVaImage function. Returns true when successful.
123 // This is intended for testing only.
124 bool GetVaImageForTesting(VASurfaceID va_surface_id
,
128 // Release the VAImage (and the associated memory mapping) obtained from
129 // GetVaImage(). This is intended for testing only.
130 void ReturnVaImageForTesting(VAImage
* image
);
132 // Upload contents of |frame| into |va_surface_id| for encode.
133 bool UploadVideoFrameToSurface(const scoped_refptr
<media::VideoFrame
>& frame
,
134 VASurfaceID va_surface_id
);
136 // Create a buffer of |size| bytes to be used as encode output.
137 bool CreateCodedBuffer(size_t size
, VABufferID
* buffer_id
);
139 // Download the contents of the buffer with given |buffer_id| into a buffer of
140 // size |target_size|, pointed to by |target_ptr|. The number of bytes
141 // downloaded will be returned in |coded_data_size|. |sync_surface_id| will
142 // be used as a sync point, i.e. it will have to become idle before starting
143 // the download. |sync_surface_id| should be the source surface passed
144 // to the encode job.
145 bool DownloadAndDestroyCodedBuffer(VABufferID buffer_id
,
146 VASurfaceID sync_surface_id
,
149 size_t* coded_data_size
);
151 // Destroy all previously-allocated (and not yet destroyed) coded buffers.
152 void DestroyCodedBuffers();
154 // Blits a VASurface |va_surface_id_src| into another VASurface
155 // |va_surface_id_dest| applying pixel format conversion and scaling
157 bool BlitSurface(VASurfaceID va_surface_id_src
,
158 const gfx::Size
& src_size
,
159 VASurfaceID va_surface_id_dest
,
160 const gfx::Size
& dest_size
);
165 bool Initialize(CodecMode mode
,
166 media::VideoCodecProfile profile
,
167 const base::Closure
& report_error__to_uma_cb
);
169 bool VaInitialize(const base::Closure
& report_error_to_uma_cb
);
170 bool GetSupportedVaProfiles(std::vector
<VAProfile
>* profiles
);
171 bool IsEntrypointSupported(VAProfile va_profile
, VAEntrypoint entrypoint
);
172 bool AreAttribsSupported(VAProfile va_profile
,
173 VAEntrypoint entrypoint
,
174 const std::vector
<VAConfigAttrib
>& required_attribs
);
176 // Destroys a |va_surface| created using CreateUnownedSurface.
177 void DestroyUnownedSurface(VASurfaceID va_surface_id
);
179 // Initialize the video post processing context with the |size| of
180 // the input pictures to be processed.
181 bool InitializeVpp_Locked();
183 // Deinitialize the video post processing context.
184 void DeinitializeVpp();
186 // Execute pending job in hardware and destroy pending buffers. Return false
187 // if vaapi driver refuses to accept parameter or slice buffers submitted
188 // by client, or if execution fails in hardware.
189 bool Execute(VASurfaceID va_surface_id
);
191 // Attempt to set render mode to "render to texture.". Failure is non-fatal.
192 void TryToSetVADisplayAttributeToLocalGPU();
194 // Lazily initialize static data after sandbox is enabled. Return false on
196 static bool PostSandboxInitialization();
198 // Libva is not thread safe, so we have to do locking for it ourselves.
199 // This lock is to be taken for the duration of all VA-API calls and for
200 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers().
203 // Allocated ids for VASurfaces.
204 std::vector
<VASurfaceID
> va_surface_ids_
;
206 // The VAAPI version.
207 int major_version_
, minor_version_
;
210 // All valid after successful Initialize() and until Deinitialize().
211 VADisplay va_display_
;
212 VAConfigID va_config_id_
;
213 // Created for the current set of va_surface_ids_ in CreateSurfaces() and
214 // valid until DestroySurfaces().
215 VAContextID va_context_id_
;
216 // True if vaInitialize has been called successfully.
217 bool va_initialized_
;
219 // Data queued up for HW codec, to be committed on next execution.
220 std::vector
<VABufferID
> pending_slice_bufs_
;
221 std::vector
<VABufferID
> pending_va_bufs_
;
223 // Bitstream buffers for encode.
224 std::set
<VABufferID
> coded_buffers_
;
226 // Called to report codec errors to UMA. Errors to clients are reported via
227 // return values from public methods.
228 base::Closure report_error_to_uma_cb_
;
230 // VPP (Video Post Processing) context, this is used to convert
231 // pictures used by the decoder to RGBA pictures usable by GL or the
233 VAConfigID va_vpp_config_id_
;
234 VAContextID va_vpp_context_id_
;
235 VABufferID va_vpp_buffer_id_
;
237 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper
);
240 } // namespace content
242 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_