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 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 | |
| 130 VADisplay GetDisplay(); | |
| 131 | |
| 137 private: | 132 private: |
| 133 | |
|
Pawel Osciak
2014/09/24 11:27:13
Please don't add newlines.
| |
| 138 VaapiWrapper(); | 134 VaapiWrapper(); |
| 139 | 135 |
| 140 bool Initialize(CodecMode mode, | 136 bool Initialize(VADisplay display, |
| 137 CodecMode mode, | |
| 141 media::VideoCodecProfile profile, | 138 media::VideoCodecProfile profile, |
| 142 Display* x_display, | |
| 143 const base::Closure& report_error__to_uma_cb); | 139 const base::Closure& report_error__to_uma_cb); |
| 144 void Deinitialize(); | 140 void Deinitialize(); |
| 145 | 141 |
| 146 // Execute pending job in hardware and destroy pending buffers. Return false | 142 // Execute pending job in hardware and destroy pending buffers. Return false |
| 147 // if vaapi driver refuses to accept parameter or slice buffers submitted | 143 // if vaapi driver refuses to accept parameter or slice buffers submitted |
| 148 // by client, or if execution fails in hardware. | 144 // by client, or if execution fails in hardware. |
| 149 bool Execute(VASurfaceID va_surface_id); | 145 bool Execute(VASurfaceID va_surface_id); |
| 150 | 146 |
| 151 // Attempt to set render mode to "render to texture.". Failure is non-fatal. | 147 // Attempt to set render mode to "render to texture.". Failure is non-fatal. |
| 152 void TryToSetVADisplayAttributeToLocalGPU(); | 148 void TryToSetVADisplayAttributeToLocalGPU(); |
| 153 | 149 |
| 154 // Lazily initialize static data after sandbox is enabled. Return false on | 150 // Lazily initialize static data after sandbox is enabled. Return false on |
| 155 // init failure. | 151 // init failure. |
| 156 static bool PostSandboxInitialization(); | 152 static bool PostSandboxInitialization(); |
| 157 | 153 |
| 158 // Libva is not thread safe, so we have to do locking for it ourselves. | 154 // 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 | 155 // This lock is to be taken for the duration of all VA-API calls and for |
| 160 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers(). | 156 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers(). |
| 161 base::Lock va_lock_; | 157 base::Lock va_lock_; |
| 162 | 158 |
| 163 // Allocated ids for VASurfaces. | 159 // Allocated ids for VASurfaces. |
| 164 std::vector<VASurfaceID> va_surface_ids_; | 160 std::vector<VASurfaceID> va_surface_ids_; |
| 165 | 161 |
| 166 // The VAAPI version. | 162 // The VAAPI version. |
| 167 int major_version_, minor_version_; | 163 int major_version_, minor_version_; |
| 168 | 164 |
| 169 // VA handles. | 165 // VA handles. |
| 170 // Both valid after successful Initialize() and until Deinitialize(). | 166 // All valid after successful Initialize() and until Deinitialize(). |
| 171 VADisplay va_display_; | 167 VADisplay va_display_; |
| 172 VAConfigID va_config_id_; | 168 VAConfigID va_config_id_; |
| 173 // Created for the current set of va_surface_ids_ in CreateSurfaces() and | 169 // Created for the current set of va_surface_ids_ in CreateSurfaces() and |
| 174 // valid until DestroySurfaces(). | 170 // valid until DestroySurfaces(). |
| 175 VAContextID va_context_id_; | 171 VAContextID va_context_id_; |
| 176 | 172 |
| 177 // Data queued up for HW codec, to be committed on next execution. | 173 // Data queued up for HW codec, to be committed on next execution. |
| 178 std::vector<VABufferID> pending_slice_bufs_; | 174 std::vector<VABufferID> pending_slice_bufs_; |
| 179 std::vector<VABufferID> pending_va_bufs_; | 175 std::vector<VABufferID> pending_va_bufs_; |
| 180 | 176 |
| 181 // Bitstream buffers for encode. | 177 // Bitstream buffers for encode. |
| 182 std::set<VABufferID> coded_buffers_; | 178 std::set<VABufferID> coded_buffers_; |
| 183 | 179 |
| 184 // Called to report codec errors to UMA. Errors to clients are reported via | 180 // Called to report codec errors to UMA. Errors to clients are reported via |
| 185 // return values from public methods. | 181 // return values from public methods. |
| 186 base::Closure report_error_to_uma_cb_; | 182 base::Closure report_error_to_uma_cb_; |
| 187 | 183 |
| 188 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper); | 184 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper); |
| 189 }; | 185 }; |
| 190 | 186 |
| 191 } // namespace content | 187 } // namespace content |
| 192 | 188 |
| 193 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ | 189 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ |
| OLD | NEW |