Chromium Code Reviews| 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 to interface | 6 // VaapiVideoDecodeAccelerator and VaapiH264Decoder for decode, |
| 7 // with libva (VA-API library for hardware video decode). | 7 // and VaapiVideoEncodeAccelerator for encode, to interface |
| 8 // with libva (VA-API library for hardware video codec). | |
| 8 | 9 |
| 9 #ifndef CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ | 10 #ifndef CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ |
| 10 #define CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ | 11 #define CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ |
| 11 | 12 |
| 13 #include <set> | |
| 14 #include <vector> | |
| 15 | |
| 12 #include "base/callback.h" | 16 #include "base/callback.h" |
| 13 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 14 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
| 15 #include "content/common/content_export.h" | 19 #include "content/common/content_export.h" |
| 16 #include "content/common/gpu/media/va_surface.h" | 20 #include "content/common/gpu/media/va_surface.h" |
| 17 #include "media/base/video_decoder_config.h" | 21 #include "media/base/video_decoder_config.h" |
| 18 #include "media/base/video_frame.h" | 22 #include "media/base/video_frame.h" |
| 19 #include "third_party/libva/va/va_x11.h" | 23 #include "third_party/libva/va/va_x11.h" |
| 20 #include "ui/gfx/size.h" | 24 #include "ui/gfx/size.h" |
| 21 | 25 |
| 22 namespace content { | 26 namespace content { |
| 23 | 27 |
| 24 // This class handles VA-API calls and ensures proper locking of VA-API calls | 28 // This class handles VA-API calls and ensures proper locking of VA-API calls |
| 25 // to libva, the userspace shim to the HW decoder driver. libva is not | 29 // to libva, the userspace shim to the HW codec driver. libva is not |
| 26 // thread-safe, so we have to perform locking ourselves. This class is fully | 30 // thread-safe, so we have to perform locking ourselves. This class is fully |
| 27 // synchronous and its methods can be called from any thread and may wait on | 31 // synchronous and its methods can be called from any thread and may wait on |
| 28 // the va_lock_ while other, concurrent calls run. | 32 // the va_lock_ while other, concurrent calls run. |
| 29 // | 33 // |
| 30 // This class is responsible for managing VAAPI connection, contexts and state. | 34 // This class is responsible for managing VAAPI connection, contexts and state. |
| 31 // It is also responsible for managing and freeing VABuffers (not VASurfaces), | 35 // It is also responsible for managing and freeing VABuffers (not VASurfaces), |
| 32 // which are used to queue decode parameters and slice data to the HW decoder, | 36 // which are used to queue parameters and slice data to the HW codec, |
| 33 // as well as underlying memory for VASurfaces themselves. | 37 // as well as underlying memory for VASurfaces themselves. |
| 34 class CONTENT_EXPORT VaapiWrapper { | 38 class CONTENT_EXPORT VaapiWrapper { |
| 35 public: | 39 public: |
| 40 enum CodecMode { | |
| 41 kDecode, | |
| 42 kEncode, | |
| 43 }; | |
| 44 | |
| 36 // |report_error_to_uma_cb| will be called independently from reporting | 45 // |report_error_to_uma_cb| will be called independently from reporting |
| 37 // errors to clients via method return values. | 46 // errors to clients via method return values. |
| 38 static scoped_ptr<VaapiWrapper> Create( | 47 static scoped_ptr<VaapiWrapper> Create( |
| 48 CodecMode mode, | |
| 39 media::VideoCodecProfile profile, | 49 media::VideoCodecProfile profile, |
| 40 Display* x_display, | 50 Display* x_display, |
| 41 const base::Closure& report_error_to_uma_cb); | 51 const base::Closure& report_error_to_uma_cb); |
| 42 | 52 |
| 43 ~VaapiWrapper(); | 53 ~VaapiWrapper(); |
| 44 | 54 |
| 45 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each | 55 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each |
| 46 // of size |size|. Returns true when successful, with the created IDs in | 56 // of size |size|. Returns true when successful, with the created IDs in |
| 47 // |va_surfaces| to be managed and later wrapped in VASurfaces. | 57 // |va_surfaces| to be managed and later wrapped in VASurfaces. |
| 48 // The client must DestroySurfaces() each time before calling this method | 58 // The client must DestroySurfaces() each time before calling this method |
| 49 // again to free the allocated surfaces first, but is not required to do so | 59 // again to free the allocated surfaces first, but is not required to do so |
| 50 // at destruction time, as this will be done automatically from | 60 // at destruction time, as this will be done automatically from |
| 51 // the destructor. | 61 // the destructor. |
| 52 bool CreateSurfaces(gfx::Size size, | 62 bool CreateSurfaces(gfx::Size size, |
| 53 size_t num_surfaces, | 63 size_t num_surfaces, |
| 54 std::vector<VASurfaceID>* va_surfaces); | 64 std::vector<VASurfaceID>* va_surfaces); |
| 55 | 65 |
| 56 // Free all memory allocated in CreateSurfaces. | 66 // Free all memory allocated in CreateSurfaces. |
| 57 void DestroySurfaces(); | 67 void DestroySurfaces(); |
| 58 | 68 |
| 59 // Submit parameters or slice data of |va_buffer_type|, copying them from | 69 // Submit parameters or slice data of |va_buffer_type|, copying them from |
| 60 // |buffer| of size |size|, into HW decoder. The data in |buffer| is no | 70 // |buffer| of size |size|, into HW codec. The data in |buffer| is no |
| 61 // longer needed and can be freed after this method returns. | 71 // longer needed and can be freed after this method returns. |
| 62 // Data submitted via this method awaits in the HW decoder until | 72 // Data submitted via this method awaits in the HW codec until |
| 63 // DecodeAndDestroyPendingBuffers is called to execute or | 73 // ExecuteAndDestroyPendingBuffers() is called to execute or |
| 64 // DestroyPendingBuffers is used to cancel a pending decode. | 74 // DestroyPendingBuffers() is used to cancel a pending job. |
| 65 bool SubmitBuffer(VABufferType va_buffer_type, size_t size, void* buffer); | 75 bool SubmitBuffer(VABufferType va_buffer_type, size_t size, void* buffer); |
| 66 | 76 |
| 67 // Cancel and destroy all buffers queued to the HW decoder via SubmitBuffer. | 77 // Submit a VAEncMiscParameterBuffer of given |misc_param_type|, copying its |
| 68 // Useful when a pending decode is to be cancelled (on reset or error). | 78 // 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. | |
| 80 // Data submitted via this method awaits in the HW codec until | |
| 81 // ExecuteAndDestroyPendingBuffers() is called to execute or | |
| 82 // DestroyPendingBuffers() is used to cancel a pending job. | |
| 83 bool SubmitVAEncMiscParamBuffer(VAEncMiscParameterType misc_param_type, | |
| 84 size_t size, | |
| 85 void* buffer); | |
| 86 | |
| 87 // 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). | |
| 69 void DestroyPendingBuffers(); | 89 void DestroyPendingBuffers(); |
| 70 | 90 |
| 71 // Execute decode in hardware into |va_surface_id} and destroy pending | 91 // Execute job in hardware on target |va_surface_id} and destroy pending |
|
wuchengli
2014/06/17 14:33:13
|va_surface_id} => |va_surface_id|
Pawel Osciak
2014/06/18 07:05:09
Done.
| |
| 72 // buffers. Return false if SubmitDecode() fails. | 92 // buffers. Return false if Execute() fails. |
| 73 bool DecodeAndDestroyPendingBuffers(VASurfaceID va_surface_id); | 93 bool ExecuteAndDestroyPendingBuffers(VASurfaceID va_surface_id); |
| 74 | 94 |
| 75 // Put data from |va_surface_id| into |x_pixmap| of size |size|, | 95 // Put data from |va_surface_id| into |x_pixmap| of size |size|, |
| 76 // converting/scaling to it. | 96 // converting/scaling to it. |
| 77 bool PutSurfaceIntoPixmap(VASurfaceID va_surface_id, | 97 bool PutSurfaceIntoPixmap(VASurfaceID va_surface_id, |
| 78 Pixmap x_pixmap, | 98 Pixmap x_pixmap, |
| 79 gfx::Size dest_size); | 99 gfx::Size dest_size); |
| 80 | 100 |
| 81 // Returns true if the VAAPI version is less than the specified version. | 101 // Returns true if the VAAPI version is less than the specified version. |
| 82 bool VAAPIVersionLessThan(int major, int minor); | 102 bool VAAPIVersionLessThan(int major, int minor); |
| 83 | 103 |
| 84 // Get a VAImage from a VASurface and map it into memory. The VAImage should | 104 // Get a VAImage from a VASurface and map it into memory. The VAImage should |
| 85 // be released using the ReturnVaImage function. Returns true when successful. | 105 // be released using the ReturnVaImage function. Returns true when successful. |
| 86 // This is intended for testing only. | 106 // This is intended for testing only. |
| 87 bool GetVaImageForTesting(VASurfaceID va_surface_id, | 107 bool GetVaImageForTesting(VASurfaceID va_surface_id, |
| 88 VAImage* image, | 108 VAImage* image, |
| 89 void** mem); | 109 void** mem); |
| 90 | 110 |
| 91 // Release the VAImage (and the associated memory mapping) obtained from | 111 // Release the VAImage (and the associated memory mapping) obtained from |
| 92 // GetVaImage(). This is intended for testing only. | 112 // GetVaImage(). This is intended for testing only. |
| 93 void ReturnVaImageForTesting(VAImage* image); | 113 void ReturnVaImageForTesting(VAImage* image); |
| 94 | 114 |
| 115 // Upload contents of |frame| into |va_surface_id| for encode. | |
| 116 bool UploadVideoFrameToSurface(const scoped_refptr<media::VideoFrame>& frame, | |
| 117 VASurfaceID va_surface_id); | |
| 118 | |
| 119 // Create a buffer of |size| bytes to be used as encode output. | |
| 120 bool CreateCodedBuffer(size_t size, VABufferID* buffer_id); | |
| 121 | |
| 122 // Download the contents of |buffer| into a buffer of size |target_size|, | |
| 123 // pointed to by |target_ptr|. The number of bytes downloaded will be | |
| 124 // returned in |coded_data_size|. |sync_surface_id| will be used as a sync | |
| 125 // point, i.e. it will have to become idle before starting the download. | |
| 126 // |sync_surface_id| should be the source surface passed to the encode job. | |
| 127 bool DownloadAndDestroyCodedBuffer(VABufferID buffer, | |
|
wuchengli
2014/06/17 14:33:13
buffer_id for consistency
Pawel Osciak
2014/06/18 07:05:09
Done.
| |
| 128 VASurfaceID sync_surface_id, | |
| 129 uint8* target_ptr, | |
| 130 size_t target_size, | |
| 131 size_t* coded_data_size); | |
| 132 | |
| 133 // Destroy all previously-allocated (and not yet destroyed) coded buffers. | |
| 134 void DestroyCodedBuffers(); | |
| 135 | |
| 95 private: | 136 private: |
| 96 VaapiWrapper(); | 137 VaapiWrapper(); |
| 97 | 138 |
| 98 bool Initialize(media::VideoCodecProfile profile, | 139 bool Initialize(CodecMode mode, |
| 140 media::VideoCodecProfile profile, | |
| 99 Display* x_display, | 141 Display* x_display, |
| 100 const base::Closure& report_error__to_uma_cb); | 142 const base::Closure& report_error__to_uma_cb); |
| 101 void Deinitialize(); | 143 void Deinitialize(); |
| 102 | 144 |
| 103 // Execute decode in hardware and destroy pending buffers. Return false if | 145 // Execute pending job in hardware and destroy pending buffers. Return false |
| 104 // vaapi driver refuses to accept parameter or slice buffers submitted | 146 // if vaapi driver refuses to accept parameter or slice buffers submitted |
| 105 // by client or if decode fails in hardware. | 147 // by client, or if execution fails in hardware. |
| 106 bool SubmitDecode(VASurfaceID va_surface_id); | 148 bool Execute(VASurfaceID va_surface_id); |
| 107 | 149 |
| 108 // Attempt to set render mode to "render to texture.". Failure is non-fatal. | 150 // Attempt to set render mode to "render to texture.". Failure is non-fatal. |
| 109 void TryToSetVADisplayAttributeToLocalGPU(); | 151 void TryToSetVADisplayAttributeToLocalGPU(); |
| 110 | 152 |
| 111 // Lazily initialize static data after sandbox is enabled. Return false on | 153 // Lazily initialize static data after sandbox is enabled. Return false on |
| 112 // init failure. | 154 // init failure. |
| 113 static bool PostSandboxInitialization(); | 155 static bool PostSandboxInitialization(); |
| 114 | 156 |
| 115 // Libva is not thread safe, so we have to do locking for it ourselves. | 157 // Libva is not thread safe, so we have to do locking for it ourselves. |
| 116 // This lock is to be taken for the duration of all VA-API calls and for | 158 // This lock is to be taken for the duration of all VA-API calls and for |
| 117 // the entire decode execution sequence in DecodeAndDestroyPendingBuffers(). | 159 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers(). |
| 118 base::Lock va_lock_; | 160 base::Lock va_lock_; |
| 119 | 161 |
| 120 // Allocated ids for VASurfaces. | 162 // Allocated ids for VASurfaces. |
| 121 std::vector<VASurfaceID> va_surface_ids_; | 163 std::vector<VASurfaceID> va_surface_ids_; |
| 122 | 164 |
| 123 // The VAAPI version. | 165 // The VAAPI version. |
| 124 int major_version_, minor_version_; | 166 int major_version_, minor_version_; |
| 125 | 167 |
| 126 // VA handles. | 168 // VA handles. |
| 127 // Both valid after successful Initialize() and until Deinitialize(). | 169 // Both valid after successful Initialize() and until Deinitialize(). |
| 128 VADisplay va_display_; | 170 VADisplay va_display_; |
| 129 VAConfigID va_config_id_; | 171 VAConfigID va_config_id_; |
| 130 // Created for the current set of va_surface_ids_ in CreateSurfaces() and | 172 // Created for the current set of va_surface_ids_ in CreateSurfaces() and |
| 131 // valid until DestroySurfaces(). | 173 // valid until DestroySurfaces(). |
| 132 VAContextID va_context_id_; | 174 VAContextID va_context_id_; |
| 133 | 175 |
| 134 // Data queued up for HW decoder, to be committed on next HW decode. | 176 // Data queued up for HW codec, to be committed on next execution. |
| 135 std::vector<VABufferID> pending_slice_bufs_; | 177 std::vector<VABufferID> pending_slice_bufs_; |
| 136 std::vector<VABufferID> pending_va_bufs_; | 178 std::vector<VABufferID> pending_va_bufs_; |
| 137 | 179 |
| 138 // Called to report decoding errors to UMA. Errors to clients are reported via | 180 // Bitstream buffers for encode. |
| 181 std::set<VABufferID> coded_buffers_; | |
| 182 | |
| 183 // Called to report codec errors to UMA. Errors to clients are reported via | |
| 139 // return values from public methods. | 184 // return values from public methods. |
| 140 base::Closure report_error_to_uma_cb_; | 185 base::Closure report_error_to_uma_cb_; |
| 141 | 186 |
| 142 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper); | 187 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper); |
| 143 }; | 188 }; |
| 144 | 189 |
| 145 } // namespace content | 190 } // namespace content |
| 146 | 191 |
| 147 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ | 192 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ |
| OLD | NEW |