Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: content/common/gpu/media/vaapi_wrapper.h

Issue 490233002: VaapiVideoAccelerator: make Vaapi accelerator work with ozone (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Enable vaapi_h264_decoder_unittest on Ozone Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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" 17 #include "base/memory/ref_counted.h"
18 #include "base/synchronization/lock.h" 18 #include "base/synchronization/lock.h"
19 #include "content/common/content_export.h" 19 #include "content/common/content_export.h"
20 #include "content/common/gpu/media/va_surface.h" 20 #include "content/common/gpu/media/va_surface.h"
21 #include "media/base/video_decoder_config.h" 21 #include "media/base/video_decoder_config.h"
22 #include "media/base/video_frame.h" 22 #include "media/base/video_frame.h"
23 #include "third_party/libva/va/va.h"
24 #include "third_party/libva/va/va_vpp.h"
25 #include "ui/gfx/size.h"
26 #if defined(USE_X11)
23 #include "third_party/libva/va/va_x11.h" 27 #include "third_party/libva/va/va_x11.h"
24 #include "ui/gfx/size.h" 28 #endif // USE_X11
25 29
26 namespace content { 30 namespace content {
27 31
28 // This class handles VA-API calls and ensures proper locking of VA-API calls 32 // 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 33 // 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 34 // 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 35 // synchronous and its methods can be called from any thread and may wait on
32 // the va_lock_ while other, concurrent calls run. 36 // the va_lock_ while other, concurrent calls run.
33 // 37 //
34 // This class is responsible for managing VAAPI connection, contexts and state. 38 // This class is responsible for managing VAAPI connection, contexts and state.
35 // It is also responsible for managing and freeing VABuffers (not VASurfaces), 39 // 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, 40 // which are used to queue parameters and slice data to the HW codec,
37 // as well as underlying memory for VASurfaces themselves. 41 // as well as underlying memory for VASurfaces themselves.
38 class CONTENT_EXPORT VaapiWrapper { 42 class CONTENT_EXPORT VaapiWrapper : public base::RefCounted<VaapiWrapper> {
39 public: 43 public:
40 enum CodecMode { 44 enum CodecMode {
41 kDecode, 45 kDecode,
42 kEncode, 46 kEncode,
43 }; 47 };
44 48
45 // |report_error_to_uma_cb| will be called independently from reporting 49 // |report_error_to_uma_cb| will be called independently from reporting
46 // errors to clients via method return values. 50 // errors to clients via method return values.
47 static scoped_ptr<VaapiWrapper> Create( 51 static scoped_refptr<VaapiWrapper> Create(
48 CodecMode mode, 52 CodecMode mode,
49 media::VideoCodecProfile profile, 53 media::VideoCodecProfile profile,
50 Display* x_display,
51 const base::Closure& report_error_to_uma_cb); 54 const base::Closure& report_error_to_uma_cb);
52 55
53 // Return the supported encode profiles. 56 // Return the supported encode profiles.
54 static std::vector<media::VideoCodecProfile> GetSupportedEncodeProfiles( 57 static std::vector<media::VideoCodecProfile> GetSupportedEncodeProfiles(
55 Display* x_display,
56 const base::Closure& report_error_to_uma_cb); 58 const base::Closure& report_error_to_uma_cb);
57 59
58 ~VaapiWrapper();
59
60 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each 60 // Create |num_surfaces| backing surfaces in driver for VASurfaces, each
61 // of size |size|. Returns true when successful, with the created IDs in 61 // of size |size|. Returns true when successful, with the created IDs in
62 // |va_surfaces| to be managed and later wrapped in VASurfaces. 62 // |va_surfaces| to be managed and later wrapped in VASurfaces.
63 // The client must DestroySurfaces() each time before calling this method 63 // The client must DestroySurfaces() each time before calling this method
64 // again to free the allocated surfaces first, but is not required to do so 64 // again to free the allocated surfaces first, but is not required to do so
65 // at destruction time, as this will be done automatically from 65 // at destruction time, as this will be done automatically from
66 // the destructor. 66 // the destructor.
67 bool CreateSurfaces(gfx::Size size, 67 bool CreateSurfaces(const gfx::Size& size,
68 size_t num_surfaces, 68 size_t num_surfaces,
69 std::vector<VASurfaceID>* va_surfaces); 69 std::vector<VASurfaceID>* va_surfaces);
70 70
71 // Free all memory allocated in CreateSurfaces. 71 // Free all memory allocated in CreateSurfaces.
72 void DestroySurfaces(); 72 void DestroySurfaces();
73 73
74 // Create a VASurface of |va_format|, |size| and using
75 // |num_va_attribs| attributes from |va_attribs|. The ownership of
76 // the surface is transfered to the call. It differs from surfaces
77 // created using CreateSurfaces() where VaapiWrapper is the owner of
78 // the surfaces.
79 scoped_refptr<VASurface> CreateUnownedSurface(unsigned int va_format,
80 const gfx::Size& size,
81 VASurfaceAttrib* va_attribs,
82 size_t num_va_attribs);
83
74 // Submit parameters or slice data of |va_buffer_type|, copying them from 84 // Submit parameters or slice data of |va_buffer_type|, copying them from
75 // |buffer| of size |size|, into HW codec. The data in |buffer| is no 85 // |buffer| of size |size|, into HW codec. The data in |buffer| is no
76 // longer needed and can be freed after this method returns. 86 // longer needed and can be freed after this method returns.
77 // Data submitted via this method awaits in the HW codec until 87 // Data submitted via this method awaits in the HW codec until
78 // ExecuteAndDestroyPendingBuffers() is called to execute or 88 // ExecuteAndDestroyPendingBuffers() is called to execute or
79 // DestroyPendingBuffers() is used to cancel a pending job. 89 // DestroyPendingBuffers() is used to cancel a pending job.
80 bool SubmitBuffer(VABufferType va_buffer_type, size_t size, void* buffer); 90 bool SubmitBuffer(VABufferType va_buffer_type, size_t size, void* buffer);
81 91
82 // Submit a VAEncMiscParameterBuffer of given |misc_param_type|, copying its 92 // Submit a VAEncMiscParameterBuffer of given |misc_param_type|, copying its
83 // data from |buffer| of size |size|, into HW codec. The data in |buffer| is 93 // data from |buffer| of size |size|, into HW codec. The data in |buffer| is
84 // no longer needed and can be freed after this method returns. 94 // no longer needed and can be freed after this method returns.
85 // Data submitted via this method awaits in the HW codec until 95 // Data submitted via this method awaits in the HW codec until
86 // ExecuteAndDestroyPendingBuffers() is called to execute or 96 // ExecuteAndDestroyPendingBuffers() is called to execute or
87 // DestroyPendingBuffers() is used to cancel a pending job. 97 // DestroyPendingBuffers() is used to cancel a pending job.
88 bool SubmitVAEncMiscParamBuffer(VAEncMiscParameterType misc_param_type, 98 bool SubmitVAEncMiscParamBuffer(VAEncMiscParameterType misc_param_type,
89 size_t size, 99 size_t size,
90 void* buffer); 100 void* buffer);
91 101
92 // Cancel and destroy all buffers queued to the HW codec via SubmitBuffer(). 102 // Cancel and destroy all buffers queued to the HW codec via SubmitBuffer().
93 // Useful when a pending job is to be cancelled (on reset or error). 103 // Useful when a pending job is to be cancelled (on reset or error).
94 void DestroyPendingBuffers(); 104 void DestroyPendingBuffers();
95 105
96 // Execute job in hardware on target |va_surface_id| and destroy pending 106 // Execute job in hardware on target |va_surface_id| and destroy pending
97 // buffers. Return false if Execute() fails. 107 // buffers. Return false if Execute() fails.
98 bool ExecuteAndDestroyPendingBuffers(VASurfaceID va_surface_id); 108 bool ExecuteAndDestroyPendingBuffers(VASurfaceID va_surface_id);
99 109
100 // Put data from |va_surface_id| into |x_pixmap| of size |size|,
101 // converting/scaling to it.
102 bool PutSurfaceIntoPixmap(VASurfaceID va_surface_id,
103 Pixmap x_pixmap,
104 gfx::Size dest_size);
105
106 // Returns true if the VAAPI version is less than the specified version. 110 // Returns true if the VAAPI version is less than the specified version.
107 bool VAAPIVersionLessThan(int major, int minor); 111 bool VAAPIVersionLessThan(int major, int minor);
108 112
109 // Get a VAImage from a VASurface and map it into memory. The VAImage should 113 // Get a VAImage from a VASurface and map it into memory. The VAImage should
110 // be released using the ReturnVaImage function. Returns true when successful. 114 // be released using the ReturnVaImage function. Returns true when successful.
111 // This is intended for testing only. 115 // This is intended for testing only.
112 bool GetVaImageForTesting(VASurfaceID va_surface_id, 116 bool GetVaImageForTesting(VASurfaceID va_surface_id,
113 VAImage* image, 117 VAImage* image,
114 void** mem); 118 void** mem);
115 119
(...skipping 16 matching lines...) Expand all
132 // to the encode job. 136 // to the encode job.
133 bool DownloadAndDestroyCodedBuffer(VABufferID buffer_id, 137 bool DownloadAndDestroyCodedBuffer(VABufferID buffer_id,
134 VASurfaceID sync_surface_id, 138 VASurfaceID sync_surface_id,
135 uint8* target_ptr, 139 uint8* target_ptr,
136 size_t target_size, 140 size_t target_size,
137 size_t* coded_data_size); 141 size_t* coded_data_size);
138 142
139 // Destroy all previously-allocated (and not yet destroyed) coded buffers. 143 // Destroy all previously-allocated (and not yet destroyed) coded buffers.
140 void DestroyCodedBuffers(); 144 void DestroyCodedBuffers();
141 145
146 // Blits a VASurface |va_surface_id_src| into another VASurface
147 // |va_surface_id_dest| applying pixel format conversion and scaling
148 // if needed.
149 bool BlitSurface(VASurfaceID va_surface_id_src,
150 const gfx::Size& src_size,
151 VASurfaceID va_surface_id_dest,
152 const gfx::Size& dest_size);
153
154 #if defined(USE_X11)
155 // Put data from |va_surface_id| into |x_pixmap| of size |size|,
156 // converting/scaling to it.
157 bool PutSurfaceIntoPixmap(VASurfaceID va_surface_id,
158 Pixmap x_pixmap,
159 gfx::Size dest_size);
160 #endif // USE_X11
161
142 private: 162 private:
163 friend class base::RefCounted<VaapiWrapper>;
143 VaapiWrapper(); 164 VaapiWrapper();
165 ~VaapiWrapper();
144 166
145 bool Initialize(CodecMode mode, 167 bool Initialize(CodecMode mode,
146 media::VideoCodecProfile profile, 168 media::VideoCodecProfile profile,
147 Display* x_display, 169 const base::Closure& report_error__to_uma_cb);
148 const base::Closure& report_error_to_uma_cb);
149 void Deinitialize(); 170 void Deinitialize();
150 bool VaInitialize(Display* x_display, 171 bool VaInitialize(const base::Closure& report_error_to_uma_cb);
151 const base::Closure& report_error_to_uma_cb);
152 bool GetSupportedVaProfiles(std::vector<VAProfile>* profiles); 172 bool GetSupportedVaProfiles(std::vector<VAProfile>* profiles);
153 bool IsEntrypointSupported(VAProfile va_profile, VAEntrypoint entrypoint); 173 bool IsEntrypointSupported(VAProfile va_profile, VAEntrypoint entrypoint);
154 bool AreAttribsSupported(VAProfile va_profile, 174 bool AreAttribsSupported(VAProfile va_profile,
155 VAEntrypoint entrypoint, 175 VAEntrypoint entrypoint,
156 const std::vector<VAConfigAttrib>& required_attribs); 176 const std::vector<VAConfigAttrib>& required_attribs);
157 177
178 // Destroys a |va_surface| created using CreateUnownedSurface.
179 void DestroyUnownedSurface(VASurfaceID va_surface);
180
181 // Initialize the video post processing context with the |size| of
182 // the input pictures to be processed.
183 bool InitializeVpp_Locked();
184
185 // Deinitialize the video post processing context.
186 void DeinitializeVpp();
187 void DeinitializeVpp_Locked();
188
158 // Execute pending job in hardware and destroy pending buffers. Return false 189 // Execute pending job in hardware and destroy pending buffers. Return false
159 // if vaapi driver refuses to accept parameter or slice buffers submitted 190 // if vaapi driver refuses to accept parameter or slice buffers submitted
160 // by client, or if execution fails in hardware. 191 // by client, or if execution fails in hardware.
161 bool Execute(VASurfaceID va_surface_id); 192 bool Execute(VASurfaceID va_surface_id);
162 193
163 // Attempt to set render mode to "render to texture.". Failure is non-fatal. 194 // Attempt to set render mode to "render to texture.". Failure is non-fatal.
164 void TryToSetVADisplayAttributeToLocalGPU(); 195 void TryToSetVADisplayAttributeToLocalGPU();
165 196
166 // Lazily initialize static data after sandbox is enabled. Return false on 197 // Lazily initialize static data after sandbox is enabled. Return false on
167 // init failure. 198 // init failure.
168 static bool PostSandboxInitialization(); 199 static bool PostSandboxInitialization();
169 200
170 // Libva is not thread safe, so we have to do locking for it ourselves. 201 // Libva is not thread safe, so we have to do locking for it ourselves.
171 // This lock is to be taken for the duration of all VA-API calls and for 202 // This lock is to be taken for the duration of all VA-API calls and for
172 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers(). 203 // the entire job submission sequence in ExecuteAndDestroyPendingBuffers().
173 base::Lock va_lock_; 204 base::Lock va_lock_;
174 205
175 // Allocated ids for VASurfaces. 206 // Allocated ids for VASurfaces.
176 std::vector<VASurfaceID> va_surface_ids_; 207 std::vector<VASurfaceID> va_surface_ids_;
177 208
178 // The VAAPI version. 209 // The VAAPI version.
179 int major_version_, minor_version_; 210 int major_version_, minor_version_;
180 211
181 // VA handles. 212 // VA handles.
182 // Both valid after successful Initialize() and until Deinitialize(). 213 // All valid after successful Initialize() and until Deinitialize().
183 VADisplay va_display_; 214 VADisplay va_display_;
184 VAConfigID va_config_id_; 215 VAConfigID va_config_id_;
185 // Created for the current set of va_surface_ids_ in CreateSurfaces() and 216 // Created for the current set of va_surface_ids_ in CreateSurfaces() and
186 // valid until DestroySurfaces(). 217 // valid until DestroySurfaces().
187 VAContextID va_context_id_; 218 VAContextID va_context_id_;
188 219
189 // Data queued up for HW codec, to be committed on next execution. 220 // Data queued up for HW codec, to be committed on next execution.
190 std::vector<VABufferID> pending_slice_bufs_; 221 std::vector<VABufferID> pending_slice_bufs_;
191 std::vector<VABufferID> pending_va_bufs_; 222 std::vector<VABufferID> pending_va_bufs_;
192 223
193 // Bitstream buffers for encode. 224 // Bitstream buffers for encode.
194 std::set<VABufferID> coded_buffers_; 225 std::set<VABufferID> coded_buffers_;
195 226
196 // Called to report codec errors to UMA. Errors to clients are reported via 227 // Called to report codec errors to UMA. Errors to clients are reported via
197 // return values from public methods. 228 // return values from public methods.
198 base::Closure report_error_to_uma_cb_; 229 base::Closure report_error_to_uma_cb_;
199 230
231 // VPP context
232 VAConfigID va_vpp_config_id_;
233 VAContextID va_vpp_context_id_;
234 VABufferID va_vpp_buffer_id_;
235
200 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper); 236 DISALLOW_COPY_AND_ASSIGN(VaapiWrapper);
201 }; 237 };
202 238
203 } // namespace content 239 } // namespace content
204 240
205 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_ 241 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_WRAPPER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698