blob: 6720dd0acfc1bdc6124a7b0526471cec6c897bd3 [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
42 int ret = drm_->GetPlaneProperty(*this, "type", &p);
43 if (ret) {
44 ALOGE("Could not get plane type property");
45 return ret;
46 }
47
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020048 uint64_t type = 0;
Sean Paulfc0b1da2019-03-06 09:48:42 -050049 std::tie(ret, type) = p.value();
Sean Paul6a55e9f2015-04-30 15:31:06 -040050 if (ret) {
51 ALOGE("Failed to get plane type property value");
52 return ret;
53 }
54 switch (type) {
55 case DRM_PLANE_TYPE_OVERLAY:
56 case DRM_PLANE_TYPE_PRIMARY:
57 case DRM_PLANE_TYPE_CURSOR:
58 type_ = (uint32_t)type;
59 break;
60 default:
Sean Paulf741c672016-05-11 13:49:38 -040061 ALOGE("Invalid plane type %" PRIu64, type);
Sean Paul6a55e9f2015-04-30 15:31:06 -040062 return -EINVAL;
63 }
64
65 ret = drm_->GetPlaneProperty(*this, "CRTC_ID", &crtc_property_);
66 if (ret) {
67 ALOGE("Could not get CRTC_ID property");
68 return ret;
69 }
70
71 ret = drm_->GetPlaneProperty(*this, "FB_ID", &fb_property_);
72 if (ret) {
73 ALOGE("Could not get FB_ID property");
74 return ret;
75 }
76
77 ret = drm_->GetPlaneProperty(*this, "CRTC_X", &crtc_x_property_);
78 if (ret) {
79 ALOGE("Could not get CRTC_X property");
80 return ret;
81 }
82
83 ret = drm_->GetPlaneProperty(*this, "CRTC_Y", &crtc_y_property_);
84 if (ret) {
85 ALOGE("Could not get CRTC_Y property");
86 return ret;
87 }
88
89 ret = drm_->GetPlaneProperty(*this, "CRTC_W", &crtc_w_property_);
90 if (ret) {
91 ALOGE("Could not get CRTC_W property");
92 return ret;
93 }
94
95 ret = drm_->GetPlaneProperty(*this, "CRTC_H", &crtc_h_property_);
96 if (ret) {
97 ALOGE("Could not get CRTC_H property");
98 return ret;
99 }
100
101 ret = drm_->GetPlaneProperty(*this, "SRC_X", &src_x_property_);
102 if (ret) {
103 ALOGE("Could not get SRC_X property");
104 return ret;
105 }
106
107 ret = drm_->GetPlaneProperty(*this, "SRC_Y", &src_y_property_);
108 if (ret) {
109 ALOGE("Could not get SRC_Y property");
110 return ret;
111 }
112
113 ret = drm_->GetPlaneProperty(*this, "SRC_W", &src_w_property_);
114 if (ret) {
115 ALOGE("Could not get SRC_W property");
116 return ret;
117 }
118
119 ret = drm_->GetPlaneProperty(*this, "SRC_H", &src_h_property_);
120 if (ret) {
121 ALOGE("Could not get SRC_H property");
122 return ret;
123 }
124
Alexandru Gheorgheea1c5e52018-09-17 10:48:54 +0100125 ret = drm_->GetPlaneProperty(*this, "zpos", &zpos_property_);
126 if (ret)
127 ALOGE("Could not get zpos property for plane %u", id());
128
Sean Paul1c4c3262015-07-14 15:51:52 -0400129 ret = drm_->GetPlaneProperty(*this, "rotation", &rotation_property_);
130 if (ret)
131 ALOGE("Could not get rotation property");
132
Sean Pauld8aefb62015-10-15 15:17:31 -0400133 ret = drm_->GetPlaneProperty(*this, "alpha", &alpha_property_);
134 if (ret)
135 ALOGI("Could not get alpha property");
136
Lowry Li9b6cafd2018-08-28 17:58:21 +0800137 ret = drm_->GetPlaneProperty(*this, "pixel blend mode", &blend_property_);
Roman Stratiienko5063d532021-09-29 12:47:31 +0300138 if (ret == 0) {
Roman Stratiienko0f679aa2021-09-29 12:59:48 +0300139 blend_property_.AddEnumToMap("Pre-multiplied", DrmHwcBlending::kPreMult,
140 blending_enum_map_);
141 blend_property_.AddEnumToMap("Coverage", DrmHwcBlending::kCoverage,
142 blending_enum_map_);
143 blend_property_.AddEnumToMap("None", DrmHwcBlending::kNone,
144 blending_enum_map_);
Roman Stratiienko5063d532021-09-29 12:47:31 +0300145 } else {
Lowry Li9b6cafd2018-08-28 17:58:21 +0800146 ALOGI("Could not get pixel blend mode property");
Roman Stratiienko5063d532021-09-29 12:47:31 +0300147 }
Lowry Li9b6cafd2018-08-28 17:58:21 +0800148
Robert Fossa09220c2016-09-30 10:27:23 -0400149 ret = drm_->GetPlaneProperty(*this, "IN_FENCE_FD", &in_fence_fd_property_);
150 if (ret)
151 ALOGI("Could not get IN_FENCE_FD property");
152
Matvii Zorin8338c342020-09-08 16:12:51 +0300153 if (HasNonRgbFormat()) {
154 ret = drm_->GetPlaneProperty(*this, "COLOR_ENCODING",
155 &color_encoding_propery_);
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300156 if (ret == 0) {
Roman Stratiienko0f679aa2021-09-29 12:59:48 +0300157 color_encoding_propery_.AddEnumToMap("ITU-R BT.709 YCbCr",
158 DrmHwcColorSpace::kItuRec709,
159 color_encoding_enum_map_);
160 color_encoding_propery_.AddEnumToMap("ITU-R BT.601 YCbCr",
161 DrmHwcColorSpace::kItuRec601,
162 color_encoding_enum_map_);
163 color_encoding_propery_.AddEnumToMap("ITU-R BT.2020 YCbCr",
164 DrmHwcColorSpace::kItuRec2020,
165 color_encoding_enum_map_);
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300166 } else {
Matvii Zorin8338c342020-09-08 16:12:51 +0300167 ALOGI("Could not get COLOR_ENCODING property");
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300168 }
Matvii Zorin8338c342020-09-08 16:12:51 +0300169
170 ret = drm_->GetPlaneProperty(*this, "COLOR_RANGE", &color_range_property_);
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300171 if (ret == 0) {
Roman Stratiienko0f679aa2021-09-29 12:59:48 +0300172 color_range_property_.AddEnumToMap("YCbCr full range",
173 DrmHwcSampleRange::kFullRange,
174 color_range_enum_map_);
175 color_range_property_.AddEnumToMap("YCbCr limited range",
176 DrmHwcSampleRange::kLimitedRange,
177 color_range_enum_map_);
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300178 } else {
Matvii Zorin8338c342020-09-08 16:12:51 +0300179 ALOGI("Could not get COLOR_RANGE property");
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300180 }
Matvii Zorin8338c342020-09-08 16:12:51 +0300181 }
182
Sean Paul6a55e9f2015-04-30 15:31:06 -0400183 return 0;
184}
185
186uint32_t DrmPlane::id() const {
187 return id_;
188}
189
190bool DrmPlane::GetCrtcSupported(const DrmCrtc &crtc) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200191 return ((1 << crtc.pipe()) & possible_crtc_mask_) != 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400192}
193
Matvii Zorin67a89d32021-01-31 14:45:05 +0200194bool DrmPlane::IsValidForLayer(DrmHwcLayer *layer) {
Benjamin Lib00b28d2021-05-09 08:52:18 -0700195 if (rotation_property_.id() == 0) {
196 if (layer->transform != DrmHwcTransform::kIdentity) {
197 ALOGV("Rotation is not supported on plane %d", id_);
198 return false;
199 }
200 } else {
201 // For rotation checks, we assume the hardware reports its capabilities
202 // consistently (e.g. a 270 degree rotation is a 90 degree rotation + H
203 // flip + V flip; it wouldn't make sense to support all of the latter but
204 // not the former).
205 int ret = 0;
206 const std::pair<enum DrmHwcTransform, std::string> transforms[] =
207 {{kFlipH, "reflect-x"},
208 {kFlipV, "reflect-y"},
209 {kRotate90, "rotate-90"},
210 {kRotate180, "rotate-180"},
211 {kRotate270, "rotate-270"}};
212
213 for (const auto &[transform, name] : transforms) {
214 if (layer->transform & transform) {
215 std::tie(std::ignore,
216 ret) = rotation_property_.GetEnumValueWithName(name);
217 if (ret) {
218 ALOGV("Rotation '%s' is not supported on plane %d", name.c_str(),
219 id_);
220 return false;
221 }
222 }
223 }
Matvii Zorin67a89d32021-01-31 14:45:05 +0200224 }
225
226 if (alpha_property_.id() == 0 && layer->alpha != 0xffff) {
227 ALOGV("Alpha is not supported on plane %d", id_);
228 return false;
229 }
230
Roman Stratiienko5063d532021-09-29 12:47:31 +0300231 if (blending_enum_map_.count(layer->blending) == 0 &&
232 layer->blending != DrmHwcBlending::kNone) {
233 ALOGV("Blending is not supported on plane %d", id_);
234 return false;
Matvii Zorin67a89d32021-01-31 14:45:05 +0200235 }
236
Roman Stratiienko51287152021-02-10 14:59:52 +0200237 uint32_t format = layer->buffer_info.format;
Matvii Zorin67a89d32021-01-31 14:45:05 +0200238 if (!IsFormatSupported(format)) {
239 ALOGV("Plane %d does not supports %c%c%c%c format", id_, format,
240 format >> 8, format >> 16, format >> 24);
241 return false;
242 }
243
244 return true;
245}
246
Sean Paul6a55e9f2015-04-30 15:31:06 -0400247uint32_t DrmPlane::type() const {
248 return type_;
249}
250
Roman Kovalivskyi859b6072020-03-26 05:03:39 +0200251bool DrmPlane::IsFormatSupported(uint32_t format) const {
252 return std::find(std::begin(formats_), std::end(formats_), format) !=
253 std::end(formats_);
254}
255
Matvii Zorin8338c342020-09-08 16:12:51 +0300256bool DrmPlane::HasNonRgbFormat() const {
257 return std::find_if_not(std::begin(formats_), std::end(formats_),
258 [](uint32_t format) {
259 return BufferInfoGetter::IsDrmFormatRgb(format);
260 }) != std::end(formats_);
261}
262
Roman Stratiienko6662f712021-09-29 12:52:22 +0300263static uint64_t ToDrmRotation(DrmHwcTransform transform) {
264 uint64_t rotation = 0;
265 if (transform & DrmHwcTransform::kFlipH)
266 rotation |= DRM_MODE_REFLECT_X;
267 if (transform & DrmHwcTransform::kFlipV)
268 rotation |= DRM_MODE_REFLECT_Y;
269 if (transform & DrmHwcTransform::kRotate90)
270 rotation |= DRM_MODE_ROTATE_90;
271 else if (transform & DrmHwcTransform::kRotate180)
272 rotation |= DRM_MODE_ROTATE_180;
273 else if (transform & DrmHwcTransform::kRotate270)
274 rotation |= DRM_MODE_ROTATE_270;
275 else
276 rotation |= DRM_MODE_ROTATE_0;
277
278 return rotation;
279}
280
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300281auto DrmPlane::AtomicSetState(drmModeAtomicReq &pset, DrmHwcLayer &layer,
282 uint32_t zpos, uint32_t crtc_id) -> int {
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300283 if (!layer.FbIdHandle) {
284 ALOGE("Expected a valid framebuffer for pset");
285 return -EINVAL;
286 }
287
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300288 if (zpos_property_ && !zpos_property_.is_immutable()) {
289 uint64_t min_zpos = 0;
290
291 // Ignore ret and use min_zpos as 0 by default
292 std::tie(std::ignore, min_zpos) = zpos_property_.range_min();
293
294 if (!zpos_property_.AtomicSet(pset, zpos + min_zpos)) {
295 return -EINVAL;
296 }
297 }
298
Roman Stratiienko6662f712021-09-29 12:52:22 +0300299 if (layer.acquire_fence &&
300 !in_fence_fd_property_.AtomicSet(pset, layer.acquire_fence.Get())) {
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300301 return -EINVAL;
302 }
303
Roman Stratiienko6662f712021-09-29 12:52:22 +0300304 if (!crtc_property_.AtomicSet(pset, crtc_id) ||
305 !fb_property_.AtomicSet(pset, layer.FbIdHandle->GetFbId()) ||
306 !crtc_x_property_.AtomicSet(pset, layer.display_frame.left) ||
307 !crtc_y_property_.AtomicSet(pset, layer.display_frame.top) ||
308 !crtc_w_property_.AtomicSet(pset, layer.display_frame.right -
309 layer.display_frame.left) ||
310 !crtc_h_property_.AtomicSet(pset, layer.display_frame.bottom -
311 layer.display_frame.top) ||
312 !src_x_property_.AtomicSet(pset, (int)(layer.source_crop.left) << 16) ||
313 !src_y_property_.AtomicSet(pset, (int)(layer.source_crop.top) << 16) ||
314 !src_w_property_.AtomicSet(pset, (int)(layer.source_crop.right -
315 layer.source_crop.left)
316 << 16) ||
317 !src_h_property_.AtomicSet(pset, (int)(layer.source_crop.bottom -
318 layer.source_crop.top)
319 << 16)) {
320 return -EINVAL;
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300321 }
322
Roman Stratiienko6662f712021-09-29 12:52:22 +0300323 if (rotation_property_ &&
324 !rotation_property_.AtomicSet(pset, ToDrmRotation(layer.transform))) {
325 return -EINVAL;
326 }
327
328 if (alpha_property_ && !alpha_property_.AtomicSet(pset, layer.alpha)) {
329 return -EINVAL;
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300330 }
331
Roman Stratiienko5063d532021-09-29 12:47:31 +0300332 if (blending_enum_map_.count(layer.blending) != 0 &&
333 !blend_property_.AtomicSet(pset, blending_enum_map_[layer.blending])) {
334 return -EINVAL;
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300335 }
336
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300337 if (color_encoding_enum_map_.count(layer.color_space) != 0 &&
338 !color_encoding_propery_
339 .AtomicSet(pset, color_encoding_enum_map_[layer.color_space])) {
340 return -EINVAL;
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300341 }
342
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300343 if (color_range_enum_map_.count(layer.sample_range) != 0 &&
344 !color_range_property_
345 .AtomicSet(pset, color_range_enum_map_[layer.sample_range])) {
346 return -EINVAL;
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300347 }
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300348
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300349 return 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400350}
351
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300352auto DrmPlane::AtomicDisablePlane(drmModeAtomicReq &pset) -> int {
353 if (!crtc_property_.AtomicSet(pset, 0) || !fb_property_.AtomicSet(pset, 0)) {
354 return -EINVAL;
355 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400356
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300357 return 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400358}
Sean Paul1c4c3262015-07-14 15:51:52 -0400359
Alexandru Gheorgheea1c5e52018-09-17 10:48:54 +0100360const DrmProperty &DrmPlane::zpos_property() const {
361 return zpos_property_;
362}
363
Sean Paulf72cccd2018-08-27 13:59:08 -0400364} // namespace android