OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // This file contains an implementation of VaapiWrapper, used by | 5 // This file contains an implementation of VaapiWrapper, used by |
6 // VaapiVideoDecodeAccelerator and VaapiH264Decoder for decode, | 6 // VaapiVideoDecodeAccelerator and VaapiH264Decoder for decode, |
7 // and VaapiVideoEncodeAccelerator for encode, to interface | 7 // and VaapiVideoEncodeAccelerator for encode, to interface |
8 // with libva (VA-API library for hardware video codec). | 8 // with libva (VA-API library for hardware video codec). |
9 | 9 |
10 #ifndef CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ | 10 #ifndef CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ |
11 #define CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ | 11 #define CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ |
12 | 12 |
13 #include <set> | 13 #include <set> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "base/callback.h" | 16 #include "base/callback.h" |
17 #include "base/memory/linked_ptr.h" | |
17 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
19 #include "base/memory/weak_ptr.h" | |
18 #include "base/synchronization/lock.h" | 20 #include "base/synchronization/lock.h" |
19 #include "content/common/content_export.h" | 21 #include "content/common/content_export.h" |
20 #include "content/common/gpu/media/va_surface.h" | 22 #include "content/common/gpu/media/va_surface.h" |
21 #include "media/base/video_decoder_config.h" | 23 #include "media/base/video_decoder_config.h" |
22 #include "media/base/video_frame.h" | 24 #include "media/base/video_frame.h" |
25 #include "third_party/libva/va/va.h" | |
26 #include "third_party/libva/va/va_vpp.h" | |
27 #include "ui/gfx/size.h" | |
28 #if defined(USE_X11) | |
23 #include "third_party/libva/va/va_x11.h" | 29 #include "third_party/libva/va/va_x11.h" |
24 #include "ui/gfx/size.h" | 30 #endif // USE_X11 |
31 | |
32 namespace gfx { | |
33 class GLContext; | |
34 } // namespace gfx | |
25 | 35 |
26 namespace content { | 36 namespace content { |
27 | 37 |
38 class VaapiPicture; | |
39 | |
28 // This class handles VA-API calls and ensures proper locking of VA-API calls | 40 // 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 | 41 // 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 | 42 // 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 | 43 // synchronous and its methods can be called from any thread and may wait on |
32 // the va_lock_ while other, concurrent calls run. | 44 // the va_lock_ while other, concurrent calls run. |
33 // | 45 // |
34 // This class is responsible for managing VAAPI connection, contexts and state. | 46 // This class is responsible for managing VAAPI connection, contexts and state. |
35 // It is also responsible for managing and freeing VABuffers (not VASurfaces), | 47 // 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, | 48 // which are used to queue parameters and slice data to the HW codec, |
37 // as well as underlying memory for VASurfaces themselves. | 49 // as well as underlying memory for VASurfaces themselves. |
38 class CONTENT_EXPORT VaapiWrapper { | 50 class CONTENT_EXPORT VaapiWrapper : public base::SupportsWeakPtr<VaapiWrapper> { |
39 public: | 51 public: |
40 enum CodecMode { | 52 enum CodecMode { |
41 kDecode, | 53 kDecode, |
42 kEncode, | 54 kEncode, |
43 }; | 55 }; |
44 | 56 |
57 ~VaapiWrapper(); | |
Pawel Osciak
2014/12/08 10:55:16
Any reason to move this?
llandwerlin-old
2014/12/08 16:42:07
Moving this back.
| |
58 | |
45 // |report_error_to_uma_cb| will be called independently from reporting | 59 // |report_error_to_uma_cb| will be called independently from reporting |
46 // errors to clients via method return values. | 60 // errors to clients via method return values. |
47 static scoped_ptr<VaapiWrapper> Create( | 61 static scoped_ptr<VaapiWrapper> Create( |
48 CodecMode mode, | 62 CodecMode mode, |
49 media::VideoCodecProfile profile, | 63 media::VideoCodecProfile profile, |
50 Display* x_display, | |
51 const base::Closure& report_error_to_uma_cb); | 64 const base::Closure& report_error_to_uma_cb); |
52 | 65 |
53 // Return the supported encode profiles. | 66 // Return the supported encode profiles. |
54 static std::vector<media::VideoCodecProfile> GetSupportedEncodeProfiles( | 67 static std::vector<media::VideoCodecProfile> GetSupportedEncodeProfiles( |
55 Display* x_display, | |
56 const base::Closure& report_error_to_uma_cb); | 68 const base::Closure& report_error_to_uma_cb); |
57 | 69 |
58 ~VaapiWrapper(); | |
59 | |
60 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each | 70 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each |
61 // of size |size|. Returns true when successful, with the created IDs in | 71 // of size |size|. Returns true when successful, with the created IDs in |
62 // |va_surfaces| to be managed and later wrapped in VASurfaces. | 72 // |va_surfaces| to be managed and later wrapped in VASurfaces. |
63 // The client must DestroySurfaces() each time before calling this method | 73 // The client must DestroySurfaces() each time before calling this method |
64 // again to free the allocated surfaces first, but is not required to do so | 74 // again to free the allocated surfaces first, but is not required to do so |
65 // at destruction time, as this will be done automatically from | 75 // at destruction time, as this will be done automatically from |
66 // the destructor. | 76 // the destructor. |
67 bool CreateSurfaces(gfx::Size size, | 77 bool CreateSurfaces(const gfx::Size& size, |
68 size_t num_surfaces, | 78 size_t num_surfaces, |
69 std::vector<VASurfaceID>* va_surfaces); | 79 std::vector<VASurfaceID>* va_surfaces); |
70 | 80 |
71 // Free all memory allocated in CreateSurfaces. | 81 // Free all memory allocated in CreateSurfaces. |
72 void DestroySurfaces(); | 82 void DestroySurfaces(); |
73 | 83 |
84 // Create a VASurface of |va_format|, |size| and using | |
85 // |num_va_attribs| attributes from |va_attribs|. The ownership of | |
86 // the surface is transfered to the call. It differs from surfaces | |
Pawel Osciak
2014/12/08 10:55:16
s/call/caller/
llandwerlin-old
2014/12/08 16:42:07
Done.
| |
87 // created using CreateSurfaces() where VaapiWrapper is the owner of | |
88 // the surfaces. | |
89 scoped_refptr<VASurface> CreateUnownedSurface(unsigned int va_format, | |
90 const gfx::Size& size, | |
91 VASurfaceAttrib* va_attribs, | |
92 size_t num_va_attribs); | |
93 | |
94 // Create a VaapiPicture of |size| to be associated with | |
95 // |picture_buffer_id| and bound to |texture_id|. |gl_context| and | |
96 // |make_context_current| are provided for the GL operations. | |
97 linked_ptr<VaapiPicture> CreatePicture( | |
98 gfx::GLContext* gl_context, | |
99 const base::Callback<bool(void)> make_context_current, | |
100 int32 picture_buffer_id, | |
101 uint32 texture_id, | |
102 const gfx::Size& size); | |
103 | |
74 // Submit parameters or slice data of |va_buffer_type|, copying them from | 104 // Submit parameters or slice data of |va_buffer_type|, copying them from |
75 // |buffer| of size |size|, into HW codec. The data in |buffer| is no | 105 // |buffer| of size |size|, into HW codec. The data in |buffer| is no |
76 // longer needed and can be freed after this method returns. | 106 // longer needed and can be freed after this method returns. |
77 // Data submitted via this method awaits in the HW codec until | 107 // Data submitted via this method awaits in the HW codec until |
78 // ExecuteAndDestroyPendingBuffers() is called to execute or | 108 // ExecuteAndDestroyPendingBuffers() is called to execute or |
79 // DestroyPendingBuffers() is used to cancel a pending job. | 109 // DestroyPendingBuffers() is used to cancel a pending job. |
80 bool SubmitBuffer(VABufferType va_buffer_type, size_t size, void* buffer); | 110 bool SubmitBuffer(VABufferType va_buffer_type, size_t size, void* buffer); |
81 | 111 |
82 // Submit a VAEncMiscParameterBuffer of given |misc_param_type|, copying its | 112 // Submit a VAEncMiscParameterBuffer of given |misc_param_type|, copying its |
83 // data from |buffer| of size |size|, into HW codec. The data in |buffer| is | 113 // data from |buffer| of size |size|, into HW codec. The data in |buffer| is |
84 // no longer needed and can be freed after this method returns. | 114 // no longer needed and can be freed after this method returns. |
85 // Data submitted via this method awaits in the HW codec until | 115 // Data submitted via this method awaits in the HW codec until |
86 // ExecuteAndDestroyPendingBuffers() is called to execute or | 116 // ExecuteAndDestroyPendingBuffers() is called to execute or |
87 // DestroyPendingBuffers() is used to cancel a pending job. | 117 // DestroyPendingBuffers() is used to cancel a pending job. |
88 bool SubmitVAEncMiscParamBuffer(VAEncMiscParameterType misc_param_type, | 118 bool SubmitVAEncMiscParamBuffer(VAEncMiscParameterType misc_param_type, |
89 size_t size, | 119 size_t size, |
90 void* buffer); | 120 void* buffer); |
91 | 121 |
92 // Cancel and destroy all buffers queued to the HW codec via SubmitBuffer(). | 122 // Cancel and destroy all buffers queued to the HW codec via SubmitBuffer(). |
93 // Useful when a pending job is to be cancelled (on reset or error). | 123 // Useful when a pending job is to be cancelled (on reset or error). |
94 void DestroyPendingBuffers(); | 124 void DestroyPendingBuffers(); |
95 | 125 |
96 // Execute job in hardware on target |va_surface_id| and destroy pending | 126 // Execute job in hardware on target |va_surface_id| and destroy pending |
97 // buffers. Return false if Execute() fails. | 127 // buffers. Return false if Execute() fails. |
98 bool ExecuteAndDestroyPendingBuffers(VASurfaceID va_surface_id); | 128 bool ExecuteAndDestroyPendingBuffers(VASurfaceID va_surface_id); |
99 | 129 |
100 // Put data from |va_surface_id| into |x_pixmap| of size |size|, | |
101 // converting/scaling to it. | |
102 bool PutSurfaceIntoPixmap(VASurfaceID va_surface_id, | |
103 Pixmap x_pixmap, | |
104 gfx::Size dest_size); | |
105 | |
106 // Returns true if the VAAPI version is less than the specified version. | 130 // Returns true if the VAAPI version is less than the specified version. |
107 bool VAAPIVersionLessThan(int major, int minor); | 131 bool VAAPIVersionLessThan(int major, int minor); |
108 | 132 |
109 // Get a VAImage from a VASurface and map it into memory. The VAImage should | 133 // Get a VAImage from a VASurface and map it into memory. The VAImage should |
110 // be released using the ReturnVaImage function. Returns true when successful. | 134 // be released using the ReturnVaImage function. Returns true when successful. |
111 // This is intended for testing only. | 135 // This is intended for testing only. |
112 bool GetVaImageForTesting(VASurfaceID va_surface_id, | 136 bool GetVaImageForTesting(VASurfaceID va_surface_id, |
113 VAImage* image, | 137 VAImage* image, |
114 void** mem); | 138 void** mem); |
115 | 139 |
(...skipping 16 matching lines...) Expand all Loading... | |
132 // to the encode job. | 156 // to the encode job. |
133 bool DownloadAndDestroyCodedBuffer(VABufferID buffer_id, | 157 bool DownloadAndDestroyCodedBuffer(VABufferID buffer_id, |
134 VASurfaceID sync_surface_id, | 158 VASurfaceID sync_surface_id, |
135 uint8* target_ptr, | 159 uint8* target_ptr, |
136 size_t target_size, | 160 size_t target_size, |
137 size_t* coded_data_size); | 161 size_t* coded_data_size); |
138 | 162 |
139 // Destroy all previously-allocated (and not yet destroyed) coded buffers. | 163 // Destroy all previously-allocated (and not yet destroyed) coded buffers. |
140 void DestroyCodedBuffers(); | 164 void DestroyCodedBuffers(); |
141 | 165 |
166 // Blits a VASurface |va_surface_id_src| into another VASurface | |
167 // |va_surface_id_dest| applying pixel format conversion and scaling | |
168 // if needed. | |
169 bool BlitSurface(VASurfaceID va_surface_id_src, | |
170 const gfx::Size& src_size, | |
171 VASurfaceID va_surface_id_dest, | |
172 const gfx::Size& dest_size); | |
173 | |
174 #if defined(USE_X11) | |
175 // Put data from |va_surface_id| into |x_pixmap| of size |size|, | |
Pawel Osciak
2014/12/08 10:55:16
s/size/dest_size/
llandwerlin-old
2014/12/08 16:42:07
Done.
| |
176 // converting/scaling to it. | |
177 bool PutSurfaceIntoPixmap(VASurfaceID va_surface_id, | |
178 Pixmap x_pixmap, | |
179 gfx::Size dest_size); | |
180 #endif // USE_X11 | |
181 | |
142 private: | 182 private: |
143 VaapiWrapper(); | 183 VaapiWrapper(); |
144 | 184 |
145 bool Initialize(CodecMode mode, | 185 bool Initialize(CodecMode mode, |
146 media::VideoCodecProfile profile, | 186 media::VideoCodecProfile profile, |
147 Display* x_display, | 187 const base::Closure& report_error__to_uma_cb); |
148 const base::Closure& report_error_to_uma_cb); | |
149 void Deinitialize(); | 188 void Deinitialize(); |
150 bool VaInitialize(Display* x_display, | 189 bool VaInitialize(const base::Closure& report_error_to_uma_cb); |
151 const base::Closure& report_error_to_uma_cb); | |
152 bool GetSupportedVaProfiles(std::vector<VAProfile>* profiles); | 190 bool GetSupportedVaProfiles(std::vector<VAProfile>* profiles); |
153 bool IsEntrypointSupported(VAProfile va_profile, VAEntrypoint entrypoint); | 191 bool IsEntrypointSupported(VAProfile va_profile, VAEntrypoint entrypoint); |
154 bool AreAttribsSupported(VAProfile va_profile, | 192 bool AreAttribsSupported(VAProfile va_profile, |
155 VAEntrypoint entrypoint, | 193 VAEntrypoint entrypoint, |
156 const std::vector<VAConfigAttrib>& required_attribs); | 194 const std::vector<VAConfigAttrib>& required_attribs); |
157 | 195 |
196 // Destroys a |va_surface| created using CreateUnownedSurface. | |
197 void DestroyUnownedSurface(VASurfaceID va_surface); | |
198 | |
199 // Initialize the video post processing context with the |size| of | |
200 // the input pictures to be processed. | |
201 bool InitializeVpp_Locked(); | |
202 | |
203 // Deinitialize the video post processing context. | |
204 void DeinitializeVpp(); | |
205 void DeinitializeVpp_Locked(); | |
206 | |
158 // Execute pending job in hardware and destroy pending buffers. Return false | 207 // Execute pending job in hardware and destroy pending buffers. Return false |
159 // if vaapi driver refuses to accept parameter or slice buffers submitted | 208 // if vaapi driver refuses to accept parameter or slice buffers submitted |
160 // by client, or if execution fails in hardware. | 209 // by client, or if execution fails in hardware. |
161 bool Execute(VASurfaceID va_surface_id); | 210 bool Execute(VASurfaceID va_surface_id); |
162 | 211 |
163 // Attempt to set render mode to "render to texture.". Failure is non-fatal. | 212 // Attempt to set render mode to "render to texture.". Failure is non-fatal. |
164 void TryToSetVADisplayAttributeToLocalGPU(); | 213 void TryToSetVADisplayAttributeToLocalGPU(); |
165 | 214 |
166 // Lazily initialize static data after sandbox is enabled. Return false on | 215 // Lazily initialize static data after sandbox is enabled. Return false on |
167 // init failure. | 216 // init failure. |
168 static bool PostSandboxInitialization(); | 217 static bool PostSandboxInitialization(); |
169 | 218 |
170 // Libva is not thread safe, so we have to do locking for it ourselves. | 219 // Libva is not thread safe, so we have to do locking for it ourselves. |
171 // This lock is to be taken for the duration of all VA-API calls and for | 220 // This lock is to be taken for the duration of all VA-API calls and for |
172 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers(). | 221 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers(). |
173 base::Lock va_lock_; | 222 base::Lock va_lock_; |
174 | 223 |
175 // Allocated ids for VASurfaces. | 224 // Allocated ids for VASurfaces. |
176 std::vector<VASurfaceID> va_surface_ids_; | 225 std::vector<VASurfaceID> va_surface_ids_; |
177 | 226 |
178 // The VAAPI version. | 227 // The VAAPI version. |
179 int major_version_, minor_version_; | 228 int major_version_, minor_version_; |
180 | 229 |
181 // VA handles. | 230 // VA handles. |
182 // Both valid after successful Initialize() and until Deinitialize(). | 231 // All valid after successful Initialize() and until Deinitialize(). |
183 VADisplay va_display_; | 232 VADisplay va_display_; |
184 VAConfigID va_config_id_; | 233 VAConfigID va_config_id_; |
185 // Created for the current set of va_surface_ids_ in CreateSurfaces() and | 234 // Created for the current set of va_surface_ids_ in CreateSurfaces() and |
186 // valid until DestroySurfaces(). | 235 // valid until DestroySurfaces(). |
187 VAContextID va_context_id_; | 236 VAContextID va_context_id_; |
188 | 237 |
189 // Data queued up for HW codec, to be committed on next execution. | 238 // Data queued up for HW codec, to be committed on next execution. |
190 std::vector<VABufferID> pending_slice_bufs_; | 239 std::vector<VABufferID> pending_slice_bufs_; |
191 std::vector<VABufferID> pending_va_bufs_; | 240 std::vector<VABufferID> pending_va_bufs_; |
192 | 241 |
193 // Bitstream buffers for encode. | 242 // Bitstream buffers for encode. |
194 std::set<VABufferID> coded_buffers_; | 243 std::set<VABufferID> coded_buffers_; |
195 | 244 |
196 // Called to report codec errors to UMA. Errors to clients are reported via | 245 // Called to report codec errors to UMA. Errors to clients are reported via |
197 // return values from public methods. | 246 // return values from public methods. |
198 base::Closure report_error_to_uma_cb_; | 247 base::Closure report_error_to_uma_cb_; |
199 | 248 |
249 // VPP context | |
250 VAConfigID va_vpp_config_id_; | |
251 VAContextID va_vpp_context_id_; | |
252 VABufferID va_vpp_buffer_id_; | |
253 | |
200 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper); | 254 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper); |
201 }; | 255 }; |
202 | 256 |
203 } // namespace content | 257 } // namespace content |
204 | 258 |
205 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ | 259 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ |
OLD | NEW |