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" | |
17 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
18 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
19 #include "content/common/content_export.h" | 18 #include "content/common/content_export.h" |
20 #include "content/common/gpu/media/va_surface.h" | 19 #include "content/common/gpu/media/va_surface.h" |
21 #include "media/base/video_decoder_config.h" | 20 #include "media/base/video_decoder_config.h" |
22 #include "media/base/video_frame.h" | 21 #include "media/base/video_frame.h" |
22 #include "third_party/libva/va/va.h" | |
23 #include "ui/gfx/size.h" | |
24 #if defined(USE_X11) | |
23 #include "third_party/libva/va/va_x11.h" | 25 #include "third_party/libva/va/va_x11.h" |
24 #include "ui/gfx/size.h" | 26 #endif // USE_X11 |
25 | 27 |
26 namespace content { | 28 namespace content { |
27 | 29 |
28 // This class handles VA-API calls and ensures proper locking of VA-API calls | 30 // 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 | 31 // 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 | 32 // 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 | 33 // synchronous and its methods can be called from any thread and may wait on |
32 // the va_lock_ while other, concurrent calls run. | 34 // the va_lock_ while other, concurrent calls run. |
33 // | 35 // |
34 // This class is responsible for managing VAAPI connection, contexts and state. | 36 // This class is responsible for managing VAAPI connection, contexts and state. |
35 // It is also responsible for managing and freeing VABuffers (not VASurfaces), | 37 // 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, | 38 // which are used to queue parameters and slice data to the HW codec, |
37 // as well as underlying memory for VASurfaces themselves. | 39 // as well as underlying memory for VASurfaces themselves. |
38 class CONTENT_EXPORT VaapiWrapper { | 40 class CONTENT_EXPORT VaapiWrapper : public base::RefCounted<VaapiWrapper> { |
39 public: | 41 public: |
40 enum CodecMode { | 42 enum CodecMode { |
41 kDecode, | 43 kDecode, |
42 kEncode, | 44 kEncode, |
43 }; | 45 }; |
44 | 46 |
45 // |report_error_to_uma_cb| will be called independently from reporting | 47 // |report_error_to_uma_cb| will be called independently from reporting |
46 // errors to clients via method return values. | 48 // errors to clients via method return values. |
47 static scoped_ptr<VaapiWrapper> Create( | 49 static scoped_refptr<VaapiWrapper> Create( |
48 CodecMode mode, | 50 CodecMode mode, |
49 media::VideoCodecProfile profile, | 51 media::VideoCodecProfile profile, |
50 Display* x_display, | |
51 const base::Closure& report_error_to_uma_cb); | 52 const base::Closure& report_error_to_uma_cb); |
52 | 53 |
53 // Return the supported encode profiles. | 54 // Return the supported encode profiles. |
54 static std::vector<media::VideoCodecProfile> GetSupportedEncodeProfiles( | 55 static std::vector<media::VideoCodecProfile> GetSupportedEncodeProfiles( |
55 Display* x_display, | |
56 const base::Closure& report_error_to_uma_cb); | 56 const base::Closure& report_error_to_uma_cb); |
57 | 57 |
58 ~VaapiWrapper(); | |
59 | |
60 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each | 58 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each |
61 // 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 |
62 // |va_surfaces| to be managed and later wrapped in VASurfaces. | 60 // |va_surfaces| to be managed and later wrapped in VASurfaces. |
63 // The client must DestroySurfaces() each time before calling this method | 61 // 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 | 62 // 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 | 63 // at destruction time, as this will be done automatically from |
66 // the destructor. | 64 // the destructor. |
67 bool CreateSurfaces(gfx::Size size, | 65 bool CreateSurfaces(const gfx::Size& size, |
68 size_t num_surfaces, | 66 size_t num_surfaces, |
69 std::vector<VASurfaceID>* va_surfaces); | 67 std::vector<VASurfaceID>* va_surfaces); |
70 | 68 |
71 // Free all memory allocated in CreateSurfaces. | 69 // Free all memory allocated in CreateSurfaces. |
72 void DestroySurfaces(); | 70 void DestroySurfaces(); |
73 | 71 |
74 // Submit parameters or slice data of |va_buffer_type|, copying them from | 72 // 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 | 73 // |buffer| of size |size|, into HW codec. The data in |buffer| is no |
76 // longer needed and can be freed after this method returns. | 74 // longer needed and can be freed after this method returns. |
77 // Data submitted via this method awaits in the HW codec until | 75 // Data submitted via this method awaits in the HW codec until |
(...skipping 12 matching lines...) Expand all Loading... | |
90 void* buffer); | 88 void* buffer); |
91 | 89 |
92 // Cancel and destroy all buffers queued to the HW codec via SubmitBuffer(). | 90 // 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). | 91 // Useful when a pending job is to be cancelled (on reset or error). |
94 void DestroyPendingBuffers(); | 92 void DestroyPendingBuffers(); |
95 | 93 |
96 // Execute job in hardware on target |va_surface_id| and destroy pending | 94 // Execute job in hardware on target |va_surface_id| and destroy pending |
97 // buffers. Return false if Execute() fails. | 95 // buffers. Return false if Execute() fails. |
98 bool ExecuteAndDestroyPendingBuffers(VASurfaceID va_surface_id); | 96 bool ExecuteAndDestroyPendingBuffers(VASurfaceID va_surface_id); |
99 | 97 |
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. | 98 // Returns true if the VAAPI version is less than the specified version. |
107 bool VAAPIVersionLessThan(int major, int minor); | 99 bool VAAPIVersionLessThan(int major, int minor); |
108 | 100 |
109 // Get a VAImage from a VASurface and map it into memory. The VAImage should | 101 // 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. | 102 // be released using the ReturnVaImage function. Returns true when successful. |
111 // This is intended for testing only. | 103 // This is intended for testing only. |
112 bool GetVaImageForTesting(VASurfaceID va_surface_id, | 104 bool GetVaImageForTesting(VASurfaceID va_surface_id, |
113 VAImage* image, | 105 VAImage* image, |
114 void** mem); | 106 void** mem); |
115 | 107 |
(...skipping 16 matching lines...) Expand all Loading... | |
132 // to the encode job. | 124 // to the encode job. |
133 bool DownloadAndDestroyCodedBuffer(VABufferID buffer_id, | 125 bool DownloadAndDestroyCodedBuffer(VABufferID buffer_id, |
134 VASurfaceID sync_surface_id, | 126 VASurfaceID sync_surface_id, |
135 uint8* target_ptr, | 127 uint8* target_ptr, |
136 size_t target_size, | 128 size_t target_size, |
137 size_t* coded_data_size); | 129 size_t* coded_data_size); |
138 | 130 |
139 // Destroy all previously-allocated (and not yet destroyed) coded buffers. | 131 // Destroy all previously-allocated (and not yet destroyed) coded buffers. |
140 void DestroyCodedBuffers(); | 132 void DestroyCodedBuffers(); |
141 | 133 |
134 #if defined(USE_X11) | |
135 // Put data from |va_surface_id| into |x_pixmap| of size | |
136 // |dest_size|, converting/scaling to it. | |
137 bool PutSurfaceIntoPixmap(VASurfaceID va_surface_id, | |
138 Pixmap x_pixmap, | |
139 gfx::Size dest_size); | |
140 #endif // USE_X11 | |
141 | |
142 private: | 142 private: |
143 friend class base::RefCounted<VaapiWrapper>; | |
143 VaapiWrapper(); | 144 VaapiWrapper(); |
145 ~VaapiWrapper(); | |
144 | 146 |
145 bool Initialize(CodecMode mode, | 147 bool Initialize(CodecMode mode, |
146 media::VideoCodecProfile profile, | 148 media::VideoCodecProfile profile, |
147 Display* x_display, | 149 const base::Closure& report_error__to_uma_cb); |
piman
2014/12/17 18:57:06
nit: spurious _ added
| |
148 const base::Closure& report_error_to_uma_cb); | |
149 void Deinitialize(); | 150 void Deinitialize(); |
150 bool VaInitialize(Display* x_display, | 151 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); | 152 bool GetSupportedVaProfiles(std::vector<VAProfile>* profiles); |
153 bool IsEntrypointSupported(VAProfile va_profile, VAEntrypoint entrypoint); | 153 bool IsEntrypointSupported(VAProfile va_profile, VAEntrypoint entrypoint); |
154 bool AreAttribsSupported(VAProfile va_profile, | 154 bool AreAttribsSupported(VAProfile va_profile, |
155 VAEntrypoint entrypoint, | 155 VAEntrypoint entrypoint, |
156 const std::vector<VAConfigAttrib>& required_attribs); | 156 const std::vector<VAConfigAttrib>& required_attribs); |
157 | 157 |
158 // Execute pending job in hardware and destroy pending buffers. Return false | 158 // Execute pending job in hardware and destroy pending buffers. Return false |
159 // if vaapi driver refuses to accept parameter or slice buffers submitted | 159 // if vaapi driver refuses to accept parameter or slice buffers submitted |
160 // by client, or if execution fails in hardware. | 160 // by client, or if execution fails in hardware. |
161 bool Execute(VASurfaceID va_surface_id); | 161 bool Execute(VASurfaceID va_surface_id); |
(...skipping 10 matching lines...) Expand all Loading... | |
172 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers(). | 172 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers(). |
173 base::Lock va_lock_; | 173 base::Lock va_lock_; |
174 | 174 |
175 // Allocated ids for VASurfaces. | 175 // Allocated ids for VASurfaces. |
176 std::vector<VASurfaceID> va_surface_ids_; | 176 std::vector<VASurfaceID> va_surface_ids_; |
177 | 177 |
178 // The VAAPI version. | 178 // The VAAPI version. |
179 int major_version_, minor_version_; | 179 int major_version_, minor_version_; |
180 | 180 |
181 // VA handles. | 181 // VA handles. |
182 // Both valid after successful Initialize() and until Deinitialize(). | 182 // All valid after successful Initialize() and until Deinitialize(). |
183 VADisplay va_display_; | 183 VADisplay va_display_; |
184 VAConfigID va_config_id_; | 184 VAConfigID va_config_id_; |
185 // Created for the current set of va_surface_ids_ in CreateSurfaces() and | 185 // Created for the current set of va_surface_ids_ in CreateSurfaces() and |
186 // valid until DestroySurfaces(). | 186 // valid until DestroySurfaces(). |
187 VAContextID va_context_id_; | 187 VAContextID va_context_id_; |
188 // True if vaInitialize has been called successfully. | 188 // True if vaInitialize has been called successfully. |
189 bool va_initialized_; | 189 bool va_initialized_; |
190 | 190 |
191 // Data queued up for HW codec, to be committed on next execution. | 191 // Data queued up for HW codec, to be committed on next execution. |
192 std::vector<VABufferID> pending_slice_bufs_; | 192 std::vector<VABufferID> pending_slice_bufs_; |
193 std::vector<VABufferID> pending_va_bufs_; | 193 std::vector<VABufferID> pending_va_bufs_; |
194 | 194 |
195 // Bitstream buffers for encode. | 195 // Bitstream buffers for encode. |
196 std::set<VABufferID> coded_buffers_; | 196 std::set<VABufferID> coded_buffers_; |
197 | 197 |
198 // Called to report codec errors to UMA. Errors to clients are reported via | 198 // Called to report codec errors to UMA. Errors to clients are reported via |
199 // return values from public methods. | 199 // return values from public methods. |
200 base::Closure report_error_to_uma_cb_; | 200 base::Closure report_error_to_uma_cb_; |
201 | 201 |
202 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper); | 202 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper); |
203 }; | 203 }; |
204 | 204 |
205 } // namespace content | 205 } // namespace content |
206 | 206 |
207 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ | 207 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ |
OLD | NEW |