ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / content / common / gpu / media / vaapi_wrapper.h
blobd21f3f1ee5ec52ece294ce53594c01e4f7252846
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/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"
25 #if defined(USE_X11)
26 #include "third_party/libva/va/va_x11.h"
27 #endif // USE_X11
29 namespace content {
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 {
42 public:
43 enum CodecMode {
44 kDecode,
45 kEncode,
48 // Create VaapiWrapper for VAProfile.
49 // |report_error_to_uma_cb| will be called independently from reporting
50 // errors to clients via method return values.
51 static scoped_ptr<VaapiWrapper> Create(
52 CodecMode mode,
53 VAProfile profile,
54 const base::Closure& report_error_to_uma_cb);
56 // Create VaapiWrapper for VideoCodecProfile. It maps VideoCodecProfile
57 // |profile| to VAProfile.
58 // |report_error_to_uma_cb| will be called independently from reporting
59 // errors to clients via method return values.
60 static scoped_ptr<VaapiWrapper> CreateForVideoCodec(
61 CodecMode mode,
62 media::VideoCodecProfile profile,
63 const base::Closure& report_error_to_uma_cb);
65 // Return the supported encode profiles.
66 static std::vector<media::VideoCodecProfile> GetSupportedEncodeProfiles(
67 const base::Closure& report_error_to_uma_cb);
69 ~VaapiWrapper();
71 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each
72 // of size |size|. Returns true when successful, with the created IDs in
73 // |va_surfaces| to be managed and later wrapped in VASurfaces.
74 // 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
76 // at destruction time, as this will be done automatically from
77 // the destructor.
78 bool CreateSurfaces(const gfx::Size& size,
79 size_t num_surfaces,
80 std::vector<VASurfaceID>* va_surfaces);
82 // Free all memory allocated in CreateSurfaces.
83 void DestroySurfaces();
85 // Create a VASurface of |va_format|, |size| and using |va_attribs|
86 // attributes. The ownership of the surface is transferred to the
87 // caller. It differs from surfaces created using CreateSurfaces(),
88 // where VaapiWrapper is the owner of the surfaces.
89 scoped_refptr<VASurface> CreateUnownedSurface(
90 unsigned int va_format,
91 const gfx::Size& size,
92 const std::vector<VASurfaceAttrib>& va_attribs);
94 // Submit parameters or slice data of |va_buffer_type|, copying them from
95 // |buffer| of size |size|, into HW codec. The data in |buffer| is no
96 // longer needed and can be freed after this method returns.
97 // Data submitted via this method awaits in the HW codec until
98 // ExecuteAndDestroyPendingBuffers() is called to execute or
99 // DestroyPendingBuffers() is used to cancel a pending job.
100 bool SubmitBuffer(VABufferType va_buffer_type, size_t size, void* buffer);
102 // Submit a VAEncMiscParameterBuffer of given |misc_param_type|, copying its
103 // data from |buffer| of size |size|, into HW codec. The data in |buffer| is
104 // no longer needed and can be freed after this method returns.
105 // Data submitted via this method awaits in the HW codec until
106 // ExecuteAndDestroyPendingBuffers() is called to execute or
107 // DestroyPendingBuffers() is used to cancel a pending job.
108 bool SubmitVAEncMiscParamBuffer(VAEncMiscParameterType misc_param_type,
109 size_t size,
110 void* buffer);
112 // Cancel and destroy all buffers queued to the HW codec via SubmitBuffer().
113 // Useful when a pending job is to be cancelled (on reset or error).
114 void DestroyPendingBuffers();
116 // Execute job in hardware on target |va_surface_id| and destroy pending
117 // buffers. Return false if Execute() fails.
118 bool ExecuteAndDestroyPendingBuffers(VASurfaceID va_surface_id);
120 #if defined(USE_X11)
121 // Put data from |va_surface_id| into |x_pixmap| of size
122 // |dest_size|, converting/scaling to it.
123 bool PutSurfaceIntoPixmap(VASurfaceID va_surface_id,
124 Pixmap x_pixmap,
125 gfx::Size dest_size);
126 #endif // USE_X11
128 // Returns true if the VAAPI version is less than the specified version.
129 bool VAAPIVersionLessThan(int major, int minor);
131 // Get a VAImage from a VASurface and map it into memory. The size and format
132 // are derived from the surface. Use GetVaImage() instead if |format| or
133 // |size| are different from surface internal representation. The VAImage
134 // should be released using the ReturnVaImage function. Returns true when
135 // successful.
136 bool GetDerivedVaImage(VASurfaceID va_surface_id, VAImage* image, void** mem);
138 // Get a VAImage from a VASurface |va_surface_id| and map it into memory with
139 // given |format| and |size|. The output is |image| and the mapped memory is
140 // |mem|. If |format| doesn't equal to the internal format, the underlying
141 // implementation will do format conversion if supported. |size| should be
142 // smaller than or equal to the surface. If |size| is smaller, the image will
143 // be cropped. The VAImage should be released using the ReturnVaImage
144 // function. Returns true when successful.
145 bool GetVaImage(VASurfaceID va_surface_id,
146 VAImageFormat* format,
147 const gfx::Size& size,
148 VAImage* image,
149 void** mem);
151 // Release the VAImage (and the associated memory mapping) obtained from
152 // GetVaImage() or GetDerivedVaImage().
153 void ReturnVaImage(VAImage* image);
155 // Upload contents of |frame| into |va_surface_id| for encode.
156 bool UploadVideoFrameToSurface(const scoped_refptr<media::VideoFrame>& frame,
157 VASurfaceID va_surface_id);
159 // Create a buffer of |size| bytes to be used as encode output.
160 bool CreateCodedBuffer(size_t size, VABufferID* buffer_id);
162 // Download the contents of the buffer with given |buffer_id| into a buffer of
163 // size |target_size|, pointed to by |target_ptr|. The number of bytes
164 // downloaded will be returned in |coded_data_size|. |sync_surface_id| will
165 // be used as a sync point, i.e. it will have to become idle before starting
166 // the download. |sync_surface_id| should be the source surface passed
167 // to the encode job.
168 bool DownloadAndDestroyCodedBuffer(VABufferID buffer_id,
169 VASurfaceID sync_surface_id,
170 uint8* target_ptr,
171 size_t target_size,
172 size_t* coded_data_size);
174 // Destroy all previously-allocated (and not yet destroyed) coded buffers.
175 void DestroyCodedBuffers();
177 // Blits a VASurface |va_surface_id_src| into another VASurface
178 // |va_surface_id_dest| applying pixel format conversion and scaling
179 // if needed.
180 bool BlitSurface(VASurfaceID va_surface_id_src,
181 const gfx::Size& src_size,
182 VASurfaceID va_surface_id_dest,
183 const gfx::Size& dest_size);
185 private:
186 VaapiWrapper();
188 bool Initialize(CodecMode mode, VAProfile va_profile);
189 void Deinitialize();
190 bool VaInitialize(const base::Closure& report_error_to_uma_cb);
191 bool GetSupportedVaProfiles(std::vector<VAProfile>* profiles);
192 bool IsEntrypointSupported(VAProfile va_profile, VAEntrypoint entrypoint);
193 bool AreAttribsSupported(VAProfile va_profile,
194 VAEntrypoint entrypoint,
195 const std::vector<VAConfigAttrib>& required_attribs);
197 // Destroys a |va_surface| created using CreateUnownedSurface.
198 void DestroyUnownedSurface(VASurfaceID va_surface_id);
200 // Initialize the video post processing context with the |size| of
201 // the input pictures to be processed.
202 bool InitializeVpp_Locked();
204 // Deinitialize the video post processing context.
205 void DeinitializeVpp();
207 // Execute pending job in hardware and destroy pending buffers. Return false
208 // if vaapi driver refuses to accept parameter or slice buffers submitted
209 // by client, or if execution fails in hardware.
210 bool Execute(VASurfaceID va_surface_id);
212 // Attempt to set render mode to "render to texture.". Failure is non-fatal.
213 void TryToSetVADisplayAttributeToLocalGPU();
215 // Lazily initialize static data after sandbox is enabled. Return false on
216 // init failure.
217 static bool PostSandboxInitialization();
219 // 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
221 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers().
222 base::Lock va_lock_;
224 // Allocated ids for VASurfaces.
225 std::vector<VASurfaceID> va_surface_ids_;
227 // The VAAPI version.
228 int major_version_, minor_version_;
230 // VA handles.
231 // All valid after successful Initialize() and until Deinitialize().
232 VADisplay va_display_;
233 VAConfigID va_config_id_;
234 // Created for the current set of va_surface_ids_ in CreateSurfaces() and
235 // valid until DestroySurfaces().
236 VAContextID va_context_id_;
237 // True if vaInitialize has been called successfully.
238 bool va_initialized_;
240 // Data queued up for HW codec, to be committed on next execution.
241 std::vector<VABufferID> pending_slice_bufs_;
242 std::vector<VABufferID> pending_va_bufs_;
244 // Bitstream buffers for encode.
245 std::set<VABufferID> coded_buffers_;
247 // Called to report codec errors to UMA. Errors to clients are reported via
248 // return values from public methods.
249 base::Closure report_error_to_uma_cb_;
251 // VPP (Video Post Processing) context, this is used to convert
252 // pictures used by the decoder to RGBA pictures usable by GL or the
253 // display hardware.
254 VAConfigID va_vpp_config_id_;
255 VAContextID va_vpp_context_id_;
256 VABufferID va_vpp_buffer_id_;
258 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper);
261 } // namespace content
263 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_