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