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

Side by Side Diff: media/gpu/vaapi_wrapper.cc

Issue 2668813002: Remove LazyInstance usage from media/ (Closed)
Patch Set: Created 3 years, 10 months 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 #include "media/gpu/vaapi_wrapper.h" 5 #include "media/gpu/vaapi_wrapper.h"
6 6
7 #include <dlfcn.h> 7 #include <dlfcn.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 97
98 } // namespace 98 } // namespace
99 #endif 99 #endif
100 100
101 namespace media { 101 namespace media {
102 102
103 // Maximum framerate of encoded profile. This value is an arbitary limit 103 // Maximum framerate of encoded profile. This value is an arbitary limit
104 // and not taken from HW documentation. 104 // and not taken from HW documentation.
105 const int kMaxEncoderFramerate = 30; 105 const int kMaxEncoderFramerate = 30;
106 106
107 base::LazyInstance<VaapiWrapper::VADisplayState>
scottmg 2017/01/31 21:10:34 Just confirming that you really meant for these to
DaleCurtis 2017/01/31 22:04:33 Nope, definitely did not. Totally fubared this one
108 VaapiWrapper::va_display_state_ = LAZY_INSTANCE_INITIALIZER;
109
110 base::LazyInstance<VaapiWrapper::LazyProfileInfos>
111 VaapiWrapper::profile_infos_ = LAZY_INSTANCE_INITIALIZER;
112
113 // Config attributes common for both encode and decode. 107 // Config attributes common for both encode and decode.
114 static const VAConfigAttrib kCommonVAConfigAttribs[] = { 108 static const VAConfigAttrib kCommonVAConfigAttribs[] = {
115 {VAConfigAttribRTFormat, VA_RT_FORMAT_YUV420}, 109 {VAConfigAttribRTFormat, VA_RT_FORMAT_YUV420},
116 }; 110 };
117 111
118 // Attributes required for encode. 112 // Attributes required for encode.
119 static const VAConfigAttrib kEncodeVAConfigAttribs[] = { 113 static const VAConfigAttrib kEncodeVAConfigAttribs[] = {
120 {VAConfigAttribRateControl, VA_RC_CBR}, 114 {VAConfigAttribRateControl, VA_RC_CBR},
121 {VAConfigAttribEncPackedHeaders, 115 {VAConfigAttribEncPackedHeaders,
122 VA_ENC_PACKED_HEADER_SEQUENCE | VA_ENC_PACKED_HEADER_PICTURE}, 116 VA_ENC_PACKED_HEADER_SEQUENCE | VA_ENC_PACKED_HEADER_PICTURE},
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 } 164 }
171 165
172 VaapiWrapper::VaapiWrapper() 166 VaapiWrapper::VaapiWrapper()
173 : va_surface_format_(0), 167 : va_surface_format_(0),
174 va_display_(NULL), 168 va_display_(NULL),
175 va_config_id_(VA_INVALID_ID), 169 va_config_id_(VA_INVALID_ID),
176 va_context_id_(VA_INVALID_ID), 170 va_context_id_(VA_INVALID_ID),
177 va_vpp_config_id_(VA_INVALID_ID), 171 va_vpp_config_id_(VA_INVALID_ID),
178 va_vpp_context_id_(VA_INVALID_ID), 172 va_vpp_context_id_(VA_INVALID_ID),
179 va_vpp_buffer_id_(VA_INVALID_ID) { 173 va_vpp_buffer_id_(VA_INVALID_ID) {
180 va_lock_ = va_display_state_.Get().va_lock(); 174 static VADisplayState* display_state = new VADisplayState();
175 va_display_state_ = display_state;
176
177 static LazyProfileInfos* profile_infos = new LazyProfileInfos();
178 profile_infos_ = profile_infos;
179
180 va_lock_ = va_display_state_->va_lock();
181 } 181 }
182 182
183 VaapiWrapper::~VaapiWrapper() { 183 VaapiWrapper::~VaapiWrapper() {
184 DestroyPendingBuffers(); 184 DestroyPendingBuffers();
185 DestroyCodedBuffers(); 185 DestroyCodedBuffers();
186 DestroySurfaces(); 186 DestroySurfaces();
187 DeinitializeVpp(); 187 DeinitializeVpp();
188 Deinitialize(); 188 Deinitialize();
189 } 189 }
190 190
191 // static 191 // static
192 scoped_refptr<VaapiWrapper> VaapiWrapper::Create( 192 scoped_refptr<VaapiWrapper> VaapiWrapper::Create(
193 CodecMode mode, 193 CodecMode mode,
194 VAProfile va_profile, 194 VAProfile va_profile,
195 const base::Closure& report_error_to_uma_cb) { 195 const base::Closure& report_error_to_uma_cb) {
196 if (!profile_infos_.Get().IsProfileSupported(mode, va_profile)) { 196 if (!profile_infos_->IsProfileSupported(mode, va_profile)) {
scottmg 2017/01/31 21:14:51 Oh, actually, I think this won't work too. So mayb
197 DVLOG(1) << "Unsupported va_profile: " << va_profile; 197 DVLOG(1) << "Unsupported va_profile: " << va_profile;
198 return nullptr; 198 return nullptr;
199 } 199 }
200 200
201 scoped_refptr<VaapiWrapper> vaapi_wrapper(new VaapiWrapper()); 201 scoped_refptr<VaapiWrapper> vaapi_wrapper(new VaapiWrapper());
202 if (vaapi_wrapper->VaInitialize(report_error_to_uma_cb)) { 202 if (vaapi_wrapper->VaInitialize(report_error_to_uma_cb)) {
203 if (vaapi_wrapper->Initialize(mode, va_profile)) 203 if (vaapi_wrapper->Initialize(mode, va_profile))
204 return vaapi_wrapper; 204 return vaapi_wrapper;
205 } 205 }
206 LOG(ERROR) << "Failed to create VaapiWrapper for va_profile: " << va_profile; 206 LOG(ERROR) << "Failed to create VaapiWrapper for va_profile: " << va_profile;
207 return nullptr; 207 return nullptr;
208 } 208 }
209 209
210 // static 210 // static
211 scoped_refptr<VaapiWrapper> VaapiWrapper::CreateForVideoCodec( 211 scoped_refptr<VaapiWrapper> VaapiWrapper::CreateForVideoCodec(
212 CodecMode mode, 212 CodecMode mode,
213 VideoCodecProfile profile, 213 VideoCodecProfile profile,
214 const base::Closure& report_error_to_uma_cb) { 214 const base::Closure& report_error_to_uma_cb) {
215 VAProfile va_profile = ProfileToVAProfile(profile, mode); 215 VAProfile va_profile = ProfileToVAProfile(profile, mode);
216 scoped_refptr<VaapiWrapper> vaapi_wrapper = 216 scoped_refptr<VaapiWrapper> vaapi_wrapper =
217 Create(mode, va_profile, report_error_to_uma_cb); 217 Create(mode, va_profile, report_error_to_uma_cb);
218 return vaapi_wrapper; 218 return vaapi_wrapper;
219 } 219 }
220 220
221 // static 221 // static
222 VideoEncodeAccelerator::SupportedProfiles 222 VideoEncodeAccelerator::SupportedProfiles
223 VaapiWrapper::GetSupportedEncodeProfiles() { 223 VaapiWrapper::GetSupportedEncodeProfiles() {
224 VideoEncodeAccelerator::SupportedProfiles profiles; 224 VideoEncodeAccelerator::SupportedProfiles profiles;
225 std::vector<ProfileInfo> encode_profile_infos = 225 std::vector<ProfileInfo> encode_profile_infos =
226 profile_infos_.Get().GetSupportedProfileInfosForCodecMode(kEncode); 226 profile_infos_->GetSupportedProfileInfosForCodecMode(kEncode);
227 227
228 for (size_t i = 0; i < arraysize(kProfileMap); ++i) { 228 for (size_t i = 0; i < arraysize(kProfileMap); ++i) {
229 VAProfile va_profile = ProfileToVAProfile(kProfileMap[i].profile, kEncode); 229 VAProfile va_profile = ProfileToVAProfile(kProfileMap[i].profile, kEncode);
230 if (va_profile == VAProfileNone) 230 if (va_profile == VAProfileNone)
231 continue; 231 continue;
232 for (const auto& profile_info : encode_profile_infos) { 232 for (const auto& profile_info : encode_profile_infos) {
233 if (profile_info.va_profile == va_profile) { 233 if (profile_info.va_profile == va_profile) {
234 VideoEncodeAccelerator::SupportedProfile profile; 234 VideoEncodeAccelerator::SupportedProfile profile;
235 profile.profile = kProfileMap[i].profile; 235 profile.profile = kProfileMap[i].profile;
236 profile.max_resolution = profile_info.max_resolution; 236 profile.max_resolution = profile_info.max_resolution;
237 profile.max_framerate_numerator = kMaxEncoderFramerate; 237 profile.max_framerate_numerator = kMaxEncoderFramerate;
238 profile.max_framerate_denominator = 1; 238 profile.max_framerate_denominator = 1;
239 profiles.push_back(profile); 239 profiles.push_back(profile);
240 break; 240 break;
241 } 241 }
242 } 242 }
243 } 243 }
244 return profiles; 244 return profiles;
245 } 245 }
246 246
247 // static 247 // static
248 VideoDecodeAccelerator::SupportedProfiles 248 VideoDecodeAccelerator::SupportedProfiles
249 VaapiWrapper::GetSupportedDecodeProfiles() { 249 VaapiWrapper::GetSupportedDecodeProfiles() {
250 VideoDecodeAccelerator::SupportedProfiles profiles; 250 VideoDecodeAccelerator::SupportedProfiles profiles;
251 std::vector<ProfileInfo> decode_profile_infos = 251 std::vector<ProfileInfo> decode_profile_infos =
252 profile_infos_.Get().GetSupportedProfileInfosForCodecMode(kDecode); 252 profile_infos_->GetSupportedProfileInfosForCodecMode(kDecode);
253 253
254 for (size_t i = 0; i < arraysize(kProfileMap); ++i) { 254 for (size_t i = 0; i < arraysize(kProfileMap); ++i) {
255 VAProfile va_profile = ProfileToVAProfile(kProfileMap[i].profile, kDecode); 255 VAProfile va_profile = ProfileToVAProfile(kProfileMap[i].profile, kDecode);
256 if (va_profile == VAProfileNone) 256 if (va_profile == VAProfileNone)
257 continue; 257 continue;
258 for (const auto& profile_info : decode_profile_infos) { 258 for (const auto& profile_info : decode_profile_infos) {
259 if (profile_info.va_profile == va_profile) { 259 if (profile_info.va_profile == va_profile) {
260 VideoDecodeAccelerator::SupportedProfile profile; 260 VideoDecodeAccelerator::SupportedProfile profile;
261 profile.profile = kProfileMap[i].profile; 261 profile.profile = kProfileMap[i].profile;
262 profile.max_resolution = profile_info.max_resolution; 262 profile.max_resolution = profile_info.max_resolution;
263 profile.min_resolution.SetSize(16, 16); 263 profile.min_resolution.SetSize(16, 16);
264 profiles.push_back(profile); 264 profiles.push_back(profile);
265 break; 265 break;
266 } 266 }
267 } 267 }
268 } 268 }
269 return profiles; 269 return profiles;
270 } 270 }
271 271
272 // static 272 // static
273 bool VaapiWrapper::IsJpegDecodeSupported() { 273 bool VaapiWrapper::IsJpegDecodeSupported() {
274 return profile_infos_.Get().IsProfileSupported(kDecode, 274 return profile_infos_->IsProfileSupported(kDecode, VAProfileJPEGBaseline);
275 VAProfileJPEGBaseline);
276 } 275 }
277 276
278 void VaapiWrapper::TryToSetVADisplayAttributeToLocalGPU() { 277 void VaapiWrapper::TryToSetVADisplayAttributeToLocalGPU() {
279 base::AutoLock auto_lock(*va_lock_); 278 base::AutoLock auto_lock(*va_lock_);
280 VADisplayAttribute item = {VADisplayAttribRenderMode, 279 VADisplayAttribute item = {VADisplayAttribRenderMode,
281 1, // At least support '_LOCAL_OVERLAY'. 280 1, // At least support '_LOCAL_OVERLAY'.
282 -1, // The maximum possible support 'ALL'. 281 -1, // The maximum possible support 'ALL'.
283 VA_RENDER_MODE_LOCAL_GPU, 282 VA_RENDER_MODE_LOCAL_GPU,
284 VA_DISPLAY_ATTRIB_SETTABLE}; 283 VA_DISPLAY_ATTRIB_SETTABLE};
285 284
286 VAStatus va_res = vaSetDisplayAttributes(va_display_, &item, 1); 285 VAStatus va_res = vaSetDisplayAttributes(va_display_, &item, 1);
287 if (va_res != VA_STATUS_SUCCESS) 286 if (va_res != VA_STATUS_SUCCESS)
288 DVLOG(2) << "vaSetDisplayAttributes unsupported, ignoring by default."; 287 DVLOG(2) << "vaSetDisplayAttributes unsupported, ignoring by default.";
289 } 288 }
290 289
291 // static 290 // static
292 VAProfile VaapiWrapper::ProfileToVAProfile(VideoCodecProfile profile, 291 VAProfile VaapiWrapper::ProfileToVAProfile(VideoCodecProfile profile,
293 CodecMode mode) { 292 CodecMode mode) {
294 VAProfile va_profile = VAProfileNone; 293 VAProfile va_profile = VAProfileNone;
295 for (size_t i = 0; i < arraysize(kProfileMap); ++i) { 294 for (size_t i = 0; i < arraysize(kProfileMap); ++i) {
296 if (kProfileMap[i].profile == profile) { 295 if (kProfileMap[i].profile == profile) {
297 va_profile = kProfileMap[i].va_profile; 296 va_profile = kProfileMap[i].va_profile;
298 break; 297 break;
299 } 298 }
300 } 299 }
301 if (!profile_infos_.Get().IsProfileSupported(mode, va_profile) && 300 if (!profile_infos_->IsProfileSupported(mode, va_profile) &&
302 va_profile == VAProfileH264Baseline) { 301 va_profile == VAProfileH264Baseline) {
303 // crbug.com/345569: ProfileIDToVideoCodecProfile() currently strips 302 // crbug.com/345569: ProfileIDToVideoCodecProfile() currently strips
304 // the information whether the profile is constrained or not, so we have no 303 // the information whether the profile is constrained or not, so we have no
305 // way to know here. Try for baseline first, but if it is not supported, 304 // way to know here. Try for baseline first, but if it is not supported,
306 // try constrained baseline and hope this is what it actually is 305 // try constrained baseline and hope this is what it actually is
307 // (which in practice is true for a great majority of cases). 306 // (which in practice is true for a great majority of cases).
308 if (profile_infos_.Get().IsProfileSupported( 307 if (profile_infos_->IsProfileSupported(mode,
309 mode, VAProfileH264ConstrainedBaseline)) { 308 VAProfileH264ConstrainedBaseline)) {
310 va_profile = VAProfileH264ConstrainedBaseline; 309 va_profile = VAProfileH264ConstrainedBaseline;
311 DVLOG(1) << "Fall back to constrained baseline profile."; 310 DVLOG(1) << "Fall back to constrained baseline profile.";
312 } 311 }
313 } 312 }
314 return va_profile; 313 return va_profile;
315 } 314 }
316 315
317 std::vector<VaapiWrapper::ProfileInfo> 316 std::vector<VaapiWrapper::ProfileInfo>
318 VaapiWrapper::GetSupportedProfileInfosForCodecModeInternal(CodecMode mode) { 317 VaapiWrapper::GetSupportedProfileInfosForCodecModeInternal(CodecMode mode) {
319 std::vector<ProfileInfo> supported_profile_infos; 318 std::vector<ProfileInfo> supported_profile_infos;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 if (running_on_chromeos) 356 if (running_on_chromeos)
358 LOG(ERROR) << kErrorMsg; 357 LOG(ERROR) << kErrorMsg;
359 else 358 else
360 DVLOG(1) << kErrorMsg; 359 DVLOG(1) << kErrorMsg;
361 return false; 360 return false;
362 } 361 }
363 362
364 report_error_to_uma_cb_ = report_error_to_uma_cb; 363 report_error_to_uma_cb_ = report_error_to_uma_cb;
365 364
366 base::AutoLock auto_lock(*va_lock_); 365 base::AutoLock auto_lock(*va_lock_);
367 366 if (!va_display_state_->Initialize())
368 VADisplayState* va_display_state = &va_display_state_.Get();
369 if (!va_display_state) {
370 LOG(ERROR) << "Failed to allocate VA display state";
371 return false;
372 }
373
374 if (!va_display_state->Initialize())
375 return false; 367 return false;
376 368
377 va_display_ = va_display_state->va_display(); 369 va_display_ = va_display_state_->va_display();
378 return true; 370 return true;
379 } 371 }
380 372
381 bool VaapiWrapper::GetSupportedVaProfiles(std::vector<VAProfile>* profiles) { 373 bool VaapiWrapper::GetSupportedVaProfiles(std::vector<VAProfile>* profiles) {
382 base::AutoLock auto_lock(*va_lock_); 374 base::AutoLock auto_lock(*va_lock_);
383 // Query the driver for supported profiles. 375 // Query the driver for supported profiles.
384 int max_profiles = vaMaxNumProfiles(va_display_); 376 int max_profiles = vaMaxNumProfiles(va_display_);
385 std::vector<VAProfile> supported_profiles( 377 std::vector<VAProfile> supported_profiles(
386 base::checked_cast<size_t>(max_profiles)); 378 base::checked_cast<size_t>(max_profiles));
387 379
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 } 505 }
514 506
515 void VaapiWrapper::Deinitialize() { 507 void VaapiWrapper::Deinitialize() {
516 base::AutoLock auto_lock(*va_lock_); 508 base::AutoLock auto_lock(*va_lock_);
517 509
518 if (va_config_id_ != VA_INVALID_ID) { 510 if (va_config_id_ != VA_INVALID_ID) {
519 VAStatus va_res = vaDestroyConfig(va_display_, va_config_id_); 511 VAStatus va_res = vaDestroyConfig(va_display_, va_config_id_);
520 VA_LOG_ON_ERROR(va_res, "vaDestroyConfig failed"); 512 VA_LOG_ON_ERROR(va_res, "vaDestroyConfig failed");
521 } 513 }
522 514
523 VADisplayState* va_display_state = &va_display_state_.Get(); 515 VAStatus va_res = VA_STATUS_SUCCESS;
524 if (va_display_state) { 516 va_display_state->Deinitialize(&va_res);
525 VAStatus va_res = VA_STATUS_SUCCESS; 517 VA_LOG_ON_ERROR(va_res, "vaTerminate failed");
526 va_display_state->Deinitialize(&va_res);
527 VA_LOG_ON_ERROR(va_res, "vaTerminate failed");
528 }
529 518
530 va_config_id_ = VA_INVALID_ID; 519 va_config_id_ = VA_INVALID_ID;
531 va_display_ = NULL; 520 va_display_ = NULL;
532 } 521 }
533 522
534 bool VaapiWrapper::CreateSurfaces(unsigned int va_format, 523 bool VaapiWrapper::CreateSurfaces(unsigned int va_format,
535 const gfx::Size& size, 524 const gfx::Size& size,
536 size_t num_surfaces, 525 size_t num_surfaces,
537 std::vector<VASurfaceID>* va_surfaces) { 526 std::vector<VASurfaceID>* va_surfaces) {
538 base::AutoLock auto_lock(*va_lock_); 527 base::AutoLock auto_lock(*va_lock_);
(...skipping 600 matching lines...) Expand 10 before | Expand all | Expand 10 after
1139 } 1128 }
1140 1129
1141 // static 1130 // static
1142 void VaapiWrapper::PreSandboxInitialization() { 1131 void VaapiWrapper::PreSandboxInitialization() {
1143 #if defined(USE_OZONE) 1132 #if defined(USE_OZONE)
1144 const char kDriRenderNode0Path[] = "/dev/dri/renderD128"; 1133 const char kDriRenderNode0Path[] = "/dev/dri/renderD128";
1145 base::File drm_file = base::File( 1134 base::File drm_file = base::File(
1146 base::FilePath::FromUTF8Unsafe(kDriRenderNode0Path), 1135 base::FilePath::FromUTF8Unsafe(kDriRenderNode0Path),
1147 base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE); 1136 base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE);
1148 if (drm_file.IsValid()) 1137 if (drm_file.IsValid())
1149 va_display_state_.Get().SetDrmFd(drm_file.GetPlatformFile()); 1138 va_display_state_->SetDrmFd(drm_file.GetPlatformFile());
1150 #endif 1139 #endif
1151 } 1140 }
1152 1141
1153 // static 1142 // static
1154 bool VaapiWrapper::PostSandboxInitialization() { 1143 bool VaapiWrapper::PostSandboxInitialization() {
1155 StubPathMap paths; 1144 StubPathMap paths;
1156 1145
1157 paths[kModuleVa].push_back("libva.so.1"); 1146 paths[kModuleVa].push_back("libva.so.1");
1158 1147
1159 #if defined(USE_X11) 1148 #if defined(USE_X11)
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
1258 drm_fd_.reset(HANDLE_EINTR(dup(fd))); 1247 drm_fd_.reset(HANDLE_EINTR(dup(fd)));
1259 } 1248 }
1260 #endif // USE_OZONE 1249 #endif // USE_OZONE
1261 1250
1262 bool VaapiWrapper::VADisplayState::VAAPIVersionLessThan(int major, int minor) { 1251 bool VaapiWrapper::VADisplayState::VAAPIVersionLessThan(int major, int minor) {
1263 return (major_version_ < major) || 1252 return (major_version_ < major) ||
1264 (major_version_ == major && minor_version_ < minor); 1253 (major_version_ == major && minor_version_ < minor);
1265 } 1254 }
1266 1255
1267 } // namespace media 1256 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698