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/callback.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/synchronization/lock.h"
19 #include "content/common/content_export.h"
20 #include "content/common/gpu/media/va_surface.h"
21 #include "media/base/video_decoder_config.h"
22 #include "media/base/video_frame.h"
23 #include "third_party/libva/va/va_x11.h"
24 #include "ui/gfx/size.h"
28 // This class handles VA-API calls and ensures proper locking of VA-API calls
29 // to libva, the userspace shim to the HW codec driver. libva is not
30 // thread-safe, so we have to perform locking ourselves. This class is fully
31 // synchronous and its methods can be called from any thread and may wait on
32 // the va_lock_ while other, concurrent calls run.
34 // This class is responsible for managing VAAPI connection, contexts and state.
35 // It is also responsible for managing and freeing VABuffers (not VASurfaces),
36 // which are used to queue parameters and slice data to the HW codec,
37 // as well as underlying memory for VASurfaces themselves.
38 class CONTENT_EXPORT VaapiWrapper
{
45 // |report_error_to_uma_cb| will be called independently from reporting
46 // errors to clients via method return values.
47 static scoped_ptr
<VaapiWrapper
> Create(
49 media::VideoCodecProfile profile
,
51 const base::Closure
& report_error_to_uma_cb
);
55 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each
56 // of size |size|. Returns true when successful, with the created IDs in
57 // |va_surfaces| to be managed and later wrapped in VASurfaces.
58 // The client must DestroySurfaces() each time before calling this method
59 // again to free the allocated surfaces first, but is not required to do so
60 // at destruction time, as this will be done automatically from
62 bool CreateSurfaces(gfx::Size size
,
64 std::vector
<VASurfaceID
>* va_surfaces
);
66 // Free all memory allocated in CreateSurfaces.
67 void DestroySurfaces();
69 // Submit parameters or slice data of |va_buffer_type|, copying them from
70 // |buffer| of size |size|, into HW codec. The data in |buffer| is no
71 // longer needed and can be freed after this method returns.
72 // Data submitted via this method awaits in the HW codec until
73 // ExecuteAndDestroyPendingBuffers() is called to execute or
74 // DestroyPendingBuffers() is used to cancel a pending job.
75 bool SubmitBuffer(VABufferType va_buffer_type
, size_t size
, void* buffer
);
77 // Submit a VAEncMiscParameterBuffer of given |misc_param_type|, copying its
78 // data from |buffer| of size |size|, into HW codec. The data in |buffer| is
79 // no longer needed and can be freed after this method returns.
80 // Data submitted via this method awaits in the HW codec until
81 // ExecuteAndDestroyPendingBuffers() is called to execute or
82 // DestroyPendingBuffers() is used to cancel a pending job.
83 bool SubmitVAEncMiscParamBuffer(VAEncMiscParameterType misc_param_type
,
87 // Cancel and destroy all buffers queued to the HW codec via SubmitBuffer().
88 // Useful when a pending job is to be cancelled (on reset or error).
89 void DestroyPendingBuffers();
91 // Execute job in hardware on target |va_surface_id| and destroy pending
92 // buffers. Return false if Execute() fails.
93 bool ExecuteAndDestroyPendingBuffers(VASurfaceID va_surface_id
);
95 // Put data from |va_surface_id| into |x_pixmap| of size |size|,
96 // converting/scaling to it.
97 bool PutSurfaceIntoPixmap(VASurfaceID va_surface_id
,
101 // Returns true if the VAAPI version is less than the specified version.
102 bool VAAPIVersionLessThan(int major
, int minor
);
104 // Get a VAImage from a VASurface and map it into memory. The VAImage should
105 // be released using the ReturnVaImage function. Returns true when successful.
106 // This is intended for testing only.
107 bool GetVaImageForTesting(VASurfaceID va_surface_id
,
111 // Release the VAImage (and the associated memory mapping) obtained from
112 // GetVaImage(). This is intended for testing only.
113 void ReturnVaImageForTesting(VAImage
* image
);
115 // Upload contents of |frame| into |va_surface_id| for encode.
116 bool UploadVideoFrameToSurface(const scoped_refptr
<media::VideoFrame
>& frame
,
117 VASurfaceID va_surface_id
);
119 // Create a buffer of |size| bytes to be used as encode output.
120 bool CreateCodedBuffer(size_t size
, VABufferID
* buffer_id
);
122 // Download the contents of the buffer with given |buffer_id| into a buffer of
123 // size |target_size|, pointed to by |target_ptr|. The number of bytes
124 // downloaded will be returned in |coded_data_size|. |sync_surface_id| will
125 // be used as a sync point, i.e. it will have to become idle before starting
126 // the download. |sync_surface_id| should be the source surface passed
127 // to the encode job.
128 bool DownloadAndDestroyCodedBuffer(VABufferID buffer_id
,
129 VASurfaceID sync_surface_id
,
132 size_t* coded_data_size
);
134 // Destroy all previously-allocated (and not yet destroyed) coded buffers.
135 void DestroyCodedBuffers();
140 bool Initialize(CodecMode mode
,
141 media::VideoCodecProfile profile
,
143 const base::Closure
& report_error__to_uma_cb
);
146 // Execute pending job in hardware and destroy pending buffers. Return false
147 // if vaapi driver refuses to accept parameter or slice buffers submitted
148 // by client, or if execution fails in hardware.
149 bool Execute(VASurfaceID va_surface_id
);
151 // Attempt to set render mode to "render to texture.". Failure is non-fatal.
152 void TryToSetVADisplayAttributeToLocalGPU();
154 // Lazily initialize static data after sandbox is enabled. Return false on
156 static bool PostSandboxInitialization();
158 // Libva is not thread safe, so we have to do locking for it ourselves.
159 // This lock is to be taken for the duration of all VA-API calls and for
160 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers().
163 // Allocated ids for VASurfaces.
164 std::vector
<VASurfaceID
> va_surface_ids_
;
166 // The VAAPI version.
167 int major_version_
, minor_version_
;
170 // Both valid after successful Initialize() and until Deinitialize().
171 VADisplay va_display_
;
172 VAConfigID va_config_id_
;
173 // Created for the current set of va_surface_ids_ in CreateSurfaces() and
174 // valid until DestroySurfaces().
175 VAContextID va_context_id_
;
177 // Data queued up for HW codec, to be committed on next execution.
178 std::vector
<VABufferID
> pending_slice_bufs_
;
179 std::vector
<VABufferID
> pending_va_bufs_
;
181 // Bitstream buffers for encode.
182 std::set
<VABufferID
> coded_buffers_
;
184 // Called to report codec errors to UMA. Errors to clients are reported via
185 // return values from public methods.
186 base::Closure report_error_to_uma_cb_
;
188 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper
);
191 } // namespace content
193 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_