blob: 81142344c84a310015c4528739ab7400996a8ec0 [file] [log] [blame]
Sean Paul6a55e9f2015-04-30 15:31:06 -04001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#define LOG_TAG "hwc-drm-plane"
18
Roman Stratiienko13cc3662020-08-29 21:35:39 +030019#include "DrmPlane.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040020
Roman Stratiienko2640cd82021-02-26 17:49:40 +020021#include <algorithm>
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020022#include <cerrno>
Sean Paulf72cccd2018-08-27 13:59:08 -040023#include <cinttypes>
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020024#include <cstdint>
Sean Paul6a55e9f2015-04-30 15:31:06 -040025
Roman Stratiienko13cc3662020-08-29 21:35:39 +030026#include "DrmDevice.h"
Roman Stratiienkod518a052021-02-25 19:15:14 +020027#include "bufferinfo/BufferInfoGetter.h"
28#include "utils/log.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040029
30namespace android {
31
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010032DrmPlane::DrmPlane(DrmDevice *drm, drmModePlanePtr p)
Roman Kovalivskyi859b6072020-03-26 05:03:39 +020033 : drm_(drm),
34 id_(p->plane_id),
35 possible_crtc_mask_(p->possible_crtcs),
36 formats_(p->formats, p->formats + p->count_formats) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040037}
38
Sean Paul6a55e9f2015-04-30 15:31:06 -040039int DrmPlane::Init() {
40 DrmProperty p;
41
Roman Stratiienko0b063882021-09-30 10:15:05 +030042 if (!GetPlaneProperty("type", p)) {
43 return -ENOTSUP;
Sean Paul6a55e9f2015-04-30 15:31:06 -040044 }
45
Roman Stratiienko0b063882021-09-30 10:15:05 +030046 int ret = 0;
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020047 uint64_t type = 0;
Sean Paulfc0b1da2019-03-06 09:48:42 -050048 std::tie(ret, type) = p.value();
Sean Paul6a55e9f2015-04-30 15:31:06 -040049 if (ret) {
50 ALOGE("Failed to get plane type property value");
51 return ret;
52 }
53 switch (type) {
54 case DRM_PLANE_TYPE_OVERLAY:
55 case DRM_PLANE_TYPE_PRIMARY:
56 case DRM_PLANE_TYPE_CURSOR:
57 type_ = (uint32_t)type;
58 break;
59 default:
Sean Paulf741c672016-05-11 13:49:38 -040060 ALOGE("Invalid plane type %" PRIu64, type);
Sean Paul6a55e9f2015-04-30 15:31:06 -040061 return -EINVAL;
62 }
63
Roman Stratiienko0b063882021-09-30 10:15:05 +030064 if (!GetPlaneProperty("CRTC_ID", crtc_property_) ||
65 !GetPlaneProperty("FB_ID", fb_property_) ||
66 !GetPlaneProperty("CRTC_X", crtc_x_property_) ||
67 !GetPlaneProperty("CRTC_Y", crtc_y_property_) ||
68 !GetPlaneProperty("CRTC_W", crtc_w_property_) ||
69 !GetPlaneProperty("CRTC_H", crtc_h_property_) ||
70 !GetPlaneProperty("SRC_X", src_x_property_) ||
71 !GetPlaneProperty("SRC_Y", src_y_property_) ||
72 !GetPlaneProperty("SRC_W", src_w_property_) ||
73 !GetPlaneProperty("SRC_H", src_h_property_)) {
74 return -ENOTSUP;
Sean Paul6a55e9f2015-04-30 15:31:06 -040075 }
76
Roman Stratiienko0b063882021-09-30 10:15:05 +030077 GetPlaneProperty("zpos", zpos_property_, Presence::kOptional);
Sean Paul6a55e9f2015-04-30 15:31:06 -040078
Roman Stratiienko0b063882021-09-30 10:15:05 +030079 if (GetPlaneProperty("rotation", rotation_property_, Presence::kOptional)) {
Roman Stratiienko24a4c182021-09-29 12:59:54 +030080 rotation_property_.AddEnumToMap("rotate-0", DrmHwcTransform::kIdentity,
81 transform_enum_map_);
82 rotation_property_.AddEnumToMap("rotate-90", DrmHwcTransform::kRotate90,
83 transform_enum_map_);
84 rotation_property_.AddEnumToMap("rotate-180", DrmHwcTransform::kRotate180,
85 transform_enum_map_);
86 rotation_property_.AddEnumToMap("rotate-270", DrmHwcTransform::kRotate270,
87 transform_enum_map_);
88 rotation_property_.AddEnumToMap("reflect-x", DrmHwcTransform::kFlipH,
89 transform_enum_map_);
90 rotation_property_.AddEnumToMap("reflect-y", DrmHwcTransform::kFlipV,
91 transform_enum_map_);
Roman Stratiienko24a4c182021-09-29 12:59:54 +030092 }
Sean Paul1c4c3262015-07-14 15:51:52 -040093
Roman Stratiienko0b063882021-09-30 10:15:05 +030094 GetPlaneProperty("alpha", alpha_property_, Presence::kOptional);
Sean Pauld8aefb62015-10-15 15:17:31 -040095
Roman Stratiienko0b063882021-09-30 10:15:05 +030096 if (GetPlaneProperty("pixel blend mode", blend_property_,
97 Presence::kOptional)) {
Roman Stratiienko0f679aa2021-09-29 12:59:48 +030098 blend_property_.AddEnumToMap("Pre-multiplied", DrmHwcBlending::kPreMult,
99 blending_enum_map_);
100 blend_property_.AddEnumToMap("Coverage", DrmHwcBlending::kCoverage,
101 blending_enum_map_);
102 blend_property_.AddEnumToMap("None", DrmHwcBlending::kNone,
103 blending_enum_map_);
Roman Stratiienko5063d532021-09-29 12:47:31 +0300104 }
Lowry Li9b6cafd2018-08-28 17:58:21 +0800105
Roman Stratiienko0b063882021-09-30 10:15:05 +0300106 GetPlaneProperty("IN_FENCE_FD", in_fence_fd_property_, Presence::kOptional);
Robert Fossa09220c2016-09-30 10:27:23 -0400107
Matvii Zorin8338c342020-09-08 16:12:51 +0300108 if (HasNonRgbFormat()) {
Roman Stratiienko0b063882021-09-30 10:15:05 +0300109 if (GetPlaneProperty("COLOR_ENCODING", color_encoding_propery_,
110 Presence::kOptional)) {
Roman Stratiienko0f679aa2021-09-29 12:59:48 +0300111 color_encoding_propery_.AddEnumToMap("ITU-R BT.709 YCbCr",
112 DrmHwcColorSpace::kItuRec709,
113 color_encoding_enum_map_);
114 color_encoding_propery_.AddEnumToMap("ITU-R BT.601 YCbCr",
115 DrmHwcColorSpace::kItuRec601,
116 color_encoding_enum_map_);
117 color_encoding_propery_.AddEnumToMap("ITU-R BT.2020 YCbCr",
118 DrmHwcColorSpace::kItuRec2020,
119 color_encoding_enum_map_);
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300120 }
Matvii Zorin8338c342020-09-08 16:12:51 +0300121
Roman Stratiienko0b063882021-09-30 10:15:05 +0300122 if (GetPlaneProperty("COLOR_RANGE", color_range_property_,
123 Presence::kOptional)) {
Roman Stratiienko0f679aa2021-09-29 12:59:48 +0300124 color_range_property_.AddEnumToMap("YCbCr full range",
125 DrmHwcSampleRange::kFullRange,
126 color_range_enum_map_);
127 color_range_property_.AddEnumToMap("YCbCr limited range",
128 DrmHwcSampleRange::kLimitedRange,
129 color_range_enum_map_);
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300130 }
Matvii Zorin8338c342020-09-08 16:12:51 +0300131 }
132
Sean Paul6a55e9f2015-04-30 15:31:06 -0400133 return 0;
134}
135
Sean Paul6a55e9f2015-04-30 15:31:06 -0400136bool DrmPlane::GetCrtcSupported(const DrmCrtc &crtc) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200137 return ((1 << crtc.pipe()) & possible_crtc_mask_) != 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400138}
139
Matvii Zorin67a89d32021-01-31 14:45:05 +0200140bool DrmPlane::IsValidForLayer(DrmHwcLayer *layer) {
Roman Stratiienko24a4c182021-09-29 12:59:54 +0300141 if (!rotation_property_) {
Benjamin Lib00b28d2021-05-09 08:52:18 -0700142 if (layer->transform != DrmHwcTransform::kIdentity) {
Roman Stratiienko24a4c182021-09-29 12:59:54 +0300143 ALOGV("No rotation property on plane %d", id_);
Benjamin Lib00b28d2021-05-09 08:52:18 -0700144 return false;
145 }
146 } else {
Roman Stratiienko24a4c182021-09-29 12:59:54 +0300147 if (transform_enum_map_.count(layer->transform) == 0) {
148 ALOGV("Transform is not supported on plane %d", id_);
149 return false;
Benjamin Lib00b28d2021-05-09 08:52:18 -0700150 }
Matvii Zorin67a89d32021-01-31 14:45:05 +0200151 }
152
153 if (alpha_property_.id() == 0 && layer->alpha != 0xffff) {
154 ALOGV("Alpha is not supported on plane %d", id_);
155 return false;
156 }
157
Roman Stratiienko5063d532021-09-29 12:47:31 +0300158 if (blending_enum_map_.count(layer->blending) == 0 &&
Roman Stratiienko013d0682021-09-30 19:36:00 +0300159 layer->blending != DrmHwcBlending::kNone &&
160 layer->blending != DrmHwcBlending::kPreMult) {
Roman Stratiienko5063d532021-09-29 12:47:31 +0300161 ALOGV("Blending is not supported on plane %d", id_);
162 return false;
Matvii Zorin67a89d32021-01-31 14:45:05 +0200163 }
164
Roman Stratiienko51287152021-02-10 14:59:52 +0200165 uint32_t format = layer->buffer_info.format;
Matvii Zorin67a89d32021-01-31 14:45:05 +0200166 if (!IsFormatSupported(format)) {
167 ALOGV("Plane %d does not supports %c%c%c%c format", id_, format,
168 format >> 8, format >> 16, format >> 24);
169 return false;
170 }
171
172 return true;
173}
174
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200175uint32_t DrmPlane::GetType() const {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400176 return type_;
177}
178
Roman Kovalivskyi859b6072020-03-26 05:03:39 +0200179bool DrmPlane::IsFormatSupported(uint32_t format) const {
180 return std::find(std::begin(formats_), std::end(formats_), format) !=
181 std::end(formats_);
182}
183
Matvii Zorin8338c342020-09-08 16:12:51 +0300184bool DrmPlane::HasNonRgbFormat() const {
185 return std::find_if_not(std::begin(formats_), std::end(formats_),
186 [](uint32_t format) {
187 return BufferInfoGetter::IsDrmFormatRgb(format);
188 }) != std::end(formats_);
189}
190
Roman Stratiienko6662f712021-09-29 12:52:22 +0300191static uint64_t ToDrmRotation(DrmHwcTransform transform) {
192 uint64_t rotation = 0;
193 if (transform & DrmHwcTransform::kFlipH)
194 rotation |= DRM_MODE_REFLECT_X;
195 if (transform & DrmHwcTransform::kFlipV)
196 rotation |= DRM_MODE_REFLECT_Y;
197 if (transform & DrmHwcTransform::kRotate90)
198 rotation |= DRM_MODE_ROTATE_90;
199 else if (transform & DrmHwcTransform::kRotate180)
200 rotation |= DRM_MODE_ROTATE_180;
201 else if (transform & DrmHwcTransform::kRotate270)
202 rotation |= DRM_MODE_ROTATE_270;
203 else
204 rotation |= DRM_MODE_ROTATE_0;
205
206 return rotation;
207}
208
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300209auto DrmPlane::AtomicSetState(drmModeAtomicReq &pset, DrmHwcLayer &layer,
210 uint32_t zpos, uint32_t crtc_id) -> int {
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200211 if (!layer.fb_id_handle) {
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300212 ALOGE("Expected a valid framebuffer for pset");
213 return -EINVAL;
214 }
215
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300216 if (zpos_property_ && !zpos_property_.is_immutable()) {
217 uint64_t min_zpos = 0;
218
219 // Ignore ret and use min_zpos as 0 by default
220 std::tie(std::ignore, min_zpos) = zpos_property_.range_min();
221
222 if (!zpos_property_.AtomicSet(pset, zpos + min_zpos)) {
223 return -EINVAL;
224 }
225 }
226
Roman Stratiienko6662f712021-09-29 12:52:22 +0300227 if (layer.acquire_fence &&
228 !in_fence_fd_property_.AtomicSet(pset, layer.acquire_fence.Get())) {
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300229 return -EINVAL;
230 }
231
Roman Stratiienko6662f712021-09-29 12:52:22 +0300232 if (!crtc_property_.AtomicSet(pset, crtc_id) ||
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200233 !fb_property_.AtomicSet(pset, layer.fb_id_handle->GetFbId()) ||
Roman Stratiienko6662f712021-09-29 12:52:22 +0300234 !crtc_x_property_.AtomicSet(pset, layer.display_frame.left) ||
235 !crtc_y_property_.AtomicSet(pset, layer.display_frame.top) ||
236 !crtc_w_property_.AtomicSet(pset, layer.display_frame.right -
237 layer.display_frame.left) ||
238 !crtc_h_property_.AtomicSet(pset, layer.display_frame.bottom -
239 layer.display_frame.top) ||
240 !src_x_property_.AtomicSet(pset, (int)(layer.source_crop.left) << 16) ||
241 !src_y_property_.AtomicSet(pset, (int)(layer.source_crop.top) << 16) ||
242 !src_w_property_.AtomicSet(pset, (int)(layer.source_crop.right -
243 layer.source_crop.left)
244 << 16) ||
245 !src_h_property_.AtomicSet(pset, (int)(layer.source_crop.bottom -
246 layer.source_crop.top)
247 << 16)) {
248 return -EINVAL;
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300249 }
250
Roman Stratiienko6662f712021-09-29 12:52:22 +0300251 if (rotation_property_ &&
252 !rotation_property_.AtomicSet(pset, ToDrmRotation(layer.transform))) {
253 return -EINVAL;
254 }
255
256 if (alpha_property_ && !alpha_property_.AtomicSet(pset, layer.alpha)) {
257 return -EINVAL;
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300258 }
259
Roman Stratiienko5063d532021-09-29 12:47:31 +0300260 if (blending_enum_map_.count(layer.blending) != 0 &&
261 !blend_property_.AtomicSet(pset, blending_enum_map_[layer.blending])) {
262 return -EINVAL;
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300263 }
264
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300265 if (color_encoding_enum_map_.count(layer.color_space) != 0 &&
266 !color_encoding_propery_
267 .AtomicSet(pset, color_encoding_enum_map_[layer.color_space])) {
268 return -EINVAL;
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300269 }
270
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300271 if (color_range_enum_map_.count(layer.sample_range) != 0 &&
272 !color_range_property_
273 .AtomicSet(pset, color_range_enum_map_[layer.sample_range])) {
274 return -EINVAL;
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300275 }
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300276
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300277 return 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400278}
279
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300280auto DrmPlane::AtomicDisablePlane(drmModeAtomicReq &pset) -> int {
281 if (!crtc_property_.AtomicSet(pset, 0) || !fb_property_.AtomicSet(pset, 0)) {
282 return -EINVAL;
283 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400284
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300285 return 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400286}
Sean Paul1c4c3262015-07-14 15:51:52 -0400287
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200288const DrmProperty &DrmPlane::GetZPosProperty() const {
Alexandru Gheorgheea1c5e52018-09-17 10:48:54 +0100289 return zpos_property_;
290}
291
Roman Stratiienko0b063882021-09-30 10:15:05 +0300292auto DrmPlane::GetPlaneProperty(const char *prop_name, DrmProperty &property,
293 Presence presence) -> bool {
294 int err = drm_->GetProperty(id_, DRM_MODE_OBJECT_PLANE, prop_name, &property);
295 if (err != 0) {
296 if (presence == Presence::kMandatory) {
297 ALOGE("Could not get mandatory property \"%s\" from plane %d", prop_name,
298 id_);
299 } else {
300 ALOGV("Could not get optional property \"%s\" from plane %d", prop_name,
301 id_);
302 }
303 return false;
304 }
305
306 return true;
307}
308
Sean Paulf72cccd2018-08-27 13:59:08 -0400309} // namespace android