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