1
0
Fork 0
android_external_drm_hwcomp.../drm/DrmConnector.cpp

393 lines
14 KiB
C++

/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "drmhwc"
#include "DrmConnector.h"
#include <drm/drm_mode.h>
#include <xf86drmMode.h>
#include <algorithm>
#include <array>
#include <cerrno>
#include <cinttypes>
#include <cmath>
#include <cstdint>
#include <sstream>
#include "compositor/DisplayInfo.h"
#include "drm/DrmDevice.h"
#include "drm/DrmEncoder.h"
#include "drm/DrmMode.h"
#include "drm/DrmProperty.h"
#include "drm/DrmUnique.h"
#include "utils/log.h"
#include "utils/properties.h"
#ifndef DRM_MODE_CONNECTOR_SPI
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define DRM_MODE_CONNECTOR_SPI 19
#endif
#ifndef DRM_MODE_CONNECTOR_USB
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define DRM_MODE_CONNECTOR_USB 20
#endif
namespace android::drm_hwcomposer {
constexpr size_t kTypesCount = 21;
auto DrmConnector::GetConnectorProperty(const char *prop_name,
DrmProperty *property,
bool is_optional) -> bool {
auto err = drm_->GetProperty(GetId(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
property);
if (err == 0)
return true;
if (is_optional) {
ALOGV("Could not get optional %s property from connector %d", prop_name,
GetId());
} else {
ALOGE("Could not get %s property from connector %d", prop_name, GetId());
}
return false;
}
auto DrmConnector::CreateInstance(DrmDevice &dev, uint32_t connector_id,
uint32_t index)
-> std::unique_ptr<DrmConnector> {
auto conn = MakeDrmModeConnectorUnique(*dev.GetFd(), connector_id);
if (!conn) {
ALOGE("Failed to get connector %d", connector_id);
return {};
}
auto c = std::unique_ptr<DrmConnector>(
new DrmConnector(std::move(conn), &dev, index));
if (!c->Init()) {
ALOGE("Failed to initialize connector %d", connector_id);
return {};
}
return c;
}
DrmConnector::~DrmConnector() = default;
DrmConnector::DrmConnector(DrmModeConnectorUnique connector, DrmDevice *drm,
uint32_t index)
: connector_(std::move(connector)), drm_(drm), index_in_res_array_(index) {
}
auto DrmConnector::Init()-> bool {
if (!GetConnectorProperty("DPMS", &dpms_property_) ||
!GetConnectorProperty("CRTC_ID", &crtc_id_property_)) {
return false;
}
if (IsWriteback()) {
if (!GetConnectorProperty("WRITEBACK_PIXEL_FORMATS",
&writeback_pixel_formats_property_)) {
ALOGE("Could not get WRITEBACK_PIXEL_FORMATS property");
return false;
}
if (!GetConnectorProperty("WRITEBACK_FB_ID", &writeback_fb_id_property_)) {
ALOGE("Could not get WRITEBACK_FB_ID property");
return false;
}
if (!GetConnectorProperty("WRITEBACK_OUT_FENCE_PTR",
&writeback_out_fence_property_)) {
ALOGE("Could not get WRITEBACK_OUT_FENCE_PTR property");
return false;
}
}
if (GetOptionalConnectorProperty("Colorspace", &colorspace_property_)) {
colorspace_property_.AddEnumToMap("Default", Colorspace::kDefault,
colorspace_enum_map_);
colorspace_property_.AddEnumToMap("SMPTE_170M_YCC", Colorspace::kSmpte170MYcc,
colorspace_enum_map_);
colorspace_property_.AddEnumToMap("BT709_YCC", Colorspace::kBt709Ycc,
colorspace_enum_map_);
colorspace_property_.AddEnumToMap("XVYCC_601", Colorspace::kXvycc601,
colorspace_enum_map_);
colorspace_property_.AddEnumToMap("XVYCC_709", Colorspace::kXvycc709,
colorspace_enum_map_);
colorspace_property_.AddEnumToMap("SYCC_601", Colorspace::kSycc601,
colorspace_enum_map_);
colorspace_property_.AddEnumToMap("opYCC_601", Colorspace::kOpycc601,
colorspace_enum_map_);
colorspace_property_.AddEnumToMap("opRGB", Colorspace::kOprgb,
colorspace_enum_map_);
colorspace_property_.AddEnumToMap("BT2020_CYCC", Colorspace::kBt2020Cycc,
colorspace_enum_map_);
colorspace_property_.AddEnumToMap("BT2020_RGB", Colorspace::kBt2020Rgb,
colorspace_enum_map_);
colorspace_property_.AddEnumToMap("BT2020_YCC", Colorspace::kBt2020Ycc,
colorspace_enum_map_);
colorspace_property_.AddEnumToMap("DCI-P3_RGB_D65", Colorspace::kDciP3RgbD65,
colorspace_enum_map_);
colorspace_property_.AddEnumToMap("DCI-P3_RGB_Theater", Colorspace::kDciP3RgbTheater,
colorspace_enum_map_);
colorspace_property_.AddEnumToMap("RGB_WIDE_FIXED", Colorspace::kRgbWideFixed,
colorspace_enum_map_);
colorspace_property_.AddEnumToMap("RGB_WIDE_FLOAT",
Colorspace::kRgbWideFloat,
colorspace_enum_map_);
colorspace_property_.AddEnumToMap("BT601_YCC", Colorspace::kBt601Ycc,
colorspace_enum_map_);
}
GetOptionalConnectorProperty("content type", &content_type_property_);
GetOptionalConnectorProperty("Content Protection",
&content_protection_property_);
GetOptionalConnectorProperty("HDR_OUTPUT_METADATA",
&hdr_output_metadata_property_);
GetOptionalConnectorProperty("HDCP Content Type",
&hdcp_content_type_property_);
GetOptionalConnectorProperty("min bpc", &min_bpc_property_);
if (GetOptionalConnectorProperty("panel orientation", &panel_orientation_)) {
panel_orientation_
.AddEnumToMapReverse("Normal",
PanelOrientation::kModePanelOrientationNormal,
panel_orientation_enum_map_);
panel_orientation_
.AddEnumToMapReverse("Upside Down",
PanelOrientation::kModePanelOrientationBottomUp,
panel_orientation_enum_map_);
panel_orientation_
.AddEnumToMapReverse("Left Side Up",
PanelOrientation::kModePanelOrientationLeftUp,
panel_orientation_enum_map_);
panel_orientation_
.AddEnumToMapReverse("Right Side Up",
PanelOrientation::kModePanelOrientationRightUp,
panel_orientation_enum_map_);
}
return true;
}
int DrmConnector::UpdateEdidProperty() {
return GetOptionalConnectorProperty("EDID", &edid_property_) ? 0 : -EINVAL;
}
auto DrmConnector::GetEdidBlob() -> DrmModePropertyBlobUnique {
auto ret = UpdateEdidProperty();
if (ret != 0) {
return {};
}
auto blob_id = GetEdidProperty().GetValue();
if (!blob_id) {
return {};
}
return MakeDrmModePropertyBlobUnique(*drm_->GetFd(), *blob_id);
}
bool DrmConnector::SupportsEncoder(DrmEncoder &enc) const {
for (int i = 0; i < connector_->count_encoders; i++) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
if (connector_->encoders[i] == enc.GetId()) {
return true;
}
}
return false;
}
bool DrmConnector::IsInternal() const {
auto type = connector_->connector_type;
return type == DRM_MODE_CONNECTOR_Unknown ||
type == DRM_MODE_CONNECTOR_LVDS || type == DRM_MODE_CONNECTOR_eDP ||
type == DRM_MODE_CONNECTOR_DSI || type == DRM_MODE_CONNECTOR_VIRTUAL ||
type == DRM_MODE_CONNECTOR_DPI || type == DRM_MODE_CONNECTOR_SPI;
}
bool DrmConnector::IsExternal() const {
auto type = connector_->connector_type;
return type == DRM_MODE_CONNECTOR_HDMIA ||
type == DRM_MODE_CONNECTOR_DisplayPort ||
type == DRM_MODE_CONNECTOR_DVID || type == DRM_MODE_CONNECTOR_DVII ||
type == DRM_MODE_CONNECTOR_VGA || type == DRM_MODE_CONNECTOR_USB;
}
bool DrmConnector::IsWriteback() const {
#ifdef DRM_MODE_CONNECTOR_WRITEBACK
return connector_->connector_type == DRM_MODE_CONNECTOR_WRITEBACK;
#else
return false;
#endif
}
bool DrmConnector::IsValid() const {
return IsInternal() || IsExternal() || IsWriteback();
}
std::string DrmConnector::GetName() const {
constexpr std::array<const char *, kTypesCount> kNames =
{"None", "VGA", "DVI-I", "DVI-D", "DVI-A", "Composite",
"SVIDEO", "LVDS", "Component", "DIN", "DP", "HDMI-A",
"HDMI-B", "TV", "eDP", "Virtual", "DSI", "DPI",
"Writeback", "SPI", "USB"};
if (connector_->connector_type < kTypesCount) {
std::ostringstream name_buf;
name_buf << kNames[connector_->connector_type] << "-"
<< connector_->connector_type_id;
return name_buf.str();
}
ALOGE("Unknown type in connector %d, could not make his name", GetId());
return "None";
}
namespace {
// Some EDIDs describe the same effective resolution/refresh-rate as two
// distinct raw mode entries -- e.g. a detailed-timing "preferred" mode plus a
// CEA-861 mode for the same nominal timing, differing only in blanking or in
// the DRM_MODE_TYPE_PREFERRED bit. A byte-for-byte comparison (DrmMode::
// operator==) treats these as separate modes, so both survive into the mode
// list. That leaves which mode is "preferred" ambiguous between the kernel's
// initial modeset and drm_hwcomposer's own selection, which is the suspected
// cause of a mid-boot mode switch (visible as overscan until HDMI is
// replugged) and of framework APIs like Display.getSystemPreferredDisplayMode()
// intermittently returning null (TvSettings NPE on the Resolution screen).
constexpr float kDuplicateModeRateEpsilonHz = 0.05F;
bool IsSameEffectiveMode(const DrmMode &existing, const DrmMode &candidate) {
return existing.SameSize(candidate) &&
std::abs(existing.GetVRefresh() - candidate.GetVRefresh()) <
kDuplicateModeRateEpsilonHz;
}
} // namespace
int DrmConnector::UpdateModes() {
std::string force_mode = Properties::GetForceMode();
uint32_t xres = 0, yres = 0, rate = 0;
// Parse <xres>x<yres>[@<refreshrate>]
if (sscanf(force_mode.c_str(), "%dx%d@%d", &xres, &yres, &rate) != 3) {
rate = 0;
if (sscanf(force_mode.c_str(), "%dx%d", &xres, &yres) != 2) {
xres = yres = 0;
}
}
ALOGI_IF(xres && yres, "Force mode %dx%d@%dHz for display in connector %s",
xres, yres, rate, GetName().c_str());
auto conn = MakeDrmModeConnectorUnique(*drm_->GetFd(), GetId());
if (!conn) {
ALOGE("Failed to get connector %d", GetId());
return -ENODEV;
}
connector_ = std::move(conn);
modes_.clear();
for (int i = 0; i < connector_->count_modes; ++i) {
DrmMode m(&connector_->modes[i]);
ALOGV("Supported mode %dx%d@%fHz for display in connector %s",
m.GetRawMode().hdisplay, m.GetRawMode().vdisplay,
m.GetVRefresh(), GetName().c_str());
auto existing = std::find_if(modes_.begin(), modes_.end(),
[&m](const DrmMode &other) {
return IsSameEffectiveMode(other, m);
});
if (existing != modes_.end()) {
// Keep whichever duplicate carries the EDID's PREFERRED flag.
if ((m.GetRawMode().type & DRM_MODE_TYPE_PREFERRED) != 0 &&
(existing->GetRawMode().type & DRM_MODE_TYPE_PREFERRED) == 0) {
ALOGD("Mode %dx%d@%fHz for display in connector %s duplicated; "
"keeping the PREFERRED entry",
m.GetRawMode().hdisplay, m.GetRawMode().vdisplay,
m.GetVRefresh(), GetName().c_str());
*existing = m;
}
continue;
}
if (xres && yres) {
if (!rate && m.GetRawMode().hdisplay == xres
&& m.GetRawMode().vdisplay == yres) {
rate = m.GetVRefresh();
}
if (m.GetRawMode().hdisplay != xres
|| m.GetRawMode().vdisplay != yres
|| m.GetVRefresh() != rate) {
continue;
}
}
ALOGD("Add mode %dx%d@%fHz for display in connector %s",
m.GetRawMode().hdisplay, m.GetRawMode().vdisplay,
m.GetVRefresh(), GetName().c_str());
modes_.emplace_back(m);
}
return 0;
}
bool DrmConnector::IsLinkStatusGood() {
if (GetConnectorProperty("link-status", &link_status_property_, false)) {
auto link_status_property_value = link_status_property_.GetValue();
if (link_status_property_value &&
(link_status_property_value == DRM_MODE_LINK_STATUS_BAD))
return false;
}
return true;
}
void DrmConnector::UpdateContentProtection() {
if (!GetOptionalConnectorProperty("Content Protection",
&content_protection_property_)) {
ALOGW("No Content protection Property available");
}
}
std::optional<PanelOrientation> DrmConnector::GetPanelOrientation() {
if (!panel_orientation_.GetValue().has_value()) {
ALOGW("No panel orientation property available.");
return {};
}
/* The value_or(0) satisfies the compiler warning. However,
* panel_orientation_.GetValue() is guaranteed to have a value since we check
* has_value() and return early otherwise.
*/
uint64_t panel_orientation_value = panel_orientation_.GetValue().value_or(0);
if (panel_orientation_enum_map_.count(panel_orientation_value) == 1) {
return panel_orientation_enum_map_[panel_orientation_value];
}
ALOGE("Unknown panel orientation: panel_orientation = %" PRIu64,
panel_orientation_value);
return {};
}
} // namespace android::drm_hwcomposer