Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / common / gpu / media / vaapi_wrapper.h
blobcd2d2a6d8fa14b8e54b348690c4522c115895307
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.
4 //
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_
13 #include <set>
14 #include <vector>
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"
29 #if defined(USE_X11)
30 #include "third_party/libva/va/va_x11.h"
31 #endif // USE_X11
33 namespace content {
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 {
46 public:
47 enum CodecMode {
48 kDecode,
49 kEncode,
50 kCodecModeMax,
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(
57 CodecMode mode,
58 VAProfile va_profile,
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(
66 CodecMode mode,
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();
78 ~VaapiWrapper();
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
86 // the destructor.
87 bool CreateSurfaces(const gfx::Size& size,
88 size_t num_surfaces,
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,
118 size_t size,
119 void* buffer);
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);
129 #if defined(USE_X11)
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,
133 Pixmap x_pixmap,
134 gfx::Size dest_size);
135 #endif // USE_X11
137 // Returns true if the VAAPI version is less than the specified version.
138 bool VAAPIVersionLessThan(int major, int minor);
140 // Get a VAImage from a VASurface and map it into memory. The size and format
141 // are derived from the surface. Use GetVaImage() instead if |format| or
142 // |size| are different from surface internal representation. The VAImage
143 // should be released using the ReturnVaImage function. Returns true when
144 // successful.
145 bool GetDerivedVaImage(VASurfaceID va_surface_id, VAImage* image, void** mem);
147 // Get a VAImage from a VASurface |va_surface_id| and map it into memory with
148 // given |format| and |size|. The output is |image| and the mapped memory is
149 // |mem|. If |format| doesn't equal to the internal format, the underlying
150 // implementation will do format conversion if supported. |size| should be
151 // smaller than or equal to the surface. If |size| is smaller, the image will
152 // be cropped. The VAImage should be released using the ReturnVaImage
153 // function. Returns true when successful.
154 bool GetVaImage(VASurfaceID va_surface_id,
155 VAImageFormat* format,
156 const gfx::Size& size,
157 VAImage* image,
158 void** mem);
160 // Release the VAImage (and the associated memory mapping) obtained from
161 // GetVaImage() or GetDerivedVaImage().
162 void ReturnVaImage(VAImage* image);
164 // Upload contents of |frame| into |va_surface_id| for encode.
165 bool UploadVideoFrameToSurface(const scoped_refptr<media::VideoFrame>& frame,
166 VASurfaceID va_surface_id);
168 // Create a buffer of |size| bytes to be used as encode output.
169 bool CreateCodedBuffer(size_t size, VABufferID* buffer_id);
171 // Download the contents of the buffer with given |buffer_id| into a buffer of
172 // size |target_size|, pointed to by |target_ptr|. The number of bytes
173 // downloaded will be returned in |coded_data_size|. |sync_surface_id| will
174 // be used as a sync point, i.e. it will have to become idle before starting
175 // the download. |sync_surface_id| should be the source surface passed
176 // to the encode job.
177 bool DownloadAndDestroyCodedBuffer(VABufferID buffer_id,
178 VASurfaceID sync_surface_id,
179 uint8* target_ptr,
180 size_t target_size,
181 size_t* coded_data_size);
183 // Destroy all previously-allocated (and not yet destroyed) coded buffers.
184 void DestroyCodedBuffers();
186 // Blits a VASurface |va_surface_id_src| into another VASurface
187 // |va_surface_id_dest| applying pixel format conversion and scaling
188 // if needed.
189 bool BlitSurface(VASurfaceID va_surface_id_src,
190 const gfx::Size& src_size,
191 VASurfaceID va_surface_id_dest,
192 const gfx::Size& dest_size);
194 private:
195 struct ProfileInfo {
196 VAProfile va_profile;
197 gfx::Size max_resolution;
200 class LazyProfileInfos {
201 public:
202 LazyProfileInfos();
203 ~LazyProfileInfos();
204 std::vector<ProfileInfo> GetSupportedProfileInfosForCodecMode(
205 CodecMode mode);
206 bool IsProfileSupported(CodecMode mode, VAProfile va_profile);
208 private:
209 std::vector<ProfileInfo> supported_profiles_[kCodecModeMax];
212 VaapiWrapper();
214 bool Initialize(CodecMode mode, VAProfile va_profile);
215 void Deinitialize();
216 bool VaInitialize(const base::Closure& report_error_to_uma_cb);
217 bool GetSupportedVaProfiles(std::vector<VAProfile>* profiles);
219 // Check if |va_profile| supports |entrypoint| or not. |va_lock_| must be
220 // held on entry.
221 bool IsEntrypointSupported_Locked(VAProfile va_profile,
222 VAEntrypoint entrypoint);
224 // Return true if |va_profile| for |entrypoint| with |required_attribs| is
225 // supported. |va_lock_| must be held on entry.
226 bool AreAttribsSupported_Locked(
227 VAProfile va_profile,
228 VAEntrypoint entrypoint,
229 const std::vector<VAConfigAttrib>& required_attribs);
231 // Get maximum resolution for |va_profile| and |entrypoint| with
232 // |required_attribs|. If return value is true, |resolution| is the maximum
233 // resolution. |va_lock_| must be held on entry.
234 bool GetMaxResolution_Locked(
235 VAProfile va_profile,
236 VAEntrypoint entrypoint,
237 std::vector<VAConfigAttrib>& required_attribs,
238 gfx::Size* resolution);
240 // Destroys a |va_surface| created using CreateUnownedSurface.
241 void DestroyUnownedSurface(VASurfaceID va_surface_id);
243 // Initialize the video post processing context with the |size| of
244 // the input pictures to be processed.
245 bool InitializeVpp_Locked();
247 // Deinitialize the video post processing context.
248 void DeinitializeVpp();
250 // Execute pending job in hardware and destroy pending buffers. Return false
251 // if vaapi driver refuses to accept parameter or slice buffers submitted
252 // by client, or if execution fails in hardware.
253 bool Execute(VASurfaceID va_surface_id);
255 // Attempt to set render mode to "render to texture.". Failure is non-fatal.
256 void TryToSetVADisplayAttributeToLocalGPU();
258 // Get supported profile infos for |mode|.
259 std::vector<ProfileInfo> GetSupportedProfileInfosForCodecModeInternal(
260 CodecMode mode);
262 // Lazily initialize static data after sandbox is enabled. Return false on
263 // init failure.
264 static bool PostSandboxInitialization();
266 // Map VideoCodecProfile enum values to VaProfile values. This function
267 // includes a workaround for crbug.com/345569. If va_profile is h264 baseline
268 // and it is not supported, we try constrained baseline.
269 static VAProfile ProfileToVAProfile(media::VideoCodecProfile profile,
270 CodecMode mode);
272 // Libva is not thread safe, so we have to do locking for it ourselves.
273 // This lock is to be taken for the duration of all VA-API calls and for
274 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers().
275 base::Lock va_lock_;
277 // Allocated ids for VASurfaces.
278 std::vector<VASurfaceID> va_surface_ids_;
280 // The VAAPI version.
281 int major_version_, minor_version_;
283 // VA handles.
284 // All valid after successful Initialize() and until Deinitialize().
285 VADisplay va_display_;
286 VAConfigID va_config_id_;
287 // Created for the current set of va_surface_ids_ in CreateSurfaces() and
288 // valid until DestroySurfaces().
289 VAContextID va_context_id_;
290 // True if vaInitialize has been called successfully.
291 bool va_initialized_;
293 // Data queued up for HW codec, to be committed on next execution.
294 std::vector<VABufferID> pending_slice_bufs_;
295 std::vector<VABufferID> pending_va_bufs_;
297 // Bitstream buffers for encode.
298 std::set<VABufferID> coded_buffers_;
300 // Called to report codec errors to UMA. Errors to clients are reported via
301 // return values from public methods.
302 base::Closure report_error_to_uma_cb_;
304 // VPP (Video Post Processing) context, this is used to convert
305 // pictures used by the decoder to RGBA pictures usable by GL or the
306 // display hardware.
307 VAConfigID va_vpp_config_id_;
308 VAContextID va_vpp_context_id_;
309 VABufferID va_vpp_buffer_id_;
311 #if defined(USE_OZONE)
312 // Drm file used to obtain access to the driver interface by VA.
313 base::File drm_file_;
314 #endif // USE_OZONE
316 // Singleton variable to store supported profile information for encode and
317 // decode.
318 static base::LazyInstance<LazyProfileInfos> profile_infos_;
320 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper);
323 } // namespace content
325 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_