Commit b0f674b3 authored by David Staessens's avatar David Staessens Committed by Commit Bot

Some vaapi wrapper cleanup:

- Adapted singletons to follow recommendations:
  * Added DISALLOW_COPY_AND_ASSIGN
  * Get method returns (const)& instead of *
  * Use base::NoDestructor to construct the singletons
- Made VASupportedProfiles member functions const

Cq-Include-Trybots: luci.chromium.try:android_optional_gpu_tests_rel;luci.chromium.try:linux_optional_gpu_tests_rel;luci.chromium.try:mac_optional_gpu_tests_rel;luci.chromium.try:win_optional_gpu_tests_rel
Change-Id: Ib9b11bd42d2c7c64ea5c72e649a9504bab8014ba
Reviewed-on: https://chromium-review.googlesource.com/1143063Reviewed-by: default avatarMiguel Casas <mcasas@chromium.org>
Commit-Queue: David Staessens <dstaessens@chromium.org>
Cr-Commit-Position: refs/heads/master@{#580072}
parent b185fe6b
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/no_destructor.h"
#include "base/numerics/safe_conversions.h" #include "base/numerics/safe_conversions.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/sys_info.h" #include "base/sys_info.h"
...@@ -198,9 +199,6 @@ class VADisplayState { ...@@ -198,9 +199,6 @@ class VADisplayState {
// Initialize static data before sandbox is enabled. // Initialize static data before sandbox is enabled.
static void PreSandboxInitialization(); static void PreSandboxInitialization();
VADisplayState();
~VADisplayState() = delete;
// |va_lock_| must be held on entry. // |va_lock_| must be held on entry.
bool Initialize(); bool Initialize();
void Deinitialize(VAStatus* status); void Deinitialize(VAStatus* status);
...@@ -212,6 +210,11 @@ class VADisplayState { ...@@ -212,6 +210,11 @@ class VADisplayState {
void SetDrmFd(base::PlatformFile fd) { drm_fd_.reset(HANDLE_EINTR(dup(fd))); } void SetDrmFd(base::PlatformFile fd) { drm_fd_.reset(HANDLE_EINTR(dup(fd))); }
private: private:
friend class base::NoDestructor<VADisplayState>;
VADisplayState();
~VADisplayState() = default;
// Implementation of Initialize() called only once. // Implementation of Initialize() called only once.
bool InitializeOnce(); bool InitializeOnce();
...@@ -234,12 +237,14 @@ class VADisplayState { ...@@ -234,12 +237,14 @@ class VADisplayState {
// String acquired by vaQueryVendorString(). // String acquired by vaQueryVendorString().
std::string va_vendor_string_; std::string va_vendor_string_;
DISALLOW_COPY_AND_ASSIGN(VADisplayState);
}; };
// static // static
VADisplayState* VADisplayState::Get() { VADisplayState* VADisplayState::Get() {
static VADisplayState* display_state = new VADisplayState(); static base::NoDestructor<VADisplayState> display_state;
return display_state; return display_state.get();
} }
// static // static
...@@ -411,41 +416,44 @@ class VASupportedProfiles { ...@@ -411,41 +416,44 @@ class VASupportedProfiles {
VAProfile va_profile; VAProfile va_profile;
gfx::Size max_resolution; gfx::Size max_resolution;
}; };
static VASupportedProfiles* Get(); static const VASupportedProfiles& Get();
std::vector<ProfileInfo> GetSupportedProfileInfosForCodecMode( const std::vector<ProfileInfo>& GetSupportedProfileInfosForCodecMode(
VaapiWrapper::CodecMode mode); VaapiWrapper::CodecMode mode) const;
bool IsProfileSupported(VaapiWrapper::CodecMode mode, VAProfile va_profile); bool IsProfileSupported(VaapiWrapper::CodecMode mode,
VAProfile va_profile) const;
private: private:
friend class base::NoDestructor<VASupportedProfiles>;
VASupportedProfiles(); VASupportedProfiles();
~VASupportedProfiles() = default; ~VASupportedProfiles() = default;
bool GetSupportedVAProfiles(std::vector<VAProfile>* profiles); bool GetSupportedVAProfiles(std::vector<VAProfile>* profiles) const;
// Gets supported profile infos for |mode|. // Gets supported profile infos for |mode|.
std::vector<ProfileInfo> GetSupportedProfileInfosForCodecModeInternal( std::vector<ProfileInfo> GetSupportedProfileInfosForCodecModeInternal(
VaapiWrapper::CodecMode mode); VaapiWrapper::CodecMode mode) const;
// |va_lock_| must be held on entry in the following _Locked methods. // |va_lock_| must be held on entry in the following _Locked methods.
// Checks if |va_profile| supports |entrypoint| or not. // Checks if |va_profile| supports |entrypoint| or not.
bool IsEntrypointSupported_Locked(VAProfile va_profile, bool IsEntrypointSupported_Locked(VAProfile va_profile,
VAEntrypoint entrypoint); VAEntrypoint entrypoint) const;
// Returns true if |va_profile| for |entrypoint| with |required_attribs| is // Returns true if |va_profile| for |entrypoint| with |required_attribs| is
// supported. // supported.
bool AreAttribsSupported_Locked( bool AreAttribsSupported_Locked(
VAProfile va_profile, VAProfile va_profile,
VAEntrypoint entrypoint, VAEntrypoint entrypoint,
const std::vector<VAConfigAttrib>& required_attribs); const std::vector<VAConfigAttrib>& required_attribs) const;
// Gets maximum resolution for |va_profile| and |entrypoint| with // Gets maximum resolution for |va_profile| and |entrypoint| with
// |required_attribs|. If return value is true, |resolution| is the maximum // |required_attribs|. If return value is true, |resolution| is the maximum
// resolution. // resolution.
bool GetMaxResolution_Locked(VAProfile va_profile, bool GetMaxResolution_Locked(VAProfile va_profile,
VAEntrypoint entrypoint, VAEntrypoint entrypoint,
std::vector<VAConfigAttrib>& required_attribs, std::vector<VAConfigAttrib>& required_attribs,
gfx::Size* resolution); gfx::Size* resolution) const;
std::vector<ProfileInfo> supported_profiles_[VaapiWrapper::kCodecModeMax]; std::vector<ProfileInfo> supported_profiles_[VaapiWrapper::kCodecModeMax];
// Pointer to VADisplayState's members |va_lock_| and its |va_display_|. // Pointer to VADisplayState's members |va_lock_| and its |va_display_|.
...@@ -453,22 +461,24 @@ class VASupportedProfiles { ...@@ -453,22 +461,24 @@ class VASupportedProfiles {
VADisplay va_display_; VADisplay va_display_;
const base::Closure report_error_to_uma_cb_; const base::Closure report_error_to_uma_cb_;
DISALLOW_COPY_AND_ASSIGN(VASupportedProfiles);
}; };
// static // static
VASupportedProfiles* VASupportedProfiles::Get() { const VASupportedProfiles& VASupportedProfiles::Get() {
static VASupportedProfiles* profile_infos = new VASupportedProfiles(); static const base::NoDestructor<VASupportedProfiles> profile_infos;
return profile_infos; return *profile_infos;
} }
std::vector<VASupportedProfiles::ProfileInfo> const std::vector<VASupportedProfiles::ProfileInfo>&
VASupportedProfiles::GetSupportedProfileInfosForCodecMode( VASupportedProfiles::GetSupportedProfileInfosForCodecMode(
VaapiWrapper::CodecMode mode) { VaapiWrapper::CodecMode mode) const {
return supported_profiles_[mode]; return supported_profiles_[mode];
} }
bool VASupportedProfiles::IsProfileSupported(VaapiWrapper::CodecMode mode, bool VASupportedProfiles::IsProfileSupported(VaapiWrapper::CodecMode mode,
VAProfile va_profile) { VAProfile va_profile) const {
for (const auto& profile : supported_profiles_[mode]) { for (const auto& profile : supported_profiles_[mode]) {
if (profile.va_profile == va_profile) if (profile.va_profile == va_profile)
return true; return true;
...@@ -477,18 +487,19 @@ bool VASupportedProfiles::IsProfileSupported(VaapiWrapper::CodecMode mode, ...@@ -477,18 +487,19 @@ bool VASupportedProfiles::IsProfileSupported(VaapiWrapper::CodecMode mode,
} }
VASupportedProfiles::VASupportedProfiles() VASupportedProfiles::VASupportedProfiles()
: va_lock_(VADisplayState::Get()->va_lock()), : va_display_(nullptr), report_error_to_uma_cb_(base::DoNothing()) {
va_display_(nullptr), VADisplayState* display_state = VADisplayState::Get();
report_error_to_uma_cb_(base::DoNothing()) { va_lock_ = display_state->va_lock();
static_assert(arraysize(supported_profiles_) == VaapiWrapper::kCodecModeMax, static_assert(arraysize(supported_profiles_) == VaapiWrapper::kCodecModeMax,
"The array size of supported profile is incorrect."); "The array size of supported profile is incorrect.");
{ {
base::AutoLock auto_lock(*va_lock_); base::AutoLock auto_lock(*va_lock_);
if (!VADisplayState::Get()->Initialize()) if (!display_state->Initialize())
return; return;
} }
va_display_ = VADisplayState::Get()->va_display(); va_display_ = display_state->va_display();
DCHECK(va_display_) << "VADisplayState hasn't been properly Initialize()d"; DCHECK(va_display_) << "VADisplayState hasn't been properly Initialize()d";
for (size_t i = 0; i < VaapiWrapper::kCodecModeMax; ++i) { for (size_t i = 0; i < VaapiWrapper::kCodecModeMax; ++i) {
supported_profiles_[i] = GetSupportedProfileInfosForCodecModeInternal( supported_profiles_[i] = GetSupportedProfileInfosForCodecModeInternal(
...@@ -498,7 +509,7 @@ VASupportedProfiles::VASupportedProfiles() ...@@ -498,7 +509,7 @@ VASupportedProfiles::VASupportedProfiles()
{ {
base::AutoLock auto_lock(*va_lock_); base::AutoLock auto_lock(*va_lock_);
VAStatus va_res = VA_STATUS_SUCCESS; VAStatus va_res = VA_STATUS_SUCCESS;
VADisplayState::Get()->Deinitialize(&va_res); display_state->Deinitialize(&va_res);
VA_LOG_ON_ERROR(va_res, "vaTerminate failed"); VA_LOG_ON_ERROR(va_res, "vaTerminate failed");
va_display_ = nullptr; va_display_ = nullptr;
} }
...@@ -506,7 +517,7 @@ VASupportedProfiles::VASupportedProfiles() ...@@ -506,7 +517,7 @@ VASupportedProfiles::VASupportedProfiles()
std::vector<VASupportedProfiles::ProfileInfo> std::vector<VASupportedProfiles::ProfileInfo>
VASupportedProfiles::GetSupportedProfileInfosForCodecModeInternal( VASupportedProfiles::GetSupportedProfileInfosForCodecModeInternal(
VaapiWrapper::CodecMode mode) { VaapiWrapper::CodecMode mode) const {
std::vector<ProfileInfo> supported_profile_infos; std::vector<ProfileInfo> supported_profile_infos;
std::vector<VAProfile> va_profiles; std::vector<VAProfile> va_profiles;
if (!GetSupportedVAProfiles(&va_profiles)) if (!GetSupportedVAProfiles(&va_profiles))
...@@ -540,7 +551,7 @@ VASupportedProfiles::GetSupportedProfileInfosForCodecModeInternal( ...@@ -540,7 +551,7 @@ VASupportedProfiles::GetSupportedProfileInfosForCodecModeInternal(
} }
bool VASupportedProfiles::GetSupportedVAProfiles( bool VASupportedProfiles::GetSupportedVAProfiles(
std::vector<VAProfile>* profiles) { std::vector<VAProfile>* profiles) const {
base::AutoLock auto_lock(*va_lock_); base::AutoLock auto_lock(*va_lock_);
// Query the driver for supported profiles. // Query the driver for supported profiles.
const int max_profiles = vaMaxNumProfiles(va_display_); const int max_profiles = vaMaxNumProfiles(va_display_);
...@@ -557,13 +568,13 @@ bool VASupportedProfiles::GetSupportedVAProfiles( ...@@ -557,13 +568,13 @@ bool VASupportedProfiles::GetSupportedVAProfiles(
} }
supported_profiles.resize(base::checked_cast<size_t>(num_supported_profiles)); supported_profiles.resize(base::checked_cast<size_t>(num_supported_profiles));
*profiles = supported_profiles; *profiles = std::move(supported_profiles);
return true; return true;
} }
bool VASupportedProfiles::IsEntrypointSupported_Locked( bool VASupportedProfiles::IsEntrypointSupported_Locked(
VAProfile va_profile, VAProfile va_profile,
VAEntrypoint entrypoint) { VAEntrypoint entrypoint) const {
va_lock_->AssertAcquired(); va_lock_->AssertAcquired();
// Query the driver for supported entrypoints. // Query the driver for supported entrypoints.
int max_entrypoints = vaMaxNumEntrypoints(va_display_); int max_entrypoints = vaMaxNumEntrypoints(va_display_);
...@@ -588,7 +599,7 @@ bool VASupportedProfiles::IsEntrypointSupported_Locked( ...@@ -588,7 +599,7 @@ bool VASupportedProfiles::IsEntrypointSupported_Locked(
bool VASupportedProfiles::AreAttribsSupported_Locked( bool VASupportedProfiles::AreAttribsSupported_Locked(
VAProfile va_profile, VAProfile va_profile,
VAEntrypoint entrypoint, VAEntrypoint entrypoint,
const std::vector<VAConfigAttrib>& required_attribs) { const std::vector<VAConfigAttrib>& required_attribs) const {
va_lock_->AssertAcquired(); va_lock_->AssertAcquired();
// Query the driver for required attributes. // Query the driver for required attributes.
std::vector<VAConfigAttrib> attribs = required_attribs; std::vector<VAConfigAttrib> attribs = required_attribs;
...@@ -615,7 +626,7 @@ bool VASupportedProfiles::GetMaxResolution_Locked( ...@@ -615,7 +626,7 @@ bool VASupportedProfiles::GetMaxResolution_Locked(
VAProfile va_profile, VAProfile va_profile,
VAEntrypoint entrypoint, VAEntrypoint entrypoint,
std::vector<VAConfigAttrib>& required_attribs, std::vector<VAConfigAttrib>& required_attribs,
gfx::Size* resolution) { gfx::Size* resolution) const {
va_lock_->AssertAcquired(); va_lock_->AssertAcquired();
VAConfigID va_config_id; VAConfigID va_config_id;
VAStatus va_res = VAStatus va_res =
...@@ -666,14 +677,15 @@ VAProfile ProfileToVAProfile(VideoCodecProfile profile, ...@@ -666,14 +677,15 @@ VAProfile ProfileToVAProfile(VideoCodecProfile profile,
break; break;
} }
} }
if (!VASupportedProfiles::Get()->IsProfileSupported(mode, va_profile) && const VASupportedProfiles& supported_profiles = VASupportedProfiles::Get();
if (!supported_profiles.IsProfileSupported(mode, va_profile) &&
va_profile == VAProfileH264Baseline) { va_profile == VAProfileH264Baseline) {
// https://crbug.com/345569: ProfileIDToVideoCodecProfile() currently strips // https://crbug.com/345569: ProfileIDToVideoCodecProfile() currently strips
// the information whether the profile is constrained or not, so we have no // the information whether the profile is constrained or not, so we have no
// way to know here. Try for baseline first, but if it is not supported, // way to know here. Try for baseline first, but if it is not supported,
// try constrained baseline and hope this is what it actually is // try constrained baseline and hope this is what it actually is
// (which in practice is true for a great majority of cases). // (which in practice is true for a great majority of cases).
if (VASupportedProfiles::Get()->IsProfileSupported( if (supported_profiles.IsProfileSupported(
mode, VAProfileH264ConstrainedBaseline)) { mode, VAProfileH264ConstrainedBaseline)) {
va_profile = VAProfileH264ConstrainedBaseline; va_profile = VAProfileH264ConstrainedBaseline;
DVLOG(1) << "Fall back to constrained baseline profile."; DVLOG(1) << "Fall back to constrained baseline profile.";
...@@ -694,7 +706,7 @@ scoped_refptr<VaapiWrapper> VaapiWrapper::Create( ...@@ -694,7 +706,7 @@ scoped_refptr<VaapiWrapper> VaapiWrapper::Create(
CodecMode mode, CodecMode mode,
VAProfile va_profile, VAProfile va_profile,
const base::Closure& report_error_to_uma_cb) { const base::Closure& report_error_to_uma_cb) {
if (!VASupportedProfiles::Get()->IsProfileSupported(mode, va_profile)) { if (!VASupportedProfiles::Get().IsProfileSupported(mode, va_profile)) {
DVLOG(1) << "Unsupported va_profile: " << va_profile; DVLOG(1) << "Unsupported va_profile: " << va_profile;
return nullptr; return nullptr;
} }
...@@ -721,8 +733,8 @@ scoped_refptr<VaapiWrapper> VaapiWrapper::CreateForVideoCodec( ...@@ -721,8 +733,8 @@ scoped_refptr<VaapiWrapper> VaapiWrapper::CreateForVideoCodec(
VideoEncodeAccelerator::SupportedProfiles VideoEncodeAccelerator::SupportedProfiles
VaapiWrapper::GetSupportedEncodeProfiles() { VaapiWrapper::GetSupportedEncodeProfiles() {
VideoEncodeAccelerator::SupportedProfiles profiles; VideoEncodeAccelerator::SupportedProfiles profiles;
std::vector<VASupportedProfiles::ProfileInfo> encode_profile_infos = const std::vector<VASupportedProfiles::ProfileInfo>& encode_profile_infos =
VASupportedProfiles::Get()->GetSupportedProfileInfosForCodecMode(kEncode); VASupportedProfiles::Get().GetSupportedProfileInfosForCodecMode(kEncode);
for (size_t i = 0; i < arraysize(kProfileMap); ++i) { for (size_t i = 0; i < arraysize(kProfileMap); ++i) {
VAProfile va_profile = ProfileToVAProfile(kProfileMap[i].profile, kEncode); VAProfile va_profile = ProfileToVAProfile(kProfileMap[i].profile, kEncode);
...@@ -747,8 +759,8 @@ VaapiWrapper::GetSupportedEncodeProfiles() { ...@@ -747,8 +759,8 @@ VaapiWrapper::GetSupportedEncodeProfiles() {
VideoDecodeAccelerator::SupportedProfiles VideoDecodeAccelerator::SupportedProfiles
VaapiWrapper::GetSupportedDecodeProfiles() { VaapiWrapper::GetSupportedDecodeProfiles() {
VideoDecodeAccelerator::SupportedProfiles profiles; VideoDecodeAccelerator::SupportedProfiles profiles;
std::vector<VASupportedProfiles::ProfileInfo> decode_profile_infos = const std::vector<VASupportedProfiles::ProfileInfo>& decode_profile_infos =
VASupportedProfiles::Get()->GetSupportedProfileInfosForCodecMode(kDecode); VASupportedProfiles::Get().GetSupportedProfileInfosForCodecMode(kDecode);
for (size_t i = 0; i < arraysize(kProfileMap); ++i) { for (size_t i = 0; i < arraysize(kProfileMap); ++i) {
VAProfile va_profile = ProfileToVAProfile(kProfileMap[i].profile, kDecode); VAProfile va_profile = ProfileToVAProfile(kProfileMap[i].profile, kDecode);
...@@ -770,13 +782,13 @@ VaapiWrapper::GetSupportedDecodeProfiles() { ...@@ -770,13 +782,13 @@ VaapiWrapper::GetSupportedDecodeProfiles() {
// static // static
bool VaapiWrapper::IsJpegDecodeSupported() { bool VaapiWrapper::IsJpegDecodeSupported() {
return VASupportedProfiles::Get()->IsProfileSupported(kDecode, return VASupportedProfiles::Get().IsProfileSupported(kDecode,
VAProfileJPEGBaseline); VAProfileJPEGBaseline);
} }
// static // static
bool VaapiWrapper::IsJpegEncodeSupported() { bool VaapiWrapper::IsJpegEncodeSupported() {
return VASupportedProfiles::Get()->IsProfileSupported(kEncode, return VASupportedProfiles::Get().IsProfileSupported(kEncode,
VAProfileJPEGBaseline); VAProfileJPEGBaseline);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment