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" |
18 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
20 #include "base/threading/non_thread_safe.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" |
23 #include "third_party/libva/va/va_x11.h" | 25 #include "third_party/libva/va/va.h" |
24 #include "ui/gfx/size.h" | 26 #include "ui/gfx/size.h" |
25 | 27 |
28 namespace gfx { | |
29 class GLContext; | |
30 }; // namespace gfx | |
31 | |
26 namespace content { | 32 namespace content { |
27 | 33 |
28 // This class handles VA-API calls and ensures proper locking of VA-API calls | 34 // 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 | 35 // 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 | 36 // 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 | 37 // synchronous and its methods can be called from any thread and may wait on |
32 // the va_lock_ while other, concurrent calls run. | 38 // the va_lock_ while other, concurrent calls run. |
33 // | 39 // |
34 // This class is responsible for managing VAAPI connection, contexts and state. | 40 // This class is responsible for managing VAAPI connection, contexts and state. |
35 // It is also responsible for managing and freeing VABuffers (not VASurfaces), | 41 // 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, | 42 // which are used to queue parameters and slice data to the HW codec, |
37 // as well as underlying memory for VASurfaces themselves. | 43 // as well as underlying memory for VASurfaces themselves. |
38 class CONTENT_EXPORT VaapiWrapper { | 44 class CONTENT_EXPORT VaapiWrapper { |
39 public: | 45 public: |
40 enum CodecMode { | 46 enum CodecMode { |
41 kDecode, | 47 kDecode, |
42 kEncode, | 48 kEncode, |
43 }; | 49 }; |
44 | 50 |
51 class Picture : public base::NonThreadSafe { | |
Pawel Osciak
2014/08/25 01:13:24
We should keep Picture as an interface in VaapiVid
| |
52 public: | |
53 virtual ~Picture() {} | |
54 | |
55 int32 picture_buffer_id() const { | |
56 return picture_buffer_id_; | |
57 }; | |
58 uint32 texture_id() const { return texture_id_; } | |
59 const gfx::Size& size() const { return size_; } | |
60 | |
61 protected: | |
62 Picture(int32 picture_buffer_id, uint32 texture_id, gfx::Size size) | |
63 : picture_buffer_id_(picture_buffer_id), | |
64 texture_id_(texture_id), | |
65 size_(size) {} | |
66 | |
67 private: | |
68 int32 picture_buffer_id_; | |
69 uint32 texture_id_; | |
70 gfx::Size size_; | |
71 }; | |
72 | |
45 // |report_error_to_uma_cb| will be called independently from reporting | 73 // |report_error_to_uma_cb| will be called independently from reporting |
46 // errors to clients via method return values. | 74 // errors to clients via method return values. |
47 static scoped_ptr<VaapiWrapper> Create( | 75 static scoped_ptr<VaapiWrapper> Create( |
48 CodecMode mode, | 76 CodecMode mode, |
49 media::VideoCodecProfile profile, | 77 media::VideoCodecProfile profile, |
50 Display* x_display, | 78 gfx::GLContext* gl_context, |
79 const base::Callback<bool(void)>& make_context_current, | |
51 const base::Closure& report_error_to_uma_cb); | 80 const base::Closure& report_error_to_uma_cb); |
52 | 81 |
53 ~VaapiWrapper(); | 82 ~VaapiWrapper(); |
54 | 83 |
55 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each | 84 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each |
56 // of size |size|. Returns true when successful, with the created IDs in | 85 // of size |size|. Returns true when successful, with the created IDs in |
57 // |va_surfaces| to be managed and later wrapped in VASurfaces. | 86 // |va_surfaces| to be managed and later wrapped in VASurfaces. |
58 // The client must DestroySurfaces() each time before calling this method | 87 // 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 | 88 // 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 | 89 // at destruction time, as this will be done automatically from |
61 // the destructor. | 90 // the destructor. |
62 bool CreateSurfaces(gfx::Size size, | 91 bool CreateSurfaces(const gfx::Size& size, |
63 size_t num_surfaces, | 92 size_t num_surfaces, |
64 std::vector<VASurfaceID>* va_surfaces); | 93 std::vector<VASurfaceID>* va_surfaces); |
65 | 94 |
95 linked_ptr<Picture> CreatePicture(int32 picture_buffer_id, | |
96 uint32 texture_id, | |
97 gfx::Size size); | |
98 | |
66 // Free all memory allocated in CreateSurfaces. | 99 // Free all memory allocated in CreateSurfaces. |
67 void DestroySurfaces(); | 100 void DestroySurfaces(); |
68 | 101 |
69 // Submit parameters or slice data of |va_buffer_type|, copying them from | 102 // 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 | 103 // |buffer| of size |size|, into HW codec. The data in |buffer| is no |
71 // longer needed and can be freed after this method returns. | 104 // longer needed and can be freed after this method returns. |
72 // Data submitted via this method awaits in the HW codec until | 105 // Data submitted via this method awaits in the HW codec until |
73 // ExecuteAndDestroyPendingBuffers() is called to execute or | 106 // ExecuteAndDestroyPendingBuffers() is called to execute or |
74 // DestroyPendingBuffers() is used to cancel a pending job. | 107 // DestroyPendingBuffers() is used to cancel a pending job. |
75 bool SubmitBuffer(VABufferType va_buffer_type, size_t size, void* buffer); | 108 bool SubmitBuffer(VABufferType va_buffer_type, size_t size, void* buffer); |
76 | 109 |
77 // Submit a VAEncMiscParameterBuffer of given |misc_param_type|, copying its | 110 // 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 | 111 // 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. | 112 // no longer needed and can be freed after this method returns. |
80 // Data submitted via this method awaits in the HW codec until | 113 // Data submitted via this method awaits in the HW codec until |
81 // ExecuteAndDestroyPendingBuffers() is called to execute or | 114 // ExecuteAndDestroyPendingBuffers() is called to execute or |
82 // DestroyPendingBuffers() is used to cancel a pending job. | 115 // DestroyPendingBuffers() is used to cancel a pending job. |
83 bool SubmitVAEncMiscParamBuffer(VAEncMiscParameterType misc_param_type, | 116 bool SubmitVAEncMiscParamBuffer(VAEncMiscParameterType misc_param_type, |
84 size_t size, | 117 size_t size, |
85 void* buffer); | 118 void* buffer); |
86 | 119 |
87 // Cancel and destroy all buffers queued to the HW codec via SubmitBuffer(). | 120 // 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). | 121 // Useful when a pending job is to be cancelled (on reset or error). |
89 void DestroyPendingBuffers(); | 122 void DestroyPendingBuffers(); |
90 | 123 |
91 // Execute job in hardware on target |va_surface_id| and destroy pending | 124 // Execute job in hardware on target |va_surface_id| and destroy pending |
92 // buffers. Return false if Execute() fails. | 125 // buffers. Return false if Execute() fails. |
93 bool ExecuteAndDestroyPendingBuffers(VASurfaceID va_surface_id); | 126 bool ExecuteAndDestroyPendingBuffers(VASurfaceID va_surface_id); |
94 | 127 |
95 // Put data from |va_surface_id| into |x_pixmap| of size |size|, | 128 // Put data from |va_surface_id| into |picture|, converting/scaling to it. |
96 // converting/scaling to it. | 129 bool PutSurfaceIntoPicture(VASurfaceID va_surface_id, Picture* picture); |
97 bool PutSurfaceIntoPixmap(VASurfaceID va_surface_id, | |
98 Pixmap x_pixmap, | |
99 gfx::Size dest_size); | |
100 | 130 |
101 // Returns true if the VAAPI version is less than the specified version. | 131 // Returns true if the VAAPI version is less than the specified version. |
102 bool VAAPIVersionLessThan(int major, int minor); | 132 bool VAAPIVersionLessThan(int major, int minor); |
103 | 133 |
104 // Get a VAImage from a VASurface and map it into memory. The VAImage should | 134 // 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. | 135 // be released using the ReturnVaImage function. Returns true when successful. |
106 // This is intended for testing only. | 136 // This is intended for testing only. |
107 bool GetVaImageForTesting(VASurfaceID va_surface_id, | 137 bool GetVaImageForTesting(VASurfaceID va_surface_id, |
108 VAImage* image, | 138 VAImage* image, |
109 void** mem); | 139 void** mem); |
(...skipping 18 matching lines...) Expand all Loading... | |
128 bool DownloadAndDestroyCodedBuffer(VABufferID buffer_id, | 158 bool DownloadAndDestroyCodedBuffer(VABufferID buffer_id, |
129 VASurfaceID sync_surface_id, | 159 VASurfaceID sync_surface_id, |
130 uint8* target_ptr, | 160 uint8* target_ptr, |
131 size_t target_size, | 161 size_t target_size, |
132 size_t* coded_data_size); | 162 size_t* coded_data_size); |
133 | 163 |
134 // Destroy all previously-allocated (and not yet destroyed) coded buffers. | 164 // Destroy all previously-allocated (and not yet destroyed) coded buffers. |
135 void DestroyCodedBuffers(); | 165 void DestroyCodedBuffers(); |
136 | 166 |
137 private: | 167 private: |
168 class Backend; | |
169 class X11Backend; | |
Pawel Osciak
2014/08/25 01:13:24
All four declarations are unused here.
| |
170 class GbmBackend; | |
171 class TFPPicture; | |
172 class GbmPicture; | |
173 // friend class X11Backend; | |
174 // friend class GbmBackend; | |
175 | |
138 VaapiWrapper(); | 176 VaapiWrapper(); |
139 | 177 |
140 bool Initialize(CodecMode mode, | 178 bool Initialize(CodecMode mode, |
141 media::VideoCodecProfile profile, | 179 media::VideoCodecProfile profile, |
142 Display* x_display, | 180 gfx::GLContext* gl_context, |
181 const base::Callback<bool(void)>& make_context_current, | |
143 const base::Closure& report_error__to_uma_cb); | 182 const base::Closure& report_error__to_uma_cb); |
144 void Deinitialize(); | 183 void Deinitialize(); |
145 | 184 |
146 // Execute pending job in hardware and destroy pending buffers. Return false | 185 // Execute pending job in hardware and destroy pending buffers. Return false |
147 // if vaapi driver refuses to accept parameter or slice buffers submitted | 186 // if vaapi driver refuses to accept parameter or slice buffers submitted |
148 // by client, or if execution fails in hardware. | 187 // by client, or if execution fails in hardware. |
149 bool Execute(VASurfaceID va_surface_id); | 188 bool Execute(VASurfaceID va_surface_id); |
150 | 189 |
151 // Attempt to set render mode to "render to texture.". Failure is non-fatal. | 190 // Attempt to set render mode to "render to texture.". Failure is non-fatal. |
152 void TryToSetVADisplayAttributeToLocalGPU(); | 191 void TryToSetVADisplayAttributeToLocalGPU(); |
153 | 192 |
154 // Lazily initialize static data after sandbox is enabled. Return false on | 193 // Lazily initialize static data after sandbox is enabled. Return false on |
155 // init failure. | 194 // init failure. |
156 static bool PostSandboxInitialization(); | 195 static bool PostSandboxInitialization(); |
157 | 196 |
158 // Libva is not thread safe, so we have to do locking for it ourselves. | 197 // 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 | 198 // This lock is to be taken for the duration of all VA-API calls and for |
160 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers(). | 199 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers(). |
161 base::Lock va_lock_; | 200 base::Lock va_lock_; |
162 | 201 |
163 // Allocated ids for VASurfaces. | 202 // Allocated ids for VASurfaces. |
164 std::vector<VASurfaceID> va_surface_ids_; | 203 std::vector<VASurfaceID> va_surface_ids_; |
165 | 204 |
166 // The VAAPI version. | 205 // The VAAPI version. |
167 int major_version_, minor_version_; | 206 int major_version_, minor_version_; |
168 | 207 |
169 // VA handles. | 208 // VA handles. |
170 // Both valid after successful Initialize() and until Deinitialize(). | 209 // Both valid after successful Initialize() and until Deinitialize(). |
171 VADisplay va_display_; | |
172 VAConfigID va_config_id_; | 210 VAConfigID va_config_id_; |
173 // Created for the current set of va_surface_ids_ in CreateSurfaces() and | 211 // Created for the current set of va_surface_ids_ in CreateSurfaces() and |
174 // valid until DestroySurfaces(). | 212 // valid until DestroySurfaces(). |
175 VAContextID va_context_id_; | 213 VAContextID va_context_id_; |
176 | 214 |
177 // Data queued up for HW codec, to be committed on next execution. | 215 // Data queued up for HW codec, to be committed on next execution. |
178 std::vector<VABufferID> pending_slice_bufs_; | 216 std::vector<VABufferID> pending_slice_bufs_; |
179 std::vector<VABufferID> pending_va_bufs_; | 217 std::vector<VABufferID> pending_va_bufs_; |
180 | 218 |
181 // Bitstream buffers for encode. | 219 // Bitstream buffers for encode. |
182 std::set<VABufferID> coded_buffers_; | 220 std::set<VABufferID> coded_buffers_; |
183 | 221 |
184 // Called to report codec errors to UMA. Errors to clients are reported via | 222 // Called to report codec errors to UMA. Errors to clients are reported via |
185 // return values from public methods. | 223 // return values from public methods. |
186 base::Closure report_error_to_uma_cb_; | 224 base::Closure report_error_to_uma_cb_; |
187 | 225 |
226 // Backend for initialization/surface allocation on X11/DRM | |
227 scoped_refptr<Backend> backend_; | |
228 | |
188 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper); | 229 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper); |
189 }; | 230 }; |
190 | 231 |
191 } // namespace content | 232 } // namespace content |
192 | 233 |
193 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ | 234 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ |
OLD | NEW |