2 * Copyright (C) 2015 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #define LOG_TAG "hwc-drm-plane"
26 #include "DrmDevice.h"
27 #include "bufferinfo/BufferInfoGetter.h"
28 #include "utils/log.h"
32 DrmPlane::DrmPlane(DrmDevice *drm, drmModePlanePtr p)
35 possible_crtc_mask_(p->possible_crtcs),
36 formats_(p->formats, p->formats + p->count_formats) {
39 int DrmPlane::Init() {
40 uint64_t enum_value = UINT64_MAX;
43 int ret = drm_->GetPlaneProperty(*this, "type", &p);
45 ALOGE("Could not get plane type property");
50 std::tie(ret, type) = p.value();
52 ALOGE("Failed to get plane type property value");
56 case DRM_PLANE_TYPE_OVERLAY:
57 case DRM_PLANE_TYPE_PRIMARY:
58 case DRM_PLANE_TYPE_CURSOR:
59 type_ = (uint32_t)type;
62 ALOGE("Invalid plane type %" PRIu64, type);
66 ret = drm_->GetPlaneProperty(*this, "CRTC_ID", &crtc_property_);
68 ALOGE("Could not get CRTC_ID property");
72 ret = drm_->GetPlaneProperty(*this, "FB_ID", &fb_property_);
74 ALOGE("Could not get FB_ID property");
78 ret = drm_->GetPlaneProperty(*this, "CRTC_X", &crtc_x_property_);
80 ALOGE("Could not get CRTC_X property");
84 ret = drm_->GetPlaneProperty(*this, "CRTC_Y", &crtc_y_property_);
86 ALOGE("Could not get CRTC_Y property");
90 ret = drm_->GetPlaneProperty(*this, "CRTC_W", &crtc_w_property_);
92 ALOGE("Could not get CRTC_W property");
96 ret = drm_->GetPlaneProperty(*this, "CRTC_H", &crtc_h_property_);
98 ALOGE("Could not get CRTC_H property");
102 ret = drm_->GetPlaneProperty(*this, "SRC_X", &src_x_property_);
104 ALOGE("Could not get SRC_X property");
108 ret = drm_->GetPlaneProperty(*this, "SRC_Y", &src_y_property_);
110 ALOGE("Could not get SRC_Y property");
114 ret = drm_->GetPlaneProperty(*this, "SRC_W", &src_w_property_);
116 ALOGE("Could not get SRC_W property");
120 ret = drm_->GetPlaneProperty(*this, "SRC_H", &src_h_property_);
122 ALOGE("Could not get SRC_H property");
126 ret = drm_->GetPlaneProperty(*this, "zpos", &zpos_property_);
128 ALOGE("Could not get zpos property for plane %u", id());
130 ret = drm_->GetPlaneProperty(*this, "rotation", &rotation_property_);
132 ALOGE("Could not get rotation property");
134 ret = drm_->GetPlaneProperty(*this, "alpha", &alpha_property_);
136 ALOGI("Could not get alpha property");
138 ret = drm_->GetPlaneProperty(*this, "pixel blend mode", &blend_property_);
141 ret) = blend_property_.GetEnumValueWithName("Pre-multiplied");
143 blending_enum_map_[DrmHwcBlending::kPreMult] = enum_value;
146 ret) = blend_property_.GetEnumValueWithName("Coverage");
148 blending_enum_map_[DrmHwcBlending::kCoverage] = enum_value;
150 std::tie(enum_value, ret) = blend_property_.GetEnumValueWithName("None");
152 blending_enum_map_[DrmHwcBlending::kNone] = enum_value;
155 ALOGI("Could not get pixel blend mode property");
158 ret = drm_->GetPlaneProperty(*this, "IN_FENCE_FD", &in_fence_fd_property_);
160 ALOGI("Could not get IN_FENCE_FD property");
162 if (HasNonRgbFormat()) {
163 ret = drm_->GetPlaneProperty(*this, "COLOR_ENCODING",
164 &color_encoding_propery_);
166 std::tie(enum_value, ret) = color_encoding_propery_.GetEnumValueWithName(
167 "ITU-R BT.709 YCbCr");
169 color_encoding_enum_map_[DrmHwcColorSpace::kItuRec709] = enum_value;
171 std::tie(enum_value, ret) = color_encoding_propery_.GetEnumValueWithName(
172 "ITU-R BT.601 YCbCr");
174 color_encoding_enum_map_[DrmHwcColorSpace::kItuRec601] = enum_value;
176 std::tie(enum_value, ret) = color_encoding_propery_.GetEnumValueWithName(
177 "ITU-R BT.2020 YCbCr");
179 color_encoding_enum_map_[DrmHwcColorSpace::kItuRec2020] = enum_value;
182 ALOGI("Could not get COLOR_ENCODING property");
185 ret = drm_->GetPlaneProperty(*this, "COLOR_RANGE", &color_range_property_);
187 std::tie(enum_value, ret) = color_range_property_.GetEnumValueWithName(
190 color_range_enum_map_[DrmHwcSampleRange::kFullRange] = enum_value;
192 std::tie(enum_value, ret) = color_range_property_.GetEnumValueWithName(
193 "YCbCr limited range");
195 color_range_enum_map_[DrmHwcSampleRange::kLimitedRange] = enum_value;
198 ALOGI("Could not get COLOR_RANGE property");
205 uint32_t DrmPlane::id() const {
209 bool DrmPlane::GetCrtcSupported(const DrmCrtc &crtc) const {
210 return ((1 << crtc.pipe()) & possible_crtc_mask_) != 0;
213 bool DrmPlane::IsValidForLayer(DrmHwcLayer *layer) {
214 if (rotation_property_.id() == 0) {
215 if (layer->transform != DrmHwcTransform::kIdentity) {
216 ALOGV("Rotation is not supported on plane %d", id_);
220 // For rotation checks, we assume the hardware reports its capabilities
221 // consistently (e.g. a 270 degree rotation is a 90 degree rotation + H
222 // flip + V flip; it wouldn't make sense to support all of the latter but
225 const std::pair<enum DrmHwcTransform, std::string> transforms[] =
226 {{kFlipH, "reflect-x"},
227 {kFlipV, "reflect-y"},
228 {kRotate90, "rotate-90"},
229 {kRotate180, "rotate-180"},
230 {kRotate270, "rotate-270"}};
232 for (const auto &[transform, name] : transforms) {
233 if (layer->transform & transform) {
234 std::tie(std::ignore,
235 ret) = rotation_property_.GetEnumValueWithName(name);
237 ALOGV("Rotation '%s' is not supported on plane %d", name.c_str(),
245 if (alpha_property_.id() == 0 && layer->alpha != 0xffff) {
246 ALOGV("Alpha is not supported on plane %d", id_);
250 if (blending_enum_map_.count(layer->blending) == 0 &&
251 layer->blending != DrmHwcBlending::kNone) {
252 ALOGV("Blending is not supported on plane %d", id_);
256 uint32_t format = layer->buffer_info.format;
257 if (!IsFormatSupported(format)) {
258 ALOGV("Plane %d does not supports %c%c%c%c format", id_, format,
259 format >> 8, format >> 16, format >> 24);
266 uint32_t DrmPlane::type() const {
270 bool DrmPlane::IsFormatSupported(uint32_t format) const {
271 return std::find(std::begin(formats_), std::end(formats_), format) !=
275 bool DrmPlane::HasNonRgbFormat() const {
276 return std::find_if_not(std::begin(formats_), std::end(formats_),
277 [](uint32_t format) {
278 return BufferInfoGetter::IsDrmFormatRgb(format);
279 }) != std::end(formats_);
282 auto DrmPlane::AtomicSetState(drmModeAtomicReq &pset, DrmHwcLayer &layer,
283 uint32_t zpos, uint32_t crtc_id) -> int {
284 uint32_t fb_id = UINT32_MAX;
286 hwc_rect_t display_frame;
287 hwc_frect_t source_crop;
288 uint64_t rotation = 0;
289 uint64_t alpha = 0xFFFF;
291 if (!layer.FbIdHandle) {
292 ALOGE("Expected a valid framebuffer for pset");
296 fb_id = layer.FbIdHandle->GetFbId();
297 fence_fd = layer.acquire_fence.Get();
298 display_frame = layer.display_frame;
299 source_crop = layer.source_crop;
302 // Disable the plane if there's no framebuffer
303 if (fb_id == UINT32_MAX) {
304 if (!AtomicDisablePlane(pset)) {
310 if (zpos_property_ && !zpos_property_.is_immutable()) {
311 uint64_t min_zpos = 0;
313 // Ignore ret and use min_zpos as 0 by default
314 std::tie(std::ignore, min_zpos) = zpos_property_.range_min();
316 if (!zpos_property_.AtomicSet(pset, zpos + min_zpos)) {
322 if (layer.transform & DrmHwcTransform::kFlipH)
323 rotation |= DRM_MODE_REFLECT_X;
324 if (layer.transform & DrmHwcTransform::kFlipV)
325 rotation |= DRM_MODE_REFLECT_Y;
326 if (layer.transform & DrmHwcTransform::kRotate90)
327 rotation |= DRM_MODE_ROTATE_90;
328 else if (layer.transform & DrmHwcTransform::kRotate180)
329 rotation |= DRM_MODE_ROTATE_180;
330 else if (layer.transform & DrmHwcTransform::kRotate270)
331 rotation |= DRM_MODE_ROTATE_270;
333 rotation |= DRM_MODE_ROTATE_0;
336 if (!in_fence_fd_property_.AtomicSet(pset, fence_fd)) {
341 if (!crtc_property_.AtomicSet(pset, crtc_id) ||
342 !fb_property_.AtomicSet(pset, fb_id) ||
343 !crtc_x_property_.AtomicSet(pset, display_frame.left) ||
344 !crtc_y_property_.AtomicSet(pset, display_frame.top) ||
345 !crtc_w_property_.AtomicSet(pset,
346 display_frame.right - display_frame.left) ||
347 !crtc_h_property_.AtomicSet(pset,
348 display_frame.bottom - display_frame.top) ||
349 !src_x_property_.AtomicSet(pset, (int)(source_crop.left) << 16) ||
350 !src_y_property_.AtomicSet(pset, (int)(source_crop.top) << 16) ||
351 !src_w_property_.AtomicSet(pset,
352 (int)(source_crop.right - source_crop.left)
354 !src_h_property_.AtomicSet(pset,
355 (int)(source_crop.bottom - source_crop.top)
360 if (rotation_property_) {
361 if (!rotation_property_.AtomicSet(pset, rotation)) {
366 if (alpha_property_) {
367 if (!alpha_property_.AtomicSet(pset, alpha)) {
372 if (blending_enum_map_.count(layer.blending) != 0 &&
373 !blend_property_.AtomicSet(pset, blending_enum_map_[layer.blending])) {
377 if (color_encoding_enum_map_.count(layer.color_space) != 0 &&
378 !color_encoding_propery_
379 .AtomicSet(pset, color_encoding_enum_map_[layer.color_space])) {
383 if (color_range_enum_map_.count(layer.sample_range) != 0 &&
384 !color_range_property_
385 .AtomicSet(pset, color_range_enum_map_[layer.sample_range])) {
392 auto DrmPlane::AtomicDisablePlane(drmModeAtomicReq &pset) -> int {
393 if (!crtc_property_.AtomicSet(pset, 0) || !fb_property_.AtomicSet(pset, 0)) {
400 const DrmProperty &DrmPlane::zpos_property() const {
401 return zpos_property_;
404 } // namespace android