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/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
18 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
19 #include "content/common/content_export.h" | 19 #include "content/common/content_export.h" |
20 #include "content/common/gpu/media/va_surface.h" | 20 #include "content/common/gpu/media/va_surface.h" |
21 #include "media/base/video_decoder_config.h" | 21 #include "media/base/video_decoder_config.h" |
22 #include "media/base/video_frame.h" | 22 #include "media/base/video_frame.h" |
23 #include "third_party/libva/va/va.h" | |
24 #include "third_party/libva/va/va_vpp.h" | |
25 #include "ui/gfx/size.h" | |
26 #if defined(USE_X11) | |
23 #include "third_party/libva/va/va_x11.h" | 27 #include "third_party/libva/va/va_x11.h" |
24 #include "ui/gfx/size.h" | 28 #endif // USE_X11 |
25 | 29 |
26 namespace content { | 30 namespace content { |
27 | 31 |
28 // This class handles VA-API calls and ensures proper locking of VA-API calls | 32 // 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 | 33 // 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 | 34 // 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 | 35 // synchronous and its methods can be called from any thread and may wait on |
32 // the va_lock_ while other, concurrent calls run. | 36 // the va_lock_ while other, concurrent calls run. |
33 // | 37 // |
34 // This class is responsible for managing VAAPI connection, contexts and state. | 38 // This class is responsible for managing VAAPI connection, contexts and state. |
35 // It is also responsible for managing and freeing VABuffers (not VASurfaces), | 39 // 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, | 40 // which are used to queue parameters and slice data to the HW codec, |
37 // as well as underlying memory for VASurfaces themselves. | 41 // as well as underlying memory for VASurfaces themselves. |
38 class CONTENT_EXPORT VaapiWrapper { | 42 class CONTENT_EXPORT VaapiWrapper : public base::RefCounted<VaapiWrapper> { |
39 public: | 43 public: |
40 enum CodecMode { | 44 enum CodecMode { |
41 kDecode, | 45 kDecode, |
42 kEncode, | 46 kEncode, |
43 }; | 47 }; |
44 | 48 |
45 // |report_error_to_uma_cb| will be called independently from reporting | 49 // |report_error_to_uma_cb| will be called independently from reporting |
46 // errors to clients via method return values. | 50 // errors to clients via method return values. |
47 static scoped_ptr<VaapiWrapper> Create( | 51 static scoped_refptr<VaapiWrapper> Create( |
48 CodecMode mode, | 52 CodecMode mode, |
49 media::VideoCodecProfile profile, | 53 media::VideoCodecProfile profile, |
50 Display* x_display, | |
51 const base::Closure& report_error_to_uma_cb); | 54 const base::Closure& report_error_to_uma_cb); |
52 | 55 |
53 ~VaapiWrapper(); | 56 ~VaapiWrapper(); |
54 | 57 |
55 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each | 58 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each |
56 // of size |size|. Returns true when successful, with the created IDs in | 59 // of size |size|. Returns true when successful, with the created IDs in |
57 // |va_surfaces| to be managed and later wrapped in VASurfaces. | 60 // |va_surfaces| to be managed and later wrapped in VASurfaces. |
58 // The client must DestroySurfaces() each time before calling this method | 61 // 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 | 62 // 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 | 63 // at destruction time, as this will be done automatically from |
61 // the destructor. | 64 // the destructor. |
62 bool CreateSurfaces(gfx::Size size, | 65 bool CreateSurfaces(const gfx::Size& size, |
63 size_t num_surfaces, | 66 size_t num_surfaces, |
64 std::vector<VASurfaceID>* va_surfaces); | 67 std::vector<VASurfaceID>* va_surfaces); |
65 | 68 |
66 // Free all memory allocated in CreateSurfaces. | 69 // Free all memory allocated in CreateSurfaces. |
67 void DestroySurfaces(); | 70 void DestroySurfaces(); |
68 | 71 |
72 // Create a VASurface of |format|, |size| and using attributes | |
Pawel Osciak
2014/10/26 13:06:48
s/format/va_format/
llandwerlin-old
2014/10/29 13:52:48
Acknowledged.
| |
73 // |attribs|. The Client must call DestroyOutputSurface() to destroy | |
Pawel Osciak
2014/10/26 13:06:48
where |num_va_attribs| is the size of |va_attribs|
llandwerlin-old
2014/10/29 13:52:48
Acknowledged.
| |
74 // the created surface. | |
Pawel Osciak
2014/10/26 13:06:48
What if we returned a VASurface instead and have D
llandwerlin-old
2014/10/29 13:52:48
Yes, doing this.
| |
75 bool CreateOutputSurface(unsigned int va_format, | |
Pawel Osciak
2014/10/26 13:06:48
I'd call it CreateUnmanagedSurface, or CreateUnown
llandwerlin-old
2014/10/29 13:52:48
Acknowledged.
| |
76 const gfx::Size& size, | |
77 VASurfaceAttrib* va_attribs, | |
78 size_t num_va_attribs, | |
79 VASurfaceID* va_surface); | |
Pawel Osciak
2014/10/26 13:06:48
Please document va_surface too.
Also please always
llandwerlin-old
2014/10/29 13:52:49
Acknowledged.
| |
80 | |
81 // Destroys a |va_surface| created using CreateOutputSurface. | |
82 void DestroyOutputSurface(VASurfaceID va_surface); | |
83 | |
69 // Submit parameters or slice data of |va_buffer_type|, copying them from | 84 // 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 | 85 // |buffer| of size |size|, into HW codec. The data in |buffer| is no |
71 // longer needed and can be freed after this method returns. | 86 // longer needed and can be freed after this method returns. |
72 // Data submitted via this method awaits in the HW codec until | 87 // Data submitted via this method awaits in the HW codec until |
73 // ExecuteAndDestroyPendingBuffers() is called to execute or | 88 // ExecuteAndDestroyPendingBuffers() is called to execute or |
74 // DestroyPendingBuffers() is used to cancel a pending job. | 89 // DestroyPendingBuffers() is used to cancel a pending job. |
75 bool SubmitBuffer(VABufferType va_buffer_type, size_t size, void* buffer); | 90 bool SubmitBuffer(VABufferType va_buffer_type, size_t size, void* buffer); |
76 | 91 |
77 // Submit a VAEncMiscParameterBuffer of given |misc_param_type|, copying its | 92 // 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 | 93 // 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. | 94 // no longer needed and can be freed after this method returns. |
80 // Data submitted via this method awaits in the HW codec until | 95 // Data submitted via this method awaits in the HW codec until |
81 // ExecuteAndDestroyPendingBuffers() is called to execute or | 96 // ExecuteAndDestroyPendingBuffers() is called to execute or |
82 // DestroyPendingBuffers() is used to cancel a pending job. | 97 // DestroyPendingBuffers() is used to cancel a pending job. |
83 bool SubmitVAEncMiscParamBuffer(VAEncMiscParameterType misc_param_type, | 98 bool SubmitVAEncMiscParamBuffer(VAEncMiscParameterType misc_param_type, |
84 size_t size, | 99 size_t size, |
85 void* buffer); | 100 void* buffer); |
86 | 101 |
87 // Cancel and destroy all buffers queued to the HW codec via SubmitBuffer(). | 102 // 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). | 103 // Useful when a pending job is to be cancelled (on reset or error). |
89 void DestroyPendingBuffers(); | 104 void DestroyPendingBuffers(); |
90 | 105 |
91 // Execute job in hardware on target |va_surface_id| and destroy pending | 106 // Execute job in hardware on target |va_surface_id| and destroy pending |
92 // buffers. Return false if Execute() fails. | 107 // buffers. Return false if Execute() fails. |
93 bool ExecuteAndDestroyPendingBuffers(VASurfaceID va_surface_id); | 108 bool ExecuteAndDestroyPendingBuffers(VASurfaceID va_surface_id); |
94 | 109 |
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, | |
98 Pixmap x_pixmap, | |
99 gfx::Size dest_size); | |
100 | |
101 // Returns true if the VAAPI version is less than the specified version. | 110 // Returns true if the VAAPI version is less than the specified version. |
102 bool VAAPIVersionLessThan(int major, int minor); | 111 bool VAAPIVersionLessThan(int major, int minor); |
103 | 112 |
104 // Get a VAImage from a VASurface and map it into memory. The VAImage should | 113 // 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. | 114 // be released using the ReturnVaImage function. Returns true when successful. |
106 // This is intended for testing only. | 115 // This is intended for testing only. |
107 bool GetVaImageForTesting(VASurfaceID va_surface_id, | 116 bool GetVaImageForTesting(VASurfaceID va_surface_id, |
108 VAImage* image, | 117 VAImage* image, |
109 void** mem); | 118 void** mem); |
110 | 119 |
(...skipping 16 matching lines...) Expand all Loading... | |
127 // to the encode job. | 136 // to the encode job. |
128 bool DownloadAndDestroyCodedBuffer(VABufferID buffer_id, | 137 bool DownloadAndDestroyCodedBuffer(VABufferID buffer_id, |
129 VASurfaceID sync_surface_id, | 138 VASurfaceID sync_surface_id, |
130 uint8* target_ptr, | 139 uint8* target_ptr, |
131 size_t target_size, | 140 size_t target_size, |
132 size_t* coded_data_size); | 141 size_t* coded_data_size); |
133 | 142 |
134 // Destroy all previously-allocated (and not yet destroyed) coded buffers. | 143 // Destroy all previously-allocated (and not yet destroyed) coded buffers. |
135 void DestroyCodedBuffers(); | 144 void DestroyCodedBuffers(); |
136 | 145 |
146 // Converts a VASurface from decoder preferred format (NV12) to | |
147 // output picture format (RGBA) | |
Pawel Osciak
2014/10/26 13:06:48
Full stop at the end of sentence please. Also plea
llandwerlin-old
2014/10/29 13:52:49
Acknowledged.
| |
148 bool PutSurfaceIntoSurface(VASurfaceID va_surface_id_src, | |
Pawel Osciak
2014/10/26 13:06:48
PutSurfaceIntoPixmap() is a download, but this is
llandwerlin-old
2014/10/29 13:52:49
Went with BlitSurface().
| |
149 const gfx::Size& src_size, | |
150 VASurfaceID va_surface_id_dest, | |
151 const gfx::Size& dest_size); | |
152 | |
153 #if defined(USE_X11) | |
154 // Put data from |va_surface_id| into |x_pixmap| of size |size|, | |
155 // converting/scaling to it. | |
156 bool PutSurfaceIntoPixmap(VASurfaceID va_surface_id, | |
157 Pixmap x_pixmap, | |
158 gfx::Size dest_size); | |
159 #endif // USE_X11 | |
160 | |
137 private: | 161 private: |
138 VaapiWrapper(); | 162 VaapiWrapper(); |
139 | 163 |
140 bool Initialize(CodecMode mode, | 164 bool Initialize(VADisplay display, |
165 CodecMode mode, | |
141 media::VideoCodecProfile profile, | 166 media::VideoCodecProfile profile, |
142 Display* x_display, | |
143 const base::Closure& report_error__to_uma_cb); | 167 const base::Closure& report_error__to_uma_cb); |
144 void Deinitialize(); | 168 void Deinitialize(); |
145 | 169 |
170 // Initialize the video post processing context with the |size| of | |
171 // the decoder output pictures. | |
Pawel Osciak
2014/10/26 13:06:48
I think we don't want to mention decoder here. We
llandwerlin-old
2014/10/29 13:52:49
Actually looking into the code of the va driver on
| |
172 bool InitializeVpp(const gfx::Size& size); | |
173 | |
174 // Deinitialize the video post processing context | |
Pawel Osciak
2014/10/26 13:06:48
Please full stop at the end of sentences.
llandwerlin-old
2014/10/29 13:52:48
Acknowledged.
| |
175 void DeinitializeVpp(); | |
176 | |
146 // Execute pending job in hardware and destroy pending buffers. Return false | 177 // Execute pending job in hardware and destroy pending buffers. Return false |
147 // if vaapi driver refuses to accept parameter or slice buffers submitted | 178 // if vaapi driver refuses to accept parameter or slice buffers submitted |
148 // by client, or if execution fails in hardware. | 179 // by client, or if execution fails in hardware. |
149 bool Execute(VASurfaceID va_surface_id); | 180 bool Execute(VASurfaceID va_surface_id); |
150 | 181 |
151 // Attempt to set render mode to "render to texture.". Failure is non-fatal. | 182 // Attempt to set render mode to "render to texture.". Failure is non-fatal. |
152 void TryToSetVADisplayAttributeToLocalGPU(); | 183 void TryToSetVADisplayAttributeToLocalGPU(); |
153 | 184 |
154 // Lazily initialize static data after sandbox is enabled. Return false on | 185 // Lazily initialize static data after sandbox is enabled. Return false on |
155 // init failure. | 186 // init failure. |
156 static bool PostSandboxInitialization(); | 187 static bool PostSandboxInitialization(); |
157 | 188 |
158 // Libva is not thread safe, so we have to do locking for it ourselves. | 189 // 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 | 190 // This lock is to be taken for the duration of all VA-API calls and for |
160 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers(). | 191 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers(). |
161 base::Lock va_lock_; | 192 base::Lock va_lock_; |
162 | 193 |
163 // Allocated ids for VASurfaces. | 194 // Allocated ids for VASurfaces. |
164 std::vector<VASurfaceID> va_surface_ids_; | 195 std::vector<VASurfaceID> va_surface_ids_; |
165 | 196 |
166 // The VAAPI version. | 197 // The VAAPI version. |
167 int major_version_, minor_version_; | 198 int major_version_, minor_version_; |
168 | 199 |
169 // VA handles. | 200 // VA handles. |
170 // Both valid after successful Initialize() and until Deinitialize(). | 201 // All valid after successful Initialize() and until Deinitialize(). |
171 VADisplay va_display_; | 202 VADisplay va_display_; |
172 VAConfigID va_config_id_; | 203 VAConfigID va_config_id_; |
173 // Created for the current set of va_surface_ids_ in CreateSurfaces() and | 204 // Created for the current set of va_surface_ids_ in CreateSurfaces() and |
174 // valid until DestroySurfaces(). | 205 // valid until DestroySurfaces(). |
175 VAContextID va_context_id_; | 206 VAContextID va_context_id_; |
176 | 207 |
177 // Data queued up for HW codec, to be committed on next execution. | 208 // Data queued up for HW codec, to be committed on next execution. |
178 std::vector<VABufferID> pending_slice_bufs_; | 209 std::vector<VABufferID> pending_slice_bufs_; |
179 std::vector<VABufferID> pending_va_bufs_; | 210 std::vector<VABufferID> pending_va_bufs_; |
180 | 211 |
181 // Bitstream buffers for encode. | 212 // Bitstream buffers for encode. |
182 std::set<VABufferID> coded_buffers_; | 213 std::set<VABufferID> coded_buffers_; |
183 | 214 |
184 // Called to report codec errors to UMA. Errors to clients are reported via | 215 // Called to report codec errors to UMA. Errors to clients are reported via |
185 // return values from public methods. | 216 // return values from public methods. |
186 base::Closure report_error_to_uma_cb_; | 217 base::Closure report_error_to_uma_cb_; |
187 | 218 |
219 // VPP context | |
220 VAConfigID va_vpp_config_; | |
Pawel Osciak
2014/10/26 13:06:48
Please add _id at the end of each of these.
llandwerlin-old
2014/10/29 13:52:49
Acknowledged.
| |
221 VAContextID va_vpp_context_; | |
222 VABufferID va_vpp_buffer_; | |
223 | |
224 // Cache of the size of the VPP input pictures | |
Pawel Osciak
2014/10/26 13:06:48
"Input surface size, to which VPP is currently con
llandwerlin-old
2014/10/29 13:52:49
Removed.
| |
225 gfx::Size vpp_picture_size_; | |
226 | |
188 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper); | 227 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper); |
189 }; | 228 }; |
190 | 229 |
191 } // namespace content | 230 } // namespace content |
192 | 231 |
193 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ | 232 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ |
OLD | NEW |