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" | |
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" |
23 #include "third_party/libva/va/va_x11.h" | 22 #include "third_party/libva/va/va.h" |
24 #include "ui/gfx/size.h" | 23 #include "ui/gfx/size.h" |
25 | 24 |
26 namespace content { | 25 namespace content { |
27 | 26 |
28 // This class handles VA-API calls and ensures proper locking of VA-API calls | 27 // 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 | 28 // 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 | 29 // 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 | 30 // synchronous and its methods can be called from any thread and may wait on |
32 // the va_lock_ while other, concurrent calls run. | 31 // the va_lock_ while other, concurrent calls run. |
33 // | 32 // |
34 // This class is responsible for managing VAAPI connection, contexts and state. | 33 // This class is responsible for managing VAAPI connection, contexts and state. |
35 // It is also responsible for managing and freeing VABuffers (not VASurfaces), | 34 // 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, | 35 // which are used to queue parameters and slice data to the HW codec, |
37 // as well as underlying memory for VASurfaces themselves. | 36 // as well as underlying memory for VASurfaces themselves. |
38 class CONTENT_EXPORT VaapiWrapper { | 37 class CONTENT_EXPORT VaapiWrapper { |
39 public: | 38 public: |
40 enum CodecMode { | 39 enum CodecMode { |
41 kDecode, | 40 kDecode, |
42 kEncode, | 41 kEncode, |
43 }; | 42 }; |
44 | 43 |
45 // |report_error_to_uma_cb| will be called independently from reporting | 44 // |report_error_to_uma_cb| will be called independently from reporting |
46 // errors to clients via method return values. | 45 // errors to clients via method return values. |
47 static scoped_ptr<VaapiWrapper> Create( | 46 static scoped_ptr<VaapiWrapper> Create( |
48 CodecMode mode, | 47 CodecMode mode, |
49 media::VideoCodecProfile profile, | 48 media::VideoCodecProfile profile, |
50 Display* x_display, | |
51 const base::Closure& report_error_to_uma_cb); | 49 const base::Closure& report_error_to_uma_cb); |
52 | 50 |
53 ~VaapiWrapper(); | 51 ~VaapiWrapper(); |
54 | 52 |
55 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each | 53 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each |
56 // of size |size|. Returns true when successful, with the created IDs in | 54 // of size |size|. Returns true when successful, with the created IDs in |
57 // |va_surfaces| to be managed and later wrapped in VASurfaces. | 55 // |va_surfaces| to be managed and later wrapped in VASurfaces. |
58 // The client must DestroySurfaces() each time before calling this method | 56 // 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 | 57 // 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 | 58 // at destruction time, as this will be done automatically from |
61 // the destructor. | 59 // the destructor. |
62 bool CreateSurfaces(gfx::Size size, | 60 bool CreateSurfaces(const gfx::Size& size, |
63 size_t num_surfaces, | 61 size_t num_surfaces, |
64 std::vector<VASurfaceID>* va_surfaces); | 62 std::vector<VASurfaceID>* va_surfaces); |
65 | 63 |
66 // Free all memory allocated in CreateSurfaces. | 64 // Free all memory allocated in CreateSurfaces. |
67 void DestroySurfaces(); | 65 void DestroySurfaces(); |
68 | 66 |
69 // Submit parameters or slice data of |va_buffer_type|, copying them from | 67 // 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 | 68 // |buffer| of size |size|, into HW codec. The data in |buffer| is no |
71 // longer needed and can be freed after this method returns. | 69 // longer needed and can be freed after this method returns. |
72 // Data submitted via this method awaits in the HW codec until | 70 // Data submitted via this method awaits in the HW codec until |
(...skipping 12 matching lines...) Expand all Loading... | |
85 void* buffer); | 83 void* buffer); |
86 | 84 |
87 // Cancel and destroy all buffers queued to the HW codec via SubmitBuffer(). | 85 // 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). | 86 // Useful when a pending job is to be cancelled (on reset or error). |
89 void DestroyPendingBuffers(); | 87 void DestroyPendingBuffers(); |
90 | 88 |
91 // Execute job in hardware on target |va_surface_id| and destroy pending | 89 // Execute job in hardware on target |va_surface_id| and destroy pending |
92 // buffers. Return false if Execute() fails. | 90 // buffers. Return false if Execute() fails. |
93 bool ExecuteAndDestroyPendingBuffers(VASurfaceID va_surface_id); | 91 bool ExecuteAndDestroyPendingBuffers(VASurfaceID va_surface_id); |
94 | 92 |
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. | 93 // Returns true if the VAAPI version is less than the specified version. |
102 bool VAAPIVersionLessThan(int major, int minor); | 94 bool VAAPIVersionLessThan(int major, int minor); |
103 | 95 |
104 // Get a VAImage from a VASurface and map it into memory. The VAImage should | 96 // 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. | 97 // be released using the ReturnVaImage function. Returns true when successful. |
106 // This is intended for testing only. | 98 // This is intended for testing only. |
107 bool GetVaImageForTesting(VASurfaceID va_surface_id, | 99 bool GetVaImageForTesting(VASurfaceID va_surface_id, |
108 VAImage* image, | 100 VAImage* image, |
109 void** mem); | 101 void** mem); |
110 | 102 |
(...skipping 16 matching lines...) Expand all Loading... | |
127 // to the encode job. | 119 // to the encode job. |
128 bool DownloadAndDestroyCodedBuffer(VABufferID buffer_id, | 120 bool DownloadAndDestroyCodedBuffer(VABufferID buffer_id, |
129 VASurfaceID sync_surface_id, | 121 VASurfaceID sync_surface_id, |
130 uint8* target_ptr, | 122 uint8* target_ptr, |
131 size_t target_size, | 123 size_t target_size, |
132 size_t* coded_data_size); | 124 size_t* coded_data_size); |
133 | 125 |
134 // Destroy all previously-allocated (and not yet destroyed) coded buffers. | 126 // Destroy all previously-allocated (and not yet destroyed) coded buffers. |
135 void DestroyCodedBuffers(); | 127 void DestroyCodedBuffers(); |
136 | 128 |
129 // Returns the initialized VADisplay | |
Pawel Osciak
2014/10/08 08:17:22
I feel really uncomfortable about this. We should
llandwerlin-old
2014/10/08 09:31:18
I will implement the VPP functions into the VaapiW
| |
130 VADisplay GetDisplay(); | |
131 | |
137 private: | 132 private: |
138 VaapiWrapper(); | 133 VaapiWrapper(); |
139 | 134 |
140 bool Initialize(CodecMode mode, | 135 bool Initialize(VADisplay display, |
136 CodecMode mode, | |
141 media::VideoCodecProfile profile, | 137 media::VideoCodecProfile profile, |
142 Display* x_display, | |
143 const base::Closure& report_error__to_uma_cb); | 138 const base::Closure& report_error__to_uma_cb); |
144 void Deinitialize(); | 139 void Deinitialize(); |
145 | 140 |
146 // Execute pending job in hardware and destroy pending buffers. Return false | 141 // Execute pending job in hardware and destroy pending buffers. Return false |
147 // if vaapi driver refuses to accept parameter or slice buffers submitted | 142 // if vaapi driver refuses to accept parameter or slice buffers submitted |
148 // by client, or if execution fails in hardware. | 143 // by client, or if execution fails in hardware. |
149 bool Execute(VASurfaceID va_surface_id); | 144 bool Execute(VASurfaceID va_surface_id); |
150 | 145 |
151 // Attempt to set render mode to "render to texture.". Failure is non-fatal. | 146 // Attempt to set render mode to "render to texture.". Failure is non-fatal. |
152 void TryToSetVADisplayAttributeToLocalGPU(); | 147 void TryToSetVADisplayAttributeToLocalGPU(); |
153 | 148 |
154 // Lazily initialize static data after sandbox is enabled. Return false on | 149 // Lazily initialize static data after sandbox is enabled. Return false on |
155 // init failure. | 150 // init failure. |
156 static bool PostSandboxInitialization(); | 151 static bool PostSandboxInitialization(); |
157 | 152 |
158 // Libva is not thread safe, so we have to do locking for it ourselves. | 153 // 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 | 154 // This lock is to be taken for the duration of all VA-API calls and for |
160 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers(). | 155 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers(). |
161 base::Lock va_lock_; | 156 base::Lock va_lock_; |
162 | 157 |
163 // Allocated ids for VASurfaces. | 158 // Allocated ids for VASurfaces. |
164 std::vector<VASurfaceID> va_surface_ids_; | 159 std::vector<VASurfaceID> va_surface_ids_; |
165 | 160 |
166 // The VAAPI version. | 161 // The VAAPI version. |
167 int major_version_, minor_version_; | 162 int major_version_, minor_version_; |
168 | 163 |
169 // VA handles. | 164 // VA handles. |
170 // Both valid after successful Initialize() and until Deinitialize(). | 165 // All valid after successful Initialize() and until Deinitialize(). |
171 VADisplay va_display_; | 166 VADisplay va_display_; |
172 VAConfigID va_config_id_; | 167 VAConfigID va_config_id_; |
173 // Created for the current set of va_surface_ids_ in CreateSurfaces() and | 168 // Created for the current set of va_surface_ids_ in CreateSurfaces() and |
174 // valid until DestroySurfaces(). | 169 // valid until DestroySurfaces(). |
175 VAContextID va_context_id_; | 170 VAContextID va_context_id_; |
176 | 171 |
177 // Data queued up for HW codec, to be committed on next execution. | 172 // Data queued up for HW codec, to be committed on next execution. |
178 std::vector<VABufferID> pending_slice_bufs_; | 173 std::vector<VABufferID> pending_slice_bufs_; |
179 std::vector<VABufferID> pending_va_bufs_; | 174 std::vector<VABufferID> pending_va_bufs_; |
180 | 175 |
181 // Bitstream buffers for encode. | 176 // Bitstream buffers for encode. |
182 std::set<VABufferID> coded_buffers_; | 177 std::set<VABufferID> coded_buffers_; |
183 | 178 |
184 // Called to report codec errors to UMA. Errors to clients are reported via | 179 // Called to report codec errors to UMA. Errors to clients are reported via |
185 // return values from public methods. | 180 // return values from public methods. |
186 base::Closure report_error_to_uma_cb_; | 181 base::Closure report_error_to_uma_cb_; |
187 | 182 |
188 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper); | 183 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper); |
189 }; | 184 }; |
190 | 185 |
191 } // namespace content | 186 } // namespace content |
192 | 187 |
193 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ | 188 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ |
OLD | NEW |