blob: 2251a78e88c0dae144bb4e152ab2549c87eb8ae9 [file] [log] [blame] [edit]
/*
* 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 "DrmPlane.h"
#include <algorithm>
#include <cerrno>
#include <cinttypes>
#include <cstdint>
#include "DrmDevice.h"
#include "bufferinfo/BufferInfoGetter.h"
#include "compositor/LayerData.h"
#include "utils/log.h"
namespace android {
auto DrmPlane::CreateInstance(DrmDevice &dev, uint32_t plane_id)
-> std::unique_ptr<DrmPlane> {
auto p = MakeDrmModePlaneUnique(*dev.GetFd(), plane_id);
if (!p) {
ALOGE("Failed to get plane %d", plane_id);
return {};
}
auto plane = std::unique_ptr<DrmPlane>(new DrmPlane(dev, std::move(p)));
if (plane->Init() != 0) {
ALOGE("Failed to init plane %d", plane_id);
return {};
}
return plane;
}
int DrmPlane::Init() {
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
formats_ = {plane_->formats, plane_->formats + plane_->count_formats};
DrmProperty p;
if (!GetPlaneProperty("type", p)) {
return -ENOTSUP;
}
auto type = p.GetValue();
if (!type) {
ALOGE("Failed to get plane type property value");
return -EINVAL;
}
switch (*type) {
case DRM_PLANE_TYPE_OVERLAY:
case DRM_PLANE_TYPE_PRIMARY:
case DRM_PLANE_TYPE_CURSOR:
type_ = (uint32_t)*type;
break;
default:
ALOGE("Invalid plane type %" PRIu64, *type);
return -EINVAL;
}
if (!GetPlaneProperty("CRTC_ID", crtc_property_) ||
!GetPlaneProperty("FB_ID", fb_property_) ||
!GetPlaneProperty("CRTC_X", crtc_x_property_) ||
!GetPlaneProperty("CRTC_Y", crtc_y_property_) ||
!GetPlaneProperty("CRTC_W", crtc_w_property_) ||
!GetPlaneProperty("CRTC_H", crtc_h_property_) ||
!GetPlaneProperty("SRC_X", src_x_property_) ||
!GetPlaneProperty("SRC_Y", src_y_property_) ||
!GetPlaneProperty("SRC_W", src_w_property_) ||
!GetPlaneProperty("SRC_H", src_h_property_)) {
return -ENOTSUP;
}
GetPlaneProperty("zpos", zpos_property_, Presence::kOptional);
if (GetPlaneProperty("rotation", rotation_property_, Presence::kOptional)) {
rotation_property_.GetEnumMask(transform_enum_mask_);
}
GetPlaneProperty("alpha", alpha_property_, Presence::kOptional);
if (GetPlaneProperty("pixel blend mode", blend_property_,
Presence::kOptional)) {
blend_property_.AddEnumToMap("Pre-multiplied", BufferBlendMode::kPreMult,
blending_enum_map_);
blend_property_.AddEnumToMap("Coverage", BufferBlendMode::kCoverage,
blending_enum_map_);
blend_property_.AddEnumToMap("None", BufferBlendMode::kNone,
blending_enum_map_);
}
GetPlaneProperty("IN_FENCE_FD", in_fence_fd_property_, Presence::kOptional);
if (HasNonRgbFormat()) {
if (GetPlaneProperty("COLOR_ENCODING", color_encoding_property_,
Presence::kOptional)) {
color_encoding_property_.AddEnumToMap("ITU-R BT.709 YCbCr",
BufferColorSpace::kItuRec709,
color_encoding_enum_map_);
color_encoding_property_.AddEnumToMap("ITU-R BT.601 YCbCr",
BufferColorSpace::kItuRec601,
color_encoding_enum_map_);
color_encoding_property_.AddEnumToMap("ITU-R BT.2020 YCbCr",
BufferColorSpace::kItuRec2020,
color_encoding_enum_map_);
}
if (GetPlaneProperty("COLOR_RANGE", color_range_property_,
Presence::kOptional)) {
color_range_property_.AddEnumToMap("YCbCr full range",
BufferSampleRange::kFullRange,
color_range_enum_map_);
color_range_property_.AddEnumToMap("YCbCr limited range",
BufferSampleRange::kLimitedRange,
color_range_enum_map_);
}
}
if (type_ == DRM_PLANE_TYPE_CURSOR &&
GetPlaneProperty("SIZE_HINTS", size_hints_property_,
Presence::kOptional)) {
size_hints_property_.GetBlobData(size_hints_);
}
return 0;
}
bool DrmPlane::IsCrtcSupported(const DrmCrtc &crtc) const {
auto crtc_prop_optval = crtc_property_.GetValue();
auto crtc_prop_val = crtc_prop_optval ? *crtc_prop_optval : 0;
if (crtc_prop_val != 0 && crtc_prop_val != crtc.GetId() &&
GetType() == DRM_PLANE_TYPE_PRIMARY) {
// Some DRM driver such as omap_drm allows sharing primary plane between
// CRTCs, but the primary plane could not be shared if it has been used by
// any CRTC already, which is protected by the plane_switching_crtc function
// in the kernel drivers/gpu/drm/drm_atomic.c file.
// The current drm_hwc design is not ready to support such scenario yet,
// so adding the CRTC status check here to workaround for now.
return false;
}
return ((1 << crtc.GetIndexInResArray()) & plane_->possible_crtcs) != 0;
}
static uint64_t ToDrmRotation(LayerTransform transform) {
/* DRM/KMS uses counter-clockwise rotations, while HWC API uses
* clockwise. That's why 90 and 270 are swapped here.
*/
uint64_t rotation = DRM_MODE_ROTATE_0;
if (transform.rotate90) {
rotation |= DRM_MODE_ROTATE_270;
}
if (transform.hflip) {
rotation |= DRM_MODE_REFLECT_X;
}
if (transform.vflip) {
rotation |= DRM_MODE_REFLECT_Y;
}
// TODO(nobody): Respect transform_enum_mask_ to find alternative rotation
// values
return rotation;
}
bool DrmPlane::IsValidForLayer(LayerData *layer) {
if (layer == nullptr || !layer->bi) {
ALOGE("%s: Invalid parameters", __func__);
return false;
}
uint64_t drm_rotation = ToDrmRotation(layer->pi.transform);
if ((drm_rotation & transform_enum_mask_) != drm_rotation) {
ALOGV("Transform is not supported on plane %d", GetId());
return false;
}
if (!alpha_property_ && layer->pi.alpha != kAlphaOpaque) {
ALOGV("Alpha is not supported on plane %d", GetId());
return false;
}
if (blending_enum_map_.count(layer->bi->blend_mode) == 0 &&
layer->bi->blend_mode != BufferBlendMode::kNone &&
layer->bi->blend_mode != BufferBlendMode::kPreMult) {
ALOGV("Blending is not supported on plane %d", GetId());
return false;
}
auto format = layer->bi->format;
if (!IsFormatSupported(format)) {
ALOGV("Plane %d does not supports %c%c%c%c format", GetId(), format,
format >> 8, format >> 16, format >> 24);
return false;
}
if (type_ == DRM_PLANE_TYPE_CURSOR &&
!IsBufferValidForCursorPlane(layer->bi.value())) {
ALOGV("Buffer size %dx%d is not supported by cursor plane %d",
layer->bi->width, layer->bi->height, GetId());
return false;
}
return true;
}
bool DrmPlane::IsFormatSupported(uint32_t format) const {
return std::find(std::begin(formats_), std::end(formats_), format) !=
std::end(formats_);
}
bool DrmPlane::HasNonRgbFormat() const {
return std::find_if_not(std::begin(formats_), std::end(formats_),
[](uint32_t format) {
return BufferInfoGetter::IsDrmFormatRgb(format);
}) != std::end(formats_);
}
/* Convert float to 16.16 fixed point */
static int To1616FixPt(float in) {
constexpr int kBitShift = 16;
return int(in * (1 << kBitShift));
}
auto DrmPlane::AtomicSetState(drmModeAtomicReq &pset, LayerData &layer,
uint32_t zpos, uint32_t crtc_id,
DstRectInfo &whole_display_rect) -> int {
if (!layer.fb || !layer.bi) {
ALOGE("%s: Invalid arguments", __func__);
return -EINVAL;
}
if (zpos_property_ && !zpos_property_.IsImmutable()) {
uint64_t min_zpos = 0;
// Ignore ret and use min_zpos as 0 by default
std::tie(std::ignore, min_zpos) = zpos_property_.RangeMin();
if (!zpos_property_.AtomicSet(pset, zpos + min_zpos)) {
return -EINVAL;
}
}
if (layer.acquire_fence &&
!in_fence_fd_property_.AtomicSet(pset, *layer.acquire_fence)) {
return -EINVAL;
}
auto opt_disp = layer.pi.display_frame.i_rect;
if (!layer.pi.display_frame.i_rect) {
opt_disp = whole_display_rect.i_rect;
}
auto opt_src = layer.pi.source_crop.f_rect;
if (!layer.pi.source_crop.f_rect) {
opt_src = {0.0F, 0.0F, float(layer.bi->width), float(layer.bi->height)};
}
if (!opt_disp || !opt_src) {
ALOGE("%s: Invalid display frame or source crop", __func__);
return -EINVAL;
}
auto disp = opt_disp.value();
auto src = opt_src.value();
if (type_ == DRM_PLANE_TYPE_CURSOR) {
disp.right = disp.left + static_cast<int>(layer.bi->width);
disp.bottom = disp.top + static_cast<int>(layer.bi->height);
src = {0, 0, static_cast<float>(layer.bi->width),
static_cast<float>(layer.bi->height)};
}
if (!crtc_property_.AtomicSet(pset, crtc_id) ||
!fb_property_.AtomicSet(pset, layer.fb->GetFbId()) ||
!crtc_x_property_.AtomicSet(pset, disp.left) ||
!crtc_y_property_.AtomicSet(pset, disp.top) ||
!crtc_w_property_.AtomicSet(pset, disp.right - disp.left) ||
!crtc_h_property_.AtomicSet(pset, disp.bottom - disp.top) ||
!src_x_property_.AtomicSet(pset, To1616FixPt(src.left)) ||
!src_y_property_.AtomicSet(pset, To1616FixPt(src.top)) ||
!src_w_property_.AtomicSet(pset, To1616FixPt(src.right - src.left)) ||
!src_h_property_.AtomicSet(pset, To1616FixPt(src.bottom - src.top))) {
return -EINVAL;
}
if (rotation_property_ &&
!rotation_property_.AtomicSet(pset, ToDrmRotation(layer.pi.transform))) {
return -EINVAL;
}
if (alpha_property_ &&
!alpha_property_.AtomicSet(pset,
std::lround(layer.pi.alpha * UINT16_MAX))) {
return -EINVAL;
}
if (blending_enum_map_.count(layer.bi->blend_mode) != 0 &&
!blend_property_.AtomicSet(pset,
blending_enum_map_[layer.bi->blend_mode])) {
return -EINVAL;
}
if (color_encoding_enum_map_.count(layer.bi->color_space) != 0 &&
!color_encoding_property_
.AtomicSet(pset, color_encoding_enum_map_[layer.bi->color_space])) {
return -EINVAL;
}
if (color_range_enum_map_.count(layer.bi->sample_range) != 0 &&
!color_range_property_
.AtomicSet(pset, color_range_enum_map_[layer.bi->sample_range])) {
return -EINVAL;
}
return 0;
}
auto DrmPlane::AtomicDisablePlane(drmModeAtomicReq &pset) -> int {
if (!crtc_property_.AtomicSet(pset, 0) || !fb_property_.AtomicSet(pset, 0)) {
return -EINVAL;
}
return 0;
}
auto DrmPlane::GetPlaneProperty(const char *prop_name, DrmProperty &property,
Presence presence) -> bool {
auto err = drm_->GetProperty(GetId(), DRM_MODE_OBJECT_PLANE, prop_name,
&property);
if (err != 0) {
if (presence == Presence::kMandatory) {
ALOGE("Could not get mandatory property \"%s\" from plane %d", prop_name,
GetId());
} else {
ALOGV("Could not get optional property \"%s\" from plane %d", prop_name,
GetId());
}
return false;
}
return true;
}
bool DrmPlane::HasCursorSizeConstraints() const {
return drm_->GetCapCursorSize().has_value() || !size_hints_.empty();
}
bool DrmPlane::IsBufferValidForCursorPlane(const BufferInfo &bi) const {
if (std::find_if(size_hints_.begin(), size_hints_.end(),
[&](const auto &hint) -> bool {
return bi.width == hint.width && bi.height == hint.height;
}) != size_hints_.end()) {
return true;
}
const auto &cap_size = drm_->GetCapCursorSize();
return cap_size.has_value() && bi.width == cap_size->first &&
bi.height == cap_size->second;
}
} // namespace android