blob: f6ddad28b745a2d3cb165cd2e6a7d15178d19252 [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
136uint32_t DrmPlane::id() const {
137 return id_;
138}
139
140bool DrmPlane::GetCrtcSupported(const DrmCrtc &crtc) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200141 return ((1 << crtc.pipe()) & possible_crtc_mask_) != 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400142}
143
Matvii Zorin67a89d32021-01-31 14:45:05 +0200144bool DrmPlane::IsValidForLayer(DrmHwcLayer *layer) {
Roman Stratiienko24a4c182021-09-29 12:59:54 +0300145 if (!rotation_property_) {
Benjamin Lib00b28d2021-05-09 08:52:18 -0700146 if (layer->transform != DrmHwcTransform::kIdentity) {
Roman Stratiienko24a4c182021-09-29 12:59:54 +0300147 ALOGV("No rotation property on plane %d", id_);
Benjamin Lib00b28d2021-05-09 08:52:18 -0700148 return false;
149 }
150 } else {
Roman Stratiienko24a4c182021-09-29 12:59:54 +0300151 if (transform_enum_map_.count(layer->transform) == 0) {
152 ALOGV("Transform is not supported on plane %d", id_);
153 return false;
Benjamin Lib00b28d2021-05-09 08:52:18 -0700154 }
Matvii Zorin67a89d32021-01-31 14:45:05 +0200155 }
156
157 if (alpha_property_.id() == 0 && layer->alpha != 0xffff) {
158 ALOGV("Alpha is not supported on plane %d", id_);
159 return false;
160 }
161
Roman Stratiienko5063d532021-09-29 12:47:31 +0300162 if (blending_enum_map_.count(layer->blending) == 0 &&
Roman Stratiienko013d0682021-09-30 19:36:00 +0300163 layer->blending != DrmHwcBlending::kNone &&
164 layer->blending != DrmHwcBlending::kPreMult) {
Roman Stratiienko5063d532021-09-29 12:47:31 +0300165 ALOGV("Blending is not supported on plane %d", id_);
166 return false;
Matvii Zorin67a89d32021-01-31 14:45:05 +0200167 }
168
Roman Stratiienko51287152021-02-10 14:59:52 +0200169 uint32_t format = layer->buffer_info.format;
Matvii Zorin67a89d32021-01-31 14:45:05 +0200170 if (!IsFormatSupported(format)) {
171 ALOGV("Plane %d does not supports %c%c%c%c format", id_, format,
172 format >> 8, format >> 16, format >> 24);
173 return false;
174 }
175
176 return true;
177}
178
Sean Paul6a55e9f2015-04-30 15:31:06 -0400179uint32_t DrmPlane::type() const {
180 return type_;
181}
182
Roman Kovalivskyi859b6072020-03-26 05:03:39 +0200183bool DrmPlane::IsFormatSupported(uint32_t format) const {
184 return std::find(std::begin(formats_), std::end(formats_), format) !=
185 std::end(formats_);
186}
187
Matvii Zorin8338c342020-09-08 16:12:51 +0300188bool DrmPlane::HasNonRgbFormat() const {
189 return std::find_if_not(std::begin(formats_), std::end(formats_),
190 [](uint32_t format) {
191 return BufferInfoGetter::IsDrmFormatRgb(format);
192 }) != std::end(formats_);
193}
194
Roman Stratiienko6662f712021-09-29 12:52:22 +0300195static uint64_t ToDrmRotation(DrmHwcTransform transform) {
196 uint64_t rotation = 0;
197 if (transform & DrmHwcTransform::kFlipH)
198 rotation |= DRM_MODE_REFLECT_X;
199 if (transform & DrmHwcTransform::kFlipV)
200 rotation |= DRM_MODE_REFLECT_Y;
201 if (transform & DrmHwcTransform::kRotate90)
202 rotation |= DRM_MODE_ROTATE_90;
203 else if (transform & DrmHwcTransform::kRotate180)
204 rotation |= DRM_MODE_ROTATE_180;
205 else if (transform & DrmHwcTransform::kRotate270)
206 rotation |= DRM_MODE_ROTATE_270;
207 else
208 rotation |= DRM_MODE_ROTATE_0;
209
210 return rotation;
211}
212
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300213auto DrmPlane::AtomicSetState(drmModeAtomicReq &pset, DrmHwcLayer &layer,
214 uint32_t zpos, uint32_t crtc_id) -> int {
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300215 if (!layer.FbIdHandle) {
216 ALOGE("Expected a valid framebuffer for pset");
217 return -EINVAL;
218 }
219
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300220 if (zpos_property_ && !zpos_property_.is_immutable()) {
221 uint64_t min_zpos = 0;
222
223 // Ignore ret and use min_zpos as 0 by default
224 std::tie(std::ignore, min_zpos) = zpos_property_.range_min();
225
226 if (!zpos_property_.AtomicSet(pset, zpos + min_zpos)) {
227 return -EINVAL;
228 }
229 }
230
Roman Stratiienko6662f712021-09-29 12:52:22 +0300231 if (layer.acquire_fence &&
232 !in_fence_fd_property_.AtomicSet(pset, layer.acquire_fence.Get())) {
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300233 return -EINVAL;
234 }
235
Roman Stratiienko6662f712021-09-29 12:52:22 +0300236 if (!crtc_property_.AtomicSet(pset, crtc_id) ||
237 !fb_property_.AtomicSet(pset, layer.FbIdHandle->GetFbId()) ||
238 !crtc_x_property_.AtomicSet(pset, layer.display_frame.left) ||
239 !crtc_y_property_.AtomicSet(pset, layer.display_frame.top) ||
240 !crtc_w_property_.AtomicSet(pset, layer.display_frame.right -
241 layer.display_frame.left) ||
242 !crtc_h_property_.AtomicSet(pset, layer.display_frame.bottom -
243 layer.display_frame.top) ||
244 !src_x_property_.AtomicSet(pset, (int)(layer.source_crop.left) << 16) ||
245 !src_y_property_.AtomicSet(pset, (int)(layer.source_crop.top) << 16) ||
246 !src_w_property_.AtomicSet(pset, (int)(layer.source_crop.right -
247 layer.source_crop.left)
248 << 16) ||
249 !src_h_property_.AtomicSet(pset, (int)(layer.source_crop.bottom -
250 layer.source_crop.top)
251 << 16)) {
252 return -EINVAL;
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300253 }
254
Roman Stratiienko6662f712021-09-29 12:52:22 +0300255 if (rotation_property_ &&
256 !rotation_property_.AtomicSet(pset, ToDrmRotation(layer.transform))) {
257 return -EINVAL;
258 }
259
260 if (alpha_property_ && !alpha_property_.AtomicSet(pset, layer.alpha)) {
261 return -EINVAL;
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300262 }
263
Roman Stratiienko5063d532021-09-29 12:47:31 +0300264 if (blending_enum_map_.count(layer.blending) != 0 &&
265 !blend_property_.AtomicSet(pset, blending_enum_map_[layer.blending])) {
266 return -EINVAL;
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300267 }
268
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300269 if (color_encoding_enum_map_.count(layer.color_space) != 0 &&
270 !color_encoding_propery_
271 .AtomicSet(pset, color_encoding_enum_map_[layer.color_space])) {
272 return -EINVAL;
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300273 }
274
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300275 if (color_range_enum_map_.count(layer.sample_range) != 0 &&
276 !color_range_property_
277 .AtomicSet(pset, color_range_enum_map_[layer.sample_range])) {
278 return -EINVAL;
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300279 }
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300280
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300281 return 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400282}
283
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300284auto DrmPlane::AtomicDisablePlane(drmModeAtomicReq &pset) -> int {
285 if (!crtc_property_.AtomicSet(pset, 0) || !fb_property_.AtomicSet(pset, 0)) {
286 return -EINVAL;
287 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400288
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300289 return 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400290}
Sean Paul1c4c3262015-07-14 15:51:52 -0400291
Alexandru Gheorgheea1c5e52018-09-17 10:48:54 +0100292const DrmProperty &DrmPlane::zpos_property() const {
293 return zpos_property_;
294}
295
Roman Stratiienko0b063882021-09-30 10:15:05 +0300296auto DrmPlane::GetPlaneProperty(const char *prop_name, DrmProperty &property,
297 Presence presence) -> bool {
298 int err = drm_->GetProperty(id_, DRM_MODE_OBJECT_PLANE, prop_name, &property);
299 if (err != 0) {
300 if (presence == Presence::kMandatory) {
301 ALOGE("Could not get mandatory property \"%s\" from plane %d", prop_name,
302 id_);
303 } else {
304 ALOGV("Could not get optional property \"%s\" from plane %d", prop_name,
305 id_);
306 }
307 return false;
308 }
309
310 return true;
311}
312
Sean Paulf72cccd2018-08-27 13:59:08 -0400313} // namespace android